-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexternallib.php
209 lines (184 loc) · 8.82 KB
/
externallib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @package report_trainingsessions
* @category report
* @author Valery Fremaux (valery.fremaux@gmail.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* this file provides with WS document requests to externalize report documents
* from within an external management system
*/
defined('MOODLE_INTERNAL') || die;
require_once($CFG->dirroot.'/report/trainingsessions/lib.php');
class report_trainingsessions_external extends external_api {
public static function validate_report_parameters($configparamdefs, $inputs) {
global $DB, $CFG;
$status = self::validate_parameters($configparamdefs, $inputs);
if (!in_array($inputs['reportformat'], array('csv', 'xls', 'pdf', 'json'))) {
throw new invalid_parameter_exception('Unsupported output document format');;
}
if (!in_array($inputs['reportscope'], array('allcourses', 'currentcourse'))) {
throw new invalid_parameter_exception('Unsupported report scope');;
}
$reportlayouts = array('onefulluserpersheet', 'onefulluserperfile', 'oneuserperrow', 'alluserssessionsinglefile', 'sessions');
if (!in_array($inputs['reportlayout'], $reportlayouts)) {
throw new invalid_parameter_exception('Unsupported report layout');;
}
switch ($inputs['courseidsource']) {
case 'id': {
if (!$DB->record_exists('course', array('id' => $inputs['courseid']))) {
throw new invalid_parameter_exception('Course not found by id: '.$inputs['courseid']);
}
break;
}
case 'shortname': {
if (!$course = $DB->get_record('course', array('shortname' => $inputs['courseid']))) {
throw new invalid_parameter_exception('Course not found by shortname: '.$inputs['courseid']);
}
$status['courseid'] = $course->id;
break;
}
case 'idnumber': {
if (!$course = $DB->get_record('course', array('idnumber' => $inputs['courseid']))) {
throw new invalid_parameter_exception('Course not found by idnumber: '.$inputs['courseid']);
}
$status['courseid'] = $course->id;
break;
}
}
switch ($inputs['groupidsource']) {
// If not provided, pass through.
case 'id': {
if (!$DB->record_exists('groups', array('id' => $inputs['groupid']))) {
throw new invalid_parameter_exception('Group not found by id: '.$inputs['groupid']);
}
break;
}
case 'name': {
if (!$group = $DB->get_record('groups', array('name' => $inputs['groupid']))) {
throw new invalid_parameter_exception('Group not found by name: '.$inputs['groupid']);
}
$status['groupid'] = $group->id;
break;
}
}
switch ($inputs['useridsource']) {
case 'id': {
if (!$DB->record_exists('user', array('id' => $inputs['userid']))) {
throw new invalid_parameter_exception('User not found by id: '.$inputs['userid']);
}
break;
}
case 'username': {
if (preg_match('/^(.*)§(.*)$/', $inputs['userid'])) {
list($username, $hostroot) = explode('§', $inputs['userid']);
$hostid = $DB->get_field('mnet_host', 'id', array('wwwroot' => $hostroot));
} else {
$hostid = $CFG->mnet_localhost_id;
}
if (!$user = $DB->get_record('user', array('username' => $inputs['userid'], 'mnethostid' => $hostid))) {
throw new invalid_parameter_exception('User not found by username: '.$inputs['userid']);
}
$status['userid'] = $user->id;
break;
}
case 'idnumber': {
if (!$user = $DB->get_record('user', array('idnumber' => $inputs['userid']))) {
throw new invalid_parameter_exception('User not found by idnumber: '.$inputs['userid']);
}
$status['userid'] = $user->id;
break;
}
case 'email': {
if (!$user = $DB->get_record('user', array('email' => $inputs['userid']))) {
throw new invalid_parameter_exception('User not found by email: '.$inputs['userid']);
}
$status['userid'] = $user->id;
break;
}
}
return $status;
}
public static function get_report_url_parameters() {
return new external_function_parameters (
array(
'reportlayout' => new external_value(
PARAM_ALPHA,
'Report layout'),
'reportscope' => new external_value(
PARAM_ALPHA,
'scope of data scanned for report'),
'reportformat' => new external_value(
PARAM_ALPHA,
'document content format'),
'from' => new external_value(
PARAM_INT,
'period start timestamp'),
'to' => new external_value(
PARAM_INT,
'period end timestamp'),
'courseidsource' => new external_value(
PARAM_ALPHA,
'The source for course id. Can be "id", "idnumber", "shortname"'),
'courseid' => new external_value(
PARAM_TEXT,
'course target restriction (for group ranged reports)'),
'groupidsource' => new external_value(
PARAM_ALPHA,
'The source for group id. Can be "id" or "name"', VALUE_DEFAULT, ''),
'groupid' => new external_value(
PARAM_TEXT,
'group target restriction (for group ranged reports)', VALUE_DEFAULT, 0),
'useridsource' => new external_value(
PARAM_ALPHA,
'The source for user id. Can be "id", "username", "idnumber" or "email"', VALUE_DEFAULT, ''),
'userid' => new external_value(
PARAM_TEXT,
'user targetting restriction (for user range reports)', VALUE_DEFAULT, 0),
)
);
}
public function get_report_url($reportlayout, $reportscope, $reportformat, $from, $to, $courseidsource, $courseid = 0,
$groupidsource = '', $groupid = 0, $useridsource = '', $userid = 0) {
global $CFG;
// Ensure report format is always lowercase.
$reportformat = strtolower($reportformat);
// Validate parameters.
$parameters = array('reportlayout' => $reportlayout,
'reportscope' => $reportscope,
'reportformat' => $reportformat,
'from' => $from,
'to' => $to,
'courseidsource' => $courseidsource,
'courseid' => $courseid,
'groupidsource' => $groupidsource,
'groupid' => $groupid,
'useridsource' => $useridsource,
'userid' => $userid);
$params = self::validate_report_parameters(self::get_report_url_parameters(), $parameters);
if (report_trainingsessions_supports_feature('export/ws')) {
include_once($CFG->dirroot.'/report/trainingsessions/pro/locallib.php');
return report_trainingsessions_export_report($reportlayout, $reportscope, $reportformat, $from, $to,
$params['courseid'], $params['groupid'], $params['userid']);
} else {
throw new moodle_exception('Feature not supported in this release.');
}
}
public static function get_report_url_returns() {
return new external_value(PARAM_URL, 'An Url to the file');
}
}