MDL-42609 course: management default missing bits
[moodle.git] / course / management.php
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/>.
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  */
25 require_once('../config.php');
26 require_once($CFG->dirroot.'/lib/coursecatlib.php');
27 require_once($CFG->dirroot.'/course/lib.php');
29 $categoryid = optional_param('categoryid', null, PARAM_INT);
30 $selectedcategoryid = optional_param('selectedcategoryid', null, PARAM_INT);
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.
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.
42 if (!in_array($viewmode, array('default', 'combined', 'courses', 'categories'))) {
43     $viewmode = 'default';
44 }
46 $issearching = ($search !== '' || $blocklist !== 0 || $modulelist !== '');
47 if ($issearching) {
48     $viewmode = 'courses';
49 }
51 $url = new moodle_url('/course/management.php');
52 $systemcontext = $context = context_system::instance();
53 if ($courseid) {
54     $record = get_course($courseid);
55     $course = new course_in_list($record);
56     $category = coursecat::get($course->category);
57     $categoryid = $category->id;
58     $context = context_coursecat::instance($category->id);
59     $url->param('categoryid', $categoryid);
60     $url->param('courseid', $course->id);
62 } else if ($categoryid) {
63     $courseid = null;
64     $course = null;
65     $category = coursecat::get($categoryid);
66     $context = context_coursecat::instance($category->id);
67     $url->param('categoryid', $category->id);
69 } else {
70     $course = null;
71     $courseid = null;
72     $category = coursecat::get_default();
73     $categoryid = $category->id;
74     $context = context_coursecat::instance($category->id);
75     $url->param('categoryid', $category->id);
76 }
78 // Check if there is a selected category param, and if there is apply it.
79 if ($course === null && $selectedcategoryid !== null && $selectedcategoryid !== $categoryid) {
80     $url->param('categoryid', $selectedcategoryid);
81 }
83 if ($page !== 0) {
84     $url->param('page', $page);
85 }
86 if ($viewmode !== 'default') {
87     $url->param('view', $viewmode);
88 }
89 if ($search !== '') {
90     $url->param('search', $search);
91 }
92 if ($blocklist !== 0) {
93     $url->param('blocklist', $search);
94 }
95 if ($modulelist !== '') {
96     $url->param('modulelist', $search);
97 }
99 $strmanagement = new lang_string('coursecatmanagement');
100 $pageheading = format_string($SITE->fullname, true, array('context' => $systemcontext));
102 $PAGE->set_context($context);
103 $PAGE->set_url($url);
104 $PAGE->set_pagelayout('admin');
105 $PAGE->set_title($strmanagement);
106 $PAGE->set_heading($pageheading);
108 // This is a system level page that operates on other contexts.
109 require_login();
111 if (!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);
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 );
130 if ($category && !has_any_capability($capabilities, $systemcontext)) {
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.
133     // This will at least give the page some relevant navigation.
134     navigation_node::override_active_url(new moodle_url('/course/management.php', array('categoryid' => $category->id)));
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());
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'));
143 if (!$issearching && $category !== null) {
144     $parents = coursecat::get_many($category->get_parents());
145     $parents[] = $category;
146     foreach ($parents as $parent) {
147         $PAGE->navbar->add(
148             $parent->get_formatted_name(),
149             new moodle_url('/course/management.php', array('categoryid' => $parent->id))
150         );
151     }
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     }
158 $notificationspass = array();
159 $notificationsfail = array();
161 if ($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;
179     $redirectmessage = false;
180     switch ($action) {
181         case 'resortcategories' :
182             $sort = required_param('resort', PARAM_ALPHA);
183             $cattosort = coursecat::get((int)optional_param('categoryid', 0, PARAM_INT));
184             $redirectback = \core_course\management\helper::action_category_resort_subcategories($cattosort, $sort);
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);
202             $redirectback = \core_course\management\helper::action_course_change_sortorder_up_one($course, $category);
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);
208             $redirectback = \core_course\management\helper::action_course_change_sortorder_down_one($course, $category);
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);
223             $redirectback = \core_course\management\helper::action_category_change_sortorder_up_one($category);
224             break;
225         case 'movecategorydown' :
226             // They must have specified a category.
227             required_param('categoryid', PARAM_INT);
228             $redirectback = \core_course\management\helper::action_category_change_sortorder_down_one($category);
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             }
236             require_once($CFG->dirroot.'/course/delete_category_form.php');
237             $mform = new core_course_deletecategory_form(null, $category);
238             if ($mform->is_cancelled()) {
239                 redirect($PAGE->url);
240             }
241             // Start output.
242             /* @var core_course_management_renderer|core_renderer $renderer */
243             $renderer = $PAGE->get_renderer('core_course', 'management');
244             echo $renderer->header();
245             echo $renderer->heading(get_string('deletecategory', 'moodle', $category->get_formatted_name()));
247             if ($data = $mform->get_data()) {
248                 // The form has been submit handle it.
249                 if ($data->fulldelete == 1 && $category->can_delete_full()) {
250                     $continueurl = new moodle_url('/course/management.php');
251                     if ($category->parent != '0') {
252                         $continueurl->param('categoryid', $category->parent);
253                     }
254                     $notification = get_string('coursecategorydeleted', '', $category->get_formatted_name());
255                     $deletedcourses = $category->delete_full(true);
256                     foreach ($deletedcourses as $course) {
257                         echo $renderer->notification(get_string('coursedeleted', '', $course->shortname), 'notifysuccess');
258                     }
259                     echo $renderer->notification($notification, 'notifysuccess');
260                     echo $renderer->continue_button($continueurl);
261                 } else if ($data->fulldelete == 0 && $category->can_move_content_to($data->newparent)) {
262                     $continueurl = new moodle_url('/course/management.php', array('categoryid' => $data->newparent));
263                     $category->delete_move($data->newparent, true);
264                     echo $renderer->continue_button($continueurl);
265                 } else {
266                     // Some error in parameters (user is cheating?)
267                     $mform->display();
268                 }
269             } else {
270                 // Display the form.
271                 $mform->display();
272             }
273             // Finish output and exit.
274             echo $renderer->footer();
275             exit();
276             break;
277         case 'bulkaction':
278             $bulkmovecourses = optional_param('bulkmovecourses', false, PARAM_BOOL);
279             $bulkmovecategories = optional_param('bulkmovecategories', false, PARAM_BOOL);
280             $bulkresortcategories = optional_param('bulksort', false, PARAM_BOOL);
282             if ($bulkmovecourses) {
283                 // Move courses out of the current category and into a new category.
284                 // They must have specified a category.
285                 required_param('categoryid', PARAM_INT);
286                 $movetoid = required_param('movecoursesto', PARAM_INT);
287                 $courseids = optional_param_array('bc', false, PARAM_INT);
288                 if ($courseids === false) {
289                     break;
290                 }
291                 $moveto = coursecat::get($movetoid);
292                 try {
293                     // If this fails we want to catch the exception and report it.
294                     $redirectback = \core_course\management\helper::action_category_move_courses_into($category, $moveto,
295                         $courseids);
296                     if ($redirectback) {
297                         $a = new stdClass;
298                         $a->category = $moveto->get_formatted_name();
299                         $a->courses = count($courseids);
300                         $redirectmessage = get_string('bulkmovecoursessuccess', 'moodle', $a);
301                     }
302                 } catch (moodle_exception $ex) {
303                     $redirectback = false;
304                     $notificationsfail[] = $ex->getMessage();
305                 }
306             } else if ($bulkmovecategories) {
307                 $categoryids = optional_param_array('bcat', array(), PARAM_INT);
308                 $movetocatid = required_param('movecategoriesto', PARAM_INT);
309                 $movetocat = coursecat::get($movetocatid);
310                 $movecount = 0;
311                 foreach ($categoryids as $id) {
312                     $cattomove = coursecat::get($id);
313                     if ($id == $movetocatid) {
314                         $notificationsfail[] = get_string('movecategoryownparent', 'error', $cattomove->get_formatted_name());
315                         continue;
316                     }
317                     if (strpos($movetocat->path, $cattomove->path) === 0) {
318                         $notificationsfail[] = get_string('movecategoryparentconflict', 'error', $cattomove->get_formatted_name());
319                         continue;
320                     }
321                     if ($cattomove->parent != $movetocatid) {
322                         if ($cattomove->can_change_parent($movetocatid)) {
323                             $cattomove->change_parent($movetocatid);
324                             $movecount++;
325                         } else {
326                             $notificationsfail[] = get_string('movecategorynotpossible', 'error', $cattomove->get_formatted_name());
327                         }
328                     }
329                 }
330                 if ($movecount > 1) {
331                     $a = new stdClass;
332                     $a->count = $movecount;
333                     $a->to = $movetocat->get_formatted_name();
334                     $movesuccessstrkey = 'movecategoriessuccess';
335                     if ($movetocatid == 0) {
336                         $movesuccessstrkey = 'movecategoriestotopsuccess';
337                     }
338                     $notificationspass[] = get_string($movesuccessstrkey, 'moodle', $a);
339                 } else if ($movecount === 1) {
340                     $a = new stdClass;
341                     $a->moved = $cattomove->get_formatted_name();
342                     $a->to = $movetocat->get_formatted_name();
343                     $movesuccessstrkey = 'movecategorysuccess';
344                     if ($movetocatid == 0) {
345                         $movesuccessstrkey = 'movecategorytotopsuccess';
346                     }
347                     $notificationspass[] = get_string($movesuccessstrkey, 'moodle', $a);
348                 }
349             } else if ($bulkresortcategories) {
350                 $for = required_param('selectsortby', PARAM_ALPHA);
351                 $sortcategoriesby = required_param('resortcategoriesby', PARAM_ALPHA);
352                 $sortcoursesby = required_param('resortcoursesby', PARAM_ALPHA);
354                 if ($sortcategoriesby === 'none' && $sortcoursesby === 'none') {
355                     // They're not sorting anything.
356                     break;
357                 }
358                 if (!in_array($sortcategoriesby, array('idnumber', 'name'))) {
359                     $sortcategoriesby = false;
360                 }
361                 if (!in_array($sortcoursesby, array('idnumber', 'fullname', 'shortname'))) {
362                     $sortcoursesby = false;
363                 }
365                 if ($for === 'thiscategory') {
366                     $categoryids = array(
367                         required_param('currentcategoryid', PARAM_INT)
368                     );
369                     $categories = coursecat::get_many($categoryids);
370                 } else if ($for === 'selectedcategories') {
371                     // Bulk resort selected categories.
372                     $categoryids = optional_param_array('bcat', false, PARAM_INT);
373                     $sort = required_param('resortcategoriesby', PARAM_ALPHA);
374                     if ($categoryids === false) {
375                         break;
376                     }
377                     $categories = coursecat::get_many($categoryids);
378                 } else if ($for === 'allcategories') {
379                     if ($sortcategoriesby && coursecat::get(0)->can_resort_subcategories()) {
380                         \core_course\management\helper::action_category_resort_subcategories(coursecat::get(0), $sortcategoriesby);
381                     }
382                     $categorieslist = coursecat::make_categories_list('moodle/category:manage');
383                     $categoryids = array_keys($categorieslist);
384                     $categories = coursecat::get_many($categoryids);
385                     unset($categorieslist);
386                 } else {
387                     break;
388                 }
389                 foreach ($categories as $cat) {
390                     if ($sortcategoriesby && $cat->can_resort_subcategories()) {
391                         // Don't clean up here, we'll do it once we're all done.
392                         \core_course\management\helper::action_category_resort_subcategories($cat, $sortcategoriesby, false);
393                     }
394                     if ($sortcoursesby && $cat->can_resort_courses()) {
395                         \core_course\management\helper::action_category_resort_courses($cat, $sortcoursesby, false);
396                     }
397                 }
398                 coursecat::resort_categories_cleanup($sortcoursesby !== false);
399                 if ($category === null && count($categoryids) === 1) {
400                     // They're bulk sorting just a single category and they've not selected a category.
401                     // Lets for convenience sake auto-select the category that has been resorted for them.
402                     redirect(new moodle_url($PAGE->url, array('categoryid' => reset($categoryids))));
403                 }
404             }
405     }
406     if ($redirectback) {
407         if ($redirectmessage) {
408             redirect($PAGE->url, $redirectmessage, 5);
409         } else {
410             redirect($PAGE->url);
411         }
412     }
415 if (!is_null($perpage)) {
416     set_user_preference('coursecat_management_perpage', $perpage);
417 } else {
418     $perpage = get_user_preferences('coursecat_management_perpage', $CFG->coursesperpage);
420 if ((int)$perpage != $perpage || $perpage < 2) {
421     $perpage = $CFG->coursesperpage;
424 $categorysize = 4;
425 $coursesize = 4;
426 $detailssize = 4;
427 if ($viewmode === 'default' || $viewmode === 'combined') {
428     if (isset($courseid)) {
429         $class = 'columns-3';
430     } else {
431         $categorysize = 5;
432         $coursesize = 7;
433         $class = 'columns-2';
434     }
435 } else if ($viewmode === 'categories') {
436     $categorysize = 12;
437     $class = 'columns-1';
438 } else if ($viewmode === 'courses') {
439     if (isset($courseid)) {
440         $coursesize = 6;
441         $detailssize = 6;
442         $class = 'columns-2';
443     } else {
444         $coursesize = 12;
445         $class = 'columns-1';
446     }
448 if ($viewmode === 'default' || $viewmode === 'combined') {
449     $class .= ' viewmode-cobmined';
450 } else {
451     $class .= ' viewmode-'.$viewmode;
453 if (($viewmode === 'default' || $viewmode === 'combined' || $viewmode === 'courses') && !empty($courseid)) {
454     $class .= ' course-selected';
457 /* @var core_course_management_renderer|core_renderer $renderer */
458 $renderer = $PAGE->get_renderer('core_course', 'management');
459 $renderer->enhance_management_interface();
461 $displaycategorylisting = ($viewmode === 'default' || $viewmode === 'combined' || $viewmode === 'categories');
462 $displaycourselisting = ($viewmode === 'default' || $viewmode === 'combined' || $viewmode === 'courses');
463 $displaycoursedetail = (isset($courseid));
465 echo $renderer->header();
467 if (!$issearching) {
468     echo $renderer->management_heading($strmanagement, $viewmode, $categoryid);
469 } else {
470     echo $renderer->management_heading(new lang_string('searchresults'));
473 if (count($notificationspass) > 0) {
474     echo $renderer->notification(join('<br />', $notificationspass), 'notifysuccess');
476 if (count($notificationsfail) > 0) {
477     echo $renderer->notification(join('<br />', $notificationsfail));
480 // Start the management form.
481 echo $renderer->management_form_start();
483 echo $renderer->accessible_skipto_links($displaycategorylisting, $displaycourselisting, $displaycoursedetail);
485 echo $renderer->grid_start('course-category-listings', $class);
487 if ($displaycategorylisting) {
488     echo $renderer->grid_column_start($categorysize, 'category-listing');
489     echo $renderer->category_listing($category);
490     echo $renderer->grid_column_end();
492 if ($displaycourselisting) {
493     echo $renderer->grid_column_start($coursesize, 'course-listing');
494     if (!$issearching) {
495         echo $renderer->course_listing($category, $course, $page, $perpage);
496     } else {
497         list($courses, $coursescount, $coursestotal) =
498             \core_course\management\helper::search_courses($search, $blocklist, $modulelist, $page, $perpage);
499         echo $renderer->search_listing($courses, $coursestotal, $course, $page, $perpage);
500     }
501     echo $renderer->grid_column_end();
502     if ($displaycoursedetail) {
503         echo $renderer->grid_column_start($detailssize, 'course-detail');
504         echo $renderer->course_detail($course);
505         echo $renderer->grid_column_end();
506     }
508 echo $renderer->grid_end();
510 // End of the management form.
511 echo $renderer->management_form_end();
512 echo $renderer->footer();