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