Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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