MDL-67264 core_course: Activity chooser new feature
[moodle.git] / course / rest.php
CommitLineData
d9cb06dc 1<?php
2
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.
14//
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/>.
17
18/**
19 * Provide interface for topics AJAX course formats
20 *
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
24 */
b2054038 25
ff92b711
RK
26if (!defined('AJAX_SCRIPT')) {
27 define('AJAX_SCRIPT', true);
28}
1fcf0ca8 29require_once(__DIR__ . '/../config.php');
b2054038 30require_once($CFG->dirroot.'/course/lib.php');
b2054038 31
29ca8b88 32// Initialise ALL the incoming parameters here, up front.
33$courseid = required_param('courseId', PARAM_INT);
34$class = required_param('class', PARAM_ALPHA);
b2054038 35$field = optional_param('field', '', PARAM_ALPHA);
b2054038 36$sectionid = optional_param('sectionId', 0, PARAM_INT);
37$beforeid = optional_param('beforeId', 0, PARAM_INT);
38$value = optional_param('value', 0, PARAM_INT);
b2054038 39$id = optional_param('id', 0, PARAM_INT);
b2054038 40
51fc46a3 41$PAGE->set_url('/course/rest.php', array('courseId'=>$courseid,'class'=>$class));
b2054038 42
af189935
PS
43//NOTE: when making any changes here please make sure it is using the same access control as course/mod.php !!
44
74df2951 45$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
ff92b711
RK
46// Check user is logged in and set contexts if we are dealing with resource
47if (in_array($class, array('resource'))) {
48 $cm = get_coursemodule_from_id(null, $id, $course->id, false, MUST_EXIST);
49 require_login($course, false, $cm);
9a5e297b 50 $modcontext = context_module::instance($cm->id);
ff92b711
RK
51} else {
52 require_login($course);
ff92b711 53}
9a5e297b 54$coursecontext = context_course::instance($course->id);
92942376
PS
55require_sesskey();
56
ff92b711
RK
57echo $OUTPUT->header(); // send headers
58
4b6728e4
MG
59if ($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);
62 }
63
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);
70 }
71
72} else if ($class === 'resource' && $field === 'move') {
73
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);
77 }
78
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;
84 }
85
86 $isvisible = moveto_module($cm, $section, $beforemod);
87 echo json_encode(array('visible' => (bool) $isvisible));
b2054038 88}