PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/imscp/classes/external.php

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