MDL-49337 webservices: New WS mod_data_get_databases_by_courses
[moodle.git] / mod / data / classes / external.php
CommitLineData
2ab34819
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 * Database module external API
19 *
20 * @package mod_data
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 2.9
25 */
26
27defined('MOODLE_INTERNAL') || die;
28
29require_once("$CFG->libdir/externallib.php");
30
31/**
32 * Database module external functions
33 *
34 * @package mod_data
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 2.9
39 */
40class mod_data_external extends external_api {
41
42 /**
43 * Describes the parameters for get_databases_by_courses.
44 *
45 * @return external_external_function_parameters
46 * @since Moodle 2.9
47 */
48 public static function get_databases_by_courses_parameters() {
49 return new external_function_parameters (
50 array(
51 'courseids' => new external_multiple_structure(
52 new external_value(PARAM_INT, 'course id', VALUE_REQUIRED),
53 'Array of course ids', VALUE_DEFAULT, array()
54 ),
55 )
56 );
57 }
58
59 /**
60 * Returns a list of databases in a provided list of courses,
61 * if no list is provided all databases that the user can view will be returned.
62 *
63 * @param array $courseids the course ids
64 * @return array the database details
65 * @since Moodle 2.9
66 */
67 public static function get_databases_by_courses($courseids = array()) {
68 global $CFG;
69
70 $params = self::validate_parameters(self::get_databases_by_courses_parameters(), array('courseids' => $courseids));
71 $warnings = array();
72
73 $courses = enrol_get_my_courses();
74 // Used to test for ids that have been requested but can't be returned.
75 if (count($params['courseids']) > 0) {
76 $courseids = array();
77 foreach ($params['courseids'] as $courseid) {
78 if (!in_array($courseid, array_keys($courses))) {
79 $warnings[] = array(
80 'item' => 'course',
81 'itemid' => $courseid,
82 'warningcode' => '2',
83 'message' => 'User is not enrolled or does not have requested capability'
84 );
85 } else {
86 $courseids[] = $courseid;
87 }
88 }
89 } else {
90 $courseids = array_keys($courses);
91 }
92
93 // Array to store the databases to return.
94 $arrdatabases = array();
95
96 // Ensure there are courseids to loop through.
97 if (!empty($courseids)) {
98 // Array of the courses we are going to retrieve the databases from.
99 $dbcourses = array();
100
101 // Go through the courseids.
102 foreach ($courseids as $cid) {
103 $context = context_course::instance($cid);
104 // Check the user can function in this context.
105 try {
106 self::validate_context($context);
107
108 $dbcourses[$cid] = $courses[$cid];
109
110 } catch (Exception $e) {
111 $warnings[] = array(
112 'item' => 'course',
113 'itemid' => $cid,
114 'warningcode' => '1',
115 'message' => 'No access rights in course context '.$e->getMessage().$e->getTraceAsString()
116 );
117 }
118 }
119
120 // Get the databases in this course, this function checks users visibility permissions.
121 // We can avoid then additional validate_context calls.
122 $databases = get_all_instances_in_courses("data", $dbcourses);
123
124 foreach ($databases as $database) {
125
126 $datacontext = context_module::instance($database->coursemodule);
127
128 // Entry to return.
129 $newdb = array();
130
131 // First, we return information that any user can see in the web interface.
132 $newdb['id'] = $database->id;
133 $newdb['coursemodule'] = $database->coursemodule;
134 $newdb['course'] = $database->course;
135 $newdb['name'] = $database->name;
136 // Format intro.
137 list($newdb['intro'], $newdb['introformat']) =
138 external_format_text($database->intro, $database->introformat,
139 $datacontext->id, 'mod_data', 'intro', $database->id);
140
141 // This information should be only available if the user can see the database entries.
142 if (has_capability('mod/data:viewentry', $datacontext)) {
143 $viewablefields = array('comments', 'timeavailablefrom', 'timeavailableto', 'timeviewfrom',
144 'timeviewto', 'requiredentries', 'requiredentriestoview');
145
146 // This is for avoid a long repetitive list and for
147 // checking that we are retrieving all the required fields.
148 foreach ($viewablefields as $field) {
149 // We do not use isset because it won't work for existing null values.
150 if (!property_exists($database, $field)) {
151 throw new invalid_response_exception('Missing database module required field: ' . $field);
152 }
153 $newdb[$field] = $database->{$field};
154 }
155 }
156
157 // Check additional permissions for returning optional private settings.
158 // I avoid intentionally to use can_[add|update]_moduleinfo.
159 if (has_capability('moodle/course:manageactivities', $context)) {
160
161 $additionalfields = array('maxentries', 'rssarticles', 'singletemplate', 'listtemplate',
162 'listtemplateheader', 'listtemplatefooter', 'addtemplate', 'rsstemplate', 'rsstitletemplate',
163 'csstemplate', 'jstemplate', 'asearchtemplate', 'approval', 'scale', 'assessed', 'assesstimestart',
164 'assesstimefinish', 'defaultsort', 'defaultsortdir', 'editany', 'notification');
165
166 // This is for avoid a long repetitive list.
167 foreach ($additionalfields as $field) {
168 if (property_exists($database, $field)) {
169 $newdb[$field] = $database->{$field};
170 }
171 }
172 }
173
174 $arrdatabases[] = $newdb;
175 }
176 }
177
178 $result = array();
179 $result['databases'] = $arrdatabases;
180 $result['warnings'] = $warnings;
181 return $result;
182 }
183
184 /**
185 * Describes the get_databases_by_courses return value.
186 *
187 * @return external_single_structure
188 * @since Moodle 2.9
189 */
190 public static function get_databases_by_courses_returns() {
191
192 return new external_single_structure(
193 array(
194 'databases' => new external_multiple_structure(
195 new external_single_structure(
196 array(
197 'id' => new external_value(PARAM_INT, 'Database id'),
198 'coursemodule' => new external_value(PARAM_INT, 'Course module id'),
199 'course' => new external_value(PARAM_TEXT, 'Course id'),
200 'name' => new external_value(PARAM_TEXT, 'Database name'),
201 'intro' => new external_value(PARAM_RAW, 'The Database intro'),
202 'introformat' => new external_format_value('intro'),
203 'comments' => new external_value(PARAM_BOOL, 'comments enabled'),
204 'timeavailablefrom' => new external_value(PARAM_INT, 'timeavailablefrom field'),
205 'timeavailableto' => new external_value(PARAM_INT, 'timeavailableto field'),
206 'timeviewfrom' => new external_value(PARAM_INT, 'timeviewfrom field'),
207 'timeviewto' => new external_value(PARAM_INT, 'timeviewto field'),
208 'requiredentries' => new external_value(PARAM_INT, 'requiredentries field'),
209 'requiredentriestoview' => new external_value(PARAM_INT, 'requiredentriestoview field'),
210 'maxentries' => new external_value(PARAM_INT, 'maxentries field', VALUE_OPTIONAL),
211 'rssarticles' => new external_value(PARAM_INT, 'rssarticles field', VALUE_OPTIONAL),
212 'singletemplate' => new external_value(PARAM_RAW, 'singletemplate field', VALUE_OPTIONAL),
213 'listtemplate' => new external_value(PARAM_RAW, 'listtemplate field', VALUE_OPTIONAL),
214 'listtemplateheader' => new external_value(PARAM_RAW, 'listtemplateheader field', VALUE_OPTIONAL),
215 'listtemplatefooter' => new external_value(PARAM_RAW, 'listtemplatefooter field', VALUE_OPTIONAL),
216 'addtemplate' => new external_value(PARAM_RAW, 'addtemplate field', VALUE_OPTIONAL),
217 'rsstemplate' => new external_value(PARAM_RAW, 'rsstemplate field', VALUE_OPTIONAL),
218 'rsstitletemplate' => new external_value(PARAM_RAW, 'rsstitletemplate field', VALUE_OPTIONAL),
219 'csstemplate' => new external_value(PARAM_RAW, 'csstemplate field', VALUE_OPTIONAL),
220 'jstemplate' => new external_value(PARAM_RAW, 'jstemplate field', VALUE_OPTIONAL),
221 'asearchtemplate' => new external_value(PARAM_RAW, 'asearchtemplate field', VALUE_OPTIONAL),
222 'approval' => new external_value(PARAM_BOOL, 'approval field', VALUE_OPTIONAL),
223 'scale' => new external_value(PARAM_INT, 'scale field', VALUE_OPTIONAL),
224 'assessed' => new external_value(PARAM_INT, 'assessed field', VALUE_OPTIONAL),
225 'assesstimestart' => new external_value(PARAM_INT, 'assesstimestart field', VALUE_OPTIONAL),
226 'assesstimefinish' => new external_value(PARAM_INT, 'assesstimefinish field', VALUE_OPTIONAL),
227 'defaultsort' => new external_value(PARAM_INT, 'defaultsort field', VALUE_OPTIONAL),
228 'defaultsortdir' => new external_value(PARAM_INT, 'defaultsortdir field', VALUE_OPTIONAL),
229 'editany' => new external_value(PARAM_BOOL, 'editany field', VALUE_OPTIONAL),
230 'notification' => new external_value(PARAM_INT, 'notification field', VALUE_OPTIONAL)
231 ), 'Database'
232 )
233 ),
234 'warnings' => new external_warnings(),
235 )
236 );
237 }
238
239}