MDL-29719 Option to display course shortname alongside fullname on course lists
[moodle.git] / course / category.php
1 <?php
2       // Displays the top level category or all courses
3       // In editing mode, allows the admin to edit a category,
4       // and rearrange courses
6     require_once("../config.php");
7     require_once("lib.php");
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
12     $categoryedit = optional_param('categoryedit', -1, PARAM_BOOL);
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);
20     $site = get_site();
22     if (empty($id)) {
23         print_error("unknowcategory");
24     }
26     $PAGE->set_category_by_id($id);
27     $urlparams = array('id' => $id);
28     if ($page) {
29         $urlparams['page'] = $page;
30     }
31     if ($perpage) {
32         $urlparams['perpage'] = $perpage;
33     }
34     $PAGE->set_url(new moodle_url('/course/category.php', array('id' => $id)));
35     navigation_node::override_active_url($PAGE->url);
36     $context = $PAGE->context;
37     $category = $PAGE->category;
39     $canedit = can_edit_in_category($category->id);
40     if ($canedit) {
41         if ($categoryedit !== -1) {
42             $USER->editing = $categoryedit;
43         }
44         require_login();
45         $editingon = $PAGE->user_is_editing();
46     } else {
47         if ($CFG->forcelogin) {
48             require_login();
49         }
50         $editingon = false;
51     }
53     if (!$category->visible) {
54         require_capability('moodle/category:viewhiddencategories', $context);
55     }
57     // Process any category actions.
58     if (has_capability('moodle/category:manage', $context)) {
59         /// Resort the category if requested
60         if ($resort and confirm_sesskey()) {
61             if ($courses = get_courses($category->id, "fullname ASC", 'c.id,c.fullname,c.sortorder')) {
62                 $i = 1;
63                 foreach ($courses as $course) {
64                     $DB->set_field('course', 'sortorder', $category->sortorder+$i, array('id'=>$course->id));
65                     $i++;
66                 }
67                 fix_course_sortorder(); // should not be needed
68             }
69         }
70     }
72     // Process any course actions.
73     if ($editingon) {
74     /// Move a specified course to a new category
75         if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) {   // Some courses are being moved
76             // user must have category update in both cats to perform this
77             require_capability('moodle/category:manage', $context);
78             require_capability('moodle/category:manage', get_context_instance(CONTEXT_COURSECAT, $moveto));
80             if (!$destcategory = $DB->get_record('course_categories', array('id' => $data->moveto))) {
81                 print_error('cannotfindcategory', '', '', $data->moveto);
82             }
84             $courses = array();
85             foreach ($data as $key => $value) {
86                 if (preg_match('/^c\d+$/', $key)) {
87                     $courseid = substr($key, 1);
88                     array_push($courses, $courseid);
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                     }
98                 }
99             }
100             move_courses($courses, $data->moveto);
101         }
103     /// Hide or show a course
104         if ((!empty($hide) or !empty($show)) and confirm_sesskey()) {
105             if (!empty($hide)) {
106                 $course = $DB->get_record('course', array('id' => $hide));
107                 $visible = 0;
108             } else {
109                 $course = $DB->get_record('course', array('id' => $show));
110                 $visible = 1;
111             }
113             if ($course) {
114                 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
115                 require_capability('moodle/course:visibility', $coursecontext);
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
118             }
119         }
122     /// Move a course up or down
123         if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
124             require_capability('moodle/category:manage', $context);
126             // Ensure the course order has continuous ordering
127             fix_course_sortorder();
128             $swapcourse = NULL;
130             if (!empty($moveup)) {
131                 if ($movecourse = $DB->get_record('course', array('id' => $moveup))) {
132                     $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder - 1));
133                 }
134             } else {
135                 if ($movecourse = $DB->get_record('course', array('id' => $movedown))) {
136                     $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder + 1));
137                 }
138             }
139             if ($swapcourse and $movecourse) {
140                 // check course's category
141                 if ($movecourse->category != $id) {
142                     print_error('coursedoesnotbelongtocategory');
143                 }
144                 $DB->set_field('course', 'sortorder', $swapcourse->sortorder, array('id' => $movecourse->id));
145                 $DB->set_field('course', 'sortorder', $movecourse->sortorder, array('id' => $swapcourse->id));
146             }
147         }
149     } // End of editing stuff
151     // Print headings
152     $numcategories = $DB->count_records('course_categories');
154     $stradministration = get_string('administration');
155     $strcategories = get_string('categories');
156     $strcategory = get_string('category');
157     $strcourses = get_string('courses');
159     if ($editingon && can_edit_in_category()) {
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.
162         require_once($CFG->libdir . '/adminlib.php');
163         admin_externalpage_setup('coursemgmt', '', $urlparams, $CFG->wwwroot . '/course/category.php');
164         $PAGE->set_context($context);   // Ensure that we are actually showing blocks etc for the cat context
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         }
172         echo $OUTPUT->header();
173     } else {
174         $PAGE->set_title("$site->shortname: $category->name");
175         $PAGE->set_heading($site->fullname);
176         $PAGE->set_button(print_course_search('', true, 'navbar'));
177         $PAGE->set_pagelayout('coursecategory');
178         echo $OUTPUT->header();
179     }
181 /// Print the category selector
182     $displaylist = array();
183     $notused = array();
184     make_categories_list($displaylist, $notused);
186     echo '<div class="categorypicker">';
187     $select = new single_select(new moodle_url('category.php'), 'id', $displaylist, $category->id, null, 'switchcategory');
188     $select->set_label($strcategories.':');
189     echo $OUTPUT->render($select);
190     echo '</div>';
192 /// Print current category description
193     if (!$editingon && $category->description) {
194         echo $OUTPUT->box_start();
195         $options = new stdClass;
196         $options->noclean = true;
197         $options->para = false;
198         $options->overflowdiv = true;
199         if (!isset($category->descriptionformat)) {
200             $category->descriptionformat = FORMAT_MOODLE;
201         }
202         $text = file_rewrite_pluginfile_urls($category->description, 'pluginfile.php', $context->id, 'coursecat', 'description', null);
203         echo format_text($text, $category->descriptionformat, $options);
204         echo $OUTPUT->box_end();
205     }
207     if ($editingon && has_capability('moodle/category:manage', $context)) {
208         echo $OUTPUT->container_start('buttons');
210         // Print button to update this category
211         $options = array('id' => $category->id);
212         echo $OUTPUT->single_button(new moodle_url('/course/editcategory.php', $options), get_string('editcategorythis'), 'get');
214         // Print button for creating new categories
215         $options = array('parent' => $category->id);
216         echo $OUTPUT->single_button(new moodle_url('/course/editcategory.php', $options), get_string('addsubcategory'), 'get');
218         echo $OUTPUT->container_end();
219     }
221 /// Print out all the sub-categories
222     if ($subcategories = $DB->get_records('course_categories', array('parent' => $category->id), 'sortorder ASC')) {
223         $firstentry = true;
224         foreach ($subcategories as $subcategory) {
225             if ($subcategory->visible || has_capability('moodle/category:viewhiddencategories', $context)) {
226                 $subcategorieswereshown = true;
227                 if ($firstentry) {
228                     echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter">';
229                     echo '<tr><th scope="col">'.get_string('subcategories').'</th></tr>';
230                     echo '<tr><td style="white-space: nowrap">';
231                     $firstentry = false;
232                 }
233                 $catlinkcss = $subcategory->visible ? '' : ' class="dimmed" ';
234                 echo '<a '.$catlinkcss.' href="category.php?id='.$subcategory->id.'">'.
235                      format_string($subcategory->name, true, array('context' => get_context_instance(CONTEXT_COURSECAT, $subcategory->id))).'</a><br />';
236             }
237         }
238         if (!$firstentry) {
239             echo '</td></tr></table>';
240             echo '<br />';
241         }
242     }
244 /// Print out all the courses
245     $courses = get_courses_page($category->id, 'c.sortorder ASC',
246             'c.id,c.sortorder,c.shortname,c.fullname,c.summary,c.visible',
247             $totalcount, $page*$perpage, $perpage);
248     $numcourses = count($courses);
250     if (!$courses) {
251         if (empty($subcategorieswereshown)) {
252             echo $OUTPUT->heading(get_string("nocoursesyet"));
253         }
255     } else if ($numcourses <= COURSE_MAX_SUMMARIES_PER_PAGE and !$page and !$editingon) {
256         echo $OUTPUT->box_start('courseboxes');
257         print_courses($category);
258         echo $OUTPUT->box_end();
260     } else {
261         echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "/course/category.php?id=$category->id&perpage=$perpage");
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');
278         echo '<form id="movecourses" action="category.php" method="post"><div>';
279         echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
280         echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter"><tr>';
281         echo '<th class="header" scope="col">'.$strcourses.'</th>';
282         if ($editingon) {
283             echo '<th class="header" scope="col">'.$stredit.'</th>';
284             echo '<th class="header" scope="col">'.$strselect.'</th>';
285         } else {
286             echo '<th class="header" scope="col">&nbsp;</th>';
287         }
288         echo '</tr>';
291         $count = 0;
292         $abletomovecourses = false;  // for now
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);
298             if ($perpage > 0) {
299                 $atlastpage = (($page + 1) == ceil($totalcount / $perpage));
300             } else {
301                 $atlastpage = true;
302             }
303         } else {
304             $atfirstpage = true;
305             $atlastpage = true;
306         }
308         foreach ($courses as $acourse) {
309             $coursecontext = get_context_instance(CONTEXT_COURSE, $acourse->id);
311             $count++;
312             $up = ($count > 1 || !$atfirstpage);
313             $down = ($count < $numcourses || !$atlastpage);
315             $linkcss = $acourse->visible ? '' : ' class="dimmed" ';
316             echo '<tr>';
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>';
319             if ($editingon) {
320                 echo '<td>';
321                 if (has_capability('moodle/course:update', $coursecontext)) {
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));
325                 }
327                 // role assignment link
328                 if (has_capability('moodle/course:enrolreview', $coursecontext)) {
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')));
331                 }
333                 if (can_delete_course($acourse->id)) {
334                     echo $OUTPUT->action_icon(new moodle_url('/course/delete.php', array('id' => $acourse->id)),
335                             new pix_icon('t/delete', $strdelete));
336                 }
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)) {
340                     if (!empty($acourse->visible)) {
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));
345                     } else {
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));
350                     }
351                 }
353                 if (has_capability('moodle/backup:backupcourse', $coursecontext)) {
354                     echo $OUTPUT->action_icon(new moodle_url('/backup/backup.php', array('id' => $acourse->id)),
355                             new pix_icon('t/backup', $strbackup));
356                 }
358                 if (has_capability('moodle/restore:restorecourse', $coursecontext)) {
359                     echo $OUTPUT->action_icon(new moodle_url('/backup/restorefile.php', array('contextid' => $coursecontext->id)),
360                             new pix_icon('t/restore', $strrestore));
361                 }
363                 if (has_capability('moodle/category:manage', $context)) {
364                     if ($up) {
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));
369                     }
371                     if ($down) {
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));
376                     }
377                     $abletomovecourses = true;
378                 }
380                 echo '</td>';
381                 echo '<td align="center">';
382                 echo '<input type="checkbox" name="c'.$acourse->id.'" />';
383                 echo '</td>';
384             } else {
385                 echo '<td align="right">';
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                 }
392                 if (!empty($acourse->summary)) {
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));
396                 }
397                 echo "</td>";
398             }
399             echo "</tr>";
400         }
402         if ($abletomovecourses) {
403             $movetocategories = array();
404             $notused = array();
405             make_categories_list($movetocategories, $notused, 'moodle/category:manage');
406             $movetocategories[$category->id] = get_string('moveselectedcoursesto');
407             echo '<tr><td colspan="3" align="right">';
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));
410             echo '<input type="hidden" name="id" value="'.$category->id.'" />';
411             echo '</td></tr>';
412         }
414         echo '</table>';
415         echo '</div></form>';
416         echo '<br />';
417     }
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
422         unset($options);
423         $options['id'] = $category->id;
424         $options['resort'] = 'name';
425         $options['sesskey'] = sesskey();
426         echo $OUTPUT->single_button(new moodle_url('category.php', $options), get_string('resortcoursesbyname'), 'get');
427     }
429     if (has_capability('moodle/course:create', $context)) {
430     /// Print button to create a new course
431         unset($options);
432         $options['category'] = $category->id;
433         $options['returnto'] = 'category';
434         echo $OUTPUT->single_button(new moodle_url('edit.php', $options), get_string('addnewcourse'), 'get');
435     }
437     if (!empty($CFG->enablecourserequests) && $category->id == $CFG->defaultrequestcategory) {
438         print_course_request_buttons(get_context_instance(CONTEXT_SYSTEM));
439     }
440     echo '</div>';
442     print_course_search();
444     echo $OUTPUT->footer();