MDL-37774 Make moodle1_file_manager::migrate_directory() support trailing slash in...
[moodle.git] / course / category.php
blob71ea312e7b788297a1e15d98420727808a91c08b
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', context_coursecat::instance($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 = context_course::instance($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()));
147 add_to_log($course->id, "course", ($visible ? 'show' : 'hide'), "edit.php?id=$course->id", $course->id);
152 // Move a course up or down
153 if (!empty($moveup) or !empty($movedown)) {
154 require_capability('moodle/category:manage', $context);
156 // Ensure the course order has continuous ordering
157 fix_course_sortorder();
158 $swapcourse = NULL;
160 if (!empty($moveup)) {
161 if ($movecourse = $DB->get_record('course', array('id' => $moveup))) {
162 $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder - 1));
164 } else {
165 if ($movecourse = $DB->get_record('course', array('id' => $movedown))) {
166 $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder + 1));
169 if ($swapcourse and $movecourse) {
170 // check course's category
171 if ($movecourse->category != $id) {
172 print_error('coursedoesnotbelongtocategory');
174 $DB->set_field('course', 'sortorder', $swapcourse->sortorder, array('id' => $movecourse->id));
175 $DB->set_field('course', 'sortorder', $movecourse->sortorder, array('id' => $swapcourse->id));
176 add_to_log($movecourse->id, "course", "move", "edit.php?id=$movecourse->id", $movecourse->id);
180 } // End of editing stuff
182 // Prepare the standard URL params for this page. We'll need them later.
183 $urlparams = array('id' => $id);
184 if ($page) {
185 $urlparams['page'] = $page;
187 if ($perpage) {
188 $urlparams['perpage'] = $perpage;
191 // Begin output
192 if ($editingon && can_edit_in_category()) {
193 // Integrate into the admin tree only if the user can edit categories at the top level,
194 // otherwise the admin block does not appear to this user, and you get an error.
195 require_once($CFG->libdir . '/adminlib.php');
196 navigation_node::override_active_url(new moodle_url('/course/category.php', array('id' => $id)));
197 admin_externalpage_setup('coursemgmt', '', $urlparams, $CFG->wwwroot . '/course/category.php');
198 $PAGE->set_context($context); // Ensure that we are actually showing blocks etc for the cat context
200 $settingsnode = $PAGE->settingsnav->find_active_node();
201 if ($settingsnode) {
202 $settingsnode->make_inactive();
203 $settingsnode->force_open();
204 $PAGE->navbar->add($settingsnode->text, $settingsnode->action);
206 echo $OUTPUT->header();
207 } else {
208 $site = get_site();
209 $PAGE->set_title("$site->shortname: $category->name");
210 $PAGE->set_heading($site->fullname);
211 $PAGE->set_button(print_course_search('', true, 'navbar'));
212 $PAGE->set_pagelayout('coursecategory');
213 echo $OUTPUT->header();
216 /// Print the category selector
217 $displaylist = array();
218 $notused = array();
219 make_categories_list($displaylist, $notused);
221 echo '<div class="categorypicker">';
222 $select = new single_select(new moodle_url('/course/category.php'), 'id', $displaylist, $category->id, null, 'switchcategory');
223 $select->set_label(get_string('categories').':');
224 echo $OUTPUT->render($select);
225 echo '</div>';
227 /// Print current category description
228 if (!$editingon && $category->description) {
229 echo $OUTPUT->box_start();
230 $options = new stdClass;
231 $options->noclean = true;
232 $options->para = false;
233 $options->overflowdiv = true;
234 if (!isset($category->descriptionformat)) {
235 $category->descriptionformat = FORMAT_MOODLE;
237 $text = file_rewrite_pluginfile_urls($category->description, 'pluginfile.php', $context->id, 'coursecat', 'description', null);
238 echo format_text($text, $category->descriptionformat, $options);
239 echo $OUTPUT->box_end();
242 if ($editingon && $canmanage) {
243 echo $OUTPUT->container_start('buttons');
245 // Print button to update this category
246 $url = new moodle_url('/course/editcategory.php', array('id' => $category->id));
247 echo $OUTPUT->single_button($url, get_string('editcategorythis'), 'get');
249 // Print button for creating new categories
250 $url = new moodle_url('/course/editcategory.php', array('parent' => $category->id));
251 echo $OUTPUT->single_button($url, get_string('addsubcategory'), 'get');
253 echo $OUTPUT->container_end();
256 // Print out all the sub-categories
257 // In order to view hidden subcategories the user must have the viewhiddencategories
258 // capability in the current category.
259 if (has_capability('moodle/category:viewhiddencategories', $context)) {
260 $categorywhere = '';
261 } else {
262 $categorywhere = 'AND cc.visible = 1';
264 // We're going to preload the context for the subcategory as we know that we
265 // need it later on for formatting.
267 $ctxselect = context_helper::get_preload_record_columns_sql('ctx');
268 $sql = "SELECT cc.*, $ctxselect
269 FROM {course_categories} cc
270 JOIN {context} ctx ON cc.id = ctx.instanceid
271 WHERE cc.parent = :parentid AND
272 ctx.contextlevel = :contextlevel
273 $categorywhere
274 ORDER BY cc.sortorder ASC";
275 $subcategories = $DB->get_recordset_sql($sql, array('parentid' => $category->id, 'contextlevel' => CONTEXT_COURSECAT));
276 // Prepare a table to display the sub categories.
277 $table = new html_table;
278 $table->attributes = array('border' => '0', 'cellspacing' => '2', 'cellpadding' => '4', 'class' => 'generalbox boxaligncenter category_subcategories');
279 $table->head = array(new lang_string('subcategories'));
280 $table->data = array();
281 $baseurl = new moodle_url('/course/category.php');
282 foreach ($subcategories as $subcategory) {
283 // Preload the context we will need it to format the category name shortly.
284 context_helper::preload_from_record($subcategory);
285 $context = context_coursecat::instance($subcategory->id);
286 // Prepare the things we need to create a link to the subcategory
287 $attributes = $subcategory->visible ? array() : array('class' => 'dimmed');
288 $text = format_string($subcategory->name, true, array('context' => $context));
289 // Add the subcategory to the table
290 $baseurl->param('id', $subcategory->id);
291 $table->data[] = array(html_writer::link($baseurl, $text, $attributes));
294 $subcategorieswereshown = (count($table->data) > 0);
295 if ($subcategorieswereshown) {
296 echo html_writer::table($table);
299 // Print out all the courses.
300 $courses = get_courses_page($category->id, 'c.sortorder ASC',
301 'c.id,c.sortorder,c.shortname,c.fullname,c.summary,c.visible',
302 $totalcount, $page*$perpage, $perpage);
303 $numcourses = count($courses);
305 // We can consider that we are using pagination when the total count of courses is different than the one returned.
306 $pagingmode = $totalcount != $numcourses;
308 if (!$courses) {
309 // There is no course to display.
310 if (empty($subcategorieswereshown)) {
311 echo $OUTPUT->heading(get_string("nocoursesyet"));
313 } else if ($numcourses <= $CFG->courseswithsummarieslimit and !$pagingmode and !$editingon) {
314 // We display courses with their summaries as we have not reached the limit, also we are not
315 // in paging mode and not allowed to edit either.
316 echo $OUTPUT->box_start('courseboxes');
317 print_courses($category);
318 echo $OUTPUT->box_end();
319 } else {
320 // The conditions above have failed, we display a basic list of courses with paging/editing options.
321 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "/course/category.php?id=$category->id&perpage=$perpage");
323 echo '<form id="movecourses" action="category.php" method="post"><div>';
324 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
325 echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter"><tr>';
326 echo '<th class="header" scope="col">'.get_string('courses').'</th>';
327 if ($editingon) {
328 echo '<th class="header" scope="col">'.get_string('edit').'</th>';
329 echo '<th class="header" scope="col">'.get_string('select').'</th>';
330 } else {
331 echo '<th class="header" scope="col">&nbsp;</th>';
333 echo '</tr>';
335 $count = 0;
336 $abletomovecourses = false; // for now
338 // Checking if we are at the first or at the last page, to allow courses to
339 // be moved up and down beyond the paging border
340 if ($totalcount > $perpage) {
341 $atfirstpage = ($page == 0);
342 if ($perpage > 0) {
343 $atlastpage = (($page + 1) == ceil($totalcount / $perpage));
344 } else {
345 $atlastpage = true;
347 } else {
348 $atfirstpage = true;
349 $atlastpage = true;
352 $baseurl = new moodle_url('/course/category.php', $urlparams + array('sesskey' => sesskey()));
353 foreach ($courses as $acourse) {
354 $coursecontext = context_course::instance($acourse->id);
356 $count++;
357 $up = ($count > 1 || !$atfirstpage);
358 $down = ($count < $numcourses || !$atlastpage);
360 $linkcss = $acourse->visible ? '' : ' class="dimmed" ';
361 echo '<tr>';
362 $coursename = get_course_display_name_for_list($acourse);
363 echo '<td><a '.$linkcss.' href="view.php?id='.$acourse->id.'">'. format_string($coursename) .'</a></td>';
364 if ($editingon) {
365 echo '<td>';
366 if (has_capability('moodle/course:update', $coursecontext)) {
367 $url = new moodle_url('/course/edit.php', array('id' => $acourse->id, 'category' => $id, 'returnto' => 'category'));
368 echo $OUTPUT->action_icon($url, new pix_icon('t/edit', get_string('settings')));
371 // role assignment link
372 if (has_capability('moodle/course:enrolreview', $coursecontext)) {
373 $url = new moodle_url('/enrol/users.php', array('id' => $acourse->id));
374 echo $OUTPUT->action_icon($url, new pix_icon('t/enrolusers', get_string('enrolledusers', 'enrol')));
377 if (can_delete_course($acourse->id)) {
378 $url = new moodle_url('/course/delete.php', array('id' => $acourse->id));
379 echo $OUTPUT->action_icon($url, new pix_icon('t/delete', get_string('delete')));
382 // MDL-8885, users with no capability to view hidden courses, should not be able to lock themselves out
383 if (has_capability('moodle/course:visibility', $coursecontext) && has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
384 if (!empty($acourse->visible)) {
385 $url = new moodle_url($baseurl, array('hide' => $acourse->id));
386 echo $OUTPUT->action_icon($url, new pix_icon('t/hide', get_string('hide')));
387 } else {
388 $url = new moodle_url($baseurl, array('show' => $acourse->id));
389 echo $OUTPUT->action_icon($url, new pix_icon('t/show', get_string('show')));
393 if (has_capability('moodle/backup:backupcourse', $coursecontext)) {
394 $url = new moodle_url('/backup/backup.php', array('id' => $acourse->id));
395 echo $OUTPUT->action_icon($url, new pix_icon('t/backup', get_string('backup')));
398 if (has_capability('moodle/restore:restorecourse', $coursecontext)) {
399 $url = new moodle_url('/backup/restorefile.php', array('contextid' => $coursecontext->id));
400 echo $OUTPUT->action_icon($url, new pix_icon('t/restore', get_string('restore')));
403 if ($canmanage) {
404 if ($up) {
405 $url = new moodle_url($baseurl, array('moveup' => $acourse->id));
406 echo $OUTPUT->action_icon($url, new pix_icon('t/up', get_string('moveup')));
409 if ($down) {
410 $url = new moodle_url($baseurl, array('movedown' => $acourse->id));
411 echo $OUTPUT->action_icon($url, new pix_icon('t/down', get_string('movedown')));
413 $abletomovecourses = true;
416 echo '</td>';
417 echo '<td align="center">';
418 echo '<input type="checkbox" name="c'.$acourse->id.'" />';
419 echo '</td>';
420 } else {
421 echo '<td align="right">';
422 // print enrol info
423 if ($icons = enrol_get_course_info_icons($acourse)) {
424 foreach ($icons as $pix_icon) {
425 echo $OUTPUT->render($pix_icon);
428 if (!empty($acourse->summary)) {
429 $url = new moodle_url("/course/info.php?id=$acourse->id");
430 echo $OUTPUT->action_link($url, '<img alt="'.get_string('info').'" class="icon" src="'.$OUTPUT->pix_url('i/info') . '" />',
431 new popup_action('click', $url, 'courseinfo'), array('title'=>get_string('summary')));
433 echo "</td>";
435 echo "</tr>";
438 if ($abletomovecourses) {
439 $movetocategories = array();
440 $notused = array();
441 make_categories_list($movetocategories, $notused, 'moodle/category:manage');
442 $movetocategories[$category->id] = get_string('moveselectedcoursesto');
443 echo '<tr><td colspan="3" align="right">';
444 echo html_writer::label(get_string('moveselectedcoursesto'), 'movetoid', false, array('class' => 'accesshide'));
445 echo html_writer::select($movetocategories, 'moveto', $category->id, null, array('id'=>'movetoid', 'class' => 'autosubmit'));
446 $PAGE->requires->yui_module('moodle-core-formautosubmit',
447 'M.core.init_formautosubmit',
448 array(array('selectid' => 'movetoid', 'nothing' => $category->id))
450 echo '<input type="hidden" name="id" value="'.$category->id.'" />';
451 echo '</td></tr>';
454 echo '</table>';
455 echo '</div></form>';
456 echo '<br />';
459 echo '<div class="buttons">';
460 if ($canmanage and $numcourses > 1) {
461 // Print button to re-sort courses by name
462 $url = new moodle_url('/course/category.php', array('id' => $category->id, 'resort' => 'name', 'sesskey' => sesskey()));
463 echo $OUTPUT->single_button($url, get_string('resortcoursesbyname'), 'get');
466 if (has_capability('moodle/course:create', $context)) {
467 // Print button to create a new course
468 $url = new moodle_url('/course/edit.php', array('category' => $category->id, 'returnto' => 'category'));
469 echo $OUTPUT->single_button($url, get_string('addnewcourse'), 'get');
472 if (!empty($CFG->enablecourserequests) && $category->id == $CFG->defaultrequestcategory) {
473 print_course_request_buttons(context_system::instance());
475 echo '</div>';
477 print_course_search();
479 echo $OUTPUT->footer();