Merge branch 'MDL-37858_master' of git://github.com/dmonllao/moodle
[moodle.git] / course / manage.php
blob26827a7a5d20fba1f3776a21bcf596e8dd1068e3
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 * Allows the admin to create, delete and rename course categories rearrange courses
20 * @package core
21 * @copyright 2013 Marina Glancy
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once("../config.php");
26 require_once($CFG->dirroot.'/course/lib.php');
28 // Category id.
29 $id = optional_param('id', 0, PARAM_INT);
30 // Which page to show.
31 $page = optional_param('page', 0, PARAM_INT);
32 // How many per page.
33 $perpage = optional_param('perpage', $CFG->coursesperpage, PARAM_INT);
35 // Actions to manage courses.
36 $hide = optional_param('hide', 0, PARAM_INT);
37 $show = optional_param('show', 0, PARAM_INT);
38 $moveup = optional_param('moveup', 0, PARAM_INT);
39 $movedown = optional_param('movedown', 0, PARAM_INT);
40 $moveto = optional_param('moveto', 0, PARAM_INT);
41 $resort = optional_param('resort', 0, PARAM_BOOL);
43 // Actions to manage categories.
44 $deletecat = optional_param('deletecat', 0, PARAM_INT);
45 $hidecat = optional_param('hidecat', 0, PARAM_INT);
46 $showcat = optional_param('showcat', 0, PARAM_INT);
47 $movecat = optional_param('movecat', 0, PARAM_INT);
48 $movetocat = optional_param('movetocat', -1, PARAM_INT);
49 $moveupcat = optional_param('moveupcat', 0, PARAM_INT);
50 $movedowncat = optional_param('movedowncat', 0, PARAM_INT);
52 require_login();
54 if ($id) {
55 $PAGE->set_category_by_id($id);
56 $PAGE->set_url(new moodle_url('/course/manage.php', array('id' => $id)));
57 // This is sure to be the category context.
58 $context = $PAGE->context;
59 // And the object has been loaded for us no need for another DB call.
60 $category = $PAGE->category;
61 if (!can_edit_in_category($category->id)) {
62 redirect(new moodle_url('/course/category.php', array('id' => $category->id)));
64 if (!$category->visible) {
65 require_capability('moodle/category:viewhiddencategories', $context);
67 } else {
68 $context = context_system::instance();
69 $PAGE->set_context($context);
70 $PAGE->set_url(new moodle_url('/course/manage.php'));
71 if (!can_edit_in_category()) {
72 redirect(new moodle_url('/course/index.php'));
76 $canmanage = has_capability('moodle/category:manage', $context);
78 // Check the default category exists.
79 if (!$id && !$DB->record_exists('course_categories', array('parent' => 0))) {
80 // No category yet! Try and make one.
81 $tempcat = new stdClass;
82 $tempcat->name = get_string('miscellaneous');
83 $tempcat->id = $DB->insert_record('course_categories', $tempcat);
84 // Fetch the context to ensure it is created.
85 context_coursecat::instance($tempcat->id);
86 mark_context_dirty('/'.SYSCONTEXTID);
87 // Required to build course_categories.depth and categories.path.
88 fix_course_sortorder();
89 set_config('defaultrequestcategory', $tempcat->id);
90 // Unset the temp category. We no longer need it.
91 unset($tempcat);
94 // Process any category actions.
95 if (!empty($deletecat) and confirm_sesskey()) {
96 // Delete a category.
97 $cattodelete = $DB->get_record('course_categories', array('id' => $deletecat), '*', MUST_EXIST);
98 $context = context_coursecat::instance($deletecat);
99 require_capability('moodle/category:manage', $context);
100 require_capability('moodle/category:manage', get_category_or_system_context($cattodelete->parent));
102 $heading = get_string('deletecategory', 'moodle', format_string($cattodelete->name, true, array('context' => $context)));
104 require_once($CFG->dirroot.'/course/delete_category_form.php');
105 $mform = new delete_category_form(null, $cattodelete);
106 $mform->set_data(array('deletecat' => $deletecat));
107 if ($mform->is_cancelled()) {
108 redirect(new moodle_url('/course/manage.php'));
111 // Start output.
112 echo $OUTPUT->header();
113 echo $OUTPUT->heading($heading);
115 if ($data = $mform->get_data()) {
116 // The form has been submit handle it.
117 if ($data->fulldelete) {
118 $deletedcourses = category_delete_full($cattodelete, true);
119 foreach ($deletedcourses as $course) {
120 echo $OUTPUT->notification(get_string('coursedeleted', '', $course->shortname), 'notifysuccess');
122 $cattodeletename = format_string($cattodelete->name, true, array('context' => $context));
123 echo $OUTPUT->notification(get_string('coursecategorydeleted', '', $catetodeletename), 'notifysuccess');
125 } else {
126 category_delete_move($cattodelete, $data->newparent, true);
128 if ($deletecat == $CFG->defaultrequestcategory) {
129 // If we deleted $CFG->defaultrequestcategory, make it point somewhere else.
130 set_config('defaultrequestcategory', $DB->get_field('course_categories', 'MIN(id)', array('parent' => 0)));
132 echo $OUTPUT->continue_button(new moodle_url('/course/manage.php'));
133 } else {
134 // Display the form.
135 require_once($CFG->libdir . '/questionlib.php');
136 $mform->display();
138 // Finish output and exit.
139 echo $OUTPUT->footer();
140 exit();
143 if (!empty($movecat) and ($movetocat >= 0) and confirm_sesskey()) {
144 // Move a category to a new parent if required.
145 if ($cattomove = $DB->get_record('course_categories', array('id' => $movecat))) {
146 require_capability('moodle/category:manage', get_category_or_system_context($cattomove->parent));
147 if ($cattomove->parent != $movetocat) {
148 $newparent = $DB->get_record('course_categories', array('id' => $movetocat));
149 require_capability('moodle/category:manage', get_category_or_system_context($movetocat));
150 move_category($cattomove, $newparent);
155 // Hide or show a category.
156 if ($hidecat and confirm_sesskey()) {
157 if ($tempcat = $DB->get_record('course_categories', array('id' => $hidecat))) {
158 require_capability('moodle/category:manage', get_category_or_system_context($tempcat->parent));
159 if ($tempcat->visible == 1) {
160 course_category_hide($tempcat);
163 } else if ($showcat and confirm_sesskey()) {
164 if ($tempcat = $DB->get_record('course_categories', array('id' => $showcat))) {
165 require_capability('moodle/category:manage', get_category_or_system_context($tempcat->parent));
166 if ($tempcat->visible == 0) {
167 course_category_show($tempcat);
172 if ((!empty($moveupcat) or !empty($movedowncat)) and confirm_sesskey()) {
173 // Move a category up or down.
174 fix_course_sortorder();
175 $swapcategory = null;
177 if (!empty($moveupcat)) {
178 require_capability('moodle/category:manage', context_coursecat::instance($moveupcat));
179 if ($movecategory = $DB->get_record('course_categories', array('id' => $moveupcat))) {
180 $params = array($movecategory->sortorder, $movecategory->parent);
181 if ($swapcategory = $DB->get_records_select('course_categories', "sortorder<? AND parent=?", $params, 'sortorder DESC', '*', 0, 1)) {
182 $swapcategory = reset($swapcategory);
185 } else {
186 require_capability('moodle/category:manage', context_coursecat::instance($movedowncat));
187 if ($movecategory = $DB->get_record('course_categories', array('id' => $movedowncat))) {
188 $params = array($movecategory->sortorder, $movecategory->parent);
189 if ($swapcategory = $DB->get_records_select('course_categories', "sortorder>? AND parent=?", $params, 'sortorder ASC', '*', 0, 1)) {
190 $swapcategory = reset($swapcategory);
194 if ($swapcategory and $movecategory) {
195 $DB->set_field('course_categories', 'sortorder', $swapcategory->sortorder, array('id' => $movecategory->id));
196 $DB->set_field('course_categories', 'sortorder', $movecategory->sortorder, array('id' => $swapcategory->id));
197 add_to_log(SITEID, "category", "move", "editcategory.php?id=$movecategory->id", $movecategory->id);
200 // Finally reorder courses.
201 fix_course_sortorder();
204 if (isset($category) && $canmanage && $resort && confirm_sesskey()) {
205 // Resort the category.
206 if ($courses = get_courses($category->id, '', 'c.id,c.fullname,c.sortorder')) {
207 collatorlib::asort_objects_by_property($courses, 'fullname', collatorlib::SORT_NATURAL);
208 $i = 1;
209 foreach ($courses as $course) {
210 $DB->set_field('course', 'sortorder', $category->sortorder + $i, array('id' => $course->id));
211 $i++;
213 // This should not be needed but we do it just to be safe.
214 fix_course_sortorder();
218 if (!empty($moveto) && ($data = data_submitted()) && confirm_sesskey()) {
219 // Move a specified course to a new category.
220 // User must have category update in both cats to perform this.
221 require_capability('moodle/category:manage', $context);
222 require_capability('moodle/category:manage', context_coursecat::instance($moveto));
224 if (!$destcategory = $DB->get_record('course_categories', array('id' => $data->moveto))) {
225 print_error('cannotfindcategory', '', '', $data->moveto);
228 $courses = array();
229 foreach ($data as $key => $value) {
230 if (preg_match('/^c\d+$/', $key)) {
231 $courseid = substr($key, 1);
232 array_push($courses, $courseid);
233 // Check this course's category.
234 if ($movingcourse = $DB->get_record('course', array('id' => $courseid))) {
235 if ($movingcourse->category != $id ) {
236 print_error('coursedoesnotbelongtocategory');
238 } else {
239 print_error('cannotfindcourse');
243 move_courses($courses, $data->moveto);
246 if ((!empty($hide) or !empty($show)) && confirm_sesskey()) {
247 // Hide or show a course.
248 if (!empty($hide)) {
249 $course = $DB->get_record('course', array('id' => $hide), '*', MUST_EXIST);
250 $visible = 0;
251 } else {
252 $course = $DB->get_record('course', array('id' => $show), '*', MUST_EXIST);
253 $visible = 1;
255 $coursecontext = context_course::instance($course->id);
256 require_capability('moodle/course:visibility', $coursecontext);
257 // Set the visibility of the course. we set the old flag when user manually changes visibility of course.
258 $params = array('id' => $course->id, 'visible' => $visible, 'visibleold' => $visible, 'timemodified' => time());
259 $DB->update_record('course', $params);
260 add_to_log($course->id, "course", ($visible ? 'show' : 'hide'), "edit.php?id=$course->id", $course->id);
263 if ((!empty($moveup) or !empty($movedown)) && confirm_sesskey()) {
264 // Move a course up or down.
265 require_capability('moodle/category:manage', $context);
267 // Ensure the course order has continuous ordering.
268 fix_course_sortorder();
269 $swapcourse = null;
271 if (!empty($moveup)) {
272 if ($movecourse = $DB->get_record('course', array('id' => $moveup))) {
273 $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder - 1));
275 } else {
276 if ($movecourse = $DB->get_record('course', array('id' => $movedown))) {
277 $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder + 1));
280 if ($swapcourse and $movecourse) {
281 // Check course's category.
282 if ($movecourse->category != $id) {
283 print_error('coursedoesnotbelongtocategory');
285 $DB->set_field('course', 'sortorder', $swapcourse->sortorder, array('id' => $movecourse->id));
286 $DB->set_field('course', 'sortorder', $movecourse->sortorder, array('id' => $swapcourse->id));
287 add_to_log($movecourse->id, "course", "move", "edit.php?id=$movecourse->id", $movecourse->id);
291 // Prepare the standard URL params for this page. We'll need them later.
292 $urlparams = array('id' => $id);
293 if ($page) {
294 $urlparams['page'] = $page;
296 if ($perpage) {
297 $urlparams['perpage'] = $perpage;
300 $PAGE->set_pagelayout('coursecategory');
302 if (can_edit_in_category()) {
303 // Integrate into the admin tree only if the user can edit categories at the top level,
304 // otherwise the admin block does not appear to this user, and you get an error.
305 require_once($CFG->libdir . '/adminlib.php');
306 if ($id) {
307 navigation_node::override_active_url(new moodle_url('/course/category.php', array('id' => $id)));
309 admin_externalpage_setup('coursemgmt', '', $urlparams, $CFG->wwwroot . '/course/manage.php');
310 $settingsnode = $PAGE->settingsnav->find_active_node();
311 if ($id && $settingsnode) {
312 $settingsnode->make_inactive();
313 $settingsnode->force_open();
314 $PAGE->navbar->add($settingsnode->text, $settingsnode->action);
316 } else {
317 // If we get here then they must have arrived here using a specific category
318 // within which they can manage.
319 // We can safetly assume $category is set.
320 $site = get_site();
321 $PAGE->set_title("$site->shortname: $category->name");
322 $PAGE->set_heading($site->fullname);
323 $PAGE->set_button(print_course_search('', true, 'navbar'));
326 $parentlist = array();
327 $displaylist = array();
328 make_categories_list($displaylist, $parentlist);
329 $displaylist[0] = get_string('top');
331 // Start output.
332 echo $OUTPUT->header();
334 if (!isset($category)) {
335 // Print out the categories with all the knobs.
336 $table = new html_table;
337 $table->id = 'coursecategories';
338 $table->attributes['class'] = 'admintable generaltable editcourse';
339 $table->head = array(
340 get_string('categories'),
341 get_string('courses'),
342 get_string('movecategoryto'),
343 get_string('edit'),
345 $table->colclasses = array(
346 'leftalign name',
347 'centeralign count',
348 'centeralign icons',
349 'leftalign actions'
351 $table->data = array();
353 print_category_edit($table, null, $displaylist, $parentlist);
355 echo html_writer::table($table);
356 } else {
357 // Print the category selector.
358 $select = new single_select(new moodle_url('/course/manage.php'), 'id', $displaylist, $category->id, null, 'switchcategory');
359 $select->set_label(get_string('categories').':');
361 echo html_writer::start_tag('div', array('class' => 'categorypicker'));
362 echo $OUTPUT->render($select);
363 echo html_writer::end_tag('div');
366 if ($canmanage) {
367 echo $OUTPUT->container_start('buttons');
368 // Print button to update this category.
369 if ($id) {
370 $url = new moodle_url('/course/editcategory.php', array('id' => $id));
371 echo $OUTPUT->single_button($url, get_string('editcategorythis'), 'get');
374 // Print button for creating new categories.
375 $url = new moodle_url('/course/editcategory.php', array('parent' => $id));
376 echo $OUTPUT->single_button($url, get_string('addsubcategory'), 'get');
377 echo $OUTPUT->container_end();
380 if (isset($category)) {
381 // Print out all the sub-categories (plain mode).
382 // In order to view hidden subcategories the user must have the viewhiddencategories.
383 // capability in the current category..
384 if (has_capability('moodle/category:viewhiddencategories', $context)) {
385 $categorywhere = '';
386 } else {
387 $categorywhere = 'AND cc.visible = 1';
389 // We're going to preload the context for the subcategory as we know that we
390 // need it later on for formatting.
391 $ctxselect = context_helper::get_preload_record_columns_sql('ctx');
392 $sql = "SELECT cc.*, $ctxselect
393 FROM {course_categories} cc
394 JOIN {context} ctx ON cc.id = ctx.instanceid
395 WHERE cc.parent = :parentid AND
396 ctx.contextlevel = :contextlevel
397 $categorywhere
398 ORDER BY cc.sortorder ASC";
399 $subcategories = $DB->get_recordset_sql($sql, array('parentid' => $category->id, 'contextlevel' => CONTEXT_COURSECAT));
400 // Prepare a table to display the sub categories.
401 $table = new html_table;
402 $table->attributes = array(
403 'border' => '0',
404 'cellspacing' => '2',
405 'cellpadding' => '4',
406 'class' => 'generalbox boxaligncenter category_subcategories'
408 $table->head = array(new lang_string('subcategories'));
409 $table->data = array();
410 $baseurl = new moodle_url('/course/manage.php');
411 foreach ($subcategories as $subcategory) {
412 // Preload the context we will need it to format the category name shortly.
413 context_helper::preload_from_record($subcategory);
414 $context = context_coursecat::instance($subcategory->id);
415 // Prepare the things we need to create a link to the subcategory.
416 $attributes = $subcategory->visible ? array() : array('class' => 'dimmed');
417 $text = format_string($subcategory->name, true, array('context' => $context));
418 // Add the subcategory to the table.
419 $baseurl->param('id', $subcategory->id);
420 $table->data[] = array(html_writer::link($baseurl, $text, $attributes));
423 $subcategorieswereshown = (count($table->data) > 0);
424 if ($subcategorieswereshown) {
425 echo html_writer::table($table);
428 $courses = get_courses_page($category->id, 'c.sortorder ASC',
429 'c.id,c.sortorder,c.shortname,c.fullname,c.summary,c.visible',
430 $totalcount, $page*$perpage, $perpage);
431 $numcourses = count($courses);
432 } else {
433 $subcategorieswereshown = true;
434 $courses = array();
435 $numcourses = $totalcount = 0;
438 if (!$courses) {
439 // There is no course to display.
440 if (empty($subcategorieswereshown)) {
441 echo $OUTPUT->heading(get_string("nocoursesyet"));
443 } else {
444 // Display a basic list of courses with paging/editing options.
445 $table = new html_table;
446 $table->attributes = array('border' => 0, 'cellspacing' => 0, 'cellpadding' => '4', 'class' => 'generalbox boxaligncenter');
447 $table->head = array(
448 get_string('courses'),
449 get_string('edit'),
450 get_string('select')
452 $table->colclasses = array(null, null, 'mdl-align');
453 $table->data = array();
455 $count = 0;
456 $abletomovecourses = false;
458 // Checking if we are at the first or at the last page, to allow courses to
459 // be moved up and down beyond the paging border.
460 if ($totalcount > $perpage) {
461 $atfirstpage = ($page == 0);
462 if ($perpage > 0) {
463 $atlastpage = (($page + 1) == ceil($totalcount / $perpage));
464 } else {
465 $atlastpage = true;
467 } else {
468 $atfirstpage = true;
469 $atlastpage = true;
472 $baseurl = new moodle_url('/course/manage.php', $urlparams + array('sesskey' => sesskey()));
473 foreach ($courses as $acourse) {
474 $coursecontext = context_course::instance($acourse->id);
476 $count++;
477 $up = ($count > 1 || !$atfirstpage);
478 $down = ($count < $numcourses || !$atlastpage);
480 $courseurl = new moodle_url('/course/view.php', array('id' => $acourse->id));
481 $attributes = array();
482 $attributes['class'] = $acourse->visible ? '' : 'dimmed';
483 $coursename = get_course_display_name_for_list($acourse);
484 $coursename = format_string($coursename, true, array('context' => $coursecontext));
485 $coursename = html_writer::link($courseurl, $coursename, $attributes);
487 $icons = array();
488 // Update course icon.
489 if (has_capability('moodle/course:update', $coursecontext)) {
490 $url = new moodle_url('/course/edit.php', array('id' => $acourse->id, 'category' => $id, 'returnto' => 'catmanage'));
491 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/edit', get_string('settings')));
494 // Role assignment icon.
495 if (has_capability('moodle/course:enrolreview', $coursecontext)) {
496 $url = new moodle_url('/enrol/users.php', array('id' => $acourse->id));
497 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/enrolusers', get_string('enrolledusers', 'enrol')));
500 // Delete course icon.
501 if (can_delete_course($acourse->id)) {
502 $url = new moodle_url('/course/delete.php', array('id' => $acourse->id));
503 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/delete', get_string('delete')));
506 // Change visibility.
507 // Users with no capability to view hidden courses, should not be able to lock themselves out.
508 if (has_any_capability(array('moodle/course:visibility', 'moodle/course:viewhiddencourses'), $coursecontext)) {
509 if (!empty($acourse->visible)) {
510 $url = new moodle_url($baseurl, array('hide' => $acourse->id));
511 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/hide', get_string('hide')));
512 } else {
513 $url = new moodle_url($baseurl, array('show' => $acourse->id));
514 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/show', get_string('show')));
518 // Backup course icon.
519 if (has_capability('moodle/backup:backupcourse', $coursecontext)) {
520 $url = new moodle_url('/backup/backup.php', array('id' => $acourse->id));
521 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/backup', get_string('backup')));
524 // Restore course icon.
525 if (has_capability('moodle/restore:restorecourse', $coursecontext)) {
526 $url = new moodle_url('/backup/restorefile.php', array('contextid' => $coursecontext->id));
527 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/restore', get_string('restore')));
530 if ($canmanage) {
531 if ($up) {
532 $url = new moodle_url($baseurl, array('moveup' => $acourse->id));
533 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/up', get_string('moveup')));
535 if ($down) {
536 $url = new moodle_url($baseurl, array('movedown' => $acourse->id));
537 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/down', get_string('movedown')));
539 $abletomovecourses = true;
542 $table->data[] = new html_table_row(array(
543 new html_table_cell($coursename),
544 new html_table_cell(join('', $icons)),
545 new html_table_cell(html_writer::empty_tag('input', array('type' => 'checkbox', 'name' => 'c'.$acourse->id)))
549 if ($abletomovecourses) {
550 $movetocategories = array();
551 $notused = array();
552 make_categories_list($movetocategories, $notused, 'moodle/category:manage');
553 $movetocategories[$id] = get_string('moveselectedcoursesto');
555 $cell = new html_table_cell();
556 $cell->colspan = 3;
557 $cell->attributes['class'] = 'mdl-right';
558 $cell->text = html_writer::label(get_string('moveselectedcoursesto'), 'movetoid', false, array('class' => 'accesshide'));
559 $cell->text .= html_writer::select($movetocategories, 'moveto', $id, null, array('id'=>'movetoid', 'class' => 'autosubmit'));
560 $cell->text .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'id', 'value' => $id));
561 $PAGE->requires->yui_module('moodle-core-formautosubmit',
562 'M.core.init_formautosubmit',
563 array(array('selectid' => 'movetoid', 'nothing' => $id))
565 $table->data[] = new html_table_row(array($cell));
568 $actionurl = new moodle_url('/course/manage.php');
569 $pagingurl = new moodle_url('/course/manage.php', array('id' => $id, 'perpage' => $perpage));
571 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, $pagingurl);
572 echo html_writer::start_tag('form', array('id' => 'movecourses', 'action' => $actionurl, 'method' => 'post'));
573 echo html_writer::start_tag('div');
574 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
575 echo html_writer::table($table);
576 echo html_writer::end_tag('div');
577 echo html_writer::end_tag('form');
578 echo html_writer::empty_tag('br');
581 echo html_writer::start_tag('div', array('class' => 'buttons'));
582 if ($canmanage and $numcourses > 1) {
583 // Print button to re-sort courses by name.
584 $url = new moodle_url('/course/manage.php', array('id' => $id, 'resort' => 'name', 'sesskey' => sesskey()));
585 echo $OUTPUT->single_button($url, get_string('resortcoursesbyname'), 'get');
588 if (has_capability('moodle/course:create', $context)) {
589 // Print button to create a new course.
590 $url = new moodle_url('/course/edit.php');
591 if (isset($category)) {
592 $url->params(array('category' => $category->id, 'returnto' => 'catmanage'));
593 } else {
594 $url->params(array('category' => $CFG->defaultrequestcategory, 'returnto' => 'topcatmanage'));
596 echo $OUTPUT->single_button($url, get_string('addnewcourse'), 'get');
599 if (!empty($CFG->enablecourserequests) && $id == $CFG->defaultrequestcategory) {
600 print_course_request_buttons(context_system::instance());
602 echo html_writer::end_tag('div');
604 print_course_search();
606 echo $OUTPUT->footer();
609 * Recursive function to print all the categories ready for editing.
611 * @param html_table $table The table to add data to.
612 * @param stdClass $category The category to render
613 * @param array $displaylist The categories this can be moved to.
614 * @param array $parentslist An array of categories.
615 * @param int $depth The depth of the category.
616 * @param bool $up True if this category can be moved up.
617 * @param bool $down True if this category can be moved down.
619 function print_category_edit(html_table $table, $category, $displaylist, $parentslist, $depth=-1, $up=false, $down=false) {
620 global $OUTPUT;
622 static $str = null;
624 if (is_null($str)) {
625 $str = new stdClass;
626 $str->edit = new lang_string('edit');
627 $str->delete = new lang_string('delete');
628 $str->moveup = new lang_string('moveup');
629 $str->movedown = new lang_string('movedown');
630 $str->edit = new lang_string('editthiscategory');
631 $str->hide = new lang_string('hide');
632 $str->show = new lang_string('show');
633 $str->cohorts = new lang_string('cohorts', 'cohort');
634 $str->spacer = $OUTPUT->spacer().' ';
637 if (!empty($category)) {
639 if (!isset($category->context)) {
640 $category->context = context_coursecat::instance($category->id);
643 $attributes = array();
644 $attributes['class'] = $category->visible ? '' : 'dimmed';
645 $attributes['title'] = $str->edit;
646 $categoryurl = new moodle_url('/course/manage.php', array('id' => $category->id, 'sesskey' => sesskey()));
647 $categoryname = format_string($category->name, true, array('context' => $category->context));
648 $categorypadding = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', $depth);
649 $categoryname = $categorypadding . html_writer::link($categoryurl, $categoryname, $attributes);
651 $icons = array();
652 if (has_capability('moodle/category:manage', $category->context)) {
653 // Edit category.
654 $icons[] = $OUTPUT->action_icon(
655 new moodle_url('/course/editcategory.php', array('id' => $category->id)),
656 new pix_icon('t/edit', $str->edit, 'moodle', array('class' => 'iconsmall')),
657 null, array('title' => $str->edit)
659 // Delete category.
660 $icons[] = $OUTPUT->action_icon(
661 new moodle_url('/course/manage.php', array('deletecat' => $category->id, 'sesskey' => sesskey())),
662 new pix_icon('t/delete', $str->delete, 'moodle', array('class' => 'iconsmall')),
663 null, array('title' => $str->delete)
665 // Change visibility.
666 if (!empty($category->visible)) {
667 $icons[] = $OUTPUT->action_icon(
668 new moodle_url('/course/manage.php', array('hidecat' => $category->id, 'sesskey' => sesskey())),
669 new pix_icon('t/hide', $str->hide, 'moodle', array('class' => 'iconsmall')),
670 null, array('title' => $str->hide)
672 } else {
673 $icons[] = $OUTPUT->action_icon(
674 new moodle_url('/course/manage.php', array('showcat' => $category->id, 'sesskey' => sesskey())),
675 new pix_icon('t/show', $str->show, 'moodle', array('class' => 'iconsmall')),
676 null, array('title' => $str->show)
679 // Cohorts.
680 if (has_any_capability(array('moodle/cohort:manage', 'moodle/cohort:view'), $category->context)) {
681 $icons[] = $OUTPUT->action_icon(
682 new moodle_url('/cohort/index.php', array('contextid' => $category->context->id)),
683 new pix_icon('t/cohort', $str->cohorts, 'moodle', array('class' => 'iconsmall')),
684 null, array('title' => $str->cohorts)
687 // Move up/down.
688 if ($up) {
689 $icons[] = $OUTPUT->action_icon(
690 new moodle_url('/course/manage.php', array('moveupcat' => $category->id, 'sesskey' => sesskey())),
691 new pix_icon('t/up', $str->moveup, 'moodle', array('class' => 'iconsmall')),
692 null, array('title' => $str->moveup)
694 } else {
695 $icons[] = $str->spacer;
697 if ($down) {
698 $icons[] = $OUTPUT->action_icon(
699 new moodle_url('/course/manage.php', array('movedowncat' => $category->id, 'sesskey' => sesskey())),
700 new pix_icon('t/down', $str->movedown, 'moodle', array('class' => 'iconsmall')),
701 null, array('title' => $str->movedown)
703 } else {
704 $icons[] = $str->spacer;
708 $actions = '';
709 if (has_capability('moodle/category:manage', $category->context)) {
710 $tempdisplaylist = $displaylist;
711 unset($tempdisplaylist[$category->id]);
712 foreach ($parentslist as $key => $parents) {
713 if (in_array($category->id, $parents)) {
714 unset($tempdisplaylist[$key]);
717 $popupurl = new moodle_url("manage.php?movecat=$category->id&sesskey=".sesskey());
718 $select = new single_select($popupurl, 'movetocat', $tempdisplaylist, $category->parent, null, "moveform$category->id");
719 $select->set_label(get_string('frontpagecategorynames'), array('class' => 'accesshide'));
720 $actions = $OUTPUT->render($select);
723 $table->data[] = new html_table_row(array(
724 // Category name.
725 new html_table_cell($categoryname),
726 // Course count.
727 new html_table_cell($category->coursecount),
728 // Icons.
729 new html_table_cell(join(' ', $icons)),
730 // Actions.
731 new html_table_cell($actions)
734 // Get the subcategories to be printed.
735 $categories = get_categories($category->id);
736 } else {
737 $categories = get_categories(0);
740 if ($categories) {
741 // Print all the children recursively.
742 $countcats = count($categories);
743 $count = 0;
744 $first = true;
745 $last = false;
746 foreach ($categories as $cat) {
747 $count++;
748 if ($count == $countcats) {
749 $last = true;
751 $up = $first ? false : true;
752 $down = $last ? false : true;
753 $first = false;
755 print_category_edit($table, $cat, $displaylist, $parentslist, $depth+1, $up, $down);