-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrenderer.php
129 lines (116 loc) · 5.66 KB
/
renderer.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
<?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/>.
/**
* The main renderer for mod_quizgame
*
* @package mod_quizgame
* @copyright 2016 John Okely <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* The main renderer for mod_quizgame
*
* @package mod_quizgame
* @copyright 2016 John Okely <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_quizgame_renderer extends plugin_renderer_base {
/**
* Initialises the game and returns its HTML code
*
* @param stdClass $quizgame The quizgame to be added
* @param context $context The context
* @return string The HTML code of the game
*/
public function render_game($quizgame, $context) {
$categoryid = explode(',', $quizgame->questioncategory)[0];
$questionids = question_bank::get_finder()->get_questions_from_categories(intval($categoryid), '');
$questions = question_load_questions($questionids);
$this->page->requires->strings_for_js(
['score',
'emptyquiz',
'endofgame',
'spacetostart',
], 'mod_quizgame');
$qjson = [];
foreach ($questions as $question) {
if ($question->qtype == "multichoice" || $question->qtype == "truefalse") {
$questiontext = quizgame_cleanup($question->questiontext);
$answers = [];
foreach ($question->options->answers as $answer) {
$answertext = quizgame_cleanup($answer->answer);
$answers[] = ["text" => $answertext, "fraction" => $answer->fraction];
}
// The "single" entry is used by multichoice to determine single or multi answer.
if ($question->qtype == "truefalse") {
$qjson[] = ["question" => $questiontext, "answers" => $answers, "type" => $question->qtype];
} else {
$qjson[] = ["question" => $questiontext, "answers" => $answers, "type" => $question->qtype,
"single" => $question->qtype == "multichoice" && $question->options->single == 1, ];
}
}
if ($question->qtype == "match") {
$subquestions = [];
foreach ($question->options->subquestions as $subquestion) {
$questiontext = quizgame_cleanup($subquestion->questiontext);
$answertext = quizgame_cleanup($subquestion->answertext);
$subquestions[] = ["question" => $questiontext, "answer" => $answertext];
}
$qjson[] = ["question" => get_string("match", "quiz"), "stems" => $subquestions, "type" => $question->qtype];
}
}
$this->page->requires->js_call_amd('mod_quizgame/quizgame', 'init', [$qjson, $quizgame->id]);
$display = '<div>';
$display .= get_string('howtoplay', 'mod_quizgame') . $this->output->help_icon('howtoplay', 'mod_quizgame', '');
$display .= '</div>';
$display .= '<canvas id="mod_quizgame_game"></canvas>';
$display .= '<audio id="mod_quizgame_sound_laser" preload="auto">'.
'<source src="sound/Laser.wav" type="audio/wav" />'.
'</audio>';
$display .= '<audio id="mod_quizgame_sound_explosion" preload="auto">'.
'<source src="sound/Explosion.wav" type="audio/wav" />'.
'</audio>';
$display .= '<audio id="mod_quizgame_sound_deflect" preload="auto">'.
'<source src="sound/Deflect.wav" type="audio/wav" />'.
'</audio>';
$display .= '<audio id="mod_quizgame_sound_enemylaser" preload="auto">'.
'<source src="sound/EnemyLaser.wav" type="audio/wav" />'.
'</audio>';
$display .= '<div id="button_container">';
$display .= '<input id="mod_quizgame_fullscreen_button" class= "btn btn-secondary" type="button" value="' .
get_string('fullscreen', 'mod_quizgame') . '">';
$display .= '   ';
$display .= html_writer::checkbox('sound', '', false,
get_string('sound', 'mod_quizgame'),
['id' => 'mod_quizgame_sound_on']);
$display .= '</div>';
return $display;
}
/**
* Render the link to access the high scores.
* @param stdClass $quizgame
* @return string
*/
public function render_score_link($quizgame) {
$url = new moodle_url('/mod/quizgame/scores.php', ['id' => $quizgame->id]);
$scorestring = get_string('scoreslink', 'quizgame');
$scorestringhelp = get_string('scoreslinkhelp', 'quizgame');
$display = html_writer::start_tag('div', ['class' => 'quizgame-scores']);
$display .= html_writer::tag('a', $scorestring, ['title' => $scorestringhelp, 'href' => $url]);
$display .= html_writer::end_tag('div');
return $display;
}
}