/** * Output the HTML needed to show the statistics graph. * * @param int|object $quizorid The quiz, or its ID. * @param qubaid_condition $qubaids the question usages whose responses to analyse. * @param string $whichattempts Which attempts constant. */ protected function output_statistics_graph($quizorid, $qubaids) { global $DB, $PAGE; $quiz = $quizorid; if (!is_object($quiz)) { $quiz = $DB->get_record('quiz', array('id' => $quizorid), '*', MUST_EXIST); } // 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'); $graphname = get_string('statisticsreportgraph', 'quiz_statistics'); echo $output->chart($chart, $graphname); }
/** * Get a chart. * * @param string[] $labels Chart labels. * @param int[] $data The data. * @return \core\chart_base */ protected static function get_chart($labels, $data) { $chart = new \core\chart_bar(); $chart->set_labels($labels); $chart->get_xaxis(0, true)->set_label(get_string('grade')); $yaxis = $chart->get_yaxis(0, true); $yaxis->set_label(get_string('participants')); $yaxis->set_stepsize(max(1, round(max($data) / 10))); $series = new \core\chart_series(get_string('participants'), $data); $chart->add_series($series); return $chart; }