Merge branch '42619-26' 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
PS
59 $url->param('categoryid', $categoryid);
60 navigation_node::override_active_url($url);
5dc361e1 61 $url->param('courseid', $course->id);
d5814f4e 62
5dc361e1
SH
63} else if ($categoryid) {
64 $courseid = null;
65 $course = null;
66 $category = coursecat::get($categoryid);
b488058f 67 $context = context_coursecat::instance($category->id);
5dc361e1 68 $url->param('categoryid', $category->id);
d5814f4e
PS
69 navigation_node::override_active_url($url);
70
5dc361e1
SH
71} else {
72 $course = null;
73 $courseid = null;
74 $category = null;
75 $categoryid = null;
76 if ($viewmode === 'default') {
77 $viewmode = 'categories';
78 }
b488058f 79 $context = $systemcontext;
d5814f4e 80 navigation_node::override_active_url($url);
5dc361e1 81}
484c4c6c 82
cda49969
SH
83// Check if there is a selected category param, and if there is apply it.
84if ($course === null && $selectedcategoryid !== null && $selectedcategoryid !== $categoryid) {
85 $url->param('categoryid', $selectedcategoryid);
86}
87
5dc361e1
SH
88if ($page !== 0) {
89 $url->param('page', $page);
90}
91if ($viewmode !== 'default') {
92 $url->param('view', $viewmode);
93}
94if ($search !== '') {
95 $url->param('search', $search);
96}
97if ($blocklist !== 0) {
98 $url->param('blocklist', $search);
99}
100if ($modulelist !== '') {
101 $url->param('modulelist', $search);
102}
103
b488058f 104$title = format_string($SITE->fullname, true, array('context' => $systemcontext));
5dc361e1
SH
105
106$PAGE->set_context($context);
107$PAGE->set_url($url);
108$PAGE->set_pagelayout('admin');
109$PAGE->set_title($title);
110$PAGE->set_heading($title);
111
962885e8
SH
112// This is a system level page that operates on other contexts.
113require_login();
114
484c4c6c
SH
115if (!coursecat::has_capability_on_any(array('moodle/category:manage', 'moodle/course:create'))) {
116 // The user isn't able to manage any categories. Lets redirect them to the relevant course/index.php page.
117 $url = new moodle_url('/course/index.php');
118 if ($categoryid) {
119 $url->param('categoryid', $categoryid);
120 }
121 redirect($url);
122}
123
b488058f
SH
124// If the user poses any of these capabilities then they will be able to see the admin
125// tree and the management link within it.
126// This is the most accurate form of navigation.
127$capabilities = array(
128 'moodle/site:config',
129 'moodle/backup:backupcourse',
130 'moodle/category:manage',
131 'moodle/course:create',
132 'moodle/site:approvecourse'
133);
134if ($category && !has_any_capability($capabilities, $systemcontext)) {
484c4c6c
SH
135 // If the user doesn't poses any of these system capabilities then we're going to mark the manage link in the settings block
136 // as active, tell the page to ignore the active path and just build what the user would expect.
b488058f 137 // This will at least give the page some relevant navigation.
b488058f 138 navigation_node::override_active_url(new moodle_url('/course/management.php', array('categoryid' => $category->id)));
484c4c6c
SH
139 $PAGE->set_category_by_id($category->id);
140 $PAGE->navbar->ignore_active(true);
141 $PAGE->navbar->add(get_string('coursemgmt', 'admin'), $PAGE->url->out_omit_querystring());
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;
179 switch ($action) {
180 case 'resortcategories' :
181 $sort = required_param('resort', PARAM_ALPHA);
b488058f
SH
182 $cattosort = coursecat::get((int)optional_param('categoryid', 0, PARAM_INT));
183 $redirectback = \core_course\management\helper::action_category_resort_subcategories($cattosort, $sort);
5dc361e1
SH
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);
5aff38e4 201 $redirectback = \core_course\management\helper::action_course_change_sortorder_up_one($course, $category);
5dc361e1
SH
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);
5aff38e4 207 $redirectback = \core_course\management\helper::action_course_change_sortorder_down_one($course, $category);
5dc361e1
SH
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);
5aff38e4 222 $redirectback = \core_course\management\helper::action_category_change_sortorder_up_one($category);
5dc361e1
SH
223 break;
224 case 'movecategorydown' :
225 // They must have specified a category.
226 required_param('categoryid', PARAM_INT);
5aff38e4 227 $redirectback = \core_course\management\helper::action_category_change_sortorder_down_one($category);
5dc361e1
SH
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, 'coursecat::can_resort');
234 }
235 require_once($CFG->dirroot.'/course/delete_category_form.php');
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);
279 $bulkresortcategories = optional_param('bulkresortcategories', false, PARAM_BOOL);
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);
295 } catch (moodle_exception $ex) {
296 $redirectback = false;
297 $notificationsfail[] = $ex->getMessage();
298 }
5dc361e1 299 } else if ($bulkmovecategories) {
d61c05ac 300 $categoryids = optional_param_array('bcat', array(), PARAM_INT);
5dc361e1
SH
301 $movetocatid = required_param('movecategoriesto', PARAM_INT);
302 $movetocat = coursecat::get($movetocatid);
303 $movecount = 0;
304 foreach ($categoryids as $id) {
305 $cattomove = coursecat::get($id);
306 if ($id == $movetocatid) {
307 $notificationsfail[] = get_string('movecategoryownparent', 'error', $cattomove->get_formatted_name());
308 continue;
309 }
310 if (strpos($movetocat->path, $cattomove->path) === 0) {
311 $notificationsfail[] = get_string('movecategoryparentconflict', 'error', $cattomove->get_formatted_name());
312 continue;
313 }
314 if ($cattomove->parent != $movetocatid) {
315 if ($cattomove->can_change_parent($movetocatid)) {
316 $cattomove->change_parent($movetocatid);
317 $movecount++;
318 } else {
319 $notificationsfail[] = get_string('movecategorynotpossible', 'error', $cattomove->get_formatted_name());
320 }
321 }
322 }
323 if ($movecount > 1) {
324 $a = new stdClass;
325 $a->count = $movecount;
326 $a->to = $movetocat->get_formatted_name();
327 $notificationspass[] = get_string('movecategoriessuccess', 'moodle', $a);
328 } else if ($movecount === 1) {
329 $a = new stdClass;
330 $a->moved = $cattomove->get_formatted_name();
331 $a->to = $movetocat->get_formatted_name();
332 $notificationspass[] = get_string('movecategorysuccess', 'moodle', $a);
333 }
334 } else if ($bulkresortcategories) {
335 // Bulk resort selected categories.
336 $categoryids = optional_param_array('bcat', false, PARAM_INT);
337 $sort = required_param('resortcategoriesby', PARAM_ALPHA);
338 if ($categoryids === false) {
339 break;
340 }
341 $categories = coursecat::get_many($categoryids);
342 foreach ($categories as $cat) {
343 // Don't clean up here, we'll do it once we're all done.
344 \core_course\management\helper::action_category_resort_subcategories($cat, $sort, false);
345 }
346 coursecat::resort_categories_cleanup();
b488058f
SH
347 if ($category === null && count($categoryids) === 1) {
348 // They're bulk sorting just a single category and they've not selected a category.
349 // Lets for convenience sake auto-select the category that has been resorted for them.
350 redirect(new moodle_url($PAGE->url, array('categoryid' => reset($categoryids))));
351 }
5dc361e1
SH
352 }
353 }
354 if ($redirectback) {
355 redirect($PAGE->url);
356 }
357}
358
359if (!is_null($perpage)) {
360 set_user_preference('coursecat_management_perpage', $perpage);
361} else {
362 $perpage = get_user_preferences('coursecat_management_perpage', $CFG->coursesperpage);
363}
364if ((int)$perpage != $perpage || $perpage < 2) {
365 $perpage = $CFG->coursesperpage;
366}
367
368$categorysize = 4;
369$coursesize = 4;
370$detailssize = 4;
371if ($viewmode === 'default' || $viewmode === 'combined') {
372 if (isset($courseid)) {
373 $class = 'columns-3';
374 } else {
375 $categorysize = 5;
376 $coursesize = 7;
377 $class = 'columns-2';
378 }
379} else if ($viewmode === 'categories') {
380 $categorysize = 12;
381 $class = 'columns-1';
382} else if ($viewmode === 'courses') {
383 if (isset($courseid)) {
384 $coursesize = 6;
385 $detailssize = 6;
386 $class = 'columns-2';
387 } else {
388 $coursesize = 12;
389 $class = 'columns-1';
390 }
391}
484c4c6c
SH
392if ($viewmode === 'default' || $viewmode === 'combined') {
393 $class .= ' viewmode-cobmined';
394} else {
395 $class .= ' viewmode-'.$viewmode;
396}
397if (($viewmode === 'default' || $viewmode === 'combined' || $viewmode === 'courses') && !empty($courseid)) {
398 $class .= ' course-selected';
399}
5dc361e1
SH
400
401/* @var core_course_management_renderer|core_renderer $renderer */
402$renderer = $PAGE->get_renderer('core_course', 'management');
403$renderer->enhance_management_interface();
404
405echo $renderer->header();
406
407if (!$issearching) {
408 echo $renderer->management_heading(new lang_string('coursecatmanagement'), $viewmode, $categoryid);
409} else {
410 echo $renderer->management_heading(new lang_string('searchresults'));
411}
412
413if (count($notificationspass) > 0) {
414 echo $renderer->notification(join('<br />', $notificationspass), 'notifysuccess');
415}
416if (count($notificationsfail) > 0) {
417 echo $renderer->notification(join('<br />', $notificationsfail));
418}
419
420// Start the management form.
421echo $renderer->management_form_start();
422
423echo $renderer->grid_start('course-category-listings', $class);
424if ($viewmode === 'default' || $viewmode === 'combined' || $viewmode === 'categories') {
425 echo $renderer->grid_column_start($categorysize, 'category-listing');
426 echo $renderer->category_listing($category);
427 echo $renderer->grid_column_end();
428}
429if ($viewmode === 'default' || $viewmode === 'combined' || $viewmode === 'courses') {
430 echo $renderer->grid_column_start($coursesize, 'course-listing');
431 if (!$issearching) {
432 echo $renderer->course_listing($category, $course, $page, $perpage);
433 } else {
434 list($courses, $coursescount, $coursestotal) =
435 \core_course\management\helper::search_courses($search, $blocklist, $modulelist, $page, $perpage);
436 echo $renderer->search_listing($courses, $coursestotal, $course, $page, $perpage);
437 }
438 echo $renderer->grid_column_end();
439 if (isset($courseid)) {
440 echo $renderer->grid_column_start($detailssize, 'course-detail');
441 echo $renderer->course_detail($course);
442 echo $renderer->grid_column_end();
443 }
444}
445echo $renderer->grid_end();
446
5dc361e1
SH
447// End of the management form.
448echo $renderer->management_form_end();
d61c05ac 449echo $renderer->footer();