Merge branch 'MDL-62144-master' of git://github.com/damyon/moodle
[moodle.git] / course / management.php
blobe104ac016395661d989e5ed33e244d43a8cedcec
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 $category = core_course_category::get_default();
72 $categoryid = $category->id;
73 $context = context_coursecat::instance($category->id);
74 $url->param('categoryid', $category->id);
77 // Check if there is a selected category param, and if there is apply it.
78 if ($course === null && $selectedcategoryid !== null && $selectedcategoryid !== $categoryid) {
79 $url->param('categoryid', $selectedcategoryid);
82 if ($page !== 0) {
83 $url->param('page', $page);
85 if ($viewmode !== 'default') {
86 $url->param('view', $viewmode);
88 if ($search !== '') {
89 $url->param('search', $search);
91 if ($blocklist !== 0) {
92 $url->param('blocklist', $search);
94 if ($modulelist !== '') {
95 $url->param('modulelist', $search);
98 $strmanagement = new lang_string('coursecatmanagement');
99 $pageheading = format_string($SITE->fullname, true, array('context' => $systemcontext));
101 $PAGE->set_context($context);
102 $PAGE->set_url($url);
103 $PAGE->set_pagelayout('admin');
104 $PAGE->set_title($strmanagement);
105 $PAGE->set_heading($pageheading);
107 // This is a system level page that operates on other contexts.
108 require_login();
110 if (!core_course_category::has_capability_on_any(array('moodle/category:manage', 'moodle/course:create'))) {
111 // The user isn't able to manage any categories. Lets redirect them to the relevant course/index.php page.
112 $url = new moodle_url('/course/index.php');
113 if ($categoryid) {
114 $url->param('categoryid', $categoryid);
116 redirect($url);
119 // If the user poses any of these capabilities then they will be able to see the admin
120 // tree and the management link within it.
121 // This is the most accurate form of navigation.
122 $capabilities = array(
123 'moodle/site:config',
124 'moodle/backup:backupcourse',
125 'moodle/category:manage',
126 'moodle/course:create',
127 'moodle/site:approvecourse'
129 if ($category && !has_any_capability($capabilities, $systemcontext)) {
130 // If the user doesn't poses any of these system capabilities then we're going to mark the manage link in the settings block
131 // as active, tell the page to ignore the active path and just build what the user would expect.
132 // This will at least give the page some relevant navigation.
133 navigation_node::override_active_url(new moodle_url('/course/management.php', array('categoryid' => $category->id)));
134 $PAGE->set_category_by_id($category->id);
135 $PAGE->navbar->ignore_active(true);
136 $PAGE->navbar->add(get_string('coursemgmt', 'admin'), $PAGE->url->out_omit_querystring());
137 } else {
138 // If user has system capabilities, make sure the "Manage courses and categories" item in Administration block is active.
139 navigation_node::require_admin_tree();
140 navigation_node::override_active_url(new moodle_url('/course/management.php'));
142 if (!$issearching && $category !== null) {
143 $parents = core_course_category::get_many($category->get_parents());
144 $parents[] = $category;
145 foreach ($parents as $parent) {
146 $PAGE->navbar->add(
147 $parent->get_formatted_name(),
148 new moodle_url('/course/management.php', array('categoryid' => $parent->id))
151 if ($course instanceof core_course_list_element) {
152 // Use the list name so that it matches whats being displayed below.
153 $PAGE->navbar->add($course->get_formatted_name());
157 $notificationspass = array();
158 $notificationsfail = array();
160 if ($action !== false && confirm_sesskey()) {
161 // Actions:
162 // - resortcategories : Resort the courses in the given category.
163 // - resortcourses : Resort courses
164 // - showcourse : make a course visible.
165 // - hidecourse : make a course hidden.
166 // - movecourseup : move the selected course up one.
167 // - movecoursedown : move the selected course down.
168 // - showcategory : make a category visible.
169 // - hidecategory : make a category hidden.
170 // - movecategoryup : move category up.
171 // - movecategorydown : move category down.
172 // - deletecategory : delete the category either in full, or moving contents.
173 // - bulkaction : performs bulk actions:
174 // - bulkmovecourses.
175 // - bulkmovecategories.
176 // - bulkresortcategories.
177 $redirectback = false;
178 $redirectmessage = false;
179 switch ($action) {
180 case 'resortcategories' :
181 $sort = required_param('resort', PARAM_ALPHA);
182 $cattosort = core_course_category::get((int)optional_param('categoryid', 0, PARAM_INT));
183 $redirectback = \core_course\management\helper::action_category_resort_subcategories($cattosort, $sort);
184 break;
185 case 'resortcourses' :
186 // They must have specified a category.
187 required_param('categoryid', PARAM_INT);
188 $sort = required_param('resort', PARAM_ALPHA);
189 \core_course\management\helper::action_category_resort_courses($category, $sort);
190 break;
191 case 'showcourse' :
192 $redirectback = \core_course\management\helper::action_course_show($course);
193 break;
194 case 'hidecourse' :
195 $redirectback = \core_course\management\helper::action_course_hide($course);
196 break;
197 case 'movecourseup' :
198 // They must have specified a category and a course.
199 required_param('categoryid', PARAM_INT);
200 required_param('courseid', PARAM_INT);
201 $redirectback = \core_course\management\helper::action_course_change_sortorder_up_one($course, $category);
202 break;
203 case 'movecoursedown' :
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_down_one($course, $category);
208 break;
209 case 'showcategory' :
210 // They must have specified a category.
211 required_param('categoryid', PARAM_INT);
212 $redirectback = \core_course\management\helper::action_category_show($category);
213 break;
214 case 'hidecategory' :
215 // They must have specified a category.
216 required_param('categoryid', PARAM_INT);
217 $redirectback = \core_course\management\helper::action_category_hide($category);
218 break;
219 case 'movecategoryup' :
220 // They must have specified a category.
221 required_param('categoryid', PARAM_INT);
222 $redirectback = \core_course\management\helper::action_category_change_sortorder_up_one($category);
223 break;
224 case 'movecategorydown' :
225 // They must have specified a category.
226 required_param('categoryid', PARAM_INT);
227 $redirectback = \core_course\management\helper::action_category_change_sortorder_down_one($category);
228 break;
229 case 'deletecategory':
230 // They must have specified a category.
231 required_param('categoryid', PARAM_INT);
232 if (!$category->can_delete()) {
233 throw new moodle_exception('permissiondenied', 'error', '', null, 'core_course_category::can_resort');
235 $mform = new core_course_deletecategory_form(null, $category);
236 if ($mform->is_cancelled()) {
237 redirect($PAGE->url);
239 // Start output.
240 /* @var core_course_management_renderer|core_renderer $renderer */
241 $renderer = $PAGE->get_renderer('core_course', 'management');
242 echo $renderer->header();
243 echo $renderer->heading(get_string('deletecategory', 'moodle', $category->get_formatted_name()));
245 if ($data = $mform->get_data()) {
246 // The form has been submit handle it.
247 if ($data->fulldelete == 1 && $category->can_delete_full()) {
248 $continueurl = new moodle_url('/course/management.php');
249 if ($category->parent != '0') {
250 $continueurl->param('categoryid', $category->parent);
252 $notification = get_string('coursecategorydeleted', '', $category->get_formatted_name());
253 $deletedcourses = $category->delete_full(true);
254 foreach ($deletedcourses as $course) {
255 echo $renderer->notification(get_string('coursedeleted', '', $course->shortname), 'notifysuccess');
257 echo $renderer->notification($notification, 'notifysuccess');
258 echo $renderer->continue_button($continueurl);
259 } else if ($data->fulldelete == 0 && $category->can_move_content_to($data->newparent)) {
260 $continueurl = new moodle_url('/course/management.php', array('categoryid' => $data->newparent));
261 $category->delete_move($data->newparent, true);
262 echo $renderer->continue_button($continueurl);
263 } else {
264 // Some error in parameters (user is cheating?)
265 $mform->display();
267 } else {
268 // Display the form.
269 $mform->display();
271 // Finish output and exit.
272 echo $renderer->footer();
273 exit();
274 break;
275 case 'bulkaction':
276 $bulkmovecourses = optional_param('bulkmovecourses', false, PARAM_BOOL);
277 $bulkmovecategories = optional_param('bulkmovecategories', false, PARAM_BOOL);
278 $bulkresortcategories = optional_param('bulksort', false, PARAM_BOOL);
280 if ($bulkmovecourses) {
281 // Move courses out of the current category and into a new category.
282 // They must have specified a category.
283 required_param('categoryid', PARAM_INT);
284 $movetoid = required_param('movecoursesto', PARAM_INT);
285 $courseids = optional_param_array('bc', false, PARAM_INT);
286 if ($courseids === false) {
287 break;
289 $moveto = core_course_category::get($movetoid);
290 try {
291 // If this fails we want to catch the exception and report it.
292 $redirectback = \core_course\management\helper::move_courses_into_category($moveto,
293 $courseids);
294 if ($redirectback) {
295 $a = new stdClass;
296 $a->category = $moveto->get_formatted_name();
297 $a->courses = count($courseids);
298 $redirectmessage = get_string('bulkmovecoursessuccess', 'moodle', $a);
300 } catch (moodle_exception $ex) {
301 $redirectback = false;
302 $notificationsfail[] = $ex->getMessage();
304 } else if ($bulkmovecategories) {
305 $categoryids = optional_param_array('bcat', array(), PARAM_INT);
306 $movetocatid = required_param('movecategoriesto', PARAM_INT);
307 $movetocat = core_course_category::get($movetocatid);
308 $movecount = 0;
309 foreach ($categoryids as $id) {
310 $cattomove = core_course_category::get($id);
311 if ($id == $movetocatid) {
312 $notificationsfail[] = get_string('movecategoryownparent', 'error', $cattomove->get_formatted_name());
313 continue;
315 if (strpos($movetocat->path, $cattomove->path) === 0) {
316 $notificationsfail[] = get_string('movecategoryparentconflict', 'error', $cattomove->get_formatted_name());
317 continue;
319 if ($cattomove->parent != $movetocatid) {
320 if ($cattomove->can_change_parent($movetocatid)) {
321 $cattomove->change_parent($movetocatid);
322 $movecount++;
323 } else {
324 $notificationsfail[] = get_string('movecategorynotpossible', 'error', $cattomove->get_formatted_name());
328 if ($movecount > 1) {
329 $a = new stdClass;
330 $a->count = $movecount;
331 $a->to = $movetocat->get_formatted_name();
332 $movesuccessstrkey = 'movecategoriessuccess';
333 if ($movetocatid == 0) {
334 $movesuccessstrkey = 'movecategoriestotopsuccess';
336 $notificationspass[] = get_string($movesuccessstrkey, 'moodle', $a);
337 } else if ($movecount === 1) {
338 $a = new stdClass;
339 $a->moved = $cattomove->get_formatted_name();
340 $a->to = $movetocat->get_formatted_name();
341 $movesuccessstrkey = 'movecategorysuccess';
342 if ($movetocatid == 0) {
343 $movesuccessstrkey = 'movecategorytotopsuccess';
345 $notificationspass[] = get_string($movesuccessstrkey, 'moodle', $a);
347 } else if ($bulkresortcategories) {
348 $for = required_param('selectsortby', PARAM_ALPHA);
349 $sortcategoriesby = required_param('resortcategoriesby', PARAM_ALPHA);
350 $sortcoursesby = required_param('resortcoursesby', PARAM_ALPHA);
352 if ($sortcategoriesby === 'none' && $sortcoursesby === 'none') {
353 // They're not sorting anything.
354 break;
356 if (!in_array($sortcategoriesby, array('idnumber', 'idnumberdesc',
357 'name', 'namedesc'))) {
358 $sortcategoriesby = false;
360 if (!in_array($sortcoursesby, array('timecreated', 'timecreateddesc',
361 'idnumber', 'idnumberdesc',
362 'fullname', 'fullnamedesc',
363 'shortname', 'shortnamedesc'))) {
364 $sortcoursesby = false;
367 if ($for === 'thiscategory') {
368 $categoryids = array(
369 required_param('currentcategoryid', PARAM_INT)
371 $categories = core_course_category::get_many($categoryids);
372 } else if ($for === 'selectedcategories') {
373 // Bulk resort selected categories.
374 $categoryids = optional_param_array('bcat', false, PARAM_INT);
375 $sort = required_param('resortcategoriesby', PARAM_ALPHA);
376 if ($categoryids === false) {
377 break;
379 $categories = core_course_category::get_many($categoryids);
380 } else if ($for === 'allcategories') {
381 if ($sortcategoriesby && core_course_category::get(0)->can_resort_subcategories()) {
382 \core_course\management\helper::action_category_resort_subcategories(
383 core_course_category::get(0), $sortcategoriesby);
385 $categorieslist = core_course_category::make_categories_list('moodle/category:manage');
386 $categoryids = array_keys($categorieslist);
387 $categories = core_course_category::get_many($categoryids);
388 unset($categorieslist);
389 } else {
390 break;
392 foreach ($categories as $cat) {
393 if ($sortcategoriesby && $cat->can_resort_subcategories()) {
394 // Don't clean up here, we'll do it once we're all done.
395 \core_course\management\helper::action_category_resort_subcategories($cat, $sortcategoriesby, false);
397 if ($sortcoursesby && $cat->can_resort_courses()) {
398 \core_course\management\helper::action_category_resort_courses($cat, $sortcoursesby, false);
401 core_course_category::resort_categories_cleanup($sortcoursesby !== false);
402 if ($category === null && count($categoryids) === 1) {
403 // They're bulk sorting just a single category and they've not selected a category.
404 // Lets for convenience sake auto-select the category that has been resorted for them.
405 redirect(new moodle_url($PAGE->url, array('categoryid' => reset($categoryids))));
409 if ($redirectback) {
410 if ($redirectmessage) {
411 redirect($PAGE->url, $redirectmessage, 5);
412 } else {
413 redirect($PAGE->url);
418 if (!is_null($perpage)) {
419 set_user_preference('coursecat_management_perpage', $perpage);
420 } else {
421 $perpage = get_user_preferences('coursecat_management_perpage', $CFG->coursesperpage);
423 if ((int)$perpage != $perpage || $perpage < 2) {
424 $perpage = $CFG->coursesperpage;
427 $categorysize = 4;
428 $coursesize = 4;
429 $detailssize = 4;
430 if ($viewmode === 'default' || $viewmode === 'combined') {
431 if (isset($courseid)) {
432 $class = 'columns-3';
433 } else {
434 $categorysize = 5;
435 $coursesize = 7;
436 $class = 'columns-2';
438 } else if ($viewmode === 'categories') {
439 $categorysize = 12;
440 $class = 'columns-1';
441 } else if ($viewmode === 'courses') {
442 if (isset($courseid)) {
443 $coursesize = 6;
444 $detailssize = 6;
445 $class = 'columns-2';
446 } else {
447 $coursesize = 12;
448 $class = 'columns-1';
451 if ($viewmode === 'default' || $viewmode === 'combined') {
452 $class .= ' viewmode-cobmined';
453 } else {
454 $class .= ' viewmode-'.$viewmode;
456 if (($viewmode === 'default' || $viewmode === 'combined' || $viewmode === 'courses') && !empty($courseid)) {
457 $class .= ' course-selected';
460 /* @var core_course_management_renderer|core_renderer $renderer */
461 $renderer = $PAGE->get_renderer('core_course', 'management');
462 $renderer->enhance_management_interface();
464 $displaycategorylisting = ($viewmode === 'default' || $viewmode === 'combined' || $viewmode === 'categories');
465 $displaycourselisting = ($viewmode === 'default' || $viewmode === 'combined' || $viewmode === 'courses');
466 $displaycoursedetail = (isset($courseid));
468 echo $renderer->header();
470 if (!$issearching) {
471 echo $renderer->management_heading($strmanagement, $viewmode, $categoryid);
472 } else {
473 echo $renderer->management_heading(new lang_string('searchresults'));
476 if (count($notificationspass) > 0) {
477 echo $renderer->notification(join('<br />', $notificationspass), 'notifysuccess');
479 if (count($notificationsfail) > 0) {
480 echo $renderer->notification(join('<br />', $notificationsfail));
483 // Start the management form.
484 echo $renderer->management_form_start();
486 echo $renderer->accessible_skipto_links($displaycategorylisting, $displaycourselisting, $displaycoursedetail);
488 echo $renderer->grid_start('course-category-listings', $class);
490 if ($displaycategorylisting) {
491 echo $renderer->grid_column_start($categorysize, 'category-listing');
492 echo $renderer->category_listing($category);
493 echo $renderer->grid_column_end();
495 if ($displaycourselisting) {
496 echo $renderer->grid_column_start($coursesize, 'course-listing');
497 if (!$issearching) {
498 echo $renderer->course_listing($category, $course, $page, $perpage, $viewmode);
499 } else {
500 list($courses, $coursescount, $coursestotal) =
501 \core_course\management\helper::search_courses($search, $blocklist, $modulelist, $page, $perpage);
502 echo $renderer->search_listing($courses, $coursestotal, $course, $page, $perpage, $search);
504 echo $renderer->grid_column_end();
505 if ($displaycoursedetail) {
506 echo $renderer->grid_column_start($detailssize, 'course-detail');
507 echo $renderer->course_detail($course);
508 echo $renderer->grid_column_end();
511 echo $renderer->grid_end();
513 // End of the management form.
514 echo $renderer->management_form_end();
515 echo $renderer->course_search_form($search);
517 echo $renderer->footer();