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 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [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  /**
  18   * Page external API
  19   *
  20   * @package    mod_page
  21   * @category   external
  22   * @copyright  2015 Juan Leyva <juan@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   * @since      Moodle 3.0
  25   */
  26  
  27  use core_course\external\helper_for_get_mods_by_courses;
  28  use core_external\external_api;
  29  use core_external\external_files;
  30  use core_external\external_format_value;
  31  use core_external\external_function_parameters;
  32  use core_external\external_multiple_structure;
  33  use core_external\external_single_structure;
  34  use core_external\external_value;
  35  use core_external\external_warnings;
  36  use core_external\util;
  37  
  38  /**
  39   * Page external functions
  40   *
  41   * @package    mod_page
  42   * @category   external
  43   * @copyright  2015 Juan Leyva <juan@moodle.com>
  44   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  45   * @since      Moodle 3.0
  46   */
  47  class mod_page_external extends external_api {
  48  
  49      /**
  50       * Returns description of method parameters
  51       *
  52       * @return external_function_parameters
  53       * @since Moodle 3.0
  54       */
  55      public static function view_page_parameters() {
  56          return new external_function_parameters(
  57              array(
  58                  'pageid' => new external_value(PARAM_INT, 'page instance id')
  59              )
  60          );
  61      }
  62  
  63      /**
  64       * Simulate the page/view.php web interface page: trigger events, completion, etc...
  65       *
  66       * @param int $pageid the page instance id
  67       * @return array of warnings and status result
  68       * @since Moodle 3.0
  69       * @throws moodle_exception
  70       */
  71      public static function view_page($pageid) {
  72          global $DB, $CFG;
  73          require_once($CFG->dirroot . "/mod/page/lib.php");
  74  
  75          $params = self::validate_parameters(self::view_page_parameters(),
  76                                              array(
  77                                                  'pageid' => $pageid
  78                                              ));
  79          $warnings = array();
  80  
  81          // Request and permission validation.
  82          $page = $DB->get_record('page', array('id' => $params['pageid']), '*', MUST_EXIST);
  83          list($course, $cm) = get_course_and_cm_from_instance($page, 'page');
  84  
  85          $context = context_module::instance($cm->id);
  86          self::validate_context($context);
  87  
  88          require_capability('mod/page:view', $context);
  89  
  90          // Call the page/lib API.
  91          page_view($page, $course, $cm, $context);
  92  
  93          $result = array();
  94          $result['status'] = true;
  95          $result['warnings'] = $warnings;
  96          return $result;
  97      }
  98  
  99      /**
 100       * Returns description of method result value
 101       *
 102       * @return \core_external\external_description
 103       * @since Moodle 3.0
 104       */
 105      public static function view_page_returns() {
 106          return new external_single_structure(
 107              array(
 108                  'status' => new external_value(PARAM_BOOL, 'status: true if success'),
 109                  'warnings' => new external_warnings()
 110              )
 111          );
 112      }
 113  
 114      /**
 115       * Describes the parameters for get_pages_by_courses.
 116       *
 117       * @return external_function_parameters
 118       * @since Moodle 3.3
 119       */
 120      public static function get_pages_by_courses_parameters() {
 121          return new external_function_parameters (
 122              array(
 123                  'courseids' => new external_multiple_structure(
 124                      new external_value(PARAM_INT, 'Course id'), 'Array of course ids', VALUE_DEFAULT, array()
 125                  ),
 126              )
 127          );
 128      }
 129  
 130      /**
 131       * Returns a list of pages in a provided list of courses.
 132       * If no list is provided all pages that the user can view will be returned.
 133       *
 134       * @param array $courseids course ids
 135       * @return array of warnings and pages
 136       * @since Moodle 3.3
 137       */
 138      public static function get_pages_by_courses($courseids = array()) {
 139  
 140          $warnings = array();
 141          $returnedpages = array();
 142  
 143          $params = array(
 144              'courseids' => $courseids,
 145          );
 146          $params = self::validate_parameters(self::get_pages_by_courses_parameters(), $params);
 147  
 148          $mycourses = array();
 149          if (empty($params['courseids'])) {
 150              $mycourses = enrol_get_my_courses();
 151              $params['courseids'] = array_keys($mycourses);
 152          }
 153  
 154          // Ensure there are courseids to loop through.
 155          if (!empty($params['courseids'])) {
 156  
 157              list($courses, $warnings) = util::validate_courses($params['courseids'], $mycourses);
 158  
 159              // Get the pages in this course, this function checks users visibility permissions.
 160              // We can avoid then additional validate_context calls.
 161              $pages = get_all_instances_in_courses("page", $courses);
 162              foreach ($pages as $page) {
 163                  helper_for_get_mods_by_courses::format_name_and_intro($page, 'mod_page');
 164  
 165                  $context = context_module::instance($page->coursemodule);
 166                  list($page->content, $page->contentformat) = \core_external\util::format_text(
 167                          $page->content, $page->contentformat,
 168                          $context, 'mod_page', 'content', $page->revision, ['noclean' => true]);
 169                  $page->contentfiles = util::get_area_files($context->id, 'mod_page', 'content');
 170  
 171                  $returnedpages[] = $page;
 172              }
 173          }
 174  
 175          $result = array(
 176              'pages' => $returnedpages,
 177              'warnings' => $warnings
 178          );
 179          return $result;
 180      }
 181  
 182      /**
 183       * Describes the get_pages_by_courses return value.
 184       *
 185       * @return external_single_structure
 186       * @since Moodle 3.3
 187       */
 188      public static function get_pages_by_courses_returns() {
 189          return new external_single_structure(
 190              array(
 191                  'pages' => new external_multiple_structure(
 192                      new external_single_structure(array_merge(
 193                          helper_for_get_mods_by_courses::standard_coursemodule_elements_returns(),
 194                          [
 195                              'content' => new external_value(PARAM_RAW, 'Page content'),
 196                              'contentformat' => new external_format_value('content', VALUE_REQUIRED, 'Content format'),
 197                              'contentfiles' => new external_files('Files in the content'),
 198                              'legacyfiles' => new external_value(PARAM_INT, 'Legacy files flag'),
 199                              'legacyfileslast' => new external_value(PARAM_INT, 'Legacy files last control flag'),
 200                              'display' => new external_value(PARAM_INT, 'How to display the page'),
 201                              'displayoptions' => new external_value(PARAM_RAW, 'Display options (width, height)'),
 202                              'revision' => new external_value(PARAM_INT, 'Incremented when after each file changes, to avoid cache'),
 203                              'timemodified' => new external_value(PARAM_INT, 'Last time the page was modified'),
 204                          ]
 205                      ))
 206                  ),
 207                  'warnings' => new external_warnings(),
 208              )
 209          );
 210      }
 211  }