MDL-55026 quiz_statistics: Convert charts to the new library

Part of MDL-54987 epic.
This commit is contained in:
Frederic Massart 2016-06-27 16:29:14 +08:00 committed by Dan Poltawski
parent ec882623f3
commit baea27e4d3
4 changed files with 95 additions and 141 deletions

View File

@ -265,7 +265,7 @@ class quiz_statistics_report extends quiz_default_report {
if ($quizstats->s()) {
echo $OUTPUT->heading(get_string('quizstructureanalysis', 'quiz_statistics'), 3);
$this->output_quiz_structure_analysis_table($questionstats);
$this->output_statistics_graph($quiz->id, $currentgroup, $whichattempts);
$this->output_statistics_graph($quiz, $currentgroup, $whichattempts);
}
}
@ -510,18 +510,89 @@ class quiz_statistics_report extends quiz_default_report {
/**
* Output the HTML needed to show the statistics graph.
*
* @param $quizid
* @param $currentgroup
* @param $whichattempts
* @param int|object $quizorid The quiz, or its ID.
* @param int $currentgroup The current group.
* @param string $whichattempts Which attempts constant.
*/
protected function output_statistics_graph($quizid, $currentgroup, $whichattempts) {
global $PAGE;
protected function output_statistics_graph($quizorid, $currentgroup, $whichattempts) {
global $DB, $PAGE;
$quiz = $quizorid;
if (!is_object($quiz)) {
$quiz = $DB->get_record('quiz', array('id' => $quizorid), '*', MUST_EXIST);
}
$quizid = $quiz->id;
if (empty($currentgroup)) {
$groupstudents = [];
} else {
$groupstudents = get_users_by_capability($this->context, array('mod/quiz:reviewmyattempts', 'mod/quiz:attempt'),
'', '', '', '', $currentgroup, '', false);
}
$qubaids = quiz_statistics_qubaids_condition($quizid, $groupstudents, $whichattempts);
// Load the rest of the required data.
$questions = quiz_report_get_significant_questions($quiz);
// Only load main question not sub questions.
$questionstatistics = $DB->get_records_select('question_statistics', 'hashcode = ? AND slot IS NOT NULL',
[$qubaids->get_hash_code()]);
// Configure what to display.
$fieldstoplot = [
'facility' => get_string('facility', 'quiz_statistics'),
'discriminativeefficiency' => get_string('discriminative_efficiency', 'quiz_statistics')
];
$fieldstoplotfactor = ['facility' => 100, 'discriminativeefficiency' => 1];
// Prepare the arrays to hold the data.
$xdata = [];
foreach (array_keys($fieldstoplot) as $fieldtoplot) {
$ydata[$fieldtoplot] = [];
}
// Fill in the data for each question.
foreach ($questionstatistics as $questionstatistic) {
$number = $questions[$questionstatistic->slot]->number;
$xdata[$number] = $number;
foreach ($fieldstoplot as $fieldtoplot => $notused) {
$value = $questionstatistic->$fieldtoplot;
if (is_null($value)) {
$value = 0;
}
$value *= $fieldstoplotfactor[$fieldtoplot];
$ydata[$fieldtoplot][$number] = number_format($value, 2);
}
}
// Create the chart.
sort($xdata);
$chart = new \core\chart_bar();
$chart->get_xaxis(0, true)->set_label(get_string('position', 'quiz_statistics'));
$chart->set_labels(array_values($xdata));
foreach ($fieldstoplot as $fieldtoplot => $notused) {
ksort($ydata[$fieldtoplot]);
$series = new \core\chart_series($fieldstoplot[$fieldtoplot], array_values($ydata[$fieldtoplot]));
$chart->add_series($series);
}
// Find max.
$max = 0;
foreach ($fieldstoplot as $fieldtoplot => $notused) {
$max = max($max, max($ydata[$fieldtoplot]));
}
// Set Y properties.
$yaxis = $chart->get_yaxis(0, true);
$yaxis->set_stepsize(10);
$yaxis->set_label('%');
$output = $PAGE->get_renderer('mod_quiz');
$imageurl = new moodle_url('/mod/quiz/report/statistics/statistics_graph.php',
compact('quizid', 'currentgroup', 'whichattempts'));
$graphname = get_string('statisticsreportgraph', 'quiz_statistics');
echo $output->graph($imageurl, $graphname);
echo $output->chart($chart, $graphname);
}
/**

View File

@ -26,139 +26,11 @@
* @package quiz_statistics
* @copyright 2008 Jamie Pratt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @deprecated since Moodle 3.2
*/
require_once(__DIR__ . '/../../../../config.php');
require_once($CFG->libdir . '/graphlib.php');
require_once($CFG->dirroot . '/mod/quiz/locallib.php');
require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
require_once($CFG->dirroot . '/mod/quiz/report/statistics/statisticslib.php');
require_once($CFG->libdir . '/filelib.php');
// Get the parameters.
$quizid = required_param('quizid', PARAM_INT);
$currentgroup = required_param('currentgroup', PARAM_INT);
$whichattempts = required_param('whichattempts', PARAM_INT);
$quiz = $DB->get_record('quiz', array('id' => $quizid), '*', MUST_EXIST);
$cm = get_coursemodule_from_instance('quiz', $quiz->id);
// Check access.
require_login($quiz->course, false, $cm);
$modcontext = context_module::instance($cm->id);
require_capability('quiz/statistics:view', $modcontext);
if (groups_get_activity_groupmode($cm)) {
$groups = groups_get_activity_allowed_groups($cm);
} else {
$groups = array();
}
if ($currentgroup && !in_array($currentgroup, array_keys($groups))) {
print_error('groupnotamember', 'group');
}
if (empty($currentgroup)) {
$groupstudents = array();
} else {
$groupstudents = get_users_by_capability($modcontext, array('mod/quiz:reviewmyattempts', 'mod/quiz:attempt'),
'', '', '', '', $currentgroup, '', false);
}
$qubaids = quiz_statistics_qubaids_condition($quizid, $groupstudents, $whichattempts);
// Load the rest of the required data.
$questions = quiz_report_get_significant_questions($quiz);
// Only load main question not sub questions.
$questionstatistics = $DB->get_records_select('question_statistics', 'hashcode = ? AND slot IS NOT NULL',
array($qubaids->get_hash_code()));
// Create the graph, and set the basic options.
$graph = new graph(800, 600);
$graph->parameter['title'] = '';
$graph->parameter['y_label_left'] = '%';
$graph->parameter['x_label'] = get_string('position', 'quiz_statistics');
$graph->parameter['y_label_angle'] = 90;
$graph->parameter['x_label_angle'] = 0;
$graph->parameter['x_axis_angle'] = 60;
$graph->parameter['legend'] = 'outside-right';
$graph->parameter['legend_border'] = 'black';
$graph->parameter['legend_offset'] = 4;
$graph->parameter['bar_size'] = 1;
$graph->parameter['zero_axis'] = 'grayEE';
// Configure what to display.
$fieldstoplot = array(
'facility' => get_string('facility', 'quiz_statistics'),
'discriminativeefficiency' => get_string('discriminative_efficiency', 'quiz_statistics')
);
$fieldstoplotfactor = array('facility' => 100, 'discriminativeefficiency' => 1);
// Prepare the arrays to hold the data.
$xdata = array();
foreach (array_keys($fieldstoplot) as $fieldtoplot) {
$ydata[$fieldtoplot] = array();
$graph->y_format[$fieldtoplot] = array(
'colour' => quiz_statistics_graph_get_new_colour(),
'bar' => 'fill',
'shadow_offset' => 1,
'legend' => $fieldstoplot[$fieldtoplot]
);
}
// Fill in the data for each question.
foreach ($questionstatistics as $questionstatistic) {
$number = $questions[$questionstatistic->slot]->number;
$xdata[$number] = $number;
foreach ($fieldstoplot as $fieldtoplot => $notused) {
$value = $questionstatistic->$fieldtoplot;
if (is_null($value)) {
$value = 0;
}
$value *= $fieldstoplotfactor[$fieldtoplot];
$ydata[$fieldtoplot][$number] = $value;
}
}
// Sort the fields into order.
sort($xdata);
$graph->x_data = array_values($xdata);
foreach ($fieldstoplot as $fieldtoplot => $notused) {
ksort($ydata[$fieldtoplot]);
$graph->y_data[$fieldtoplot] = array_values($ydata[$fieldtoplot]);
}
$graph->y_order = array_keys($fieldstoplot);
// Find appropriate axis limits.
$max = 0;
$min = 0;
foreach ($fieldstoplot as $fieldtoplot => $notused) {
$max = max($max, max($graph->y_data[$fieldtoplot]));
$min = min($min, min($graph->y_data[$fieldtoplot]));
}
// Set the remaining graph options that depend on the data.
$gridresolution = 10;
$max = ceil($max / $gridresolution) * $gridresolution;
$min = floor($min / $gridresolution) * $gridresolution;
if ($max == $min) {
// Make sure there is some difference between min and max y values.
$max = $min + $gridresolution;
}
$gridlines = ceil(($max - $min) / $gridresolution) + 1;
$graph->parameter['y_axis_gridlines'] = $gridlines;
$graph->parameter['y_min_left'] = $min;
$graph->parameter['y_max_left'] = $max;
$graph->parameter['y_decimal_left'] = 0;
// Output the graph.
$graph->draw();
debugging('This way of generating the chart is deprecated, refer to quiz_statistics_report::display().', DEBUG_DEVELOPER);
send_file_not_found();

View File

@ -111,8 +111,12 @@ function quiz_statistics_qubaids_condition($quizid, $groupstudents, $whichattemp
* This helper function returns a sequence of colours each time it is called.
* Used for choosing colours for graph data series.
* @return string colour name.
* @deprecated since Moodle 3.2
*/
function quiz_statistics_graph_get_new_colour() {
debugging('The function quiz_statistics_graph_get_new_colour() is deprecated, please do not use it any more. '
. 'Colours will be handled by the charting library directly.', DEBUG_DEVELOPER);
static $colourindex = -1;
$colours = array('red', 'green', 'yellow', 'orange', 'purple', 'black',
'maroon', 'blue', 'ltgreen', 'navy', 'ltred', 'ltltgreen', 'ltltorange',

View File

@ -0,0 +1,7 @@
This files describes API changes in /mod/quiz/report/statistics/*,
information provided here is intended especially for developers.
=== 3.2 ===
* The function quiz_statistics_graph_get_new_colour() is deprecated in favour of the
funtionality present in the new charting library.