Merge branch 'MDL-37181_m24' of https://github.com/markn86/moodle into MOODLE_24_STABLE
[moodle.git] / course / category.php
blobe1df33d1c586566e0abeaa5b8d14a726d95572a1
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 // This needs to match caps checked in can_edit_in_category below.
65 $PAGE->set_other_editing_capability(array(
66 'moodle/category:manage',
67 'moodle/course:create'
68 ));
70 $canedit = can_edit_in_category($category->id);
71 if ($canedit) {
72 if ($categoryedit !== -1) {
73 $USER->editing = $categoryedit;
75 require_login();
76 $editingon = $PAGE->user_is_editing();
77 } else {
78 if ($CFG->forcelogin) {
79 require_login();
81 $editingon = false;
84 if (!$category->visible) {
85 require_capability('moodle/category:viewhiddencategories', $context);
88 $canmanage = has_capability('moodle/category:manage', $context);
89 $sesskeyprovided = !empty($sesskey) && confirm_sesskey($sesskey);
91 // Process any category actions.
92 if ($canmanage && $resort && $sesskeyprovided) {
93 // Resort the category if requested
94 if ($courses = get_courses($category->id, '', 'c.id,c.fullname,c.sortorder')) {
95 collatorlib::asort_objects_by_property($courses, 'fullname', collatorlib::SORT_NATURAL);
96 $i = 1;
97 foreach ($courses as $course) {
98 $DB->set_field('course', 'sortorder', $category->sortorder+$i, array('id'=>$course->id));
99 $i++;
101 fix_course_sortorder(); // should not be needed
105 // Process any course actions.
106 if ($editingon && $sesskeyprovided) {
108 // Move a specified course to a new category
109 if (!empty($moveto) and $data = data_submitted()) {
110 // Some courses are being moved
111 // user must have category update in both cats to perform this
112 require_capability('moodle/category:manage', $context);
113 require_capability('moodle/category:manage', context_coursecat::instance($moveto));
115 if (!$destcategory = $DB->get_record('course_categories', array('id' => $data->moveto))) {
116 print_error('cannotfindcategory', '', '', $data->moveto);
119 $courses = array();
120 foreach ($data as $key => $value) {
121 if (preg_match('/^c\d+$/', $key)) {
122 $courseid = substr($key, 1);
123 array_push($courses, $courseid);
125 // check this course's category
126 if ($movingcourse = $DB->get_record('course', array('id'=>$courseid))) {
127 if ($movingcourse->category != $id ) {
128 print_error('coursedoesnotbelongtocategory');
130 } else {
131 print_error('cannotfindcourse');
135 move_courses($courses, $data->moveto);
138 // Hide or show a course
139 if (!empty($hide) or !empty($show)) {
140 if (!empty($hide)) {
141 $course = $DB->get_record('course', array('id' => $hide));
142 $visible = 0;
143 } else {
144 $course = $DB->get_record('course', array('id' => $show));
145 $visible = 1;
148 if ($course) {
149 $coursecontext = context_course::instance($course->id);
150 require_capability('moodle/course:visibility', $coursecontext);
151 // Set the visibility of the course. we set the old flag when user manually changes visibility of course.
152 $DB->update_record('course', array('id' => $course->id, 'visible' => $visible, 'visibleold' => $visible, 'timemodified' => time()));
153 add_to_log($course->id, "course", ($visible ? 'show' : 'hide'), "edit.php?id=$course->id", $course->id);
158 // Move a course up or down
159 if (!empty($moveup) or !empty($movedown)) {
160 require_capability('moodle/category:manage', $context);
162 // Ensure the course order has continuous ordering
163 fix_course_sortorder();
164 $swapcourse = NULL;
166 if (!empty($moveup)) {
167 if ($movecourse = $DB->get_record('course', array('id' => $moveup))) {
168 $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder - 1));
170 } else {
171 if ($movecourse = $DB->get_record('course', array('id' => $movedown))) {
172 $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder + 1));
175 if ($swapcourse and $movecourse) {
176 // check course's category
177 if ($movecourse->category != $id) {
178 print_error('coursedoesnotbelongtocategory');
180 $DB->set_field('course', 'sortorder', $swapcourse->sortorder, array('id' => $movecourse->id));
181 $DB->set_field('course', 'sortorder', $movecourse->sortorder, array('id' => $swapcourse->id));
182 add_to_log($movecourse->id, "course", "move", "edit.php?id=$movecourse->id", $movecourse->id);
186 } // End of editing stuff
188 // Prepare the standard URL params for this page. We'll need them later.
189 $urlparams = array('id' => $id);
190 if ($page) {
191 $urlparams['page'] = $page;
193 if ($perpage) {
194 $urlparams['perpage'] = $perpage;
197 // Begin output
198 if ($editingon && can_edit_in_category()) {
199 // Integrate into the admin tree only if the user can edit categories at the top level,
200 // otherwise the admin block does not appear to this user, and you get an error.
201 require_once($CFG->libdir . '/adminlib.php');
202 navigation_node::override_active_url(new moodle_url('/course/category.php', array('id' => $id)));
203 admin_externalpage_setup('coursemgmt', '', $urlparams, $CFG->wwwroot . '/course/category.php');
204 $PAGE->set_context($context); // Ensure that we are actually showing blocks etc for the cat context
206 $settingsnode = $PAGE->settingsnav->find_active_node();
207 if ($settingsnode) {
208 $settingsnode->make_inactive();
209 $settingsnode->force_open();
210 $PAGE->navbar->add($settingsnode->text, $settingsnode->action);
212 echo $OUTPUT->header();
213 } else {
214 $site = get_site();
215 $PAGE->set_title("$site->shortname: $category->name");
216 $PAGE->set_heading($site->fullname);
217 $PAGE->set_button(print_course_search('', true, 'navbar'));
218 $PAGE->set_pagelayout('coursecategory');
219 echo $OUTPUT->header();
222 /// Print the category selector
223 $displaylist = array();
224 $notused = array();
225 make_categories_list($displaylist, $notused);
227 echo '<div class="categorypicker">';
228 $select = new single_select(new moodle_url('/course/category.php'), 'id', $displaylist, $category->id, null, 'switchcategory');
229 $select->set_label(get_string('categories').':');
230 echo $OUTPUT->render($select);
231 echo '</div>';
233 /// Print current category description
234 if (!$editingon && $category->description) {
235 echo $OUTPUT->box_start();
236 $options = new stdClass;
237 $options->noclean = true;
238 $options->para = false;
239 $options->overflowdiv = true;
240 if (!isset($category->descriptionformat)) {
241 $category->descriptionformat = FORMAT_MOODLE;
243 $text = file_rewrite_pluginfile_urls($category->description, 'pluginfile.php', $context->id, 'coursecat', 'description', null);
244 echo format_text($text, $category->descriptionformat, $options);
245 echo $OUTPUT->box_end();
248 if ($editingon && $canmanage) {
249 echo $OUTPUT->container_start('buttons');
251 // Print button to update this category
252 $url = new moodle_url('/course/editcategory.php', array('id' => $category->id));
253 echo $OUTPUT->single_button($url, get_string('editcategorythis'), 'get');
255 // Print button for creating new categories
256 $url = new moodle_url('/course/editcategory.php', array('parent' => $category->id));
257 echo $OUTPUT->single_button($url, get_string('addsubcategory'), 'get');
259 echo $OUTPUT->container_end();
262 // Print out all the sub-categories
263 // In order to view hidden subcategories the user must have the viewhiddencategories
264 // capability in the current category.
265 if (has_capability('moodle/category:viewhiddencategories', $context)) {
266 $categorywhere = '';
267 } else {
268 $categorywhere = 'AND cc.visible = 1';
270 // We're going to preload the context for the subcategory as we know that we
271 // need it later on for formatting.
273 $ctxselect = context_helper::get_preload_record_columns_sql('ctx');
274 $sql = "SELECT cc.*, $ctxselect
275 FROM {course_categories} cc
276 JOIN {context} ctx ON cc.id = ctx.instanceid
277 WHERE cc.parent = :parentid AND
278 ctx.contextlevel = :contextlevel
279 $categorywhere
280 ORDER BY cc.sortorder ASC";
281 $subcategories = $DB->get_recordset_sql($sql, array('parentid' => $category->id, 'contextlevel' => CONTEXT_COURSECAT));
282 // Prepare a table to display the sub categories.
283 $table = new html_table;
284 $table->attributes = array('border' => '0', 'cellspacing' => '2', 'cellpadding' => '4', 'class' => 'generalbox boxaligncenter category_subcategories');
285 $table->head = array(new lang_string('subcategories'));
286 $table->data = array();
287 $baseurl = new moodle_url('/course/category.php');
288 foreach ($subcategories as $subcategory) {
289 // Preload the context we will need it to format the category name shortly.
290 context_helper::preload_from_record($subcategory);
291 $context = context_coursecat::instance($subcategory->id);
292 // Prepare the things we need to create a link to the subcategory
293 $attributes = $subcategory->visible ? array() : array('class' => 'dimmed');
294 $text = format_string($subcategory->name, true, array('context' => $context));
295 // Add the subcategory to the table
296 $baseurl->param('id', $subcategory->id);
297 $table->data[] = array(html_writer::link($baseurl, $text, $attributes));
300 $subcategorieswereshown = (count($table->data) > 0);
301 if ($subcategorieswereshown) {
302 echo html_writer::table($table);
305 // Print out all the courses.
306 $courses = get_courses_page($category->id, 'c.sortorder ASC',
307 'c.id,c.sortorder,c.shortname,c.fullname,c.summary,c.visible',
308 $totalcount, $page*$perpage, $perpage);
309 $numcourses = count($courses);
311 // We can consider that we are using pagination when the total count of courses is different than the one returned.
312 $pagingmode = $totalcount != $numcourses;
314 if (!$courses) {
315 // There is no course to display.
316 if (empty($subcategorieswereshown)) {
317 echo $OUTPUT->heading(get_string("nocoursesyet"));
319 } else if ($numcourses <= $CFG->courseswithsummarieslimit and !$pagingmode and !$editingon) {
320 // We display courses with their summaries as we have not reached the limit, also we are not
321 // in paging mode and not allowed to edit either.
322 echo $OUTPUT->box_start('courseboxes');
323 print_courses($category);
324 echo $OUTPUT->box_end();
325 } else {
326 // The conditions above have failed, we display a basic list of courses with paging/editing options.
327 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "/course/category.php?id=$category->id&perpage=$perpage");
329 echo '<form id="movecourses" action="category.php" method="post"><div>';
330 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
331 echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter"><tr>';
332 echo '<th class="header" scope="col">'.get_string('courses').'</th>';
333 if ($editingon) {
334 echo '<th class="header" scope="col">'.get_string('edit').'</th>';
335 echo '<th class="header" scope="col">'.get_string('select').'</th>';
336 } else {
337 echo '<th class="header" scope="col">&nbsp;</th>';
339 echo '</tr>';
341 $count = 0;
342 $abletomovecourses = false; // for now
344 // Checking if we are at the first or at the last page, to allow courses to
345 // be moved up and down beyond the paging border
346 if ($totalcount > $perpage) {
347 $atfirstpage = ($page == 0);
348 if ($perpage > 0) {
349 $atlastpage = (($page + 1) == ceil($totalcount / $perpage));
350 } else {
351 $atlastpage = true;
353 } else {
354 $atfirstpage = true;
355 $atlastpage = true;
358 $baseurl = new moodle_url('/course/category.php', $urlparams + array('sesskey' => sesskey()));
359 foreach ($courses as $acourse) {
360 $coursecontext = context_course::instance($acourse->id);
362 $count++;
363 $up = ($count > 1 || !$atfirstpage);
364 $down = ($count < $numcourses || !$atlastpage);
366 $linkcss = $acourse->visible ? '' : ' class="dimmed" ';
367 echo '<tr>';
368 $coursename = get_course_display_name_for_list($acourse);
369 echo '<td><a '.$linkcss.' href="view.php?id='.$acourse->id.'">'. format_string($coursename) .'</a></td>';
370 if ($editingon) {
371 echo '<td>';
372 if (has_capability('moodle/course:update', $coursecontext)) {
373 $url = new moodle_url('/course/edit.php', array('id' => $acourse->id, 'category' => $id, 'returnto' => 'category'));
374 echo $OUTPUT->action_icon($url, new pix_icon('t/edit', get_string('settings')));
377 // role assignment link
378 if (has_capability('moodle/course:enrolreview', $coursecontext)) {
379 $url = new moodle_url('/enrol/users.php', array('id' => $acourse->id));
380 echo $OUTPUT->action_icon($url, new pix_icon('t/enrolusers', get_string('enrolledusers', 'enrol')));
383 if (can_delete_course($acourse->id)) {
384 $url = new moodle_url('/course/delete.php', array('id' => $acourse->id));
385 echo $OUTPUT->action_icon($url, new pix_icon('t/delete', get_string('delete')));
388 // MDL-8885, users with no capability to view hidden courses, should not be able to lock themselves out
389 if (has_capability('moodle/course:visibility', $coursecontext) && has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
390 if (!empty($acourse->visible)) {
391 $url = new moodle_url($baseurl, array('hide' => $acourse->id));
392 echo $OUTPUT->action_icon($url, new pix_icon('t/hide', get_string('hide')));
393 } else {
394 $url = new moodle_url($baseurl, array('show' => $acourse->id));
395 echo $OUTPUT->action_icon($url, new pix_icon('t/show', get_string('show')));
399 if (has_capability('moodle/backup:backupcourse', $coursecontext)) {
400 $url = new moodle_url('/backup/backup.php', array('id' => $acourse->id));
401 echo $OUTPUT->action_icon($url, new pix_icon('t/backup', get_string('backup')));
404 if (has_capability('moodle/restore:restorecourse', $coursecontext)) {
405 $url = new moodle_url('/backup/restorefile.php', array('contextid' => $coursecontext->id));
406 echo $OUTPUT->action_icon($url, new pix_icon('t/restore', get_string('restore')));
409 if ($canmanage) {
410 if ($up) {
411 $url = new moodle_url($baseurl, array('moveup' => $acourse->id));
412 echo $OUTPUT->action_icon($url, new pix_icon('t/up', get_string('moveup')));
415 if ($down) {
416 $url = new moodle_url($baseurl, array('movedown' => $acourse->id));
417 echo $OUTPUT->action_icon($url, new pix_icon('t/down', get_string('movedown')));
419 $abletomovecourses = true;
422 echo '</td>';
423 echo '<td align="center">';
424 echo '<input type="checkbox" name="c'.$acourse->id.'" />';
425 echo '</td>';
426 } else {
427 echo '<td align="right">';
428 // print enrol info
429 if ($icons = enrol_get_course_info_icons($acourse)) {
430 foreach ($icons as $pix_icon) {
431 echo $OUTPUT->render($pix_icon);
434 if (!empty($acourse->summary)) {
435 $url = new moodle_url("/course/info.php?id=$acourse->id");
436 echo $OUTPUT->action_link($url, '<img alt="'.get_string('info').'" class="icon" src="'.$OUTPUT->pix_url('i/info') . '" />',
437 new popup_action('click', $url, 'courseinfo'), array('title'=>get_string('summary')));
439 echo "</td>";
441 echo "</tr>";
444 if ($abletomovecourses) {
445 $movetocategories = array();
446 $notused = array();
447 make_categories_list($movetocategories, $notused, 'moodle/category:manage');
448 $movetocategories[$category->id] = get_string('moveselectedcoursesto');
449 echo '<tr><td colspan="3" align="right">';
450 echo html_writer::label(get_string('moveselectedcoursesto'), 'movetoid', false, array('class' => 'accesshide'));
451 echo html_writer::select($movetocategories, 'moveto', $category->id, null, array('id'=>'movetoid', 'class' => 'autosubmit'));
452 $PAGE->requires->yui_module('moodle-core-formautosubmit',
453 'M.core.init_formautosubmit',
454 array(array('selectid' => 'movetoid', 'nothing' => $category->id))
456 echo '<input type="hidden" name="id" value="'.$category->id.'" />';
457 echo '</td></tr>';
460 echo '</table>';
461 echo '</div></form>';
462 echo '<br />';
465 echo '<div class="buttons">';
466 if ($canmanage and $numcourses > 1) {
467 // Print button to re-sort courses by name
468 $url = new moodle_url('/course/category.php', array('id' => $category->id, 'resort' => 'name', 'sesskey' => sesskey()));
469 echo $OUTPUT->single_button($url, get_string('resortcoursesbyname'), 'get');
472 if (has_capability('moodle/course:create', $context)) {
473 // Print button to create a new course
474 $url = new moodle_url('/course/edit.php', array('category' => $category->id, 'returnto' => 'category'));
475 echo $OUTPUT->single_button($url, get_string('addnewcourse'), 'get');
478 if (!empty($CFG->enablecourserequests) && $category->id == $CFG->defaultrequestcategory) {
479 print_course_request_buttons(context_system::instance());
481 echo '</div>';
483 print_course_search();
485 echo $OUTPUT->footer();