MDL-29719 Option to display course shortname alongside fullname on course lists
[moodle.git] / course / category.php
CommitLineData
58b45fd7 1<?php
d2b6ba70 2 // Displays the top level category or all courses
daa27ce4 3 // In editing mode, allows the admin to edit a category,
d2b6ba70 4 // and rearrange courses
c2cb4545 5
4f006bc1 6 require_once("../config.php");
7 require_once("lib.php");
c2cb4545 8
8ed5dd63 9 $id = required_param('id', PARAM_INT); // Category id
10 $page = optional_param('page', 0, PARAM_INT); // which page to show
11 $perpage = optional_param('perpage', $CFG->coursesperpage, PARAM_INT); // how many per page
1be4d00d 12 $categoryedit = optional_param('categoryedit', -1, PARAM_BOOL);
8ed5dd63 13 $hide = optional_param('hide', 0, PARAM_INT);
14 $show = optional_param('show', 0, PARAM_INT);
15 $moveup = optional_param('moveup', 0, PARAM_INT);
16 $movedown = optional_param('movedown', 0, PARAM_INT);
17 $moveto = optional_param('moveto', 0, PARAM_INT);
18 $resort = optional_param('resort', 0, PARAM_BOOL);
c2cb4545 19
3f77c158 20 $site = get_site();
58b45fd7 21
10ed581c 22 if (empty($id)) {
ba6018a9 23 print_error("unknowcategory");
10ed581c 24 }
e295df44 25
948203a5 26 $PAGE->set_category_by_id($id);
a102baea 27 $urlparams = array('id' => $id);
28 if ($page) {
29 $urlparams['page'] = $page;
30 }
31 if ($perpage) {
32 $urlparams['perpage'] = $perpage;
33 }
afc45eb1
SH
34 $PAGE->set_url(new moodle_url('/course/category.php', array('id' => $id)));
35 navigation_node::override_active_url($PAGE->url);
948203a5 36 $context = $PAGE->context;
37 $category = $PAGE->category;
4f006bc1 38
a102baea 39 $canedit = can_edit_in_category($category->id);
40 if ($canedit) {
1be4d00d 41 if ($categoryedit !== -1) {
830dd6e9 42 $USER->editing = $categoryedit;
d2b6ba70 43 }
ebff4779 44 require_login();
ebff4779 45 $editingon = $PAGE->user_is_editing();
d2b6ba70 46 } else {
ebff4779 47 if ($CFG->forcelogin) {
48 require_login();
49 }
8ed5dd63 50 $editingon = false;
d2b6ba70 51 }
c2cb4545 52
ebff4779 53 if (!$category->visible) {
54 require_capability('moodle/category:viewhiddencategories', $context);
55 }
56
8ed5dd63 57 // Process any category actions.
58 if (has_capability('moodle/category:manage', $context)) {
52a554db 59 /// Resort the category if requested
de9924ae 60 if ($resort and confirm_sesskey()) {
ba87a4da 61 if ($courses = get_courses($category->id, "fullname ASC", 'c.id,c.fullname,c.sortorder')) {
0cbe8111 62 $i = 1;
6bc502cc 63 foreach ($courses as $course) {
0cbe8111 64 $DB->set_field('course', 'sortorder', $category->sortorder+$i, array('id'=>$course->id));
65 $i++;
6bc502cc 66 }
0cbe8111 67 fix_course_sortorder(); // should not be needed
6bc502cc 68 }
52a554db 69 }
49d3bab8 70 }
71
1fb3d044 72 // Process any course actions.
8ed5dd63 73 if ($editingon) {
daa27ce4 74 /// Move a specified course to a new category
294ce987 75 if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) { // Some courses are being moved
115a622d 76 // user must have category update in both cats to perform this
8ed5dd63 77 require_capability('moodle/category:manage', $context);
78 require_capability('moodle/category:manage', get_context_instance(CONTEXT_COURSECAT, $moveto));
e295df44 79
8ed5dd63 80 if (!$destcategory = $DB->get_record('course_categories', array('id' => $data->moveto))) {
81 print_error('cannotfindcategory', '', '', $data->moveto);
f2bb0045 82 }
83
e295df44 84 $courses = array();
8ed5dd63 85 foreach ($data as $key => $value) {
861efb19 86 if (preg_match('/^c\d+$/', $key)) {
4c211e95 87 $courseid = substr($key, 1);
88 array_push($courses, $courseid);
89
90 // check this course's category
91 if ($movingcourse = $DB->get_record('course', array('id'=>$courseid))) {
92 if ($movingcourse->category != $id ) {
93 print_error('coursedoesnotbelongtocategory');
94 }
95 } else {
96 print_error('cannotfindcourse');
97 }
d2b6ba70 98 }
e295df44 99 }
861efb19 100 move_courses($courses, $data->moveto);
c2cb4545 101 }
c2cb4545 102
daa27ce4 103 /// Hide or show a course
7168423c 104 if ((!empty($hide) or !empty($show)) and confirm_sesskey()) {
105 if (!empty($hide)) {
8ed5dd63 106 $course = $DB->get_record('course', array('id' => $hide));
d2b6ba70 107 $visible = 0;
108 } else {
8ed5dd63 109 $course = $DB->get_record('course', array('id' => $show));
d2b6ba70 110 $visible = 1;
111 }
4c211e95 112
d2b6ba70 113 if ($course) {
4c211e95 114 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
115 require_capability('moodle/course:visibility', $coursecontext);
df997f84
PS
116 $DB->set_field('course', 'visible', $visible, array('id' => $course->id));
117 $DB->set_field('course', 'visibleold', $visible, array('id' => $course->id)); // we set the old flag when user manually changes visibility of course
d2b6ba70 118 }
119 }
c2cb4545 120
c2cb4545 121
d2b6ba70 122 /// Move a course up or down
7168423c 123 if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
8ed5dd63 124 require_capability('moodle/category:manage', $context);
daa27ce4 125
8ed5dd63 126 // Ensure the course order has continuous ordering
0cbe8111 127 fix_course_sortorder();
128 $swapcourse = NULL;
daa27ce4 129
7168423c 130 if (!empty($moveup)) {
8ed5dd63 131 if ($movecourse = $DB->get_record('course', array('id' => $moveup))) {
132 $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder - 1));
0cbe8111 133 }
ba87a4da 134 } else {
8ed5dd63 135 if ($movecourse = $DB->get_record('course', array('id' => $movedown))) {
136 $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder + 1));
d2b6ba70 137 }
c2cb4545 138 }
0cbe8111 139 if ($swapcourse and $movecourse) {
4c211e95 140 // check course's category
141 if ($movecourse->category != $id) {
142 print_error('coursedoesnotbelongtocategory');
143 }
8ed5dd63 144 $DB->set_field('course', 'sortorder', $swapcourse->sortorder, array('id' => $movecourse->id));
145 $DB->set_field('course', 'sortorder', $movecourse->sortorder, array('id' => $swapcourse->id));
0cbe8111 146 }
ba87a4da 147 }
d2b6ba70 148
149 } // End of editing stuff
c2cb4545 150
1fb3d044 151 // Print headings
152 $numcategories = $DB->count_records('course_categories');
153
154 $stradministration = get_string('administration');
155 $strcategories = get_string('categories');
156 $strcategory = get_string('category');
157 $strcourses = get_string('courses');
158
a102baea 159 if ($editingon && can_edit_in_category()) {
1fb3d044 160 // Integrate into the admin tree only if the user can edit categories at the top level,
161 // otherwise the admin block does not appear to this user, and you get an error.
a102baea 162 require_once($CFG->libdir . '/adminlib.php');
163 admin_externalpage_setup('coursemgmt', '', $urlparams, $CFG->wwwroot . '/course/category.php');
17f950eb 164 $PAGE->set_context($context); // Ensure that we are actually showing blocks etc for the cat context
afc45eb1
SH
165
166 $settingsnode = $PAGE->settingsnav->find_active_node();
167 if ($settingsnode) {
168 $settingsnode->make_inactive();
169 $settingsnode->force_open();
170 $PAGE->navbar->add($settingsnode->text, $settingsnode->action);
171 }
61ef8f9f 172 echo $OUTPUT->header();
214a81b8 173 } else {
0a122046 174 $PAGE->set_title("$site->shortname: $category->name");
b3ddf59a 175 $PAGE->set_heading($site->fullname);
0a122046 176 $PAGE->set_button(print_course_search('', true, 'navbar'));
191b267b 177 $PAGE->set_pagelayout('coursecategory');
0a122046 178 echo $OUTPUT->header();
1fb3d044 179 }
180
1fb3d044 181/// Print the category selector
182 $displaylist = array();
183 $notused = array();
184 make_categories_list($displaylist, $notused);
185
186 echo '<div class="categorypicker">';
f8dab966 187 $select = new single_select(new moodle_url('category.php'), 'id', $displaylist, $category->id, null, 'switchcategory');
64e12bb7 188 $select->set_label($strcategories.':');
f8dab966 189 echo $OUTPUT->render($select);
1fb3d044 190 echo '</div>';
191
192/// Print current category description
193 if (!$editingon && $category->description) {
98b369f8 194 echo $OUTPUT->box_start();
8bdc9cac
SH
195 $options = new stdClass;
196 $options->noclean = true;
197 $options->para = false;
367a75fa 198 $options->overflowdiv = true;
8bdc9cac
SH
199 if (!isset($category->descriptionformat)) {
200 $category->descriptionformat = FORMAT_MOODLE;
201 }
64f93798 202 $text = file_rewrite_pluginfile_urls($category->description, 'pluginfile.php', $context->id, 'coursecat', 'description', null);
214a81b8 203 echo format_text($text, $category->descriptionformat, $options);
98b369f8 204 echo $OUTPUT->box_end();
1fb3d044 205 }
206
8ed5dd63 207 if ($editingon && has_capability('moodle/category:manage', $context)) {
b4531207 208 echo $OUTPUT->container_start('buttons');
09deab06 209
8ed5dd63 210 // Print button to update this category
211 $options = array('id' => $category->id);
a6855934 212 echo $OUTPUT->single_button(new moodle_url('/course/editcategory.php', $options), get_string('editcategorythis'), 'get');
8ed5dd63 213
214 // Print button for creating new categories
215 $options = array('parent' => $category->id);
a6855934 216 echo $OUTPUT->single_button(new moodle_url('/course/editcategory.php', $options), get_string('addsubcategory'), 'get');
09deab06 217
b4531207 218 echo $OUTPUT->container_end();
09deab06 219 }
220
8210692d 221/// Print out all the sub-categories
8ed5dd63 222 if ($subcategories = $DB->get_records('course_categories', array('parent' => $category->id), 'sortorder ASC')) {
92c26858 223 $firstentry = true;
8210692d 224 foreach ($subcategories as $subcategory) {
8ed5dd63 225 if ($subcategory->visible || has_capability('moodle/category:viewhiddencategories', $context)) {
15cf5d21 226 $subcategorieswereshown = true;
92c26858 227 if ($firstentry) {
4e957c86 228 echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter">';
54926e78 229 echo '<tr><th scope="col">'.get_string('subcategories').'</th></tr>';
fbe31d22 230 echo '<tr><td style="white-space: nowrap">';
92c26858 231 $firstentry = false;
232 }
8ed5dd63 233 $catlinkcss = $subcategory->visible ? '' : ' class="dimmed" ';
e5e81e78 234 echo '<a '.$catlinkcss.' href="category.php?id='.$subcategory->id.'">'.
63390481 235 format_string($subcategory->name, true, array('context' => get_context_instance(CONTEXT_COURSECAT, $subcategory->id))).'</a><br />';
92c26858 236 }
237 }
238 if (!$firstentry) {
8ed5dd63 239 echo '</td></tr></table>';
240 echo '<br />';
8210692d 241 }
8210692d 242 }
daa27ce4 243
d2b6ba70 244/// Print out all the courses
daa27ce4 245 $courses = get_courses_page($category->id, 'c.sortorder ASC',
df997f84 246 'c.id,c.sortorder,c.shortname,c.fullname,c.summary,c.visible',
8ed5dd63 247 $totalcount, $page*$perpage, $perpage);
f1c1b17a 248 $numcourses = count($courses);
c2cb4545 249
e646a256 250 if (!$courses) {
15cf5d21 251 if (empty($subcategorieswereshown)) {
7c5286cd 252 echo $OUTPUT->heading(get_string("nocoursesyet"));
15cf5d21 253 }
c2cb4545 254
8ed5dd63 255 } else if ($numcourses <= COURSE_MAX_SUMMARIES_PER_PAGE and !$page and !$editingon) {
98b369f8 256 echo $OUTPUT->box_start('courseboxes');
35d0244a 257 print_courses($category);
98b369f8 258 echo $OUTPUT->box_end();
c2cb4545 259
daa27ce4 260 } else {
8c5ce0ec 261 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "/course/category.php?id=$category->id&perpage=$perpage");
8ef9cb56 262
8ed5dd63 263 $strcourses = get_string('courses');
264 $strselect = get_string('select');
265 $stredit = get_string('edit');
266 $strdelete = get_string('delete');
267 $strbackup = get_string('backup');
268 $strrestore = get_string('restore');
269 $strmoveup = get_string('moveup');
270 $strmovedown = get_string('movedown');
271 $strupdate = get_string('update');
272 $strhide = get_string('hide');
273 $strshow = get_string('show');
274 $strsummary = get_string('summary');
275 $strsettings = get_string('settings');
c2cb4545 276
d2b6ba70 277
be636898 278 echo '<form id="movecourses" action="category.php" method="post"><div>';
d8807190 279 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
be636898 280 echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter"><tr>';
281 echo '<th class="header" scope="col">'.$strcourses.'</th>';
8ed5dd63 282 if ($editingon) {
be636898 283 echo '<th class="header" scope="col">'.$stredit.'</th>';
8ed5dd63 284 echo '<th class="header" scope="col">'.$strselect.'</th>';
3d95bdb7 285 } else {
be636898 286 echo '<th class="header" scope="col">&nbsp;</th>';
d2b6ba70 287 }
d8807190 288 echo '</tr>';
d2b6ba70 289
c2cb4545 290
c2cb4545 291 $count = 0;
f2bb0045 292 $abletomovecourses = false; // for now
c2cb4545 293
c851eb94 294 // Checking if we are at the first or at the last page, to allow courses to
295 // be moved up and down beyond the paging border
296 if ($totalcount > $perpage) {
297 $atfirstpage = ($page == 0);
7b0d5cd5 298 if ($perpage > 0) {
299 $atlastpage = (($page + 1) == ceil($totalcount / $perpage));
300 } else {
301 $atlastpage = true;
302 }
c851eb94 303 } else {
304 $atfirstpage = true;
305 $atlastpage = true;
306 }
307
e081f299 308 foreach ($courses as $acourse) {
4f0c2d00 309 $coursecontext = get_context_instance(CONTEXT_COURSE, $acourse->id);
e295df44 310
c2cb4545 311 $count++;
c851eb94 312 $up = ($count > 1 || !$atfirstpage);
313 $down = ($count < $numcourses || !$atlastpage);
c2cb4545 314
8ed5dd63 315 $linkcss = $acourse->visible ? '' : ' class="dimmed" ';
d8807190 316 echo '<tr>';
7fb46992 317 $coursename = get_course_display_name_for_list($course);
318 echo '<td><a '.$linkcss.' href="view.php?id='.$acourse->id.'">'. format_string($coursename) .'</a></td>';
8ed5dd63 319 if ($editingon) {
320 echo '<td>';
4827a1d1 321 if (has_capability('moodle/course:update', $coursecontext)) {
47cdfde3
DM
322 echo $OUTPUT->action_icon(new moodle_url('/course/edit.php',
323 array('id' => $acourse->id, 'category' => $id, 'returnto' => 'category')),
324 new pix_icon('t/edit', $strsettings));
8ed5dd63 325 }
e295df44 326
327 // role assignment link
6163adcc 328 if (has_capability('moodle/course:enrolreview', $coursecontext)) {
47cdfde3
DM
329 echo $OUTPUT->action_icon(new moodle_url('/enrol/users.php', array('id' => $acourse->id)),
330 new pix_icon('i/users', get_string('enrolledusers', 'enrol')));
e295df44 331 }
332
2585a68d 333 if (can_delete_course($acourse->id)) {
47cdfde3
DM
334 echo $OUTPUT->action_icon(new moodle_url('/course/delete.php', array('id' => $acourse->id)),
335 new pix_icon('t/delete', $strdelete));
555b75f4 336 }
bbbf2d40 337
cbc943f7 338 // MDL-8885, users with no capability to view hidden courses, should not be able to lock themselves out
339 if (has_capability('moodle/course:visibility', $coursecontext) && has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
555b75f4 340 if (!empty($acourse->visible)) {
47cdfde3
DM
341 echo $OUTPUT->action_icon(new moodle_url('/course/category.php',
342 array('id' => $category->id, 'page' => $page, 'perpage' => $perpage,
343 'hide' => $acourse->id, 'sesskey' => sesskey())),
344 new pix_icon('t/hide', $strhide));
555b75f4 345 } else {
47cdfde3
DM
346 echo $OUTPUT->action_icon(new moodle_url('/course/category.php',
347 array('id' => $category->id, 'page' => $page, 'perpage' => $perpage,
348 'show' => $acourse->id, 'sesskey' => sesskey())),
349 new pix_icon('t/show', $strshow));
bbbf2d40 350 }
555b75f4 351 }
bbbf2d40 352
d2940003 353 if (has_capability('moodle/backup:backupcourse', $coursecontext)) {
47cdfde3
DM
354 echo $OUTPUT->action_icon(new moodle_url('/backup/backup.php', array('id' => $acourse->id)),
355 new pix_icon('t/backup', $strbackup));
555b75f4 356 }
e295df44 357
d2940003 358 if (has_capability('moodle/restore:restorecourse', $coursecontext)) {
47cdfde3
DM
359 echo $OUTPUT->action_icon(new moodle_url('/backup/restorefile.php', array('contextid' => $coursecontext->id)),
360 new pix_icon('t/restore', $strrestore));
555b75f4 361 }
daa27ce4 362
8ed5dd63 363 if (has_capability('moodle/category:manage', $context)) {
d2b6ba70 364 if ($up) {
47cdfde3
DM
365 echo $OUTPUT->action_icon(new moodle_url('/course/category.php',
366 array('id' => $category->id, 'page' => $page, 'perpage' => $perpage,
367 'moveup' => $acourse->id, 'sesskey' => sesskey())),
368 new pix_icon('t/up', $strmoveup));
d2b6ba70 369 }
daa27ce4 370
d2b6ba70 371 if ($down) {
47cdfde3
DM
372 echo $OUTPUT->action_icon(new moodle_url('/course/category.php',
373 array('id' => $category->id, 'page' => $page, 'perpage' => $perpage,
374 'movedown' => $acourse->id, 'sesskey' => sesskey())),
375 new pix_icon('t/down', $strmovedown));
d2b6ba70 376 }
f2bb0045 377 $abletomovecourses = true;
3d95bdb7 378 }
e295df44 379
d8807190 380 echo '</td>';
555b75f4 381 echo '<td align="center">';
382 echo '<input type="checkbox" name="c'.$acourse->id.'" />';
e295df44 383 echo '</td>';
3d95bdb7 384 } else {
d8807190 385 echo '<td align="right">';
bf423bb1
PS
386 // print enrol info
387 if ($icons = enrol_get_course_info_icons($acourse)) {
388 foreach ($icons as $pix_icon) {
389 echo $OUTPUT->render($pix_icon);
390 }
391 }
0f06e85a 392 if (!empty($acourse->summary)) {
75015e5f
PS
393 $link = new moodle_url("/course/info.php?id=$acourse->id");
394 echo $OUTPUT->action_link($link, '<img alt="'.get_string('info').'" class="icon" src="'.$OUTPUT->pix_url('i/info') . '" />',
395 new popup_action('click', $link, 'courseinfo'), array('title'=>$strsummary));
3d95bdb7 396 }
397 echo "</td>";
c2cb4545 398 }
c2cb4545 399 echo "</tr>";
400 }
f2bb0045 401
402 if ($abletomovecourses) {
8ed5dd63 403 $movetocategories = array();
404 $notused = array();
405 make_categories_list($movetocategories, $notused, 'moodle/category:manage');
406 $movetocategories[$category->id] = get_string('moveselectedcoursesto');
d8807190 407 echo '<tr><td colspan="3" align="right">';
edc28287
PS
408 echo html_writer::select($movetocategories, 'moveto', $category->id, null, array('id'=>'movetoid'));
409 $PAGE->requires->js_init_call('M.util.init_select_autosubmit', array('movecourses', 'movetoid', false));
e5e81e78 410 echo '<input type="hidden" name="id" value="'.$category->id.'" />';
d8807190 411 echo '</td></tr>';
f2bb0045 412 }
daa27ce4 413
d8807190 414 echo '</table>';
be636898 415 echo '</div></form>';
d8807190 416 echo '<br />';
c2cb4545 417 }
e295df44 418
8ed5dd63 419 echo '<div class="buttons">';
420 if (has_capability('moodle/category:manage', $context) and $numcourses > 1) {
421 /// Print button to re-sort courses by name
49d3bab8 422 unset($options);
d8807190 423 $options['id'] = $category->id;
424 $options['resort'] = 'name';
4d933beb 425 $options['sesskey'] = sesskey();
5c2ed7e2 426 echo $OUTPUT->single_button(new moodle_url('category.php', $options), get_string('resortcoursesbyname'), 'get');
c432fd32 427 }
49d3bab8 428
8ed5dd63 429 if (has_capability('moodle/course:create', $context)) {
430 /// Print button to create a new course
f2bb0045 431 unset($options);
d8807190 432 $options['category'] = $category->id;
7feebf6f 433 $options['returnto'] = 'category';
5c2ed7e2 434 echo $OUTPUT->single_button(new moodle_url('edit.php', $options), get_string('addnewcourse'), 'get');
c432fd32 435 }
77eddcd5 436
94b8fa5a 437 if (!empty($CFG->enablecourserequests) && $category->id == $CFG->defaultrequestcategory) {
77eddcd5 438 print_course_request_buttons(get_context_instance(CONTEXT_SYSTEM));
439 }
8ed5dd63 440 echo '</div>';
e295df44 441
61240489 442 print_course_search();
e295df44 443
d60c1124 444 echo $OUTPUT->footer();
c2cb4545 445