MDL-37009 function print_course_search() is moved to course renderer
[moodle.git] / course / manage.php
CommitLineData
2c49fb4c
MG
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.
13//
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/>.
16
17/**
4d26b7f6 18 * Allows the admin to create, delete and rename course categories rearrange courses
2c49fb4c
MG
19 *
20 * @package core
21 * @copyright 2013 Marina Glancy
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25require_once("../config.php");
26require_once($CFG->dirroot.'/course/lib.php');
deb65ced 27require_once($CFG->libdir.'/coursecatlib.php');
2c49fb4c 28
4d26b7f6
SH
29// Category id.
30$id = optional_param('id', 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);
2c49fb4c 35
4d26b7f6 36// Actions to manage courses.
2c49fb4c
MG
37$hide = optional_param('hide', 0, PARAM_INT);
38$show = optional_param('show', 0, PARAM_INT);
39$moveup = optional_param('moveup', 0, PARAM_INT);
40$movedown = optional_param('movedown', 0, PARAM_INT);
41$moveto = optional_param('moveto', 0, PARAM_INT);
42$resort = optional_param('resort', 0, PARAM_BOOL);
43
4d26b7f6
SH
44// Actions to manage categories.
45$deletecat = optional_param('deletecat', 0, PARAM_INT);
46$hidecat = optional_param('hidecat', 0, PARAM_INT);
47$showcat = optional_param('showcat', 0, PARAM_INT);
48$movecat = optional_param('movecat', 0, PARAM_INT);
49$movetocat = optional_param('movetocat', -1, PARAM_INT);
50$moveupcat = optional_param('moveupcat', 0, PARAM_INT);
51$movedowncat = optional_param('movedowncat', 0, PARAM_INT);
2c49fb4c
MG
52
53require_login();
4d26b7f6 54
e1d54562
MG
55// Retrieve coursecat object
56// This will also make sure that category is accessible and create default category if missing
57$coursecat = coursecat::get($id);
58
2c49fb4c
MG
59if ($id) {
60 $PAGE->set_category_by_id($id);
4d26b7f6
SH
61 $PAGE->set_url(new moodle_url('/course/manage.php', array('id' => $id)));
62 // This is sure to be the category context.
2c49fb4c 63 $context = $PAGE->context;
e1d54562
MG
64 if (!can_edit_in_category($coursecat->id)) {
65 redirect(new moodle_url('/course/category.php', array('id' => $coursecat->id)));
4d26b7f6 66 }
2c49fb4c
MG
67} else {
68 $context = context_system::instance();
69 $PAGE->set_context($context);
4d26b7f6
SH
70 $PAGE->set_url(new moodle_url('/course/manage.php'));
71 if (!can_edit_in_category()) {
2c49fb4c
MG
72 redirect(new moodle_url('/course/index.php'));
73 }
74}
75
2c49fb4c 76$canmanage = has_capability('moodle/category:manage', $context);
2c49fb4c 77
2c49fb4c 78// Process any category actions.
2c49fb4c 79if (!empty($deletecat) and confirm_sesskey()) {
4d26b7f6 80 // Delete a category.
deb65ced 81 $cattodelete = coursecat::get($deletecat);
2c49fb4c
MG
82 $context = context_coursecat::instance($deletecat);
83 require_capability('moodle/category:manage', $context);
84 require_capability('moodle/category:manage', get_category_or_system_context($cattodelete->parent));
85
86 $heading = get_string('deletecategory', 'moodle', format_string($cattodelete->name, true, array('context' => $context)));
2c49fb4c 87
4d26b7f6
SH
88 require_once($CFG->dirroot.'/course/delete_category_form.php');
89 $mform = new delete_category_form(null, $cattodelete);
2c49fb4c 90 if ($mform->is_cancelled()) {
4d26b7f6 91 redirect(new moodle_url('/course/manage.php'));
2c49fb4c
MG
92 }
93
4d26b7f6 94 // Start output.
2c49fb4c
MG
95 echo $OUTPUT->header();
96 echo $OUTPUT->heading($heading);
97
4d26b7f6
SH
98 if ($data = $mform->get_data()) {
99 // The form has been submit handle it.
deb65ced
MG
100 if ($data->fulldelete == 1 && $cattodelete->can_delete_full()) {
101 $cattodeletename = $cattodelete->get_formatted_name();
102 $deletedcourses = $cattodelete->delete_full(true);
4d26b7f6
SH
103 foreach ($deletedcourses as $course) {
104 echo $OUTPUT->notification(get_string('coursedeleted', '', $course->shortname), 'notifysuccess');
105 }
b22b7403 106 echo $OUTPUT->notification(get_string('coursecategorydeleted', '', $cattodeletename), 'notifysuccess');
deb65ced 107 echo $OUTPUT->continue_button(new moodle_url('/course/manage.php'));
2c49fb4c 108
deb65ced
MG
109 } else if ($data->fulldelete == 0 && $cattodelete->can_move_content_to($data->newparent)) {
110 $cattodelete->delete_move($data->newparent, true);
111 echo $OUTPUT->continue_button(new moodle_url('/course/manage.php'));
4d26b7f6 112 } else {
deb65ced
MG
113 // Some error in parameters (user is cheating?)
114 $mform->display();
2c49fb4c 115 }
2c49fb4c 116 } else {
4d26b7f6 117 // Display the form.
4d26b7f6 118 $mform->display();
2c49fb4c 119 }
4d26b7f6 120 // Finish output and exit.
2c49fb4c 121 echo $OUTPUT->footer();
4d26b7f6 122 exit();
2c49fb4c
MG
123}
124
2c49fb4c 125if (!empty($movecat) and ($movetocat >= 0) and confirm_sesskey()) {
4d26b7f6 126 // Move a category to a new parent if required.
6e1d1ee0
MG
127 $cattomove = coursecat::get($movecat);
128 if ($cattomove->parent != $movetocat) {
129 if ($cattomove->can_change_parent($movetocat)) {
130 $cattomove->change_parent($movetocat);
131 } else {
132 print_error('cannotmovecategory');
2c49fb4c
MG
133 }
134 }
135}
136
4d26b7f6 137// Hide or show a category.
2c49fb4c 138if ($hidecat and confirm_sesskey()) {
e1d54562
MG
139 $cattohide = coursecat::get($hidecat);
140 require_capability('moodle/category:manage', get_category_or_system_context($cattohide->parent));
141 $cattohide->hide();
2c49fb4c 142} else if ($showcat and confirm_sesskey()) {
e1d54562
MG
143 $cattoshow = coursecat::get($showcat);
144 require_capability('moodle/category:manage', get_category_or_system_context($cattoshow->parent));
145 $cattoshow->show();
2c49fb4c
MG
146}
147
2c49fb4c 148if ((!empty($moveupcat) or !empty($movedowncat)) and confirm_sesskey()) {
4d26b7f6 149 // Move a category up or down.
2c49fb4c 150 fix_course_sortorder();
4d26b7f6
SH
151 $swapcategory = null;
152
2c49fb4c
MG
153 if (!empty($moveupcat)) {
154 require_capability('moodle/category:manage', context_coursecat::instance($moveupcat));
4d26b7f6 155 if ($movecategory = $DB->get_record('course_categories', array('id' => $moveupcat))) {
9d093b71 156 $params = array($movecategory->sortorder, $movecategory->parent);
4d26b7f6 157 if ($swapcategory = $DB->get_records_select('course_categories', "sortorder<? AND parent=?", $params, 'sortorder DESC', '*', 0, 1)) {
2c49fb4c
MG
158 $swapcategory = reset($swapcategory);
159 }
160 }
161 } else {
162 require_capability('moodle/category:manage', context_coursecat::instance($movedowncat));
4d26b7f6 163 if ($movecategory = $DB->get_record('course_categories', array('id' => $movedowncat))) {
9d093b71 164 $params = array($movecategory->sortorder, $movecategory->parent);
4d26b7f6 165 if ($swapcategory = $DB->get_records_select('course_categories', "sortorder>? AND parent=?", $params, 'sortorder ASC', '*', 0, 1)) {
2c49fb4c
MG
166 $swapcategory = reset($swapcategory);
167 }
168 }
169 }
170 if ($swapcategory and $movecategory) {
4d26b7f6
SH
171 $DB->set_field('course_categories', 'sortorder', $swapcategory->sortorder, array('id' => $movecategory->id));
172 $DB->set_field('course_categories', 'sortorder', $movecategory->sortorder, array('id' => $swapcategory->id));
2c49fb4c
MG
173 add_to_log(SITEID, "category", "move", "editcategory.php?id=$movecategory->id", $movecategory->id);
174 }
175
4d26b7f6 176 // Finally reorder courses.
2c49fb4c
MG
177 fix_course_sortorder();
178}
179
e1d54562 180if ($coursecat->id && $canmanage && $resort && confirm_sesskey()) {
4d26b7f6 181 // Resort the category.
e1d54562 182 if ($courses = get_courses($coursecat->id, '', 'c.id,c.fullname,c.sortorder')) {
2c49fb4c
MG
183 collatorlib::asort_objects_by_property($courses, 'fullname', collatorlib::SORT_NATURAL);
184 $i = 1;
185 foreach ($courses as $course) {
e1d54562 186 $DB->set_field('course', 'sortorder', $coursecat->sortorder + $i, array('id' => $course->id));
2c49fb4c
MG
187 $i++;
188 }
4d26b7f6
SH
189 // This should not be needed but we do it just to be safe.
190 fix_course_sortorder();
eabbfa82 191 cache_helper::purge_by_event('changesincourse');
2c49fb4c
MG
192 }
193}
194
e6a3f4ae 195if (!empty($moveto) && ($data = data_submitted()) && confirm_sesskey()) {
4d26b7f6
SH
196 // Move a specified course to a new category.
197 // User must have category update in both cats to perform this.
198 require_capability('moodle/category:manage', $context);
199 require_capability('moodle/category:manage', context_coursecat::instance($moveto));
2c49fb4c 200
4d26b7f6
SH
201 if (!$destcategory = $DB->get_record('course_categories', array('id' => $data->moveto))) {
202 print_error('cannotfindcategory', '', '', $data->moveto);
203 }
2c49fb4c 204
4d26b7f6
SH
205 $courses = array();
206 foreach ($data as $key => $value) {
207 if (preg_match('/^c\d+$/', $key)) {
208 $courseid = substr($key, 1);
209 array_push($courses, $courseid);
210 // Check this course's category.
211 if ($movingcourse = $DB->get_record('course', array('id' => $courseid))) {
212 if ($movingcourse->category != $id ) {
213 print_error('coursedoesnotbelongtocategory');
2c49fb4c 214 }
4d26b7f6
SH
215 } else {
216 print_error('cannotfindcourse');
2c49fb4c
MG
217 }
218 }
2c49fb4c 219 }
4d26b7f6
SH
220 move_courses($courses, $data->moveto);
221}
2c49fb4c 222
4d26b7f6
SH
223if ((!empty($hide) or !empty($show)) && confirm_sesskey()) {
224 // Hide or show a course.
225 if (!empty($hide)) {
226 $course = $DB->get_record('course', array('id' => $hide), '*', MUST_EXIST);
227 $visible = 0;
228 } else {
229 $course = $DB->get_record('course', array('id' => $show), '*', MUST_EXIST);
230 $visible = 1;
2c49fb4c 231 }
4d26b7f6
SH
232 $coursecontext = context_course::instance($course->id);
233 require_capability('moodle/course:visibility', $coursecontext);
234 // Set the visibility of the course. we set the old flag when user manually changes visibility of course.
235 $params = array('id' => $course->id, 'visible' => $visible, 'visibleold' => $visible, 'timemodified' => time());
236 $DB->update_record('course', $params);
eabbfa82 237 cache_helper::purge_by_event('changesincourse');
4d26b7f6
SH
238 add_to_log($course->id, "course", ($visible ? 'show' : 'hide'), "edit.php?id=$course->id", $course->id);
239}
2c49fb4c 240
4d26b7f6
SH
241if ((!empty($moveup) or !empty($movedown)) && confirm_sesskey()) {
242 // Move a course up or down.
243 require_capability('moodle/category:manage', $context);
2c49fb4c 244
4d26b7f6
SH
245 // Ensure the course order has continuous ordering.
246 fix_course_sortorder();
247 $swapcourse = null;
2c49fb4c 248
4d26b7f6
SH
249 if (!empty($moveup)) {
250 if ($movecourse = $DB->get_record('course', array('id' => $moveup))) {
251 $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder - 1));
2c49fb4c 252 }
4d26b7f6
SH
253 } else {
254 if ($movecourse = $DB->get_record('course', array('id' => $movedown))) {
255 $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder + 1));
2c49fb4c
MG
256 }
257 }
4d26b7f6
SH
258 if ($swapcourse and $movecourse) {
259 // Check course's category.
260 if ($movecourse->category != $id) {
261 print_error('coursedoesnotbelongtocategory');
262 }
263 $DB->set_field('course', 'sortorder', $swapcourse->sortorder, array('id' => $movecourse->id));
264 $DB->set_field('course', 'sortorder', $movecourse->sortorder, array('id' => $swapcourse->id));
eabbfa82 265 cache_helper::purge_by_event('changesincourse');
4d26b7f6
SH
266 add_to_log($movecourse->id, "course", "move", "edit.php?id=$movecourse->id", $movecourse->id);
267 }
268}
2c49fb4c
MG
269
270// Prepare the standard URL params for this page. We'll need them later.
271$urlparams = array('id' => $id);
272if ($page) {
273 $urlparams['page'] = $page;
274}
275if ($perpage) {
276 $urlparams['perpage'] = $perpage;
277}
278
2c49fb4c 279$PAGE->set_pagelayout('coursecategory');
f4b571ab 280$courserenderer = $PAGE->get_renderer('core', 'course');
4d26b7f6 281
2c49fb4c
MG
282if (can_edit_in_category()) {
283 // Integrate into the admin tree only if the user can edit categories at the top level,
284 // otherwise the admin block does not appear to this user, and you get an error.
285 require_once($CFG->libdir . '/adminlib.php');
286 if ($id) {
287 navigation_node::override_active_url(new moodle_url('/course/category.php', array('id' => $id)));
288 }
289 admin_externalpage_setup('coursemgmt', '', $urlparams, $CFG->wwwroot . '/course/manage.php');
2c49fb4c
MG
290 $settingsnode = $PAGE->settingsnav->find_active_node();
291 if ($id && $settingsnode) {
292 $settingsnode->make_inactive();
293 $settingsnode->force_open();
294 $PAGE->navbar->add($settingsnode->text, $settingsnode->action);
295 }
2c49fb4c
MG
296} else {
297 $site = get_site();
e1d54562 298 $PAGE->set_title("$site->shortname: $coursecat->name");
2c49fb4c 299 $PAGE->set_heading($site->fullname);
f4b571ab 300 $PAGE->set_button($courserenderer->course_search_form('', 'navbar'));
2c49fb4c
MG
301}
302
4d26b7f6 303$displaylist[0] = get_string('top');
2c49fb4c 304
4d26b7f6
SH
305// Start output.
306echo $OUTPUT->header();
2c49fb4c 307
e1d54562 308if (!$coursecat->id) {
4d26b7f6
SH
309 // Print out the categories with all the knobs.
310 $table = new html_table;
311 $table->id = 'coursecategories';
312 $table->attributes['class'] = 'admintable generaltable editcourse';
313 $table->head = array(
314 get_string('categories'),
315 get_string('courses'),
4d26b7f6 316 get_string('edit'),
7d579b1e 317 get_string('movecategoryto'),
4d26b7f6
SH
318 );
319 $table->colclasses = array(
320 'leftalign name',
321 'centeralign count',
322 'centeralign icons',
323 'leftalign actions'
324 );
325 $table->data = array();
2c49fb4c 326
e1d54562 327 print_category_edit($table, $coursecat);
2c49fb4c 328
4d26b7f6 329 echo html_writer::table($table);
2c49fb4c 330} else {
4d26b7f6 331 // Print the category selector.
e1d54562 332 $select = new single_select(new moodle_url('/course/manage.php'), 'id', $displaylist, $coursecat->id, null, 'switchcategory');
2c49fb4c 333 $select->set_label(get_string('categories').':');
4d26b7f6
SH
334
335 echo html_writer::start_tag('div', array('class' => 'categorypicker'));
2c49fb4c 336 echo $OUTPUT->render($select);
4d26b7f6 337 echo html_writer::end_tag('div');
2c49fb4c
MG
338}
339
340if ($canmanage) {
341 echo $OUTPUT->container_start('buttons');
4d26b7f6
SH
342 // Print button to update this category.
343 if ($id) {
344 $url = new moodle_url('/course/editcategory.php', array('id' => $id));
2c49fb4c
MG
345 echo $OUTPUT->single_button($url, get_string('editcategorythis'), 'get');
346 }
347
4d26b7f6
SH
348 // Print button for creating new categories.
349 $url = new moodle_url('/course/editcategory.php', array('parent' => $id));
79137823
SH
350 if ($id) {
351 $title = get_string('addsubcategory');
352 } else {
353 $title = get_string('addnewcategory');
354 }
355 echo $OUTPUT->single_button($url, $title, 'get');
2c49fb4c
MG
356 echo $OUTPUT->container_end();
357}
358
e1d54562 359if ($coursecat->id) {
4d26b7f6
SH
360 // Print out all the sub-categories (plain mode).
361 // In order to view hidden subcategories the user must have the viewhiddencategories.
362 // capability in the current category..
2c49fb4c
MG
363 if (has_capability('moodle/category:viewhiddencategories', $context)) {
364 $categorywhere = '';
365 } else {
366 $categorywhere = 'AND cc.visible = 1';
367 }
368 // We're going to preload the context for the subcategory as we know that we
369 // need it later on for formatting.
2c49fb4c
MG
370 $ctxselect = context_helper::get_preload_record_columns_sql('ctx');
371 $sql = "SELECT cc.*, $ctxselect
372 FROM {course_categories} cc
373 JOIN {context} ctx ON cc.id = ctx.instanceid
374 WHERE cc.parent = :parentid AND
375 ctx.contextlevel = :contextlevel
376 $categorywhere
377 ORDER BY cc.sortorder ASC";
e1d54562 378 $subcategories = $DB->get_recordset_sql($sql, array('parentid' => $coursecat->id, 'contextlevel' => CONTEXT_COURSECAT));
2c49fb4c
MG
379 // Prepare a table to display the sub categories.
380 $table = new html_table;
4d26b7f6
SH
381 $table->attributes = array(
382 'border' => '0',
383 'cellspacing' => '2',
384 'cellpadding' => '4',
385 'class' => 'generalbox boxaligncenter category_subcategories'
386 );
2c49fb4c
MG
387 $table->head = array(new lang_string('subcategories'));
388 $table->data = array();
389 $baseurl = new moodle_url('/course/manage.php');
390 foreach ($subcategories as $subcategory) {
391 // Preload the context we will need it to format the category name shortly.
392 context_helper::preload_from_record($subcategory);
393 $context = context_coursecat::instance($subcategory->id);
4d26b7f6 394 // Prepare the things we need to create a link to the subcategory.
2c49fb4c
MG
395 $attributes = $subcategory->visible ? array() : array('class' => 'dimmed');
396 $text = format_string($subcategory->name, true, array('context' => $context));
4d26b7f6 397 // Add the subcategory to the table.
2c49fb4c
MG
398 $baseurl->param('id', $subcategory->id);
399 $table->data[] = array(html_writer::link($baseurl, $text, $attributes));
400 }
401
402 $subcategorieswereshown = (count($table->data) > 0);
403 if ($subcategorieswereshown) {
404 echo html_writer::table($table);
405 }
406
e1d54562 407 $courses = get_courses_page($coursecat->id, 'c.sortorder ASC',
2c49fb4c
MG
408 'c.id,c.sortorder,c.shortname,c.fullname,c.summary,c.visible',
409 $totalcount, $page*$perpage, $perpage);
410 $numcourses = count($courses);
411} else {
412 $subcategorieswereshown = true;
413 $courses = array();
414 $numcourses = $totalcount = 0;
415}
416
417if (!$courses) {
418 // There is no course to display.
419 if (empty($subcategorieswereshown)) {
420 echo $OUTPUT->heading(get_string("nocoursesyet"));
421 }
422} else {
4d26b7f6
SH
423 // Display a basic list of courses with paging/editing options.
424 $table = new html_table;
425 $table->attributes = array('border' => 0, 'cellspacing' => 0, 'cellpadding' => '4', 'class' => 'generalbox boxaligncenter');
426 $table->head = array(
427 get_string('courses'),
428 get_string('edit'),
429 get_string('select')
430 );
431 $table->colclasses = array(null, null, 'mdl-align');
432 $table->data = array();
2c49fb4c
MG
433
434 $count = 0;
4d26b7f6 435 $abletomovecourses = false;
2c49fb4c
MG
436
437 // Checking if we are at the first or at the last page, to allow courses to
4d26b7f6 438 // be moved up and down beyond the paging border.
2c49fb4c
MG
439 if ($totalcount > $perpage) {
440 $atfirstpage = ($page == 0);
441 if ($perpage > 0) {
442 $atlastpage = (($page + 1) == ceil($totalcount / $perpage));
443 } else {
444 $atlastpage = true;
445 }
446 } else {
447 $atfirstpage = true;
448 $atlastpage = true;
449 }
450
451 $baseurl = new moodle_url('/course/manage.php', $urlparams + array('sesskey' => sesskey()));
452 foreach ($courses as $acourse) {
453 $coursecontext = context_course::instance($acourse->id);
454
455 $count++;
456 $up = ($count > 1 || !$atfirstpage);
457 $down = ($count < $numcourses || !$atlastpage);
458
4d26b7f6
SH
459 $courseurl = new moodle_url('/course/view.php', array('id' => $acourse->id));
460 $attributes = array();
461 $attributes['class'] = $acourse->visible ? '' : 'dimmed';
2c49fb4c 462 $coursename = get_course_display_name_for_list($acourse);
4d26b7f6
SH
463 $coursename = format_string($coursename, true, array('context' => $coursecontext));
464 $coursename = html_writer::link($courseurl, $coursename, $attributes);
465
466 $icons = array();
467 // Update course icon.
2c49fb4c
MG
468 if (has_capability('moodle/course:update', $coursecontext)) {
469 $url = new moodle_url('/course/edit.php', array('id' => $acourse->id, 'category' => $id, 'returnto' => 'catmanage'));
4d26b7f6 470 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/edit', get_string('settings')));
2c49fb4c
MG
471 }
472
4d26b7f6 473 // Role assignment icon.
2c49fb4c
MG
474 if (has_capability('moodle/course:enrolreview', $coursecontext)) {
475 $url = new moodle_url('/enrol/users.php', array('id' => $acourse->id));
4d26b7f6 476 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/enrolusers', get_string('enrolledusers', 'enrol')));
2c49fb4c
MG
477 }
478
4d26b7f6 479 // Delete course icon.
2c49fb4c
MG
480 if (can_delete_course($acourse->id)) {
481 $url = new moodle_url('/course/delete.php', array('id' => $acourse->id));
4d26b7f6 482 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/delete', get_string('delete')));
2c49fb4c
MG
483 }
484
4d26b7f6
SH
485 // Change visibility.
486 // Users with no capability to view hidden courses, should not be able to lock themselves out.
ef5a8210 487 if (has_any_capability(array('moodle/course:visibility', 'moodle/course:viewhiddencourses'), $coursecontext)) {
2c49fb4c
MG
488 if (!empty($acourse->visible)) {
489 $url = new moodle_url($baseurl, array('hide' => $acourse->id));
4d26b7f6 490 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/hide', get_string('hide')));
2c49fb4c
MG
491 } else {
492 $url = new moodle_url($baseurl, array('show' => $acourse->id));
4d26b7f6 493 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/show', get_string('show')));
2c49fb4c
MG
494 }
495 }
496
4d26b7f6 497 // Backup course icon.
2c49fb4c
MG
498 if (has_capability('moodle/backup:backupcourse', $coursecontext)) {
499 $url = new moodle_url('/backup/backup.php', array('id' => $acourse->id));
4d26b7f6 500 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/backup', get_string('backup')));
2c49fb4c
MG
501 }
502
4d26b7f6 503 // Restore course icon.
2c49fb4c
MG
504 if (has_capability('moodle/restore:restorecourse', $coursecontext)) {
505 $url = new moodle_url('/backup/restorefile.php', array('contextid' => $coursecontext->id));
4d26b7f6 506 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/restore', get_string('restore')));
2c49fb4c
MG
507 }
508
509 if ($canmanage) {
510 if ($up) {
511 $url = new moodle_url($baseurl, array('moveup' => $acourse->id));
4d26b7f6 512 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/up', get_string('moveup')));
2c49fb4c 513 }
2c49fb4c
MG
514 if ($down) {
515 $url = new moodle_url($baseurl, array('movedown' => $acourse->id));
4d26b7f6 516 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/down', get_string('movedown')));
2c49fb4c
MG
517 }
518 $abletomovecourses = true;
519 }
520
4d26b7f6
SH
521 $table->data[] = new html_table_row(array(
522 new html_table_cell($coursename),
523 new html_table_cell(join('', $icons)),
524 new html_table_cell(html_writer::empty_tag('input', array('type' => 'checkbox', 'name' => 'c'.$acourse->id)))
525 ));
2c49fb4c
MG
526 }
527
528 if ($abletomovecourses) {
4e0b6025 529 $movetocategories = coursecat::make_categories_list('moodle/category:manage');
4d26b7f6
SH
530 $movetocategories[$id] = get_string('moveselectedcoursesto');
531
532 $cell = new html_table_cell();
533 $cell->colspan = 3;
534 $cell->attributes['class'] = 'mdl-right';
535 $cell->text = html_writer::label(get_string('moveselectedcoursesto'), 'movetoid', false, array('class' => 'accesshide'));
536 $cell->text .= html_writer::select($movetocategories, 'moveto', $id, null, array('id'=>'movetoid', 'class' => 'autosubmit'));
537 $cell->text .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'id', 'value' => $id));
2c49fb4c
MG
538 $PAGE->requires->yui_module('moodle-core-formautosubmit',
539 'M.core.init_formautosubmit',
4d26b7f6 540 array(array('selectid' => 'movetoid', 'nothing' => $id))
2c49fb4c 541 );
4d26b7f6 542 $table->data[] = new html_table_row(array($cell));
2c49fb4c
MG
543 }
544
4d26b7f6
SH
545 $actionurl = new moodle_url('/course/manage.php');
546 $pagingurl = new moodle_url('/course/manage.php', array('id' => $id, 'perpage' => $perpage));
547
548 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, $pagingurl);
549 echo html_writer::start_tag('form', array('id' => 'movecourses', 'action' => $actionurl, 'method' => 'post'));
550 echo html_writer::start_tag('div');
551 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
552 echo html_writer::table($table);
553 echo html_writer::end_tag('div');
554 echo html_writer::end_tag('form');
555 echo html_writer::empty_tag('br');
2c49fb4c
MG
556}
557
4d26b7f6 558echo html_writer::start_tag('div', array('class' => 'buttons'));
2c49fb4c 559if ($canmanage and $numcourses > 1) {
4d26b7f6
SH
560 // Print button to re-sort courses by name.
561 $url = new moodle_url('/course/manage.php', array('id' => $id, 'resort' => 'name', 'sesskey' => sesskey()));
2c49fb4c
MG
562 echo $OUTPUT->single_button($url, get_string('resortcoursesbyname'), 'get');
563}
564
565if (has_capability('moodle/course:create', $context)) {
4d26b7f6 566 // Print button to create a new course.
2c49fb4c 567 $url = new moodle_url('/course/edit.php');
e1d54562
MG
568 if ($coursecat->id) {
569 $url->params(array('category' => $coursecat->id, 'returnto' => 'catmanage'));
2c49fb4c 570 } else {
4d26b7f6 571 $url->params(array('category' => $CFG->defaultrequestcategory, 'returnto' => 'topcatmanage'));
2c49fb4c
MG
572 }
573 echo $OUTPUT->single_button($url, get_string('addnewcourse'), 'get');
574}
575
4d26b7f6 576if (!empty($CFG->enablecourserequests) && $id == $CFG->defaultrequestcategory) {
2c49fb4c
MG
577 print_course_request_buttons(context_system::instance());
578}
4d26b7f6 579echo html_writer::end_tag('div');
2c49fb4c 580
f4b571ab 581echo $courserenderer->course_search_form();
2c49fb4c
MG
582
583echo $OUTPUT->footer();
584
4d26b7f6
SH
585/**
586 * Recursive function to print all the categories ready for editing.
587 *
588 * @param html_table $table The table to add data to.
e1d54562 589 * @param coursecat $category The category to render
4d26b7f6
SH
590 * @param int $depth The depth of the category.
591 * @param bool $up True if this category can be moved up.
592 * @param bool $down True if this category can be moved down.
593 */
e1d54562 594function print_category_edit(html_table $table, coursecat $category, $depth = -1, $up = false, $down = false) {
4d26b7f6 595 global $OUTPUT;
2c49fb4c 596
4d26b7f6 597 static $str = null;
2c49fb4c
MG
598
599 if (is_null($str)) {
600 $str = new stdClass;
4d26b7f6
SH
601 $str->edit = new lang_string('edit');
602 $str->delete = new lang_string('delete');
603 $str->moveup = new lang_string('moveup');
604 $str->movedown = new lang_string('movedown');
605 $str->edit = new lang_string('editthiscategory');
606 $str->hide = new lang_string('hide');
607 $str->show = new lang_string('show');
608 $str->cohorts = new lang_string('cohorts', 'cohort');
2c49fb4c
MG
609 $str->spacer = $OUTPUT->spacer().' ';
610 }
611
e1d54562 612 if ($category->id) {
2c49fb4c 613
e1d54562 614 $categorycontext = context_coursecat::instance($category->id);
2c49fb4c 615
4d26b7f6
SH
616 $attributes = array();
617 $attributes['class'] = $category->visible ? '' : 'dimmed';
618 $attributes['title'] = $str->edit;
619 $categoryurl = new moodle_url('/course/manage.php', array('id' => $category->id, 'sesskey' => sesskey()));
e1d54562 620 $categoryname = $category->get_formatted_name();
4d26b7f6
SH
621 $categorypadding = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', $depth);
622 $categoryname = $categorypadding . html_writer::link($categoryurl, $categoryname, $attributes);
2c49fb4c 623
4d26b7f6 624 $icons = array();
e1d54562 625 if (has_capability('moodle/category:manage', $categorycontext)) {
4d26b7f6
SH
626 // Edit category.
627 $icons[] = $OUTPUT->action_icon(
628 new moodle_url('/course/editcategory.php', array('id' => $category->id)),
629 new pix_icon('t/edit', $str->edit, 'moodle', array('class' => 'iconsmall')),
630 null, array('title' => $str->edit)
631 );
632 // Delete category.
633 $icons[] = $OUTPUT->action_icon(
634 new moodle_url('/course/manage.php', array('deletecat' => $category->id, 'sesskey' => sesskey())),
635 new pix_icon('t/delete', $str->delete, 'moodle', array('class' => 'iconsmall')),
636 null, array('title' => $str->delete)
637 );
638 // Change visibility.
2c49fb4c 639 if (!empty($category->visible)) {
4d26b7f6
SH
640 $icons[] = $OUTPUT->action_icon(
641 new moodle_url('/course/manage.php', array('hidecat' => $category->id, 'sesskey' => sesskey())),
642 new pix_icon('t/hide', $str->hide, 'moodle', array('class' => 'iconsmall')),
643 null, array('title' => $str->hide)
644 );
2c49fb4c 645 } else {
4d26b7f6
SH
646 $icons[] = $OUTPUT->action_icon(
647 new moodle_url('/course/manage.php', array('showcat' => $category->id, 'sesskey' => sesskey())),
648 new pix_icon('t/show', $str->show, 'moodle', array('class' => 'iconsmall')),
649 null, array('title' => $str->show)
650 );
2c49fb4c 651 }
4d26b7f6 652 // Cohorts.
e1d54562 653 if (has_any_capability(array('moodle/cohort:manage', 'moodle/cohort:view'), $categorycontext)) {
4d26b7f6 654 $icons[] = $OUTPUT->action_icon(
e1d54562 655 new moodle_url('/cohort/index.php', array('contextid' => $categorycontext->id)),
4d26b7f6
SH
656 new pix_icon('t/cohort', $str->cohorts, 'moodle', array('class' => 'iconsmall')),
657 null, array('title' => $str->cohorts)
658 );
2c49fb4c 659 }
4d26b7f6 660 // Move up/down.
2c49fb4c 661 if ($up) {
4d26b7f6
SH
662 $icons[] = $OUTPUT->action_icon(
663 new moodle_url('/course/manage.php', array('moveupcat' => $category->id, 'sesskey' => sesskey())),
664 new pix_icon('t/up', $str->moveup, 'moodle', array('class' => 'iconsmall')),
665 null, array('title' => $str->moveup)
666 );
2c49fb4c 667 } else {
4d26b7f6 668 $icons[] = $str->spacer;
2c49fb4c
MG
669 }
670 if ($down) {
4d26b7f6
SH
671 $icons[] = $OUTPUT->action_icon(
672 new moodle_url('/course/manage.php', array('movedowncat' => $category->id, 'sesskey' => sesskey())),
673 new pix_icon('t/down', $str->movedown, 'moodle', array('class' => 'iconsmall')),
674 null, array('title' => $str->movedown)
675 );
2c49fb4c 676 } else {
4d26b7f6 677 $icons[] = $str->spacer;
2c49fb4c
MG
678 }
679 }
2c49fb4c 680
4d26b7f6 681 $actions = '';
e1d54562 682 if (has_capability('moodle/category:manage', $categorycontext)) {
2c49fb4c 683 $popupurl = new moodle_url("manage.php?movecat=$category->id&sesskey=".sesskey());
4e0b6025 684 $tempdisplaylist = array(0 => get_string('top')) + coursecat::make_categories_list('moodle/category:manage', $category->id);
2c49fb4c
MG
685 $select = new single_select($popupurl, 'movetocat', $tempdisplaylist, $category->parent, null, "moveform$category->id");
686 $select->set_label(get_string('frontpagecategorynames'), array('class' => 'accesshide'));
4d26b7f6 687 $actions = $OUTPUT->render($select);
2c49fb4c 688 }
4d26b7f6
SH
689
690 $table->data[] = new html_table_row(array(
691 // Category name.
692 new html_table_cell($categoryname),
693 // Course count.
694 new html_table_cell($category->coursecount),
695 // Icons.
696 new html_table_cell(join(' ', $icons)),
697 // Actions.
698 new html_table_cell($actions)
699 ));
2c49fb4c
MG
700 }
701
e1d54562 702 if ($categories = $category->get_children()) {
4d26b7f6 703 // Print all the children recursively.
2c49fb4c
MG
704 $countcats = count($categories);
705 $count = 0;
706 $first = true;
707 $last = false;
708 foreach ($categories as $cat) {
709 $count++;
710 if ($count == $countcats) {
711 $last = true;
712 }
713 $up = $first ? false : true;
714 $down = $last ? false : true;
715 $first = false;
716
4e0b6025 717 print_category_edit($table, $cat, $depth+1, $up, $down);
2c49fb4c
MG
718 }
719 }
ef5a8210 720}