MDL-31830 course: added behat test of category creation
[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);
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.
35
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.
40
41$issearching = ($search !== '' || $blocklist !== 0 || $modulelist !== '');
42if ($issearching) {
43 $viewmode = 'courses';
44}
45
46$url = new moodle_url('/course/management.php');
47navigation_node::override_active_url($url);
48if ($courseid) {
49 $record = get_course($courseid);
50 $course = new course_in_list($record);
51 $category = coursecat::get($course->category);
52 $categoryid = $category->id;
53 $url->param('courseid', $course->id);
54} else if ($categoryid) {
55 $courseid = null;
56 $course = null;
57 $category = coursecat::get($categoryid);
58 $url->param('categoryid', $category->id);
59} else {
60 $course = null;
61 $courseid = null;
62 $category = null;
63 $categoryid = null;
64 if ($viewmode === 'default') {
65 $viewmode = 'categories';
66 }
67}
68if ($page !== 0) {
69 $url->param('page', $page);
70}
71if ($viewmode !== 'default') {
72 $url->param('view', $viewmode);
73}
74if ($search !== '') {
75 $url->param('search', $search);
76}
77if ($blocklist !== 0) {
78 $url->param('blocklist', $search);
79}
80if ($modulelist !== '') {
81 $url->param('modulelist', $search);
82}
83
84$context = context_system::instance();
85$title = format_string($SITE->fullname, true, array('context' => $context));
86
87$PAGE->set_context($context);
88$PAGE->set_url($url);
89$PAGE->set_pagelayout('admin');
90$PAGE->set_title($title);
91$PAGE->set_heading($title);
92
93if (!$issearching && $category !== null) {
94 $parents = coursecat::get_many($category->get_parents());
95 foreach ($parents as $parent) {
96 $PAGE->navbar->add($parent->get_formatted_name());
97 }
98 $PAGE->navbar->add($category->get_formatted_name());
99 if ($course instanceof course_in_list) {
100 // Use the list name so that it matches whats being displayed below.
101 $PAGE->navbar->add($course->get_formatted_name());
102 }
103}
104
105require_login();
106
107$notificationspass = array();
108$notificationsfail = array();
109
110if ($action !== false && confirm_sesskey()) {
111 // Actions:
112 // - resortcategories : Resort the courses in the given category.
113 // - resortcourses : Resort courses
114 // - showcourse : make a course visible.
115 // - hidecourse : make a course hidden.
116 // - movecourseup : move the selected course up one.
117 // - movecoursedown : move the selected course down.
118 // - showcategory : make a category visible.
119 // - hidecategory : make a category hidden.
120 // - movecategoryup : move category up.
121 // - movecategorydown : move category down.
122 // - deletecategory : delete the category either in full, or moving contents.
123 // - bulkaction : performs bulk actions:
124 // - bulkmovecourses.
125 // - bulkmovecategories.
126 // - bulkresortcategories.
127 $redirectback = false;
128 switch ($action) {
129 case 'resortcategories' :
130 $sort = required_param('resort', PARAM_ALPHA);
131 \core_course\management\helper::action_category_resort_subcategories(coursecat::get(0), $sort);
132 break;
133 case 'resortcourses' :
134 // They must have specified a category.
135 required_param('categoryid', PARAM_INT);
136 $sort = required_param('resort', PARAM_ALPHA);
137 \core_course\management\helper::action_category_resort_courses($category, $sort);
138 break;
139 case 'showcourse' :
140 $redirectback = \core_course\management\helper::action_course_show($course);
141 break;
142 case 'hidecourse' :
143 $redirectback = \core_course\management\helper::action_course_hide($course);
144 break;
145 case 'movecourseup' :
146 // They must have specified a category and a course.
147 required_param('categoryid', PARAM_INT);
148 required_param('courseid', PARAM_INT);
149 $redirectback = \core_course\management\helper::action_course_moveup($course, $category);
150 break;
151 case 'movecoursedown' :
152 // They must have specified a category and a course.
153 required_param('categoryid', PARAM_INT);
154 required_param('courseid', PARAM_INT);
155 $redirectback = \core_course\management\helper::action_course_movedown($course, $category);
156 break;
157 case 'showcategory' :
158 // They must have specified a category.
159 required_param('categoryid', PARAM_INT);
160 $redirectback = \core_course\management\helper::action_category_show($category);
161 break;
162 case 'hidecategory' :
163 // They must have specified a category.
164 required_param('categoryid', PARAM_INT);
165 $redirectback = \core_course\management\helper::action_category_hide($category);
166 break;
167 case 'movecategoryup' :
168 // They must have specified a category.
169 required_param('categoryid', PARAM_INT);
170 $redirectback = \core_course\management\helper::action_category_moveup($category);
171 break;
172 case 'movecategorydown' :
173 // They must have specified a category.
174 required_param('categoryid', PARAM_INT);
175 $redirectback = \core_course\management\helper::action_category_movedown($category);
176 break;
177 case 'deletecategory':
178 // They must have specified a category.
179 required_param('categoryid', PARAM_INT);
180 if (!$category->can_delete()) {
181 throw new moodle_exception('permissiondenied', 'error', '', null, 'coursecat::can_resort');
182 }
183 require_once($CFG->dirroot.'/course/delete_category_form.php');
184 $mform = new delete_category_form(null, $category);
185 if ($mform->is_cancelled()) {
186 redirect(new moodle_url('/course/management.php'));
187 }
188 // Start output.
189 /* @var core_course_management_renderer|core_renderer $renderer */
190 $renderer = $PAGE->get_renderer('core_course', 'management');
191 echo $renderer->header();
192 echo $renderer->heading(get_string('deletecategory', 'moodle', $category->get_formatted_name()));
193
194 if ($data = $mform->get_data()) {
195 // The form has been submit handle it.
196 if ($data->fulldelete == 1 && $category->can_delete_full()) {
197 $continueurl = new moodle_url('/course/management.php', array('categoryid' => $category->parent));
198 $notification = get_string('coursecategorydeleted', '', $category->get_formatted_name());
199 $deletedcourses = $category->delete_full(true);
200 foreach ($deletedcourses as $course) {
201 echo $renderer->notification(get_string('coursedeleted', '', $course->shortname), 'notifysuccess');
202 }
203 echo $renderer->notification($notification, 'notifysuccess');
204 echo $renderer->continue_button($continueurl);
205 } else if ($data->fulldelete == 0 && $category->can_move_content_to($data->newparent)) {
206 $continueurl = new moodle_url('/course/management.php', array('categoryid' => $data->newparent));
207 $category->delete_move($data->newparent, true);
208 echo $renderer->continue_button($continueurl);
209 } else {
210 // Some error in parameters (user is cheating?)
211 $mform->display();
212 }
213 } else {
214 // Display the form.
215 $mform->display();
216 }
217 // Finish output and exit.
218 echo $renderer->footer();
219 exit();
220 break;
221 case 'bulkaction':
222 $bulkmovecourses = optional_param('bulkmovecourses', false, PARAM_BOOL);
223 $bulkmovecategories = optional_param('bulkmovecategories', false, PARAM_BOOL);
224 $bulkresortcategories = optional_param('bulkresortcategories', false, PARAM_BOOL);
225
226 if ($bulkmovecourses) {
227 // Move courses out of the current category and into a new category.
228 // They must have specified a category.
229 required_param('categoryid', PARAM_INT);
230 $movetoid = required_param('movecoursesto', PARAM_INT);
231 $courseids = optional_param_array('bc', false, PARAM_INT);
232 if ($courseids === false) {
233 break;
234 }
235 $moveto = coursecat::get($movetoid);
236 $redirectback = \core_course\management\helper::action_category_move_courses_into($category, $moveto, $courseids);
237 } else if ($bulkmovecategories) {
238 $categoryids = optional_param_array('bcat', false, PARAM_INT);
239 $movetocatid = required_param('movecategoriesto', PARAM_INT);
240 $movetocat = coursecat::get($movetocatid);
241 $movecount = 0;
242 foreach ($categoryids as $id) {
243 $cattomove = coursecat::get($id);
244 if ($id == $movetocatid) {
245 $notificationsfail[] = get_string('movecategoryownparent', 'error', $cattomove->get_formatted_name());
246 continue;
247 }
248 if (strpos($movetocat->path, $cattomove->path) === 0) {
249 $notificationsfail[] = get_string('movecategoryparentconflict', 'error', $cattomove->get_formatted_name());
250 continue;
251 }
252 if ($cattomove->parent != $movetocatid) {
253 if ($cattomove->can_change_parent($movetocatid)) {
254 $cattomove->change_parent($movetocatid);
255 $movecount++;
256 } else {
257 $notificationsfail[] = get_string('movecategorynotpossible', 'error', $cattomove->get_formatted_name());
258 }
259 }
260 }
261 if ($movecount > 1) {
262 $a = new stdClass;
263 $a->count = $movecount;
264 $a->to = $movetocat->get_formatted_name();
265 $notificationspass[] = get_string('movecategoriessuccess', 'moodle', $a);
266 } else if ($movecount === 1) {
267 $a = new stdClass;
268 $a->moved = $cattomove->get_formatted_name();
269 $a->to = $movetocat->get_formatted_name();
270 $notificationspass[] = get_string('movecategorysuccess', 'moodle', $a);
271 }
272 } else if ($bulkresortcategories) {
273 // Bulk resort selected categories.
274 $categoryids = optional_param_array('bcat', false, PARAM_INT);
275 $sort = required_param('resortcategoriesby', PARAM_ALPHA);
276 if ($categoryids === false) {
277 break;
278 }
279 $categories = coursecat::get_many($categoryids);
280 foreach ($categories as $cat) {
281 // Don't clean up here, we'll do it once we're all done.
282 \core_course\management\helper::action_category_resort_subcategories($cat, $sort, false);
283 }
284 coursecat::resort_categories_cleanup();
285 }
286 }
287 if ($redirectback) {
288 redirect($PAGE->url);
289 }
290}
291
292if (!is_null($perpage)) {
293 set_user_preference('coursecat_management_perpage', $perpage);
294} else {
295 $perpage = get_user_preferences('coursecat_management_perpage', $CFG->coursesperpage);
296}
297if ((int)$perpage != $perpage || $perpage < 2) {
298 $perpage = $CFG->coursesperpage;
299}
300
301$categorysize = 4;
302$coursesize = 4;
303$detailssize = 4;
304if ($viewmode === 'default' || $viewmode === 'combined') {
305 if (isset($courseid)) {
306 $class = 'columns-3';
307 } else {
308 $categorysize = 5;
309 $coursesize = 7;
310 $class = 'columns-2';
311 }
312} else if ($viewmode === 'categories') {
313 $categorysize = 12;
314 $class = 'columns-1';
315} else if ($viewmode === 'courses') {
316 if (isset($courseid)) {
317 $coursesize = 6;
318 $detailssize = 6;
319 $class = 'columns-2';
320 } else {
321 $coursesize = 12;
322 $class = 'columns-1';
323 }
324}
325
326/* @var core_course_management_renderer|core_renderer $renderer */
327$renderer = $PAGE->get_renderer('core_course', 'management');
328$renderer->enhance_management_interface();
329
330echo $renderer->header();
331
332if (!$issearching) {
333 echo $renderer->management_heading(new lang_string('coursecatmanagement'), $viewmode, $categoryid);
334} else {
335 echo $renderer->management_heading(new lang_string('searchresults'));
336}
337
338if (count($notificationspass) > 0) {
339 echo $renderer->notification(join('<br />', $notificationspass), 'notifysuccess');
340}
341if (count($notificationsfail) > 0) {
342 echo $renderer->notification(join('<br />', $notificationsfail));
343}
344
345// Start the management form.
346echo $renderer->management_form_start();
347
348echo $renderer->grid_start('course-category-listings', $class);
349if ($viewmode === 'default' || $viewmode === 'combined' || $viewmode === 'categories') {
350 echo $renderer->grid_column_start($categorysize, 'category-listing');
351 echo $renderer->category_listing($category);
352 echo $renderer->grid_column_end();
353}
354if ($viewmode === 'default' || $viewmode === 'combined' || $viewmode === 'courses') {
355 echo $renderer->grid_column_start($coursesize, 'course-listing');
356 if (!$issearching) {
357 echo $renderer->course_listing($category, $course, $page, $perpage);
358 } else {
359 list($courses, $coursescount, $coursestotal) =
360 \core_course\management\helper::search_courses($search, $blocklist, $modulelist, $page, $perpage);
361 echo $renderer->search_listing($courses, $coursestotal, $course, $page, $perpage);
362 }
363 echo $renderer->grid_column_end();
364 if (isset($courseid)) {
365 echo $renderer->grid_column_start($detailssize, 'course-detail');
366 echo $renderer->course_detail($course);
367 echo $renderer->grid_column_end();
368 }
369}
370echo $renderer->grid_end();
371
372if (!empty($CFG->enablecourserequests) && $id == $CFG->defaultrequestcategory) {
373 print_course_request_buttons(context_system::instance());
374}
375
376// End of the management form.
377echo $renderer->management_form_end();
378echo $renderer->footer();