Skip to content

Commit

Permalink
MDL-19054 quiz/report/statistics: Add chart statistics to multiple ch…
Browse files Browse the repository at this point in the history
…oice questions
  • Loading branch information
nadavkav committed Oct 16, 2024
1 parent fc29add commit 7ed13db
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion mod/quiz/report/statistics/report.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
use core_question\statistics\responses\analyser;
use mod_quiz\local\reports\report_base;
use core_question\statistics\questions\all_calculated_for_qubaid_condition;
use core\chart_pie;
use core\chart_series;

require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
require_once($CFG->dirroot . '/mod/quiz/report/statistics/statistics_form.php');
Expand Down Expand Up @@ -432,7 +434,7 @@ protected function output_individual_question_response_analysis($question, $vari
if ($this->table->is_downloading()) {
$exportclass->output_headers($qtable->headers);
}

$chartdata = [];
// Where no variant no is specified the variant no is actually one.
if ($variantno === null) {
$variantno = 1;
Expand All @@ -442,13 +444,29 @@ protected function output_individual_question_response_analysis($question, $vari
foreach ($subpart->get_response_class_ids() as $responseclassid) {
$responseclass = $subpart->get_response_class($responseclassid);
$tabledata = $responseclass->data_for_question_response_table($subpart->has_multiple_response_classes(), $partid);
$chartdata[] = ['label'=> $tabledata[0]->response, 'value' => $tabledata[0]->totalcount ];
foreach ($tabledata as $row) {
$qtable->add_data_keyed($qtable->format_row($row));
}
}
}

$qtable->finish_output(!$this->table->is_downloading());

// Extract labels and counts
$labels = array_column($chartdata, 'label');
$counts = array_column($chartdata, 'value');

// Create the chart
$chart = new chart_pie();
$series = new chart_series('Quiz Results', $counts);

// Add series and labels
$chart->add_series($series);
$chart->set_labels($labels);

// Render the chart in the report page
echo $OUTPUT->render($chart);
}

/**
Expand Down

0 comments on commit 7ed13db

Please sign in to comment.