/
report.php
1497 lines (1302 loc) · 66.2 KB
/
report.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 mod_offlinequiz for 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/>.
/**
* Offlinequiz statistics report class.
*
* @package offlinequiz_statistics
* @author Juergen Zimmer <zimmerj7@univie.ac.at>
* @copyright 2015 Academic Moodle Cooperation {@link http://www.academic-moodle-cooperation.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/mod/offlinequiz/report/statistics/statistics_form.php');
require_once($CFG->dirroot . '/mod/offlinequiz/report/statistics/statistics_table.php');
require_once($CFG->dirroot . '/mod/offlinequiz/report/statistics/statistics_question_table.php');
require_once($CFG->dirroot . '/mod/offlinequiz/report/statistics/statistics_question_answer_table.php');
require_once($CFG->dirroot . '/mod/offlinequiz/report/statistics/qstats.php');
require_once($CFG->dirroot . '/mod/offlinequiz/report/statistics/responseanalysis.php');
/**
* The offlinequiz statistics report provides summary information about each question in
* a offlinequiz, compared to the whole offlinequiz. It also provides a drill-down to more
* detailed information about each question.
*
* @copyright 2015 Academic Moodle Cooperation {@link http://www.academic-moodle-cooperation.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class offlinequiz_statistics_report extends offlinequiz_default_report {
/** @var integer Time after which statistics are automatically recomputed. */
const TIME_TO_CACHE_STATS = 900; // 15 minutes.
/** @var object instance of table class used for main questions stats table. */
protected $table;
/**
* Display the report.
*/
public function display($offlinequiz, $cm, $course) {
global $CFG, $DB, $OUTPUT, $PAGE;
$this->context = context_module::instance($cm->id);
// Work out the display options.
$download = optional_param('download', '', PARAM_ALPHA);
$everything = optional_param('everything', 0, PARAM_BOOL);
$recalculate = optional_param('recalculate', 0, PARAM_BOOL);
// A qid paramter indicates we should display the detailed analysis of a question and subquestions.
$qid = optional_param('qid', 0, PARAM_INT);
$questionid = optional_param('questionid', 0, PARAM_INT);
// Determine statistics mode.
$statmode = optional_param('statmode', 'statsoverview', PARAM_ALPHA);
$pageoptions = array();
$pageoptions['id'] = $cm->id;
$pageoptions['mode'] = 'statistics';
$pageoptions['statmode'] = $statmode;
// When showing big tables add the JavaScript for the double scrollbar.
if ($statmode == 'questionstats' || $statmode == 'questionandanswerstats') {
$module = array(
'name' => 'mod_offlinequiz_statistics',
'fullpath' => '/mod/offlinequiz/report/statistics/doublescroll.js',
'requires' => array(),
'strings' => array(),
'async' => false,
);
$PAGE->requires->jquery();
$PAGE->requires->jquery_plugin('ui');
$PAGE->requires->jquery_plugin('doubleScroll', 'mod_offlinequiz');
$PAGE->requires->js_init_call('offlinequiz_statistics_init_doublescroll', null, false, $module);
}
if (!$groups = $DB->get_records('offlinequiz_groups',
array('offlinequizid' => $offlinequiz->id), 'number', '*', 0, $offlinequiz->numgroups)) {
print_error('nogroups', 'offlinequiz', $CFG->wwwroot . '/course/view.php?id=' .
$COURSE->id, $scannedpage->offlinequizid);
}
// Determine groupid.
$groupnumber = optional_param('offlinegroup', -1, PARAM_INT);
if ($groupnumber === -1 and !empty($SESSION->question_pagevars['groupnumber'])) {
$groupnumber = $SESSION->question_pagevars['groupnumber'];
}
if ($groupnumber > 0) {
$pageoptions['offlinegroup'] = $groupnumber;
$offlinequiz->groupnumber = $groupnumber;
$offlinequiz->sumgrades = $DB->get_field('offlinequiz_groups', 'sumgrades',
array('offlinequizid' => $offlinequiz->id, 'number' => $groupnumber));
if ($offlinegroup = offlinequiz_get_group($offlinequiz, $groupnumber)) {
$offlinequiz->groupid = $offlinegroup->id;
$groupquestions = offlinequiz_get_group_question_ids($offlinequiz);
$offlinequiz->questions = $groupquestions;
} else {
print_error('invalidgroupnumber', 'offlinequiz');
}
} else {
$offlinequiz->groupid = 0;
// The user wants to evaluate results from all offlinequiz groups.
// Compare the sumgrades of all offlinequiz groups. First we put all sumgrades in an array.
$sumgrades = array();
foreach ($groups as $group) {
$sumgrades[] = round($group->sumgrades, $offlinequiz->decimalpoints);
}
// Now we remove duplicates.
$sumgrades = array_unique($sumgrades);
if (count($sumgrades) > 1) {
// If the groups have different sumgrades, we can't pick one.
$offlinequiz->sumgrades = -1;
} else if (count($sumgrades) == 1) {
// If the groups all have the same sumgrades, we pick the first one.
$offlinequiz->sumgrades = $sumgrades[0];
} else {
// Pathological, there are no sumgrades, i.e. no groups...
$offlinequiz->sumgrades = 0;
}
// If no group has been chosen we simply take the questions from the question instances.
$sql = "SELECT DISTINCT(questionid)
FROM {offlinequiz_group_questions}
WHERE offlinequizid = :offlinequizid";
$questionids = $DB->get_fieldset_sql($sql, array('offlinequizid' => $offlinequiz->id));
$offlinequiz->questions = $questionids;
}
// We warn the user if the different offlinequiz groups have different sets of questions.
$differentquestions = false;
if ($offlinequiz->groupid == 0 && count($groups) > 1 &&
$this->groups_have_different_questions($offlinequiz, $groups)) {
$differentquestions = true;
}
$reporturl = new moodle_url('/mod/offlinequiz/report.php', $pageoptions);
$useallattempts = 0;
// Find out current groups mode.
$currentgroup = $this->get_current_group($cm, $course, $this->context);
$nostudentsingroup = false; // True if a group is selected and there is no one in it.
if (empty($currentgroup)) {
$currentgroup = 0;
$groupstudents = array();
} else if ($currentgroup == self::NO_GROUPS_ALLOWED) {
$groupstudents = array();
$nostudentsingroup = true;
} else {
// All users who can attempt offlinequizzes and who are in the currently selected group.
$groupstudents = get_users_by_capability($this->context,
array('mod/offlinequiz:reviewmyattempts', 'mod/offlinequiz:attempt'),
'', '', '', '', $currentgroup, '', false);
if (!$groupstudents) {
$nostudentsingroup = true;
}
}
// If recalculate was requested, handle that.
if ($recalculate && confirm_sesskey()) {
$this->clear_cached_data($offlinequiz->id, $currentgroup, $useallattempts, $offlinequiz->groupid);
redirect($reporturl);
}
// Set up the main table.
if ($statmode == 'statsoverview' || $statmode == 'questionstats') {
$this->table = new offlinequiz_statistics_table();
} else {
$this->table = new offlinequiz_question_answer_statistics_table();
}
if ($everything) {
$report = get_string('completestatsfilename', 'offlinequiz_statistics');
} else {
$report = get_string('questionstatsfilename', 'offlinequiz_statistics');
}
$courseshortname = format_string($course->shortname, true,
array('context' => context_course::instance($course->id)));
$filename = offlinequiz_report_download_filename($report, $courseshortname, $offlinequiz->name);
$this->table->is_downloading($download, $filename,
get_string('offlinequizstructureanalysis', 'offlinequiz_statistics'));
// Load the questions.
// NOTE: function is hacked to deliver question array with question IDs as keys, not the slot as before.
$questions = offlinequiz_report_get_significant_questions($offlinequiz);
$questionids = array_keys($questions);
$fullquestions = question_load_questions($questionids);
foreach ($questions as $quid => $question) {
$q = $fullquestions[$quid];
$q->maxmark = $question->maxmark;
$q->number = $question->number;
$questions[$quid] = $q;
}
// Get the data to be displayed.
list($offlinequizstats, $questions, $subquestions, $s) =
$this->get_offlinequiz_and_questions_stats($offlinequiz, $currentgroup,
$nostudentsingroup, $useallattempts, $groupstudents, $questions);
$offlinequizinfo = $this->get_formatted_offlinequiz_info_data($course, $cm, $offlinequiz, $offlinequizstats);
// Set up the table, if there is data.
if ($s) {
$this->table->statistics_setup($offlinequiz, $cm->id, $reporturl, $s);
}
// Print the page header stuff (if not downloading.
if (!$this->table->is_downloading()) {
$this->print_header_and_tabs($cm, $course, $offlinequiz, $statmode, 'statistics');
// Options for the help text popup_action.
$options = array('width' => 650,
'height' => 400,
'resizable' => false,
'top' => 0,
'left' => 0,
'menubar' => false,
'location' => false,
'scrollbars' => true,
'toolbar' => false,
'status' => false,
'directories' => false,
'fullscreen' => false,
'dependent' => false);
$helpfilename = 'statistics_help_';
if (current_language() == 'de') {
$helpfilename .= 'de.html';
} else {
$helpfilename .= 'en.html';
}
$url = new moodle_url($CFG->wwwroot . '/mod/offlinequiz/report/statistics/help/' . $helpfilename);
$pixicon = new pix_icon('help', get_string('statisticshelp', 'offlinequiz_statistics'));
$helpaction = $OUTPUT->action_icon($url, $pixicon, new popup_action('click', $url, 'help123', $options));
echo $OUTPUT->box_start('linkbox');
echo $OUTPUT->heading(format_string($offlinequiz->name));
echo $OUTPUT->heading(get_string($statmode . 'header', 'offlinequiz_statistics') . $helpaction);
echo $OUTPUT->box_end();
if (!$questionid) {
$this->print_offlinequiz_group_selector($cm, $groups, $groupnumber, $pageoptions);
if ($statmode == 'statsoverview' && ($offlinequiz->sumgrades == -1 || $differentquestions)) {
echo $OUTPUT->box_start();
$notificationmessage = get_string('remarks', 'offlinequiz_statistics') . ":<br />";
if ($offlinequiz->sumgrades == -1) {
$notificationmessage .= '- ' . get_string('differentsumgrades', 'offlinequiz_statistics',
implode(', ', $sumgrades)) . "<br />";
}
if ($differentquestions) {
$notificationmessage .= '- ' . get_string('differentquestions', 'offlinequiz_statistics',
implode(', ', $sumgrades));
}
echo $OUTPUT->notification($notificationmessage, 'notifynote');
echo $OUTPUT->box_end();
}
}
if (groups_get_activity_groupmode($cm)) {
groups_print_activity_menu($cm, $reporturl->out());
if ($currentgroup && !$groupstudents) {
echo $OUTPUT->notification(get_string('nostudentsingroup', 'offlinequiz_statistics'));
}
}
if (!$offlinequiz->questions) {
echo offlinequiz_no_questions_message($offlinequiz, $cm, $this->context);
} else if (!$this->table->is_downloading() && $s == 0) {
echo $OUTPUT->box_start('linkbox');
echo $OUTPUT->notification(get_string('noattempts', 'offlinequiz'), 'notifyproblem');
echo $OUTPUT->box_end();
echo '<br/>';
}
}
if ($everything) { // Implies is downloading.
// Overall report, then the analysis of each question.
if ($statmode == 'statsoverview') {
$this->download_offlinequiz_info_table($offlinequizinfo);
} else if ($statmode == 'questionstats') {
if ($s) {
$this->output_offlinequiz_structure_analysis_table($s, $questions, $subquestions);
if ($this->table->is_downloading() == 'xhtml') {
$this->output_statistics_graph($offlinequizstats->id, $s);
}
// foreach ($questions as $question) {
// if (question_bank::get_qtype(
// $question->qtype, false)->can_analyse_responses()) {
// $this->output_individual_question_response_analysis(
// $question, $reporturl, $offlinequizstats);
// } else if (!empty($question->_stats->subquestions)) {
// $subitemstodisplay = explode(',', $question->_stats->subquestions);
// foreach ($subitemstodisplay as $subitemid) {
// $this->output_individual_question_response_analysis(
// $subquestions[$subitemid], $reporturl, $offlinequizstats);
// }
// }
// }
}
} else if ($statmode == 'questionandanswerstats') {
if ($s) {
$this->output_offlinequiz_structure_analysis_table($s, $questions, $subquestions);
if ($this->table->is_downloading() == 'xhtml') {
$this->output_statistics_graph($offlinequizstats->id, $s);
}
foreach ($questions as $question) {
if (question_bank::get_qtype(
$question->qtype, false)->can_analyse_responses()) {
$this->output_individual_question_response_analysis(
$question, $reporturl, $offlinequizstats);
} else if (!empty($question->_stats->subquestions)) {
$subitemstodisplay = explode(',', $question->_stats->subquestions);
foreach ($subitemstodisplay as $subitemid) {
$this->output_individual_question_response_analysis(
$subquestions[$subitemid], $reporturl, $offlinequizstats);
}
}
}
}
}
$this->table->export_class_instance()->finish_document();
} else if ($questionid) {
// Report on an individual question indexed by position.
if (!isset($questions[$questionid])) {
print_error('questiondoesnotexist', 'question');
}
$this->output_individual_question_data($offlinequiz, $questions[$questionid]);
$this->output_individual_question_response_analysis(
$questions[$questionid], $reporturl, $offlinequizstats);
// Back to overview link.
echo $OUTPUT->box('<a href="' . $reporturl->out() . '">' .
get_string('backtoquestionsandanswers', 'offlinequiz_statistics') . '</a>',
'backtomainstats boxaligncenter backlinkbox generalbox boxwidthnormal mdl-align');
} else if ($qid) {
// Report on an individual sub-question indexed questionid.
if (!isset($subquestions[$qid])) {
print_error('questiondoesnotexist', 'question');
}
$this->output_individual_question_data($offlinequiz, $subquestions[$qid]);
$this->output_individual_question_response_analysis(
$subquestions[$qid], $reporturl, $offlinequizstats);
// Back to overview link.
echo $OUTPUT->box('<a href="' . $reporturl->out() . '">' .
get_string('backtoquestionsandanswers', 'offlinequiz_statistics') . '</a>',
'boxaligncenter backlinkbox generalbox boxwidthnormal mdl-align');
} else {
// On-screen display of overview report.
echo $this->output_caching_info($offlinequizstats, $offlinequiz->id, $currentgroup,
$groupstudents, $useallattempts, $reporturl, $offlinequiz->groupid);
if ($statmode == 'statsoverview') {
echo $this->everything_download_options();
echo '<br/><center>';
echo $this->output_offlinequiz_info_table($offlinequizinfo);
echo '</center>';
} else if ($statmode == 'questionstats') {
if ($s) {
echo '<br/>';
$this->output_offlinequiz_structure_analysis_table($s, $questions, $subquestions);
}
} else if ($statmode == 'questionandanswerstats') {
if ($s) {
echo '<br/>';
$this->output_offlinequiz_question_answer_table($s, $questions, $subquestions, $offlinequizstats);
}
}
}
}
/**
* Checks whether the different offlinequiz groups have different sets of questions (order is irrelevant).
*
* @param unknown_type $offlinequiz
* @param unknown_type $groups
* @return boolean
*/
private function groups_have_different_questions($offlinequiz, $groups) {
$agroup = array_pop($groups);
$aquestions = offlinequiz_get_group_question_ids($offlinequiz, $agroup->id);
// Compare all other groups to the first one.
foreach ($groups as $bgroup) {
$bquestions = offlinequiz_get_group_question_ids($offlinequiz, $bgroup->id);
// Check which questions are in group A but not in group B.
$diff1 = array_diff($aquestions, $bquestions);
// Check which questions are in group B but not in group A.
$diff2 = array_diff($bquestions, $aquestions);
// Return true if there are any differences.
if (!empty($diff1) || !empty($diff2)) {
return true;
}
}
return false;
}
/**
*
* @param unknown_type $cm The course module, needed to construct the base URL
* @param unknown_type $groups The group objects as read from the database
* @param unknown_type $groupnumber The currently chosen group number
*/
private function print_offlinequiz_group_selector($cm, $groups, $groupnumber, $pageoptions) {
global $CFG, $OUTPUT;
$options = array();
$letterstr = 'ABCDEFGH';
$prefix = get_string('statisticsforgroup', 'offlinequiz_statistics');
foreach ($groups as $group) {
$options[$group->number] = $prefix . ' ' . $letterstr[$group->number - 1];
}
$urlparams = array('id' => $cm->id, 'mode' => 'statistics', 'statmode' => $pageoptions['statmode']);
if (key_exists('offlinegroup', $pageoptions)) {
$urlparams['offlinegroup'] = $pageoptions['offlinegroup'];
}
$url = new moodle_url($CFG->wwwroot . '/mod/offlinequiz/report.php', $urlparams);
echo $OUTPUT->single_select($url, 'offlinegroup', $options, $groupnumber,
array(0 => get_string('allgroups', 'offlinequiz_statistics')));
}
/**
* Display the statistical and introductory information about a question.
* Only called when not downloading.
* @param object $offlinequiz the offlinequiz settings.
* @param object $question the question to report on.
* @param moodle_url $reporturl the URL to resisplay this report.
* @param object $offlinequizstats Holds the offlinequiz statistics.
*/
protected function output_individual_question_data($offlinequiz, $question) {
global $OUTPUT;
// On-screen display. Show a summary of the question's place in the offlinequiz,
// and the question statistics.
$datumfromtable = $this->table->format_row($question);
echo '<strong>';
echo $question->name . ' ' . $datumfromtable['actions'] . ' ';
echo '</strong>';
echo $datumfromtable['icon'] . ' ' .
question_bank::get_qtype($question->qtype, false)->menu_name() . ' ' .
$datumfromtable['icon'] . '<br/>';
echo $this->render_question_text_plain($question);
// Set up the question statistics table.
$questionstatstable = new html_table();
$questionstatstable->id = 'questionstatstable';
$questionstatstable->align = array('left', 'right');
$questionstatstable->attributes['class'] = 'generaltable titlesleft';
unset($datumfromtable['number']);
unset($datumfromtable['icon']);
$actions = $datumfromtable['actions'];
unset($datumfromtable['actions']);
unset($datumfromtable['name']);
unset($datumfromtable['response']);
unset($datumfromtable['frequency']);
unset($datumfromtable['count']);
unset($datumfromtable['fraction']);
$labels = array(
's' => get_string('attempts', 'offlinequiz_statistics'),
'facility' => get_string('facility', 'offlinequiz_statistics'),
'sd' => get_string('standarddeviationq', 'offlinequiz_statistics'),
'random_guess_score' => get_string('random_guess_score', 'offlinequiz_statistics'),
'intended_weight' => get_string('intended_weight', 'offlinequiz_statistics'),
'effective_weight' => get_string('effective_weight', 'offlinequiz_statistics'),
'discrimination_index' => get_string('discrimination_index', 'offlinequiz_statistics'),
'discriminative_efficiency' => get_string('discriminative_efficiency', 'offlinequiz_statistics'),
'correct' => get_string('correct', 'offlinequiz_statistics'),
'partially' => get_string('partially', 'offlinequiz_statistics'),
'wrong' => get_string('wrong', 'offlinequiz_statistics'),
);
foreach ($datumfromtable as $item => $value) {
$questionstatstable->data[] = array($labels[$item], $value);
}
// Display the various bits.
echo '<br/>';
echo '<center>';
echo html_writer::table($questionstatstable);
echo '</center>';
}
/**
* @param object $question question data.
* @return string HTML of question text, ready for display.
*/
protected function render_question_text($question) {
global $OUTPUT;
$text = question_rewrite_question_preview_urls($question->questiontext, $question->id,
$question->contextid, 'question', 'questiontext', $question->id,
$this->context->id, 'quiz_statistics');
return $OUTPUT->box(format_text($text, $question->questiontextformat,
array('noclean' => true, 'para' => false, 'overflowdiv' => true)),
'questiontext boxaligncenter generalbox boxwidthnormal mdl-align');
}
/**
* @param object $question question data.
* @return string HTML of question text, ready for display.
*/
protected function render_question_text_plain($question, $showimages = true) {
global $OUTPUT;
if ($showimages) {
$text = question_rewrite_question_preview_urls($question->questiontext, $question->id,
$question->contextid, 'question', 'questiontext', $question->id,
$this->context->id, 'quiz_statistics');
} else {
$text = $question->questiontext;
}
$questiontext = question_utils::to_plain_text($text, $question->questiontextformat,
array('noclean' => true, 'para' => false, 'overflowdiv' => true));
return ' ' . $questiontext;
}
/**
* Display the response analysis for a question.
* @param object $question the question to report on.
* @param moodle_url $reporturl the URL to resisplay this report.
* @param object $offlinequizstats Holds the offlinequiz statistics.
*/
protected function output_individual_question_response_analysis($question,
$reporturl, $offlinequizstats) {
global $OUTPUT;
if (!question_bank::get_qtype($question->qtype, false)->can_analyse_responses()) {
return;
}
$qtable = new offlinequiz_statistics_question_table($question->id);
$qtable->set_attribute('id', 'statisticsquestiontable');
$exportclass = $this->table->export_class_instance();
$qtable->export_class_instance($exportclass);
if (!$this->table->is_downloading()) {
// Output an appropriate title.
echo $OUTPUT->heading(get_string('analysisofresponses', 'offlinequiz_statistics'));
echo $this->render_question_text_plain($question, false);
echo '<br/>';
} else {
// Work out an appropriate title.
$questiontabletitle = '"' . $question->name . '"';
if (!empty($question->number)) {
$questiontabletitle = '(' . $question->number . ') ' . $questiontabletitle;
}
if ($this->table->is_downloading() == 'xhtml') {
$questiontabletitle = get_string('analysisofresponsesfor',
'offlinequiz_statistics', $questiontabletitle);
}
// Set up the table.
$exportclass->start_table($questiontabletitle);
if ($this->table->is_downloading() == 'xhtml') {
echo $this->render_question_text($question);
}
}
$responesstats = new offlinequiz_statistics_response_analyser($question);
$responesstats->load_cached($offlinequizstats->id);
$qtable->question_setup($reporturl, $question, $responesstats);
if ($this->table->is_downloading()) {
$exportclass->output_headers($qtable->headers);
}
$letterstr = 'abcdefghijklmnopqrstuvwxyz';
$counter = 0;
foreach ($responesstats->responseclasses as $partid => $partclasses) {
$rowdata = new stdClass();
foreach ($partclasses as $responseclassid => $responseclass) {
$rowdata->responseclass = $responseclass->responseclass;
$responsesdata = $responesstats->responses[$partid][$responseclassid];
if (empty($responsesdata)) {
if ($responseclass->responseclass != get_string('noresponse', 'question')) {
$rowdata->part = $letterstr[$counter++] . ')';
} else {
$rowdata->part = '';
}
if (!array_key_exists('responseclass', $qtable->columns)) {
$rowdata->response = $responseclass->responseclass;
} else {
$rowdata->response = '';
}
$rowdata->fraction = $responseclass->fraction;
$rowdata->count = 0;
$classname = '';
if ($rowdata->fraction > 0) {
$classname = 'greenrow';
} else if ($rowdata->fraction < 0) {
$classname = 'redrow';
}
$qtable->add_data_keyed($qtable->format_row($rowdata), $classname);
continue;
}
foreach ($responsesdata as $response => $data) {
if ($response != get_string('noresponse', 'question')) {
$rowdata->part = $letterstr[$counter++] . ')';
} else {
$rowdata->part = '';
}
$rowdata->response = $response;
$rowdata->fraction = $data->fraction;
$rowdata->count = $data->count;
$classname = '';
if ($rowdata->fraction > 0) {
$classname = 'greenrow';
} else if ($rowdata->fraction < 0) {
$classname = 'redrow';
}
$qtable->add_data_keyed($qtable->format_row($rowdata), $classname);
break;
}
}
}
$qtable->finish_output(!$this->table->is_downloading());
}
/**
* Output the table that lists all the questions in the offlinequiz with their statistics.
* @param int $s number of attempts.
* @param array $questions the questions in the offlinequiz.
* @param array $subquestions the subquestions of any random questions.
*/
protected function output_offlinequiz_structure_analysis_table($s, $questions, $subquestions) {
if (!$s) {
return;
}
foreach ($questions as $question) {
// Output the data for this question.
$this->table->add_data_keyed($this->table->format_row($question));
if (empty($question->_stats->subquestions)) {
continue;
}
// And its subquestions, if it has any.
$subitemstodisplay = explode(',', $question->_stats->subquestions);
foreach ($subitemstodisplay as $subitemid) {
$subquestions[$subitemid]->maxmark = $question->maxmark;
$this->table->add_data_keyed($this->table->format_row($subquestions[$subitemid]));
}
}
$this->table->finish_output(!$this->table->is_downloading());
}
protected function get_formatted_offlinequiz_info_data($course, $cm, $offlinequiz, $offlinequizstats) {
// You can edit this array to control which statistics are displayed.
$todisplay = array( // Comment in 'firstattemptscount' => 'number'.
'allattemptscount' => 'number',
'maxgrade' => 'number_format',
'bestgrade' => 'scale_to_maxgrade',
'worstgrade' => 'scale_to_maxgrade',
'allattemptsavg' => 'scale_to_maxgrade',
'median' => 'scale_to_maxgrade',
'standarddeviation' => 'scale_to_maxgrade', // The 'summarks_as_percentage'.
'skewness' => 'number_format',
'kurtosis' => 'number_format',
'cic' => 'percent_to_number_format',
'errorratio' => 'number_format_percent',
'standarderror' => 'scale_to_maxgrade');
if ($offlinequiz->sumgrades > 0) {
$offlinequizstats->sumgrades = $offlinequiz->sumgrades;
} else if ($offlinequiz->sumgrades == -1) {
$offlinequizstats->sumgrades = '';
$offlinequizstats->bestgrade = '';
$offlinequizstats->worstgrade = '';
$offlinequizstats->allattemptsavg = '';
$offlinequizstats->median = '';
$offlinequizstats->standarddeviation = '';
}
$offlinequizstats->maxgrade = $offlinequiz->grade;
// General information about the offlinequiz.
$offlinequizinfo = array();
$offlinequizinfo[get_string('offlinequizname', 'offlinequiz_statistics')] = format_string($offlinequiz->name);
if ($cm->idnumber) {
$offlinequizinfo[get_string('idnumbermod')] = $cm->idnumber;
}
if ($offlinequiz->timeopen) {
$offlinequizinfo[get_string('reviewopens', 'offlinequiz')] = userdate($offlinequiz->timeopen);
}
if ($offlinequiz->timeclose) {
$offlinequizinfo[get_string('reviewcloses', 'offlinequiz')] = userdate($offlinequiz->timeclose);
}
if ($offlinequiz->timeopen && $offlinequiz->timeclose) {
$offlinequizinfo[get_string('duration', 'offlinequiz_statistics')] =
format_time($offlinequiz->timeclose - $offlinequiz->timeopen);
}
// The statistics.
foreach ($todisplay as $property => $format) {
if (!isset($offlinequizstats->$property) || empty($format)) {
continue;
}
$value = $offlinequizstats->$property;
switch ($format) {
case 'summarks_as_percentage':
$formattedvalue = offlinequiz_report_scale_summarks_as_percentage($value, $offlinequiz);
break;
case 'scale_to_maxgrade':
$formattedvalue = offlinequiz_report_scale_grade($value, $offlinequiz);
break;
case 'number_format_percent':
$formattedvalue = offlinequiz_format_grade($offlinequiz, $value) . '%';
break;
case 'number_format':
// 2 extra decimal places, since not a percentage,
// and we want the same number of sig figs.???
$formattedvalue = format_float($value, $offlinequiz->decimalpoints);
break;
case 'percent_to_number_format':
$formattedvalue = format_float($value / 100.00, $offlinequiz->decimalpoints);
break;
case 'number':
$formattedvalue = $value + 0;
break;
default:
$formattedvalue = $value;
}
$offlinequizinfo[get_string($property, 'offlinequiz_statistics',
$this->using_attempts_string(!empty($offlinequizstats->allattempts)))] =
$formattedvalue;
}
return $offlinequizinfo;
}
/**
* Output the table that lists all the questions in the offlinequiz with their statistics.
* @param int $s number of attempts.
* @param array $questions the questions in the offlinequiz.
* @param array $subquestions the subquestions of any random questions.
*/
protected function output_offlinequiz_question_answer_table($s, $questions, $subquestions, $offlinequizstats) {
if (!$s) {
return;
}
foreach ($questions as $question) {
// Output the data for this question.
$question->actions = 'actions';
$this->table->add_data_keyed($this->table->format_row($question));
$this->output_question_answers($question, $offlinequizstats);
}
$this->table->finish_output(!$this->table->is_downloading());
}
/**
* Output a question and its answers in one table in a sequence of rows.
*
* @param object $question
*/
protected function output_question_answers($question, $offlinequizstats) {
$exportclass = $this->table->export_class_instance();
$responesstats = new offlinequiz_statistics_response_analyser($question);
$responesstats->load_cached($offlinequizstats->id);
$this->table->set_questiondata($question);
$letterstr = 'abcdefghijklmnopqrstuvwxyz';
$counter = 0;
$counter2 = 0;
foreach ($responesstats->responseclasses as $partid => $partclasses) {
$rowdata = new stdclass();
$partcounter = 0;
foreach ($partclasses as $responseclassid => $responseclass) {
$rowdata->responseclass = $responseclass->responseclass;
$responsesdata = $responesstats->responses[$partid][$responseclassid];
if (empty($responsesdata)) {
$rowdata->part = $letterstr[$counter++] . ')';
$rowdata->response = $responseclass->responseclass;
$rowdata->response = str_ireplace(array('<br />', '<br/>', '<br>', "\r\n"),
array('', '', '', ''), $rowdata->response);
$rowdata->fraction = $responseclass->fraction;
$rowdata->count = 0;
$classname = '';
if ($rowdata->fraction > 0) {
$classname = 'greenrow';
} else if ($rowdata->fraction < 0) {
$classname = 'redrow';
}
if ($counter2 == 0 && $partcounter == 0) {
if ($this->table->is_downloading()) {
$rowdata->name = format_text(strip_tags($question->questiontext), FORMAT_PLAIN);
$rowdata->name = str_ireplace(array('<br />', '<br/>', '<br>', "\r\n"),
array('', '', '', ''), $rowdata->name);
} else {
$rowdata->name = format_text(html_to_text($question->questiontext));
}
} else {
$rowdata->name = '';
}
$rowdata->s = '';
$rowdata->facility = '';
$rowdata->sd = '';
$rowdata->intended_weight = '';
$rowdata->effective_weight = '';
$rowdata->discrimination_index = '';
$this->table->add_data_keyed($this->table->format_row($rowdata), $classname);
$partcounter++;
continue;
} else {
foreach ($responsesdata as $response => $data) {
$rowdata->response = $response;
$rowdata->response = str_ireplace(array('<br />', '<br/>', '<br>', "\r\n"),
array('', '', '', ''), $rowdata->response);
$rowdata->fraction = $data->fraction;
$rowdata->count = $data->count;
$rowdata->part = $letterstr[$counter++] . ')';
$classname = '';
if ($rowdata->fraction > 0) {
$classname = 'greenrow';
} else if ($rowdata->fraction < 0) {
$classname = 'redrow';
}
if ($counter2 == 0 && $partcounter == 0) {
if ($this->table->is_downloading()) {
$rowdata->name = format_text(strip_tags($question->questiontext), FORMAT_PLAIN);
$rowdata->name = str_ireplace(array('<br />', '<br/>', '<br>', "\r\n"),
array('', '', '', ''), $rowdata->name);
} else {
$rowdata->name = format_text(html_to_text($question->questiontext));
}
} else {
$rowdata->name = '';
}
$rowdata->s = '';
$rowdata->facility = '';
$rowdata->sd = '';
$rowdata->intended_weight = '';
$rowdata->effective_weight = '';
$rowdata->discrimination_index = '';
$this->table->add_data_keyed($this->table->format_row($rowdata), $classname);
$partcounter++;
break; // We want to display every response only once.
}
}
}
$counter2++;
}
}
/**
* Output the table of overall offlinequiz statistics.
* @param array $offlinequizinfo as returned by {@link get_formatted_offlinequiz_info_data()}.
* @return string the HTML.
*/
protected function output_offlinequiz_info_table($offlinequizinfo) {
$offlinequizinfotable = new html_table();
$offlinequizinfotable->id = 'statsoverviewtable';
$offlinequizinfotable->align = array('left', 'right');
$offlinequizinfotable->attributes['class'] = 'generaltable titlesleft';
$offlinequizinfotable->data = array();
foreach ($offlinequizinfo as $heading => $value) {
$offlinequizinfotable->data[] = array($heading, $value);
}
return html_writer::table($offlinequizinfotable);
}
/**
* Download the table of overall offlinequiz statistics.
* @param array $offlinequizinfo as returned by {@link get_formatted_offlinequiz_info_data()}.
*/
protected function download_offlinequiz_info_table($offlinequizinfo) {
global $OUTPUT;
// XHTML download is a special case.
if ($this->table->is_downloading() == 'xhtml') {
echo $OUTPUT->heading(get_string('offlinequizinformation', 'offlinequiz_statistics'));
echo $this->output_offlinequiz_info_table($offlinequizinfo);
return;
}
// Reformat the data ready for output.
$headers = array();
$row = array();
foreach ($offlinequizinfo as $heading => $value) {
$headers[] = $heading;
if (is_double($value)) {
$row[] = format_float($value, 2);
} else {
$row[] = $value;
}
}
// Do the output.
$exportclass = $this->table->export_class_instance();
$exportclass->start_table(get_string('offlinequizinformation', 'offlinequiz_statistics'));
$exportclass->output_headers($headers);
$exportclass->add_data($row);
$exportclass->finish_table();
}
/**
* Output the HTML needed to show the statistics graph.
* @param int $offlinequizstatsid the id of the statistics to show in the graph.
*/
protected function output_statistics_graph($offlinequizstatsid, $s) {
global $PAGE;
if ($s == 0) {
return;
}
$output = $PAGE->get_renderer('mod_offlinequiz');
$imageurl = new moodle_url('/mod/offlinequiz/report/statistics/statistics_graph.php',
array('id' => $offlinequizstatsid));
$graphname = get_string('statisticsreportgraph', 'offlinequiz_statistics');
echo $output->graph($imageurl, $graphname);
}
/**
* Return the stats data for when there are no stats to show.
*
* @param array $questions question definitions.
* @param int $firstattemptscount number of first attempts (optional).
* @param int $firstattemptscount total number of attempts (optional).
* @return array with three elements:
* - integer $s Number of attempts included in the stats (0).
* - array $offlinequizstats The statistics for overall attempt scores.
* - array $qstats The statistics for each question.
*/
protected function get_emtpy_stats($questions, $firstattemptscount = 0,
$allattemptscount = 0) {
$offlinequizstats = new stdClass();
$offlinequizstats->firstattemptscount = $firstattemptscount;
$offlinequizstats->allattemptscount = $allattemptscount;
$qstats = new stdClass();
$qstats->questions = $questions;
$qstats->subquestions = array();
$qstats->responses = array();
return array(0, $offlinequizstats, false);
}
/**
* Compute the offlinequiz statistics.
*
* @param object $offlinequizid the offlinequiz id.
* @param int $currentgroup the current group. 0 for none.
* @param bool $nostudentsingroup true if there a no students.
* @param bool $useallattempts use all attempts, or just first attempts.