-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmask_type_qcm.class.php
151 lines (125 loc) · 6.38 KB
/
mask_type_qcm.class.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
<?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/>.
/**
* Display masks plugin frame
*
* @copyright 2016 Edunao SAS ([email protected])
* @author Sadge ([email protected])
* @package mod_masks
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_masks;
defined('MOODLE_INTERNAL') || die;
require_once(dirname(__FILE__).'/mask_type.class.php');
class mask_type_qcm extends mask_type{
// -------------------------------------------------------------------------
// data
private $maskType = 'qcm';
private $dbInterface = null;
private $fields = null;
// -------------------------------------------------------------------------
// basics
public function __construct(){
// Establish database connection
require_once(dirname(__FILE__).'/database_interface.class.php');
$this->dbInterface = new database_interface;
// Define the fields that are to appear in the question editing form
$this->fields = array(
'question' => FIELD_BIGTEXTAREA + FIELD_REQUIRED,
'goodanswer' => FIELD_TEXT + FIELD_REQUIRED,
'badanswer0' => FIELD_TEXT + FIELD_REQUIRED,
'badanswer1' => FIELD_TEXT + FIELD_NOHEADING,
'badanswer2' => FIELD_TEXT + FIELD_NOHEADING,
'badanswer3' => FIELD_TEXT + FIELD_NOHEADING,
'goodanswerhint' => FIELD_TEXTAREA + FIELD_FEEDBACK,
'badanswerhint' => FIELD_TEXTAREA + FIELD_FEEDBACK,
'userhint' => FIELD_TEXTAREA + FIELD_HINT,
);
}
// -------------------------------------------------------------------------
// mask_type API
public function onNewMask( $id, $pageId ){
// delegate work to generic method in base class
$this->doNewMask( $id, $pageId, $this->maskType, $this->fields, $this->dbInterface, MASK_FLAGS_QUESTION );
}
public function onEditMask( $id, $maskId, $questionId, $questionData ){
// delegate work to generic method in base class
$this->doEditMask( $id, $maskId, $questionId, $questionData, $this->maskType, $this->fields, $this->dbInterface );
}
public function onClickMask( $questionId, $questionData, $hiddenFields, $isLastQuestion ){
// identify the set of available answers
$choices = array( 'goodanswer', 'badanswer0' );
for ($i = 1; array_key_exists('badanswer'.$i, $this->fields); ++$i ){
$propName = 'badanswer'.$i;
if ( property_exists( $questionData, $propName ) && ! empty( $questionData->$propName ) ){
$choices[] = $propName;
}
}
// build an indirection table for shuffling the questions with
$choiceIndex = array();
for ($i = 0; $i < count( $choices ); ++$i){
$choiceIndex[] = $i;
}
// setup a deterministic random seed based on the user's user id
global $USER;
$seed = intval($USER->id) + intval($questionId);
srand($seed);
// shuffle the questions
for ($i = count( $choices ); $i > 1; --$i){
$rand = rand();
$slot = $rand % $i;
$hold = $choiceIndex[ $slot ];
$choiceIndex[ $slot ] = $choiceIndex[ $i - 1 ];
$choiceIndex[ $i - 1 ] = $hold;
}
// has the user submitted an answer?
if ( ! array_key_exists( 'answer', $_GET ) ){
// no, so we need to render the question
// prepare an html blob for the answer entry
$answerHTML = '';
for ($i = 0; $i < count( $choices ); ++$i){
$choice = $choiceIndex[ $i ];
$answerField = $choices[ $choice ];
$answer = $questionData->$answerField;
$answerHTML .= \html_writer::start_tag( 'label', array( 'class' => 'option' ) );
$answerHTML .= \html_writer::tag( 'input', '', array( 'type' => 'radio', 'name' => 'answer', 'value' => $i , 'required' => 'required' ) );
$answerHTML .= \html_writer::span( $answer );
$answerHTML .= \html_writer::end_tag( 'label' );
}
// render the page (updating database etc as we do)
$hintText = property_exists( $questionData, 'userhint' ) ? $questionData->userhint : '';
$questionText = $questionData->question;
$this->renderQuestionPage( $hintText, $questionText, $answerHTML, $hiddenFields, $this->dbInterface, $questionId );
// return false as we don't have a result to evaluate yet
return false;
} else {
// compare the answer to the correct answer
$answer = $_GET[ 'answer' ];
$answerIsCorrect = ( ( $answer >= 0 ) && ( $answer < count( $choiceIndex ) ) && ( $choiceIndex[ $answer ] == 0 ) );
// render the response page (updating database etc as we do)
$goodAnswerResponse = ( property_exists( $questionData, 'goodanswerhint' ) ) ? $questionData->goodanswerhint : '';
$badAnswerResponse = ( property_exists( $questionData, 'badanswerhint' ) ) ? $questionData->badanswerhint : '';
$hintText = ( property_exists( $questionData, 'userhint' ) ) ? $questionData->userhint : '';
if ( $badAnswerResponse == '' ){
$badAnswerResponse = $hintText;
$hintText = '';
}
$this->renderAnswerResponsePage( $answerIsCorrect, $goodAnswerResponse, $badAnswerResponse, $hintText, $this->dbInterface, $questionId, $isLastQuestion );
// return true or false to represent 'question passed' of not
return $answerIsCorrect;
}
}
}