1 |
efrain |
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 |
use core_course\external\helper_for_get_mods_by_courses;
|
|
|
18 |
use core_external\external_api;
|
|
|
19 |
use core_external\external_files;
|
|
|
20 |
use core_external\external_function_parameters;
|
|
|
21 |
use core_external\external_multiple_structure;
|
|
|
22 |
use core_external\external_single_structure;
|
|
|
23 |
use core_external\external_value;
|
|
|
24 |
use core_external\external_warnings;
|
|
|
25 |
use core_external\util;
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Resource external functions
|
|
|
29 |
*
|
|
|
30 |
* @package mod_resource
|
|
|
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_resource_external extends external_api {
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Returns description of method parameters
|
|
|
40 |
*
|
|
|
41 |
* @return external_function_parameters
|
|
|
42 |
* @since Moodle 3.0
|
|
|
43 |
*/
|
|
|
44 |
public static function view_resource_parameters() {
|
|
|
45 |
return new external_function_parameters(
|
|
|
46 |
array(
|
|
|
47 |
'resourceid' => new external_value(PARAM_INT, 'resource instance id')
|
|
|
48 |
)
|
|
|
49 |
);
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Simulate the resource/view.php web interface page: trigger events, completion, etc...
|
|
|
54 |
*
|
|
|
55 |
* @param int $resourceid the resource instance id
|
|
|
56 |
* @return array of warnings and status result
|
|
|
57 |
* @since Moodle 3.0
|
|
|
58 |
* @throws moodle_exception
|
|
|
59 |
*/
|
|
|
60 |
public static function view_resource($resourceid) {
|
|
|
61 |
global $DB, $CFG;
|
|
|
62 |
require_once($CFG->dirroot . "/mod/resource/lib.php");
|
|
|
63 |
|
|
|
64 |
$params = self::validate_parameters(self::view_resource_parameters(),
|
|
|
65 |
array(
|
|
|
66 |
'resourceid' => $resourceid
|
|
|
67 |
));
|
|
|
68 |
$warnings = array();
|
|
|
69 |
|
|
|
70 |
// Request and permission validation.
|
|
|
71 |
$resource = $DB->get_record('resource', array('id' => $params['resourceid']), '*', MUST_EXIST);
|
|
|
72 |
list($course, $cm) = get_course_and_cm_from_instance($resource, 'resource');
|
|
|
73 |
|
|
|
74 |
$context = context_module::instance($cm->id);
|
|
|
75 |
self::validate_context($context);
|
|
|
76 |
|
|
|
77 |
require_capability('mod/resource:view', $context);
|
|
|
78 |
|
|
|
79 |
// Call the resource/lib API.
|
|
|
80 |
resource_view($resource, $course, $cm, $context);
|
|
|
81 |
|
|
|
82 |
$result = array();
|
|
|
83 |
$result['status'] = true;
|
|
|
84 |
$result['warnings'] = $warnings;
|
|
|
85 |
return $result;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* Returns description of method result value
|
|
|
90 |
*
|
|
|
91 |
* @return \core_external\external_description
|
|
|
92 |
* @since Moodle 3.0
|
|
|
93 |
*/
|
|
|
94 |
public static function view_resource_returns() {
|
|
|
95 |
return new external_single_structure(
|
|
|
96 |
array(
|
|
|
97 |
'status' => new external_value(PARAM_BOOL, 'status: true if success'),
|
|
|
98 |
'warnings' => new external_warnings()
|
|
|
99 |
)
|
|
|
100 |
);
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
/**
|
|
|
104 |
* Describes the parameters for get_resources_by_courses.
|
|
|
105 |
*
|
|
|
106 |
* @return external_function_parameters
|
|
|
107 |
* @since Moodle 3.3
|
|
|
108 |
*/
|
|
|
109 |
public static function get_resources_by_courses_parameters() {
|
|
|
110 |
return new external_function_parameters (
|
|
|
111 |
array(
|
|
|
112 |
'courseids' => new external_multiple_structure(
|
|
|
113 |
new external_value(PARAM_INT, 'Course id'), 'Array of course ids', VALUE_DEFAULT, array()
|
|
|
114 |
),
|
|
|
115 |
)
|
|
|
116 |
);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* Returns a list of files in a provided list of courses.
|
|
|
121 |
* If no list is provided all files that the user can view will be returned.
|
|
|
122 |
*
|
|
|
123 |
* @param array $courseids course ids
|
|
|
124 |
* @return array of warnings and files
|
|
|
125 |
* @since Moodle 3.3
|
|
|
126 |
*/
|
|
|
127 |
public static function get_resources_by_courses($courseids = array()) {
|
|
|
128 |
|
|
|
129 |
$warnings = array();
|
|
|
130 |
$returnedresources = array();
|
|
|
131 |
|
|
|
132 |
$params = array(
|
|
|
133 |
'courseids' => $courseids,
|
|
|
134 |
);
|
|
|
135 |
$params = self::validate_parameters(self::get_resources_by_courses_parameters(), $params);
|
|
|
136 |
|
|
|
137 |
$mycourses = array();
|
|
|
138 |
if (empty($params['courseids'])) {
|
|
|
139 |
$mycourses = enrol_get_my_courses();
|
|
|
140 |
$params['courseids'] = array_keys($mycourses);
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
// Ensure there are courseids to loop through.
|
|
|
144 |
if (!empty($params['courseids'])) {
|
|
|
145 |
|
|
|
146 |
list($courses, $warnings) = util::validate_courses($params['courseids'], $mycourses);
|
|
|
147 |
|
|
|
148 |
// Get the resources in this course, this function checks users visibility permissions.
|
|
|
149 |
// We can avoid then additional validate_context calls.
|
|
|
150 |
$resources = get_all_instances_in_courses("resource", $courses);
|
|
|
151 |
foreach ($resources as $resource) {
|
|
|
152 |
$context = context_module::instance($resource->coursemodule);
|
|
|
153 |
|
|
|
154 |
helper_for_get_mods_by_courses::format_name_and_intro($resource, 'mod_resource');
|
|
|
155 |
$resource->contentfiles = util::get_area_files($context->id, 'mod_resource', 'content');
|
|
|
156 |
|
|
|
157 |
$returnedresources[] = $resource;
|
|
|
158 |
}
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
$result = array(
|
|
|
162 |
'resources' => $returnedresources,
|
|
|
163 |
'warnings' => $warnings
|
|
|
164 |
);
|
|
|
165 |
return $result;
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
/**
|
|
|
169 |
* Describes the get_resources_by_courses return value.
|
|
|
170 |
*
|
|
|
171 |
* @return external_single_structure
|
|
|
172 |
* @since Moodle 3.3
|
|
|
173 |
*/
|
|
|
174 |
public static function get_resources_by_courses_returns() {
|
|
|
175 |
return new external_single_structure(
|
|
|
176 |
array(
|
|
|
177 |
'resources' => new external_multiple_structure(
|
|
|
178 |
new external_single_structure(array_merge(
|
|
|
179 |
helper_for_get_mods_by_courses::standard_coursemodule_elements_returns(),
|
|
|
180 |
[
|
|
|
181 |
'contentfiles' => new external_files('Files in the content'),
|
|
|
182 |
'tobemigrated' => new external_value(PARAM_INT, 'Whether this resource was migrated'),
|
|
|
183 |
'legacyfiles' => new external_value(PARAM_INT, 'Legacy files flag'),
|
|
|
184 |
'legacyfileslast' => new external_value(PARAM_INT, 'Legacy files last control flag'),
|
|
|
185 |
'display' => new external_value(PARAM_INT, 'How to display the resource'),
|
|
|
186 |
'displayoptions' => new external_value(PARAM_RAW, 'Display options (width, height)'),
|
|
|
187 |
'filterfiles' => new external_value(PARAM_INT, 'If filters should be applied to the resource content'),
|
|
|
188 |
'revision' => new external_value(PARAM_INT, 'Incremented when after each file changes, to avoid cache'),
|
|
|
189 |
'timemodified' => new external_value(PARAM_INT, 'Last time the resource was modified'),
|
|
|
190 |
]
|
|
|
191 |
))
|
|
|
192 |
),
|
|
|
193 |
'warnings' => new external_warnings(),
|
|
|
194 |
)
|
|
|
195 |
);
|
|
|
196 |
}
|
|
|
197 |
}
|