2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Resource external API
20 * @package mod_resource
22 * @copyright 2015 Juan Leyva <juan@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die;
29 require_once("$CFG->libdir/externallib.php");
32 * Resource external functions
34 * @package mod_resource
36 * @copyright 2015 Juan Leyva <juan@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class mod_resource_external extends external_api {
43 * Returns description of method parameters
45 * @return external_function_parameters
48 public static function view_resource_parameters() {
49 return new external_function_parameters(
51 'resourceid' => new external_value(PARAM_INT, 'resource instance id')
57 * Simulate the resource/view.php web interface page: trigger events, completion, etc...
59 * @param int $resourceid the resource instance id
60 * @return array of warnings and status result
62 * @throws moodle_exception
64 public static function view_resource($resourceid) {
66 require_once($CFG->dirroot . "/mod/resource/lib.php");
68 $params = self::validate_parameters(self::view_resource_parameters(),
70 'resourceid' => $resourceid
74 // Request and permission validation.
75 $resource = $DB->get_record('resource', array('id' => $params['resourceid']), '*', MUST_EXIST);
76 list($course, $cm) = get_course_and_cm_from_instance($resource, 'resource');
78 $context = context_module::instance($cm->id);
79 self::validate_context($context);
81 require_capability('mod/resource:view', $context);
83 // Call the resource/lib API.
84 resource_view($resource, $course, $cm, $context);
87 $result['status'] = true;
88 $result['warnings'] = $warnings;
93 * Returns description of method result value
95 * @return external_description
98 public static function view_resource_returns() {
99 return new external_single_structure(
101 'status' => new external_value(PARAM_BOOL, 'status: true if success'),
102 'warnings' => new external_warnings()
108 * Describes the parameters for get_resources_by_courses.
110 * @return external_function_parameters
113 public static function get_resources_by_courses_parameters() {
114 return new external_function_parameters (
116 'courseids' => new external_multiple_structure(
117 new external_value(PARAM_INT, 'Course id'), 'Array of course ids', VALUE_DEFAULT, array()
124 * Returns a list of files in a provided list of courses.
125 * If no list is provided all files that the user can view will be returned.
127 * @param array $courseids course ids
128 * @return array of warnings and files
131 public static function get_resources_by_courses($courseids = array()) {
134 $returnedresources = array();
137 'courseids' => $courseids,
139 $params = self::validate_parameters(self::get_resources_by_courses_parameters(), $params);
141 $mycourses = array();
142 if (empty($params['courseids'])) {
143 $mycourses = enrol_get_my_courses();
144 $params['courseids'] = array_keys($mycourses);
147 // Ensure there are courseids to loop through.
148 if (!empty($params['courseids'])) {
150 list($courses, $warnings) = external_util::validate_courses($params['courseids'], $mycourses);
152 // Get the resources in this course, this function checks users visibility permissions.
153 // We can avoid then additional validate_context calls.
154 $resources = get_all_instances_in_courses("resource", $courses);
155 foreach ($resources as $resource) {
156 $context = context_module::instance($resource->coursemodule);
158 $resource->name = external_format_string($resource->name, $context->id);
160 list($resource->intro, $resource->introformat) = external_format_text($resource->intro,
161 $resource->introformat, $context->id, 'mod_resource', 'intro', null);
162 $resource->introfiles = external_util::get_area_files($context->id, 'mod_resource', 'intro', false, false);
163 $resource->contentfiles = external_util::get_area_files($context->id, 'mod_resource', 'content');
165 $returnedresources[] = $resource;
170 'resources' => $returnedresources,
171 'warnings' => $warnings
177 * Describes the get_resources_by_courses return value.
179 * @return external_single_structure
182 public static function get_resources_by_courses_returns() {
183 return new external_single_structure(
185 'resources' => new external_multiple_structure(
186 new external_single_structure(
188 'id' => new external_value(PARAM_INT, 'Module id'),
189 'course' => new external_value(PARAM_INT, 'Course id'),
190 'name' => new external_value(PARAM_RAW, 'Page name'),
191 'intro' => new external_value(PARAM_RAW, 'Summary'),
192 'introformat' => new external_format_value('intro', 'Summary format'),
193 'introfiles' => new external_files('Files in the introduction text'),
194 'contentfiles' => new external_files('Files in the content'),
195 'tobemigrated' => new external_value(PARAM_INT, 'Whether this resource was migrated'),
196 'legacyfiles' => new external_value(PARAM_INT, 'Legacy files flag'),
197 'legacyfileslast' => new external_value(PARAM_INT, 'Legacy files last control flag'),
198 'display' => new external_value(PARAM_INT, 'How to display the resource'),
199 'displayoptions' => new external_value(PARAM_RAW, 'Display options (width, height)'),
200 'filterfiles' => new external_value(PARAM_INT, 'If filters should be applied to the resource content'),
201 'revision' => new external_value(PARAM_INT, 'Incremented when after each file changes, to avoid cache'),
202 'timemodified' => new external_value(PARAM_INT, 'Last time the resource was modified'),
203 'section' => new external_value(PARAM_INT, 'Course section id'),
204 'visible' => new external_value(PARAM_INT, 'Module visibility'),
205 'groupmode' => new external_value(PARAM_INT, 'Group mode'),
206 'groupingid' => new external_value(PARAM_INT, 'Grouping id'),
210 'warnings' => new external_warnings(),