-
Notifications
You must be signed in to change notification settings - Fork 6
/
questiontype.php
783 lines (662 loc) · 30.5 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
<?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/>.
/**
* Question type class for qtype_kprime.
*
* @package qtype_kprime
* @author Amr Hourani ([email protected])
* @author Martin Hanusch ([email protected])
* @author Jürgen Zimmer ([email protected])
* @author Andreas Hruska ([email protected])
* @copyright 2016 ETHZ {@link http://ethz.ch/}
* @copyright 2014 eDaktik GmbH {@link http://www.edaktik.at}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/questionlib.php');
require_once($CFG->dirroot . '/question/type/kprime/lib.php');
/**
* Question hint for qtype_kprime.
*
* An extension of {@see question_hint} for questions like match and multiple
* choice with multile answers, where there are options for whether to show the
* number of parts right at each stage, and to reset the wrong parts.
*
* @copyright 2010 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class question_hint_kprime extends question_hint_with_parts {
/** @var int statewhichincorrect */
public $statewhichincorrect;
/**
* Constructor.
* @param int $id the hint id from the database.
* @param string $hint The hint text
* @param int $hintformat the corresponding text FORMAT_... type.
* @param bool $shownumcorrect whether the number of right parts should be shown
* @param bool $clearwrong whether the wrong parts should be reset.
* @param int $statewhichincorrect
*/
public function __construct($id, $hint, $hintformat, $shownumcorrect, $clearwrong, $statewhichincorrect) {
parent::__construct($id, $hint, $hintformat, $shownumcorrect, $clearwrong);
$this->statewhichincorrect = $statewhichincorrect;
}
/**
* Create a basic hint from a row loaded from the question_hints table in the database.
* @param object $row with property options as well as hint, shownumcorrect and clearwrong set.
* @return question_hint_kprime
*/
public static function load_from_record($row) {
return new question_hint_kprime($row->id, $row->hint, $row->hintformat,
$row->shownumcorrect, $row->clearwrong, $row->options);
}
/**
* Make any changes to the display options before a question is rendered, so
* that it can be displayed in a way that is appropriate for the statue it is
* currently in. For example, by default, if the question is finished, we
* ensure that it is only ever displayed read-only.
* @param question_display_options $options the options to adjust. Just change
* the properties of this object - objects are passed by referece.
*/
public function adjust_display_options(question_display_options $options) {
parent::adjust_display_options($options);
$options->statewhichincorrect = $this->statewhichincorrect;
}
}
/**
* The qtype_kprime.
*/
class qtype_kprime extends question_type {
/**
* Sets the default options for the question.
* (non-PHPdoc)
* @see question_type::set_default_options()
* @param object $question
*/
public function set_default_options($question) {
$kprimeconfig = get_config('qtype_kprime');
if (!isset($question->options)) {
$question->options = new stdClass();
}
if (!isset($question->options->numberofrows)) {
$question->options->numberofrows = QTYPE_KPRIME_NUMBER_OF_OPTIONS;
}
if (!isset($question->options->numberofcolumns)) {
$question->options->numberofcolumns = QTYPE_KPRIME_NUMBER_OF_RESPONSES;
}
if (!isset($question->options->shuffleanswers)) {
$question->options->shuffleanswers = $kprimeconfig->shuffleanswers;
}
if (!isset($question->options->scoringmethod)) {
$question->options->scoringmethod = $kprimeconfig->scoringmethod;
}
if (!isset($question->options->rows)) {
$rows = [];
for ($i = 1; $i <= $question->options->numberofrows; ++$i) {
$row = new stdClass();
$row->number = $i;
$row->optiontext = '';
$row->optiontextformat = FORMAT_HTML;
$row->optionfeedback = '';
$row->optionfeedbackformat = FORMAT_HTML;
$rows[] = $row;
}
$question->options->rows = $rows;
}
if (!isset($question->options->columns)) {
$columns = [];
for ($i = 1; $i <= $question->options->numberofcolumns; ++$i) {
$column = new stdClass();
$column->number = $i;
if (isset($kprimeconfig->{'responsetext' . $i})) {
$responsetextcol = $kprimeconfig->{'responsetext' . $i};
} else {
$responsetextcol = '';
}
$column->responsetext = $responsetextcol;
$column->responsetextformat = FORMAT_MOODLE;
$columns[] = $column;
}
$question->options->columns = $columns;
}
}
/**
* Loads the question options, rows, columns and weights from the database.
* (non-PHPdoc)
* @see question_type::get_question_options()
* @param object $question
*/
public function get_question_options($question) {
global $DB, $OUTPUT;
parent::get_question_options($question);
// Retrieve the question options.
if ($qoptions = $DB->get_record('qtype_kprime_options',
['questionid' => $question->id])) {
$question->options = $qoptions;
} else {
return false;
}
// Retrieve the question rows (kprime options).
if ($orows = $DB->get_records('qtype_kprime_rows',
['questionid' => $question->id],
'number ASC', '*', 0, $question->options->numberofrows)) {
$question->options->rows = $orows;
} else {
return false;
}
// Retrieve the question columns.
if ($ocols = $DB->get_records('qtype_kprime_columns',
['questionid' => $question->id],
'number ASC', '*', 0, $question->options->numberofcolumns)) {
$question->options->columns = $ocols;
} else {
return false;
}
// Retrieve the question weights.
if (!$weightrecords = $DB->get_records('qtype_kprime_weights',
['questionid' => $question->id],
'rownumber ASC, columnnumber ASC')) {
return false;
}
foreach ($question->options->rows as $key => $row) {
$question->{'option_' . $row->number}['text'] = $row->optiontext;
$question->{'option_' . $row->number}['format'] = $row->optiontextformat;
$question->{'feedback_' . $row->number}['text'] = $row->optionfeedback;
$question->{'feedback_' . $row->number}['format'] = $row->optionfeedbackformat;
}
foreach ($question->options->columns as $key => $column) {
$question->{'responsetext_' . $column->number} = $column->responsetext;
}
foreach ($weightrecords as $key => $weight) {
if ($weight->weight == 1.0) {
$question->{'weightbutton_' . $weight->rownumber} = $weight->columnnumber;
}
}
// Put the weight records into an array indexed by rownumber and columnnumber.
$question->options->weights = $this->weight_records_to_array($weightrecords);
return true;
}
/**
* Stores the question options in the database.
* (non-PHPdoc)
* @see question_type::save_question_options()
* @param object $question
*/
public function save_question_options($question) {
global $DB;
$context = $question->context;
$result = new stdClass();
// Insert all the new options.
$options = $DB->get_record('qtype_kprime_options', ['questionid' => $question->id]);
if (!$options) {
$options = new stdClass();
$options->questionid = $question->id;
$options->scoringmethod = '';
$options->shuffleanswers = '';
$options->numberofcolumns = '';
$options->numberofrows = '';
$options->id = $DB->insert_record('qtype_kprime_options', $options);
}
$options->scoringmethod = $question->scoringmethod;
$options->shuffleanswers = $question->shuffleanswers;
$options->numberofrows = $question->numberofrows;
$options->numberofcolumns = $question->numberofcolumns;
$DB->update_record('qtype_kprime_options', $options);
$this->save_hints($question, true);
// Insert all the new rows.
$oldrows = $DB->get_records('qtype_kprime_rows', ['questionid' => $question->id], 'number ASC');
for ($i = 1; $i <= $options->numberofrows; ++$i) {
$row = array_shift($oldrows);
if (!$row) {
$row = new stdClass();
$row->questionid = $question->id;
$row->number = $i;
$row->optiontext = '';
$row->optiontextformat = FORMAT_HTML;
$row->optionfeedback = '';
$row->optionfeedbackformat = FORMAT_HTML;
$row->id = $DB->insert_record('qtype_kprime_rows', $row);
}
// Also save images in optiontext and feedback.
$optiondata = $question->{'option_' . $i};
$row->optiontext = $this->import_or_save_files(
$optiondata, $context, 'qtype_kprime', 'optiontext', $row->id);
$row->optiontextformat = $question->{'option_' . $i}['format'];
$optionfeedback = $question->{'feedback_' . $i};
$row->optionfeedback = $this->import_or_save_files(
$optionfeedback, $context, 'qtype_kprime', 'feedbacktext', $row->id);
$row->optionfeedbackformat = $question->{'feedback_' . $i}['format'];
$DB->update_record('qtype_kprime_rows', $row);
}
$oldcolumns = $DB->get_records('qtype_kprime_columns', ['questionid' => $question->id], 'number ASC');
// Insert all new columns.
for ($i = 1; $i <= $options->numberofcolumns; ++$i) {
$column = array_shift($oldcolumns);
if (!$column) {
$column = new stdClass();
$column->questionid = $question->id;
$column->number = $i;
$column->responsetext = '';
$column->responsetextformat = FORMAT_MOODLE;
$column->id = $DB->insert_record('qtype_kprime_columns', $column);
}
// Perform an update.
$column->responsetext = $question->{'responsetext_' . $i};
$column->responsetextformat = FORMAT_MOODLE;
$DB->update_record('qtype_kprime_columns', $column);
}
// Set all the new weights.
$oldweightrecords = $DB->get_records('qtype_kprime_weights',
['questionid' => $question->id],
'rownumber ASC, columnnumber ASC');
// Put the old weights into an array.
$oldweights = $this->weight_records_to_array($oldweightrecords);
for ($i = 1; $i <= $options->numberofrows; ++$i) {
for ($j = 1; $j <= $options->numberofcolumns; ++$j) {
if (!empty($oldweights[$i][$j])) {
$weight = $oldweights[$i][$j];
} else {
$weight = new stdClass();
$weight->questionid = $question->id;
$weight->rownumber = $i;
$weight->columnnumber = $j;
$weight->weight = 0.0;
$weight->id = $DB->insert_record('qtype_kprime_weights', $weight);
}
// Perform the weight update.
if (property_exists($question, 'weightbutton_' . $i)) {
if ($question->{'weightbutton_' . $i} == $j) {
$weight->weight = 1.0;
} else {
$weight->weight = 0.0;
}
} else {
$weight->weight = 0.0;
}
$DB->update_record('qtype_kprime_weights', $weight);
}
}
}
/**
* Save question hints
* @param object $formdata
* @param bool $withparts
*/
public function save_hints($formdata, $withparts = false) {
global $DB;
$context = $formdata->context;
$oldhints = $DB->get_records('question_hints', ['questionid' => $formdata->id], 'id ASC');
if (!empty($formdata->hint)) {
$numhints = max(array_keys($formdata->hint)) + 1;
} else {
$numhints = 0;
}
if ($withparts) {
if (!empty($formdata->hintclearwrong)) {
$numclears = max(array_keys($formdata->hintclearwrong)) + 1;
} else {
$numclears = 0;
}
if (!empty($formdata->hintshownumcorrect)) {
$numshows = max(array_keys($formdata->hintshownumcorrect)) + 1;
} else {
$numshows = 0;
}
$numhints = max($numhints, $numclears, $numshows);
}
for ($i = 0; $i < $numhints; $i += 1) {
if (html_is_blank($formdata->hint[$i]['text'])) {
$formdata->hint[$i]['text'] = '';
}
if ($withparts) {
$clearwrong = !empty($formdata->hintclearwrong[$i]);
$shownumcorrect = !empty($formdata->hintshownumcorrect[$i]);
$statewhichincorrect = !empty($formdata->hintoptions[$i]);
}
if (empty($formdata->hint[$i]['text'])
&& empty($clearwrong)
&& empty($shownumcorrect)
&& empty($statewhichincorrect)) {
continue;
}
// Update an existing hint if possible.
$hint = array_shift($oldhints);
if (!$hint) {
$hint = new stdClass();
$hint->questionid = $formdata->id;
$hint->hint = '';
$hint->id = $DB->insert_record('question_hints', $hint);
}
$hint->hint = $this->import_or_save_files($formdata->hint[$i], $context, 'question', 'hint', $hint->id);
$hint->hintformat = $formdata->hint[$i]['format'];
if ($withparts) {
$hint->clearwrong = $clearwrong;
$hint->shownumcorrect = $shownumcorrect;
$hint->options = $statewhichincorrect;
}
$DB->update_record('question_hints', $hint);
}
// Delete any remaining old hints.
$fs = get_file_storage();
foreach ($oldhints as $oldhint) {
$fs->delete_area_files($context->id, 'question', 'hint', $oldhint->id);
$DB->delete_records('question_hints', ['id' => $oldhint->id]);
}
}
/**
* Create a question_hint, or an appropriate subclass for this question,
* from a row loaded from the database.
* @param object $hint the DB row from the question hints table.
* @return question_hint
*/
protected function make_hint($hint) {
return question_hint_kprime::load_from_record($hint);
}
/**
* Initialise the common 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->shuffleanswers = $questiondata->options->shuffleanswers;
$question->scoringmethod = $questiondata->options->scoringmethod;
$question->numberofrows = $questiondata->options->numberofrows;
$question->numberofcolumns = $questiondata->options->numberofcolumns;
$question->rows = $questiondata->options->rows;
$question->columns = $questiondata->options->columns;
$question->weights = $questiondata->options->weights;
}
/**
* Custom method for deleting mtf questions.
* (non-PHPdoc)
* @see question_type::delete_question()
* @param int $questionid
* @param int $contextid
*/
public function delete_question($questionid, $contextid) {
global $DB;
$DB->delete_records('qtype_kprime_options', ['questionid' => $questionid]);
$DB->delete_records('qtype_kprime_rows', ['questionid' => $questionid]);
$DB->delete_records('qtype_kprime_columns', ['questionid' => $questionid]);
$DB->delete_records('qtype_kprime_weights', ['questionid' => $questionid]);
parent::delete_question($questionid, $contextid);
}
/**
* Turns an array of records from the table qtype_kprime_weights into an array of array indexed
* by rows and columns.
* @param object $weightrecords
* @return Ambigous <multitype:multitype: , unknown>
*/
private function weight_records_to_array($weightrecords) {
$weights = [];
foreach ($weightrecords as $id => $weight) {
if (!property_exists((object) $weights, $weight->rownumber)) {
$weights[$weight->rownumber] = [];
}
$weights[$weight->rownumber][$weight->columnnumber] = $weight;
}
return $weights;
}
/**
* (non-PHPdoc).
* @see question_type::get_random_guess_score()
* @param object $questiondata
* @return int
*/
public function get_random_guess_score($questiondata) {
$scoring = $questiondata->options->scoringmethod;
if ($scoring == 'kprime') {
return 0.1875;
} else if ($scoring == 'kprimeonezero') {
return 0.0625;
} else if ($scoring == 'subpoints') {
return 0.5;
} else {
return 0.00;
}
}
/**
* (non-PHPdoc).
* @see question_type::get_possible_responses()
* @param object $questiondata
* @return array
*/
public function get_possible_responses($questiondata) {
$question = $this->make_question($questiondata);
$weights = $question->weights;
$parts = [];
foreach ($question->rows as $rowid => $row) {
$choices = [];
foreach ($question->columns as $columnid => $column) {
// Calculate the partial credit.
if ($question->scoringmethod == 'subpoints') {
$partialcredit = 0.0;
} else {
$partialcredit = -0.999; // Due to non-linear math.
}
if ($question->scoringmethod == 'subpoints' &&
$weights[$row->number][$column->number]->weight > 0) {
$partialcredit = 1 / count($question->rows);
}
$correctreponse = '';
if ($weights[$row->number][$column->number]->weight > 0) { // Is it correct
// Response?
$correctreponse = ' (' . get_string('correctresponse', 'qtype_kprime') . ')';
}
$choices[$columnid] = new question_possible_response(
question_utils::to_plain_text($row->optiontext, $row->optiontextformat) .
': ' . question_utils::to_plain_text(
$column->responsetext . $correctreponse,
$column->responsetextformat), $partialcredit);
}
$choices[null] = question_possible_response::no_response();
$parts[$rowid] = $choices;
}
return $parts;
}
/**
* (non-PHPdoc).
* @see question_type::move_files()
* @param int $questionid
* @param int $oldcontextid
* @param int $newcontextid
*/
public function move_files($questionid, $oldcontextid, $newcontextid) {
parent::move_files($questionid, $oldcontextid, $newcontextid);
$this->move_files_in_options_and_feedback($questionid, $oldcontextid, $newcontextid, true);
}
/**
* (non-PHPdoc).
* @see question_type::delete_files()
* @param int $questionid
* @param int $contextid
*/
protected function delete_files($questionid, $contextid) {
parent::delete_files($questionid, $contextid);
$this->delete_files_in_options_and_feedback($questionid, $contextid);
}
/**
* Move all the files belonging to this question's options and feedbacks
* when the question is moved 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.
* @param bool $answerstoo whether there is an 'answer' question area,
* as well as an 'answerfeedback' one. Default false.
*/
protected function move_files_in_options_and_feedback($questionid, $oldcontextid, $newcontextid, $answerstoo = false) {
global $DB;
$fs = get_file_storage();
$rowids = $DB->get_records_menu('qtype_kprime_rows', ['questionid' => $questionid], 'id', 'id,1');
foreach ($rowids as $rowid => $notused) {
$fs->move_area_files_to_new_context($oldcontextid, $newcontextid, 'qtype_kprime', 'optiontext', $rowid);
$fs->move_area_files_to_new_context($oldcontextid, $newcontextid, 'qtype_kprime', 'feedbacktext', $rowid);
}
}
/**
* Delete all the files belonging to this question's options and feedback.
* @param unknown $questionid
* @param unknown $contextid
*/
protected function delete_files_in_options_and_feedback($questionid, $contextid) {
global $DB;
$fs = get_file_storage();
$rowids = $DB->get_records_menu('qtype_kprime_rows', ['questionid' => $questionid], 'id', 'id,1');
foreach ($rowids as $rowid => $notused) {
$fs->delete_area_files($contextid, 'qtype_kprime', 'optiontext', $rowid);
$fs->delete_area_files($contextid, 'qtype_kprime', 'feedbacktext', $rowid);
}
}
/**
* Provide export functionality for xml format.
* @param object $question the question object
* @param qformat_xml $format the format object so that helper methods can be used
* @param mixed $extra any additional format specific data that may be passed by the format (see
* format code for info)
* @return string the data to append to the output buffer or false if error
*/
public function export_to_xml($question, qformat_xml $format, $extra = null) {
$expout = '';
$fs = get_file_storage();
$contextid = $question->contextid;
// First set the additional fields.
$expout .= ' <scoringmethod>' . $format->writetext($question->options->scoringmethod) .
"</scoringmethod>\n";
$expout .= ' <shuffleanswers>' . $format->get_single($question->options->shuffleanswers) .
"</shuffleanswers>\n";
$expout .= ' <numberofrows>' . $question->options->numberofrows . "</numberofrows>\n";
$expout .= ' <numberofcolumns>' . $question->options->numberofcolumns .
"</numberofcolumns>\n";
// Now we export the question rows (options).
foreach ($question->options->rows as $row) {
$number = $row->number;
$expout .= " <row number=\"$number\">\n";
$textformat = $format->get_format($row->optiontextformat);
$files = $fs->get_area_files($contextid, 'qtype_kprime', 'optiontext', $row->id);
$expout .= " <optiontext format=\"$textformat\">\n" . ' ' .
$format->writetext($row->optiontext);
$expout .= $format->write_files($files);
$expout .= " </optiontext>\n";
$textformat = $format->get_format($row->optionfeedbackformat);
$files = $fs->get_area_files($contextid, 'qtype_kprime', 'feedbacktext', $row->id);
$expout .= " <feedbacktext format=\"$textformat\">\n" . ' ' .
$format->writetext($row->optionfeedback);
$expout .= $format->write_files($files);
$expout .= " </feedbacktext>\n";
$expout .= " </row>\n";
}
// Now we export the columns (responses).
foreach ($question->options->columns as $column) {
$number = $column->number;
$expout .= " <column number=\"$number\">\n";
$textformat = $format->get_format($column->responsetextformat);
$expout .= " <responsetext format=\"$textformat\">\n" . ' ' .
$format->writetext($column->responsetext);
$expout .= " </responsetext>\n";
$expout .= " </column>\n";
}
// Finally, we export the weights.
$weights = call_user_func_array('array_merge', $question->options->weights);
foreach ($weights as $weight) {
$rownumber = $weight->rownumber;
$columnnumber = $weight->columnnumber;
$value = $weight->weight;
$expout .= " <weight rownumber=\"$rownumber\" columnnumber=\"$columnnumber\">\n";
$expout .= " <value>\n";
$expout .= ' ' . $value . "\n";
$expout .= " </value>\n";
$expout .= " </weight>\n";
}
return $expout;
}
/**
* Provide import functionality for xml format.
* @param mixed $data the segment of data containing the question
* @param object $question question object processed (so far) by standard import code
* @param qformat_xml $format the format object so that helper methods can be used (in particular error())
* @param mixed $extra any additional format specific data that may be passed by the format (see format code for info)
* @return object $question object suitable for save_options() call or false if cannot handle
*/
public function import_from_xml($data, $question, qformat_xml $format, $extra = null) {
// Check whether the question is for us.
if (!isset($data['@']['type']) || $data['@']['type'] != 'kprime') {
return false;
}
$question = $format->import_headers($data);
$question->qtype = 'kprime';
$question->scoringmethod = $format->getpath($data, ['#', 'scoringmethod', 0, '#', 'text', 0, '#'], 'kprime');
$question->shuffleanswers = $format->trans_single($format->getpath($data, ['#', 'shuffleanswers', 0, '#'], 1));
$question->numberofrows = $format->getpath(
$data, ['#', 'numberofrows', 0, '#'], QTYPE_KPRIME_NUMBER_OF_OPTIONS);
$question->numberofcolumns = $format->getpath(
$data, ['#', 'numberofcolumns', 0, '#'], QTYPE_KPRIME_NUMBER_OF_RESPONSES);
$rows = $data['#']['row'];
$i = 1;
foreach ($rows as $row) {
$number = $format->getpath($row, ['@', 'number'], $i++);
$question->{'option_' . $number} = [];
$question->{'option_' . $number}['text'] = $format->getpath(
$row, ['#', 'optiontext', 0, '#', 'text', 0, '#'], '', true);
$question->{'option_' . $number}['format'] = $format->trans_format(
$format->getpath($row, ['#', 'optiontext', 0, '@', 'format'], FORMAT_HTML));
$question->{'option_' . $number}['files'] = [];
// Restore files in options (rows).
$files = $format->getpath($row, ['#', 'optiontext', 0, '#', 'file'], [], false);
foreach ($files as $file) {
$filesdata = new stdclass();
$filesdata->content = $file['#'];
$filesdata->encoding = $file['@']['encoding'];
$filesdata->name = $file['@']['name'];
$question->{'option_' . $number}['files'][] = $filesdata;
}
$question->{'feedback_' . $number} = [];
$question->{'feedback_' . $number}['text'] = $format->getpath(
$row, ['#', 'feedbacktext', 0, '#', 'text', 0, '#'], '', true);
$question->{'feedback_' . $number}['format'] = $format->trans_format(
$format->getpath($row, ['#', 'feedbacktext', 0, '@', 'format'], FORMAT_HTML));
// Restore files in option feedback.
$question->{'feedback_' . $number}['files'] = [];
$files = $format->getpath($row, ['#', 'feedbacktext', 0, '#', 'file'], [], false);
foreach ($files as $file) {
$filesdata = new stdclass();
$filesdata->content = $file['#'];
$filesdata->encoding = $file['@']['encoding'];
$filesdata->name = $file['@']['name'];
$question->{'feedback_' . $number}['files'][] = $filesdata;
}
}
$columns = $data['#']['column'];
$j = 1;
foreach ($columns as $column) {
$number = $format->getpath($column, ['@', 'number'], $j++);
$question->{'responsetext_' . $number} = $format->getpath(
$column, ['#', 'responsetext', 0, '#', 'text', 0, '#'], '', true);
}
// Finally, import the weights.
$weights = $data['#']['weight'];
foreach ($weights as $weight) {
$rownumber = $format->getpath($weight, ['@', 'rownumber'], 1);
$columnnumber = $format->getpath($weight, ['@', 'columnnumber'], 1);
$value = $format->getpath($weight, ['#', 'value', 0, '#'], 0.0);
if ($value > 0.0) {
$question->{'weightbutton_' . $rownumber} = $columnnumber;
}
}
return $question;
}
}