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 400 and 401]

   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;
  18  
  19  use coding_exception;
  20  use core_courseformat\base as course_format;
  21  use renderer_base;
  22  use stdClass;
  23  use course_modinfo;
  24  use JsonSerializable;
  25  
  26  /**
  27   * Class to track state actions.
  28   *
  29   * The methods from this class should be executed via "stateactions" methods.
  30   *
  31   * Each format plugin could extend this class to provide new updates to the frontend
  32   * mutation module.
  33   * Extended classes should be located in "format_XXX\course" namespace and
  34   * extends {@see \core_courseformat\stateupdates}.
  35   *
  36   * @package    core_course
  37   * @copyright  2021 Ferran Recio <ferran@moodle.com>
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class stateupdates implements JsonSerializable {
  41  
  42      /** @var course_format format the course format */
  43      protected $format;
  44  
  45      /** @var renderer_base renderer format renderer */
  46      protected $output;
  47  
  48      /** @var array the tracked updates */
  49      protected $updates;
  50  
  51      /**
  52       * State update class constructor.
  53       *
  54       * @param course_format $format Course format.
  55       */
  56      public function __construct(course_format $format) {
  57          global $PAGE;
  58  
  59          $this->format = $format;
  60          $this->output = $this->format->get_renderer($PAGE);
  61          $this->updates = [];
  62      }
  63  
  64      /**
  65       * Return the data to serialize the current track in JSON.
  66       *
  67       * @return stdClass the statement data structure
  68       */
  69      public function jsonSerialize(): array {
  70          return $this->updates;
  71      }
  72  
  73      /**
  74       * Add track about a general course state change.
  75       */
  76      public function add_course_put(): void {
  77          $courseclass = $this->format->get_output_classname('state\\course');
  78          $currentstate = new $courseclass($this->format);
  79          $this->add_update('course', 'put', $currentstate->export_for_template($this->output));
  80      }
  81  
  82      /**
  83       * Add track about a section state put.
  84       *
  85       * @param int $sectionid The affected section id.
  86       */
  87      public function add_section_put(int $sectionid): void {
  88          $this->create_or_put_section($sectionid, 'put');
  89      }
  90  
  91      /**
  92       * Add track about a new section created.
  93       *
  94       * @param int $sectionid The affected section id.
  95       */
  96      public function add_section_create(int $sectionid): void {
  97          $this->create_or_put_section($sectionid, 'create');
  98      }
  99  
 100      /**
 101       * Add track about section created or put.
 102       *
 103       * @param int $sectionid The affected section id.
 104       * @param string $action The action to track for the section ('create' or 'put').
 105       */
 106      protected function create_or_put_section(int $sectionid, string $action): void {
 107          if ($action != 'create' && $action != 'put') {
 108              throw new coding_exception(
 109                  "Invalid action passed ($action) to create_or_put_section. Only 'create' and 'put' are valid."
 110              );
 111          }
 112          $course = $this->format->get_course();
 113          $modinfo = course_modinfo::instance($course);
 114          $format = $this->format;
 115  
 116          $section = $modinfo->get_section_info_by_id($sectionid, MUST_EXIST);
 117  
 118          if (!$format->is_section_visible($section)) {
 119              return;
 120          }
 121  
 122          $sectionclass = $format->get_output_classname('state\\section');
 123          $currentstate = new $sectionclass($this->format, $section);
 124  
 125          $this->add_update('section', $action, $currentstate->export_for_template($this->output));
 126      }
 127  
 128      /**
 129       * Add track about a section deleted.
 130       *
 131       * @deprecated since Moodle 4.1 MDL-74925 - please call add_section_remove() instead.
 132       * @param int $sectionid The affected section id.
 133       */
 134      public function add_section_delete(int $sectionid): void {
 135          debugging('add_section_delete() is deprecated. Please use add_section_remove() instead.', DEBUG_DEVELOPER);
 136  
 137          $this->add_update('section', 'remove', (object)['id' => $sectionid]);
 138      }
 139  
 140      /**
 141       * Add track about a section removed.
 142       *
 143       * @param int $sectionid The affected section id.
 144       */
 145      public function add_section_remove(int $sectionid): void {
 146          $this->add_update('section', 'remove', (object)['id' => $sectionid]);
 147      }
 148  
 149      /**
 150       * Add track about a course module state update.
 151       *
 152       * @param int $cmid the affected course module id
 153       */
 154      public function add_cm_put(int $cmid): void {
 155          $this->create_or_put_cm($cmid, 'put');
 156      }
 157  
 158      /**
 159       * Add track about a course module created.
 160       *
 161       * @param int $cmid the affected course module id
 162       */
 163      public function add_cm_create(int $cmid): void {
 164          $this->create_or_put_cm($cmid, 'create', true);
 165      }
 166  
 167      /**
 168       * Add track about section created or put.
 169       *
 170       * @param int $cmid The affected course module id.
 171       * @param string $action The action to track for the section ('create' or 'put').
 172       */
 173      protected function create_or_put_cm(int $cmid, string $action): void {
 174          $modinfo = course_modinfo::instance($this->format->get_course());
 175  
 176          $cm = $modinfo->get_cm($cmid);
 177          $section = $modinfo->get_section_info_by_id($cm->section);
 178          $format = $this->format;
 179  
 180          if (!$section->uservisible || !$cm->is_visible_on_course_page()) {
 181              return;
 182          }
 183  
 184          $cmclass = $format->get_output_classname('state\\cm');
 185          $currentstate = new $cmclass($this->format, $section, $cm);
 186  
 187          $this->add_update('cm', $action, $currentstate->export_for_template($this->output));
 188      }
 189  
 190      /**
 191       * Add track about a course module deleted.
 192       *
 193       * @deprecated since Moodle 4.1 MDL-74925 - please call add_cm_remove() instead.
 194       * @param int $cmid the affected course module id
 195       */
 196      public function add_cm_delete(int $cmid): void {
 197          debugging('add_cm_delete() is deprecated. Please use add_cm_remove() instead.', DEBUG_DEVELOPER);
 198  
 199          $this->add_update('cm', 'remove', (object)['id' => $cmid]);
 200      }
 201  
 202      /**
 203       * Add track about a course module removed.
 204       *
 205       * @param int $cmid the affected course module id
 206       */
 207      public function add_cm_remove(int $cmid): void {
 208          $this->add_update('cm', 'remove', (object)['id' => $cmid]);
 209      }
 210  
 211      /**
 212       * Add a valid update message to the update list.
 213       *
 214       * @param string $name the update name
 215       * @param string $action the update action (usually update, create, remove)
 216       * @param stdClass $fields the object fields
 217       */
 218      protected function add_update(string $name, string $action, stdClass $fields): void {
 219          $this->updates[] = (object)[
 220              'name' => $name,
 221              'action' => $action,
 222              'fields' => $fields,
 223          ];
 224      }
 225  
 226  }