MDL-42016 repository: Implement new sync method in repositories
[moodle.git] / course / manage.php
blobbe8d1153645b1ff7df3f77094ef1b4ae1f52f3b9
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 /**
30 * Limit the total number of categories where user has 'moodle/course:manage'
31 * permission in order for "Move categories" dropdown to be displayed on this page.
32 * If number of categories exceeds this limit, user can always use edit category
33 * form to change the parent. Otherwise the page size becomes too big.
35 if (!defined('COURSECAT_QUICKMOVE_LIMIT')) {
36 define('COURSECAT_QUICKMOVE_LIMIT', 200);
39 // Category id.
40 $id = optional_param('categoryid', 0, PARAM_INT);
41 // Which page to show.
42 $page = optional_param('page', 0, PARAM_INT);
43 // How many per page.
44 $perpage = optional_param('perpage', $CFG->coursesperpage, PARAM_INT);
46 $search = optional_param('search', '', PARAM_RAW); // search words
47 $blocklist = optional_param('blocklist', 0, PARAM_INT);
48 $modulelist= optional_param('modulelist', '', PARAM_PLUGIN);
49 if (!$id && !empty($search)) {
50 $searchcriteria = array('search' => $search);
51 } else if (!$id && !empty($blocklist)) {
52 $searchcriteria = array('blocklist' => $blocklist);
53 } else if (!$id && !empty($modulelist)) {
54 $searchcriteria = array('modulelist' => $modulelist);
55 } else {
56 $searchcriteria = array();
59 // Actions to manage courses.
60 $hide = optional_param('hide', 0, PARAM_INT);
61 $show = optional_param('show', 0, PARAM_INT);
62 $moveup = optional_param('moveup', 0, PARAM_INT);
63 $movedown = optional_param('movedown', 0, PARAM_INT);
64 $moveto = optional_param('moveto', 0, PARAM_INT);
65 $resort = optional_param('resort', 0, PARAM_BOOL);
67 // Actions to manage categories.
68 $deletecat = optional_param('deletecat', 0, PARAM_INT);
69 $hidecat = optional_param('hidecat', 0, PARAM_INT);
70 $showcat = optional_param('showcat', 0, PARAM_INT);
71 $movecat = optional_param('movecat', 0, PARAM_INT);
72 $movetocat = optional_param('movetocat', -1, PARAM_INT);
73 $moveupcat = optional_param('moveupcat', 0, PARAM_INT);
74 $movedowncat = optional_param('movedowncat', 0, PARAM_INT);
76 require_login();
78 // Retrieve coursecat object
79 // This will also make sure that category is accessible and create default category if missing
80 $coursecat = coursecat::get($id);
82 if ($id) {
83 $PAGE->set_category_by_id($id);
84 $PAGE->set_url(new moodle_url('/course/manage.php', array('categoryid' => $id)));
85 // This is sure to be the category context.
86 $context = $PAGE->context;
87 if (!can_edit_in_category($coursecat->id)) {
88 redirect(new moodle_url('/course/index.php', array('categoryid' => $coursecat->id)));
90 } else {
91 $context = context_system::instance();
92 $PAGE->set_context($context);
93 $PAGE->set_url(new moodle_url('/course/manage.php'));
94 if (!can_edit_in_category()) {
95 redirect(new moodle_url('/course/index.php'));
99 $canmanage = has_capability('moodle/category:manage', $context);
101 // Process any category actions.
102 if (!empty($deletecat) and confirm_sesskey()) {
103 // Delete a category.
104 $cattodelete = coursecat::get($deletecat);
105 $context = context_coursecat::instance($deletecat);
106 require_capability('moodle/category:manage', $context);
107 require_capability('moodle/category:manage', get_category_or_system_context($cattodelete->parent));
109 $heading = get_string('deletecategory', 'moodle', format_string($cattodelete->name, true, array('context' => $context)));
111 require_once($CFG->dirroot.'/course/delete_category_form.php');
112 $mform = new delete_category_form(null, $cattodelete);
113 if ($mform->is_cancelled()) {
114 redirect(new moodle_url('/course/manage.php'));
117 // Start output.
118 echo $OUTPUT->header();
119 echo $OUTPUT->heading($heading);
121 if ($data = $mform->get_data()) {
122 // The form has been submit handle it.
123 if ($data->fulldelete == 1 && $cattodelete->can_delete_full()) {
124 $cattodeletename = $cattodelete->get_formatted_name();
125 $deletedcourses = $cattodelete->delete_full(true);
126 foreach ($deletedcourses as $course) {
127 echo $OUTPUT->notification(get_string('coursedeleted', '', $course->shortname), 'notifysuccess');
129 echo $OUTPUT->notification(get_string('coursecategorydeleted', '', $cattodeletename), 'notifysuccess');
130 echo $OUTPUT->continue_button(new moodle_url('/course/manage.php'));
132 } else if ($data->fulldelete == 0 && $cattodelete->can_move_content_to($data->newparent)) {
133 $cattodelete->delete_move($data->newparent, true);
134 echo $OUTPUT->continue_button(new moodle_url('/course/manage.php'));
135 } else {
136 // Some error in parameters (user is cheating?)
137 $mform->display();
139 } else {
140 // Display the form.
141 $mform->display();
143 // Finish output and exit.
144 echo $OUTPUT->footer();
145 exit();
148 if (!empty($movecat) and ($movetocat >= 0) and confirm_sesskey()) {
149 // Move a category to a new parent if required.
150 $cattomove = coursecat::get($movecat);
151 if ($cattomove->parent != $movetocat) {
152 if ($cattomove->can_change_parent($movetocat)) {
153 $cattomove->change_parent($movetocat);
154 } else {
155 print_error('cannotmovecategory');
160 // Hide or show a category.
161 if ($hidecat and confirm_sesskey()) {
162 $cattohide = coursecat::get($hidecat);
163 require_capability('moodle/category:manage', get_category_or_system_context($cattohide->parent));
164 $cattohide->hide();
165 } else if ($showcat and confirm_sesskey()) {
166 $cattoshow = coursecat::get($showcat);
167 require_capability('moodle/category:manage', get_category_or_system_context($cattoshow->parent));
168 $cattoshow->show();
171 if ((!empty($moveupcat) or !empty($movedowncat)) and confirm_sesskey()) {
172 // Move a category up or down.
173 fix_course_sortorder();
174 $swapcategory = null;
176 if (!empty($moveupcat)) {
177 require_capability('moodle/category:manage', context_coursecat::instance($moveupcat));
178 if ($movecategory = $DB->get_record('course_categories', array('id' => $moveupcat))) {
179 $params = array($movecategory->sortorder, $movecategory->parent);
180 if ($swapcategory = $DB->get_records_select('course_categories', "sortorder<? AND parent=?", $params, 'sortorder DESC', '*', 0, 1)) {
181 $swapcategory = reset($swapcategory);
184 } else {
185 require_capability('moodle/category:manage', context_coursecat::instance($movedowncat));
186 if ($movecategory = $DB->get_record('course_categories', array('id' => $movedowncat))) {
187 $params = array($movecategory->sortorder, $movecategory->parent);
188 if ($swapcategory = $DB->get_records_select('course_categories', "sortorder>? AND parent=?", $params, 'sortorder ASC', '*', 0, 1)) {
189 $swapcategory = reset($swapcategory);
193 if ($swapcategory and $movecategory) {
194 $DB->set_field('course_categories', 'sortorder', $swapcategory->sortorder, array('id' => $movecategory->id));
195 $DB->set_field('course_categories', 'sortorder', $movecategory->sortorder, array('id' => $swapcategory->id));
196 cache_helper::purge_by_event('changesincoursecat');
197 add_to_log(SITEID, "category", "move", "editcategory.php?id=$movecategory->id", $movecategory->id);
200 // Finally reorder courses.
201 fix_course_sortorder();
204 if ($coursecat->id && $canmanage && $resort && confirm_sesskey()) {
205 // Resort the category.
206 if ($courses = get_courses($coursecat->id, '', 'c.id,c.fullname,c.sortorder')) {
207 core_collator::asort_objects_by_property($courses, 'fullname', core_collator::SORT_NATURAL);
208 $i = 1;
209 foreach ($courses as $course) {
210 $DB->set_field('course', 'sortorder', $coursecat->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();
215 cache_helper::purge_by_event('changesincourse');
219 if (!empty($moveto) && ($data = data_submitted()) && confirm_sesskey()) {
220 // Move a specified course to a new category.
221 // User must have category update in both cats to perform this.
222 require_capability('moodle/category:manage', $context);
223 require_capability('moodle/category:manage', context_coursecat::instance($moveto));
225 if (!$destcategory = $DB->get_record('course_categories', array('id' => $data->moveto))) {
226 print_error('cannotfindcategory', '', '', $data->moveto);
229 $courses = array();
230 foreach ($data as $key => $value) {
231 if (preg_match('/^c\d+$/', $key)) {
232 $courseid = substr($key, 1);
233 array_push($courses, $courseid);
234 // Check this course's category.
235 if ($movingcourse = $DB->get_record('course', array('id' => $courseid))) {
236 if ($id && $movingcourse->category != $id ) {
237 print_error('coursedoesnotbelongtocategory');
239 } else {
240 print_error('cannotfindcourse');
244 move_courses($courses, $data->moveto);
247 if ((!empty($hide) or !empty($show)) && confirm_sesskey()) {
248 // Hide or show a course.
249 if (!empty($hide)) {
250 $course = $DB->get_record('course', array('id' => $hide), '*', MUST_EXIST);
251 $visible = 0;
252 } else {
253 $course = $DB->get_record('course', array('id' => $show), '*', MUST_EXIST);
254 $visible = 1;
256 $coursecontext = context_course::instance($course->id);
257 require_capability('moodle/course:visibility', $coursecontext);
258 // Set the visibility of the course. we set the old flag when user manually changes visibility of course.
259 $params = array('id' => $course->id, 'visible' => $visible, 'visibleold' => $visible, 'timemodified' => time());
260 $DB->update_record('course', $params);
261 cache_helper::purge_by_event('changesincourse');
263 // Update the course object we pass to the event class.
264 $course->visible = $params['visible'];
265 $course->visibleold = $params['visibleold'];
266 $course->timemodified = $params['timemodified'];
268 // Trigger a course updated event.
269 $event = \core\event\course_updated::create(array(
270 'objectid' => $course->id,
271 'context' => $coursecontext,
272 'other' => array('shortname' => $course->shortname,
273 'fullname' => $course->fullname)
275 $event->add_record_snapshot('course', $course);
276 $event->set_legacy_logdata(array($course->id, 'course', ($visible ? 'show' : 'hide'), 'edit.php?id=' . $course->id, $course->id));
277 $event->trigger();
280 if ((!empty($moveup) or !empty($movedown)) && confirm_sesskey()) {
281 // Move a course up or down.
282 require_capability('moodle/category:manage', $context);
284 // Ensure the course order has continuous ordering.
285 fix_course_sortorder();
286 $swapcourse = null;
288 if (!empty($moveup)) {
289 if ($movecourse = $DB->get_record('course', array('id' => $moveup))) {
290 $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder - 1));
292 } else {
293 if ($movecourse = $DB->get_record('course', array('id' => $movedown))) {
294 $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder + 1));
297 if ($swapcourse and $movecourse) {
298 // Check course's category.
299 if ($movecourse->category != $id) {
300 print_error('coursedoesnotbelongtocategory');
302 $DB->set_field('course', 'sortorder', $swapcourse->sortorder, array('id' => $movecourse->id));
303 $DB->set_field('course', 'sortorder', $movecourse->sortorder, array('id' => $swapcourse->id));
304 cache_helper::purge_by_event('changesincourse');
306 // Update $movecourse's sortorder.
307 $movecourse->sortorder = $swapcourse->sortorder;
309 // Trigger a course updated event.
310 $event = \core\event\course_updated::create(array(
311 'objectid' => $movecourse->id,
312 'context' => context_course::instance($movecourse->id),
313 'other' => array('shortname' => $movecourse->shortname,
314 'fullname' => $movecourse->fullname)
316 $event->add_record_snapshot('course', $movecourse);
317 $event->set_legacy_logdata(array($movecourse->id, 'course', 'move', 'edit.php?id=' . $movecourse->id, $movecourse->id));
318 $event->trigger();
322 // Prepare the standard URL params for this page. We'll need them later.
323 $urlparams = array('categoryid' => $id);
324 if ($page) {
325 $urlparams['page'] = $page;
327 if ($perpage) {
328 $urlparams['perpage'] = $perpage;
330 $urlparams += $searchcriteria;
332 $PAGE->set_pagelayout('coursecategory');
333 $courserenderer = $PAGE->get_renderer('core', 'course');
335 if (can_edit_in_category()) {
336 // Integrate into the admin tree only if the user can edit categories at the top level,
337 // otherwise the admin block does not appear to this user, and you get an error.
338 require_once($CFG->libdir . '/adminlib.php');
339 if ($id) {
340 navigation_node::override_active_url(new moodle_url('/course/index.php', array('categoryid' => $id)));
342 admin_externalpage_setup('coursemgmt', '', $urlparams, $CFG->wwwroot . '/course/manage.php');
343 $settingsnode = $PAGE->settingsnav->find_active_node();
344 if ($id && $settingsnode) {
345 $settingsnode->make_inactive();
346 $settingsnode->force_open();
347 $PAGE->navbar->add($settingsnode->text, $settingsnode->action);
349 } else {
350 $site = get_site();
351 $PAGE->set_title("$site->shortname: $coursecat->name");
352 $PAGE->set_heading($site->fullname);
353 $PAGE->set_button($courserenderer->course_search_form('', 'navbar'));
356 // Start output.
357 echo $OUTPUT->header();
359 if (!empty($searchcriteria)) {
360 echo $OUTPUT->heading(new lang_string('searchresults'));
361 } else if (!$coursecat->id) {
362 // Print out the categories with all the knobs.
363 $manageablecategories = coursecat::make_categories_list('moodle/category:manage');
364 $displaymovecategoryto = !empty($manageablecategories) && (count($manageablecategories) <= COURSECAT_QUICKMOVE_LIMIT);
365 $table = new html_table;
366 $table->id = 'coursecategories';
367 $table->attributes['class'] = 'admintable generaltable editcourse';
368 $table->head = array(
369 get_string('categories'),
370 get_string('courses'),
371 get_string('edit'),
373 if ($displaymovecategoryto) {
374 $table->head[] = get_string('movecategoryto');
376 $table->colclasses = array(
377 'leftalign name',
378 'centeralign count',
379 'centeralign icons',
381 if ($displaymovecategoryto) {
382 $table->colclasses[] = 'leftalign actions';
384 $table->data = array();
386 print_category_edit($table, $coursecat, -1, false, false, $displaymovecategoryto);
388 echo html_writer::table($table);
389 } else {
390 // Print the category selector.
391 $displaylist = coursecat::make_categories_list();
392 $select = new single_select(new moodle_url('/course/manage.php'), 'categoryid', $displaylist, $coursecat->id, null, 'switchcategory');
393 $select->set_label(get_string('categories').':');
395 echo html_writer::start_tag('div', array('class' => 'categorypicker'));
396 echo $OUTPUT->render($select);
397 echo html_writer::end_tag('div');
400 if ($canmanage && empty($searchcriteria)) {
401 echo $OUTPUT->container_start('buttons');
402 // Print button to update this category.
403 if ($id) {
404 $url = new moodle_url('/course/editcategory.php', array('id' => $id));
405 echo $OUTPUT->single_button($url, get_string('editcategorythis'), 'get');
408 // Print button for creating new categories.
409 $url = new moodle_url('/course/editcategory.php', array('parent' => $id));
410 if ($id) {
411 $title = get_string('addsubcategory');
412 } else {
413 $title = get_string('addnewcategory');
415 echo $OUTPUT->single_button($url, $title, 'get');
416 echo $OUTPUT->container_end();
419 if (!empty($searchcriteria)) {
420 $courses = coursecat::get(0)->search_courses($searchcriteria, array('recursive' => true,
421 'offset' => $page * $perpage, 'limit' => $perpage, 'sort' => array('fullname' => 1)));
422 $numcourses = count($courses);
423 $totalcount = coursecat::get(0)->search_courses_count($searchcriteria, array('recursive' => true));
424 } else if ($coursecat->id) {
425 // Print out all the sub-categories (plain mode).
426 // In order to view hidden subcategories the user must have the viewhiddencategories.
427 // capability in the current category..
428 if (has_capability('moodle/category:viewhiddencategories', $context)) {
429 $categorywhere = '';
430 } else {
431 $categorywhere = 'AND cc.visible = 1';
433 // We're going to preload the context for the subcategory as we know that we
434 // need it later on for formatting.
435 $ctxselect = context_helper::get_preload_record_columns_sql('ctx');
436 $sql = "SELECT cc.*, $ctxselect
437 FROM {course_categories} cc
438 JOIN {context} ctx ON cc.id = ctx.instanceid
439 WHERE cc.parent = :parentid AND
440 ctx.contextlevel = :contextlevel
441 $categorywhere
442 ORDER BY cc.sortorder ASC";
443 $subcategories = $DB->get_recordset_sql($sql, array('parentid' => $coursecat->id, 'contextlevel' => CONTEXT_COURSECAT));
444 // Prepare a table to display the sub categories.
445 $table = new html_table;
446 $table->attributes = array(
447 'border' => '0',
448 'cellspacing' => '2',
449 'cellpadding' => '4',
450 'class' => 'generalbox boxaligncenter category_subcategories'
452 $table->head = array(new lang_string('subcategories'));
453 $table->data = array();
454 $baseurl = new moodle_url('/course/manage.php');
455 foreach ($subcategories as $subcategory) {
456 // Preload the context we will need it to format the category name shortly.
457 context_helper::preload_from_record($subcategory);
458 $context = context_coursecat::instance($subcategory->id);
459 // Prepare the things we need to create a link to the subcategory.
460 $attributes = $subcategory->visible ? array() : array('class' => 'dimmed');
461 $text = format_string($subcategory->name, true, array('context' => $context));
462 // Add the subcategory to the table.
463 $baseurl->param('categoryid', $subcategory->id);
464 $table->data[] = array(html_writer::link($baseurl, $text, $attributes));
467 $subcategorieswereshown = (count($table->data) > 0);
468 if ($subcategorieswereshown) {
469 echo html_writer::table($table);
472 $courses = get_courses_page($coursecat->id, 'c.sortorder ASC',
473 'c.id,c.sortorder,c.shortname,c.fullname,c.summary,c.visible',
474 $totalcount, $page*$perpage, $perpage);
475 $numcourses = count($courses);
476 } else {
477 $subcategorieswereshown = true;
478 $courses = array();
479 $numcourses = $totalcount = 0;
482 if (!$courses) {
483 // There is no course to display.
484 if (empty($subcategorieswereshown)) {
485 echo $OUTPUT->heading(get_string("nocoursesyet"));
487 } else {
488 // Display a basic list of courses with paging/editing options.
489 $table = new html_table;
490 $table->attributes = array('border' => 0, 'cellspacing' => 0, 'cellpadding' => '4', 'class' => 'generalbox boxaligncenter');
491 $table->head = array(
492 get_string('courses'),
493 get_string('edit'),
494 get_string('select')
496 $table->colclasses = array(null, null, 'mdl-align');
497 if (!empty($searchcriteria)) {
498 // add 'Category' column
499 array_splice($table->head, 1, 0, array(get_string('category')));
500 array_splice($table->colclasses, 1, 0, array(null));
502 $table->data = array();
504 $count = 0;
505 $abletomovecourses = false;
507 // Checking if we are at the first or at the last page, to allow courses to
508 // be moved up and down beyond the paging border.
509 if ($totalcount > $perpage) {
510 $atfirstpage = ($page == 0);
511 if ($perpage > 0) {
512 $atlastpage = (($page + 1) == ceil($totalcount / $perpage));
513 } else {
514 $atlastpage = true;
516 } else {
517 $atfirstpage = true;
518 $atlastpage = true;
521 $baseurl = new moodle_url('/course/manage.php', $urlparams + array('sesskey' => sesskey()));
522 foreach ($courses as $acourse) {
523 $coursecontext = context_course::instance($acourse->id);
525 $count++;
526 $up = ($count > 1 || !$atfirstpage);
527 $down = ($count < $numcourses || !$atlastpage);
529 $courseurl = new moodle_url('/course/view.php', array('id' => $acourse->id));
530 $attributes = array();
531 $attributes['class'] = $acourse->visible ? '' : 'dimmed';
532 $coursename = get_course_display_name_for_list($acourse);
533 $coursename = format_string($coursename, true, array('context' => $coursecontext));
534 $coursename = html_writer::link($courseurl, $coursename, $attributes);
536 $icons = array();
537 // Update course icon.
538 if (has_capability('moodle/course:update', $coursecontext)) {
539 $url = new moodle_url('/course/edit.php', array('id' => $acourse->id, 'category' => $id, 'returnto' => 'catmanage'));
540 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/edit', get_string('settings')));
543 // Role assignment icon.
544 if (has_capability('moodle/course:enrolreview', $coursecontext)) {
545 $url = new moodle_url('/enrol/users.php', array('id' => $acourse->id));
546 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/enrolusers', get_string('enrolledusers', 'enrol')));
549 // Delete course icon.
550 if (can_delete_course($acourse->id)) {
551 $url = new moodle_url('/course/delete.php', array('id' => $acourse->id));
552 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/delete', get_string('delete')));
555 // Change visibility.
556 // Users with no capability to view hidden courses, should not be able to lock themselves out.
557 if (has_any_capability(array('moodle/course:visibility', 'moodle/course:viewhiddencourses'), $coursecontext)) {
558 if (!empty($acourse->visible)) {
559 $url = new moodle_url($baseurl, array('hide' => $acourse->id));
560 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/hide', get_string('hide')));
561 } else {
562 $url = new moodle_url($baseurl, array('show' => $acourse->id));
563 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/show', get_string('show')));
567 // Backup course icon.
568 if (has_capability('moodle/backup:backupcourse', $coursecontext)) {
569 $url = new moodle_url('/backup/backup.php', array('id' => $acourse->id));
570 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/backup', get_string('backup')));
573 // Restore course icon.
574 if (has_capability('moodle/restore:restorecourse', $coursecontext)) {
575 $url = new moodle_url('/backup/restorefile.php', array('contextid' => $coursecontext->id));
576 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/restore', get_string('restore')));
579 if ($canmanage) {
580 if ($up && empty($searchcriteria)) {
581 $url = new moodle_url($baseurl, array('moveup' => $acourse->id));
582 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/up', get_string('moveup')));
584 if ($down && empty($searchcriteria)) {
585 $url = new moodle_url($baseurl, array('movedown' => $acourse->id));
586 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/down', get_string('movedown')));
588 $abletomovecourses = true;
591 $table->data[] = new html_table_row(array(
592 new html_table_cell($coursename),
593 new html_table_cell(join('', $icons)),
594 new html_table_cell(html_writer::empty_tag('input', array('type' => 'checkbox', 'name' => 'c'.$acourse->id)))
597 if (!empty($searchcriteria)) {
598 // add 'Category' column
599 $category = coursecat::get($acourse->category, IGNORE_MISSING, true);
600 $cell = new html_table_cell($category->get_formatted_name());
601 $cell->attributes['class'] = $category->visible ? '' : 'dimmed_text';
602 array_splice($table->data[count($table->data) - 1]->cells, 1, 0, array($cell));
606 if ($abletomovecourses) {
607 $movetocategories = coursecat::make_categories_list('moodle/category:manage');
608 $movetocategories[$id] = get_string('moveselectedcoursesto');
610 $cell = new html_table_cell();
611 $cell->colspan = 3;
612 $cell->attributes['class'] = 'mdl-right';
613 $cell->text = html_writer::label(get_string('moveselectedcoursesto'), 'movetoid', false, array('class' => 'accesshide'));
614 $cell->text .= html_writer::select($movetocategories, 'moveto', $id, null, array('id' => 'movetoid', 'class' => 'autosubmit'));
615 $cell->text .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'categoryid', 'value' => $id));
616 $PAGE->requires->yui_module('moodle-core-formautosubmit',
617 'M.core.init_formautosubmit',
618 array(array('selectid' => 'movetoid', 'nothing' => $id))
620 $table->data[] = new html_table_row(array($cell));
623 $actionurl = new moodle_url('/course/manage.php');
624 $pagingurl = new moodle_url('/course/manage.php', array('categoryid' => $id, 'perpage' => $perpage) + $searchcriteria);
626 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, $pagingurl);
627 echo html_writer::start_tag('form', array('id' => 'movecourses', 'action' => $actionurl, 'method' => 'post'));
628 echo html_writer::start_tag('div');
629 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
630 foreach ($searchcriteria as $key => $value) {
631 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => $key, 'value' => $value));
633 echo html_writer::table($table);
634 echo html_writer::end_tag('div');
635 echo html_writer::end_tag('form');
636 echo html_writer::empty_tag('br');
639 echo html_writer::start_tag('div', array('class' => 'buttons'));
640 if ($canmanage and $numcourses > 1 && empty($searchcriteria)) {
641 // Print button to re-sort courses by name.
642 $url = new moodle_url('/course/manage.php', array('categoryid' => $id, 'resort' => 'name', 'sesskey' => sesskey()));
643 echo $OUTPUT->single_button($url, get_string('resortcoursesbyname'), 'get');
646 if (has_capability('moodle/course:create', $context) && empty($searchcriteria)) {
647 // Print button to create a new course.
648 $url = new moodle_url('/course/edit.php');
649 if ($coursecat->id) {
650 $url->params(array('category' => $coursecat->id, 'returnto' => 'catmanage'));
651 } else {
652 $url->params(array('category' => $CFG->defaultrequestcategory, 'returnto' => 'topcatmanage'));
654 echo $OUTPUT->single_button($url, get_string('addnewcourse'), 'get');
657 if (!empty($CFG->enablecourserequests) && $id == $CFG->defaultrequestcategory) {
658 print_course_request_buttons(context_system::instance());
660 echo html_writer::end_tag('div');
662 echo $courserenderer->course_search_form();
664 echo $OUTPUT->footer();
667 * Recursive function to print all the categories ready for editing.
669 * @param html_table $table The table to add data to.
670 * @param coursecat $category The category to render
671 * @param int $depth The depth of the category.
672 * @param bool $up True if this category can be moved up.
673 * @param bool $down True if this category can be moved down.
674 * @param bool $displaymovecategoryto whether to display a dropdown for quick category move.
676 function print_category_edit(html_table $table, coursecat $category, $depth = -1, $up = false, $down = false, $displaymovecategoryto = true) {
677 global $OUTPUT;
679 static $str = null;
681 if (is_null($str)) {
682 $str = new stdClass;
683 $str->edit = new lang_string('edit');
684 $str->delete = new lang_string('delete');
685 $str->moveup = new lang_string('moveup');
686 $str->movedown = new lang_string('movedown');
687 $str->edit = new lang_string('editthiscategory');
688 $str->hide = new lang_string('hide');
689 $str->show = new lang_string('show');
690 $str->cohorts = new lang_string('cohorts', 'cohort');
691 $str->spacer = $OUTPUT->spacer().' ';
694 if ($category->id) {
696 $categorycontext = context_coursecat::instance($category->id);
698 $attributes = array();
699 $attributes['class'] = $category->visible ? '' : 'dimmed';
700 $attributes['title'] = $str->edit;
701 $categoryurl = new moodle_url('/course/manage.php', array('categoryid' => $category->id, 'sesskey' => sesskey()));
702 $categoryname = $category->get_formatted_name();
703 $categorypadding = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', $depth);
704 $categoryname = $categorypadding . html_writer::link($categoryurl, $categoryname, $attributes);
706 $icons = array();
707 if (has_capability('moodle/category:manage', $categorycontext)) {
708 // Edit category.
709 $icons[] = $OUTPUT->action_icon(
710 new moodle_url('/course/editcategory.php', array('id' => $category->id)),
711 new pix_icon('t/edit', $str->edit, 'moodle', array('class' => 'iconsmall')),
712 null, array('title' => $str->edit)
714 // Delete category.
715 $icons[] = $OUTPUT->action_icon(
716 new moodle_url('/course/manage.php', array('deletecat' => $category->id, 'sesskey' => sesskey())),
717 new pix_icon('t/delete', $str->delete, 'moodle', array('class' => 'iconsmall')),
718 null, array('title' => $str->delete)
720 // Change visibility.
721 if (!empty($category->visible)) {
722 $icons[] = $OUTPUT->action_icon(
723 new moodle_url('/course/manage.php', array('hidecat' => $category->id, 'sesskey' => sesskey())),
724 new pix_icon('t/hide', $str->hide, 'moodle', array('class' => 'iconsmall')),
725 null, array('title' => $str->hide)
727 } else {
728 $icons[] = $OUTPUT->action_icon(
729 new moodle_url('/course/manage.php', array('showcat' => $category->id, 'sesskey' => sesskey())),
730 new pix_icon('t/show', $str->show, 'moodle', array('class' => 'iconsmall')),
731 null, array('title' => $str->show)
734 // Cohorts.
735 if (has_any_capability(array('moodle/cohort:manage', 'moodle/cohort:view'), $categorycontext)) {
736 $icons[] = $OUTPUT->action_icon(
737 new moodle_url('/cohort/index.php', array('contextid' => $categorycontext->id)),
738 new pix_icon('t/cohort', $str->cohorts, 'moodle', array('class' => 'iconsmall')),
739 null, array('title' => $str->cohorts)
742 // Move up/down.
743 if ($up) {
744 $icons[] = $OUTPUT->action_icon(
745 new moodle_url('/course/manage.php', array('moveupcat' => $category->id, 'sesskey' => sesskey())),
746 new pix_icon('t/up', $str->moveup, 'moodle', array('class' => 'iconsmall')),
747 null, array('title' => $str->moveup)
749 } else {
750 $icons[] = $str->spacer;
752 if ($down) {
753 $icons[] = $OUTPUT->action_icon(
754 new moodle_url('/course/manage.php', array('movedowncat' => $category->id, 'sesskey' => sesskey())),
755 new pix_icon('t/down', $str->movedown, 'moodle', array('class' => 'iconsmall')),
756 null, array('title' => $str->movedown)
758 } else {
759 $icons[] = $str->spacer;
763 $actions = '';
764 if ($displaymovecategoryto && has_capability('moodle/category:manage', $categorycontext)) {
765 $popupurl = new moodle_url('/course/manage.php', array('movecat' => $category->id, 'sesskey' => sesskey()));
766 $tempdisplaylist = array(0 => get_string('top')) + coursecat::make_categories_list('moodle/category:manage', $category->id);
767 $select = new single_select($popupurl, 'movetocat', $tempdisplaylist, $category->parent, null, "moveform$category->id");
768 $select->set_label(get_string('frontpagecategorynames'), array('class' => 'accesshide'));
769 $actions = $OUTPUT->render($select);
772 $thisrowdata = array(
773 // Category name.
774 new html_table_cell($categoryname),
775 // Course count.
776 new html_table_cell($category->coursecount),
777 // Icons.
778 new html_table_cell(join(' ', $icons)),
780 if ($displaymovecategoryto) {
781 // Actions.
782 $thisrowdata[] = new html_table_cell($actions);
784 $table->data[] = new html_table_row($thisrowdata);
787 if ($categories = $category->get_children()) {
788 // Print all the children recursively.
789 $countcats = count($categories);
790 $count = 0;
791 $first = true;
792 $last = false;
793 foreach ($categories as $cat) {
794 $count++;
795 if ($count == $countcats) {
796 $last = true;
798 $up = $first ? false : true;
799 $down = $last ? false : true;
800 $first = false;
802 print_category_edit($table, $cat, $depth+1, $up, $down, $displaymovecategoryto);