public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) { global $OUTPUT; $analysed_item = $this->get_analysed($item, $groupid, $courseid); if ($analysed_item) { echo "<table class=\"analysis itemtype_{$item->typ}\">"; echo '<tr><th colspan="2" align="left">'; echo $itemnr . ' '; if (strval($item->label) !== '') { echo '(' . format_string($item->label) . ') '; } echo format_string($analysed_item[1]); echo '</th></tr>'; echo '</table>'; $analysed_vals = $analysed_item[2]; $avg = 0.0; $count = 0; $data = []; foreach ($analysed_vals as $val) { $avg += $val->avg; $quotient = format_float($val->quotient * 100, 2); $answertext = '(' . $val->value . ') ' . format_text(trim($val->answertext), FORMAT_HTML, array('noclean' => true, 'para' => false)); if ($val->quotient > 0) { $strquotient = ' (' . $quotient . ' %)'; } else { $strquotient = ''; } $data['labels'][$count] = $answertext; $data['series'][$count] = $val->answercount; $data['series_labels'][$count] = $val->answercount . $strquotient; $count++; } $chart = new \core\chart_bar(); $chart->set_horizontal(true); $series = new \core\chart_series(format_string(get_string("responses", "feedback")), $data['series']); $series->set_labels($data['series_labels']); $chart->add_series($series); $chart->set_labels($data['labels']); echo $OUTPUT->render($chart); $avg = format_float($avg, 2); echo '<tr><td align="left" colspan="2"><b>'; echo get_string('average', 'feedback') . ': ' . $avg . '</b>'; echo '</td></tr>'; } }
/** * Generate the choice result chart. * * Can be displayed either in the vertical or horizontal position. * * @param stdClass $choices Choices responses object. * @param int $displaylayout The constants DISPLAY_HORIZONTAL_LAYOUT or DISPLAY_VERTICAL_LAYOUT. * @return string the rendered chart. */ public function display_publish_anonymous($choices, $displaylayout) { global $OUTPUT; $count = 0; $data = []; $numberofuser = 0; $percentageamount = 0; foreach ($choices->options as $optionid => $option) { if (!empty($option->user)) { $numberofuser = count($option->user); } if ($choices->numberofuser > 0) { $percentageamount = (double) $numberofuser / (double) $choices->numberofuser * 100.0; } $data['labels'][$count] = $option->text; $data['series'][$count] = $numberofuser; $data['series_labels'][$count] = $numberofuser . ' (' . format_float($percentageamount, 1) . '%)'; $count++; $numberofuser = 0; } $chart = new \core\chart_bar(); if ($displaylayout == DISPLAY_HORIZONTAL_LAYOUT) { $chart->set_horizontal(true); } $series = new \core\chart_series(format_string(get_string("responses", "choice")), $data['series']); $series->set_labels($data['series_labels']); $chart->add_series($series); $chart->set_labels($data['labels']); $yaxis = $chart->get_yaxis(0, true); $yaxis->set_stepsize(max(1, round(max($data['series']) / 10))); return $OUTPUT->render($chart); }