-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexternallib.php
More file actions
279 lines (236 loc) · 10.5 KB
/
externallib.php
File metadata and controls
279 lines (236 loc) · 10.5 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
use core_course_external;
use external_api;
use external_function_parameters;
use external_multiple_structure;
use external_single_structure;
use external_value;
use invalid_parameter_exception;
use core_cohort_external;
use core_user_external;
defined('MOODLE_INTERNAL') || die;
require_once($CFG->libdir . '/externallib.php');
require_once($CFG->dirroot . '/cohort/externallib.php');
require_once($CFG->dirroot . '/user/externallib.php');
require_once($CFG->dirroot . '/user/lib.php');
require_once($CFG->dirroot . '/course/externallib.php');
class local_analytics_external extends external_api {
/**
* Returns description of method parameters
*
* @return external_function_parameters
*/
public static function get_user_enrolment_data_parameters() {
return new external_function_parameters(
[
'userid' => new external_value(PARAM_INT, 'User ID'),
'courseid' => new external_value(PARAM_INT, 'Course ID')
]
);
}
public static function get_user_enrolment_data($userid, $courseid) {
global $DB;
$user_enrolment_datas = $DB->get_records("user_enrolments", array('userid' => $userid));
$enrol_data = null;
$user_enrolment_data = null;
foreach ($user_enrolment_datas as $user_enrol) {
$enrol_db_data = $DB->get_record("enrol", array('courseid' => $courseid, 'id' => $user_enrol->enrolid));
if ($enrol_db_data != null) {
$enrol_data = $enrol_db_data;
$user_enrolment_data = $user_enrol;
}
};
$data = new \stdClass();
$data->userid = $user_enrolment_data->userid;
$data->courseid = $enrol_data->courseid;
$data->timecreated = $enrol_data->timecreated;
$data->enrol = $enrol_data->enrol;
$data->roleid = $enrol_data->roleid;
$data->enrolstartdate = $user_enrolment_data->timestart;
$data->enrolenddate = $user_enrolment_data->timeend;
return $data;
}
/**
* Returns description of method result value
*
* @return external_multiple_structure
*/
public static function get_user_enrolment_data_returns(): external_single_structure {
return new external_single_structure(
array(
'userid' => new external_value(PARAM_INT, 'Standard Moodle primary key.'),
'courseid' => new external_value(PARAM_INT, 'Standard Moodle primary key.'),
'timecreated' => new external_value(PARAM_INT, 'Standard Moodle primary key.'),
'enrol' => new external_value(PARAM_TEXT, 'Standard Moodle primary key.'),
'roleid' => new external_value(PARAM_INT, 'Standard Moodle primary key.'),
'enrolstartdate' => new external_value(PARAM_INT, 'Standard Moodle primary key.'),
'enrolenddate' => new external_value(PARAM_INT, 'Standard Moodle primary key.'),
)
);
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
*/
public static function get_user_certificates_parameters() {
return new external_function_parameters(
[
'userid' => new external_value(PARAM_INT, 'User ID'),
'courseid' => new external_value(PARAM_INT, 'Course ID'),
'coursecategory' => new external_value(PARAM_INT, 'Course Category'),
'courseidnumber' => new external_value(PARAM_TEXT, 'Course ID Number')
]
);
}
public static function get_user_certificates($userid, $courseid, $coursecategory, $courseidnumber) {
global $DB;
$courses= core_course_external::get_courses_by_field('category',$coursecategory);
$certificates_in_category = [];
foreach ($courses["courses"] as $key => $course) {
if ($course["id"]!=$courseid){
$sql = "SELECT cc.id, ci.timecreated, cc.name FROM {customcert_issues} AS ci INNER JOIN {customcert} as CC ON ci.customcertid = cc.id WHERE ci.userid=:userid and cc.course=:courseid ORDER BY ci.timecreated ASC";
$user_certificates_data = $DB->get_records_sql($sql, array('userid' => $userid, 'courseid' => $course["id"]));
$index = 0;
foreach ($user_certificates_data as $key2 => $certificate) {
$data = new \stdClass();
$data->certificateid = $certificate->id;
$data->timecreated = $certificate->timecreated;
$data->name = $certificate->name;
$data->userid = $userid;
$data->courseid = $courseid;
$data->type = "T";
if (count($user_certificates_data) <= 1) {
if (substr($courseidnumber,0,2) === "RC"){
$data->type = "R";
}
} else if ($index == (count($user_certificates_data) - 1)) {
$data->type = "P";
}
$index++;
$certificates_in_category[$course["id"]] = $data;
}
}
}
//viejo
$sql = "SELECT cc.id, ci.timecreated, cc.name FROM {customcert_issues} AS ci INNER JOIN {customcert} as CC ON ci.customcertid = cc.id WHERE ci.userid=:userid and cc.course=:courseid ORDER BY ci.timecreated ASC";
$user_certificates_data = $DB->get_records_sql($sql, array('userid' => $userid, 'courseid' => $courseid));
$certificates_to_retrieve = [];
$index = 0;
foreach ($user_certificates_data as $key => $certificate) {
$data = new \stdClass();
$data->certificateid = $certificate->id;
$data->timecreated = $certificate->timecreated;
$data->name = $certificate->name;
$data->userid = $userid;
$data->courseid = $courseid;
$data->type = "T";
$data->latest = true;
if (count($user_certificates_data) <= 1) {
if (substr($courseidnumber,0,2) === "RC"){
$data->type = "R";
}
} else if ($index == (count($user_certificates_data) - 1)) {
$data->type = "P";
}
foreach ($certificates_in_category as $key2 => $course){
#other certificate time created > current certificate time created
if($course->timecreated > $certificate->timecreated){
$data->latest = false;
}
}
$index++;
array_push($certificates_to_retrieve, $data);
}
//termina viejo
return $certificates_to_retrieve;
}
/**
* Returns description of method result value
*
* @return external_multiple_structure
*/
public static function get_user_certificates_returns(): external_multiple_structure {
return new external_multiple_structure(
new external_single_structure(
array(
'certificateid' => new external_value(PARAM_INT, 'Standard Moodle primary key.'),
'timecreated' => new external_value(PARAM_TEXT, 'Standard Moodle primary key.'),
'name' => new external_value(PARAM_TEXT, 'Standard Moodle primary key.'),
'userid' => new external_value(PARAM_INT, 'Standard Moodle primary key.'),
'courseid' => new external_value(PARAM_INT, 'Standard Moodle primary key.'),
'type' => new external_value(PARAM_TEXT, 'Standard Moodle primary key'),
'latest' => new external_value(PARAM_BOOL, 'Standard Moodle primary key')
)
)
);
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
*/
public static function update_user_supervisor_parameters() {
return new external_function_parameters(
[
'userid' => new external_value(PARAM_INT, 'User ID')
]
);
}
public static function update_user_supervisor($userid) {
global $DB;
$transaction = $DB->start_delegated_transaction();
$supervisor_username = '';
$cohorts = core_cohort_external::get_cohorts();
foreach ($cohorts as $key => $cohort) {
$cohort_members = core_cohort_external::get_cohort_members([$cohort["id"]]);
$key2 = array_search($userid, $cohort_members[0]["userids"]);
if ($key2 !== false) {
$supervisor_username = $cohort["idnumber"];
}
}
//TODO:
// - find admin's email
$supervisor = core_user_external::get_users_by_field('username', [$supervisor_username]);
$supervisor_email = $supervisor[0]['email'];
$supervisor_name = $supervisor[0]['fullname'];
//TODO:
// - update user data with the supervisor
$user_customfield_supervisor_name = [
//'name' => 'Supervisor Name',
'value' => $supervisor_name,
'type' => 'supervisor_name',
//'shortname'=>'supervisor_name'
];
$user_customfield_supervisor_email = [
//'name' => 'Supervisor Email',
'value' => $supervisor_email,
'type' => 'supervisor_email',
//'shortname'=> 'supervisor_email'
];
$updated_user = [
'id' => $userid,
'customfields' => [
$user_customfield_supervisor_name,
$user_customfield_supervisor_email
]
];
core_user_external::update_users([$updated_user]);
$transaction->allow_commit();
return [
'success' => 'true',
];
}
/**
* Returns description of method result value
*
* @return external_multiple_structure
*/
public static function update_user_supervisor_returns(): external_single_structure {
return new external_single_structure(
array(
'success' => new external_value(PARAM_TEXT, 'Standard Moodle primary key.'),
)
);
}
}