forked from jacano1969/moodle-mod-360
-
Notifications
You must be signed in to change notification settings - Fork 1
/
locallib.php
630 lines (547 loc) · 20.5 KB
/
locallib.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
<?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/>.
global $CFG;
require_once("$CFG->libdir/ddllib.php");
class shim
{
static function build_navigation($navlinks)
{
if(isset($PAGE)) {
foreach ($navlinks as $navlink) {
/** @var moodle_page $PAGE */
$PAGE->navbar->add($navlink['name'], new moodle_url($navlink['name']));
}
}
}
}
/**
* List of competencies along with their skills.
*
* @param $activityid int
* @return array
*/
function threesixty_get_competency_listing($activityid) {
global $DB;
$ret = array();
$table_skill = '{threesixty_skill}';
$table_competency = '{threesixty_competency}';
$sql =
"SELECT s.id AS skillid, c.id AS competencyid, c.name, c.description, s.name AS skillname, c.showfeedback, c.sortorder AS competencyorder, s.sortorder AS skillorder FROM ".$table_skill." AS s".
" RIGHT OUTER JOIN ".$table_competency." AS c ON s.competencyid = c.id".
" WHERE c.activityid = $activityid".
" ORDER BY c.sortorder, s.sortorder"
;
if ($rs = $DB->get_records_sql($sql)) {
foreach ($rs as $record) {
if (empty($ret[$record->competencyid])) {
$competency = new object;
$competency->id = $record->competencyid;
$competency->name = $record->name;
$competency->description = $record->description;
$competency->showfeedback = ($record->showfeedback == 1);
$competency->skills = $record->skillname;
$ret[$competency->id] = $competency;
} else {
$ret[$record->competencyid]->skills .= ', ' . $record->skillname;
}
}
}
return $ret;
}
/**
* Delete the given competency from the database.
*
* @param integer $competencyid The ID of the competency record
* @param boolean $intransaction True if there is already an active transation
* @return bool True if the operation has succeeded, false otherwise
* @throws Exception
*/
function threesixty_delete_competency($competencyid, $intransaction=false) {
global $DB;
if (!$intransaction) {
$transaction = $DB->start_delegated_transaction();
}
// Delete all dependent skills.
$skills = $DB->get_records('threesixty_skill', array('competencyid' => $competencyid));
if ($skills and count($skills) > 0) {
foreach ($skills as $skill) {
if (!threesixty_delete_skill($skill->id, true)) {
throw new Exception('Couldn\'t delete the skill');
}
}
}
// Delete all dependent response competencies.
if (!$DB->delete_records('threesixty_response_comp', array('competencyid' => $competencyid))) {
if (!$intransaction) {
// TODO moodle_rollback_sql();
}
return false;
}
// Delete competencies to be carried to the training diary.
if (!$DB->delete_records('threesixty_carried_comp', array('competencyid' => $competencyid))) {
if (!$intransaction) {
throw new Exception('Couldn\'t delete competency');
}
return false;
}
// Perform the deletions.
if (!$DB->delete_records('threesixty_competency', array('id' => $competencyid))) {
if (!$intransaction) {
throw new Exception('Couldn\'t delete competency');
}
return false;
}
if (isset($transaction) && !$intransaction) {
$DB->commit_delegated_transaction($transaction);
}
return true;
}
/**
* Delete the given skill from the database.
*
* @param integer $skillid The ID of the skill record
* @param boolean $intransaction True if there is already an active transation
* @return bool True if the operation has succeeded, false otherwise
* @throws Exception
*/
function threesixty_delete_skill($skillid, $intransaction=false) {
global $DB;
if (!$intransaction) {
$transaction = $DB->start_delegated_transaction();
}
// Delete all dependent response skills.
if (!$DB->delete_records('threesixty_response_skill', array('skillid' => $skillid))) {
if (!$intransaction) {
throw new Exception('Couldn\'t delete skills');
}
return false;
}
// Delete matching records in trdiary_pdp_skill.
$trdiarytable = new xmldb_table('trdiary_pdp_skill');
$dbman = $DB->get_manager();
if ($dbman->table_exists($trdiarytable)) {
if (!$DB->delete_records('trdiary_pdp_skill', array('skillid' => $skillid))) {
if (!$intransaction) {
throw new Exception('Couldn\'t delete skills');
}
return false;
}
}
// Perform the deletion.
if (!$DB->delete_records('threesixty_skill', array('id' => $skillid))) {
if (!$intransaction) {
throw new Exception('Couldn\'t delete skills');
}
return false;
}
if (isset($transaction) && !$intransaction) {
$DB->commit_delegated_transaction($transaction);
}
return true;
}
/**
* Delete the given analysis from the database.
*
* @param integer $analysisid The ID of the analysis record
* @param boolean $intransaction True if there is already an active transation
* @return bool True if the operation has succeeded, false otherwise
* @throws Exception
*/
function threesixty_delete_analysis($analysisid, $intransaction=false) {
global $DB;
if (!$intransaction) {
$transaction = $DB->start_delegated_transaction();
}
// Delete all dependent responses.
// ...$responses = $DB->get_record('threesixty_response', array('analysisid' => $analysisid), 'id');.
$responses = $DB->get_records('threesixty_response', array('analysisid' => $analysisid), '', 'id');
if ($responses and count($responses) > 0) {
foreach ($responses as $response) {
if (!threesixty_delete_response($response->id, true)) {
if (!$intransaction) {
throw new Exception('Couldn\'t delete responses');
}
return false;
}
}
}
// Delete all dependent carried_competencies.
if (!$DB->delete_records('threesixty_carried_comp', array('analysisid' => $analysisid))) {
if (!$intransaction) {
throw new Exception('Couldn\'t delete responses');
}
return false;
}
// Delete all dependent respondent.
if (!$DB->delete_records('threesixty_respondent', array('analysisid' => $analysisid))) {
if (!$intransaction) {
throw new Exception('Couldn\'t delete responses');
}
return false;
}
// Perform the deletion.
if (!$DB->delete_records('threesixty_analysis', array('id' => $analysisid))) {
if (!$intransaction) {
throw new Exception('Couldn\'t delete responses');
}
return false;
}
if (isset($transaction) && !$intransaction) {
$DB->commit_delegated_transaction($transaction);
}
return true;
}
/**
* Delete the given response from the database.
*
* @param integer $responseid The ID of the response record
* @param boolean $intransaction True if there is already an active transation
* @return bool True if the operation has succeeded, false otherwise
* @throws Exception
*/
function threesixty_delete_response($responseid, $intransaction=false) {
global $DB;
if (!$intransaction) {
$transaction = $DB->start_delegated_transaction();
}
// Delete all dependent response competencies.
if (!$DB->delete_records('threesixty_response_comp', array('responseid' => $responseid))) {
if (!$intransaction) {
throw new Exception('Couldn\'t delete responses');
}
return false;
}
// Delete all dependent response skills.
if (!$DB->delete_records('threesixty_response_skill', array('responseid' => $responseid))) {
if (!$intransaction) {
throw new Exception('Couldn\'t delete responses');
}
return false;
}
// Perform the deletion.
if (!$DB->delete_records('threesixty_response', array('id' => $responseid))) {
if (!$intransaction) {
throw new Exception('Couldn\'t delete responses');
}
return false;
}
if (isset($transaction) && !$intransaction) {
$DB->commit_delegated_transaction($transaction);
}
return true;
}
/**
* Delete the given respondent from the database.
*
* @param integer $respondentid The ID of the respondent record
* @param boolean $intransaction True if there is already an active transation
* @return bool True if the operation has succeeded, false otherwise
* @throws Exception
*/
function threesixty_delete_respondent($respondentid, $intransaction=false) {
global $DB;
if (!$intransaction) {
$transaction = $DB->start_delegated_transaction();
}
// Delete the dependent response if necessary.
if ($responseid = $DB->get_field('threesixty_response', 'id', array('respondentid'=>$respondentid))) {
if (!threesixty_delete_response($responseid, true)) {
if (!$intransaction) {
throw new Exception('Couldn\'t delete responses');
}
return false;
}
}
// Perform the deletion.
if (!$DB->delete_records('threesixty_respondent', array('id' => $respondentid))) {
if (!$intransaction) {
throw new Exception('Couldn\'t delete respondents');
}
return false;
}
if (isset($transaction) && !$intransaction) {
$DB->commit_delegated_transaction($transaction);
}
return true;
}
/**
* List of skills and their competency.
*
* @param $activityid int
* @return array
*/
function threesixty_get_skill_names($activityid) {
global $DB;
$sql = "SELECT s.id, c.id AS competencyid, c.name AS competencyname, s.name AS skillname
FROM {threesixty_competency} c
RIGHT OUTER JOIN {threesixty_skill} s ON c.id = s.competencyid
WHERE c.activityid = $activityid
ORDER BY c.sortorder, s.sortorder";
return $DB->get_records_sql($sql);
}
/**
* List of competencyid and feedback.
*
* @param $analysisid int
* @return array
*/
function threesixty_get_feedback($analysisid) {
global $DB;
$sql = "SELECT trc.id, trc.competencyid, trc.feedback
FROM {threesixty_response} tr
JOIN {threesixty_response_comp} trc
ON trc.responseid=tr.id
WHERE tr.analysisid=$analysisid";
$ret = $DB->get_records_sql($sql);
if ($ret) {
return $ret;
} else {
return array();
}
}
/**
* List of scores set by the user as well as the name of the score.
*
* @param $analysisid int id of analysis to filter to
* @param $competencyaverage boolean whether to include aggregate statistics
* @param null $typeid int id, if any, of respondent type to filter to
* @return object describing the search performed and it's results
*/
function threesixty_get_self_scores($analysisid, $competencyaverage, $typeid=null) {
global $selfresponsetypes, $DB;
$ret = new object();
$ret->name = $selfresponsetypes[$typeid];
$ret->type = 'self'.$typeid;
$idcolumn = 's.id';
$scorecolumn = 'rs.score';
$competencyjoin = '';
$groupbyclause = '';
$orderbyclause = 'order by s.competencyid, s.sortorder';
if ($competencyaverage) {
$idcolumn = 'c.id';
$scorecolumn = 'AVG(rs.score) AS score';
$competencyjoin = "JOIN {threesixty_competency} c ON c.id = s.competencyid";
$groupbyclause = 'GROUP BY c.id';
$orderbyclause = '';
}
$sql = "SELECT $idcolumn, $scorecolumn FROM {threesixty_respondent} rp".
" RIGHT OUTER JOIN {threesixty_response} r ON r.respondentid = rp.id".
" JOIN {threesixty_response_skill} rs ON r.id = rs.responseid".
" RIGHT OUTER JOIN {threesixty_skill} s ON s.id = rs.skillid".
$competencyjoin.
" WHERE (r.analysisid IS NULL OR r.analysisid = $analysisid)".
" AND (r.timecompleted IS NULL or r.timecompleted > 0)".
" AND rp.uniquehash IS NULL".
($typeid === null ? '' : ' AND rp.type = '.$typeid.' ').
" ".$groupbyclause.
" ".$orderbyclause
;
if (!$ret->records = $DB->get_records_sql($sql)) {
$ret->records = array();
}
return $ret;
}
/**
* Returns true if the given activity has been completed by the given user.
*
* @param $activityid int
* @param $userid int
* @return bool
*/
function threesixty_is_completed($activityid, $userid) {
/*
global $CFG, $DB;
... $sql = "SELECT r.id
FROM {threesixty_analysis} a
JOIN {threesixty_response} r ON r.analysisid = a.id
WHERE a.activityid = $activityid AND a.userid = $userid AND
r.timecompleted > 0 AND r.d IS NULL";
return $DB->get_records_sql($sql) ? true : false;... */
return true;
}
/**
* Return a list of users having submitted a response in this activity.
*
* @param object $activity Record from the threesixty table
* @returns array of user records.
*/
function threesixty_users($activity) {
global $DB;
$sql = "SELECT DISTINCT u.id, u.firstname, u.lastname
FROM {threesixty_analysis} a
JOIN {threesixty_response} r ON r.analysisid = a.id
JOIN {user} u ON a.userid = u.id
WHERE a.activityid = $activity->id AND
r.timecompleted > 0";
$records = $DB->get_records_sql($sql);
return $records;
}
/**
* Returns a list of all of the users who are eligible to participate in the
* 360 activity.
*
* @author eleanor.martin
* @param <type> $context
* @param string $sort
* @return array of user records with id, firstname, lastname.
*/
function threesixty_get_possible_participants($context, $sort="u.lastname") {
$fields = 'u.id, u.firstname, u.lastname';
// ...params are $context, $capability, $fields, $sort, $limitfrom, $limitnum, $groups, $exceptions, $doanything.
// ...doanything set to false so admins are not brought back by default.
$users = get_users_by_capability($context, 'mod/threesixty:participate', $fields, $sort, '', '', '', '', false);
return $users;
}
/**
* Return an html table listing the users.
*
* @param object $activity Record from the threesixty table
* @param string $url URL of the page to open once the 'userid' param has been added
* @returns string The HTML to print out on the page (either a table or error message)
*/
function threesixty_user_listing($activity, $url) {
if ($records = threesixty_users($activity)) {
$table = new html_table();
$table->head = array(get_string('name'));
$table->data = array();
foreach ($records as $r) {
$name = format_string(fullname($r));
$selectlink = "<a href=\"$url&userid=$r->id\">$name</a>";
$table->data[] = array($selectlink);
}
return get_string('selectuser', 'threesixty').html_writer::table($table);
}
return get_string('nousersfound', 'threesixty');
}
/**
* Return the heading to print out to show the currently selected user.
*
* @param object $user Record from the user table
* @param int $courseid ID of the current course
* @param string $url URL of the current page without the userid parameter
* @param bool $selectanother
* @return string
*/
function threesixty_selected_user_heading($user, $courseid, $url, $selectanother=true) {
global $CFG, $OUTPUT;
// ...echo "<pre>";.
$name = format_string(fullname($user));
// ...echo "\n\n"; var_dump($name);.
$data = new object;
$data->fullname = "<a href=\"$CFG->wwwroot/user/view.php?id={$user->id}&course=$courseid\">$name</a>";
$data->url = $url;
// ...echo "\n\n"; var_dump($data); die();.
if ($selectanother) {
$text = get_string('selecteduser', 'threesixty', $data);
} else {
$text = get_string('reportforuser', 'threesixty', $data);
}
// ...return $OUTPUT->heading($text, '', 2, 'main', true);.
return $OUTPUT->heading($text);
}
/**
* Return the page where the first incomplete competency is or 1 if it's complete.
*
* @param $activityid
* @param $userid
* @param $respondent
* @return int
*/
function threesixty_get_first_incomplete_competency($activityid, $userid, $respondent) {
global $DB;
$respondentclause = 'r.respondentid IS NULL';
if ($respondent != null) {
$respondentclause = "r.respondentid = $respondent->id";
}
if (!$response = $DB->get_record_sql("SELECT r.id
FROM {threesixty_analysis} a
JOIN {threesixty_response} r ON r.analysisid = a.id
WHERE a.activityid = $activityid AND a.userid = $userid AND
$respondentclause AND r.timecompleted = 0")) {
return 1; // ...activity is either not started or completed already.
}
$sql = "SELECT c.id, c.sortorder
FROM {threesixty_response} r
JOIN {threesixty_response_skill} rs ON rs.responseid = r.id
RIGHT OUTER JOIN {threesixty_skill} s ON rs.skillid = s.id
JOIN {threesixty_competency} c ON c.id = s.competencyid
WHERE (r.id IS NULL or r.id = $response->id) AND (score IS NULL OR score = 0)
ORDER BY c.sortorder";
// ...if ($rs = $DB->get_recordset_sql($sql, null, 1) and $record = rs_fetch_record($rs)) { .
$rs = $DB->get_recordset_sql($sql, null, 1);
if ($rs->valid()) {
$record = $rs->current();
// Figure out which page this competency is in.
return $record->sortorder+1;
}
// All skills have been scored, form has not been submitted, go to last page.
return $DB->$DB->count_records('threesixty_competency', 'activityid', $activityid);
}
function threesixty_get_average_skill_scores($analysisid, $respondenttype, $competencyaverage) {
global $respondenttypes, $DB;
$ret = new object();
$fromclause = "FROM {threesixty_response} r";
$wherefragment = '';
if ($respondenttype !== false) {
$fromclause = "FROM {threesixty_respondent} rp
RIGHT OUTER JOIN {threesixty_response} r ON r.respondentid = rp.id";
$wherefragment = "AND rp.uniquehash IS NOT NULL AND rp.type = $respondenttype";
$ret->name = $respondenttypes[$respondenttype] . ' ' . get_string('filter:average', 'threesixty');
$ret->type = 'type'.$respondenttype;
} else {
$ret->name = get_string('filter:average', 'threesixty');
$ret->type = get_string('filter:average', 'threesixty');
}
$idcolumn = 's.id';
$competencyjoin = '';
if ($competencyaverage) {
$idcolumn = 'c.id';
$competencyjoin = "JOIN {threesixty_competency} c ON s.competencyid = c.id";
}
$sql = "SELECT $idcolumn, AVG(rs.score) AS score
$fromclause
JOIN {threesixty_response_skill} rs ON r.id = rs.responseid
RIGHT OUTER JOIN {threesixty_skill} s ON s.id = rs.skillid
$competencyjoin
WHERE (r.analysisid IS NULL OR r.analysisid = $analysisid) AND
(r.timecompleted IS NULL or r.timecompleted > 0)
$wherefragment
GROUP BY $idcolumn";
if (!$ret->records = $DB->get_records_sql($sql)) {
$ret->records = array();
}
return $ret;
}
/*
* Redo the sort orders of the competencies in a given activity.
*
* @param $activityid - the id of the activity to reorder the competencies for.
*/
function threesixty_reorder_competencies($activityid) {
global $DB;
// Get the remaining competencies, ordered correctly, and reset the sortorder from 0.
$competencies = $DB->get_records('threesixty_competency', array('activityid' => $activityid));
if ($competencies) {
$neworder = 0;
foreach ($competencies as $competency) {
if ($competency->sortorder != $neworder) {
$competency->sortorder = $neworder;
$DB->update_record('threesixty_competency', $competency);
}
$neworder++;
}
}
}