MDL-31018 Improve consistency of horizontal alignment across grade reports
[moodle.git] / course / category.php
blob9ed7ebca867d71e5c58ab345d084874b5e8788c5
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");
26 $PAGE->set_category_by_id($id);
27 $urlparams = array('id' => $id);
28 if ($page) {
29 $urlparams['page'] = $page;
31 if ($perpage) {
32 $urlparams['perpage'] = $perpage;
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;
44 require_login();
45 $editingon = $PAGE->user_is_editing();
46 } else {
47 if ($CFG->forcelogin) {
48 require_login();
50 $editingon = false;
53 if (!$category->visible) {
54 require_capability('moodle/category:viewhiddencategories', $context);
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++;
67 fix_course_sortorder(); // should not be needed
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);
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');
95 } else {
96 print_error('cannotfindcourse');
100 move_courses($courses, $data->moveto);
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;
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
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));
134 } else {
135 if ($movecourse = $DB->get_record('course', array('id' => $movedown))) {
136 $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder + 1));
139 if ($swapcourse and $movecourse) {
140 // check course's category
141 if ($movecourse->category != $id) {
142 print_error('coursedoesnotbelongtocategory');
144 $DB->set_field('course', 'sortorder', $swapcourse->sortorder, array('id' => $movecourse->id));
145 $DB->set_field('course', 'sortorder', $movecourse->sortorder, array('id' => $swapcourse->id));
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);
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();
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;
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();
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();
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;
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 />';
238 if (!$firstentry) {
239 echo '</td></tr></table>';
240 echo '<br />';
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"));
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>';
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;
303 } else {
304 $atfirstpage = true;
305 $atlastpage = true;
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 echo '<td><a '.$linkcss.' href="view.php?id='.$acourse->id.'">'. format_string($acourse->fullname) .'</a></td>';
318 if ($editingon) {
319 echo '<td>';
320 if (has_capability('moodle/course:update', $coursecontext)) {
321 echo $OUTPUT->action_icon(new moodle_url('/course/edit.php',
322 array('id' => $acourse->id, 'category' => $id, 'returnto' => 'category')),
323 new pix_icon('t/edit', $strsettings));
326 // role assignment link
327 if (has_capability('moodle/course:enrolreview', $coursecontext)) {
328 echo $OUTPUT->action_icon(new moodle_url('/enrol/users.php', array('id' => $acourse->id)),
329 new pix_icon('i/users', get_string('enrolledusers', 'enrol')));
332 if (can_delete_course($acourse->id)) {
333 echo $OUTPUT->action_icon(new moodle_url('/course/delete.php', array('id' => $acourse->id)),
334 new pix_icon('t/delete', $strdelete));
337 // MDL-8885, users with no capability to view hidden courses, should not be able to lock themselves out
338 if (has_capability('moodle/course:visibility', $coursecontext) && has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
339 if (!empty($acourse->visible)) {
340 echo $OUTPUT->action_icon(new moodle_url('/course/category.php',
341 array('id' => $category->id, 'page' => $page, 'perpage' => $perpage,
342 'hide' => $acourse->id, 'sesskey' => sesskey())),
343 new pix_icon('t/hide', $strhide));
344 } else {
345 echo $OUTPUT->action_icon(new moodle_url('/course/category.php',
346 array('id' => $category->id, 'page' => $page, 'perpage' => $perpage,
347 'show' => $acourse->id, 'sesskey' => sesskey())),
348 new pix_icon('t/show', $strshow));
352 if (has_capability('moodle/backup:backupcourse', $coursecontext)) {
353 echo $OUTPUT->action_icon(new moodle_url('/backup/backup.php', array('id' => $acourse->id)),
354 new pix_icon('t/backup', $strbackup));
357 if (has_capability('moodle/restore:restorecourse', $coursecontext)) {
358 echo $OUTPUT->action_icon(new moodle_url('/backup/restorefile.php', array('contextid' => $coursecontext->id)),
359 new pix_icon('t/restore', $strrestore));
362 if (has_capability('moodle/category:manage', $context)) {
363 if ($up) {
364 echo $OUTPUT->action_icon(new moodle_url('/course/category.php',
365 array('id' => $category->id, 'page' => $page, 'perpage' => $perpage,
366 'moveup' => $acourse->id, 'sesskey' => sesskey())),
367 new pix_icon('t/up', $strmoveup));
370 if ($down) {
371 echo $OUTPUT->action_icon(new moodle_url('/course/category.php',
372 array('id' => $category->id, 'page' => $page, 'perpage' => $perpage,
373 'movedown' => $acourse->id, 'sesskey' => sesskey())),
374 new pix_icon('t/down', $strmovedown));
376 $abletomovecourses = true;
379 echo '</td>';
380 echo '<td align="center">';
381 echo '<input type="checkbox" name="c'.$acourse->id.'" />';
382 echo '</td>';
383 } else {
384 echo '<td align="right">';
385 // print enrol info
386 if ($icons = enrol_get_course_info_icons($acourse)) {
387 foreach ($icons as $pix_icon) {
388 echo $OUTPUT->render($pix_icon);
391 if (!empty($acourse->summary)) {
392 $link = new moodle_url("/course/info.php?id=$acourse->id");
393 echo $OUTPUT->action_link($link, '<img alt="'.get_string('info').'" class="icon" src="'.$OUTPUT->pix_url('i/info') . '" />',
394 new popup_action('click', $link, 'courseinfo'), array('title'=>$strsummary));
396 echo "</td>";
398 echo "</tr>";
401 if ($abletomovecourses) {
402 $movetocategories = array();
403 $notused = array();
404 make_categories_list($movetocategories, $notused, 'moodle/category:manage');
405 $movetocategories[$category->id] = get_string('moveselectedcoursesto');
406 echo '<tr><td colspan="3" align="right">';
407 echo html_writer::select($movetocategories, 'moveto', $category->id, null, array('id'=>'movetoid'));
408 $PAGE->requires->js_init_call('M.util.init_select_autosubmit', array('movecourses', 'movetoid', false));
409 echo '<input type="hidden" name="id" value="'.$category->id.'" />';
410 echo '</td></tr>';
413 echo '</table>';
414 echo '</div></form>';
415 echo '<br />';
418 echo '<div class="buttons">';
419 if (has_capability('moodle/category:manage', $context) and $numcourses > 1) {
420 /// Print button to re-sort courses by name
421 unset($options);
422 $options['id'] = $category->id;
423 $options['resort'] = 'name';
424 $options['sesskey'] = sesskey();
425 echo $OUTPUT->single_button(new moodle_url('category.php', $options), get_string('resortcoursesbyname'), 'get');
428 if (has_capability('moodle/course:create', $context)) {
429 /// Print button to create a new course
430 unset($options);
431 $options['category'] = $category->id;
432 $options['returnto'] = 'category';
433 echo $OUTPUT->single_button(new moodle_url('edit.php', $options), get_string('addnewcourse'), 'get');
436 if (!empty($CFG->enablecourserequests) && $category->id == $CFG->defaultrequestcategory) {
437 print_course_request_buttons(get_context_instance(CONTEXT_SYSTEM));
439 echo '</div>';
441 print_course_search();
443 echo $OUTPUT->footer();