Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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