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