mirrored from git://git.moodle.org/moodle.git
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
questiontype.php
1816 lines (1653 loc) · 78.8 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 // $Id$
/**
* The default questiontype class.
*
* @author Martin Dougiamas and many others. This has recently been completely
* rewritten by Alex Smith, Julian Sedding and Gustav Delius as part of
* the Serving Mathematics project
* {@link http://maths.york.ac.uk/serving_maths}
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package questionbank
* @subpackage questiontypes
*/
require_once($CFG->libdir . '/questionlib.php');
/**
* This is the base class for Moodle question types.
*
* There are detailed comments on each method, explaining what the method is
* for, and the circumstances under which you might need to override it.
*
* Note: the questiontype API should NOT be considered stable yet. Very few
* question tyeps have been produced yet, so we do not yet know all the places
* where the current API is insufficient. I would rather learn from the
* experiences of the first few question type implementors, and improve the
* interface to meet their needs, rather the freeze the API prematurely and
* condem everyone to working round a clunky interface for ever afterwards.
*
* @package questionbank
* @subpackage questiontypes
*/
class default_questiontype {
/**
* Name of the question type
*
* The name returned should coincide with the name of the directory
* in which this questiontype is located
*
* @return string the name of this question type.
*/
function name() {
return 'default';
}
/**
* The name this question should appear as in the create new question
* dropdown.
*
* @return mixed the desired string, or false to hide this question type in the menu.
*/
function menu_name() {
$name = $this->name();
$menu_name = get_string($name, 'qtype_' . $name);
if ($menu_name[0] == '[') {
// Legacy behavior, if the string was not in the proper qtype_name
// language file, look it up in the quiz one.
$menu_name = get_string($name, 'quiz');
}
return $menu_name;
}
/**
* @return boolean true if this question type sometimes requires manual grading.
*/
function is_manual_graded() {
return false;
}
/**
* @param object $question a question of this type.
* @param string $otherquestionsinuse comma-separate list of other question ids in this attempt.
* @return boolean true if a particular instance of this question requires manual grading.
*/
function is_question_manual_graded($question, $otherquestionsinuse) {
return $this->is_manual_graded();
}
/**
* @return boolean true if this question type can be used by the random question type.
*/
function is_usable_by_random() {
return true;
}
/**
* @return whether the question_answers.answer field needs to have
* restore_decode_content_links_worker called on it.
*/
function has_html_answers() {
return false;
}
/**
* 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 wherer 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.
*/
function extra_question_fields() {
return null;
}
/**
* 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
*/
function questionid_column_name() {
return 'questionid';
}
/**
* If your question type has a table that extends the question_answers table,
* make this method return an array wherer the first element is the table name,
* and the subsequent entries are the column names (apart from id and answerid).
*
* @return mixed array as above, or null to tell the base class to do nothing.
*/
function extra_answer_fields() {
return null;
}
/**
* Return an instance of the question editing form definition. This looks for a
* class called edit_{$this->name()}_question_form in the file
* {$CFG->docroot}/question/type/{$this->name()}/edit_{$this->name()}_question_form.php
* and if it exists returns an instance of it.
*
* @param string $submiturl passed on to the constructor call.
* @return object an instance of the form definition, or null if one could not be found.
*/
function create_editing_form($submiturl, $question, $category, $contexts, $formeditable) {
global $CFG;
require_once("{$CFG->dirroot}/question/type/edit_question_form.php");
$definition_file = $CFG->dirroot.'/question/type/'.$this->name().'/edit_'.$this->name().'_form.php';
if (!(is_readable($definition_file) && is_file($definition_file))) {
return null;
}
require_once($definition_file);
$classname = 'question_edit_'.$this->name().'_form';
if (!class_exists($classname)) {
return null;
}
return new $classname($submiturl, $question, $category, $contexts, $formeditable);
}
/**
* @return string the full path of the folder this plugin's files live in.
*/
function plugin_dir() {
global $CFG;
return $CFG->dirroot . '/question/type/' . $this->name();
}
/**
* @return string the URL of the folder this plugin's files live in.
*/
function plugin_baseurl() {
global $CFG;
return $CFG->wwwroot . '/question/type/' . $this->name();
}
/**
* This method should be overriden if you want to include a special heading or some other
* html on a question editing page besides the question editing form.
*
* @param question_edit_form $mform a child of question_edit_form
* @param object $question
* @param string $wizardnow is '' for first page.
*/
function display_question_editing_page(&$mform, $question, $wizardnow){
list($heading, $langmodule) = $this->get_heading(empty($question->id));
print_heading_with_help($heading, $this->name(), $langmodule);
$permissionstrs = array();
if (!empty($question->id)){
if ($question->formoptions->canedit){
$permissionstrs[] = get_string('permissionedit', 'question');
}
if ($question->formoptions->canmove){
$permissionstrs[] = get_string('permissionmove', 'question');
}
if ($question->formoptions->cansaveasnew){
$permissionstrs[] = get_string('permissionsaveasnew', 'question');
}
}
if (!$question->formoptions->movecontext && count($permissionstrs)){
print_heading(get_string('permissionto', 'question'), 'center', 3);
$html = '<ul>';
foreach ($permissionstrs as $permissionstr){
$html .= '<li>'.$permissionstr.'</li>';
}
$html .= '</ul>';
print_box($html, 'boxwidthnarrow boxaligncenter generalbox');
}
$mform->display();
}
/**
* Method called by display_question_editing_page and by question.php to get heading for breadcrumbs.
*
* @return array a string heading and the langmodule in which it was found.
*/
function get_heading($adding = false){
$name = $this->name();
$langmodule = 'qtype_' . $name;
if (!$adding){
$strtoget = 'editing' . $name;
} else {
$strtoget = 'adding' . $name;
}
$strheading = get_string($strtoget, $langmodule);
if ($strheading[0] == '[') {
// Legacy behavior, if the string was not in the proper qtype_name
// language file, look it up in the quiz one.
$langmodule = 'quiz';
$strheading = get_string($strtoget, $langmodule);
}
return array($strheading, $langmodule);
}
/**
*
*
* @param $question
*/
function set_default_options(&$question) {
}
/**
* Saves (creates or updates) a question.
*
* Given some question info and some data about the answers
* this function parses, organises and saves the question
* It is used by {@link question.php} when saving new data from
* a form, and also by {@link import.php} when importing questions
* This function in turn calls {@link save_question_options}
* to save question-type specific data.
*
* Whether we are saving a new question or updating an existing one can be
* determined by testing !empty($question->id). If it is not empty, we are updating.
*
* The question will be saved in category $form->category.
*
* @param object $question the question object which should be updated. For a new question will be mostly empty.
* @param object $form the object containing the information to save, as if from the question editing form.
* @param object $course not really used any more.
* @return object On success, return the new question object. On failure,
* return an object as follows. If the error object has an errors field,
* display that as an error message. Otherwise, the editing form will be
* redisplayed with validation errors, from validation_errors field, which
* is itself an object, shown next to the form fields. (I don't think this is accurate any more.)
*/
function save_question($question, $form, $course) {
global $USER;
// This default implementation is suitable for most
// question types.
// First, save the basic question itself
$question->name = trim($form->name);
$question->questiontext = trim($form->questiontext);
$question->questiontextformat = $form->questiontextformat;
$question->parent = isset($form->parent) ? $form->parent : 0;
$question->length = $this->actual_number_of_questions($question);
$question->penalty = isset($form->penalty) ? $form->penalty : 0;
if (empty($form->image)) {
$question->image = '';
} else {
$question->image = $form->image;
}
if (empty($form->generalfeedback)) {
$question->generalfeedback = '';
} else {
$question->generalfeedback = trim($form->generalfeedback);
}
if (empty($question->name)) {
$question->name = shorten_text(strip_tags($question->questiontext), 15);
if (empty($question->name)) {
$question->name = '-';
}
}
if ($question->penalty > 1 or $question->penalty < 0) {
$question->errors['penalty'] = get_string('invalidpenalty', 'quiz');
}
if (isset($form->defaultgrade)) {
$question->defaultgrade = $form->defaultgrade;
}
list($question->category) = explode(',', $form->category);
if (!empty($question->id)) {
/// Question already exists, update.
$question->modifiedby = $USER->id;
$question->timemodified = time();
if (!update_record('question', $question)) {
error('Could not update question!');
}
} else {
/// New question.
// Set the unique code
$question->stamp = make_unique_id_code();
$question->createdby = $USER->id;
$question->modifiedby = $USER->id;
$question->timecreated = time();
$question->timemodified = time();
if (!$question->id = insert_record('question', $question)) {
error('Could not insert new question!');
}
}
// Now to save all the answers and type-specific options
$form->id = $question->id;
$form->qtype = $question->qtype;
$form->category = $question->category;
$form->questiontext = $question->questiontext;
$result = $this->save_question_options($form);
if (!empty($result->error)) {
error($result->error);
}
if (!empty($result->notice)) {
notice($result->notice, "question.php?id=$question->id");
}
if (!empty($result->noticeyesno)) {
notice_yesno($result->noticeyesno, "question.php?id=$question->id&courseid={$course->id}",
"edit.php?courseid={$course->id}");
print_footer($course);
exit;
}
// Give the question a unique version stamp determined by question_hash()
if (!set_field('question', 'version', question_hash($question), 'id', $question->id)) {
error('Could not update question version field');
}
return $question;
}
/**
* 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.
*/
function save_question_options($question) {
$extra_question_fields = $this->extra_question_fields();
if (is_array($extra_question_fields)) {
$question_extension_table = array_shift($extra_question_fields);
$function = 'update_record';
$questionidcolname = $this->questionid_column_name();
$options = get_record($question_extension_table, $questionidcolname, $question->id);
if (!$options) {
$function = 'insert_record';
$options = new stdClass;
$options->$questionidcolname = $question->id;
}
foreach ($extra_question_fields as $field) {
if (!isset($question->$field)) {
$result = new stdClass;
$result->error = "No data for field $field when saving " .
$this->name() . ' question id ' . $question->id;
return $result;
}
$options->$field = $question->$field;
}
if (!$function($question_extension_table, $options)) {
$result = new stdClass;
$result->error = 'Could not save question options for ' .
$this->name() . ' question id ' . $question->id;
return $result;
}
}
$extra_answer_fields = $this->extra_answer_fields();
// TODO save the answers, with any extra data.
return null;
}
/**
* Changes all states for the given attempts over to a new question
*
* This is used by the versioning code if the teacher requests that a question
* gets replaced by the new version. In order for the attempts to be regraded
* properly all data in the states referring to the old question need to be
* changed to refer to the new version instead. In particular for question types
* that use the answers table the answers belonging to the old question have to
* be changed to those belonging to the new version.
*
* @param integer $oldquestionid The id of the old question
* @param object $newquestion The new question
* @param array $attempts An array of all attempt objects in whose states
* replacement should take place
*/
function replace_question_in_attempts($oldquestionid, $newquestion, $attemtps) {
echo 'Not yet implemented';
return;
}
/**
* 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).
*/
function get_question_options(&$question) {
global $CFG;
if (!isset($question->options)) {
$question->options = new object;
}
$extra_question_fields = $this->extra_question_fields();
if (is_array($extra_question_fields)) {
$question_extension_table = array_shift($extra_question_fields);
$extra_data = get_record($question_extension_table, $this->questionid_column_name(), $question->id, '', '', '', '', implode(', ', $extra_question_fields));
if ($extra_data) {
foreach ($extra_question_fields as $field) {
$question->options->$field = $extra_data->$field;
}
} else {
notify("Failed to load question options from the table $question_extension_table for questionid " .
$question->id);
return false;
}
}
$extra_answer_fields = $this->extra_answer_fields();
if (is_array($extra_answer_fields)) {
$answer_extension_table = array_shift($extra_answer_fields);
$question->options->answers = get_records_sql('
SELECT qa.*, qax.' . implode(', qax.', $extra_answer_fields) . '
FROM ' . $CFG->prefix . 'question_answers qa, ' . $CFG->prefix . '$answer_extension_table qax
WHERE qa.questionid = ' . $question->id . ' AND qax.answerid = qa.id');
if (!$question->options->answers) {
notify("Failed to load question answers from the table $answer_extension_table for questionid " .
$question->id);
return false;
}
} else {
// Don't check for success or failure because some question types do not use the answers table.
$question->options->answers = get_records('question_answers', 'question', $question->id, 'id ASC');
}
return true;
}
/**
* Deletes states from the question-type specific tables
*
* @param string $stateslist Comma separated list of state ids to be deleted
*/
function delete_states($stateslist) {
/// The default question type does not have any tables of its own
// therefore there is nothing to delete
return true;
}
/**
* Deletes a question from the question-type specific tables
*
* @return boolean Success/Failure
* @param object $question The question being deleted
*/
function delete_question($questionid) {
global $CFG;
$success = true;
$extra_question_fields = $this->extra_question_fields();
if (is_array($extra_question_fields)) {
$question_extension_table = array_shift($extra_question_fields);
$success = $success && delete_records($question_extension_table,
$this->questionid_column_name(), $questionid);
}
$extra_answer_fields = $this->extra_answer_fields();
if (is_array($extra_answer_fields)) {
$answer_extension_table = array_shift($extra_answer_fields);
$success = $success && delete_records_select($answer_extension_table,
"answerid IN (SELECT qa.id FROM {$CFG->prefix}question_answers qa WHERE qa.question = $questionid)");
}
$success = $success && delete_records('question_answers', 'question', $questionid);
return $success;
}
/**
* Returns the number of question numbers which are used by the question
*
* This function returns the number of question numbers to be assigned
* to the question. Most question types will have length one; they will be
* assigned one number. The 'description' type, however does not use up a
* number and so has a length of zero. Other question types may wish to
* handle a bundle of questions and hence return a number greater than one.
* @return integer The number of question numbers which should be
* assigned to the question.
* @param object $question The question whose length is to be determined.
* Question type specific information is included.
*/
function actual_number_of_questions($question) {
// By default, each question is given one number
return 1;
}
/**
* Creates empty session and response information for the question
*
* This function is called to start a question session. Empty question type
* specific session data (if any) and empty response data will be added to the
* state object. Session data is any data which must persist throughout the
* attempt possibly with updates as the user interacts with the
* question. This function does NOT create new entries in the database for
* the session; a call to the {@link save_session_and_responses} member will
* occur to do this.
* @return bool Indicates success or failure.
* @param object $question The question for which the session is to be
* created. Question type specific information is
* included.
* @param object $state The state to create the session for. Note that
* this will not have been saved in the database so
* there will be no id. This object will be updated
* to include the question type specific information
* (it is passed by reference). In particular, empty
* responses will be created in the ->responses
* field.
* @param object $cmoptions
* @param object $attempt The attempt for which the session is to be
* started. Questions may wish to initialize the
* session in different ways depending on the user id
* or time available for the attempt.
*/
function create_session_and_responses(&$question, &$state, $cmoptions, $attempt) {
// The default implementation should work for the legacy question types.
// Most question types with only a single form field for the student's response
// will use the empty string '' as the index for that one response. This will
// automatically be stored in and restored from the answer field in the
// question_states table.
$state->responses = array(
'' => '',
);
return true;
}
/**
* Restores the session data and most recent responses for the given state
*
* This function loads any session data associated with the question
* session in the given state from the database into the state object.
* In particular it loads the responses that have been saved for the given
* state into the ->responses member of the state object.
*
* Question types with only a single form field for the student's response
* will not need not restore the responses; the value of the answer
* field in the question_states table is restored to ->responses['']
* before this function is called. Question types with more response fields
* should override this method and set the ->responses field to an
* associative array of responses.
* @return bool Indicates success or failure.
* @param object $question The question object for the question including any
* question type specific information.
* @param object $state The saved state to load the session for. This
* object should be updated to include the question
* type specific session information and responses
* (it is passed by reference).
*/
function restore_session_and_responses(&$question, &$state) {
// The default implementation does nothing (successfully)
return true;
}
/**
* Saves the session data and responses for the given question and state
*
* This function saves the question type specific session data from the
* state object to the database. In particular for most question types it saves the
* responses from the ->responses member of the state object. The question type
* non-specific data for the state has already been saved in the question_states
* table and the state object contains the corresponding id and
* sequence number which may be used to index a question type specific table.
*
* Question types with only a single form field for the student's response
* which is contained in ->responses[''] will not have to save this response,
* it will already have been saved to the answer field of the question_states table.
* Question types with more response fields should override this method to convert
* the data the ->responses array into a single string field, and save it in the
* database. The implementation in the multichoice question type is a good model to follow.
* http://cvs.moodle.org/contrib/plugins/question/type/opaque/questiontype.php?view=markup
* has a solution that is probably quite generally applicable.
* @return bool Indicates success or failure.
* @param object $question The question object for the question including
* the question type specific information.
* @param object $state The state for which the question type specific
* data and responses should be saved.
*/
function save_session_and_responses(&$question, &$state) {
// The default implementation does nothing (successfully)
return true;
}
/**
* Returns an array of values which will give full marks if graded as
* the $state->responses field
*
* The correct answer to the question in the given state, or an example of
* a correct answer if there are many, is returned. This is used by some question
* types in the {@link grade_responses()} function but it is also used by the
* question preview screen to fill in correct responses.
* @return mixed A response array giving the responses corresponding
* to the (or a) correct answer to the question. If there is
* no correct answer that scores 100% then null is returned.
* @param object $question The question for which the correct answer is to
* be retrieved. Question type specific information is
* available.
* @param object $state The state of the question, for which a correct answer is
* needed. Question type specific information is included.
*/
function get_correct_responses(&$question, &$state) {
/* The default implementation returns the response for the first answer
that gives full marks. */
if ($question->options->answers) {
foreach ($question->options->answers as $answer) {
if (((int) $answer->fraction) === 1) {
return array('' => addslashes($answer->answer));
}
}
}
return null;
}
/**
* Return an array of values with the texts for all possible responses stored
* for the question
*
* All answers are found and their text values isolated
* @return object A mixed object
* ->id question id. Needed to manage random questions:
* it's the id of the actual question presented to user in a given attempt
* ->responses An array of values giving the responses corresponding
* to all answers to the question. Answer ids are used as keys.
* The text and partial credit are the object components
* @param object $question The question for which the answers are to
* be retrieved. Question type specific information is
* available.
*/
// ULPGC ecastro
function get_all_responses(&$question, &$state) {
if (isset($question->options->answers) && is_array($question->options->answers)) {
$answers = array();
foreach ($question->options->answers as $aid=>$answer) {
$r = new stdClass;
$r->answer = $answer->answer;
$r->credit = $answer->fraction;
$answers[$aid] = $r;
}
$result = new stdClass;
$result->id = $question->id;
$result->responses = $answers;
return $result;
} else {
return null;
}
}
/**
* Return the actual response to the question in a given state
* for the question.
*
* @return mixed An array containing the response or reponses (multiple answer, match)
* given by the user in a particular attempt.
* @param object $question The question for which the correct answer is to
* be retrieved. Question type specific information is
* available.
* @param object $state The state object that corresponds to the question,
* for which a correct answer is needed. Question
* type specific information is included.
*/
// ULPGC ecastro
function get_actual_response($question, $state) {
if (!empty($state->responses)) {
$responses[] = stripslashes($state->responses['']);
} else {
$responses[] = '';
}
return $responses;
}
// ULPGC ecastro
function get_fractional_grade(&$question, &$state) {
$maxgrade = $question->maxgrade;
$grade = $state->grade;
if ($maxgrade) {
return (float)($grade/$maxgrade);
} else {
return (float)$grade;
}
}
/**
* Checks if the response given is correct and returns the id
*
* @return int The ide number for the stored answer that matches the response
* given by the user in a particular attempt.
* @param object $question The question for which the correct answer is to
* be retrieved. Question type specific information is
* available.
* @param object $state The state object that corresponds to the question,
* for which a correct answer is needed. Question
* type specific information is included.
*/
// ULPGC ecastro
function check_response(&$question, &$state){
return false;
}
// Used by the following function, so that it only returns results once per quiz page.
var $htmlheadalreadydone = false; // no private in 1.9 yet!
/**
* If this question type requires extra CSS or JavaScript to function,
* then this method will return an array of <link ...> tags that reference
* those stylesheets. This function will also call require_js()
* from ajaxlib.php, to get any necessary JavaScript linked in too.
*
* Remember that there may be more than one question of this type on a page.
* try to avoid including JS and CSS more than once.
*
* The two parameters match the first two parameters of print_question.
*
* @param object $question The question object.
* @param object $state The state object.
*
* @return an array of bits of HTML to add to the head of pages where
* this question is print_question-ed in the body. The array should use
* integer array keys, which have no significance.
*/
function get_html_head_contributions(&$question, &$state) {
// We only do this once for this question type, no matter how often this
// method is called on one page.
if ($this->htmlheadalreadydone) {
return array();
}
$this->htmlheadalreadydone = true;
// By default, we link to any of the files styles.css, styles.php,
// script.js or script.php that exist in the plugin folder.
// Core question types should not use this mechanism. Their styles
// should be included in the standard theme.
return $this->find_standard_scripts_and_css();
}
/**
* Like @see{get_html_head_contributions}, but this method is for CSS and
* JavaScript required on the question editing page question/question.php.
*
* @return an array of bits of HTML to add to the head of pages where
* this question is print_question-ed in the body. The array should use
* integer array keys, which have no significance.
*/
function get_editing_head_contributions() {
// By default, we link to any of the files styles.css, styles.php,
// script.js or script.php that exist in the plugin folder.
// Core question types should not use this mechanism. Their styles
// should be included in the standard theme.
return $this->find_standard_scripts_and_css();
}
/**
* Utility method used by @see{get_html_head_contributions} and
* @see{get_editing_head_contributions}. This looks for any of the files
* styles.css, styles.php, script.js or script.php that exist in the plugin
* folder and ensures they get included.
*
* @return array as required by get_html_head_contributions or get_editing_head_contributions.
*/
function find_standard_scripts_and_css() {
$plugindir = $this->plugin_dir();
$baseurl = $this->plugin_baseurl();
if (file_exists($plugindir . '/script.js')) {
require_js($baseurl . '/script.js');
}
if (file_exists($plugindir . '/script.php')) {
require_js($baseurl . '/script.php');
}
$stylesheets = array();
if (file_exists($plugindir . '/styles.css')) {
$stylesheets[] = 'styles.css';
}
if (file_exists($plugindir . '/styles.php')) {
$stylesheets[] = 'styles.php';
}
$contributions = array();
foreach ($stylesheets as $stylesheet) {
$contributions[] = '<link rel="stylesheet" type="text/css" href="' .
$baseurl . '/' . $stylesheet . '" />';
}
return $contributions;
}
/**
* Prints the question including the number, grading details, content,
* feedback and interactions
*
* This function prints the question including the question number,
* grading details, content for the question, any feedback for the previously
* submitted responses and the interactions. The default implementation calls
* various other methods to print each of these parts and most question types
* will just override those methods.
* @param object $question The question to be rendered. Question type
* specific information is included. The
* maximum possible grade is in ->maxgrade. The name
* prefix for any named elements is in ->name_prefix.
* @param object $state The state to render the question in. The grading
* information is in ->grade, ->raw_grade and
* ->penalty. The current responses are in
* ->responses. This is an associative array (or the
* empty string or null in the case of no responses
* submitted). The last graded state is in
* ->last_graded (hence the most recently graded
* responses are in ->last_graded->responses). The
* question type specific information is also
* included.
* @param integer $number The number for this question.
* @param object $cmoptions
* @param object $options An object describing the rendering options.
*/
function print_question(&$question, &$state, $number, $cmoptions, $options) {
/* The default implementation should work for most question types
provided the member functions it calls are overridden where required.
The layout is determined by the template question.html */
global $CFG;
$isgraded = question_state_is_graded($state->last_graded);
// For editing teachers print a link to an editing popup window
$editlink = $this->get_question_edit_link($question, $cmoptions, $options);
$generalfeedback = '';
if ($isgraded && $options->generalfeedback) {
$generalfeedback = $this->format_text($question->generalfeedback,
$question->questiontextformat, $cmoptions);
}
$grade = '';
if ($question->maxgrade and $options->scores) {
if ($cmoptions->optionflags & QUESTION_ADAPTIVE) {
$grade = !$isgraded ? '--/' : round($state->last_graded->grade, $cmoptions->decimalpoints).'/';
}
$grade .= $question->maxgrade;
}
$formatoptions = new stdClass;
$formatoptions->para = false;
$comment = format_text(stripslashes($state->manualcomment), FORMAT_HTML,
$formatoptions, $cmoptions->course);
$commentlink = '';
if (!empty($options->questioncommentlink)) {
$strcomment = get_string('commentorgrade', 'quiz');
$question_to_comment = isset($question->randomquestionid) ? $question->randomquestionid : $question->id;
$commentlink = link_to_popup_window($options->questioncommentlink .
'?attempt=' . $state->attempt . '&question=' . $question_to_comment,
'commentquestion', $strcomment, 470, 740, $strcomment, 'none', true);
$commentlink = '<div class="commentlink">'. $commentlink .'</div>';
}
$history = $this->history($question, $state, $number, $cmoptions, $options);
include "$CFG->dirroot/question/type/question.html";
}
/**
* Get a link to an edit icon for this question, if the current user is allowed
* to edit it.
*
* @param object $question the question object.
* @param object $cmoptions the options from the module. If $cmoptions->thispageurl is set
* then the link will be to edit the question in this browser window, then return to
* $cmoptions->thispageurl. Otherwise the link will be to edit in a popup. $cmoptions->cmid should also be set.
* @return string the HTML of the link, or nothing it the currenty user is not allowed to edit.
*/
function get_question_edit_link($question, $cmoptions, $options) {
global $CFG;
/// Is this user allowed to edit this question?
if (!empty($options->noeditlink) || !question_has_capability_on($question, 'edit')) {
return '';
}
/// Work out the right URL.
$linkurl = '/question/question.php?id=' . $question->id;
if (!empty($cmoptions->cmid)) {
$linkurl .= '&cmid=' . $cmoptions->cmid;
} else if (!empty($cmoptions->course)) {
$linkurl .= '&courseid=' . $cmoptions->course;
} else {
error('Need to provide courseid or cmid to get_question_edit_link.');
}
/// Work out the contents of the link.
$stredit = get_string('edit');
$linktext = '<img src="' . $CFG->pixpath . '/t/edit.gif" alt="' . $stredit . '" />';
if (!empty($cmoptions->thispageurl)) {
/// The module allow editing in the same window, print an ordinary link.
return '<a href="' . $CFG->wwwroot . $linkurl . '&returnurl=' .
urlencode($cmoptions->thispageurl . '#q' . $question->id) .
'" title="' . $stredit . '">' . $linktext . '</a>';
} else {
/// We have to edit in a pop-up.
return link_to_popup_window($linkurl . '&inpopup=1', 'editquestion',
$linktext, false, false, $stredit, '', true);
}
}
/*
* Print history of responses
*
* Used by print_question()
*/
function history($question, $state, $number, $cmoptions, $options) {
if (empty($options->history)) {
return '';
}
if (isset($question->randomquestionid)) {
$qid = $question->randomquestionid;
$randomprefix = 'random' . $question->id . '-';
} else {
$qid = $question->id;
$randomprefix = '';
}
if ($options->history == 'all') {
$eventtest = 'event > 0';
} else {
$eventtest = 'event IN (' . QUESTION_EVENTS_GRADED . ')';
}
$states = get_records_select('question_states',
'attempt = ' . $state->attempt . ' AND question = ' . $qid .
' AND ' . $eventtest, 'seq_number ASC');
if (count($states) <= 1) {
return '';
}
$strreviewquestion = get_string('reviewresponse', 'quiz');
$table = new stdClass;
$table->width = '100%';
$table->head = array (
get_string('numberabbr', 'quiz'),
get_string('action', 'quiz'),
get_string('response', 'quiz'),
get_string('time'),
);
if ($options->scores) {
$table->head[] = get_string('score', 'quiz');
$table->head[] = get_string('grade', 'quiz');
}
foreach ($states as $st) {
if ($randomprefix && strpos($st->answer, $randomprefix) === 0) {
$st->answer = substr($st->answer, strlen($randomprefix));
}
$st->responses[''] = $st->answer;
$this->restore_session_and_responses($question, $st);
if ($state->id == $st->id) {
$link = '<b>' . $st->seq_number . '</b>';
} else if (isset($options->questionreviewlink)) {
$link = link_to_popup_window($options->questionreviewlink.'?state='.$st->id.'&number='.$number,
'reviewquestion', $st->seq_number, 450, 650, $strreviewquestion, 'none', true);
} else {
$link = $st->seq_number;
}
if ($state->id == $st->id) {