MDL-35963: Assignment - do not accept empty submissions
[moodle.git] / course / category.php
blobcb1d0c79fa5f422558d5b0b9d8c56b94d15d7efd
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Displays the top level category or all courses
19 * In editing mode, allows the admin to edit a category,
20 * and rearrange courses
22 * @package core
23 * @subpackage course
24 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 require_once("../config.php");
29 require_once($CFG->dirroot.'/course/lib.php');
30 require_once($CFG->libdir.'/textlib.class.php');
32 $id = required_param('id', PARAM_INT); // Category id
33 $page = optional_param('page', 0, PARAM_INT); // which page to show
34 $categoryedit = optional_param('categoryedit', -1, PARAM_BOOL);
35 $hide = optional_param('hide', 0, PARAM_INT);
36 $show = optional_param('show', 0, PARAM_INT);
37 $moveup = optional_param('moveup', 0, PARAM_INT);
38 $movedown = optional_param('movedown', 0, PARAM_INT);
39 $moveto = optional_param('moveto', 0, PARAM_INT);
40 $resort = optional_param('resort', 0, PARAM_BOOL);
41 $sesskey = optional_param('sesskey', '', PARAM_RAW);
43 // MDL-27824 - This is a temporary fix until we have the proper
44 // way to check/initialize $CFG value.
45 // @todo MDL-35138 remove this temporary solution
46 if (!empty($CFG->coursesperpage)) {
47 $defaultperpage = $CFG->coursesperpage;
48 } else {
49 $defaultperpage = 20;
51 $perpage = optional_param('perpage', $defaultperpage, PARAM_INT); // how many per page
53 if (empty($id)) {
54 print_error("unknowcategory");
57 $PAGE->set_category_by_id($id);
58 $PAGE->set_url(new moodle_url('/course/category.php', array('id' => $id)));
59 // This is sure to be the category context
60 $context = $PAGE->context;
61 // And the object has been loaded for us no need for another DB call
62 $category = $PAGE->category;
64 $canedit = can_edit_in_category($category->id);
65 if ($canedit) {
66 if ($categoryedit !== -1) {
67 $USER->editing = $categoryedit;
69 require_login();
70 $editingon = $PAGE->user_is_editing();
71 } else {
72 if ($CFG->forcelogin) {
73 require_login();
75 $editingon = false;
78 if (!$category->visible) {
79 require_capability('moodle/category:viewhiddencategories', $context);
82 $canmanage = has_capability('moodle/category:manage', $context);
83 $sesskeyprovided = !empty($sesskey) && confirm_sesskey($sesskey);
85 // Process any category actions.
86 if ($canmanage && $resort && $sesskeyprovided) {
87 // Resort the category if requested
88 if ($courses = get_courses($category->id, '', 'c.id,c.fullname,c.sortorder')) {
89 collatorlib::asort_objects_by_property($courses, 'fullname', collatorlib::SORT_NATURAL);
90 $i = 1;
91 foreach ($courses as $course) {
92 $DB->set_field('course', 'sortorder', $category->sortorder+$i, array('id'=>$course->id));
93 $i++;
95 fix_course_sortorder(); // should not be needed
99 // Process any course actions.
100 if ($editingon && $sesskeyprovided) {
102 // Move a specified course to a new category
103 if (!empty($moveto) and $data = data_submitted()) {
104 // Some courses are being moved
105 // user must have category update in both cats to perform this
106 require_capability('moodle/category:manage', $context);
107 require_capability('moodle/category:manage', get_context_instance(CONTEXT_COURSECAT, $moveto));
109 if (!$destcategory = $DB->get_record('course_categories', array('id' => $data->moveto))) {
110 print_error('cannotfindcategory', '', '', $data->moveto);
113 $courses = array();
114 foreach ($data as $key => $value) {
115 if (preg_match('/^c\d+$/', $key)) {
116 $courseid = substr($key, 1);
117 array_push($courses, $courseid);
119 // check this course's category
120 if ($movingcourse = $DB->get_record('course', array('id'=>$courseid))) {
121 if ($movingcourse->category != $id ) {
122 print_error('coursedoesnotbelongtocategory');
124 } else {
125 print_error('cannotfindcourse');
129 move_courses($courses, $data->moveto);
132 // Hide or show a course
133 if (!empty($hide) or !empty($show)) {
134 if (!empty($hide)) {
135 $course = $DB->get_record('course', array('id' => $hide));
136 $visible = 0;
137 } else {
138 $course = $DB->get_record('course', array('id' => $show));
139 $visible = 1;
142 if ($course) {
143 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
144 require_capability('moodle/course:visibility', $coursecontext);
145 // Set the visibility of the course. we set the old flag when user manually changes visibility of course.
146 $DB->update_record('course', array('id' => $course->id, 'visible' => $visible, 'visibleold' => $visible, 'timemodified' => time()));
151 // Move a course up or down
152 if (!empty($moveup) or !empty($movedown)) {
153 require_capability('moodle/category:manage', $context);
155 // Ensure the course order has continuous ordering
156 fix_course_sortorder();
157 $swapcourse = NULL;
159 if (!empty($moveup)) {
160 if ($movecourse = $DB->get_record('course', array('id' => $moveup))) {
161 $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder - 1));
163 } else {
164 if ($movecourse = $DB->get_record('course', array('id' => $movedown))) {
165 $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder + 1));
168 if ($swapcourse and $movecourse) {
169 // check course's category
170 if ($movecourse->category != $id) {
171 print_error('coursedoesnotbelongtocategory');
173 $DB->set_field('course', 'sortorder', $swapcourse->sortorder, array('id' => $movecourse->id));
174 $DB->set_field('course', 'sortorder', $movecourse->sortorder, array('id' => $swapcourse->id));
178 } // End of editing stuff
180 // Prepare the standard URL params for this page. We'll need them later.
181 $urlparams = array('id' => $id);
182 if ($page) {
183 $urlparams['page'] = $page;
185 if ($perpage) {
186 $urlparams['perpage'] = $perpage;
189 // Begin output
190 if ($editingon && can_edit_in_category()) {
191 // Integrate into the admin tree only if the user can edit categories at the top level,
192 // otherwise the admin block does not appear to this user, and you get an error.
193 require_once($CFG->libdir . '/adminlib.php');
194 admin_externalpage_setup('coursemgmt', '', $urlparams, $CFG->wwwroot . '/course/category.php');
195 $PAGE->set_context($context); // Ensure that we are actually showing blocks etc for the cat context
197 $settingsnode = $PAGE->settingsnav->find_active_node();
198 if ($settingsnode) {
199 $settingsnode->make_inactive();
200 $settingsnode->force_open();
201 $PAGE->navbar->add($settingsnode->text, $settingsnode->action);
203 echo $OUTPUT->header();
204 } else {
205 $site = get_site();
206 $PAGE->set_title("$site->shortname: $category->name");
207 $PAGE->set_heading($site->fullname);
208 $PAGE->set_button(print_course_search('', true, 'navbar'));
209 $PAGE->set_pagelayout('coursecategory');
210 echo $OUTPUT->header();
213 /// Print the category selector
214 $displaylist = array();
215 $notused = array();
216 make_categories_list($displaylist, $notused);
218 echo '<div class="categorypicker">';
219 $select = new single_select(new moodle_url('/course/category.php'), 'id', $displaylist, $category->id, null, 'switchcategory');
220 $select->set_label(get_string('categories').':');
221 echo $OUTPUT->render($select);
222 echo '</div>';
224 /// Print current category description
225 if (!$editingon && $category->description) {
226 echo $OUTPUT->box_start();
227 $options = new stdClass;
228 $options->noclean = true;
229 $options->para = false;
230 $options->overflowdiv = true;
231 if (!isset($category->descriptionformat)) {
232 $category->descriptionformat = FORMAT_MOODLE;
234 $text = file_rewrite_pluginfile_urls($category->description, 'pluginfile.php', $context->id, 'coursecat', 'description', null);
235 echo format_text($text, $category->descriptionformat, $options);
236 echo $OUTPUT->box_end();
239 if ($editingon && $canmanage) {
240 echo $OUTPUT->container_start('buttons');
242 // Print button to update this category
243 $url = new moodle_url('/course/editcategory.php', array('id' => $category->id));
244 echo $OUTPUT->single_button($url, get_string('editcategorythis'), 'get');
246 // Print button for creating new categories
247 $url = new moodle_url('/course/editcategory.php', array('parent' => $category->id));
248 echo $OUTPUT->single_button($url, get_string('addsubcategory'), 'get');
250 echo $OUTPUT->container_end();
253 // Print out all the sub-categories
254 // In order to view hidden subcategories the user must have the viewhiddencategories
255 // capability in the current category.
256 if (has_capability('moodle/category:viewhiddencategories', $context)) {
257 $categorywhere = '';
258 } else {
259 $categorywhere = 'AND cc.visible = 1';
261 // We're going to preload the context for the subcategory as we know that we
262 // need it later on for formatting.
264 $ctxselect = context_helper::get_preload_record_columns_sql('ctx');
265 $sql = "SELECT cc.*, $ctxselect
266 FROM {course_categories} cc
267 JOIN {context} ctx ON cc.id = ctx.instanceid
268 WHERE cc.parent = :parentid AND
269 ctx.contextlevel = :contextlevel
270 $categorywhere
271 ORDER BY cc.sortorder ASC";
272 $subcategories = $DB->get_recordset_sql($sql, array('parentid' => $category->id, 'contextlevel' => CONTEXT_COURSECAT));
273 // Prepare a table to display the sub categories.
274 $table = new html_table;
275 $table->attributes = array('border' => '0', 'cellspacing' => '2', 'cellpadding' => '4', 'class' => 'generalbox boxaligncenter category_subcategories');
276 $table->head = array(new lang_string('subcategories'));
277 $table->data = array();
278 $baseurl = new moodle_url('/course/category.php');
279 foreach ($subcategories as $subcategory) {
280 // Preload the context we will need it to format the category name shortly.
281 context_helper::preload_from_record($subcategory);
282 $context = get_context_instance(CONTEXT_COURSECAT, $subcategory->id);
283 // Prepare the things we need to create a link to the subcategory
284 $attributes = $subcategory->visible ? array() : array('class' => 'dimmed');
285 $text = format_string($subcategory->name, true, array('context' => $context));
286 // Add the subcategory to the table
287 $baseurl->param('id', $subcategory->id);
288 $table->data[] = array(html_writer::link($baseurl, $text, $attributes));
291 $subcategorieswereshown = (count($table->data) > 0);
292 if ($subcategorieswereshown) {
293 echo html_writer::table($table);
296 // Print out all the courses
297 $courses = get_courses_page($category->id, 'c.sortorder ASC',
298 'c.id,c.sortorder,c.shortname,c.fullname,c.summary,c.visible',
299 $totalcount, $page*$perpage, $perpage);
300 $numcourses = count($courses);
302 if (!$courses) {
303 if (empty($subcategorieswereshown)) {
304 echo $OUTPUT->heading(get_string("nocoursesyet"));
307 } else if ($numcourses <= COURSE_MAX_SUMMARIES_PER_PAGE and !$page and !$editingon) {
308 echo $OUTPUT->box_start('courseboxes');
309 print_courses($category);
310 echo $OUTPUT->box_end();
312 } else {
313 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "/course/category.php?id=$category->id&perpage=$perpage");
315 echo '<form id="movecourses" action="category.php" method="post"><div>';
316 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
317 echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter"><tr>';
318 echo '<th class="header" scope="col">'.get_string('courses').'</th>';
319 if ($editingon) {
320 echo '<th class="header" scope="col">'.get_string('edit').'</th>';
321 echo '<th class="header" scope="col">'.get_string('select').'</th>';
322 } else {
323 echo '<th class="header" scope="col">&nbsp;</th>';
325 echo '</tr>';
327 $count = 0;
328 $abletomovecourses = false; // for now
330 // Checking if we are at the first or at the last page, to allow courses to
331 // be moved up and down beyond the paging border
332 if ($totalcount > $perpage) {
333 $atfirstpage = ($page == 0);
334 if ($perpage > 0) {
335 $atlastpage = (($page + 1) == ceil($totalcount / $perpage));
336 } else {
337 $atlastpage = true;
339 } else {
340 $atfirstpage = true;
341 $atlastpage = true;
344 $baseurl = new moodle_url('/course/category.php', $urlparams + array('sesskey' => sesskey()));
345 foreach ($courses as $acourse) {
346 $coursecontext = get_context_instance(CONTEXT_COURSE, $acourse->id);
348 $count++;
349 $up = ($count > 1 || !$atfirstpage);
350 $down = ($count < $numcourses || !$atlastpage);
352 $linkcss = $acourse->visible ? '' : ' class="dimmed" ';
353 echo '<tr>';
354 $coursename = get_course_display_name_for_list($acourse);
355 echo '<td><a '.$linkcss.' href="view.php?id='.$acourse->id.'">'. format_string($coursename) .'</a></td>';
356 if ($editingon) {
357 echo '<td>';
358 if (has_capability('moodle/course:update', $coursecontext)) {
359 $url = new moodle_url('/course/edit.php', array('id' => $acourse->id, 'category' => $id, 'returnto' => 'category'));
360 echo $OUTPUT->action_icon($url, new pix_icon('t/edit', get_string('settings')));
363 // role assignment link
364 if (has_capability('moodle/course:enrolreview', $coursecontext)) {
365 $url = new moodle_url('/enrol/users.php', array('id' => $acourse->id));
366 echo $OUTPUT->action_icon($url, new pix_icon('i/users', get_string('enrolledusers', 'enrol')));
369 if (can_delete_course($acourse->id)) {
370 $url = new moodle_url('/course/delete.php', array('id' => $acourse->id));
371 echo $OUTPUT->action_icon($url, new pix_icon('t/delete', get_string('delete')));
374 // MDL-8885, users with no capability to view hidden courses, should not be able to lock themselves out
375 if (has_capability('moodle/course:visibility', $coursecontext) && has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
376 if (!empty($acourse->visible)) {
377 $url = new moodle_url($baseurl, array('hide' => $acourse->id));
378 echo $OUTPUT->action_icon($url, new pix_icon('t/hide', get_string('hide')));
379 } else {
380 $url = new moodle_url($baseurl, array('show' => $acourse->id));
381 echo $OUTPUT->action_icon($url, new pix_icon('t/show', get_string('show')));
385 if (has_capability('moodle/backup:backupcourse', $coursecontext)) {
386 $url = new moodle_url('/backup/backup.php', array('id' => $acourse->id));
387 echo $OUTPUT->action_icon($url, new pix_icon('t/backup', get_string('backup')));
390 if (has_capability('moodle/restore:restorecourse', $coursecontext)) {
391 $url = new moodle_url('/backup/restorefile.php', array('contextid' => $coursecontext->id));
392 echo $OUTPUT->action_icon($url, new pix_icon('t/restore', get_string('restore')));
395 if ($canmanage) {
396 if ($up) {
397 $url = new moodle_url($baseurl, array('moveup' => $acourse->id));
398 echo $OUTPUT->action_icon($url, new pix_icon('t/up', get_string('moveup')));
401 if ($down) {
402 $url = new moodle_url($baseurl, array('movedown' => $acourse->id));
403 echo $OUTPUT->action_icon($url, new pix_icon('t/down', get_string('movedown')));
405 $abletomovecourses = true;
408 echo '</td>';
409 echo '<td align="center">';
410 echo '<input type="checkbox" name="c'.$acourse->id.'" />';
411 echo '</td>';
412 } else {
413 echo '<td align="right">';
414 // print enrol info
415 if ($icons = enrol_get_course_info_icons($acourse)) {
416 foreach ($icons as $pix_icon) {
417 echo $OUTPUT->render($pix_icon);
420 if (!empty($acourse->summary)) {
421 $url = new moodle_url("/course/info.php?id=$acourse->id");
422 echo $OUTPUT->action_link($url, '<img alt="'.get_string('info').'" class="icon" src="'.$OUTPUT->pix_url('i/info') . '" />',
423 new popup_action('click', $url, 'courseinfo'), array('title'=>get_string('summary')));
425 echo "</td>";
427 echo "</tr>";
430 if ($abletomovecourses) {
431 $movetocategories = array();
432 $notused = array();
433 make_categories_list($movetocategories, $notused, 'moodle/category:manage');
434 $movetocategories[$category->id] = get_string('moveselectedcoursesto');
435 echo '<tr><td colspan="3" align="right">';
436 echo html_writer::label(get_string('moveselectedcoursesto'), 'movetoid', false, array('class' => 'accesshide'));
437 echo html_writer::select($movetocategories, 'moveto', $category->id, null, array('id'=>'movetoid'));
438 $PAGE->requires->js_init_call('M.util.init_select_autosubmit', array('movecourses', 'movetoid', false));
439 echo '<input type="hidden" name="id" value="'.$category->id.'" />';
440 echo '</td></tr>';
443 echo '</table>';
444 echo '</div></form>';
445 echo '<br />';
448 echo '<div class="buttons">';
449 if ($canmanage and $numcourses > 1) {
450 // Print button to re-sort courses by name
451 $url = new moodle_url('/course/category.php', array('id' => $category->id, 'resort' => 'name', 'sesskey' => sesskey()));
452 echo $OUTPUT->single_button($url, get_string('resortcoursesbyname'), 'get');
455 if (has_capability('moodle/course:create', $context)) {
456 // Print button to create a new course
457 $url = new moodle_url('/course/edit.php', array('category' => $category->id, 'returnto' => 'category'));
458 echo $OUTPUT->single_button($url, get_string('addnewcourse'), 'get');
461 if (!empty($CFG->enablecourserequests) && $category->id == $CFG->defaultrequestcategory) {
462 print_course_request_buttons(get_context_instance(CONTEXT_SYSTEM));
464 echo '</div>';
466 print_course_search();
468 echo $OUTPUT->footer();