Search moodle.org's
Developer Documentation

See Release Notes

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

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

   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_value;
  22  
  23  /**
  24   * Class for exporting a course state.
  25   *
  26   * @package    core_course
  27   * @copyright  2021 Ferran Recio <ferran@moodle.com>
  28   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   * @since      Moodle 4.0
  30   */
  31  class get_state extends external_api {
  32  
  33      /**
  34       * Webservice parameters.
  35       *
  36       * @return external_function_parameters
  37       */
  38      public static function execute_parameters(): external_function_parameters {
  39          return new external_function_parameters([
  40              'courseid' => new external_value(PARAM_INT, 'course id', VALUE_REQUIRED),
  41          ]);
  42      }
  43  
  44      /**
  45       * This method will load all course, sections and cm states needed to initialize the frontend
  46       * course editor module. The state data of every individual course, section and cm is
  47       * build using the specifics "state" output components.
  48       *
  49       * By default, the states are generated by:
  50       *  - core_courseformat\output\state\course
  51       *  - core_courseformat\output\state\section
  52       *  - core_courseformat\output\state\cm
  53       *
  54       * As the other main course outputs, format plugins can override those output components
  55       * to send more information to the frontend course editor. These extended classes should
  56       * be located in format_XXX\output\courseformat\state\course, format_XXX\output\courseformat\state\section
  57       * or format_XXX\output\courseformat\state\cm.
  58       *
  59       * @param int $courseid the course id
  60       * @return string Course state in JSON
  61       */
  62      public static function execute(int $courseid): string {
  63          global $PAGE, $CFG;
  64  
  65          require_once($CFG->dirroot.'/course/lib.php');
  66  
  67          $params = external_api::validate_parameters(self::execute_parameters(), [
  68              'courseid' => $courseid,
  69          ]);
  70          $courseid = $params['courseid'];
  71  
  72          self::validate_context(\context_course::instance($courseid));
  73  
  74          $courseformat = course_get_format($courseid);
  75          $modinfo = $courseformat->get_modinfo();
  76  
  77          // Get the proper renderer.
  78          $renderer = $courseformat->get_renderer($PAGE);
  79  
  80          $result = (object)[
  81              'course' => (object)[],
  82              'section' => [],
  83              'cm' => [],
  84          ];
  85  
  86          // Load the output class names.
  87          $courseclass = $courseformat->get_output_classname('state\\course');
  88          $sectionclass = $courseformat->get_output_classname('state\\section');
  89          $cmclass = $courseformat->get_output_classname('state\\cm');
  90  
  91          // General state.
  92          $coursestate = new $courseclass($courseformat);
  93          $result->course = $coursestate->export_for_template($renderer);
  94  
  95          // Sections and course modules state.
  96          $sections = $modinfo->get_section_info_all();
  97          foreach ($sections as $section) {
  98              if ($courseformat->is_section_visible($section)) {
  99                  // Only return this section data if it's visible by current user on the course page.
 100                  $sectionstate = new $sectionclass($courseformat, $section);
 101                  $result->section[] = $sectionstate->export_for_template($renderer);
 102              }
 103          }
 104  
 105          foreach ($modinfo->cms as $cm) {
 106              if ($cm->is_visible_on_course_page()) {
 107                  // Only return this course module data if it's visible by current user on the course page.
 108                  $section = $sections[$cm->sectionnum];
 109                  $cmstate = new $cmclass($courseformat, $section, $cm);
 110                  $result->cm[] = $cmstate->export_for_template($renderer);
 111              }
 112          }
 113  
 114          return json_encode($result);
 115      }
 116  
 117      /**
 118       * Webservice returns.
 119       *
 120       * @return external_value
 121       */
 122      public static function execute_returns(): external_value {
 123          return new external_value(PARAM_RAW, 'Encoded course state JSON');
 124      }
 125  }