Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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