Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
/course/ -> rest.php (source)
   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   */
  25  
  26  if (!defined('AJAX_SCRIPT')) {
  27      define('AJAX_SCRIPT', true);
  28  }
  29  require_once(__DIR__ . '/../config.php');
  30  require_once($CFG->dirroot.'/course/lib.php');
  31  
  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);
  40  
  41  $PAGE->set_url('/course/rest.php', array('courseId'=>$courseid,'class'=>$class));
  42  
  43  //NOTE: when making any changes here please make sure it is using the same access control as course/mod.php !!
  44  
  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);
  53  }
  54  $coursecontext = context_course::instance($course->id);
  55  require_sesskey();
  56  
  57  echo $OUTPUT->header(); // send headers
  58  
  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);
  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));
  88  }