Merge branch 'MDL-29847_22' of git://github.com/timhunt/moodle into MOODLE_22_STABLE
[moodle.git] / course / category.php
blob9fd187c539175bb6ff51987b468e3f5caa15b013
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.
252 list($ctxselect, $ctxjoin) = context_instance_preload_sql('cc.id', CONTEXT_COURSECAT, 'ctx');
253 $sql = "SELECT cc.* $ctxselect
254 FROM {course_categories} cc
255 $ctxjoin
256 WHERE cc.parent = :parentid
257 $categorywhere
258 ORDER BY cc.sortorder ASC";
259 $subcategories = $DB->get_recordset_sql($sql, array('parentid' => $category->id));
260 // Prepare a table to display the sub categories.
261 $table = new html_table;
262 $table->attributes = array('border' => '0', 'cellspacing' => '2', 'cellpadding' => '4', 'class' => 'generalbox boxaligncenter category_subcategories');
263 $table->head = array(get_string('subcategories'));
264 $table->data = array();
265 $baseurl = new moodle_url('/course/category.php');
266 foreach ($subcategories as $subcategory) {
267 // Preload the context we will need it to format the category name shortly.
268 context_instance_preload($subcategory);
269 $context = get_context_instance(CONTEXT_COURSECAT, $subcategory->id);
270 // Prepare the things we need to create a link to the subcategory
271 $attributes = $subcategory->visible ? array() : array('class' => 'dimmed');
272 $text = format_string($subcategory->name, true, array('context' => $context));
273 // Add the subcategory to the table
274 $baseurl->param('id', $subcategory->id);
275 $table->data[] = array(html_writer::link($baseurl, $text, $attributes));
278 $subcategorieswereshown = (count($table->data) > 0);
279 if ($subcategorieswereshown) {
280 echo html_writer::table($table);
283 // Print out all the courses
284 $courses = get_courses_page($category->id, 'c.sortorder ASC',
285 'c.id,c.sortorder,c.shortname,c.fullname,c.summary,c.visible',
286 $totalcount, $page*$perpage, $perpage);
287 $numcourses = count($courses);
289 if (!$courses) {
290 if (empty($subcategorieswereshown)) {
291 echo $OUTPUT->heading(get_string("nocoursesyet"));
294 } else if ($numcourses <= COURSE_MAX_SUMMARIES_PER_PAGE and !$page and !$editingon) {
295 echo $OUTPUT->box_start('courseboxes');
296 print_courses($category);
297 echo $OUTPUT->box_end();
299 } else {
300 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "/course/category.php?id=$category->id&perpage=$perpage");
302 echo '<form id="movecourses" action="category.php" method="post"><div>';
303 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
304 echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter"><tr>';
305 echo '<th class="header" scope="col">'.get_string('courses').'</th>';
306 if ($editingon) {
307 echo '<th class="header" scope="col">'.get_string('edit').'</th>';
308 echo '<th class="header" scope="col">'.get_string('select').'</th>';
309 } else {
310 echo '<th class="header" scope="col">&nbsp;</th>';
312 echo '</tr>';
314 $count = 0;
315 $abletomovecourses = false; // for now
317 // Checking if we are at the first or at the last page, to allow courses to
318 // be moved up and down beyond the paging border
319 if ($totalcount > $perpage) {
320 $atfirstpage = ($page == 0);
321 if ($perpage > 0) {
322 $atlastpage = (($page + 1) == ceil($totalcount / $perpage));
323 } else {
324 $atlastpage = true;
326 } else {
327 $atfirstpage = true;
328 $atlastpage = true;
331 $baseurl = new moodle_url('/course/category.php', $urlparams + array('sesskey' => sesskey()));
332 foreach ($courses as $acourse) {
333 $coursecontext = get_context_instance(CONTEXT_COURSE, $acourse->id);
335 $count++;
336 $up = ($count > 1 || !$atfirstpage);
337 $down = ($count < $numcourses || !$atlastpage);
339 $linkcss = $acourse->visible ? '' : ' class="dimmed" ';
340 echo '<tr>';
341 $coursename = get_course_display_name_for_list($acourse);
342 echo '<td><a '.$linkcss.' href="view.php?id='.$acourse->id.'">'. format_string($coursename) .'</a></td>';
343 if ($editingon) {
344 echo '<td>';
345 if (has_capability('moodle/course:update', $coursecontext)) {
346 $url = new moodle_url('/course/edit.php', array('id' => $acourse->id, 'category' => $id, 'returnto' => 'category'));
347 echo $OUTPUT->action_icon($url, new pix_icon('t/edit', get_string('settings')));
350 // role assignment link
351 if (has_capability('moodle/course:enrolreview', $coursecontext)) {
352 $url = new moodle_url('/enrol/users.php', array('id' => $acourse->id));
353 echo $OUTPUT->action_icon($url, new pix_icon('i/users', get_string('enrolledusers', 'enrol')));
356 if (can_delete_course($acourse->id)) {
357 $url = new moodle_url('/course/delete.php', array('id' => $acourse->id));
358 echo $OUTPUT->action_icon($url, new pix_icon('t/delete', get_string('delete')));
361 // MDL-8885, users with no capability to view hidden courses, should not be able to lock themselves out
362 if (has_capability('moodle/course:visibility', $coursecontext) && has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
363 if (!empty($acourse->visible)) {
364 $url = new moodle_url($baseurl, array('hide' => $acourse->id));
365 echo $OUTPUT->action_icon($url, new pix_icon('t/hide', get_string('hide')));
366 } else {
367 $url = new moodle_url($baseurl, array('show' => $acourse->id));
368 echo $OUTPUT->action_icon($url, new pix_icon('t/show', get_string('show')));
372 if (has_capability('moodle/backup:backupcourse', $coursecontext)) {
373 $url = new moodle_url('/backup/backup.php', array('id' => $acourse->id));
374 echo $OUTPUT->action_icon($url, new pix_icon('t/backup', get_string('backup')));
377 if (has_capability('moodle/restore:restorecourse', $coursecontext)) {
378 $url = new moodle_url('/backup/restorefile.php', array('contextid' => $coursecontext->id));
379 echo $OUTPUT->action_icon($url, new pix_icon('t/restore', get_string('restore')));
382 if ($canmanage) {
383 if ($up) {
384 $url = new moodle_url($baseurl, array('moveup' => $acourse->id));
385 echo $OUTPUT->action_icon($url, new pix_icon('t/up', get_string('moveup')));
388 if ($down) {
389 $url = new moodle_url($baseurl, array('movedown' => $acourse->id));
390 echo $OUTPUT->action_icon($url, new pix_icon('t/down', get_string('movedown')));
392 $abletomovecourses = true;
395 echo '</td>';
396 echo '<td align="center">';
397 echo '<input type="checkbox" name="c'.$acourse->id.'" />';
398 echo '</td>';
399 } else {
400 echo '<td align="right">';
401 // print enrol info
402 if ($icons = enrol_get_course_info_icons($acourse)) {
403 foreach ($icons as $pix_icon) {
404 echo $OUTPUT->render($pix_icon);
407 if (!empty($acourse->summary)) {
408 $url = new moodle_url("/course/info.php?id=$acourse->id");
409 echo $OUTPUT->action_link($url, '<img alt="'.get_string('info').'" class="icon" src="'.$OUTPUT->pix_url('i/info') . '" />',
410 new popup_action('click', $url, 'courseinfo'), array('title'=>get_string('summary')));
412 echo "</td>";
414 echo "</tr>";
417 if ($abletomovecourses) {
418 $movetocategories = array();
419 $notused = array();
420 make_categories_list($movetocategories, $notused, 'moodle/category:manage');
421 $movetocategories[$category->id] = get_string('moveselectedcoursesto');
422 echo '<tr><td colspan="3" align="right">';
423 echo html_writer::label(get_string('moveselectedcoursesto'), 'movetoid', false, array('class' => 'accesshide'));
424 echo html_writer::select($movetocategories, 'moveto', $category->id, null, array('id'=>'movetoid'));
425 $PAGE->requires->js_init_call('M.util.init_select_autosubmit', array('movecourses', 'movetoid', false));
426 echo '<input type="hidden" name="id" value="'.$category->id.'" />';
427 echo '</td></tr>';
430 echo '</table>';
431 echo '</div></form>';
432 echo '<br />';
435 echo '<div class="buttons">';
436 if ($canmanage and $numcourses > 1) {
437 // Print button to re-sort courses by name
438 $url = new moodle_url('/course/category.php', array('id' => $category->id, 'resort' => 'name', 'sesskey' => sesskey()));
439 echo $OUTPUT->single_button($url, get_string('resortcoursesbyname'), 'get');
442 if (has_capability('moodle/course:create', $context)) {
443 // Print button to create a new course
444 $url = new moodle_url('/course/edit.php', array('category' => $category->id, 'returnto' => 'category'));
445 echo $OUTPUT->single_button($url, get_string('addnewcourse'), 'get');
448 if (!empty($CFG->enablecourserequests) && $category->id == $CFG->defaultrequestcategory) {
449 print_course_request_buttons(get_context_instance(CONTEXT_SYSTEM));
451 echo '</div>';
453 print_course_search();
455 echo $OUTPUT->footer();