forked from bdaloukas/moodle-mod_game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshowanswers.php
444 lines (390 loc) · 15.2 KB
/
showanswers.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
<?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/>.
/**
* This page shows the answers of the current game
*
* @author bdaloukas
* @version $Id: showanswers.php,v 1.7 2012/07/25 22:46:42 bdaloukas Exp $
* @package game
**/
require_once("../../config.php");
require_once( "headergame.php");
if (!has_capability('mod/game:viewreports', $context)) {
print_error( get_string( 'only_teachers', 'game'));
}
$PAGE->navbar->add(get_string('showanswers', 'game'));
$action = optional_param('action', "", PARAM_ALPHANUM); // Action.
if ($action == 'delstats') {
$DB->delete_records('game_repetitions', array('gameid' => $game->id, 'userid' => $USER->id));
}
if ($action == 'computestats') {
game_compute_repetitions($game);
}
echo '<b>'.get_string('repetitions', 'game').': </b>';
echo get_string('user').': ';
game_showusers($game);
echo " <a href=\"{$CFG->wwwroot}/mod/game/showanswers.php?q=$q&action=delstats\">".
get_string('clearrepetitions', 'game').'</a>';
echo " <a href=\"{$CFG->wwwroot}/mod/game/showanswers.php?q=$q&action=computestats\">".
get_string('computerepetitions', 'game').'</a>';
echo '<br><br>';
$existsbook = ($DB->get_record( 'modules', array( 'name' => 'book'), 'id,id'));
game_showanswers( $game, $existsbook, $context);
echo $OUTPUT->footer();
function game_compute_repetitions($game) {
global $DB, $USER;
$DB->delete_records('game_repetitions', array('gameid' => $game->id, 'userid' => $USER->id));
$sql = "INSERT INTO {game_repetitions}( gameid,userid,questionid,glossaryentryid,repetitions) ".
"SELECT $game->id,$USER->id,questionid,glossaryentryid,COUNT(*) ".
"FROM {game_queries} WHERE gameid=$game->id AND userid=$USER->id GROUP BY questionid,glossaryentryid";
if (!$DB->execute( $sql)) {
print_error('Problem on computing statistics for repetitions');
}
}
function game_showusers($game) {
global $CFG, $USER;
$users = array();
$context = game_get_context_course_instance( $game->course);
if ($courseusers = get_enrolled_users($context)) {
foreach ($courseusers as $courseuser) {
$users[$courseuser->id] = fullname($courseuser, has_capability('moodle/site:viewfullnames', $context));
}
}
if ($guest = guest_user()) {
$users[$guest->id] = fullname($guest);
}
$href = $CFG->wwwroot.'/mod/game/showanswers.php?q='.$game->id.'&userid=';
?>
<script type="text/javascript">
function onselectuser()
{
window.location.href =
"<?php echo $href;?>" + document.getElementById('menuuser').value;
}
</script>
<?php
$attributes = 'onchange="javascript:onselectuser();"';
$name = 'user';
$id = 'menu'.$name;
$class = 'menu'.$name;
$class = 'select ' . $class; // Add 'select' selector always.
$nothing = get_string("allparticipants");
$nothingvalue = '0';
$options = $users;
$selected = optional_param('userid', $USER->id, PARAM_INT);
$output = '<select id="'. $id .'" class="'. $class .'" name="'. $name .'" '. $attributes .'>' . "\n";
$output .= ' <option value="'. s($nothingvalue) .'"'. "\n";
if ($nothingvalue === $selected) {
$output .= ' selected="selected"';
}
$output .= '>'. $nothing .'</option>' . "\n";
if (!empty($options)) {
foreach ($options as $value => $label) {
$output .= ' <option value="'. s($value) .'"';
if ((string)$value == (string)$selected ||
(is_array($selected) && in_array($value, $selected))) {
$output .= ' selected="selected"';
}
if ($label === '') {
$output .= '>'. $value .'</option>' . "\n";
} else {
$output .= '>'. $label .'</option>' . "\n";
}
}
}
echo $output . '</select>' . "\n";
}
function game_showanswers( $game, $existsbook, $context) {
if ($game->gamekind == 'bookquiz' and $existsbook) {
game_showanswers_bookquiz( $game, $context);
return;
}
switch ($game->sourcemodule) {
case 'question':
game_showanswers_question( $game, $context);
break;
case 'glossary':
game_showanswers_glossary( $game);
break;
case 'quiz':
game_showanswers_quiz( $game, $context);
break;
}
}
function game_showanswers_appendselect( $game) {
switch ($game->gamekind) {
case 'hangman':
case 'cross':
case 'crypto':
return " AND qtype='shortanswer'";
case 'millionaire':
return " AND qtype = 'multichoice'";
case 'sudoku':
case 'bookquiz':
case 'snakes':
return " AND qtype in ('shortanswer', 'truefalse', 'multichoice')";
}
return '';
}
function game_showanswers_question( $game, $context) {
global $DB;
if ($game->gamekind != 'bookquiz') {
$select = ' category='.$game->questioncategoryid;
if ($game->subcategories) {
$cats = question_categorylist( $game->questioncategoryid);
if (strpos( $cats, ',') > 0) {
$select = ' category in ('.$cats.')';
}
}
} else {
$context2 = get_context_instance(50, $COURSE->id);
$select = " contextid in ($context2->id)";
$select2 = '';
if ($recs = $DB->get_records_select( 'question_categories', $select, null, 'id,id')) {
foreach ($recs as $rec) {
$select2 .= ','.$rec->id;
}
}
$select = ' AND category IN ('.substr( $select2, 1).')';
}
$select .= ' AND hidden = 0 ';
$select .= game_showanswers_appendselect( $game);
$showcategories = ($game->gamekind == 'bookquiz');
$order = ($showcategories ? 'category,questiontext' : 'questiontext');
game_showanswers_question_select( $game, '{question} q', $select, '*', $order, $showcategories, $game->course, $context);
}
function game_showanswers_quiz( $game, $context) {
global $CFG;
if (game_get_moodle_version() < '02.07') {
$select = "quiz='$game->quizid' ".
' AND qqi.question=q.id'.
' AND q.hidden=0'.
game_showanswers_appendselect( $game);
$table = '{question} q,{quiz_question_instances} qqi';
} else {
$select = "qs.quizid='$game->quizid' ".
" AND qs.questionid=q.id ".game_showanswers_appendselect( $game);
$table = "{question} q,{quiz_slots} qs";
}
game_showanswers_question_select( $game, $table, $select, 'q.*', 'category,questiontext', false, $game->course, $context);
}
function game_showanswers_question_select( $game, $table, $select, $fields, $order, $showcategoryname, $courseid, $context) {
global $CFG, $DB, $OUTPUT;
$sql = "SELECT $fields FROM $table WHERE $select ORDER BY $order";
if (($questions = $DB->get_records_sql( $sql)) === false) {
return;
}
$table .= ",{game_repetitions} gr";
$select .= " AND gr.questionid=q.id AND gr.glossaryentryid=0 AND gr.gameid=".$game->id;
$userid = optional_param('userid', 0, PARAM_INT);
if ($userid) {
$select .= " AND gr.userid=$userid";
}
$sql = "SELECT q.id as id,SUM(repetitions) as c FROM {$table} WHERE $select GROUP BY q.id";
$reps = $DB->get_records_sql( $sql);
$categorynames = array();
if ($showcategoryname) {
$select = '';
$recs = $DB->get_records( 'question_categories', null, '', '*', 0, 1);
foreach ($recs as $rec) {
if (array_key_exists( 'course', $rec)) {
$select = "course=$courseid";
} else {
$context = get_context_instance(50, $courseid);
$select = " contextid in ($context->id)";
}
break;
}
if (($categories = $DB->get_records_select( 'question_categories', $select, null, '', 'id,name'))) {
foreach ($categories as $rec) {
$categorynames[ $rec->id] = $rec->name;
}
}
}
echo '<table border="1">';
echo '<tr><td></td>';
if ($showcategoryname) {
echo '<td><b>'.get_string( 'categories', 'quiz').'</b></td>';
}
echo '<td><b>'.get_string( 'questions', 'quiz').'</b></td>';
echo '<td><b>'.get_string( 'answers', 'quiz').'</b></td>';
echo '<td><b>'.get_string( 'feedbacks', 'game').'</b></td>';
if ($reps) {
echo '<td><b>'.get_string( 'repetitions', 'game').'</b></td>';
}
echo "</tr>\r\n";
$line = 0;
foreach ($questions as $question) {
echo '<tr>';
echo '<td>'.(++$line);
echo '</td>';
if ($showcategoryname) {
echo '<td>';
if (array_key_exists( $question->category, $categorynames)) {
echo $categorynames[ $question->category];
} else {
echo ' ';
}
echo '</td>';
}
echo '<td>';
echo "<a title=\"Edit\" ".
"href=\"{$CFG->wwwroot}/question/question.php?inpopup=1&id=$question->id&courseid=$courseid\" target=\"_blank\">";
echo "<img src=\"".$OUTPUT->pix_url('t/edit')."\" alt=\"Edit\" /></a> ";
echo game_filterquestion(str_replace( array( "\'", '\"'), array( "'", '"'),
$question->questiontext), $question->id, $context->id, $game->course);
switch ($question->qtype) {
case 'shortanswer':
$recs = $DB->get_records( 'question_answers',
array( 'question' => $question->id), 'fraction DESC', 'id,answer,feedback');
if ($recs == false) {
$rec = false;
} else {
foreach ($recs as $rec) {
break;
}
}
echo "<td>$rec->answer</td>";
if ($rec->feedback == '') {
$rec->feedback = ' ';
}
echo "<td>$rec->feedback</td>";
break;
case 'multichoice':
case 'truefalse':
$recs = $DB->get_records( 'question_answers', array( 'question' => $question->id));
$feedback = '';
echo '<td>';
$i = 0;
foreach ($recs as $rec) {
if ($i++ > 0) {
echo '<br>';
}
if ($rec->fraction == 1) {
echo " <b>$rec->answer</b>";
} else {
echo " $rec->answer";
}
if ($rec->feedback == '') {
$feedback .= '<br>';
} else {
$feedback .= "<b>{$rec->feedback}</b><br>";
}
}
echo '</td>';
if ($feedback == '') {
$feedback = ' ';
}
echo "<td>$feedback</td>";
break;
default:
echo "<td>$question->qtype</td>";
break;
}
// Show repetitions.
if ($reps) {
if (array_key_exists( $question->id, $reps)) {
$rep = $reps[ $question->id];
echo '<td><center>'.$rep->c.'</td>';
} else {
echo '<td> </td>';
}
}
echo "</tr>\r\n";
}
echo "</table><br>\r\n\r\n";
}
function game_showanswers_glossary( $game) {
global $CFG, $DB;
$table = '{glossary_entries} ge';
$select = "glossaryid={$game->glossaryid}";
if ($game->glossarycategoryid) {
$select .= " AND gec.entryid = ge.id ".
" AND gec.categoryid = {$game->glossarycategoryid}";
$table .= ",{glossary_entries_categories} gec";
}
if ($game->glossaryonlyapproved) {
// Only approved glossary entries will be used.
$select .= ' AND (ge.approved=1 OR teacherentry=1)';
}
$sql = "SELECT ge.id,definition,concept FROM $table WHERE $select ORDER BY definition";
if (($questions = $DB->get_records_sql( $sql)) === false) {
return;
}
// Show repetitions of questions.
$table = "{glossary_entries} ge, {game_repetitions} gr";
$select = "glossaryid={$game->glossaryid} AND gr.glossaryentryid=ge.id AND gr.gameid=".$game->id;
$userid = optional_param('userid', 0, PARAM_INT);
if ($userid) {
$select .= " AND gr.userid=$userid";
}
if ($game->glossarycategoryid) {
$select .= " AND gec.entryid = ge.id ".
" AND gec.categoryid = {$game->glossarycategoryid}";
$table .= ",{glossary_entries_categories} gec";
}
$sql = "SELECT ge.id,SUM(repetitions) as c FROM {$table} WHERE $select GROUP BY ge.id";
$reps = $DB->get_records_sql( $sql);
echo '<table border="1">';
echo '<tr><td></td>';
echo '<td><b>'.get_string( 'questions', 'quiz').'</b></td>';
echo '<td><b>'.get_string( 'answers', 'quiz').'</b></td>';
if ($reps != false) {
echo '<td><b>'.get_string( 'repetitions', 'game').'</b></td>';
}
echo "</tr>\r\n";
$line = 0;
foreach ($questions as $question) {
if ($game->param7 == 0) { // Not allowed spaces.
if (!( strpos( $question->concept, ' ') === false)) {
continue;
}
}
if ($game->param8 == 0) { // Not allowed -.
if (!( strpos( $question->concept, '-') === false)) {
continue;
}
}
echo '<tr>';
echo '<td>'.(++$line);
echo '</td>';
$query = new StdClass;
$query->glossaryid = $game->glossaryid;
$query->glossaryentryid = $question->id;
echo '<td>'.game_show_query( $game, $query, $question->definition).'</td>';
echo '<td>'.$question->concept.'</td>';
if ($reps != false) {
if (array_key_exists( $question->id, $reps)) {
$rep = $reps[ $question->id];
echo '<td><center>'.$rep->c.'</td>';
} else {
echo '<td> </td>';
}
}
echo "</tr>\r\n";
}
echo "</table><br>\r\n\r\n";
}
function game_showanswers_bookquiz( $game, $context) {
global $CFG;
$select = "gbq.questioncategoryid=q.category ".
" AND gbq.gameid = $game->id ".
" AND bc.id = gbq.chapterid";
$table = "{question} q,{game_bookquiz_questions} gbq,{book_chapters} bc";
$showcategories = ($game->gamekind == 'bookquiz');
game_showanswers_question_select( $game, $table, $select, "DISTINCT q.*",
"bc.pagenum,questiontext", $showcategories, $game->course, $context);
}