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 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  /**
  18   * This is the external method for returning a list of h5p activities.
  19   *
  20   * @package    mod_h5pactivity
  21   * @since      Moodle 3.9
  22   * @copyright  2020 Carlos Escobedo <carlos@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace mod_h5pactivity\external;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  require_once($CFG->libdir . '/externallib.php');
  31  
  32  use external_api;
  33  use external_function_parameters;
  34  use external_value;
  35  use external_single_structure;
  36  use external_multiple_structure;
  37  use external_util;
  38  use external_warnings;
  39  use context_module;
  40  use core_h5p\factory;
  41  
  42  /**
  43   * This is the external method for returning a list of h5p activities.
  44   *
  45   * @copyright  2020 Carlos Escobedo <carlos@moodle.com>
  46   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  47   */
  48  class get_h5pactivities_by_courses extends external_api {
  49      /**
  50       * Parameters.
  51       *
  52       * @return external_function_parameters
  53       */
  54      public static function execute_parameters(): external_function_parameters {
  55          return new external_function_parameters (
  56              [
  57                  'courseids' => new external_multiple_structure(
  58                      new external_value(PARAM_INT, 'Course id'), 'Array of course ids', VALUE_DEFAULT, []
  59                  ),
  60              ]
  61          );
  62      }
  63  
  64      /**
  65       * Returns a list of h5p activities in a provided list of courses.
  66       * If no list is provided all h5p activities that the user can view will be returned.
  67       *
  68       * @param  array $courseids course ids
  69       * @return array of h5p activities and warnings
  70       * @since Moodle 3.9
  71       */
  72      public static function execute(array $courseids): array {
  73          global $PAGE;
  74  
  75          $warnings = [];
  76          $returnedh5pactivities = [];
  77  
  78          $params = external_api::validate_parameters(self::execute_parameters(), [
  79              'courseids' => $courseids
  80          ]);
  81  
  82          $mycourses = [];
  83          if (empty($params['courseids'])) {
  84              $mycourses = enrol_get_my_courses();
  85              $params['courseids'] = array_keys($mycourses);
  86          }
  87  
  88          // Ensure there are courseids to loop through.
  89          if (!empty($params['courseids'])) {
  90  
  91              $factory = new factory();
  92  
  93              list($courses, $warnings) = external_util::validate_courses($params['courseids'], $mycourses);
  94              $output = $PAGE->get_renderer('core');
  95  
  96              // Get the h5p activities in this course, this function checks users visibility permissions.
  97              // We can avoid then additional validate_context calls.
  98              $h5pactivities = get_all_instances_in_courses('h5pactivity', $courses);
  99              foreach ($h5pactivities as $h5pactivity) {
 100                  $context = context_module::instance($h5pactivity->coursemodule);
 101                  // Remove fields that are not from the h5p activity (added by get_all_instances_in_courses).
 102                  unset($h5pactivity->coursemodule, $h5pactivity->context,
 103                      $h5pactivity->visible, $h5pactivity->section,
 104                      $h5pactivity->groupmode, $h5pactivity->groupingid);
 105  
 106                  $exporter = new h5pactivity_summary_exporter($h5pactivity,
 107                      ['context' => $context, 'factory' => $factory]);
 108                  $summary = $exporter->export($output);
 109                  $returnedh5pactivities[] = $summary;
 110              }
 111          }
 112  
 113          $result = [
 114              'h5pactivities' => $returnedh5pactivities,
 115              'warnings' => $warnings
 116          ];
 117          return $result;
 118      }
 119  
 120      /**
 121       * Describes the get_h5pactivities_by_courses return value.
 122       *
 123       * @return external_single_structure
 124       * @since Moodle 3.9
 125       */
 126      public static function execute_returns() {
 127          return new external_single_structure(
 128              [
 129                  'h5pactivities' => new external_multiple_structure(
 130                      h5pactivity_summary_exporter::get_read_structure()
 131                  ),
 132                  'warnings' => new external_warnings(),
 133              ]
 134          );
 135      }
 136  }