-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlib.php
More file actions
executable file
·586 lines (494 loc) · 16.6 KB
/
lib.php
File metadata and controls
executable file
·586 lines (494 loc) · 16.6 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
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
<?php // $Id: lib.php,v 1.8 2007/12/12 00:09:46 stronk7 Exp $
/**
* Library of functions and constants for module exagames
* This file should have two well differenced parts:
* - All the core Moodle functions, neeeded to allow
* the module to work integrated in Moodle.
* - All the exagames specific functions, needed
* to implement all the module logic. Please, note
* that, if the module become complex and this lib
* grows a lot, it's HIGHLY recommended to move all
* these module specific functions to a new php file,
* called "locallib.php" (see forum, quiz...). This will
* help to save some memory when Moodle is performing
* actions across all modules.
*/
require_once($CFG->dirroot . '/mod/quiz/locallib.php');
require_once($CFG->dirroot . '/question/engine/lib.php');
/**
* Given an object containing all the necessary data,
* (defined by the form in mod.html) this function
* will create a new instance and return the id number
* of the new instance.
*
* @param object $instance An object from the form in mod.html
* @return int The id of the newly inserted exagames record
**/
function exagames_add_instance($game)
{
global $DB;
$game->timecreated = time();
if (!$game->introformat) $game->introformat = '';
if (!$game->id = $DB->insert_record("exagames", $game)) {
return false;
}
if($game->gametype != "gamelabs") {
exagames_after_add_or_update($game);
}
return $game->id;
}
/**
* Given an object containing all the necessary data,
* (defined by the form in mod.html) this function
* will update an existing instance with new data.
*
* @param object $instance An object from the form in mod.html
* @return boolean Success/Fail
**/
function exagames_update_instance($game)
{
global $DB, $PAGE;
$exagame = $DB->get_record('exagames', ['id'=>$PAGE->cm->instance]);
$game->id = $exagame->id;
if ($game->gametype == "gamelabs" && !$game->url) {
return false;
}
$game->timemodified = time();
if (!$game->introformat) {
$game->introformat = '';
}
if (!$DB->update_record("exagames", $game)) {
return false; // some error occurred
}
// Do the processing required after an add or an update.
if ($game->gametype != "gamelabs") {
exagames_after_add_or_update($game);
}
return true;
}
/**
* Given an ID of an instance of this module,
* this function will permanently delete the instance
* and any data that depends on it.
*
* @param int $id Id of the module instance
* @return boolean Success/Failure
**/
function exagames_delete_instance($id) {
global $DB;
if (! $game = $DB->get_record("exagames", array("id"=>$id))) {
return false;
}
$result = true;
# Delete any dependent records here #
if (! $DB->delete_records("exagames", array("id"=>$id))) {
$result = false;
}
exagames_grade_item_delete($game);
return $result;
}
/**
* Return a small object with summary information about what a
* user has done with a given particular instance of this module
* Used for user activity reports.
* $return->time = the time they did it
* $return->info = a short text description
*
* @return null
* @todo Finish documenting this function
**/
function exagames_user_outline($course, $user, $mod, $exagames) {
return $return;
}
/**
* Print a detailed representation of what a user has done with
* a given particular instance of this module, for user activity reports.
*
* @return boolean
* @todo Finish documenting this function
**/
function exagames_user_complete($course, $user, $mod, $exagames) {
return true;
}
/**
* Given a course and a time, this module should find recent activity
* that has occurred in exagames activities and print it out.
* Return true if there was output, or false is there was none.
*
* @uses $CFG
* @return boolean
* @todo Finish documenting this function
**/
function exagames_print_recent_activity($course, $isteacher, $timestart) {
global $CFG;
return false; // True if anything was printed, otherwise false
}
/**
* Function to be run periodically according to the moodle cron
* This function searches for things that need to be done, such
* as sending out mail, toggling flags etc ...
*
* @uses $CFG
* @return boolean
* @todo Finish documenting this function
**/
function exagames_cron () {
global $CFG;
return true;
}
/**
* Must return an array of grades for a given instance of this module,
* indexed by user. It also returns a maximum allowed grade.
*
* Example:
* $return->grades = array of grades;
* $return->maxgrade = maximum allowed grade;
*
* return $return;
*
* @param int $exagamesid ID of an instance of this module
* @return mixed Null or object with an array of grades and with the maximum grade
**/
function exagames_grades($exagamesid) {
return NULL;
}
/**
* Must return an array of user records (all data) who are participants
* for a given instance of exagames. Must include every user involved
* in the instance, independient of his role (student, teacher, admin...)
* See other modules as example.
*
* @param int $exagamesid ID of an instance of this module
* @return mixed boolean/array of students
**/
function exagames_get_participants($exagamesid) {
return false;
}
/**
* This function returns if a scale is being used by one exagames
* it it has support for grading and scales. Commented code should be
* modified if necessary. See forum, glossary or journal modules
* as reference.
*
* @param int $exagamesid ID of an instance of this module
* @return mixed
* @todo Finish documenting this function
**/
function exagames_scale_used ($exagamesid,$scaleid) {
$return = false;
//$rec = get_record("exagames","id","$exagamesid","scale","-$scaleid");
//
//if (!empty($rec) && !empty($scaleid)) {
// $return = true;
//}
return $return;
}
/**
* Checks if scale is being used by any instance of exagames.
* This function was added in 1.9
*
* This is used to find out if scale used anywhere
* @param $scaleid int
* @return boolean True if the scale is used by any exagames
*/
function exagames_scale_used_anywhere($scaleid) {
if ($scaleid and record_exists('exagames', 'grade', -$scaleid)) {
return true;
} else {
return false;
}
}
/**
* Execute post-install custom actions for the module
* This function was added in 1.9
*
* @return boolean true if success, false on error
*/
function exagames_install() {
return true;
}
/**
* Execute post-uninstall custom actions for the module
* This function was added in 1.9
*
* @return boolean true if success, false on error
*/
function exagames_uninstall() {
return true;
}
//////////////////////////////////////////////////////////////////////////////////////
/// Any other exagames functions go here. Each of them must have a name that
/// starts with exagames_
/// Remember (see note in first lines) that, if this section grows, it's HIGHLY
/// recommended to move all funcions below to a new "localib.php" file.
function exagames_print_tabs($game, $currenttab)
{
global $CFG, $USER, $DB, $COURSE, $cm;
$tabs = array();
$row = array();
$inactive = array();
$activated = array();
$row[] = new tabobject('show', $CFG->wwwroot.'/mod/exagames/view.php?id='.$cm->id, get_string('show'));
$context = context_module::instance($cm->id);
if (has_capability('moodle/course:manageactivities', $context)) {
$url = $CFG->wwwroot.'/course/mod.php?update='.$cm->id.'&return=1&sesskey='.sesskey();
$row[] = new tabobject('edit', $url, get_string('edit'));
if ($game->gametype != 'tiles' && $game->gametype != 'braingame') {
$row[] = new tabobject('edit', $CFG->wwwroot.'/grade/report/index.php?id='.$COURSE->id, get_string('grades'));
}
}
if (count($row) > 1) {
$tabs[] = $row;
}
print_tabs($tabs, $currenttab, $inactive, $activated);
}
function exagames_load_quiz($quizid) {
global $CFG, $DB, $USER;
$quizid = (int)$quizid;
$questions = $DB->get_records_sql("
SELECT qu.*
FROM {$CFG->prefix}question_bank_entries as en
INNER JOIN {$CFG->prefix}question_versions as qv on en.id = qv.questionbankentryid
INNER JOIN {$CFG->prefix}question as qu on qv.questionid = qu.id
WHERE en.questioncategoryid = ?
", [$quizid]);
if ($questions == null) {
print_error('quiznotfound');
}
// read questions accoridng to the sorting
$quiz = new stdClass();
$quiz->id = $quizid;
$quiz->questions = array();
$quiz->sumgrades = 0;
/*$quizobj = quiz::create($quiz->id, $USER->id);
$quizobj->preload_questions();
$quizobj->load_questions();*/
foreach ($questions as $question)
{
$question = question_bank::make_question($question);
$answers = $DB->get_records_sql("
SELECT qaw.*
FROM {$CFG->prefix}question as qu
INNER JOIN {$CFG->prefix}question_answers as qaw on qu.id = qaw.question
WHERE qaw.question = ?
",[$question->id]);
// only load multichoice and truefalse
$question->answers = $answers;
if (!($question instanceof qtype_multichoice_base) and !($question instanceof qtype_truefalse_question))
continue;
$question->maxmark = @$question->maxmark ? $question->maxmark : $question->defaultmark;
$quiz->sumgrades += $question->maxmark;
$quiz->questions[$question->id] = $question;
$questionExtraData = $DB->get_record('exagames_question', array('id'=>$question->id));
$question->tile_size = $questionExtraData ? $questionExtraData->tile_size : '';
$question->difficulty = $questionExtraData ? $questionExtraData->difficulty : '';
$question->content_url = $questionExtraData ? $questionExtraData->content_url : '';
$question->display_order = $questionExtraData ? $questionExtraData->display_order : '';
if (!empty($question->answers) && @$question->shuffleanswers && !empty($question->shuffleanswers)) {
$question->answers = swapshuffle_assoc($question->answers);
}
}
//echo "<pre>";
//var_dump($quiz);
return $quiz;
}
function exagames_get_game_instance($instanceid)
{
global $DB;
$game = $DB->get_record("exagames", array("id" => $instanceid));
// test for correct gametype
if (!in_array($game->gametype, array_keys(exagames_get_available_games()))) {
$game->gametype = exagames_get_default_game();
}
$game->swf = exagames_get_swf_url($game->gametype);
if ($game->gametype == 'tiles') {
$game->hasHighscore = true;
$game->width = 940;
$game->height = 535;
} else {
$game->hasHighscore = true;
$game->width = 800;
$game->height = 600;
}
return $game;
}
function exagames_get_available_games($quizzes = true)
{
if ($quizzes) return array(
'braingame' => get_string('game_braingame', 'exagames'),
'tiles' => get_string('game_tiles', 'exagames'),
'gamelabs' => get_string('game_gamelabs','exagames')
);
else return array(
'gamelabs' => get_string('game_gamelabs','exagames')
);
}
function exagames_get_default_game()
{
return 'braingame';
}
function exagames_get_default_swf()
{
return 'braingame';
}
function exagames_get_swf_url($gametype)
{
global $CFG;
if (file_exists($CFG->dirroot.'/mod/exagames/swf/'.$gametype.'.swf')) {
return $CFG->wwwroot.'/mod/exagames/swf/'.$gametype.'.swf';
} else {
return $CFG->wwwroot.'/mod/exagames/swf/'.exagames_get_default_swf().'.swf';
}
}
/**
* Delete grade item for given game
*
* @param object $quiz object
* @return object quiz
*/
function exagames_grade_item_delete($game)
{
global $CFG;
require_once $CFG->libdir.'/gradelib.php';
return grade_update('mod/exabisgaems', $game->course, 'mod', 'exagames', $game->id, 0, NULL, array('deleted'=>1));
}
/**
* This function is called at the end of exabisgaems_add_instance
* and exabisgaems_update_instance, to do the common processing.
*
* @param object $game the game object.
*/
function exagames_after_add_or_update($game)
{
//update related grade item
exagames_grade_item_update($game);
}
/**
* Create grade item for given game
*
* @param object $quiz object with extra cmidnumber
* @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
* @return int 0 if ok, error code otherwise
*/
function exagames_grade_item_update($game, $grades=NULL)
{
global $CFG;
require_once $CFG->libdir.'/gradelib.php';
$quiz = exagames_load_quiz($game->quizid);
$params = array('itemname'=>$game->name);
$params['gradetype'] = GRADE_TYPE_VALUE;
$params['grademax'] = $quiz->sumgrades;
$params['grademin'] = 0;
if ($grades === 'reset') {
$params['reset'] = true;
$grades = NULL;
}
return grade_update('mod/exagames', $game->course, 'mod', 'exagames', $game->id, 0, $grades, $params);
}
function exagames_quiz_attempt($game, $grade)
{
global $DB, $COURSE, $USER;
$quiz = exagames_load_quiz($game->quizid);
$attemptnum = 1 + $DB->get_field_sql('SELECT MAX(attempt) FROM {quiz_attempts} WHERE quiz=? AND userid=?', array($game->quizid, $grade->userid));
// $uniqueid = 1 + $DB->get_field_sql('SELECT MAX(uniqueid) FROM {quiz_attempts}');
// preview made from teacher or higher has always attemptnum = 1, so this attemptnum is reserved
$context_course = context_course::instance_by_id($COURSE->id);
$roles = get_user_roles($context_course, $USER->id);
foreach ($roles as $role) {
if ($role->roleid <= 4 && $attemptnum == 1) {
$attemptnum = 2;
}
}
// copied from question/engine/datalib.php: public function insert_questions_usage_by_activity(question_usage_by_activity $quba)
// create a new question usage, and use that id to create a quiz attempt
// then we can delete the question usage.
$cm = get_coursemodule_from_instance('question_category', $quiz->id, $quiz->course, false, MUST_EXIST);
$context = context_module::instance($cm->id);
$record = new stdClass();
$record->contextid = $context->id;
$record->component = 'mod_quiz';
$record->preferredbehaviour = 'deferredfeedback';
$newid = $DB->insert_record('question_usages', $record);
// $DB->delete_records("question_usages", array("id"=>$newid));
$attempt = new StdClass;
$attempt->quiz = $game->quizid;
$attempt->userid = $grade->userid;
$attempt->attempt = $attemptnum;
$attempt->sumgrades = $grade->rawgrade;
$attempt->timestart = time();
$attempt->timefinish = time();
$attempt->timemodified = time();
$attempt->layout = '';
$attempt->state = 'finished';
$attempt->uniqueid = $newid;
$DB->insert_record('quiz_attempts', $attempt);
$quiz_grade = new StdClass;
$quiz_grade->quiz = $game->quizid;
$quiz_grade->userid = $grade->userid;
$quiz_grade->grade = $grade->rawgrade / $quiz->sumgrades * $quiz->grade;
$quiz_grade->timemodified = time();
if ($db_quiz_grade = $DB->get_record('quiz_grades', array('quiz'=>$game->quizid, 'userid'=>$grade->userid))) {
$quiz_grade->id = $db_quiz_grade->id;
$DB->update_record('quiz_grades', $quiz_grade);
} else {
$DB->insert_record('quiz_grades', $quiz_grade);
}
}
//http://localhost/mod/exagames/view.php?id=10&responses[5]=false&responses[6]=16
function exagames_calc_grade_from_responses($quiz, $responses)
{
$overallGrade = 0;
foreach ($quiz->questions as $question) {
if (isset($responses[$question->id])) {
$response = $responses[$question->id];
if ($question instanceof qtype_multichoice_single_question) {
$fraction = isset($question->answers[$response]) ? $question->answers[$response]->fraction : 0;
} elseif ($question instanceof qtype_multichoice_multi_question) {
$response = explode(',', $response);
$fraction = 0;
foreach ($response as $ansid) {
if (isset($question->answers[$ansid]))
$fraction += $question->answers[$ansid]->fraction;
}
$fraction = min(max(0, $fraction), 1.0);
} elseif ($question instanceof qtype_truefalse_question) {
$fraction = (int) ($question->rightanswer == (bool)$response);
} else {
die('wrong question type');
}
$overallGrade += $fraction * $question->maxmark;
}
}
return $overallGrade;
}
function exagames_html_to_text($text)
{
$text = str_replace(array("\r", "\n"), '', $text);
$text = preg_replace("!<(p|br)[^a-z]*>!iU", "\n", $text);
$text = strip_tags($text);
$text = trim($text);
return $text;
}
if (!function_exists('printr')) {
function printr($var)
{
echo "<pre>";
if (is_array($var)) {
print_r($var);
} elseif (is_object($var)) {
print_r($var);
echo 'methods of class '.get_class($var).': ';
print_r(get_class_methods($var));
} else {
var_dump($var);
}
echo "</pre>";
}
}
function exagames_get_string($string, $library = null)
{
$manager = get_string_manager();
if ($manager->string_exists($string, "exagames"))
return $manager->get_string($string, 'exagames');
return $manager->get_string($string, $library);
}