-
Notifications
You must be signed in to change notification settings - Fork 6
/
rpclib.php
459 lines (407 loc) · 18.1 KB
/
rpclib.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<?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/>.
/**
* Form for editing HTML block instances.
*
* @package block_use_stats
* @category blocks
* @author Valery Fremaux ([email protected])
* @copyright Valery Fremaux ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
define('USE_STATS_SITE_SCOPE', 1);
define('USE_STATS_COURSE_SCOPE', 2);
define('USE_STATS_MODULE_SCOPE', 3);
require_once($CFG->dirroot.'/blocks/use_stats/locallib.php');
require_once($CFG->dirroot.'/mnet/xmlrpc/client.php');
/*
* Constants.
*/
if (!defined('RPC_SUCCESS')) {
define('RPC_TEST', 100);
define('RPC_SUCCESS', 200);
define('RPC_FAILURE', 500);
define('RPC_FAILURE_USER', 501);
define('RPC_FAILURE_CONFIG', 502);
define('RPC_FAILURE_DATA', 503);
define('RPC_FAILURE_CAPABILITY', 510);
define('MNET_FAILURE', 511);
define('RPC_FAILURE_RECORD', 520);
define('RPC_FAILURE_RUN', 521);
}
/**
* Invoke the local user who make the RPC call and check his rights.
* @param object $user The calling user.
* @param string $capability The capability to check.
* @param object $context The capability's context (optional / CONTEXT_SYSTEM by default).
*/
function use_stats_invoke_local_user($user, $capability, $context = null) {
global $USER, $DB;
// Creating response.
$response = new stdclass;
$response->status = RPC_SUCCESS;
// Checking user.
if (!array_key_exists('username', $user) ||
!array_key_exists('remoteuserhostroot', $user) ||
!array_key_exists('remotehostroot', $user)) {
$response->status = RPC_FAILURE_USER;
$response->errors[] = 'Bad client user format.';
return(json_encode($response));
}
if (empty($user['username'])) {
$response->status = RPC_FAILURE_USER;
$response->errors[] = 'Empty username.';
return(json_encode($response));
}
// Get local identity.
if (!$remotehost = $DB->get_record('mnet_host', array('wwwroot' => $user['remotehostroot']))) {
$response->status = RPC_FAILURE;
$response->errors[] = 'Calling host is not registered. Check MNET configuration';
return(json_encode($response));
}
$userhost = $DB->get_record('mnet_host', array('wwwroot' => $user['remoteuserhostroot']));
if (!$localuser = $DB->get_record('user', array('username' => $user['username'], 'mnethostid' => $userhost->id))) {
$response->status = RPC_FAILURE_USER;
$response->errors[] = "Calling user has no local account. Register remote user first";
return(json_encode($response));
}
// Replacing current user by remote user.
$USER = $localuser;
// Checking capabilities.
if (is_null($context)) {
$context = context_system::instance();
}
if ((is_string($capability) && !has_capability($capability, $context)) ||
(is_string($capability) && !has_one_capability($capability, $context))) {
$response->status = RPC_FAILURE_CAPABILITY;
$response->errors[] = 'Local user\'s identity has no capability to run';
return(json_encode($response));
}
return '';
}
/**
* get a complete report of user stats for a single user.
*
* @param array $callinguser The calling user descriptor
* @param string $targetuser Who stats are required for
* @param string $whereroot Where the user comes from
* @param string $statsscope Scope of the stats
* @param string $timefrom Scope of the stats
*/
function use_stats_rpc_get_stats($callinguser, $targetuser, $whereroot,
$statsscope = USE_STATS_SITE_SCOPE, $timefrom = 0, $jsonresponse = true) {
global $CFG, $USER, $DB;
$extresponse = new stdclass;
$extresponse->status = RPC_SUCCESS;
$extresponse->errors[] = array();
// Invoke local user and check his rights.
$capabilities = array('block/use_stats:seesitedetails', 'block/use_stats:seecoursedetails');
if ($authresponse = use_stats_invoke_local_user((array)$callinguser, $capabilities)) {
if ($jsonresponse) {
return $authresponse;
} else {
return json_decode($authresponse);
}
}
if (empty($whereroot) || $whereroot == $CFG->wwwroot) {
if (!$targetuser = $DB->get_record('user', array('username' => $targetuser))) {
$extresponse->status = RPC_FAILURE_RECORD;
$extresponse->errors[] = 'Target user does not exist.';
if ($jsonresponse) {
return json_encode($extresponse);
} else {
return $extresponse;
}
}
// Get stats and report answer.
if (empty($config->threshold)) {
set_config('threshold', 60, 'block_use_stats');
$config->threshold = 60;
}
$logs = use_stats_extract_logs($timefrom, time(), $targetuser->id);
$lasttime = $timefrom;
$totaltime = 0;
$totaltimecourse = array();
$totaltimemodule = array();
if ($logs) {
foreach ($logs as $alog) {
$delta = $alog->time - $lasttime;
if ($delta < $config->threshold * MINSECS) {
$totaltime = $totaltime + $delta;
if ($statsscope >= USE_STATS_COURSE_SCOPE) {
if (!array_key_exists($alog->course, $totaltimecourse)) {
$totaltimecourse[$alog->course] = 0;
} else {
$totaltimecourse[$alog->course] = $totaltimecourse[$alog->course] + $delta;
}
}
if ($statsscope >= USE_STATS_MODULE_SCOPE) {
if (!array_key_exists($alog->course, $totaltimemodule)) {
$totaltimemodule[$alog->course][$alog->module] = 0;
} else if (!array_key_exists($alog->module, $totaltimemodule[$alog->course])) {
$totaltimemodule[$alog->course][$alog->module] = 0;
} else {
$t = $totaltimemodule[$alog->course][$alog->module] + $delta;
$totaltimemodule[$alog->course][$alog->module] = $t;
}
}
}
$lasttime = $alog->time;
}
$elapsed = floor($totaltime / MINSECS);
$data .= "\t<USERNAME>{$targetuser->username}</USERNAME>\n";
$data .= "\t<FIRSTNAME>{$targetuser->firstname}</FIRSTNAME>\n";
$data .= "\t<LASTNAME>{$course->idnumber}</LASTNAME>\n";
$data .= "\t<FROM>{$timefrom}</FROM>\n";
$data .= "\t<ELAPSED>{$elapsed}</ELAPSED>\n";
$message = "<USER>\n$data\n</USER>";
if ($statsscope >= USE_STATS_COURSE_SCOPE) {
$sitedata = '';
foreach ($totaltimecourse as $courseid => $statvalue) {
$courseinfo = $DB->get_record('course', array('id' => $courseid));
$elapsed = floor($statvalue / MINSECS);
if ($elapsed < 5) {
// Cleaning output from unsignificant values.
continue;
}
$coursedata = "\t<NAME>{$courseinfo->fullname}</NAME>\n";
$coursedata .= "\t<SHORTNAME>{$courseinfo->shortname}</SHORTNAME>\n";
$coursedata .= "\t<IDNUMBER>{$courseinfo->idnumber}</IDNUMBER>\n";
$coursedata .= "\t<ELAPSED>{$elapsed}</ELAPSED>\n";
if ($statsscope >= USE_STATS_MODULE_SCOPE) {
$moddata = '';
foreach ($totaltimemodule as $cmid => $statvalue) {
$elapsed = floor($statvalue / MINSECS);
if ($elapsed < 2) {
// Cleaning output from unsignificant values.
continue;
}
$cm = $DB->get_record('course_modules', array('id' => $cmid));
if ($cm) {
$modulename = $DB->get_field('modules', 'name', array('id' => $cm->module));
$modrecname = $DB->get_field($modulename, 'name', array('id' => $cm->instance));
} else {
$modulename = 'N.C.';
$modrecname = 'N.C.';
}
$data = "\t<NAME>{$modrecname}</NAME>\n";
$data .= "\t<TYPE>{$modulename}</TYPE>\n";
$data .= "\t<IDNUMBER>{$cm->idnumber}</IDNUMBER>\n";
$data .= "\t<ELAPSED>{$elapsed}</ELAPSED>\n";
$moddata .= "<MODULE>\n$data\n</MODULE>\n";
}
$coursedata .= "<MODULES>\n$moddata\n</MODULES>\n";
}
$sitedata .= "<COURSE>\n{$coursedata}\n</COURSE>\n";
}
$message .= "<COURSES>\n{$sitedata}\n</COURSES>\n";
$extresponse->message = "<USE_STATS>\n{$message}\n</USE_STATS>";
} else {
$extresponse->message = "<USE_STATS>\n{$message}\n</USE_STATS>";
}
} else {
$extresponse->message = "<USE_STATS><EMPTYSET /></USE_STATS>";
}
$extresponse->status = RPC_SUCCESS;
if ($jsonresponse) {
return json_encode($extresponse);
} else {
return $extresponse;
}
} else {
// Make remote call.
$userhostroot = $DB->get_field('mnet_host', 'wwwroot', array('id' => $USER->mnethostid));
$rpcclient = new mnet_xmlrpc_client();
$rpcclient->set_method('blocks/use_stats/rpclib.php/use_stats_rpc_get_stats');
$caller->username = $USER->username;
$caller->remoteuserhostroot = $userhostroot;
$caller->remotehostroot = $CFG->wwwroot;
$rpcclient->add_param($caller, 'struct'); // Caller user.
$rpcclient->add_param($targetuser, 'string');
$rpcclient->add_param($wherewwwroot, 'string');
$rpcclient->add_param($statsscope, 'string');
$rpcclient->add_param($timefrom, 'int');
$mnethost = new mnet_peer();
$mnethost->set_wwwroot($whereroot);
if (!$rpcclient->send($mnethost)) {
$extresponse->status = RPC_FAILURE;
$extresponse->errors[] = 'REMOTE : '.implode("<br/>\n", $rpcclient->errors);
$extresponse->errors[] = json_encode($rpcclient);
if ($jsonresponse) {
return json_encode($extresponse);
} else {
return $extresponse;
}
}
$response = json_decode($rpcclient->response);
if ($response->status == 100) {
$extresponse->message = "Remote Test Point : ".$response->teststatus;
if ($jsonresponse) {
return json_encode($extresponse);
} else {
return $extresponse;
}
}
if ($response->status == 200) {
$extresponse->message = $response->message;
if ($jsonresponse) {
return json_encode($extresponse);
} else {
return $extresponse;
}
} else {
$extresponse->status = RPC_FAILURE;
$extresponse->errors[] = 'Remote application error : ';
$extresponse->errors[] = $response->errors;
if ($jsonresponse) {
return json_encode($extresponse);
} else {
return $extresponse;
}
}
}
}
function use_stats_rpc_get_stats_wrapped($wrap) {
return use_stats_rpc_get_stats(@$wrap['callinguser'], @$wrap['targetuser'], @$wrap['whereroot'], @$wrap['statsscope'],
@$wrap['timefrom'], @$wrap['json_response']);
}
/**
* get a complete report of user scoring for a single user.
*
* @param array $callinguser The calling user descriptor
* @param string $targetuser Who stats are required for
* @param string $whereroot Where the user comes from
* @param string $scorescope The grading scope
* @param string $courseidfield The course identifier field as 'id', 'idnumber' or 'shortname'
* @param string $courseidentifier The course effective identifier
* @param string $jsonresponse If true expects a jsonified scalar response
* @return a response object
*/
function use_stats_rpc_get_scores($callinguser, $targetuser, $whereroot, $scorescope = 'notes/global', $courseidfield,
$courseidentifier, $jsonresponse = true) {
global $CFG, $USER, $DB;
$extresponse = new stdclass;
$extresponse->status = RPC_SUCCESS;
$extresponse->errors[] = array();
// Invoke local user and check his rights.
$capabilities = array('block/use_stats:seesitedetails', 'block/use_stats:seecoursedetails');
if ($authresponse = use_stats_invoke_local_user((array)$callinguser, $capabilities)) {
if ($jsonresponse) {
return $authresponse;
} else {
return json_decode($authresponse);
}
}
if (empty($whereroot) || $whereroot == $CFG->wwwroot) {
// Getting remote_course definition.
switch ($courseidfield) {
case 'id':
$course = $DB->get_record('course', array('id' => $courseidentifier));
break;
case 'shortname':
$course = $DB->get_record('course', array('shortname' => $courseidentifier));
break;
case 'idnumber':
$course = $DB->get_record('course', array('idnumber' => $courseidentifier));
break;
}
if (!$course) {
$extresponse->status = RPC_FAILURE_RECORD;
$extresponse->errors[] = 'Unkown course.';
if ($jsonresponse) {
return json_encode($extresponse);
} else {
return $extresponse;
}
}
if (!$targetuser = $DB->get_record('user', array('username' => $targetuser))) {
$extresponse->status = RPC_FAILURE_RECORD;
$extresponse->errors[] = 'Target user does not exist.';
if ($jsonresponse) {
return json_encode($extresponse);
} else {
return $extresponse;
}
}
$data .= "\t<USERNAME>{$targetuser->username}</USERNAME>\n";
$data .= "\t<FIRSTNAME>{$targetuser->firstname}</FIRSTNAME>\n";
$data .= "\t<LASTNAME>{$targetuser->idnumber}</LASTNAME>\n";
if ($statsscope == 'notes/global') {
$gradeitem = $DB->get_record('grade_items', array('itemtype' => 'course', 'courseid' => $course->id));
$grade = $DB->get_record('grade_grades', array('itemid' => $gradeitem->id));
$message = "<USER>\n$data\n</USER>";
$message .= "<SCORE>$grade->rawgrade</SCORE>";
} else {
$message = "<ERROR>Not implemented</ERROR>";
$extresponse->message = "<USER_SCORES>\n{$message}\n</USER_SCORES>";
}
$extresponse->status = RPC_SUCCESS;
if ($jsonresponse) {
return json_encode($extresponse);
} else {
return $extresponse;
}
} else {
// Make remote call.
$userhostroot = $DB->get_field('mnet_host', 'wwwroot', array('id' => $USER->mnethostid));
$rpcclient = new mnet_xmlrpc_client();
$rpcclient->set_method('blocks/use_stats/rpclib.php/use_stats_rpc_get_scores');
$caller->username = $USER->username;
$caller->remoteuserhostroot = $userhostroot;
$caller->remotehostroot = $CFG->wwwroot;
$rpcclient->add_param($caller, 'struct'); // Caller user.
$rpcclient->add_param($targetuser, 'string');
$rpcclient->add_param($whereroot, 'string');
$rpcclient->add_param($statsscope, 'string');
$rpcclient->add_param($courseidfield, 'string');
$rpcclient->add_param($courseidentifier, 'string');
$mnethost = new mnet_peer();
$mnethost->set_wwwroot($whereroot);
if (!$rpcclient->send($mnethost)) {
$extresponse->status = RPC_FAILURE;
$extresponse->errors[] = 'REMOTE : '.implode("<br/>\n", $rpcclient->errors);
$extresponse->errors[] = json_encode($rpcclient);
if ($jsonresponse) {
return json_encode($extresponse);
} else {
return $extresponse;
}
}
$response = json_decode($rpcclient->response);
if ($response->status == 200) {
$extresponse->message = $response->message;
if ($jsonresponse) {
return json_encode($extresponse);
} else {
return $extresponse;
}
} else {
$extresponse->status = RPC_FAILURE;
$extresponse->errors[] = 'Remote application error : ';
$extresponse->errors[] = $response->errors;
if ($jsonresponse) {
return json_encode($extresponse);
} else {
return $extresponse;
}
}
}
}
function use_stats_rpc_get_scores_wrapped($wrap) {
return use_stats_rpc_get_scores(@$wrap['callinguser'], @$wrap['targetuser'], @$wrap['whereroot'], @$wrap['scorescope'],
@$wrap['courseidfield'], @$wrap['courseidentifier'], @$wrap['json_response']);
}