MDL-33502 restore: there are no files with itemid based on section numbers.
[moodle.git] / course / category.php
blob935b83a20437910bb25ade9cd304a1b5abf40ddd
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');
31 $id = required_param('id', PARAM_INT); // Category id
32 $page = optional_param('page', 0, PARAM_INT); // which page to show
33 $perpage = optional_param('perpage', $CFG->coursesperpage, PARAM_INT); // how many per page
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 if (empty($id)) {
44 print_error("unknowcategory");
47 $PAGE->set_category_by_id($id);
48 $PAGE->set_url(new moodle_url('/course/category.php', array('id' => $id)));
49 // This is sure to be the category context
50 $context = $PAGE->context;
51 // And the object has been loaded for us no need for another DB call
52 $category = $PAGE->category;
54 $canedit = can_edit_in_category($category->id);
55 if ($canedit) {
56 if ($categoryedit !== -1) {
57 $USER->editing = $categoryedit;
59 require_login();
60 $editingon = $PAGE->user_is_editing();
61 } else {
62 if ($CFG->forcelogin) {
63 require_login();
65 $editingon = false;
68 if (!$category->visible) {
69 require_capability('moodle/category:viewhiddencategories', $context);
72 $canmanage = has_capability('moodle/category:manage', $context);
73 $sesskeyprovided = !empty($sesskey) && confirm_sesskey($sesskey);
75 // Process any category actions.
76 if ($canmanage && $resort && $sesskeyprovided) {
77 // Resort the category if requested
78 if ($courses = get_courses($category->id, "fullname ASC", 'c.id,c.fullname,c.sortorder')) {
79 $i = 1;
80 foreach ($courses as $course) {
81 $DB->set_field('course', 'sortorder', $category->sortorder+$i, array('id'=>$course->id));
82 $i++;
84 fix_course_sortorder(); // should not be needed
88 // Process any course actions.
89 if ($editingon && $sesskeyprovided) {
91 // Move a specified course to a new category
92 if (!empty($moveto) and $data = data_submitted()) {
93 // Some courses are being moved
94 // user must have category update in both cats to perform this
95 require_capability('moodle/category:manage', $context);
96 require_capability('moodle/category:manage', get_context_instance(CONTEXT_COURSECAT, $moveto));
98 if (!$destcategory = $DB->get_record('course_categories', array('id' => $data->moveto))) {
99 print_error('cannotfindcategory', '', '', $data->moveto);
102 $courses = array();
103 foreach ($data as $key => $value) {
104 if (preg_match('/^c\d+$/', $key)) {
105 $courseid = substr($key, 1);
106 array_push($courses, $courseid);
108 // check this course's category
109 if ($movingcourse = $DB->get_record('course', array('id'=>$courseid))) {
110 if ($movingcourse->category != $id ) {
111 print_error('coursedoesnotbelongtocategory');
113 } else {
114 print_error('cannotfindcourse');
118 move_courses($courses, $data->moveto);
121 // Hide or show a course
122 if (!empty($hide) or !empty($show)) {
123 if (!empty($hide)) {
124 $course = $DB->get_record('course', array('id' => $hide));
125 $visible = 0;
126 } else {
127 $course = $DB->get_record('course', array('id' => $show));
128 $visible = 1;
131 if ($course) {
132 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
133 require_capability('moodle/course:visibility', $coursecontext);
134 // Set the visibility of the course. we set the old flag when user manually changes visibility of course.
135 $DB->update_record('course', array('id' => $course->id, 'visible' => $visible, 'visibleold' => $visible, 'timemodified' => time()));
140 // Move a course up or down
141 if (!empty($moveup) or !empty($movedown)) {
142 require_capability('moodle/category:manage', $context);
144 // Ensure the course order has continuous ordering
145 fix_course_sortorder();
146 $swapcourse = NULL;
148 if (!empty($moveup)) {
149 if ($movecourse = $DB->get_record('course', array('id' => $moveup))) {
150 $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder - 1));
152 } else {
153 if ($movecourse = $DB->get_record('course', array('id' => $movedown))) {
154 $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder + 1));
157 if ($swapcourse and $movecourse) {
158 // check course's category
159 if ($movecourse->category != $id) {
160 print_error('coursedoesnotbelongtocategory');
162 $DB->set_field('course', 'sortorder', $swapcourse->sortorder, array('id' => $movecourse->id));
163 $DB->set_field('course', 'sortorder', $movecourse->sortorder, array('id' => $swapcourse->id));
167 } // End of editing stuff
169 // Prepare the standard URL params for this page. We'll need them later.
170 $urlparams = array('id' => $id);
171 if ($page) {
172 $urlparams['page'] = $page;
174 if ($perpage) {
175 $urlparams['perpage'] = $perpage;
178 // Begin output
179 if ($editingon && can_edit_in_category()) {
180 // Integrate into the admin tree only if the user can edit categories at the top level,
181 // otherwise the admin block does not appear to this user, and you get an error.
182 require_once($CFG->libdir . '/adminlib.php');
183 admin_externalpage_setup('coursemgmt', '', $urlparams, $CFG->wwwroot . '/course/category.php');
184 $PAGE->set_context($context); // Ensure that we are actually showing blocks etc for the cat context
186 $settingsnode = $PAGE->settingsnav->find_active_node();
187 if ($settingsnode) {
188 $settingsnode->make_inactive();
189 $settingsnode->force_open();
190 $PAGE->navbar->add($settingsnode->text, $settingsnode->action);
192 echo $OUTPUT->header();
193 } else {
194 $site = get_site();
195 $PAGE->set_title("$site->shortname: $category->name");
196 $PAGE->set_heading($site->fullname);
197 $PAGE->set_button(print_course_search('', true, 'navbar'));
198 $PAGE->set_pagelayout('coursecategory');
199 echo $OUTPUT->header();
202 /// Print the category selector
203 $displaylist = array();
204 $notused = array();
205 make_categories_list($displaylist, $notused);
207 echo '<div class="categorypicker">';
208 $select = new single_select(new moodle_url('/course/category.php'), 'id', $displaylist, $category->id, null, 'switchcategory');
209 $select->set_label(get_string('categories').':');
210 echo $OUTPUT->render($select);
211 echo '</div>';
213 /// Print current category description
214 if (!$editingon && $category->description) {
215 echo $OUTPUT->box_start();
216 $options = new stdClass;
217 $options->noclean = true;
218 $options->para = false;
219 $options->overflowdiv = true;
220 if (!isset($category->descriptionformat)) {
221 $category->descriptionformat = FORMAT_MOODLE;
223 $text = file_rewrite_pluginfile_urls($category->description, 'pluginfile.php', $context->id, 'coursecat', 'description', null);
224 echo format_text($text, $category->descriptionformat, $options);
225 echo $OUTPUT->box_end();
228 if ($editingon && $canmanage) {
229 echo $OUTPUT->container_start('buttons');
231 // Print button to update this category
232 $url = new moodle_url('/course/editcategory.php', array('id' => $category->id));
233 echo $OUTPUT->single_button($url, get_string('editcategorythis'), 'get');
235 // Print button for creating new categories
236 $url = new moodle_url('/course/editcategory.php', array('parent' => $category->id));
237 echo $OUTPUT->single_button($url, get_string('addsubcategory'), 'get');
239 echo $OUTPUT->container_end();
242 // Print out all the sub-categories
243 // In order to view hidden subcategories the user must have the viewhiddencategories
244 // capability in the current category.
245 if (has_capability('moodle/category:viewhiddencategories', $context)) {
246 $categorywhere = '';
247 } else {
248 $categorywhere = 'AND cc.visible = 1';
250 // We're going to preload the context for the subcategory as we know that we
251 // need it later on for formatting.
253 $ctxselect = context_helper::get_preload_record_columns_sql('ctx');
254 $sql = "SELECT cc.*, $ctxselect
255 FROM {course_categories} cc
256 JOIN {context} ctx ON cc.id = ctx.instanceid
257 WHERE cc.parent = :parentid AND
258 ctx.contextlevel = :contextlevel
259 $categorywhere
260 ORDER BY cc.sortorder ASC";
261 $subcategories = $DB->get_recordset_sql($sql, array('parentid' => $category->id, 'contextlevel' => CONTEXT_COURSECAT));
262 // Prepare a table to display the sub categories.
263 $table = new html_table;
264 $table->attributes = array('border' => '0', 'cellspacing' => '2', 'cellpadding' => '4', 'class' => 'generalbox boxaligncenter category_subcategories');
265 $table->head = array(new lang_string('subcategories'));
266 $table->data = array();
267 $baseurl = new moodle_url('/course/category.php');
268 foreach ($subcategories as $subcategory) {
269 // Preload the context we will need it to format the category name shortly.
270 context_helper::preload_from_record($subcategory);
271 $context = get_context_instance(CONTEXT_COURSECAT, $subcategory->id);
272 // Prepare the things we need to create a link to the subcategory
273 $attributes = $subcategory->visible ? array() : array('class' => 'dimmed');
274 $text = format_string($subcategory->name, true, array('context' => $context));
275 // Add the subcategory to the table
276 $baseurl->param('id', $subcategory->id);
277 $table->data[] = array(html_writer::link($baseurl, $text, $attributes));
280 $subcategorieswereshown = (count($table->data) > 0);
281 if ($subcategorieswereshown) {
282 echo html_writer::table($table);
285 // Print out all the courses
286 $courses = get_courses_page($category->id, 'c.sortorder ASC',
287 'c.id,c.sortorder,c.shortname,c.fullname,c.summary,c.visible',
288 $totalcount, $page*$perpage, $perpage);
289 $numcourses = count($courses);
291 if (!$courses) {
292 if (empty($subcategorieswereshown)) {
293 echo $OUTPUT->heading(get_string("nocoursesyet"));
296 } else if ($numcourses <= COURSE_MAX_SUMMARIES_PER_PAGE and !$page and !$editingon) {
297 echo $OUTPUT->box_start('courseboxes');
298 print_courses($category);
299 echo $OUTPUT->box_end();
301 } else {
302 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "/course/category.php?id=$category->id&perpage=$perpage");
304 echo '<form id="movecourses" action="category.php" method="post"><div>';
305 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
306 echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter"><tr>';
307 echo '<th class="header" scope="col">'.get_string('courses').'</th>';
308 if ($editingon) {
309 echo '<th class="header" scope="col">'.get_string('edit').'</th>';
310 echo '<th class="header" scope="col">'.get_string('select').'</th>';
311 } else {
312 echo '<th class="header" scope="col">&nbsp;</th>';
314 echo '</tr>';
316 $count = 0;
317 $abletomovecourses = false; // for now
319 // Checking if we are at the first or at the last page, to allow courses to
320 // be moved up and down beyond the paging border
321 if ($totalcount > $perpage) {
322 $atfirstpage = ($page == 0);
323 if ($perpage > 0) {
324 $atlastpage = (($page + 1) == ceil($totalcount / $perpage));
325 } else {
326 $atlastpage = true;
328 } else {
329 $atfirstpage = true;
330 $atlastpage = true;
333 $baseurl = new moodle_url('/course/category.php', $urlparams + array('sesskey' => sesskey()));
334 foreach ($courses as $acourse) {
335 $coursecontext = get_context_instance(CONTEXT_COURSE, $acourse->id);
337 $count++;
338 $up = ($count > 1 || !$atfirstpage);
339 $down = ($count < $numcourses || !$atlastpage);
341 $linkcss = $acourse->visible ? '' : ' class="dimmed" ';
342 echo '<tr>';
343 $coursename = get_course_display_name_for_list($acourse);
344 echo '<td><a '.$linkcss.' href="view.php?id='.$acourse->id.'">'. format_string($coursename) .'</a></td>';
345 if ($editingon) {
346 echo '<td>';
347 if (has_capability('moodle/course:update', $coursecontext)) {
348 $url = new moodle_url('/course/edit.php', array('id' => $acourse->id, 'category' => $id, 'returnto' => 'category'));
349 echo $OUTPUT->action_icon($url, new pix_icon('t/edit', get_string('settings')));
352 // role assignment link
353 if (has_capability('moodle/course:enrolreview', $coursecontext)) {
354 $url = new moodle_url('/enrol/users.php', array('id' => $acourse->id));
355 echo $OUTPUT->action_icon($url, new pix_icon('i/users', get_string('enrolledusers', 'enrol')));
358 if (can_delete_course($acourse->id)) {
359 $url = new moodle_url('/course/delete.php', array('id' => $acourse->id));
360 echo $OUTPUT->action_icon($url, new pix_icon('t/delete', get_string('delete')));
363 // MDL-8885, users with no capability to view hidden courses, should not be able to lock themselves out
364 if (has_capability('moodle/course:visibility', $coursecontext) && has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
365 if (!empty($acourse->visible)) {
366 $url = new moodle_url($baseurl, array('hide' => $acourse->id));
367 echo $OUTPUT->action_icon($url, new pix_icon('t/hide', get_string('hide')));
368 } else {
369 $url = new moodle_url($baseurl, array('show' => $acourse->id));
370 echo $OUTPUT->action_icon($url, new pix_icon('t/show', get_string('show')));
374 if (has_capability('moodle/backup:backupcourse', $coursecontext)) {
375 $url = new moodle_url('/backup/backup.php', array('id' => $acourse->id));
376 echo $OUTPUT->action_icon($url, new pix_icon('t/backup', get_string('backup')));
379 if (has_capability('moodle/restore:restorecourse', $coursecontext)) {
380 $url = new moodle_url('/backup/restorefile.php', array('contextid' => $coursecontext->id));
381 echo $OUTPUT->action_icon($url, new pix_icon('t/restore', get_string('restore')));
384 if ($canmanage) {
385 if ($up) {
386 $url = new moodle_url($baseurl, array('moveup' => $acourse->id));
387 echo $OUTPUT->action_icon($url, new pix_icon('t/up', get_string('moveup')));
390 if ($down) {
391 $url = new moodle_url($baseurl, array('movedown' => $acourse->id));
392 echo $OUTPUT->action_icon($url, new pix_icon('t/down', get_string('movedown')));
394 $abletomovecourses = true;
397 echo '</td>';
398 echo '<td align="center">';
399 echo '<input type="checkbox" name="c'.$acourse->id.'" />';
400 echo '</td>';
401 } else {
402 echo '<td align="right">';
403 // print enrol info
404 if ($icons = enrol_get_course_info_icons($acourse)) {
405 foreach ($icons as $pix_icon) {
406 echo $OUTPUT->render($pix_icon);
409 if (!empty($acourse->summary)) {
410 $url = new moodle_url("/course/info.php?id=$acourse->id");
411 echo $OUTPUT->action_link($url, '<img alt="'.get_string('info').'" class="icon" src="'.$OUTPUT->pix_url('i/info') . '" />',
412 new popup_action('click', $url, 'courseinfo'), array('title'=>get_string('summary')));
414 echo "</td>";
416 echo "</tr>";
419 if ($abletomovecourses) {
420 $movetocategories = array();
421 $notused = array();
422 make_categories_list($movetocategories, $notused, 'moodle/category:manage');
423 $movetocategories[$category->id] = get_string('moveselectedcoursesto');
424 echo '<tr><td colspan="3" align="right">';
425 echo html_writer::select($movetocategories, 'moveto', $category->id, null, array('id'=>'movetoid'));
426 $PAGE->requires->js_init_call('M.util.init_select_autosubmit', array('movecourses', 'movetoid', false));
427 echo '<input type="hidden" name="id" value="'.$category->id.'" />';
428 echo '</td></tr>';
431 echo '</table>';
432 echo '</div></form>';
433 echo '<br />';
436 echo '<div class="buttons">';
437 if ($canmanage and $numcourses > 1) {
438 // Print button to re-sort courses by name
439 $url = new moodle_url('/course/category.php', array('id' => $category->id, 'resort' => 'name', 'sesskey' => sesskey()));
440 echo $OUTPUT->single_button($url, get_string('resortcoursesbyname'), 'get');
443 if (has_capability('moodle/course:create', $context)) {
444 // Print button to create a new course
445 $url = new moodle_url('/course/edit.php', array('category' => $category->id, 'returnto' => 'category'));
446 echo $OUTPUT->single_button($url, get_string('addnewcourse'), 'get');
449 if (!empty($CFG->enablecourserequests) && $category->id == $CFG->defaultrequestcategory) {
450 print_course_request_buttons(get_context_instance(CONTEXT_SYSTEM));
452 echo '</div>';
454 print_course_search();
456 echo $OUTPUT->footer();