-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathquestiontype.php
1022 lines (931 loc) · 47.9 KB
/
questiontype.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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?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/>.
/**
* Moodle formulas question type class.
*
* @copyright © 2010-2011 Hon Wai, Lau
* @author Hon Wai, Lau <lau65536@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License version 3
* @package qtype_formulas
*/
use qtype_formulas\answer_unit_conversion;
use qtype_formulas\unit_conversion_rules;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/questionlib.php');
require_once($CFG->dirroot . '/question/engine/lib.php');
require_once("$CFG->dirroot/question/type/formulas/variables.php");
require_once("$CFG->dirroot/question/type/formulas/answer_unit.php");
require_once("$CFG->dirroot/question/type/formulas/conversion_rules.php");
require_once($CFG->dirroot . '/question/type/formulas/question.php');
/**
* The formulas question class
*
* TODO give an overview of how the class works here.
*/
class qtype_formulas extends question_type {
private $qv;
public function __construct() {
$this->qv = new qtype_formulas\variables();
}
/**
* column names of qtype_formulas_answers table (apart from id and questionid)
* WARNING qtype_formulas_answers is NOT an extension of answers table
* so we can't use extra_answer_fields here.
* subqtext, subqtextformat, feedback and feedbackformat and all part's combined
* feedback fields are not included as their handling is special
* @return array.
*/
public function part_tags() {
return array('placeholder', 'answermark', 'answertype', 'numbox', 'vars1', 'answer', 'answernotunique', 'vars2', 'correctness'
, 'unitpenalty', 'postunit', 'ruleid', 'otherrule');
}
/**
* If you use extra_question_fields, overload this function to return question id field name
* in case you table use another name for this column
*/
public function questionid_column_name() {
return 'questionid';
}
/**
* If your question type has a table that extends the question table, and
* you want the base class to automatically save, backup and restore the extra fields,
* override this method to return an array where the first element is the table name,
* and the subsequent entries are the column names (apart from id and questionid).
*
* @return mixed array as above, or null to tell the base class to do nothing.
*/
public function extra_question_fields() {
return array('qtype_formulas_options', 'varsrandom', 'varsglobal', 'answernumbering');
}
/**
* Move all the files belonging to this question from one context to another.
* @param int $questionid the question being moved.
* @param int $oldcontextid the context it is moving from.
* @param int $newcontextid the context it is moving to.
*/
public function move_files($questionid, $oldcontextid, $newcontextid) {
$fs = get_file_storage();
parent::move_files($questionid, $oldcontextid, $newcontextid);
$fs->move_area_files_to_new_context($oldcontextid,
$newcontextid, 'qtype_formulas', 'answersubqtext', $questionid);
$fs->move_area_files_to_new_context($oldcontextid,
$newcontextid, 'qtype_formulas', 'answerfeedback', $questionid);
$fs->move_area_files_to_new_context($oldcontextid,
$newcontextid, 'qtype_formulas', 'partcorrectfb', $questionid);
$fs->move_area_files_to_new_context($oldcontextid,
$newcontextid, 'qtype_formulas', 'partpartiallycorrectfb', $questionid);
$fs->move_area_files_to_new_context($oldcontextid,
$newcontextid, 'qtype_formulas', 'partincorrectfb', $questionid);
$this->move_files_in_combined_feedback($questionid, $oldcontextid, $newcontextid);
$this->move_files_in_hints($questionid, $oldcontextid, $newcontextid);
}
/**
* Delete all the files belonging to this question.
* @param int $questionid the question being deleted.
* @param int $contextid the context the question is in.
*/
protected function delete_files($questionid, $contextid) {
$fs = get_file_storage();
parent::delete_files($questionid, $contextid);
$this->delete_files_in_combined_feedback($questionid, $contextid);
$this->delete_files_in_hints($questionid, $contextid);
$fs->delete_area_files($contextid, 'question', 'correctfeedback', $questionid);
$fs->delete_area_files($contextid, 'question', 'partiallycorrectfeedback', $questionid);
$fs->delete_area_files($contextid, 'question', 'incorrectfeedback', $questionid);
$fs->delete_area_files($contextid, 'qtype_formulas', 'answersubqtext', $questionid);
$fs->delete_area_files($contextid, 'qtype_formulas', 'answerfeedback', $questionid);
$fs->delete_area_files($contextid, 'qtype_formulas', 'partcorrectfb', $questionid);
$fs->delete_area_files($contextid, 'qtype_formulas', 'partpartiallycorrectfb', $questionid);
$fs->delete_area_files($contextid, 'qtype_formulas', 'partincorrectfb', $questionid);
}
/**
* Loads the question type specific options for the question.
*
* This function loads any question type specific options for the
* question from the database into the question object. This information
* is placed in the $question->options field. A question type is
* free, however, to decide on a internal structure of the options field.
* @return bool Indicates success or failure.
* @param object $question The question object for the question. This object
* should be updated to include the question type
* specific information (it is passed by reference).
*/
public function get_question_options($question) {
global $DB;
$question->options = $DB->get_record('qtype_formulas_options', ['questionid' => $question->id]);
if ($question->options === false) {
// If this has happened, then we have a problem.
// For the user to be able to edit or delete this question, we need options.
debugging("Formulas question ID {$question->id} was missing an options record. Using default.", DEBUG_DEVELOPER);
$question->options = $this->create_default_options($question);
}
parent::get_question_options($question);
$question->options->answers = $DB->get_records(
'qtype_formulas_answers',
array('questionid' => $question->id), 'partindex ASC'
);
$question->options->numpart = count($question->options->answers);
$question->options->answers = array_values($question->options->answers);
return true;
}
/**
* Create a default options object for the provided question.
*
* @param object $question The question we are working with.
* @return object The options object.
*/
protected function create_default_options($question) {
// Create a default question options record.
$options = new stdClass();
$options->questionid = $question->id;
$options->varsrandom = '';
$options->varsglobal = '';
// Get the default strings and just set the format.
$options->correctfeedback = get_string('correctfeedbackdefault', 'question');
$options->correctfeedbackformat = FORMAT_HTML;
$options->partiallycorrectfeedback = get_string('partiallycorrectfeedbackdefault', 'question');;
$options->partiallycorrectfeedbackformat = FORMAT_HTML;
$options->incorrectfeedback = get_string('incorrectfeedbackdefault', 'question');
$options->incorrectfeedbackformat = FORMAT_HTML;
$options->answernumbering = 'none';
$options->shownumcorrect = 0;
return $options;
}
/**
* Saves question-type specific options
*
* This is called by {@link save_question()} to save the question-type specific data
* @return object $result->error or $result->noticeyesno or $result->notice
* @param object $question This holds the information from the editing form,
* it is not a standard question object.
*/
public function save_question_options($question) {
global $DB;
$context = $question->context;
$result = new stdClass();
$oldanswers = $DB->get_records('qtype_formulas_answers', array('questionid' => $question->id), 'partindex ASC');
try {
$filtered = $this->validate($question); // Data from the web input interface should be validated.
if (count($filtered->errors) > 0) { // There may be errors from import or restore.
throw new Exception('Format error! Probably imported/restored formulas questions have been damaged.');
}
$answersorder = $this->reorder_answers($question->questiontext, $filtered->answers);
// Reorder the answers, so that answer's order is the same as the placeholder's order in questiontext.
$newanswers = array();
foreach ($answersorder as $newloc) {
$newanswers[] = $filtered->answers[$newloc];
}
$idcount = 0;
foreach ($newanswers as $i => $ans) {
$ans->partindex = $i;
$ans->questionid = $question->id;
// Save all editors content (arrays).
$subqtextarr = $ans->subqtext;
$ans->subqtext = $subqtextarr['text'];
$ans->subqtextformat = $subqtextarr['format'];
$feedbackarr = $ans->feedback;
$ans->feedback = $feedbackarr['text'];
$ans->feedbackformat = $feedbackarr['format'];
$correctfbarr = $ans->partcorrectfb;
$ans->partcorrectfb = $correctfbarr['text'];
$ans->partcorrectfbformat = $correctfbarr['format'];
$partiallycorrectfbarr = $ans->partpartiallycorrectfb;
$ans->partpartiallycorrectfb = $partiallycorrectfbarr['text'];
$ans->partpartiallycorrectfbformat = $partiallycorrectfbarr['format'];
$incorrectfbarr = $ans->partincorrectfb;
$ans->partincorrectfb = $incorrectfbarr['text'];
$ans->partincorrectfbformat = $incorrectfbarr['format'];
// Update an existing answer if possible.
$answer = array_shift($oldanswers);
if (!$answer) {
$answer = new stdClass();
$answer->questionid = $question->id;
$answer->answermark = 1;
$answer->numbox = 1;
$answer->answer = '';
$answer->answernotunique = 1;
$answer->correctness = '';
$answer->ruleid = 1;
$answer->trialmarkseq = '';
$answer->subqtextformat = 0;
$answer->feedbackformat = 0;
$answer->partcorrectfb = '';
$answer->partcorrectfbformat = 0;
$answer->partpartiallycorrectfb = '';
$answer->partpartiallycorrectfbformat = 0;
$answer->partincorrectfb = '';
$answer->partincorrectfbformat = 0;
$ans->id = $DB->insert_record('qtype_formulas_answers', $answer);
} else {
$ans->id = $answer->id;
}
$ans->subqtext = $this->import_or_save_files($subqtextarr, $context, 'qtype_formulas', 'answersubqtext', $ans->id);
$ans->feedback = $this->import_or_save_files($feedbackarr, $context, 'qtype_formulas', 'answerfeedback', $ans->id);
$ans->partcorrectfb = $this->import_or_save_files(
$correctfbarr, $context,
'qtype_formulas',
'partcorrectfb',
$ans->id
);
$ans->partpartiallycorrectfb = $this->import_or_save_files(
$partiallycorrectfbarr,
$context,
'qtype_formulas',
'partpartiallycorrectfb',
$ans->id
);
$ans->partincorrectfb = $this->import_or_save_files(
$incorrectfbarr,
$context,
'qtype_formulas',
'partincorrectfb',
$ans->id
);
$DB->update_record('qtype_formulas_answers', $ans);
}
// Delete any left over old answer records.
$fs = get_file_storage();
foreach ($oldanswers as $oldanswer) {
$fs->delete_area_files($context->id, 'qtype_formulas', 'answersubqtext', $oldanswer->id);
$fs->delete_area_files($context->id, 'qtype_formulas', 'answerfeedback', $oldanswer->id);
$fs->delete_area_files($context->id, 'qtype_formulas', 'partcorrectfb', $oldanswer->id);
$fs->delete_area_files($context->id, 'qtype_formulas', 'partpartiallycorrectfb', $oldanswer->id);
$fs->delete_area_files($context->id, 'qtype_formulas', 'partincorrectfb', $oldanswer->id);
$DB->delete_records('qtype_formulas_answers', array('id' => $oldanswer->id));
}
} catch (Exception $e) {
return (object)array('error' => $e->getMessage());
}
// Save the question options.
$options = $DB->get_record('qtype_formulas_options', array('questionid' => $question->id));
if (!$options) {
$options = new stdClass();
$options->questionid = $question->id;
$options->correctfeedback = '';
$options->partiallycorrectfeedback = '';
$options->incorrectfeedback = '';
$options->answernumbering = 'none';
$options->id = $DB->insert_record('qtype_formulas_options', $options);
}
$extraquestionfields = $this->extra_question_fields();
array_shift($extraquestionfields);
foreach ($extraquestionfields as $extra) {
if (property_exists($question, $extra)) {
$options->$extra = $question->$extra;
}
}
$options = $this->save_combined_feedback_helper($options, $question, $context, true);
$DB->update_record('qtype_formulas_options', $options);
$this->save_hints($question, true);
return true;
}
// Override the parent save_question in order to change the defaultmark.
public function save_question($question, $form) {
$form->defaultmark = 0;
foreach ($form->answermark as $key => $data) {
if (trim($form->answermark[$key]) == '' || trim($form->answer[$key]) == '') {
continue;
}
// Question's default mark is the total of all non empty parts's marks.
$form->defaultmark += $form->answermark[$key];
}
// Add the unitpenalty and ruleid to each part, from the global option.
$form->unitpenalty = array_fill(0, count($form->answer), $form->globalunitpenalty);
$form->ruleid = array_fill(0, count($form->answer), $form->globalruleid);
$question = parent::save_question($question, $form);
return $question;
}
protected function make_hint($hint) {
return question_hint_with_parts::load_from_record($hint);
}
// Delete question from the question-type specific tables with $questionid.
public function delete_question($questionid, $contextid) {
global $DB;
// The qtype_formulas records are deleted in parent as extra_question_fields records.
// But we need to take care of qtype_formulas_answers as they are not real answers.
$DB->delete_records('qtype_formulas_answers', array('questionid' => $questionid));
parent::delete_question($questionid, $contextid);
}
/**
* Split the main question text according to the placeholders.
*
* @param string $questiontext The main question text containing all placeholders.
* @param array $answers array of answers with placeholder (can be empty string)
* @return array of text fragments with count($answers) + 1 elements.
*/
public function split_questiontext($questiontext, $answers) {
// TODO Simplify this now that answers are in right order in data structure.
$locations = array(); // Store the (scaled) location of the *named* placeholder in the main text.
foreach ($answers as $idx => $answer) {
if (strlen($answer->placeholder) != 0) {
$locations[] = 1000 * strpos($questiontext, '{'.$answer->placeholder.'}') + $idx; // Store the pair (location, idx).
}
}
sort($locations); // Performs stable sort of location and answerorder pairs.
$fragments = array();
foreach ($locations as $i => $location) {
$answerorder = $location % 1000;
list($fragments[$i], $questiontext) = explode('{'.$answers[$answerorder]->placeholder.'}', $questiontext);
}
foreach ($answers as $answer) {
if (strlen($answer->placeholder) == 0) { // Add the parts with empty placeholder at the end.
$fragments[] = $questiontext;
$questiontext = '';
}
}
$fragments[] = $questiontext; // Add the post-question text, if any.
return $fragments;
}
/**
* Initialise the question_definition fields.
* @param question_definition $question the question_definition we are creating.
* @param object $questiondata the question data loaded from the database.
*/
protected function initialise_question_instance(question_definition $question, $questiondata) {
parent::initialise_question_instance($question, $questiondata);
$question->parts = array();
if (!empty($questiondata->options->answers)) {
foreach ($questiondata->options->answers as $ans) {
$part = new qtype_formulas_part();
foreach ($ans as $key => $value) {
$part->{$key} = $value;
}
$question->parts[$part->partindex] = $part;
}
}
$question->varsrandom = $questiondata->options->varsrandom;
$question->varsglobal = $questiondata->options->varsglobal;
$question->answernumbering = $questiondata->options->answernumbering;
$question->qv = new qtype_formulas\variables();
$question->numpart = $questiondata->options->numpart;
if ($question->numpart != 0) {
$question->fractions = array_fill(0, $question->numpart, 0);
$question->anscorrs = array_fill(0, $question->numpart, 0);
$question->unitcorrs = array_fill(0, $question->numpart, 0);
}
$question->textfragments = $this->split_questiontext($question->questiontext, $question->parts);
$this->initialise_combined_feedback($question, $questiondata, true);
}
/**
* This method should return all the possible types of response that are
* recognised for this question.
*
* The question is modelled as comprising one or more subparts. For each
* subpart, there are one or more classes that that students response
* might fall into, each of those classes earning a certain score.
*
* For example, in a shortanswer question, there is only one subpart, the
* text entry field. The response the student gave will be classified according
* to which of the possible $question->options->answers it matches.
*
* For the matching question type, there will be one subpart for each
* question stem, and for each stem, each of the possible choices is a class
* of student's response.
*
* A response is an object with two fields, ->responseclass is a string
* presentation of that response, and ->fraction, the credit for a response
* in that class.
*
* Array keys have no specific meaning, but must be unique, and must be
* the same if this function is called repeatedly.
*
* @param object $question the question definition data.
* @return array keys are part partindex, values are arrays of possible
* responses to that question part.
*/
public function get_possible_responses($questiondata) {
$resp = array();
$q = $this->make_question($questiondata);
foreach ($q->parts as $part) {
if ($part->postunit == '') {
$resp[$part->partindex] = array(
'wrong' => new question_possible_response(
'Wrong', 0),
'right' => new question_possible_response(
'Right', 1),
null => question_possible_response::no_response()
);
} else {
$resp[$part->partindex] = array(
'wrong' => new question_possible_response(
'Wrong', 0),
'right' => new question_possible_response(
'Right', 1),
'wrongvalue' => new question_possible_response(
'Wrong value right unit', 0),
'wrongunit' => new question_possible_response(
'Right value wrong unit', 1 - $part->unitpenalty),
null => question_possible_response::no_response()
);
}
}
return $resp;
}
/**
* Imports the question from Moodle XML format.
*
* @param $xml structure containing the XML data
* @param $fromform question object to fill: ignored by this function (assumed to be null)
* @param $format format class exporting the question
* @param $extra extra information (not required for importing this question in this format)
*/
public function import_from_xml($xml, $fromform, qformat_xml $format, $extra=null) {
// Return if data type is not our own one.
if (!isset($xml['@']['type']) || $xml['@']['type'] != $this->name()) {
return false;
}
// Import the common question headers and set the corresponding field,
// Unfortunately we can't use the parent method because it will try to import answers,
// and fails as formulas "answers" are not real answers but formulas question parts !!
$fromform = $format->import_headers($xml);
$fromform->qtype = $this->name();
$format->import_combined_feedback($fromform, $xml, true);
$format->import_hints($fromform, $xml, true);
$fromform->varsrandom = $format->getpath($xml, array('#', 'varsrandom', 0, '#', 'text', 0, '#'), '', true);
$fromform->varsglobal = $format->getpath($xml, array('#', 'varsglobal', 0, '#', 'text', 0, '#'), '', true);
$fromform->answernumbering = $format->getpath($xml, array('#', 'answernumbering', 0, '#', 'text', 0, '#'), 'none', true);
// Loop over each answer block found in the XML.
$tags = $this->part_tags();
$anscount = 0;
foreach ($xml['#']['answers'] as $answer) {
$partindex = $format->getpath($answer, array('#', 'partindex', 0 , '#' , 'text' , 0 , '#'), false);
if ($partindex) {
$fromform->partindex[$anscount] = $partindex;
}
foreach ($tags as $tag) {
// Older questions do not have this field, so we do not want to issue an error message.
// Also, for maximum backwards compatibility, we set the default value to 1. With this,
// nothing changes for old questions.
if ($tag === 'answernotunique') {
$ifnotexists = '';
$default = '1';
} else {
$ifnotexists = 'error';
$default = '0';
}
$fromform->{$tag}[$anscount] = $format->getpath(
$answer,
array('#', $tag, 0 , '#' , 'text' , 0 , '#'),
$default,
false,
$ifnotexists
);
}
$subqxml = $format->getpath($answer, array('#', 'subqtext', 0), array());
$fromform->subqtext[$anscount] = $format->import_text_with_files($subqxml,
array(), '', $format->get_format($fromform->questiontextformat));
$feedbackxml = $format->getpath($answer, array('#', 'feedback', 0), array());
$fromform->feedback[$anscount] = $format->import_text_with_files($feedbackxml,
array(), '', $format->get_format($fromform->questiontextformat));
$feedbackxml = $format->getpath($answer, array('#', 'correctfeedback', 0), array());
$fromform->partcorrectfb[$anscount] = $format->import_text_with_files($feedbackxml,
array(), '', $format->get_format($fromform->questiontextformat));
$feedbackxml = $format->getpath($answer, array('#', 'partiallycorrectfeedback', 0), array());
$fromform->partpartiallycorrectfb[$anscount] = $format->import_text_with_files($feedbackxml,
array(), '', $format->get_format($fromform->questiontextformat));
$feedbackxml = $format->getpath($answer, array('#', 'incorrectfeedback', 0), array());
$fromform->partincorrectfb[$anscount] = $format->import_text_with_files($feedbackxml,
array(), '', $format->get_format($fromform->questiontextformat));
++$anscount;
}
$fromform->defaultmark = array_sum($fromform->answermark); // Make the defaultmark consistent if not specified.
return $fromform;
}
/**
* Exports the question to Moodle XML format.
*
* @param $question question to be exported into XML format
* @param $format format class exporting the question
* @param $extra extra information (not required for exporting this question in this format)
* @return text string containing the question data in XML format
*/
public function export_to_xml($question, qformat_xml $format, $extra=null) {
$expout = '';
$fs = get_file_storage();
$contextid = $question->contextid;
$expout .= $format->write_combined_feedback($question->options,
$question->id,
$question->contextid);
$extraquestionfields = $this->extra_question_fields();
array_shift($extraquestionfields);
foreach ($extraquestionfields as $extra) {
$expout .= "<$extra>".$format->writetext($question->options->$extra)."</$extra>\n";
}
$tags = $this->part_tags();
foreach ($question->options->answers as $answer) {
$expout .= "<answers>\n";
$expout .= " <partindex>\n ".$format->writetext($answer->partindex)." </partindex>\n";
foreach ($tags as $tag) {
$expout .= " <$tag>\n ".$format->writetext($answer->$tag)." </$tag>\n";
}
$subqfiles = $fs->get_area_files($contextid, 'qtype_formulas', 'answersubqtext', $answer->id);
$subqtextformat = $format->get_format($answer->subqtextformat);
$expout .= " <subqtext format=\"$subqtextformat\">\n";
$expout .= $format->writetext($answer->subqtext);
$expout .= $format->write_files($subqfiles);
$expout .= " </subqtext>\n";
$fbfiles = $fs->get_area_files($contextid, 'qtype_formulas', 'answerfeedback', $answer->id);
$feedbackformat = $format->get_format($answer->feedbackformat);
$expout .= " <feedback format=\"$feedbackformat\">\n";
$expout .= $format->writetext($answer->feedback);
$expout .= $format->write_files($fbfiles);
$expout .= " </feedback>\n";
$fbfiles = $fs->get_area_files($contextid, 'qtype_formulas', 'partcorrectfb', $answer->id);
$feedbackformat = $format->get_format($answer->partcorrectfbformat);
$expout .= " <correctfeedback format=\"$feedbackformat\">\n";
$expout .= $format->writetext($answer->partcorrectfb);
$expout .= $format->write_files($fbfiles);
$expout .= " </correctfeedback>\n";
$fbfiles = $fs->get_area_files($contextid, 'qtype_formulas', 'partpartiallycorrectfb', $answer->id);
$feedbackformat = $format->get_format($answer->partpartiallycorrectfbformat);
$expout .= " <partiallycorrectfeedback format=\"$feedbackformat\">\n";
$expout .= $format->writetext($answer->partpartiallycorrectfb);
$expout .= $format->write_files($fbfiles);
$expout .= " </partiallycorrectfeedback>\n";
$fbfiles = $fs->get_area_files($contextid, 'qtype_formulas', 'partincorrectfb', $answer->id);
$feedbackformat = $format->get_format($answer->partincorrectfbformat);
$expout .= " <incorrectfeedback format=\"$feedbackformat\">\n";
$expout .= $format->writetext($answer->partincorrectfb);
$expout .= $format->write_files($fbfiles);
$expout .= " </incorrectfeedback>\n";
$expout .= "</answers>\n";
}
return $expout;
}
/**
* Check if placeholders in answers are correct and compatible with questiontext.
*
* @param $questiontext string text of the main question
* @param $answers array of objects only the placeholder is used
* @return $errors array
*/
public function check_placeholder($questiontext, $answers) {
$placeholderformat = '#\w+';
$placeholders = array();
foreach ($answers as $idx => $answer) {
if ( strlen($answer->placeholder) == 0 ) {
continue; // No error if answer's placeholder is empty.
}
$errstr = array();
if ( strlen($answer->placeholder) >= 40 ) {
$errstr[] = get_string('error_placeholder_too_long', 'qtype_formulas');
}
if ( !preg_match('/^'.$placeholderformat.'$/', $answer->placeholder) ) {
$errstr[] = get_string('error_placeholder_format', 'qtype_formulas');
}
if ( array_key_exists($answer->placeholder, $placeholders) ) {
$errstr[] = get_string('error_placeholder_sub_duplicate', 'qtype_formulas');
}
$placeholders[$answer->placeholder] = true;
$count = substr_count($questiontext, '{'.$answer->placeholder.'}');
if ($count < 1) {
$errstr[] = get_string('error_placeholder_missing', 'qtype_formulas');
}
if ($count > 1) {
$errstr[] = get_string('error_placeholder_main_duplicate', 'qtype_formulas');
}
if (!empty($errstr)) {
$errors["placeholder[$idx]"] = implode(' ', $errstr);
}
}
return isset($errors) ? $errors : array();
}
/**
* Check that all required fields have been filled and return the filtered classes of the answers.
*
* @param $form all the input form data
* @return an object with a field 'answers' containing valid answers. Otherwise, the 'errors' field will be set
*/
public function check_and_filter_answers($form) {
// This function is also called when importing a question.
// The answers of imported questions already have their unitpenalty and ruleid set.
$isfromimport = property_exists($form, 'unitpenalty') && property_exists($form, 'ruleid');
$tags = $this->part_tags();
$res = (object)array('answers' => array());
foreach ($form->answermark as $i => $a) {
if ((strlen(trim($form->answermark[$i])) == 0 || strlen(trim($form->answer[$i])) == 0)
&& (strlen(trim($form->subqtext[$i]['text'])) != 0
|| strlen(trim($form->feedback[$i]['text'])) != 0
|| strlen(trim($form->vars1[$i])) != 0
)
) {
$res->errors["answer[$i]"] = get_string('error_answer_missing', 'qtype_formulas');
$skip = true;
}
if (strlen(trim($form->answermark[$i])) == 0 || strlen(trim($form->answer[$i])) == 0) {
continue; // If no mark or no answer, then skip this answer.
}
if (floatval($form->answermark[$i]) <= 0) {
$res->errors["answermark[$i]"] = get_string('error_mark', 'qtype_formulas');
}
$skip = false;
if (strlen(trim($form->correctness[$i])) == 0) {
$res->errors["correctness[$i]"] = get_string('error_criterion', 'qtype_formulas');
$skip = true;
}
if ($skip) {
continue; // If no answer or correctness conditions, it cannot check other parts, so skip.
}
$res->answers[$i] = new stdClass();
$res->answers[$i]->questionid = $form->id;
foreach ($tags as $tag) {
// The unitpenalty and ruleid are set via a global option,
// but stored with each part. When importing questions,
// this is not the case.
if (!$isfromimport && ($tag === 'unitpenalty' || $tag === 'ruleid')) {
$res->answers[$i]->{$tag} = trim($form->{'global' . $tag});
} else {
$res->answers[$i]->{$tag} = trim($form->{$tag}[$i]);
}
}
$subqtext = array();
$subqtext['text'] = $form->subqtext[$i]['text'];
$subqtext['format'] = $form->subqtext[$i]['format'];
if (isset($form->subqtext[$i]['itemid'])) {
$subqtext['itemid'] = $form->subqtext[$i]['itemid'];
}
$res->answers[$i]->subqtext = $subqtext;
$fb = array();
$fb['text'] = $form->feedback[$i]['text'];
$fb['format'] = $form->feedback[$i]['format'];
if (isset($form->feedback[$i]['itemid'])) {
$fb['itemid'] = $form->feedback[$i]['itemid'];
}
$res->answers[$i]->feedback = $fb;
$fb = array();
$fb['text'] = $form->partcorrectfb[$i]['text'];
$fb['format'] = $form->partcorrectfb[$i]['format'];
if (isset($form->partcorrectfb[$i]['itemid'])) {
$fb['itemid'] = $form->partcorrectfb[$i]['itemid'];
}
$res->answers[$i]->partcorrectfb = $fb;
$fb = array();
$fb['text'] = $form->partpartiallycorrectfb[$i]['text'];
$fb['format'] = $form->partpartiallycorrectfb[$i]['format'];
if (isset($form->partpartiallycorrectfb[$i]['itemid'])) {
$fb['itemid'] = $form->partpartiallycorrectfb[$i]['itemid'];
}
$res->answers[$i]->partpartiallycorrectfb = $fb;
$fb = array();
$fb['text'] = $form->partincorrectfb[$i]['text'];
$fb['format'] = $form->partincorrectfb[$i]['format'];
if (isset($form->partincorrectfb[$i]['itemid'])) {
$fb['itemid'] = $form->partincorrectfb[$i]['itemid'];
}
$res->answers[$i]->partincorrectfb = $fb;
}
if (count($res->answers) == 0) {
$res->errors["answermark[0]"] = get_string('error_no_answer', 'qtype_formulas');
}
return $res;
}
// It checks basic errors as well as formula errors by evaluating one instantiation.
public function validate($form) {
$errors = array();
$answerschecked = $this->check_and_filter_answers($form);
if (isset($answerschecked->errors)) {
$errors = array_merge($errors, $answerschecked->errors);
}
$validanswers = $answerschecked->answers;
// The value from the globalunitpenalty field is only used to set
// the penalty for each part. Is has to be validated separately.
// The same is true for the globalruleid, but as this is a select
// field, there is no need to validate it.
// If we are importing a question, there will be no globalunitpenalty or globalruleid,
// because the question will already have those values in its parts.
// No validation is needed in that case, as the parts have been checked before.
if (!property_exists($form, 'unitpenalty') || !property_exists($form, 'ruleid')) {
if ($form->globalunitpenalty < 0 || $form->globalunitpenalty > 1) {
$errors['globalunitpenalty'] = get_string('error_unitpenalty', 'qtype_formulas');;
}
}
foreach ($validanswers as $idx => $part) {
try {
$pattern = '\{(_[0-9u][0-9]*)(:[^{}]+)?\}';
preg_match_all('/'.$pattern.'/', $part->subqtext['text'], $matches);
$boxes = array();
foreach ($matches[1] as $j => $match) {
if (array_key_exists($match, $boxes)) {
throw new Exception(get_string('error_answerbox_duplicate', 'qtype_formulas'));
} else {
$boxes[$match] = 1;
}
}
} catch (Exception $e) {
$errors["subqtext[$idx]"] = $e->getMessage();
}
}
$placeholdererrors = $this->check_placeholder(
is_string($form->questiontext) ? $form->questiontext : $form->questiontext['text'],
$validanswers
);
$errors = array_merge($errors, $placeholdererrors);
$instantiationerrors = $this->validate_instantiation($form, $validanswers);
$errors = array_merge($errors, $instantiationerrors);
return (object)array('errors' => $errors, 'answers' => $validanswers);
}
// Validating the data from the client, and return errors.
// If no errors, the $validanswers should be appended by numbox variables.
public function validate_instantiation($form, &$validanswers) {
$errors = array();
// Create a formulas question so we can use its methods for validation.
$qo = new qtype_formulas_question;
// This is legacy code and it will iterate over a whole lot of form fields, assigning
// values to undeclared class properties ("dynamic properties"). This is deprecated as
// of PHP 8.2, so for the time being, filter them out. This will not be needed once the
// new parser is finished.
$keystoskip = ['sesskey', 'correctness_simple_tol', 'correctness_simple_type', 'correctness_simple_comp', 'template',
'tags', 'oldparent', 'context', '_qf__qtype_formulas_edit_form', 'numdataset', 'multiplier', 'import_process',
'inpopup', 'cmid', 'courseid', 'returnurl', 'scrollpos', 'appendqnumstring', 'usecase', 'export_process',
'makecopy', 'submitbutton', 'status', 'shownumcorrect', 'correctness_simple_mode', 'mdlscrollto', 'image',
'coursetags', 'answernotunique', 'updatebutton'];
foreach ($form as $key => $value) {
if (in_array($key, $keystoskip)) {
continue;
}
$qo->$key = $value;
}
$tags = $this->part_tags();
$qo->options = new stdClass();
$extraquestionfields = $this->extra_question_fields();
array_shift($extraquestionfields);
foreach ($extraquestionfields as $field) {
if (isset($form->{$field})) {
$qo->{$field} = $form->{$field};
$qo->options->{$field} = $form->{$field};
}
}
if (count($form->answer)) {
// This function is also called when importing a question.
// The answers of imported questions already have their unitpenalty and ruleid set.
$isfromimport = property_exists($form, 'unitpenalty') && property_exists($form, 'ruleid');
foreach ($form->answer as $key => $answer) {
$ans = new stdClass();
foreach ($tags as $tag) {
// The unitpenalty and ruleid are set via a global option,
// but stored with each part. When importing questions,
// this is not the case.
if (!$isfromimport && ($tag == 'unitpenalty' || $tag == 'ruleid')) {
$ans->{$tag} = $form->{'global'.$tag};
} else {
$ans->{$tag} = $form->{$tag}[$key];
}
}
$ans->subqtext = $form->subqtext[$key];
$ans->feedback = $form->feedback[$key];
$ans->partcorrectfb = $form->partcorrectfb[$key];
$ans->partpartiallycorrectfb = $form->partpartiallycorrectfb[$key];
$ans->partincorrectfb = $form->partincorrectfb[$key];
$qo->options->answers[] = $ans;
}
}
$qo->parts = array();
if (!empty($qo->options->answers)) {
foreach ($qo->options->answers as $i => $ans) {
$ans->partindex = $i;
$ans->subqtextformat = $ans->subqtext['format'];
$ans->subqtext = $ans->subqtext['text'];
$ans->feedbackformat = $ans->feedback['format'];
$ans->feedback = $ans->feedback['text'];
$ans->partcorrectfbformat = $ans->partcorrectfb['format'];
$ans->partcorrectfb = $ans->partcorrectfb['text'];
$ans->partpartiallycorrectfbformat = $ans->partpartiallycorrectfb['format'];
$ans->partpartiallycorrectfb = $ans->partpartiallycorrectfb['text'];
$ans->partincorrectfbformat = $ans->partincorrectfb['format'];
$ans->partincorrectfb = $ans->partincorrectfb['text'];
$qo->parts[$i] = new qtype_formulas_part();
foreach ($ans as $key => $value) {
$qo->parts[$i]->$key = $value;
// TODO verify if part id is set (but do we actually need it here?).
}
}
}
$qo->qv = new qtype_formulas\variables();
$qo->options->numpart = count($qo->options->answers);
$qo->numpart = $qo->options->numpart;
$qo->fractions = array_fill(0, $qo->numpart, 0);
$qo->anscorrs = array_fill(0, $qo->numpart, 0);
$qo->unitcorrs = array_fill(0, $qo->numpart, 0);
try {
$vstack = $qo->qv->parse_random_variables($qo->varsrandom);
$qo->randomsvars = $qo->qv->instantiate_random_variables($vstack); // Instantiate a set of random variables.
} catch (Exception $e) {
$errors["varsrandom"] = $e->getMessage();
return $errors;
}
try {
$qo->globalvars = $qo->qv->evaluate_assignments($qo->randomsvars, $qo->varsglobal);
} catch (Exception $e) {
$errors["varsglobal"] = get_string('error_validation_eval', 'qtype_formulas') . $e->getMessage();
return $errors;
}
// Attempt to compute answers to see if they are wrong or not.
foreach ($validanswers as $idx => $ans) {
$ans->partindex = $idx;
$unitcheck = new answer_unit_conversion;
try {
$unitcheck->parse_targets($ans->postunit);
} catch (Exception $e) {
$errors["postunit[$idx]"] = get_string('error_unit', 'qtype_formulas') . $e->getMessage();
}
try {
$unitcheck->assign_additional_rules($ans->otherrule);
$unitcheck->reparse_all_rules();
} catch (Exception $e) {
$errors["otherrule[$idx]"] = get_string('error_rule', 'qtype_formulas') . $e->getMessage();
}
try {
$conversionrules = new unit_conversion_rules;
$entry = $conversionrules->entry($ans->ruleid);
if ($entry === null || $entry[1] === null) {
throw new Exception(get_string('error_ruleid', 'qtype_formulas'));
}
$unitcheck->assign_default_rules($ans->ruleid, $entry[1]);
$unitcheck->reparse_all_rules();
} catch (Exception $e) {
$errors["ruleid[$idx]"] = $e->getMessage();
}
try {
$vars = $qo->qv->evaluate_assignments($qo->globalvars, $ans->vars1);
} catch (Exception $e) {
$errors["vars1[$idx]"] = get_string('error_validation_eval', 'qtype_formulas') . $e->getMessage();
continue;
}
try {
$modelanswers = $qo->get_evaluated_answer($ans);
$cloneanswers = $modelanswers;
// Set the number of 'coordinates' which is used to display all answer boxes.
$ans->numbox = count($modelanswers);
$gradingtype = $ans->answertype;
} catch (Exception $e) {
$errors["answer[$idx]"] = $e->getMessage();
continue;
}
try {
$dres = $qo->compute_response_difference($vars, $modelanswers, $cloneanswers, 1, $gradingtype);
if ($dres === null) {
throw new Exception();
}
} catch (Exception $e) {
$errors["answer[$idx]"] = get_string('error_validation_eval', 'qtype_formulas') . $e->getMessage();
continue;
}
try {
$qo->add_special_correctness_variables($vars, $modelanswers, $cloneanswers, $dres->diff, $dres->is_number);
$qo->qv->evaluate_assignments($vars, $ans->vars2);
} catch (Exception $e) {
$errors["vars2[$idx]"] = get_string('error_validation_eval', 'qtype_formulas') . $e->getMessage();
continue;
}
try {
$responses = $qo->get_correct_responses_individually($ans);
$correctness = $qo->grade_responses_individually($ans, $responses, $unitcheck, true);
} catch (Exception $e) {
$errors["correctness[$idx]"] = get_string('error_validation_eval', 'qtype_formulas') . $e->getMessage();
continue;
}
}
return $errors;
}
/**
* Reorder the answers according to the order of placeholders in main question text.
* The check_placeholder() function should be called before.
*
* @param string $questiontext The main question text containing the placeholders.
* @param array $answers array of answers, containing the placeholder name (must not be empty).
* @return array answersorder.
*/