Commit | Line | Data |
---|---|---|
752036b8 JL |
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 | * URL external API | |
19 | * | |
20 | * @package mod_url | |
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 | * URL external functions | |
33 | * | |
34 | * @package mod_url | |
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_url_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_url_parameters() { | |
49 | return new external_function_parameters( | |
50 | array( | |
51 | 'urlid' => new external_value(PARAM_INT, 'url instance id') | |
52 | ) | |
53 | ); | |
54 | } | |
55 | ||
56 | /** | |
1c2b7882 | 57 | * Trigger the course module viewed event and update the module completion status. |
752036b8 JL |
58 | * |
59 | * @param int $urlid the url 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_url($urlid) { | |
65 | global $DB, $CFG; | |
66 | require_once($CFG->dirroot . "/mod/url/lib.php"); | |
67 | ||
68 | $params = self::validate_parameters(self::view_url_parameters(), | |
69 | array( | |
70 | 'urlid' => $urlid | |
71 | )); | |
72 | $warnings = array(); | |
73 | ||
74 | // Request and permission validation. | |
75 | $url = $DB->get_record('url', array('id' => $params['urlid']), '*', MUST_EXIST); | |
76 | list($course, $cm) = get_course_and_cm_from_instance($url, 'url'); | |
77 | ||
78 | $context = context_module::instance($cm->id); | |
79 | self::validate_context($context); | |
80 | ||
81 | require_capability('mod/url:view', $context); | |
82 | ||
83 | // Call the url/lib API. | |
84 | url_view($url, $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_url_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 | ||
c76d5fd6 JL |
107 | /** |
108 | * Describes the parameters for get_urls_by_courses. | |
109 | * | |
110 | * @return external_function_parameters | |
111 | * @since Moodle 3.3 | |
112 | */ | |
113 | public static function get_urls_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 urls in a provided list of courses. | |
125 | * If no list is provided all urls that the user can view will be returned. | |
126 | * | |
127 | * @param array $courseids course ids | |
128 | * @return array of warnings and urls | |
129 | * @since Moodle 3.3 | |
130 | */ | |
131 | public static function get_urls_by_courses($courseids = array()) { | |
132 | ||
133 | $warnings = array(); | |
134 | $returnedurls = array(); | |
135 | ||
136 | $params = array( | |
137 | 'courseids' => $courseids, | |
138 | ); | |
139 | $params = self::validate_parameters(self::get_urls_by_courses_parameters(), $params); | |
140 | ||
141 | $mycourses = array(); | |
142 | if (empty($params['courseids'])) { | |
143 | $mycourses = enrol_get_my_courses(); | |
144 | $params['courseids'] = array_keys($mycourses); | |
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'], $mycourses); | |
151 | ||
152 | // Get the urls in this course, this function checks users visibility permissions. | |
153 | // We can avoid then additional validate_context calls. | |
154 | $urls = get_all_instances_in_courses("url", $courses); | |
155 | foreach ($urls as $url) { | |
156 | $context = context_module::instance($url->coursemodule); | |
157 | // Entry to return. | |
158 | $url->name = external_format_string($url->name, $context->id); | |
159 | ||
160 | list($url->intro, $url->introformat) = external_format_text($url->intro, | |
161 | $url->introformat, $context->id, 'mod_url', 'intro', null); | |
162 | $url->introfiles = external_util::get_area_files($context->id, 'mod_url', 'intro', false, false); | |
163 | ||
164 | $returnedurls[] = $url; | |
165 | } | |
166 | } | |
167 | ||
168 | $result = array( | |
169 | 'urls' => $returnedurls, | |
170 | 'warnings' => $warnings | |
171 | ); | |
172 | return $result; | |
173 | } | |
174 | ||
175 | /** | |
176 | * Describes the get_urls_by_courses return value. | |
177 | * | |
178 | * @return external_single_structure | |
179 | * @since Moodle 3.3 | |
180 | */ | |
181 | public static function get_urls_by_courses_returns() { | |
182 | return new external_single_structure( | |
183 | array( | |
184 | 'urls' => new external_multiple_structure( | |
185 | new external_single_structure( | |
186 | array( | |
187 | 'id' => new external_value(PARAM_INT, 'Module id'), | |
16840c71 | 188 | 'coursemodule' => new external_value(PARAM_INT, 'Course module id'), |
c76d5fd6 JL |
189 | 'course' => new external_value(PARAM_INT, 'Course id'), |
190 | 'name' => new external_value(PARAM_RAW, 'URL 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 | 'externalurl' => new external_value(PARAM_RAW_TRIMMED, 'External URL'), | |
195 | 'display' => new external_value(PARAM_INT, 'How to display the url'), | |
196 | 'displayoptions' => new external_value(PARAM_RAW, 'Display options (width, height)'), | |
197 | 'parameters' => new external_value(PARAM_RAW, 'Parameters to append to the URL'), | |
198 | 'timemodified' => new external_value(PARAM_INT, 'Last time the url was modified'), | |
199 | 'section' => new external_value(PARAM_INT, 'Course section id'), | |
200 | 'visible' => new external_value(PARAM_INT, 'Module visibility'), | |
201 | 'groupmode' => new external_value(PARAM_INT, 'Group mode'), | |
202 | 'groupingid' => new external_value(PARAM_INT, 'Grouping id'), | |
203 | ) | |
204 | ) | |
205 | ), | |
206 | 'warnings' => new external_warnings(), | |
207 | ) | |
208 | ); | |
209 | } | |
752036b8 | 210 | } |