MDL-43079 quiz statistics : fix stats graph when all grades zero
[moodle.git] / mod / quiz / report / statistics / statistics_graph.php
CommitLineData
aeb15530 1<?php
04853f27
TH
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * This script renders the quiz statistics graph.
19 *
20 * It takes one parameter, the quiz_statistics.id. This is enough to identify the
21 * quiz etc.
22 *
23 * It plots a bar graph showing certain question statistics plotted against
24 * question number.
25 *
8d76124c
TH
26 * @package quiz_statistics
27 * @copyright 2008 Jamie Pratt
28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
04853f27
TH
29 */
30
04853f27
TH
31require_once(dirname(__FILE__) . '/../../../../config.php');
32require_once($CFG->libdir . '/graphlib.php');
33require_once($CFG->dirroot . '/mod/quiz/locallib.php');
34require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
e68e4ccf 35require_once($CFG->dirroot . '/mod/quiz/report/statistics/statisticslib.php');
04853f27
TH
36
37// Get the parameters.
e68e4ccf
JP
38$quizid = required_param('quizid', PARAM_INT);
39$currentgroup = required_param('currentgroup', PARAM_INT);
6dd9362e 40$whichattempts = required_param('whichattempts', PARAM_INT);
43ec99aa 41
e68e4ccf 42$quiz = $DB->get_record('quiz', array('id' => $quizid), '*', MUST_EXIST);
43ec99aa 43$cm = get_coursemodule_from_instance('quiz', $quiz->id);
04853f27
TH
44
45// Check access.
46require_login($quiz->course, false, $cm);
26aded55 47$modcontext = context_module::instance($cm->id);
04853f27
TH
48require_capability('quiz/statistics:view', $modcontext);
49
50if (groups_get_activity_groupmode($cm)) {
43ec99aa 51 $groups = groups_get_activity_allowed_groups($cm);
52} else {
04853f27 53 $groups = array();
43ec99aa 54}
e68e4ccf 55if ($currentgroup && !in_array($currentgroup, array_keys($groups))) {
04853f27 56 print_error('groupnotamember', 'group');
43ec99aa 57}
e68e4ccf 58
6dd9362e
JP
59if (empty($currentgroup)) {
60 $groupstudents = array();
61} else {
62 $groupstudents = get_users_by_capability($modcontext, array('mod/quiz:reviewmyattempts', 'mod/quiz:attempt'),
63 '', '', '', '', $currentgroup, '', false);
64}
65$qubaids = quiz_statistics_qubaids_condition($quizid, $groupstudents, $whichattempts);
43ec99aa 66
04853f27
TH
67// Load the rest of the required data.
68$questions = quiz_report_get_significant_questions($quiz);
e68e4ccf 69
7de1e35b 70// Only load main question not sub questions.
e68e4ccf
JP
71$questionstatistics = $DB->get_records_select('question_statistics', 'hashcode = ? AND slot IS NOT NULL',
72 array($qubaids->get_hash_code()));
43ec99aa 73
04853f27 74// Create the graph, and set the basic options.
55ca80ed 75$graph = new graph(800, 600);
04853f27 76$graph->parameter['title'] = '';
43ec99aa 77
04853f27
TH
78$graph->parameter['y_label_left'] = '%';
79$graph->parameter['x_label'] = get_string('position', 'quiz_statistics');
80$graph->parameter['y_label_angle'] = 90;
81$graph->parameter['x_label_angle'] = 0;
82$graph->parameter['x_axis_angle'] = 60;
43ec99aa 83
04853f27
TH
84$graph->parameter['legend'] = 'outside-right';
85$graph->parameter['legend_border'] = 'black';
86$graph->parameter['legend_offset'] = 4;
869309b8 87
04853f27 88$graph->parameter['bar_size'] = 1;
869309b8 89
04853f27 90$graph->parameter['zero_axis'] = 'grayEE';
43ec99aa 91
04853f27
TH
92// Configure what to display.
93$fieldstoplot = array(
94 'facility' => get_string('facility', 'quiz_statistics'),
95 'discriminativeefficiency' => get_string('discriminative_efficiency', 'quiz_statistics')
96);
43ec99aa 97$fieldstoplotfactor = array('facility' => 100, 'discriminativeefficiency' => 1);
98
04853f27
TH
99// Prepare the arrays to hold the data.
100$xdata = array();
101foreach (array_keys($fieldstoplot) as $fieldtoplot) {
102 $ydata[$fieldtoplot] = array();
103 $graph->y_format[$fieldtoplot] = array(
e68e4ccf 104 'colour' => quiz_statistics_graph_get_new_colour(),
04853f27
TH
105 'bar' => 'fill',
106 'shadow_offset' => 1,
107 'legend' => $fieldstoplot[$fieldtoplot]
108 );
43ec99aa 109}
04853f27
TH
110
111// Fill in the data for each question.
112foreach ($questionstatistics as $questionstatistic) {
113 $number = $questions[$questionstatistic->slot]->number;
114 $xdata[$number] = $number;
115
116 foreach ($fieldstoplot as $fieldtoplot => $notused) {
117 $value = $questionstatistic->$fieldtoplot;
118 if (is_null($value)) {
119 $value = 0;
120 }
121 $value *= $fieldstoplotfactor[$fieldtoplot];
122
123 $ydata[$fieldtoplot][$number] = $value;
43ec99aa 124 }
125}
04853f27
TH
126
127// Sort the fields into order.
128sort($xdata);
129$graph->x_data = array_values($xdata);
130
131foreach ($fieldstoplot as $fieldtoplot => $notused) {
132 ksort($ydata[$fieldtoplot]);
133 $graph->y_data[$fieldtoplot] = array_values($ydata[$fieldtoplot]);
869309b8 134}
04853f27
TH
135$graph->y_order = array_keys($fieldstoplot);
136
137// Find appropriate axis limits.
43ec99aa 138$max = 0;
139$min = 0;
04853f27
TH
140foreach ($fieldstoplot as $fieldtoplot => $notused) {
141 $max = max($max, max($graph->y_data[$fieldtoplot]));
142 $min = min($min, min($graph->y_data[$fieldtoplot]));
43ec99aa 143}
43ec99aa 144
04853f27 145// Set the remaining graph options that depend on the data.
869309b8 146$gridresolution = 10;
869309b8 147$max = ceil($max / $gridresolution) * $gridresolution;
148$min = floor($min / $gridresolution) * $gridresolution;
34292e21
JP
149
150if ($max == $min) {
151 // Make sure there is some difference between min and max y values.
152 $max = $min + $gridresolution;
153}
154
04853f27 155$gridlines = ceil(($max - $min) / $gridresolution) + 1;
869309b8 156
04853f27 157$graph->parameter['y_axis_gridlines'] = $gridlines;
43ec99aa 158
04853f27
TH
159$graph->parameter['y_min_left'] = $min;
160$graph->parameter['y_max_left'] = $max;
161$graph->parameter['y_decimal_left'] = 0;
aeb15530 162
04853f27
TH
163// Output the graph.
164$graph->draw();