-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
471 lines (438 loc) · 16 KB
/
lib.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
<?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/>.
/**
* Library of interface functions and constants.
*
* @package block_escapecell
* @copyright marc.leconte
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* All students enrolled in the course.
*
* @param int $courseid The course ID where the students are registered.
* @return \stdClass Standard Moodle DB object with the ID, firstname and lastname
* for each student enrolled in the course.
*/
function liststudent($courseid) {
global $DB;
$result = $DB->get_record('role', array('shortname' => 'student'), "id");
$studentroleid = $result->id;
$request = "
SELECT DISTINCT u.id, u.firstname, u.lastname
FROM {user} u
JOIN {role_assignments} ra
ON u.id = ra.userid
JOIN {context} cx
ON ra.contextid = cx.id
JOIN {course} c
ON cx.instanceid = c.id
AND cx.contextlevel = 50
WHERE 1=1
AND c.id = ?
AND ra.roleid = ?
ORDER BY u.lastname
";
$result = $DB->get_records_sql($request, array($courseid, $studentroleid));
return $result;
}
/**
* Calculate the distribution of students by level and bonus.
*
* @param sdtClass[] $students All students with their scores.
* @return table containing the distribution of students by score.
**/
function computerepart($students) {
$repart = array();
$tot = count($students);
for ($i = 1; $i < 10; $i++) {
$repart[$i] = [0, 0, 0, 0, $tot];
}
foreach ($students as $student) {
foreach ($student->score as $key => $value) {
$repart[$key][$value] = $repart[$key][$value] + 1;
$repart[$key][4] = $repart[$key][4] - 1;
}
}
return $repart;
}
/**
* Calculates the score value according to the bonuses obtained per level.
* Score = sum (bonus * 4).
*
* @param sdtClass[] $students All students with their bonus per level.
* @return the table of scores sorted from the best to the worst.
* clt[userid] = scoreValue.
**/
function computescore($students) {
$clt = array();
foreach ($students as $student) {
$totpoint = 0;
foreach ($student->score as $key => $value) {
$totpoint += $key * ($value + 1);
}
$clt[$student->id] = $totpoint;
}
arsort($clt);
return $clt;
}
function computescore2($students, $courseid) {
global $DB;
$qcmmod = $DB->get_record('modules', ['name' => 'quiz'], $fields = 'id');
$qcmmodid = $qcmmod->id;
$reqqcm = "SELECT sum(finalgrade) as sumpts
FROM {grade_grades}
WHERE userid = ?
AND itemid IN (SELECT id FROM {grade_items}
WHERE iteminstance IN (SELECT instance
FROM {course_modules}
WHERE course = ?
and module = ?
)
)";
$clt = array();
foreach ($students as $student) {
$totpoint = 0;
foreach ($student->score as $key => $value) {
$totpoint += $value + 6;
}
// Sum of QUIZ points for 1 student in this course.
$totqcm = $DB->get_record_sql($reqqcm, array($student->id, $courseid, $qcmmodid));
$clt[$student->id] = $totpoint + intval($totqcm->sumpts);
}
arsort($clt);
return $clt;
}
/**
* Reads all the scores obtained by the player and Keeps only the best bonus per level.
*
* @param int $userid The player ID.
* @return The table of scores obtained, i.e. the number of bonuses per level.
* $bonus[level] = nb of bonus.
**/
function getscores($userid) {
global $DB;
$scores = $DB->get_records('escapecell_score', array('userid' => $userid));
$bonus = array();
foreach ($scores as $score) {
if (!isset($bonus[$score->niveau])) {
$bonus[$score->niveau] = 0;
}
if ($bonus[$score->niveau] < $score->score) {
$bonus[$score->niveau] = $score->score;
}
}
return $bonus;
}
/**
* Liste les identifiants des etudiants qui ont passé un niveau du jeu.
*/
function getstudentbyniv($niv) {
global $DB;
$scores = $DB->get_records('escapecell_score', array('niveau' => $niv));
$std = array();
foreach ($scores as $score) {
$std[$score->userid] = 1;
}
return $std;
}
function computstat($courseid, $niv, $nobonus, $bonus1, $bonus2, $bonus3) {
global $DB;
$qcmmod = $DB->get_record('modules', ['name' => 'quiz'], $fields = 'id');
$qcmmodid = $qcmmod->id;
$escapemod = $DB->get_record('modules', ['name' => 'escapecell'], $fields = 'id');
$escapemodid = $escapemod->id;
// Instance du jeu.
$req = "select id from {escapecell} where course = ? and startlevel = ?";
$idjeu = $DB->get_field_sql($req, array ($courseid, $niv));
// Id section.
$req = "SELECT section FROM {course_modules} where course = ? and module = ?
and instance = ?";
$idsection = $DB->get_field_sql($req, array ($courseid, $escapemodid, $idjeu));
// Instance qcm correspondant.
$req = "SELECT instance FROM {course_modules} WHERE section = ? and module = ?";
$instanceqcm = $DB->get_field_sql($req, array ($idsection, $qcmmodid));
// Ensemble des etudiants qui ont passés ce QCM.
$req = "SELECT id FROM {grade_items} WHERE iteminstance = ? and courseid = ?";
$idtest = $DB->get_field_sql($req, array ($instanceqcm, $courseid));
$lststudents = liststudent($courseid);
$onlystudents = array(); // Uniquement eleves inscrit, util pour limiter aux eleves et déterminer les non participant.
foreach ($lststudents as $student) {
$onlystudents[$student->id] = -1;
}
$studentquiz = $DB->get_records('grade_grades', array('itemid' => $idtest));
$students = array();
// Soit $students[userid] = nb bonus obtenu dans jeu ou -1 si pas de jeu fait.
foreach ($studentquiz as $student) {
if (isset($student->finalgrade) && isset($onlystudents[$student->userid])) {
$students[$student->userid] = -1;
$onlystudents[$student->userid] = 0;
}
}
// On prend tous les resultats pour l'instance du jeu sur le niveau.
$resultjeux = $DB->get_records('escapecell_score', array('jeux' => $idjeu, 'niveau' => $niv));
foreach ($resultjeux as $resultat) {
// Si l'etudiant a passé le qcm alors on enregistre le resultat obtenu.
if (isset($students[$resultat->userid])) {
$students[$resultat->userid] = $resultat->score;
}
if (isset($onlystudents[$resultat->userid])) {
$onlystudents[$resultat->userid] = 0;
}
}
// On a le nombre "uniquement qcm" en faisant la somme des score a -1.
$uniqqcm = 0;
$repartqcm = [0, 0, 0, 0];
$repartjeu = [0, 0, 0, 0];
foreach ($students as $key => $value) {
if ($value == -1) {
$uniqqcm++;
} else {
$repartqcm[$value] = $repartqcm[$value] + 1;
}
}
$noparticipation = 0;
foreach ($onlystudents as $key => $value) {
if ($value == -1) {
$noparticipation++;
}
}
$ret = array();
$ret[0] = $noparticipation;
$ret[1] = $uniqqcm;
$ret[2] = $nobonus - $repartqcm[0];
$ret[3] = $bonus1 - $repartqcm[1];
$ret[4] = $bonus2 - $repartqcm[2];
$ret[5] = $bonus3 - $repartqcm[3];
$ret[6] = $repartqcm[0];
$ret[7] = $repartqcm[1];
$ret[8] = $repartqcm[2];
$ret[9] = $repartqcm[3];
return $ret;
}
/**
* Tests whether the current user is registered as a learner in the course.
*
* @param int $courseid The identifier of the course being tested
* @return true if the user is an student or false in other case.
**/
function isstudent($courseid) {
global $USER;
$result = liststudent($courseid);
$student = false;
foreach ($result as $enreg) {
if ($USER->id == $enreg->id) {
$student = true;
}
}
return $student;
}
/**
* Etablit la correspondance entre QCM et niveau escapecell.
* return un tableau tab[idsection] = [
* elementscore {
* instance : val,
* type : « QCM » / « Escapecell »
* }
* ]
*/
function getquizescapecell() {
global $DB, $COURSE;
$qcmmod = $DB->get_record('modules', ['name' => 'quiz'], $fields = 'id');
$qcmmodid = $qcmmod->id;
$escapecellmod = $DB->get_record('modules', ['name' => 'escapecell'], $fields = 'id');
$escapecellmodid = $escapecellmod->id;
$sql = "select gr.id as grid, instance, section
from {course_modules} cm
join {grade_items} gr on gr.iteminstance = cm.instance
where course = ? and module = ? and gr.courseid = ?";
$qcms = $DB->get_records_sql($sql, array($COURSE->id, $qcmmodid, $COURSE->id));
$correspondance = array();
foreach ($qcms as $qcm) {
if (isset($correspondance[$qcm->section])) {
$tabelt = $correspondance[$qcm->section];
} else {
$tabelt = array();
}
$eltscore = new stdClass();
$eltscore->instance = $qcm->instance;
$eltscore->type = 'QCM';
$eltscore->iteminstance = $qcm->grid;
$tabelt[] = $eltscore;
$correspondance[$qcm->section] = $tabelt;
}
$cells = $DB->get_records('course_modules',
['module' => $escapecellmodid, 'course' => $COURSE->id],
$fields = 'section, instance');
foreach ($cells as $cell) {
if (isset($correspondance[$cell->section])) {
$tabelt = $correspondance[$cell->section];
} else {
$tabelt = array();
}
$eltscore = new stdClass();
$eltscore->instance = $cell->instance;
$eltscore->type = 'CELL';
$eltscore->niveau = $DB->get_field('escapecell', 'startlevel', ['id' => $cell->instance]);
$tabelt[] = $eltscore;
$correspondance[$cell->section] = $tabelt;
}
return $correspondance;
}
function getlevel($correspond, $userid) {
$tabresultat = geteltunjoueur($correspond, $userid);
$tabresultat = fusionsection($correspond, $tabresultat);
return $tabresultat;
}
/**
* fusionne la structure resultat
* en placant dans la meme structure les qcm et niveau
* correspondant.
* La structure resultat contient au depart
* soit id = $qcm->itemid;
note = $qcm->finalgrade;
maxi = $qcm->rawgrademax;
timemodified = $qcm->timemodified;
soit id = $lvl->jeux;
bonus = $lvl->score;
niveau = $lvl->niveau;
timemodified = $lvl->timemodified;
*/
function fusionsection($correspond, $tabresultat) {
$ret = array();
foreach ($tabresultat as $resultat) {
if (isset($resultat->note) && !isset($resultat->delete)) { // QCM.
$elt = getniveaucorr($correspond, $resultat->id);
if ($elt != null) {
// Val defauts.
$resultat->rp_jeux = $elt->instance;
$resultat->rp_bonus = -1;
$resultat->rp_niveau = $elt->niveau;
foreach ($tabresultat as $rech) {
if ($rech->id == $elt->instance && isset($rech->bonus)
&& $rech->timemodified > $resultat->timemodified) {
$resultat->rp_bonus = $rech->bonus;
$rech->delete = 1;
}
}
}
} else if (isset($resultat->niveau) && !isset($resultat->delete)) {
$elt = getqcmcorr($correspond, $resultat->id);
if ($elt != null) {
$resultat->rp_id = $elt->iteminstance;
$resultat->rp_note = -1;
$resultat->rp_maxi = -1;
foreach ($tabresultat as $rech) {
if ($rech->id == $elt->iteminstance && isset($rech->note)
&& $rech->timemodified > $resultat->timemodified) {
$resultat->rp_note = $rech->note;
$resultat->rp_maxi = $rech->maxi;
$rech->delete = 1;
}
}
}
}
if (isset($resultat->delete) && $resultat->delete != 1) {
$ret[] = $resultat;
}
if (!isset($resultat->delete)) {
$ret[] = $resultat;
}
}
return $ret;
}
function getqcmcorr($correspond, $levelid) {
foreach ($correspond as $tabitem) {
$trouve = false;
foreach ($tabitem as $item) {
if ($item->type == 'CELL' && $item->instance == $levelid) {
$trouve = true;
}
}
// On prend le premier elt de type='QCM'.
if ($trouve) {
foreach ($tabitem as $item) {
if ($item->type == 'QCM') {
return $item;
}
}
}
}
return null;
}
function getniveaucorr($correspond, $qcmid) {
foreach ($correspond as $tabitem) {
$trouve = false;
foreach ($tabitem as $item) {
if ($item->type == 'QCM' && $item->iteminstance == $qcmid) {
$trouve = true;
}
}
// On prend le premier elt de type='CELL'.
if ($trouve) {
foreach ($tabitem as $item) {
if ($item->type == 'CELL') {
return $item;
}
}
}
}
return null;
}
/**
* Tout les resultats d'un joueur par ordre de passage.
* les resultats QCM et EscapeCell sont dissociés.
*/
function geteltunjoueur($correspond, $userid) {
global $DB;
$res = array();
foreach ($correspond as $tabitem) {
foreach ($tabitem as $item) {
if (isset($item->iteminstance)) { // Quiz.
$qcms = $DB->get_records('grade_grades',
['itemid' => $item->iteminstance, 'userid' => $userid],
$fields = 'finalgrade, rawgrademax, timemodified,itemid');
foreach ($qcms as $qcm) {
if (isset($qcm->finalgrade)) {
$result = new stdClass();
$result->id = $qcm->itemid;
$result->note = $qcm->finalgrade;
$result->maxi = $qcm->rawgrademax;
$result->timemodified = $qcm->timemodified;
$res[$result->timemodified] = $result;
}
}
} else {
$lvls = $DB->get_records('escapecell_score',
['jeux' => $item->instance, 'userid' => $userid],
$fields = 'niveau, score, timemodified, jeux');
foreach ($lvls as $lvl) {
if (isset($lvl->niveau)) {
$result = new stdClass();
$result->id = $lvl->jeux;
$result->bonus = $lvl->score;
$result->niveau = $lvl->niveau;
$result->timemodified = $lvl->timemodified;
$res[$result->timemodified] = $result;
}
}
}
}
}
ksort($res);
return $res;
}