MDL-39957 course: replaced add_to_log for moving courses
[moodle.git] / course / manage.php
blobddf02ce48488fe829baf44c57607448fc4379e77
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');
27 require_once($CFG->libdir.'/coursecatlib.php');
29 // Category id.
30 $id = optional_param('categoryid', 0, PARAM_INT);
31 // Which page to show.
32 $page = optional_param('page', 0, PARAM_INT);
33 // How many per page.
34 $perpage = optional_param('perpage', $CFG->coursesperpage, PARAM_INT);
36 $search = optional_param('search', '', PARAM_RAW); // search words
37 $blocklist = optional_param('blocklist', 0, PARAM_INT);
38 $modulelist= optional_param('modulelist', '', PARAM_PLUGIN);
39 if (!$id && !empty($search)) {
40 $searchcriteria = array('search' => $search);
41 } else if (!$id && !empty($blocklist)) {
42 $searchcriteria = array('blocklist' => $blocklist);
43 } else if (!$id && !empty($modulelist)) {
44 $searchcriteria = array('modulelist' => $modulelist);
45 } else {
46 $searchcriteria = array();
49 // Actions to manage courses.
50 $hide = optional_param('hide', 0, PARAM_INT);
51 $show = optional_param('show', 0, PARAM_INT);
52 $moveup = optional_param('moveup', 0, PARAM_INT);
53 $movedown = optional_param('movedown', 0, PARAM_INT);
54 $moveto = optional_param('moveto', 0, PARAM_INT);
55 $resort = optional_param('resort', 0, PARAM_BOOL);
57 // Actions to manage categories.
58 $deletecat = optional_param('deletecat', 0, PARAM_INT);
59 $hidecat = optional_param('hidecat', 0, PARAM_INT);
60 $showcat = optional_param('showcat', 0, PARAM_INT);
61 $movecat = optional_param('movecat', 0, PARAM_INT);
62 $movetocat = optional_param('movetocat', -1, PARAM_INT);
63 $moveupcat = optional_param('moveupcat', 0, PARAM_INT);
64 $movedowncat = optional_param('movedowncat', 0, PARAM_INT);
66 require_login();
68 // Retrieve coursecat object
69 // This will also make sure that category is accessible and create default category if missing
70 $coursecat = coursecat::get($id);
72 if ($id) {
73 $PAGE->set_category_by_id($id);
74 $PAGE->set_url(new moodle_url('/course/manage.php', array('categoryid' => $id)));
75 // This is sure to be the category context.
76 $context = $PAGE->context;
77 if (!can_edit_in_category($coursecat->id)) {
78 redirect(new moodle_url('/course/index.php', array('categoryid' => $coursecat->id)));
80 } else {
81 $context = context_system::instance();
82 $PAGE->set_context($context);
83 $PAGE->set_url(new moodle_url('/course/manage.php'));
84 if (!can_edit_in_category()) {
85 redirect(new moodle_url('/course/index.php'));
89 $canmanage = has_capability('moodle/category:manage', $context);
91 // Process any category actions.
92 if (!empty($deletecat) and confirm_sesskey()) {
93 // Delete a category.
94 $cattodelete = coursecat::get($deletecat);
95 $context = context_coursecat::instance($deletecat);
96 require_capability('moodle/category:manage', $context);
97 require_capability('moodle/category:manage', get_category_or_system_context($cattodelete->parent));
99 $heading = get_string('deletecategory', 'moodle', format_string($cattodelete->name, true, array('context' => $context)));
101 require_once($CFG->dirroot.'/course/delete_category_form.php');
102 $mform = new delete_category_form(null, $cattodelete);
103 if ($mform->is_cancelled()) {
104 redirect(new moodle_url('/course/manage.php'));
107 // Start output.
108 echo $OUTPUT->header();
109 echo $OUTPUT->heading($heading);
111 if ($data = $mform->get_data()) {
112 // The form has been submit handle it.
113 if ($data->fulldelete == 1 && $cattodelete->can_delete_full()) {
114 $cattodeletename = $cattodelete->get_formatted_name();
115 $deletedcourses = $cattodelete->delete_full(true);
116 foreach ($deletedcourses as $course) {
117 echo $OUTPUT->notification(get_string('coursedeleted', '', $course->shortname), 'notifysuccess');
119 echo $OUTPUT->notification(get_string('coursecategorydeleted', '', $cattodeletename), 'notifysuccess');
120 echo $OUTPUT->continue_button(new moodle_url('/course/manage.php'));
122 } else if ($data->fulldelete == 0 && $cattodelete->can_move_content_to($data->newparent)) {
123 $cattodelete->delete_move($data->newparent, true);
124 echo $OUTPUT->continue_button(new moodle_url('/course/manage.php'));
125 } else {
126 // Some error in parameters (user is cheating?)
127 $mform->display();
129 } else {
130 // Display the form.
131 $mform->display();
133 // Finish output and exit.
134 echo $OUTPUT->footer();
135 exit();
138 if (!empty($movecat) and ($movetocat >= 0) and confirm_sesskey()) {
139 // Move a category to a new parent if required.
140 $cattomove = coursecat::get($movecat);
141 if ($cattomove->parent != $movetocat) {
142 if ($cattomove->can_change_parent($movetocat)) {
143 $cattomove->change_parent($movetocat);
144 } else {
145 print_error('cannotmovecategory');
150 // Hide or show a category.
151 if ($hidecat and confirm_sesskey()) {
152 $cattohide = coursecat::get($hidecat);
153 require_capability('moodle/category:manage', get_category_or_system_context($cattohide->parent));
154 $cattohide->hide();
155 } else if ($showcat and confirm_sesskey()) {
156 $cattoshow = coursecat::get($showcat);
157 require_capability('moodle/category:manage', get_category_or_system_context($cattoshow->parent));
158 $cattoshow->show();
161 if ((!empty($moveupcat) or !empty($movedowncat)) and confirm_sesskey()) {
162 // Move a category up or down.
163 fix_course_sortorder();
164 $swapcategory = null;
166 if (!empty($moveupcat)) {
167 require_capability('moodle/category:manage', context_coursecat::instance($moveupcat));
168 if ($movecategory = $DB->get_record('course_categories', array('id' => $moveupcat))) {
169 $params = array($movecategory->sortorder, $movecategory->parent);
170 if ($swapcategory = $DB->get_records_select('course_categories', "sortorder<? AND parent=?", $params, 'sortorder DESC', '*', 0, 1)) {
171 $swapcategory = reset($swapcategory);
174 } else {
175 require_capability('moodle/category:manage', context_coursecat::instance($movedowncat));
176 if ($movecategory = $DB->get_record('course_categories', array('id' => $movedowncat))) {
177 $params = array($movecategory->sortorder, $movecategory->parent);
178 if ($swapcategory = $DB->get_records_select('course_categories', "sortorder>? AND parent=?", $params, 'sortorder ASC', '*', 0, 1)) {
179 $swapcategory = reset($swapcategory);
183 if ($swapcategory and $movecategory) {
184 $DB->set_field('course_categories', 'sortorder', $swapcategory->sortorder, array('id' => $movecategory->id));
185 $DB->set_field('course_categories', 'sortorder', $movecategory->sortorder, array('id' => $swapcategory->id));
186 cache_helper::purge_by_event('changesincoursecat');
187 add_to_log(SITEID, "category", "move", "editcategory.php?id=$movecategory->id", $movecategory->id);
190 // Finally reorder courses.
191 fix_course_sortorder();
194 if ($coursecat->id && $canmanage && $resort && confirm_sesskey()) {
195 // Resort the category.
196 if ($courses = get_courses($coursecat->id, '', 'c.id,c.fullname,c.sortorder')) {
197 core_collator::asort_objects_by_property($courses, 'fullname', core_collator::SORT_NATURAL);
198 $i = 1;
199 foreach ($courses as $course) {
200 $DB->set_field('course', 'sortorder', $coursecat->sortorder + $i, array('id' => $course->id));
201 $i++;
203 // This should not be needed but we do it just to be safe.
204 fix_course_sortorder();
205 cache_helper::purge_by_event('changesincourse');
209 if (!empty($moveto) && ($data = data_submitted()) && confirm_sesskey()) {
210 // Move a specified course to a new category.
211 // User must have category update in both cats to perform this.
212 require_capability('moodle/category:manage', $context);
213 require_capability('moodle/category:manage', context_coursecat::instance($moveto));
215 if (!$destcategory = $DB->get_record('course_categories', array('id' => $data->moveto))) {
216 print_error('cannotfindcategory', '', '', $data->moveto);
219 $courses = array();
220 foreach ($data as $key => $value) {
221 if (preg_match('/^c\d+$/', $key)) {
222 $courseid = substr($key, 1);
223 array_push($courses, $courseid);
224 // Check this course's category.
225 if ($movingcourse = $DB->get_record('course', array('id' => $courseid))) {
226 if ($id && $movingcourse->category != $id ) {
227 print_error('coursedoesnotbelongtocategory');
229 } else {
230 print_error('cannotfindcourse');
234 move_courses($courses, $data->moveto);
237 if ((!empty($hide) or !empty($show)) && confirm_sesskey()) {
238 // Hide or show a course.
239 if (!empty($hide)) {
240 $course = $DB->get_record('course', array('id' => $hide), '*', MUST_EXIST);
241 $visible = 0;
242 } else {
243 $course = $DB->get_record('course', array('id' => $show), '*', MUST_EXIST);
244 $visible = 1;
246 $coursecontext = context_course::instance($course->id);
247 require_capability('moodle/course:visibility', $coursecontext);
248 // Set the visibility of the course. we set the old flag when user manually changes visibility of course.
249 $params = array('id' => $course->id, 'visible' => $visible, 'visibleold' => $visible, 'timemodified' => time());
250 $DB->update_record('course', $params);
251 cache_helper::purge_by_event('changesincourse');
252 add_to_log($course->id, "course", ($visible ? 'show' : 'hide'), "edit.php?id=$course->id", $course->id);
255 if ((!empty($moveup) or !empty($movedown)) && confirm_sesskey()) {
256 // Move a course up or down.
257 require_capability('moodle/category:manage', $context);
259 // Ensure the course order has continuous ordering.
260 fix_course_sortorder();
261 $swapcourse = null;
263 if (!empty($moveup)) {
264 if ($movecourse = $DB->get_record('course', array('id' => $moveup))) {
265 $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder - 1));
267 } else {
268 if ($movecourse = $DB->get_record('course', array('id' => $movedown))) {
269 $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder + 1));
272 if ($swapcourse and $movecourse) {
273 // Check course's category.
274 if ($movecourse->category != $id) {
275 print_error('coursedoesnotbelongtocategory');
277 $DB->set_field('course', 'sortorder', $swapcourse->sortorder, array('id' => $movecourse->id));
278 $DB->set_field('course', 'sortorder', $movecourse->sortorder, array('id' => $swapcourse->id));
279 cache_helper::purge_by_event('changesincourse');
281 // Update $movecourse's sortorder.
282 $movecourse->sortorder = $swapcourse->sortorder;
284 // Trigger a course updated event.
285 $event = \core\event\course_updated::create(array(
286 'objectid' => $movecourse->id,
287 'context' => context_course::instance($movecourse->id),
288 'other' => array('shortname' => $movecourse->shortname,
289 'fullname' => $movecourse->fullname)
291 $event->add_record_snapshot('course', $movecourse);
292 $event->set_legacy_logdata(array($movecourse->id, 'course', 'move', 'edit.php?id=' . $movecourse->id, $movecourse->id));
293 $event->trigger();
297 // Prepare the standard URL params for this page. We'll need them later.
298 $urlparams = array('categoryid' => $id);
299 if ($page) {
300 $urlparams['page'] = $page;
302 if ($perpage) {
303 $urlparams['perpage'] = $perpage;
305 $urlparams += $searchcriteria;
307 $PAGE->set_pagelayout('coursecategory');
308 $courserenderer = $PAGE->get_renderer('core', 'course');
310 if (can_edit_in_category()) {
311 // Integrate into the admin tree only if the user can edit categories at the top level,
312 // otherwise the admin block does not appear to this user, and you get an error.
313 require_once($CFG->libdir . '/adminlib.php');
314 if ($id) {
315 navigation_node::override_active_url(new moodle_url('/course/index.php', array('categoryid' => $id)));
317 admin_externalpage_setup('coursemgmt', '', $urlparams, $CFG->wwwroot . '/course/manage.php');
318 $settingsnode = $PAGE->settingsnav->find_active_node();
319 if ($id && $settingsnode) {
320 $settingsnode->make_inactive();
321 $settingsnode->force_open();
322 $PAGE->navbar->add($settingsnode->text, $settingsnode->action);
324 } else {
325 $site = get_site();
326 $PAGE->set_title("$site->shortname: $coursecat->name");
327 $PAGE->set_heading($site->fullname);
328 $PAGE->set_button($courserenderer->course_search_form('', 'navbar'));
331 // Start output.
332 echo $OUTPUT->header();
334 if (!empty($searchcriteria)) {
335 echo $OUTPUT->heading(new lang_string('searchresults'));
336 } else if (!$coursecat->id) {
337 // Print out the categories with all the knobs.
338 $table = new html_table;
339 $table->id = 'coursecategories';
340 $table->attributes['class'] = 'admintable generaltable editcourse';
341 $table->head = array(
342 get_string('categories'),
343 get_string('courses'),
344 get_string('edit'),
345 get_string('movecategoryto'),
347 $table->colclasses = array(
348 'leftalign name',
349 'centeralign count',
350 'centeralign icons',
351 'leftalign actions'
353 $table->data = array();
355 print_category_edit($table, $coursecat);
357 echo html_writer::table($table);
358 } else {
359 // Print the category selector.
360 $displaylist = coursecat::make_categories_list();
361 $select = new single_select(new moodle_url('/course/manage.php'), 'categoryid', $displaylist, $coursecat->id, null, 'switchcategory');
362 $select->set_label(get_string('categories').':');
364 echo html_writer::start_tag('div', array('class' => 'categorypicker'));
365 echo $OUTPUT->render($select);
366 echo html_writer::end_tag('div');
369 if ($canmanage && empty($searchcriteria)) {
370 echo $OUTPUT->container_start('buttons');
371 // Print button to update this category.
372 if ($id) {
373 $url = new moodle_url('/course/editcategory.php', array('id' => $id));
374 echo $OUTPUT->single_button($url, get_string('editcategorythis'), 'get');
377 // Print button for creating new categories.
378 $url = new moodle_url('/course/editcategory.php', array('parent' => $id));
379 if ($id) {
380 $title = get_string('addsubcategory');
381 } else {
382 $title = get_string('addnewcategory');
384 echo $OUTPUT->single_button($url, $title, 'get');
385 echo $OUTPUT->container_end();
388 if (!empty($searchcriteria)) {
389 $courses = coursecat::get(0)->search_courses($searchcriteria, array('recursive' => true,
390 'offset' => $page * $perpage, 'limit' => $perpage, 'sort' => array('fullname' => 1)));
391 $numcourses = count($courses);
392 $totalcount = coursecat::get(0)->search_courses_count($searchcriteria, array('recursive' => true));
393 } else if ($coursecat->id) {
394 // Print out all the sub-categories (plain mode).
395 // In order to view hidden subcategories the user must have the viewhiddencategories.
396 // capability in the current category..
397 if (has_capability('moodle/category:viewhiddencategories', $context)) {
398 $categorywhere = '';
399 } else {
400 $categorywhere = 'AND cc.visible = 1';
402 // We're going to preload the context for the subcategory as we know that we
403 // need it later on for formatting.
404 $ctxselect = context_helper::get_preload_record_columns_sql('ctx');
405 $sql = "SELECT cc.*, $ctxselect
406 FROM {course_categories} cc
407 JOIN {context} ctx ON cc.id = ctx.instanceid
408 WHERE cc.parent = :parentid AND
409 ctx.contextlevel = :contextlevel
410 $categorywhere
411 ORDER BY cc.sortorder ASC";
412 $subcategories = $DB->get_recordset_sql($sql, array('parentid' => $coursecat->id, 'contextlevel' => CONTEXT_COURSECAT));
413 // Prepare a table to display the sub categories.
414 $table = new html_table;
415 $table->attributes = array(
416 'border' => '0',
417 'cellspacing' => '2',
418 'cellpadding' => '4',
419 'class' => 'generalbox boxaligncenter category_subcategories'
421 $table->head = array(new lang_string('subcategories'));
422 $table->data = array();
423 $baseurl = new moodle_url('/course/manage.php');
424 foreach ($subcategories as $subcategory) {
425 // Preload the context we will need it to format the category name shortly.
426 context_helper::preload_from_record($subcategory);
427 $context = context_coursecat::instance($subcategory->id);
428 // Prepare the things we need to create a link to the subcategory.
429 $attributes = $subcategory->visible ? array() : array('class' => 'dimmed');
430 $text = format_string($subcategory->name, true, array('context' => $context));
431 // Add the subcategory to the table.
432 $baseurl->param('categoryid', $subcategory->id);
433 $table->data[] = array(html_writer::link($baseurl, $text, $attributes));
436 $subcategorieswereshown = (count($table->data) > 0);
437 if ($subcategorieswereshown) {
438 echo html_writer::table($table);
441 $courses = get_courses_page($coursecat->id, 'c.sortorder ASC',
442 'c.id,c.sortorder,c.shortname,c.fullname,c.summary,c.visible',
443 $totalcount, $page*$perpage, $perpage);
444 $numcourses = count($courses);
445 } else {
446 $subcategorieswereshown = true;
447 $courses = array();
448 $numcourses = $totalcount = 0;
451 if (!$courses) {
452 // There is no course to display.
453 if (empty($subcategorieswereshown)) {
454 echo $OUTPUT->heading(get_string("nocoursesyet"));
456 } else {
457 // Display a basic list of courses with paging/editing options.
458 $table = new html_table;
459 $table->attributes = array('border' => 0, 'cellspacing' => 0, 'cellpadding' => '4', 'class' => 'generalbox boxaligncenter');
460 $table->head = array(
461 get_string('courses'),
462 get_string('edit'),
463 get_string('select')
465 $table->colclasses = array(null, null, 'mdl-align');
466 if (!empty($searchcriteria)) {
467 // add 'Category' column
468 array_splice($table->head, 1, 0, array(get_string('category')));
469 array_splice($table->colclasses, 1, 0, array(null));
471 $table->data = array();
473 $count = 0;
474 $abletomovecourses = false;
476 // Checking if we are at the first or at the last page, to allow courses to
477 // be moved up and down beyond the paging border.
478 if ($totalcount > $perpage) {
479 $atfirstpage = ($page == 0);
480 if ($perpage > 0) {
481 $atlastpage = (($page + 1) == ceil($totalcount / $perpage));
482 } else {
483 $atlastpage = true;
485 } else {
486 $atfirstpage = true;
487 $atlastpage = true;
490 $baseurl = new moodle_url('/course/manage.php', $urlparams + array('sesskey' => sesskey()));
491 foreach ($courses as $acourse) {
492 $coursecontext = context_course::instance($acourse->id);
494 $count++;
495 $up = ($count > 1 || !$atfirstpage);
496 $down = ($count < $numcourses || !$atlastpage);
498 $courseurl = new moodle_url('/course/view.php', array('id' => $acourse->id));
499 $attributes = array();
500 $attributes['class'] = $acourse->visible ? '' : 'dimmed';
501 $coursename = get_course_display_name_for_list($acourse);
502 $coursename = format_string($coursename, true, array('context' => $coursecontext));
503 $coursename = html_writer::link($courseurl, $coursename, $attributes);
505 $icons = array();
506 // Update course icon.
507 if (has_capability('moodle/course:update', $coursecontext)) {
508 $url = new moodle_url('/course/edit.php', array('id' => $acourse->id, 'category' => $id, 'returnto' => 'catmanage'));
509 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/edit', get_string('settings')));
512 // Role assignment icon.
513 if (has_capability('moodle/course:enrolreview', $coursecontext)) {
514 $url = new moodle_url('/enrol/users.php', array('id' => $acourse->id));
515 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/enrolusers', get_string('enrolledusers', 'enrol')));
518 // Delete course icon.
519 if (can_delete_course($acourse->id)) {
520 $url = new moodle_url('/course/delete.php', array('id' => $acourse->id));
521 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/delete', get_string('delete')));
524 // Change visibility.
525 // Users with no capability to view hidden courses, should not be able to lock themselves out.
526 if (has_any_capability(array('moodle/course:visibility', 'moodle/course:viewhiddencourses'), $coursecontext)) {
527 if (!empty($acourse->visible)) {
528 $url = new moodle_url($baseurl, array('hide' => $acourse->id));
529 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/hide', get_string('hide')));
530 } else {
531 $url = new moodle_url($baseurl, array('show' => $acourse->id));
532 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/show', get_string('show')));
536 // Backup course icon.
537 if (has_capability('moodle/backup:backupcourse', $coursecontext)) {
538 $url = new moodle_url('/backup/backup.php', array('id' => $acourse->id));
539 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/backup', get_string('backup')));
542 // Restore course icon.
543 if (has_capability('moodle/restore:restorecourse', $coursecontext)) {
544 $url = new moodle_url('/backup/restorefile.php', array('contextid' => $coursecontext->id));
545 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/restore', get_string('restore')));
548 if ($canmanage) {
549 if ($up && empty($searchcriteria)) {
550 $url = new moodle_url($baseurl, array('moveup' => $acourse->id));
551 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/up', get_string('moveup')));
553 if ($down && empty($searchcriteria)) {
554 $url = new moodle_url($baseurl, array('movedown' => $acourse->id));
555 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/down', get_string('movedown')));
557 $abletomovecourses = true;
560 $table->data[] = new html_table_row(array(
561 new html_table_cell($coursename),
562 new html_table_cell(join('', $icons)),
563 new html_table_cell(html_writer::empty_tag('input', array('type' => 'checkbox', 'name' => 'c'.$acourse->id)))
566 if (!empty($searchcriteria)) {
567 // add 'Category' column
568 $category = coursecat::get($acourse->category, IGNORE_MISSING, true);
569 $cell = new html_table_cell($category->get_formatted_name());
570 $cell->attributes['class'] = $category->visible ? '' : 'dimmed_text';
571 array_splice($table->data[count($table->data) - 1]->cells, 1, 0, array($cell));
575 if ($abletomovecourses) {
576 $movetocategories = coursecat::make_categories_list('moodle/category:manage');
577 $movetocategories[$id] = get_string('moveselectedcoursesto');
579 $cell = new html_table_cell();
580 $cell->colspan = 3;
581 $cell->attributes['class'] = 'mdl-right';
582 $cell->text = html_writer::label(get_string('moveselectedcoursesto'), 'movetoid', false, array('class' => 'accesshide'));
583 $cell->text .= html_writer::select($movetocategories, 'moveto', $id, null, array('id' => 'movetoid', 'class' => 'autosubmit'));
584 $cell->text .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'categoryid', 'value' => $id));
585 $PAGE->requires->yui_module('moodle-core-formautosubmit',
586 'M.core.init_formautosubmit',
587 array(array('selectid' => 'movetoid', 'nothing' => $id))
589 $table->data[] = new html_table_row(array($cell));
592 $actionurl = new moodle_url('/course/manage.php');
593 $pagingurl = new moodle_url('/course/manage.php', array('categoryid' => $id, 'perpage' => $perpage) + $searchcriteria);
595 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, $pagingurl);
596 echo html_writer::start_tag('form', array('id' => 'movecourses', 'action' => $actionurl, 'method' => 'post'));
597 echo html_writer::start_tag('div');
598 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
599 foreach ($searchcriteria as $key => $value) {
600 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => $key, 'value' => $value));
602 echo html_writer::table($table);
603 echo html_writer::end_tag('div');
604 echo html_writer::end_tag('form');
605 echo html_writer::empty_tag('br');
608 echo html_writer::start_tag('div', array('class' => 'buttons'));
609 if ($canmanage and $numcourses > 1 && empty($searchcriteria)) {
610 // Print button to re-sort courses by name.
611 $url = new moodle_url('/course/manage.php', array('categoryid' => $id, 'resort' => 'name', 'sesskey' => sesskey()));
612 echo $OUTPUT->single_button($url, get_string('resortcoursesbyname'), 'get');
615 if (has_capability('moodle/course:create', $context) && empty($searchcriteria)) {
616 // Print button to create a new course.
617 $url = new moodle_url('/course/edit.php');
618 if ($coursecat->id) {
619 $url->params(array('category' => $coursecat->id, 'returnto' => 'catmanage'));
620 } else {
621 $url->params(array('category' => $CFG->defaultrequestcategory, 'returnto' => 'topcatmanage'));
623 echo $OUTPUT->single_button($url, get_string('addnewcourse'), 'get');
626 if (!empty($CFG->enablecourserequests) && $id == $CFG->defaultrequestcategory) {
627 print_course_request_buttons(context_system::instance());
629 echo html_writer::end_tag('div');
631 echo $courserenderer->course_search_form();
633 echo $OUTPUT->footer();
636 * Recursive function to print all the categories ready for editing.
638 * @param html_table $table The table to add data to.
639 * @param coursecat $category The category to render
640 * @param int $depth The depth of the category.
641 * @param bool $up True if this category can be moved up.
642 * @param bool $down True if this category can be moved down.
644 function print_category_edit(html_table $table, coursecat $category, $depth = -1, $up = false, $down = false) {
645 global $OUTPUT;
647 static $str = null;
649 if (is_null($str)) {
650 $str = new stdClass;
651 $str->edit = new lang_string('edit');
652 $str->delete = new lang_string('delete');
653 $str->moveup = new lang_string('moveup');
654 $str->movedown = new lang_string('movedown');
655 $str->edit = new lang_string('editthiscategory');
656 $str->hide = new lang_string('hide');
657 $str->show = new lang_string('show');
658 $str->cohorts = new lang_string('cohorts', 'cohort');
659 $str->spacer = $OUTPUT->spacer().' ';
662 if ($category->id) {
664 $categorycontext = context_coursecat::instance($category->id);
666 $attributes = array();
667 $attributes['class'] = $category->visible ? '' : 'dimmed';
668 $attributes['title'] = $str->edit;
669 $categoryurl = new moodle_url('/course/manage.php', array('categoryid' => $category->id, 'sesskey' => sesskey()));
670 $categoryname = $category->get_formatted_name();
671 $categorypadding = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', $depth);
672 $categoryname = $categorypadding . html_writer::link($categoryurl, $categoryname, $attributes);
674 $icons = array();
675 if (has_capability('moodle/category:manage', $categorycontext)) {
676 // Edit category.
677 $icons[] = $OUTPUT->action_icon(
678 new moodle_url('/course/editcategory.php', array('id' => $category->id)),
679 new pix_icon('t/edit', $str->edit, 'moodle', array('class' => 'iconsmall')),
680 null, array('title' => $str->edit)
682 // Delete category.
683 $icons[] = $OUTPUT->action_icon(
684 new moodle_url('/course/manage.php', array('deletecat' => $category->id, 'sesskey' => sesskey())),
685 new pix_icon('t/delete', $str->delete, 'moodle', array('class' => 'iconsmall')),
686 null, array('title' => $str->delete)
688 // Change visibility.
689 if (!empty($category->visible)) {
690 $icons[] = $OUTPUT->action_icon(
691 new moodle_url('/course/manage.php', array('hidecat' => $category->id, 'sesskey' => sesskey())),
692 new pix_icon('t/hide', $str->hide, 'moodle', array('class' => 'iconsmall')),
693 null, array('title' => $str->hide)
695 } else {
696 $icons[] = $OUTPUT->action_icon(
697 new moodle_url('/course/manage.php', array('showcat' => $category->id, 'sesskey' => sesskey())),
698 new pix_icon('t/show', $str->show, 'moodle', array('class' => 'iconsmall')),
699 null, array('title' => $str->show)
702 // Cohorts.
703 if (has_any_capability(array('moodle/cohort:manage', 'moodle/cohort:view'), $categorycontext)) {
704 $icons[] = $OUTPUT->action_icon(
705 new moodle_url('/cohort/index.php', array('contextid' => $categorycontext->id)),
706 new pix_icon('t/cohort', $str->cohorts, 'moodle', array('class' => 'iconsmall')),
707 null, array('title' => $str->cohorts)
710 // Move up/down.
711 if ($up) {
712 $icons[] = $OUTPUT->action_icon(
713 new moodle_url('/course/manage.php', array('moveupcat' => $category->id, 'sesskey' => sesskey())),
714 new pix_icon('t/up', $str->moveup, 'moodle', array('class' => 'iconsmall')),
715 null, array('title' => $str->moveup)
717 } else {
718 $icons[] = $str->spacer;
720 if ($down) {
721 $icons[] = $OUTPUT->action_icon(
722 new moodle_url('/course/manage.php', array('movedowncat' => $category->id, 'sesskey' => sesskey())),
723 new pix_icon('t/down', $str->movedown, 'moodle', array('class' => 'iconsmall')),
724 null, array('title' => $str->movedown)
726 } else {
727 $icons[] = $str->spacer;
731 $actions = '';
732 if (has_capability('moodle/category:manage', $categorycontext)) {
733 $popupurl = new moodle_url('/course/manage.php', array('movecat' => $category->id, 'sesskey' => sesskey()));
734 $tempdisplaylist = array(0 => get_string('top')) + coursecat::make_categories_list('moodle/category:manage', $category->id);
735 $select = new single_select($popupurl, 'movetocat', $tempdisplaylist, $category->parent, null, "moveform$category->id");
736 $select->set_label(get_string('frontpagecategorynames'), array('class' => 'accesshide'));
737 $actions = $OUTPUT->render($select);
740 $table->data[] = new html_table_row(array(
741 // Category name.
742 new html_table_cell($categoryname),
743 // Course count.
744 new html_table_cell($category->coursecount),
745 // Icons.
746 new html_table_cell(join(' ', $icons)),
747 // Actions.
748 new html_table_cell($actions)
752 if ($categories = $category->get_children()) {
753 // Print all the children recursively.
754 $countcats = count($categories);
755 $count = 0;
756 $first = true;
757 $last = false;
758 foreach ($categories as $cat) {
759 $count++;
760 if ($count == $countcats) {
761 $last = true;
763 $up = $first ? false : true;
764 $down = $last ? false : true;
765 $first = false;
767 print_category_edit($table, $cat, $depth+1, $up, $down);