Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 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   * IMSCP external API
  19   *
  20   * @package    mod_imscp
  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_function_parameters;
  30  use core_external\external_multiple_structure;
  31  use core_external\external_single_structure;
  32  use core_external\external_value;
  33  use core_external\external_warnings;
  34  use core_external\util;
  35  
  36  /**
  37   * IMSCP external functions
  38   *
  39   * @package    mod_imscp
  40   * @category   external
  41   * @copyright  2015 Juan Leyva <juan@moodle.com>
  42   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   * @since      Moodle 3.0
  44   */
  45  class mod_imscp_external extends external_api {
  46  
  47      /**
  48       * Returns description of method parameters
  49       *
  50       * @return external_function_parameters
  51       * @since Moodle 3.0
  52       */
  53      public static function view_imscp_parameters() {
  54          return new external_function_parameters(
  55              array(
  56                  'imscpid' => new external_value(PARAM_INT, 'imscp instance id')
  57              )
  58          );
  59      }
  60  
  61      /**
  62       * Simulate the imscp/view.php web interface page: trigger events, completion, etc...
  63       *
  64       * @param int $imscpid the imscp instance id
  65       * @return array of warnings and status result
  66       * @since Moodle 3.0
  67       * @throws moodle_exception
  68       */
  69      public static function view_imscp($imscpid) {
  70          global $DB, $CFG;
  71          require_once($CFG->dirroot . "/mod/imscp/lib.php");
  72  
  73          $params = self::validate_parameters(self::view_imscp_parameters(),
  74                                              array(
  75                                                  'imscpid' => $imscpid
  76                                              ));
  77          $warnings = array();
  78  
  79          // Request and permission validation.
  80          $imscp = $DB->get_record('imscp', array('id' => $params['imscpid']), '*', MUST_EXIST);
  81          list($course, $cm) = get_course_and_cm_from_instance($imscp, 'imscp');
  82  
  83          $context = context_module::instance($cm->id);
  84          self::validate_context($context);
  85  
  86          require_capability('mod/imscp:view', $context);
  87  
  88          // Call the imscp/lib API.
  89          imscp_view($imscp, $course, $cm, $context);
  90  
  91          $result = array();
  92          $result['status'] = true;
  93          $result['warnings'] = $warnings;
  94          return $result;
  95      }
  96  
  97      /**
  98       * Returns description of method result value
  99       *
 100       * @return \core_external\external_description
 101       * @since Moodle 3.0
 102       */
 103      public static function view_imscp_returns() {
 104          return new external_single_structure(
 105              array(
 106                  'status' => new external_value(PARAM_BOOL, 'status: true if success'),
 107                  'warnings' => new external_warnings()
 108              )
 109          );
 110      }
 111  
 112      /**
 113       * Describes the parameters for get_imscps_by_courses.
 114       *
 115       * @return external_function_parameters
 116       * @since Moodle 3.0
 117       */
 118      public static function get_imscps_by_courses_parameters() {
 119          return new external_function_parameters (
 120              array(
 121                  'courseids' => new external_multiple_structure(
 122                      new external_value(PARAM_INT, 'course id'), 'Array of course ids', VALUE_DEFAULT, array()
 123                  ),
 124              )
 125          );
 126      }
 127  
 128      /**
 129       * Returns a list of IMSCP packages in a provided list of courses,
 130       * if no list is provided all IMSCP packages that the user can view will be returned.
 131       *
 132       * @param array $courseids the course ids
 133       * @return array of IMSCP packages details and possible warnings
 134       * @since Moodle 3.0
 135       */
 136      public static function get_imscps_by_courses($courseids = array()) {
 137          global $CFG;
 138  
 139          $returnedimscps = array();
 140          $warnings = array();
 141  
 142          $params = self::validate_parameters(self::get_imscps_by_courses_parameters(), array('courseids' => $courseids));
 143  
 144          $courses = array();
 145          if (empty($params['courseids'])) {
 146              $courses = enrol_get_my_courses();
 147              $params['courseids'] = array_keys($courses);
 148          }
 149  
 150          // Ensure there are courseids to loop through.
 151          if (!empty($params['courseids'])) {
 152  
 153              list($courses, $warnings) = util::validate_courses($params['courseids'], $courses);
 154  
 155              // Get the imscps in this course, this function checks users visibility permissions.
 156              // We can avoid then additional validate_context calls.
 157              $imscps = get_all_instances_in_courses("imscp", $courses);
 158              foreach ($imscps as $imscp) {
 159                  $imscpdetails = helper_for_get_mods_by_courses::standard_coursemodule_element_values(
 160                          $imscp, 'mod_imscp', 'moodle/course:manageactivities', 'mod/imscp:view');
 161  
 162                  if (has_capability('moodle/course:manageactivities', context_module::instance($imscp->coursemodule))) {
 163                      $imscpdetails['revision']      = $imscp->revision;
 164                      $imscpdetails['keepold']       = $imscp->keepold;
 165                      $imscpdetails['structure']     = $imscp->structure;
 166                      $imscpdetails['timemodified']  = $imscp->timemodified;
 167                  }
 168                  $returnedimscps[] = $imscpdetails;
 169              }
 170          }
 171          $result = array();
 172          $result['imscps'] = $returnedimscps;
 173          $result['warnings'] = $warnings;
 174          return $result;
 175      }
 176  
 177      /**
 178       * Describes the get_imscps_by_courses return value.
 179       *
 180       * @return external_single_structure
 181       * @since Moodle 3.0
 182       */
 183      public static function get_imscps_by_courses_returns() {
 184          return new external_single_structure(
 185              array(
 186                  'imscps' => new external_multiple_structure(
 187                      new external_single_structure(array_merge(
 188                          helper_for_get_mods_by_courses::standard_coursemodule_elements_returns(true),
 189                          [
 190                              'revision' => new external_value(PARAM_INT, 'Revision', VALUE_OPTIONAL),
 191                              'keepold' => new external_value(PARAM_INT, 'Number of old IMSCP to keep', VALUE_OPTIONAL),
 192                              'structure' => new external_value(PARAM_RAW, 'IMSCP structure', VALUE_OPTIONAL),
 193                              'timemodified' => new external_value(PARAM_RAW, 'Time of last modification', VALUE_OPTIONAL),
 194                          ]
 195                      ), 'IMS content packages')
 196                  ),
 197                  'warnings' => new external_warnings(),
 198              )
 199          );
 200      }
 201  
 202  }