MDL-47919 tool_monitor: adjustments to behat tests
[moodle.git] / blocks / course_overview / move.php
blob2b5e74d60e1c063cceed9330f1011b48a197c752
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 * Move/order course functionality for course_overview block.
20 * @package block_course_overview
21 * @copyright 2012 Adam Olley <adam.olley@netspot.com.au>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 require_once(dirname(__FILE__) . '/../../config.php');
25 require_once(dirname(__FILE__) . '/locallib.php');
27 require_sesskey();
28 require_login();
30 $coursetomove = required_param('courseid', PARAM_INT);
31 $moveto = required_param('moveto', PARAM_INT);
33 list($courses, $sitecourses, $coursecount) = block_course_overview_get_sorted_courses();
34 $sortedcourses = array_keys($courses);
36 $currentcourseindex = array_search($coursetomove, $sortedcourses);
37 // If coursetomove is not found or moveto < 0 or > count($sortedcourses) then throw error.
38 if ($currentcourseindex === false) {
39 print_error("invalidcourseid", null, null, $coursetomove);
40 } else if (($moveto < 0) || ($moveto >= count($sortedcourses))) {
41 print_error("invalidaction");
44 // If current course index is same as destination index then don't do anything.
45 if ($currentcourseindex === $moveto) {
46 redirect(new moodle_url('/my/index.php'));
49 // Create neworder list for courses.
50 $neworder = array();
52 unset($sortedcourses[$currentcourseindex]);
53 $neworder = array_slice($sortedcourses, 0, $moveto, true);
54 $neworder[] = $coursetomove;
55 $remaningcourses = array_slice($sortedcourses, $moveto);
56 foreach ($remaningcourses as $courseid) {
57 $neworder[] = $courseid;
59 block_course_overview_update_myorder(array_values($neworder));
60 redirect(new moodle_url('/my/index.php'));