MDL-36622 usability: Adaptations following new icons
[moodle.git] / course / category.php
CommitLineData
58b45fd7 1<?php
f73cea54
SH
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.
13//
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/>.
16
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
21 *
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
26 */
27
28require_once("../config.php");
29require_once($CFG->dirroot.'/course/lib.php');
4b30377d 30require_once($CFG->libdir.'/textlib.class.php');
f73cea54
SH
31
32$id = required_param('id', PARAM_INT); // Category id
33$page = optional_param('page', 0, PARAM_INT); // which page to show
f73cea54
SH
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);
42
d9b87135 43// MDL-27824 - This is a temporary fix until we have the proper
98068e1d 44// way to check/initialize $CFG value.
08af0029 45// @todo MDL-35138 remove this temporary solution
929cdf06 46if (!empty($CFG->coursesperpage)) {
98068e1d
RW
47 $defaultperpage = $CFG->coursesperpage;
48} else {
49 $defaultperpage = 20;
50}
51$perpage = optional_param('perpage', $defaultperpage, PARAM_INT); // how many per page
52
f73cea54
SH
53if (empty($id)) {
54 print_error("unknowcategory");
55}
56
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;
63
64$canedit = can_edit_in_category($category->id);
65if ($canedit) {
66 if ($categoryedit !== -1) {
67 $USER->editing = $categoryedit;
a102baea 68 }
f73cea54
SH
69 require_login();
70 $editingon = $PAGE->user_is_editing();
71} else {
72 if ($CFG->forcelogin) {
ebff4779 73 require_login();
f73cea54
SH
74 }
75 $editingon = false;
76}
77
78if (!$category->visible) {
79 require_capability('moodle/category:viewhiddencategories', $context);
80}
81
82$canmanage = has_capability('moodle/category:manage', $context);
83$sesskeyprovided = !empty($sesskey) && confirm_sesskey($sesskey);
84
85// Process any category actions.
86if ($canmanage && $resort && $sesskeyprovided) {
87 // Resort the category if requested
2ee34c85 88 if ($courses = get_courses($category->id, '', 'c.id,c.fullname,c.sortorder')) {
4b30377d 89 collatorlib::asort_objects_by_property($courses, 'fullname', collatorlib::SORT_NATURAL);
f73cea54
SH
90 $i = 1;
91 foreach ($courses as $course) {
92 $DB->set_field('course', 'sortorder', $category->sortorder+$i, array('id'=>$course->id));
93 $i++;
ebff4779 94 }
f73cea54 95 fix_course_sortorder(); // should not be needed
d2b6ba70 96 }
f73cea54 97}
c2cb4545 98
f73cea54
SH
99// Process any course actions.
100if ($editingon && $sesskeyprovided) {
ebff4779 101
f73cea54
SH
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);
9a5e297b 107 require_capability('moodle/category:manage', context_coursecat::instance($moveto));
f73cea54
SH
108
109 if (!$destcategory = $DB->get_record('course_categories', array('id' => $data->moveto))) {
110 print_error('cannotfindcategory', '', '', $data->moveto);
52a554db 111 }
49d3bab8 112
f73cea54
SH
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);
f2bb0045 118
f73cea54
SH
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');
4c211e95 123 }
f73cea54
SH
124 } else {
125 print_error('cannotfindcourse');
d2b6ba70 126 }
e295df44 127 }
c2cb4545 128 }
f73cea54
SH
129 move_courses($courses, $data->moveto);
130 }
c2cb4545 131
f73cea54
SH
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;
140 }
4c211e95 141
f73cea54 142 if ($course) {
9a5e297b 143 $coursecontext = context_course::instance($course->id);
f73cea54 144 require_capability('moodle/course:visibility', $coursecontext);
7ca52615
AA
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()));
d2b6ba70 147 }
f73cea54 148 }
c2cb4545 149
c2cb4545 150
f73cea54
SH
151 // Move a course up or down
152 if (!empty($moveup) or !empty($movedown)) {
153 require_capability('moodle/category:manage', $context);
daa27ce4 154
f73cea54
SH
155 // Ensure the course order has continuous ordering
156 fix_course_sortorder();
157 $swapcourse = NULL;
daa27ce4 158
f73cea54
SH
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));
c2cb4545 162 }
f73cea54
SH
163 } else {
164 if ($movecourse = $DB->get_record('course', array('id' => $movedown))) {
165 $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder + 1));
0cbe8111 166 }
ba87a4da 167 }
f73cea54
SH
168 if ($swapcourse and $movecourse) {
169 // check course's category
170 if ($movecourse->category != $id) {
171 print_error('coursedoesnotbelongtocategory');
172 }
173 $DB->set_field('course', 'sortorder', $swapcourse->sortorder, array('id' => $movecourse->id));
174 $DB->set_field('course', 'sortorder', $movecourse->sortorder, array('id' => $swapcourse->id));
afc45eb1 175 }
1fb3d044 176 }
177
f73cea54
SH
178} // End of editing stuff
179
180// Prepare the standard URL params for this page. We'll need them later.
181$urlparams = array('id' => $id);
182if ($page) {
183 $urlparams['page'] = $page;
184}
185if ($perpage) {
186 $urlparams['perpage'] = $perpage;
187}
188
189// Begin output
190if ($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
196
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);
202 }
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();
211}
212
1fb3d044 213/// Print the category selector
f73cea54
SH
214$displaylist = array();
215$notused = array();
216make_categories_list($displaylist, $notused);
1fb3d044 217
f73cea54
SH
218echo '<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').':');
221echo $OUTPUT->render($select);
222echo '</div>';
1fb3d044 223
224/// Print current category description
f73cea54
SH
225if (!$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;
1fb3d044 233 }
f73cea54
SH
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();
237}
238
239if ($editingon && $canmanage) {
240 echo $OUTPUT->container_start('buttons');
241
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');
245
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');
249
250 echo $OUTPUT->container_end();
251}
252
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.
256if (has_capability('moodle/category:viewhiddencategories', $context)) {
257 $categorywhere = '';
258} else {
259 $categorywhere = 'AND cc.visible = 1';
260}
261// We're going to preload the context for the subcategory as we know that we
262// need it later on for formatting.
e141bc81
SH
263
264$ctxselect = context_helper::get_preload_record_columns_sql('ctx');
265$sql = "SELECT cc.*, $ctxselect
f73cea54 266 FROM {course_categories} cc
e141bc81
SH
267 JOIN {context} ctx ON cc.id = ctx.instanceid
268 WHERE cc.parent = :parentid AND
269 ctx.contextlevel = :contextlevel
f73cea54
SH
270 $categorywhere
271 ORDER BY cc.sortorder ASC";
e141bc81 272$subcategories = $DB->get_recordset_sql($sql, array('parentid' => $category->id, 'contextlevel' => CONTEXT_COURSECAT));
f73cea54
SH
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();
96e78552 278$baseurl = new moodle_url('/course/category.php');
f73cea54
SH
279foreach ($subcategories as $subcategory) {
280 // Preload the context we will need it to format the category name shortly.
e141bc81 281 context_helper::preload_from_record($subcategory);
9a5e297b 282 $context = context_coursecat::instance($subcategory->id);
f73cea54
SH
283 // Prepare the things we need to create a link to the subcategory
284 $attributes = $subcategory->visible ? array() : array('class' => 'dimmed');
f73cea54
SH
285 $text = format_string($subcategory->name, true, array('context' => $context));
286 // Add the subcategory to the table
7a3a0a30
SH
287 $baseurl->param('id', $subcategory->id);
288 $table->data[] = array(html_writer::link($baseurl, $text, $attributes));
f73cea54
SH
289}
290
291$subcategorieswereshown = (count($table->data) > 0);
292if ($subcategorieswereshown) {
293 echo html_writer::table($table);
294}
295
3b7bfbb5 296// Print out all the courses.
f73cea54
SH
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);
301
3b7bfbb5
FM
302// We can consider that we are using pagination when the total count of courses is different than the one returned.
303$pagingmode = $totalcount != $numcourses;
304
f73cea54 305if (!$courses) {
3b7bfbb5 306 // There is no course to display.
f73cea54
SH
307 if (empty($subcategorieswereshown)) {
308 echo $OUTPUT->heading(get_string("nocoursesyet"));
09deab06 309 }
3b7bfbb5
FM
310} else if ($numcourses <= $CFG->courseswithsummarieslimit and !$pagingmode and !$editingon) {
311 // We display courses with their summaries as we have not reached the limit, also we are not
312 // in paging mode and not allowed to edit either.
f73cea54
SH
313 echo $OUTPUT->box_start('courseboxes');
314 print_courses($category);
315 echo $OUTPUT->box_end();
f73cea54 316} else {
3b7bfbb5 317 // The conditions above have failed, we display a basic list of courses with paging/editing options.
f73cea54 318 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "/course/category.php?id=$category->id&perpage=$perpage");
c2cb4545 319
f73cea54
SH
320 echo '<form id="movecourses" action="category.php" method="post"><div>';
321 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
322 echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter"><tr>';
323 echo '<th class="header" scope="col">'.get_string('courses').'</th>';
324 if ($editingon) {
325 echo '<th class="header" scope="col">'.get_string('edit').'</th>';
326 echo '<th class="header" scope="col">'.get_string('select').'</th>';
327 } else {
328 echo '<th class="header" scope="col">&nbsp;</th>';
329 }
330 echo '</tr>';
c2cb4545 331
f73cea54
SH
332 $count = 0;
333 $abletomovecourses = false; // for now
c2cb4545 334
f73cea54
SH
335 // Checking if we are at the first or at the last page, to allow courses to
336 // be moved up and down beyond the paging border
337 if ($totalcount > $perpage) {
338 $atfirstpage = ($page == 0);
339 if ($perpage > 0) {
340 $atlastpage = (($page + 1) == ceil($totalcount / $perpage));
3d95bdb7 341 } else {
f73cea54 342 $atlastpage = true;
d2b6ba70 343 }
f73cea54
SH
344 } else {
345 $atfirstpage = true;
346 $atlastpage = true;
347 }
d2b6ba70 348
96e78552 349 $baseurl = new moodle_url('/course/category.php', $urlparams + array('sesskey' => sesskey()));
f73cea54 350 foreach ($courses as $acourse) {
9a5e297b 351 $coursecontext = context_course::instance($acourse->id);
c2cb4545 352
f73cea54
SH
353 $count++;
354 $up = ($count > 1 || !$atfirstpage);
355 $down = ($count < $numcourses || !$atlastpage);
c2cb4545 356
f73cea54
SH
357 $linkcss = $acourse->visible ? '' : ' class="dimmed" ';
358 echo '<tr>';
359 $coursename = get_course_display_name_for_list($acourse);
360 echo '<td><a '.$linkcss.' href="view.php?id='.$acourse->id.'">'. format_string($coursename) .'</a></td>';
361 if ($editingon) {
362 echo '<td>';
363 if (has_capability('moodle/course:update', $coursecontext)) {
364 $url = new moodle_url('/course/edit.php', array('id' => $acourse->id, 'category' => $id, 'returnto' => 'category'));
365 echo $OUTPUT->action_icon($url, new pix_icon('t/edit', get_string('settings')));
7b0d5cd5 366 }
c851eb94 367
f73cea54
SH
368 // role assignment link
369 if (has_capability('moodle/course:enrolreview', $coursecontext)) {
370 $url = new moodle_url('/enrol/users.php', array('id' => $acourse->id));
c42651d6 371 echo $OUTPUT->action_icon($url, new pix_icon('t/enrolusers', get_string('enrolledusers', 'enrol')));
f73cea54 372 }
e295df44 373
f73cea54
SH
374 if (can_delete_course($acourse->id)) {
375 $url = new moodle_url('/course/delete.php', array('id' => $acourse->id));
376 echo $OUTPUT->action_icon($url, new pix_icon('t/delete', get_string('delete')));
377 }
e295df44 378
f73cea54
SH
379 // MDL-8885, users with no capability to view hidden courses, should not be able to lock themselves out
380 if (has_capability('moodle/course:visibility', $coursecontext) && has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
f73cea54 381 if (!empty($acourse->visible)) {
96e78552 382 $url = new moodle_url($baseurl, array('hide' => $acourse->id));
f73cea54
SH
383 echo $OUTPUT->action_icon($url, new pix_icon('t/hide', get_string('hide')));
384 } else {
96e78552 385 $url = new moodle_url($baseurl, array('show' => $acourse->id));
f73cea54 386 echo $OUTPUT->action_icon($url, new pix_icon('t/show', get_string('show')));
555b75f4 387 }
f73cea54 388 }
bbbf2d40 389
f73cea54
SH
390 if (has_capability('moodle/backup:backupcourse', $coursecontext)) {
391 $url = new moodle_url('/backup/backup.php', array('id' => $acourse->id));
392 echo $OUTPUT->action_icon($url, new pix_icon('t/backup', get_string('backup')));
393 }
bbbf2d40 394
f73cea54
SH
395 if (has_capability('moodle/restore:restorecourse', $coursecontext)) {
396 $url = new moodle_url('/backup/restorefile.php', array('contextid' => $coursecontext->id));
397 echo $OUTPUT->action_icon($url, new pix_icon('t/restore', get_string('restore')));
398 }
e295df44 399
f73cea54 400 if ($canmanage) {
f73cea54 401 if ($up) {
96e78552 402 $url = new moodle_url($baseurl, array('moveup' => $acourse->id));
f73cea54 403 echo $OUTPUT->action_icon($url, new pix_icon('t/up', get_string('moveup')));
555b75f4 404 }
daa27ce4 405
f73cea54 406 if ($down) {
96e78552 407 $url = new moodle_url($baseurl, array('movedown' => $acourse->id));
f73cea54 408 echo $OUTPUT->action_icon($url, new pix_icon('t/down', get_string('movedown')));
3d95bdb7 409 }
f73cea54
SH
410 $abletomovecourses = true;
411 }
e295df44 412
f73cea54
SH
413 echo '</td>';
414 echo '<td align="center">';
415 echo '<input type="checkbox" name="c'.$acourse->id.'" />';
416 echo '</td>';
417 } else {
418 echo '<td align="right">';
419 // print enrol info
420 if ($icons = enrol_get_course_info_icons($acourse)) {
421 foreach ($icons as $pix_icon) {
422 echo $OUTPUT->render($pix_icon);
3d95bdb7 423 }
c2cb4545 424 }
f73cea54
SH
425 if (!empty($acourse->summary)) {
426 $url = new moodle_url("/course/info.php?id=$acourse->id");
427 echo $OUTPUT->action_link($url, '<img alt="'.get_string('info').'" class="icon" src="'.$OUTPUT->pix_url('i/info') . '" />',
428 new popup_action('click', $url, 'courseinfo'), array('title'=>get_string('summary')));
429 }
430 echo "</td>";
f2bb0045 431 }
f73cea54 432 echo "</tr>";
c2cb4545 433 }
e295df44 434
f73cea54
SH
435 if ($abletomovecourses) {
436 $movetocategories = array();
437 $notused = array();
438 make_categories_list($movetocategories, $notused, 'moodle/category:manage');
439 $movetocategories[$category->id] = get_string('moveselectedcoursesto');
440 echo '<tr><td colspan="3" align="right">';
70ad5136 441 echo html_writer::label(get_string('moveselectedcoursesto'), 'movetoid', false, array('class' => 'accesshide'));
f73cea54
SH
442 echo html_writer::select($movetocategories, 'moveto', $category->id, null, array('id'=>'movetoid'));
443 $PAGE->requires->js_init_call('M.util.init_select_autosubmit', array('movecourses', 'movetoid', false));
444 echo '<input type="hidden" name="id" value="'.$category->id.'" />';
445 echo '</td></tr>';
c432fd32 446 }
49d3bab8 447
f73cea54
SH
448 echo '</table>';
449 echo '</div></form>';
450 echo '<br />';
451}
77eddcd5 452
f73cea54
SH
453echo '<div class="buttons">';
454if ($canmanage and $numcourses > 1) {
455 // Print button to re-sort courses by name
456 $url = new moodle_url('/course/category.php', array('id' => $category->id, 'resort' => 'name', 'sesskey' => sesskey()));
457 echo $OUTPUT->single_button($url, get_string('resortcoursesbyname'), 'get');
458}
459
460if (has_capability('moodle/course:create', $context)) {
461 // Print button to create a new course
462 $url = new moodle_url('/course/edit.php', array('category' => $category->id, 'returnto' => 'category'));
463 echo $OUTPUT->single_button($url, get_string('addnewcourse'), 'get');
464}
e295df44 465
f73cea54 466if (!empty($CFG->enablecourserequests) && $category->id == $CFG->defaultrequestcategory) {
9a5e297b 467 print_course_request_buttons(context_system::instance());
f73cea54
SH
468}
469echo '</div>';
e295df44 470
f73cea54 471print_course_search();
c2cb4545 472
7a3a0a30 473echo $OUTPUT->footer();