-
Notifications
You must be signed in to change notification settings - Fork 10
/
question.php
656 lines (612 loc) · 21.6 KB
/
question.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
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
<?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/>.
/**
* wordselect question definition class.
*
* @package qtype_wordselect
* @copyright Marcus Green 2018)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Represents a wordselect question.
*
* @copyright 2016 Marcus Green
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_wordselect_question extends question_graded_automatically_with_countback {
/**
*
* @var number how many items clicked on are not correct answers
*/
public $wrongresponsecount;
/**
*
* @var number how many items clicked on are correct answers
*/
public $rightresponsecount;
/**
* Do all selectable items need to be marked with delimiters
* @var boolean
*/
public $multiword = false;
/**
* is this item selectable. Only makes
* sense if multiword is true
* @var boolean
*/
public $isselectable;
/**
* TODO
* I am not sure this is necessary
* @var string
*/
public $questiontextsplit;
/**
* the place number with p appended, i.e. p0 p1 etc
* @param number $place
* @return string the question-type variable name
*/
public function field($place) {
return 'p' . $place;
}
/**
* Initialise the question. This ought really to be done via the constructor
*
* @param string $questiontext
* @param string $delimitchars short array that gest split in to the 2 dlimiters
*/
public function init($questiontext, $delimitchars) {
$this->questiontext = $questiontext;
$this->delimitchars = $delimitchars;
$l = substr($this->delimitchars, 0, 1);
$r = substr($this->delimitchars, 1, 1);
if (strpos($questiontext, $l . $l) !== false) {
$this->multiword = true;
}
$this->eligables = strip_tags($this->questiontext);
}
/**
* TODO fix this comment as the purpose/return values have probably changed.
* The text with delimiters removed so the user cannot see
* which words are the ones that should be selected. So The cow [jumped]
* becomes The cow jumped
*
* @param boolean $stripdelim (possibly redundant)
* @return array
*/
public function get_words($stripdelim = true) {
$questiontextnodelim = $this->questiontext;
$l = substr($this->delimitchars, 0, 1);
$r = substr($this->delimitchars, 1, 1);
$allwords = array();
/* strip html tags otherwise there is all manner of clickable debris */
$this->selectable = explode(' ', strip_tags($questiontextnodelim));
if (strpos($questiontextnodelim, $l . $l) !== false) {
$this->multiword = true;
$fieldregex = ' #\\'.$l.'+.*?\\'.$r.'+\s*|[^ ]+\s*#';
$questiontextnodelim = $this->pad_angle_brackets($questiontextnodelim);
$matches = preg_replace("# #", " ", $questiontextnodelim);
preg_match_all($fieldregex, $matches, $matches);
$this->items = array();
foreach ($matches[0] as $key => $match) {
$item = new wordselect_item($key, $match, $this->delimitchars, true);
$item->set_is_selectable();
$this->items[] = $item;
}
if ($stripdelim == true) {
$allwords = $this->stripdelim($matches[0]);
} else {
$allwords = $matches[0];
}
} else {
$text = $this->pad_angle_brackets($this->questiontext);
$this->eligables = strip_tags($text);
$regex = "/(\S+\s+)/";
$matches = preg_split($regex, $text, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$this->items = array();
foreach ($matches as $key => $text) {
$item = new wordselect_item($key, $text, $this->delimitchars, $this->multiword);
$item->set_is_selectable($this->eligables);
$this->items[] = $item;
}
$allwords = $matches[0];
}
return $this->items;
}
/**
* The text with delimiters removed so the user cannot see
* which words are the ones that should be selected. So The cow [jumped]
* becomes The cow jumped
*
* @param string $text
* @return string
*/
public function stripdelim($text) {
$l = substr($this->delimitchars, 0, 1);
$r = substr($this->delimitchars, 1, 1);
$matches = preg_replace('/\\' . $l . '/', '', $text);
$matches = preg_replace('/\\' . $r . '/', '', $matches);
return $matches;
}
/**
* Confirm if the the provided haystack has a first character
* matching the given needle. Handy for checking if the first
* character is a delimiter.
*
* @param string $needle
* @param string $haystack
* @return string
*/
public function startswith($needle, $haystack) {
return preg_match('/^' . preg_quote($needle, '/') . '/', $haystack);
}
/**
* Add one space to the pointy end of angle brackets.
* This means that text within table fields can be set
* as selectable. This ensures the td contents is split
* from the td. Only makes sense in multi word mode (selectables
* must be marked.
*
* @param string $questiontext
* @return string
*/
public static function pad_angle_brackets($questiontext) {
// Put a space before and after tags so they get split as words.
$text = str_replace('>', '> ', $questiontext);
$text = str_replace('<', ' <', $text);
return $text;
}
/**
* Part of an experiment, can probably be deleted
*
* @param string $questiontext
* @return string
*/
public function get_unselectable_words($questiontext) {
$questiontext = $this->get_questiontext_exploded($questiontext);
$allwords = preg_split('/[\s\n]/', $questiontext);
$unselectable = array();
$started = false;
foreach ($allwords as $key => $word) {
$start = substr($word, 0, 1);
$len = strlen($word);
$end = substr($word, $len - 1, $len);
if ($start == "*") {
$started = true;
}
if ($end == "*") {
$started = false;
$unselectable[$key] = $word;
}
if ($started == true) {
$unselectable[$key] = $word;
}
}
return $unselectable;
}
/**
* Put a space before and after tags so they get split as words
* This allows the use of tables amongst other html things
*
* @param string $questiontext
* @return string
*/
public static function get_questiontext_exploded($questiontext) {
$text = str_replace('>', '> ', $questiontext);
$text = str_replace('<', ' <', $text);
return $text;
}
/**
*
* Split the question text into words delimited by spaces
* then return an array of all the words that are correct
* i.e. surrounded by the delimit chars. Note that
* word in this context means any string that can be separated
* by a space marker so that will include html etc
* @param string $questiontext raw text of question with delim
* @param string $delimitchars delimiting characters e.g. [ and ]
* @return array index places in array of correct words
*/
public function get_correct_places($questiontext, $delimitchars) {
$correctplaces = array();
$items = $this->get_words(false);
$l = substr($delimitchars, 0, 1);
$r = substr($delimitchars, 1, 1);
if ($this->multiword == true) {
$regex = '/\\' . $l . '\\' . $l . '.*\\' . $r . '\\' . $r . '/';
} else {
$regex = '/\\' . $l . '.*\\' . $r . '/';
}
foreach ($items as $key => $item) {
$word = $item->get_text();
if (preg_match($regex, $word)) {
$correctplaces[] = $key;
}
}
return $correctplaces;
}
/**
* Return an array of the question type variables that could be submitted
* as part of a question of this type, with their types, so they can be
* properly cleaned.
* @return array variable name => PARAM_... constant.
*/
public function get_expected_data() {
$wordcount = count($this->get_words());
for ($key = 0; $key < $wordcount; $key++) {
$data['p' . $key] = PARAM_RAW_TRIMMED;
}
return $data;
}
/**
*
* summary of response shown in the responses report
*
* @param array $response
* @return string allwords
*/
public function summarise_response(array $response) {
$summary = '';
$allwords = $this->get_words();
foreach ($response as $index => $value) {
$summary .= " " . $allwords[substr($index, 1)]->get_without_delim() . " ";
}
return $summary;
}
/**
* At runtime, decide if a word has been clicked on to select
*
* @param number $place
* @param array $response
* @return boolean
*/
public function is_word_selected($place, $response) {
$responseplace = 'p' . $place;
if (isset($response[$responseplace]) && (($response[$responseplace] == "on" ) || ($response[$responseplace] == "true" ) )) {
return true;
} else {
return false;
}
}
/**
*
* Have any words been selected?
*
* @param array $response
* @return boolean
*/
public function is_complete_response(array $response) {
foreach ($response as $item) {
if ($item == "on") {
return true;
}
}
return false;
}
/**
*
* Get string validation to display for user.
*
* @param array $response
* @return string
*/
public function get_validation_error(array $response) {
if (! $this->is_complete_response($response)) {
return get_string('pleaseselectananswer', 'qtype_wordselect');
}
return '';
}
/**
* if you are moving from viewing one question to another this will
* discard the processing if the answer has not changed. If you don't
* use this method it will constantantly generate new question steps and
* the question will be repeatedly set to incomplete. This is a comparison of
* the equality of two arrays. Without this deferred feedback behaviour probably
* wont work.
*
* @param array $prevresponse
* @param array $newresponse
* @return boolean
*/
public function is_same_response(array $prevresponse, array $newresponse) {
if ($prevresponse === $newresponse) {
return true;
} else {
return false;
}
}
/**
* contains the a response that would get full marks.
* used in preview mode. If this doesn't return a
* correct value the button labeled "Fill in correct response"
* in the preview form will not work. This value gets written
* into the rightanswer field of the question_attempts table
* when a quiz containing this question starts.
*
* @return question_answer
*/
public function get_correct_response() {
$correctplaces = $this->get_correct_places($this->questiontext, $this->delimitchars);
$correctresponse = array();
foreach ($correctplaces as $place) {
$correctresponse['p' . $place] = 'on';
}
return $correctresponse;
}
/**
* Checks whether the users is allow to be served a particular file.
* TODO Work out why this is this necessary in the sense of what does it
* do that the parent version does not do
*
* @param question_attempt $qa the question attempt being displayed.
* @param question_display_options $options the options that control display of the question.
* @param string $component the name of the component we are serving files for.
* @param string $filearea the name of the file area.
* @param array $args the remaining bits of the file path.
* @param bool $forcedownload whether the user must be forced to download the file.
* @return bool true if the user can access this file.
*/
public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
if ($component == 'question' && $filearea == 'answerfeedback') {
$currentanswer = $qa->get_last_qt_var('answer');
$answer = $this->get_matching_answer(array('answer' => $currentanswer));
$answerid = reset($args); // Itemid is answer id.
return $options->feedback && $answer && $answerid == $answer->id;
} else if ($component == 'question' && $filearea == 'hint') {
return $this->check_hint_file_access($qa, $options, $args);
} else if ($component == 'qtype_wordselect' && $filearea == 'introduction') {
$question = $qa->get_question();
if ($question->introduction > "") {
return true;
}
} else {
return parent::check_file_access($qa, $options, $component, $filearea, $args, $forcedownload);
}
}
/**
* Is this place correct and so get a mark if selected
*
* @param number $correctplaces
* @param number $place
* @return boolean
*/
public function is_correct_place($correctplaces, $place) {
if (in_array($place, $correctplaces)) {
return true;
} else {
return false;
}
}
/**
* Complete grade for this attempt at the question
*
* @param array $response responses, as returned by
* {@link question_attempt_step::get_qt_data()}.
* @return array (number, integer) the fraction, and the state.
*/
public function grade_response(array $response) {
$totalscore = 0;
$correctplaces = $this->get_correct_places($this->questiontext, $this->delimitchars);
$this->wrongresponsecount = $this->get_wrong_responsecount($correctplaces, $response);
foreach ($correctplaces as $place) {
if (isset($response['p' . $place])) {
if (( $response['p' . $place] === 'on') || ( $response['p' . $place] === 'true')) {
$this->rightresponsecount++;
}
}
}
$wrongfraction = @($this->wrongresponsecount / count($correctplaces));
$fraction = @($this->rightresponsecount / count($correctplaces));
$fraction = max(0, $fraction - $wrongfraction);
$grade = array($fraction, question_state::graded_state_for_fraction($fraction));
return $grade;
}
/**
*
* Not called in interactive mode
*
* @param array $responses
* @param int $totaltries doesn't seem to be used
* @return int
*/
public function compute_final_grade($responses, $totaltries) {
$totalscore = 0;
$correctplaces = $this->get_correct_places($this->questiontext, $this->delimitchars);
$wrongresponsecount = $this->get_wrong_responsecount($correctplaces, $responses[0]);
foreach ($correctplaces as $place) {
$lastwrongindex = -1;
$finallyright = false;
foreach ($responses as $i => $response) {
if (!array_key_exists(('p' . $place), $response)) {
$lastwrongindex = $i;
$finallyright = false;
continue;
} else {
$finallyright = true;
}
}
if ($finallyright) {
$totalscore += max(0, 1 - ($lastwrongindex + 1) * $this->penalty);
}
}
$wrongfraction = @($wrongresponsecount / count($correctplaces));
$totalscore = $totalscore / count($correctplaces);
$totalscore = max(0, $totalscore - $wrongfraction);
return $totalscore;
}
/**
* Used when calculating the final grade
* @param array $correctplaces
* @param array $responses
* @return int
*/
public function get_wrong_responsecount($correctplaces, $responses) {
$wrongresponsecount = 0;
foreach ($responses as $key => $value) {
/* chop off the leading p */
$place = substr($key, 1);
/* if its not in the correct places and it is turned on */
if (!in_array($place, $correctplaces) && ($value == 'on')) {
$wrongresponsecount++;
}
}
return $wrongresponsecount;
}
}
/**
* items that will be processed by the question type. Typically this is a word
* or a group of words
*/
class wordselect_item {
/**
* characters that delimit a word or chunk of words
* @var string
*/
private $delimitchars;
/**
* left delimiter
* @var string
*/
private $l;
/**
* right delimiter
* @var string
*/
private $r;
/**
* question instance id
* @var int
*/
private $id;
/**
* item text including any delimiter
* @var string
*/
private $text;
/**
* do selectables have to be marked
* @var boolean
*/
private $multiword;
/**
* is this item selectable. Only makes
* sense if multiword is true
* @var boolean
*/
public $isselectable;
/**
* Initialise this instance of question chunk
* @param int $id
* @param string $text
* @param string $delimitchars
* @param boolean multiword
*/
public function __construct($id, $text, $delimitchars, $multiword = false) {
$this->l = substr($delimitchars, 0, 1);
$this->r = substr($delimitchars, 1, 1);
$this->id = $id;
$this->text = $text;
$this->delimitchars = $delimitchars;
$this->multiword = $multiword;
}
/**
* Get white space after the "word" or group of words delimited
* by double delimiting characters
*
* @param string $eligables
* @return string
*/
public function get_space_after($eligables) {
if ($this->text == "") {
return "";
}
if (strpos($eligables, $this->text) == false) {
/* if this is nevery eligable for selection (typically
* a piece of html e.g. <p>, then tag the original space back on to
* the end of it
*/
preg_match('/\s+/', $this->text, $matches);
if (isset($matches[0])) {
return $matches[0];
} else {
return "";
}
} else {
preg_match('/\s+/', $this->text, $matches);
if (isset($matches[0])) {
$len = strlen($matches[0]);
if ($len > 1) {
return " " . str_repeat(' ', $len);
} else {
return " ";
}
} else {
return "";
}
}
}
/**
* Work out which strings could be selectable typically anything that
* is not an HTML tag. $eligables seems to be an awkward name, it could
* have been called something like non-html but that is also awkward
* and might be a limitation in the future if some other reason for text
* being non eligable turns up.
* @param string $eligables
*/
public function set_is_selectable($eligables = "") {
if ($this->multiword == true) {
$regex = '/\\' . $this->l . '([^\\' . $this->l . '\\' . $this->r . ']*)\\' . $this->r . '/';
if (preg_match($regex, $this->text) > 0) {
$this->isselectable = true;
}
} else {
if (($eligables > "") && ($this->get_without_delim() > "")) {
if (strpos($eligables, $this->get_without_delim()) !== false) {
if ($this->multiword == false) {
$this->isselectable = true;
} else {
$regex = '/\\' . $this->l . '([^\\' . $this->l . '\\' . $this->r . ']*)\\' . $this->r . '/';
if (preg_match($regex, $this->text) > 0) {
$this->isselectable = true;
}
}
}
}
}
}
/**
* Get chunk of questiontext for this item that will include
* any delimiter.
*
* @return string
*/
public function get_text() {
return $this->text;
}
/**
* Get the word (or set of words) without the delimiters
* So [cat] will be returned as cat
* @return string
*/
public function get_without_delim() {
$matches = preg_replace('/\\' . $this->l . '/', '', $this->text);
$matches = preg_replace('/\\' . $this->r . '/', '', $matches);
/* trim trailing html space characters */
$matches = preg_replace("#(^( |\s)+|( |\s)+$)#", "", $matches);
return $matches;
}
}