Merge branch '41502-28' of git://github.com/samhemelryk/moodle
[moodle.git] / course / management.php
CommitLineData
5dc361e1
SH
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/**
18 * Course and category management interfaces.
19 *
20 * @package core_course
21 * @copyright 2013 Sam Hemelryk
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25require_once('../config.php');
26require_once($CFG->dirroot.'/lib/coursecatlib.php');
27require_once($CFG->dirroot.'/course/lib.php');
28
29$categoryid = optional_param('categoryid', null, PARAM_INT);
cda49969 30$selectedcategoryid = optional_param('selectedcategoryid', null, PARAM_INT);
5dc361e1
SH
31$courseid = optional_param('courseid', null, PARAM_INT);
32$action = optional_param('action', false, PARAM_ALPHA);
33$page = optional_param('page', 0, PARAM_INT);
34$perpage = optional_param('perpage', null, PARAM_INT);
35$viewmode = optional_param('view', 'default', PARAM_ALPHA); // Can be one of default, combined, courses, or categories.
36
37// Search related params.
38$search = optional_param('search', '', PARAM_RAW); // Search words. Shortname, fullname, idnumber and summary get searched.
39$blocklist = optional_param('blocklist', 0, PARAM_INT); // Find courses containing this block.
40$modulelist = optional_param('modulelist', '', PARAM_PLUGIN); // Find courses containing the given modules.
41
484c4c6c
SH
42if (!in_array($viewmode, array('default', 'combined', 'courses', 'categories'))) {
43 $viewmode = 'default';
44}
45
5dc361e1
SH
46$issearching = ($search !== '' || $blocklist !== 0 || $modulelist !== '');
47if ($issearching) {
48 $viewmode = 'courses';
49}
50
51$url = new moodle_url('/course/management.php');
b488058f 52$systemcontext = $context = context_system::instance();
5dc361e1
SH
53if ($courseid) {
54 $record = get_course($courseid);
55 $course = new course_in_list($record);
56 $category = coursecat::get($course->category);
57 $categoryid = $category->id;
b488058f 58 $context = context_coursecat::instance($category->id);
d5814f4e 59 $url->param('categoryid', $categoryid);
5dc361e1 60 $url->param('courseid', $course->id);
d5814f4e 61
5dc361e1
SH
62} else if ($categoryid) {
63 $courseid = null;
64 $course = null;
65 $category = coursecat::get($categoryid);
b488058f 66 $context = context_coursecat::instance($category->id);
5dc361e1 67 $url->param('categoryid', $category->id);
d5814f4e 68
5dc361e1
SH
69} else {
70 $course = null;
71 $courseid = null;
ddd2361e
SH
72 $category = coursecat::get_default();
73 $categoryid = $category->id;
fa4f0de6
EL
74 $context = context_coursecat::instance($category->id);
75 $url->param('categoryid', $category->id);
5dc361e1 76}
484c4c6c 77
cda49969
SH
78// Check if there is a selected category param, and if there is apply it.
79if ($course === null && $selectedcategoryid !== null && $selectedcategoryid !== $categoryid) {
80 $url->param('categoryid', $selectedcategoryid);
81}
82
5dc361e1
SH
83if ($page !== 0) {
84 $url->param('page', $page);
85}
86if ($viewmode !== 'default') {
87 $url->param('view', $viewmode);
88}
89if ($search !== '') {
90 $url->param('search', $search);
91}
92if ($blocklist !== 0) {
93 $url->param('blocklist', $search);
94}
95if ($modulelist !== '') {
96 $url->param('modulelist', $search);
97}
98
62846237 99$strmanagement = new lang_string('coursecatmanagement');
52c93a26 100$pageheading = format_string($SITE->fullname, true, array('context' => $systemcontext));
5dc361e1
SH
101
102$PAGE->set_context($context);
103$PAGE->set_url($url);
104$PAGE->set_pagelayout('admin');
52c93a26
SH
105$PAGE->set_title($strmanagement);
106$PAGE->set_heading($pageheading);
5dc361e1 107
962885e8
SH
108// This is a system level page that operates on other contexts.
109require_login();
110
484c4c6c
SH
111if (!coursecat::has_capability_on_any(array('moodle/category:manage', 'moodle/course:create'))) {
112 // The user isn't able to manage any categories. Lets redirect them to the relevant course/index.php page.
113 $url = new moodle_url('/course/index.php');
114 if ($categoryid) {
115 $url->param('categoryid', $categoryid);
116 }
117 redirect($url);
118}
119
b488058f
SH
120// If the user poses any of these capabilities then they will be able to see the admin
121// tree and the management link within it.
122// This is the most accurate form of navigation.
123$capabilities = array(
124 'moodle/site:config',
125 'moodle/backup:backupcourse',
126 'moodle/category:manage',
127 'moodle/course:create',
128 'moodle/site:approvecourse'
129);
130if ($category && !has_any_capability($capabilities, $systemcontext)) {
484c4c6c
SH
131 // If the user doesn't poses any of these system capabilities then we're going to mark the manage link in the settings block
132 // as active, tell the page to ignore the active path and just build what the user would expect.
b488058f 133 // This will at least give the page some relevant navigation.
b488058f 134 navigation_node::override_active_url(new moodle_url('/course/management.php', array('categoryid' => $category->id)));
484c4c6c
SH
135 $PAGE->set_category_by_id($category->id);
136 $PAGE->navbar->ignore_active(true);
137 $PAGE->navbar->add(get_string('coursemgmt', 'admin'), $PAGE->url->out_omit_querystring());
9fcd0290
MG
138} else {
139 // If user has system capabilities, make sure the "Manage courses and categories" item in Administration block is active.
140 navigation_node::require_admin_tree();
141 navigation_node::override_active_url(new moodle_url('/course/management.php'));
484c4c6c
SH
142}
143if (!$issearching && $category !== null) {
5dc361e1 144 $parents = coursecat::get_many($category->get_parents());
484c4c6c 145 $parents[] = $category;
5dc361e1 146 foreach ($parents as $parent) {
484c4c6c
SH
147 $PAGE->navbar->add(
148 $parent->get_formatted_name(),
149 new moodle_url('/course/management.php', array('categoryid' => $parent->id))
150 );
5dc361e1 151 }
5dc361e1
SH
152 if ($course instanceof course_in_list) {
153 // Use the list name so that it matches whats being displayed below.
154 $PAGE->navbar->add($course->get_formatted_name());
155 }
156}
157
5dc361e1
SH
158$notificationspass = array();
159$notificationsfail = array();
160
161if ($action !== false && confirm_sesskey()) {
162 // Actions:
163 // - resortcategories : Resort the courses in the given category.
164 // - resortcourses : Resort courses
165 // - showcourse : make a course visible.
166 // - hidecourse : make a course hidden.
167 // - movecourseup : move the selected course up one.
168 // - movecoursedown : move the selected course down.
169 // - showcategory : make a category visible.
170 // - hidecategory : make a category hidden.
171 // - movecategoryup : move category up.
172 // - movecategorydown : move category down.
173 // - deletecategory : delete the category either in full, or moving contents.
174 // - bulkaction : performs bulk actions:
175 // - bulkmovecourses.
176 // - bulkmovecategories.
177 // - bulkresortcategories.
178 $redirectback = false;
38a15200 179 $redirectmessage = false;
5dc361e1
SH
180 switch ($action) {
181 case 'resortcategories' :
182 $sort = required_param('resort', PARAM_ALPHA);
b488058f
SH
183 $cattosort = coursecat::get((int)optional_param('categoryid', 0, PARAM_INT));
184 $redirectback = \core_course\management\helper::action_category_resort_subcategories($cattosort, $sort);
5dc361e1
SH
185 break;
186 case 'resortcourses' :
187 // They must have specified a category.
188 required_param('categoryid', PARAM_INT);
189 $sort = required_param('resort', PARAM_ALPHA);
190 \core_course\management\helper::action_category_resort_courses($category, $sort);
191 break;
192 case 'showcourse' :
193 $redirectback = \core_course\management\helper::action_course_show($course);
194 break;
195 case 'hidecourse' :
196 $redirectback = \core_course\management\helper::action_course_hide($course);
197 break;
198 case 'movecourseup' :
199 // They must have specified a category and a course.
200 required_param('categoryid', PARAM_INT);
201 required_param('courseid', PARAM_INT);
5aff38e4 202 $redirectback = \core_course\management\helper::action_course_change_sortorder_up_one($course, $category);
5dc361e1
SH
203 break;
204 case 'movecoursedown' :
205 // They must have specified a category and a course.
206 required_param('categoryid', PARAM_INT);
207 required_param('courseid', PARAM_INT);
5aff38e4 208 $redirectback = \core_course\management\helper::action_course_change_sortorder_down_one($course, $category);
5dc361e1
SH
209 break;
210 case 'showcategory' :
211 // They must have specified a category.
212 required_param('categoryid', PARAM_INT);
213 $redirectback = \core_course\management\helper::action_category_show($category);
214 break;
215 case 'hidecategory' :
216 // They must have specified a category.
217 required_param('categoryid', PARAM_INT);
218 $redirectback = \core_course\management\helper::action_category_hide($category);
219 break;
220 case 'movecategoryup' :
221 // They must have specified a category.
222 required_param('categoryid', PARAM_INT);
5aff38e4 223 $redirectback = \core_course\management\helper::action_category_change_sortorder_up_one($category);
5dc361e1
SH
224 break;
225 case 'movecategorydown' :
226 // They must have specified a category.
227 required_param('categoryid', PARAM_INT);
5aff38e4 228 $redirectback = \core_course\management\helper::action_category_change_sortorder_down_one($category);
5dc361e1
SH
229 break;
230 case 'deletecategory':
231 // They must have specified a category.
232 required_param('categoryid', PARAM_INT);
233 if (!$category->can_delete()) {
234 throw new moodle_exception('permissiondenied', 'error', '', null, 'coursecat::can_resort');
235 }
67e1f268 236 $mform = new core_course_deletecategory_form(null, $category);
5dc361e1 237 if ($mform->is_cancelled()) {
484c4c6c 238 redirect($PAGE->url);
5dc361e1
SH
239 }
240 // Start output.
241 /* @var core_course_management_renderer|core_renderer $renderer */
242 $renderer = $PAGE->get_renderer('core_course', 'management');
243 echo $renderer->header();
244 echo $renderer->heading(get_string('deletecategory', 'moodle', $category->get_formatted_name()));
245
246 if ($data = $mform->get_data()) {
247 // The form has been submit handle it.
248 if ($data->fulldelete == 1 && $category->can_delete_full()) {
8aa3aa3d
SH
249 $continueurl = new moodle_url('/course/management.php');
250 if ($category->parent != '0') {
251 $continueurl->param('categoryid', $category->parent);
252 }
5dc361e1
SH
253 $notification = get_string('coursecategorydeleted', '', $category->get_formatted_name());
254 $deletedcourses = $category->delete_full(true);
255 foreach ($deletedcourses as $course) {
256 echo $renderer->notification(get_string('coursedeleted', '', $course->shortname), 'notifysuccess');
257 }
258 echo $renderer->notification($notification, 'notifysuccess');
259 echo $renderer->continue_button($continueurl);
260 } else if ($data->fulldelete == 0 && $category->can_move_content_to($data->newparent)) {
261 $continueurl = new moodle_url('/course/management.php', array('categoryid' => $data->newparent));
262 $category->delete_move($data->newparent, true);
263 echo $renderer->continue_button($continueurl);
264 } else {
265 // Some error in parameters (user is cheating?)
266 $mform->display();
267 }
268 } else {
269 // Display the form.
270 $mform->display();
271 }
272 // Finish output and exit.
273 echo $renderer->footer();
274 exit();
275 break;
276 case 'bulkaction':
277 $bulkmovecourses = optional_param('bulkmovecourses', false, PARAM_BOOL);
278 $bulkmovecategories = optional_param('bulkmovecategories', false, PARAM_BOOL);
c7a2291f 279 $bulkresortcategories = optional_param('bulksort', false, PARAM_BOOL);
5dc361e1
SH
280
281 if ($bulkmovecourses) {
282 // Move courses out of the current category and into a new category.
283 // They must have specified a category.
284 required_param('categoryid', PARAM_INT);
285 $movetoid = required_param('movecoursesto', PARAM_INT);
286 $courseids = optional_param_array('bc', false, PARAM_INT);
287 if ($courseids === false) {
288 break;
289 }
290 $moveto = coursecat::get($movetoid);
67e1f268
SH
291 try {
292 // If this fails we want to catch the exception and report it.
293 $redirectback = \core_course\management\helper::action_category_move_courses_into($category, $moveto,
294 $courseids);
38a15200
SH
295 if ($redirectback) {
296 $a = new stdClass;
297 $a->category = $moveto->get_formatted_name();
298 $a->courses = count($courseids);
299 $redirectmessage = get_string('bulkmovecoursessuccess', 'moodle', $a);
300 }
67e1f268
SH
301 } catch (moodle_exception $ex) {
302 $redirectback = false;
303 $notificationsfail[] = $ex->getMessage();
304 }
5dc361e1 305 } else if ($bulkmovecategories) {
d61c05ac 306 $categoryids = optional_param_array('bcat', array(), PARAM_INT);
5dc361e1
SH
307 $movetocatid = required_param('movecategoriesto', PARAM_INT);
308 $movetocat = coursecat::get($movetocatid);
309 $movecount = 0;
310 foreach ($categoryids as $id) {
311 $cattomove = coursecat::get($id);
312 if ($id == $movetocatid) {
313 $notificationsfail[] = get_string('movecategoryownparent', 'error', $cattomove->get_formatted_name());
314 continue;
315 }
316 if (strpos($movetocat->path, $cattomove->path) === 0) {
317 $notificationsfail[] = get_string('movecategoryparentconflict', 'error', $cattomove->get_formatted_name());
318 continue;
319 }
320 if ($cattomove->parent != $movetocatid) {
321 if ($cattomove->can_change_parent($movetocatid)) {
322 $cattomove->change_parent($movetocatid);
323 $movecount++;
324 } else {
325 $notificationsfail[] = get_string('movecategorynotpossible', 'error', $cattomove->get_formatted_name());
326 }
327 }
328 }
329 if ($movecount > 1) {
330 $a = new stdClass;
331 $a->count = $movecount;
332 $a->to = $movetocat->get_formatted_name();
3b732cd6
RT
333 $movesuccessstrkey = 'movecategoriessuccess';
334 if ($movetocatid == 0) {
335 $movesuccessstrkey = 'movecategoriestotopsuccess';
336 }
337 $notificationspass[] = get_string($movesuccessstrkey, 'moodle', $a);
5dc361e1
SH
338 } else if ($movecount === 1) {
339 $a = new stdClass;
340 $a->moved = $cattomove->get_formatted_name();
341 $a->to = $movetocat->get_formatted_name();
3b732cd6
RT
342 $movesuccessstrkey = 'movecategorysuccess';
343 if ($movetocatid == 0) {
344 $movesuccessstrkey = 'movecategorytotopsuccess';
345 }
346 $notificationspass[] = get_string($movesuccessstrkey, 'moodle', $a);
5dc361e1
SH
347 }
348 } else if ($bulkresortcategories) {
c7a2291f
SH
349 $for = required_param('selectsortby', PARAM_ALPHA);
350 $sortcategoriesby = required_param('resortcategoriesby', PARAM_ALPHA);
351 $sortcoursesby = required_param('resortcoursesby', PARAM_ALPHA);
352
353 if ($sortcategoriesby === 'none' && $sortcoursesby === 'none') {
354 // They're not sorting anything.
355 break;
356 }
38a15200
SH
357 if (!in_array($sortcategoriesby, array('idnumber', 'name'))) {
358 $sortcategoriesby = false;
359 }
360 if (!in_array($sortcoursesby, array('idnumber', 'fullname', 'shortname'))) {
361 $sortcoursesby = false;
362 }
c7a2291f
SH
363
364 if ($for === 'thiscategory') {
365 $categoryids = array(
366 required_param('currentcategoryid', PARAM_INT)
367 );
368 $categories = coursecat::get_many($categoryids);
369 } else if ($for === 'selectedcategories') {
370 // Bulk resort selected categories.
371 $categoryids = optional_param_array('bcat', false, PARAM_INT);
372 $sort = required_param('resortcategoriesby', PARAM_ALPHA);
373 if ($categoryids === false) {
374 break;
375 }
376 $categories = coursecat::get_many($categoryids);
377 } else if ($for === 'allcategories') {
c820e700 378 if ($sortcategoriesby && coursecat::get(0)->can_resort_subcategories()) {
38a15200
SH
379 \core_course\management\helper::action_category_resort_subcategories(coursecat::get(0), $sortcategoriesby);
380 }
381 $categorieslist = coursecat::make_categories_list('moodle/category:manage');
382 $categoryids = array_keys($categorieslist);
383 $categories = coursecat::get_many($categoryids);
384 unset($categorieslist);
c7a2291f 385 } else {
5dc361e1
SH
386 break;
387 }
5dc361e1 388 foreach ($categories as $cat) {
c820e700 389 if ($sortcategoriesby && $cat->can_resort_subcategories()) {
c7a2291f
SH
390 // Don't clean up here, we'll do it once we're all done.
391 \core_course\management\helper::action_category_resort_subcategories($cat, $sortcategoriesby, false);
392 }
c820e700 393 if ($sortcoursesby && $cat->can_resort_courses()) {
c7a2291f
SH
394 \core_course\management\helper::action_category_resort_courses($cat, $sortcoursesby, false);
395 }
5dc361e1 396 }
c7a2291f 397 coursecat::resort_categories_cleanup($sortcoursesby !== false);
b488058f
SH
398 if ($category === null && count($categoryids) === 1) {
399 // They're bulk sorting just a single category and they've not selected a category.
400 // Lets for convenience sake auto-select the category that has been resorted for them.
401 redirect(new moodle_url($PAGE->url, array('categoryid' => reset($categoryids))));
402 }
5dc361e1
SH
403 }
404 }
405 if ($redirectback) {
38a15200
SH
406 if ($redirectmessage) {
407 redirect($PAGE->url, $redirectmessage, 5);
408 } else {
409 redirect($PAGE->url);
410 }
5dc361e1
SH
411 }
412}
413
414if (!is_null($perpage)) {
415 set_user_preference('coursecat_management_perpage', $perpage);
416} else {
417 $perpage = get_user_preferences('coursecat_management_perpage', $CFG->coursesperpage);
418}
419if ((int)$perpage != $perpage || $perpage < 2) {
420 $perpage = $CFG->coursesperpage;
421}
422
423$categorysize = 4;
424$coursesize = 4;
425$detailssize = 4;
426if ($viewmode === 'default' || $viewmode === 'combined') {
427 if (isset($courseid)) {
428 $class = 'columns-3';
429 } else {
430 $categorysize = 5;
431 $coursesize = 7;
432 $class = 'columns-2';
433 }
434} else if ($viewmode === 'categories') {
435 $categorysize = 12;
436 $class = 'columns-1';
437} else if ($viewmode === 'courses') {
438 if (isset($courseid)) {
439 $coursesize = 6;
440 $detailssize = 6;
441 $class = 'columns-2';
442 } else {
443 $coursesize = 12;
444 $class = 'columns-1';
445 }
446}
484c4c6c
SH
447if ($viewmode === 'default' || $viewmode === 'combined') {
448 $class .= ' viewmode-cobmined';
449} else {
450 $class .= ' viewmode-'.$viewmode;
451}
452if (($viewmode === 'default' || $viewmode === 'combined' || $viewmode === 'courses') && !empty($courseid)) {
453 $class .= ' course-selected';
454}
5dc361e1
SH
455
456/* @var core_course_management_renderer|core_renderer $renderer */
457$renderer = $PAGE->get_renderer('core_course', 'management');
458$renderer->enhance_management_interface();
459
d0647301
SH
460$displaycategorylisting = ($viewmode === 'default' || $viewmode === 'combined' || $viewmode === 'categories');
461$displaycourselisting = ($viewmode === 'default' || $viewmode === 'combined' || $viewmode === 'courses');
462$displaycoursedetail = (isset($courseid));
463
5dc361e1
SH
464echo $renderer->header();
465
466if (!$issearching) {
62846237 467 echo $renderer->management_heading($strmanagement, $viewmode, $categoryid);
5dc361e1
SH
468} else {
469 echo $renderer->management_heading(new lang_string('searchresults'));
470}
471
472if (count($notificationspass) > 0) {
473 echo $renderer->notification(join('<br />', $notificationspass), 'notifysuccess');
474}
475if (count($notificationsfail) > 0) {
476 echo $renderer->notification(join('<br />', $notificationsfail));
477}
478
479// Start the management form.
480echo $renderer->management_form_start();
481
d0647301
SH
482echo $renderer->accessible_skipto_links($displaycategorylisting, $displaycourselisting, $displaycoursedetail);
483
a1451599
SH
484echo $renderer->grid_start('course-category-listings', $class);
485
d0647301 486if ($displaycategorylisting) {
5dc361e1
SH
487 echo $renderer->grid_column_start($categorysize, 'category-listing');
488 echo $renderer->category_listing($category);
489 echo $renderer->grid_column_end();
490}
d0647301 491if ($displaycourselisting) {
5dc361e1
SH
492 echo $renderer->grid_column_start($coursesize, 'course-listing');
493 if (!$issearching) {
494 echo $renderer->course_listing($category, $course, $page, $perpage);
495 } else {
496 list($courses, $coursescount, $coursestotal) =
497 \core_course\management\helper::search_courses($search, $blocklist, $modulelist, $page, $perpage);
498 echo $renderer->search_listing($courses, $coursestotal, $course, $page, $perpage);
499 }
500 echo $renderer->grid_column_end();
d0647301 501 if ($displaycoursedetail) {
5dc361e1
SH
502 echo $renderer->grid_column_start($detailssize, 'course-detail');
503 echo $renderer->course_detail($course);
504 echo $renderer->grid_column_end();
505 }
506}
507echo $renderer->grid_end();
508
5dc361e1
SH
509// End of the management form.
510echo $renderer->management_form_end();
d61c05ac 511echo $renderer->footer();