Merge branch 'MDL-62144-master' of git://github.com/damyon/moodle
[moodle.git] / course / rest.php
blobe7d5b2e83b31b90c84ae0be708abc564810a5da5
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Provide interface for topics AJAX course formats
21 * @copyright 1999 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @package course
26 if (!defined('AJAX_SCRIPT')) {
27 define('AJAX_SCRIPT', true);
29 require_once(__DIR__ . '/../config.php');
30 require_once($CFG->dirroot.'/course/lib.php');
32 // Initialise ALL the incoming parameters here, up front.
33 $courseid = required_param('courseId', PARAM_INT);
34 $class = required_param('class', PARAM_ALPHA);
35 $field = optional_param('field', '', PARAM_ALPHA);
36 $sectionid = optional_param('sectionId', 0, PARAM_INT);
37 $beforeid = optional_param('beforeId', 0, PARAM_INT);
38 $value = optional_param('value', 0, PARAM_INT);
39 $id = optional_param('id', 0, PARAM_INT);
41 $PAGE->set_url('/course/rest.php', array('courseId'=>$courseid,'class'=>$class));
43 //NOTE: when making any changes here please make sure it is using the same access control as course/mod.php !!
45 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
46 // Check user is logged in and set contexts if we are dealing with resource
47 if (in_array($class, array('resource'))) {
48 $cm = get_coursemodule_from_id(null, $id, $course->id, false, MUST_EXIST);
49 require_login($course, false, $cm);
50 $modcontext = context_module::instance($cm->id);
51 } else {
52 require_login($course);
54 $coursecontext = context_course::instance($course->id);
55 require_sesskey();
57 echo $OUTPUT->header(); // send headers
59 if ($class === 'section' && $field === 'move') {
60 if (!$DB->record_exists('course_sections', array('course' => $course->id, 'section' => $id))) {
61 throw new moodle_exception('AJAX commands.php: Bad Section ID ' . $id);
64 require_capability('moodle/course:movesections', $coursecontext);
65 move_section_to($course, $id, $value);
66 // See if format wants to do something about it.
67 $response = course_get_format($course)->ajax_section_move();
68 if ($response !== null) {
69 echo json_encode($response);
72 } else if ($class === 'resource' && $field === 'move') {
74 require_capability('moodle/course:manageactivities', $modcontext);
75 if (!$section = $DB->get_record('course_sections', array('course' => $course->id, 'section' => $sectionid))) {
76 throw new moodle_exception('AJAX commands.php: Bad section ID '.$sectionid);
79 if ($beforeid > 0) {
80 $beforemod = get_coursemodule_from_id('', $beforeid, $course->id);
81 $beforemod = $DB->get_record('course_modules', array('id' => $beforeid));
82 } else {
83 $beforemod = null;
86 $isvisible = moveto_module($cm, $section, $beforemod);
87 echo json_encode(array('visible' => (bool) $isvisible));