Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 and 403]

   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  namespace core_courseformat\external;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  require_once($CFG->libdir . '/externallib.php');
  23  
  24  use external_api;
  25  use external_function_parameters;
  26  use external_value;
  27  use external_multiple_structure;
  28  use moodle_exception;
  29  use coding_exception;
  30  use context_course;
  31  use core_courseformat\base as course_format;
  32  
  33  /**
  34   * External secrvie to update the course from the course editor components.
  35   *
  36   * @package    core_course
  37   * @copyright  2021 Ferran Recio <moodle@moodle.com>
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   * @since      Moodle 4.0
  40   */
  41  class update_course extends external_api {
  42  
  43      /**
  44       * Webservice parameters.
  45       *
  46       * @return external_function_parameters
  47       */
  48      public static function execute_parameters(): external_function_parameters {
  49          return new external_function_parameters(
  50              [
  51                  'action' => new external_value(
  52                      PARAM_ALPHANUMEXT,
  53                      'action: cm_hide, cm_show, section_hide, section_show, cm_moveleft...',
  54                      VALUE_REQUIRED
  55                  ),
  56                  'courseid' => new external_value(PARAM_INT, 'course id', VALUE_REQUIRED),
  57                  'ids' => new external_multiple_structure(
  58                      new external_value(PARAM_INT, 'Target id'),
  59                      'Affected ids',
  60                      VALUE_DEFAULT,
  61                      []
  62                  ),
  63                  'targetsectionid' => new external_value(
  64                      PARAM_INT, 'Optional target section id', VALUE_DEFAULT, null
  65                  ),
  66                  'targetcmid' => new external_value(
  67                      PARAM_INT, 'Optional target cm id', VALUE_DEFAULT, null
  68                  ),
  69              ]
  70          );
  71      }
  72  
  73      /**
  74       * This webservice will execute any action from the course editor. The default actions
  75       * are located in {@see \core_courseformat\stateactions} but the format plugin can extend that class
  76       * in format_XXX\course.
  77       *
  78       * The specific action methods will register in a {@see \core_courseformat\stateupdates} all the affected
  79       * sections, cms and course attribute. This object (in JSON) will be sent back to the
  80       * frontend editor to refresh the updated state elements.
  81       *
  82       * By default, {@see \core_courseformat\stateupdates} will register only create, delete and update events
  83       * on cms, sections and the general course data. However, if some plugin needs adhoc messages for
  84       * its own mutation module, extend this class in format_XXX\course.
  85       *
  86       * @param string $action the action name to execute
  87       * @param int $courseid the course id
  88       * @param int[] $ids the affected ids (section or cm depending on the action)
  89       * @param int|null $targetsectionid optional target section id (for move action)
  90       * @param int|null $targetcmid optional target cm id (for move action)
  91       * @return string Course state in JSON
  92       */
  93      public static function execute(string $action, int $courseid, array $ids = [],
  94              ?int $targetsectionid = null, ?int $targetcmid = null): string {
  95          global $CFG;
  96  
  97          require_once($CFG->dirroot . '/course/lib.php');
  98  
  99          $params = external_api::validate_parameters(self::execute_parameters(), [
 100              'action' => $action,
 101              'courseid' => $courseid,
 102              'ids' => $ids,
 103              'targetsectionid' => $targetsectionid,
 104              'targetcmid' => $targetcmid,
 105          ]);
 106          $action = $params['action'];
 107          $courseid = $params['courseid'];
 108          $ids = $params['ids'];
 109          $targetsectionid = $params['targetsectionid'];
 110          $targetcmid = $params['targetcmid'];
 111  
 112          self::validate_context(context_course::instance($courseid));
 113  
 114          $courseformat = course_get_format($courseid);
 115  
 116          // Create a course changes tracker object.
 117          $defaultupdatesclass = 'core_courseformat\\stateupdates';
 118          $updatesclass = 'format_' . $courseformat->get_format() . '\\courseformat\\stateupdates';
 119          if (!class_exists($updatesclass)) {
 120              $updatesclass = $defaultupdatesclass;
 121          }
 122          $updates = new $updatesclass($courseformat);
 123  
 124          if (!is_a($updates, $defaultupdatesclass)) {
 125              throw new coding_exception("The \"$updatesclass\" class must extend \"$defaultupdatesclass\"");
 126          }
 127  
 128          // Get the actions class from the course format.
 129          $actionsclass = 'format_'. $courseformat->get_format().'\\courseformat\\stateactions';
 130          if (!class_exists($actionsclass)) {
 131              $actionsclass = 'core_courseformat\\stateactions';
 132          }
 133          $actions = new $actionsclass();
 134  
 135          if (!is_callable([$actions, $action])) {
 136              throw new moodle_exception("Invalid course state action $action in ".get_class($actions));
 137          }
 138  
 139          $course = $courseformat->get_course();
 140  
 141          // Execute the action.
 142          $actions->$action($updates, $course, $ids, $targetsectionid, $targetcmid);
 143  
 144          // Any state action mark the state cache as dirty.
 145          course_format::session_cache_reset($course);
 146  
 147          return json_encode($updates);
 148      }
 149  
 150      /**
 151       * Webservice returns.
 152       *
 153       * @return external_value
 154       */
 155      public static function execute_returns(): external_value {
 156          return new external_value(PARAM_RAW, 'Encoded course update JSON');
 157      }
 158  }