Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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;
  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       * @param int $sectionid The affected section id.
 132       */
 133      public function add_section_delete(int $sectionid): void {
 134          $this->add_update('section', 'remove', (object)['id' => $sectionid]);
 135      }
 136  
 137      /**
 138       * Add track about a section removed.
 139       *
 140       * @param int $sectionid The affected section id.
 141       */
 142      public function add_section_remove(int $sectionid): void {
 143          $this->add_update('section', 'remove', (object)['id' => $sectionid]);
 144      }
 145  
 146      /**
 147       * Add track about a course module state update.
 148       *
 149       * @param int $cmid the affected course module id
 150       */
 151      public function add_cm_put(int $cmid): void {
 152          $this->create_or_put_cm($cmid, 'put');
 153      }
 154  
 155      /**
 156       * Add track about a course module created.
 157       *
 158       * @param int $cmid the affected course module id
 159       */
 160      public function add_cm_create(int $cmid): void {
 161          $this->create_or_put_cm($cmid, 'create', true);
 162      }
 163  
 164      /**
 165       * Add track about section created or put.
 166       *
 167       * @param int $cmid The affected course module id.
 168       * @param string $action The action to track for the section ('create' or 'put').
 169       */
 170      protected function create_or_put_cm(int $cmid, string $action): void {
 171          $modinfo = course_modinfo::instance($this->format->get_course());
 172  
 173          $cm = $modinfo->get_cm($cmid);
 174          $section = $modinfo->get_section_info_by_id($cm->section);
 175          $format = $this->format;
 176  
 177          if (!$section->uservisible || !$cm->is_visible_on_course_page()) {
 178              return;
 179          }
 180  
 181          $cmclass = $format->get_output_classname('state\\cm');
 182          $currentstate = new $cmclass($this->format, $section, $cm);
 183  
 184          $this->add_update('cm', $action, $currentstate->export_for_template($this->output));
 185      }
 186  
 187      /**
 188       * Add track about a course module deleted.
 189       *
 190       * @param int $cmid the affected course module id
 191       */
 192      public function add_cm_delete(int $cmid): void {
 193          $this->add_update('cm', 'remove', (object)['id' => $cmid]);
 194      }
 195  
 196      /**
 197       * Add track about a course module removed.
 198       *
 199       * @param int $cmid the affected course module id
 200       */
 201      public function add_cm_remove(int $cmid): void {
 202          $this->add_update('cm', 'remove', (object)['id' => $cmid]);
 203      }
 204  
 205      /**
 206       * Add a valid update message to the update list.
 207       *
 208       * @param string $name the update name
 209       * @param string $action the update action (usually update, create, remove)
 210       * @param stdClass $fields the object fields
 211       */
 212      protected function add_update(string $name, string $action, stdClass $fields): void {
 213          $this->updates[] = (object)[
 214              'name' => $name,
 215              'action' => $action,
 216              'fields' => $fields,
 217          ];
 218      }
 219  
 220  }