From 1850e97e376b8ea34003d500c193b0dcdf5b30fc Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Thu, 14 Jul 2016 15:41:08 +0800 Subject: [PATCH] MDL-55208 mod_choice: Convert choice results chart to new library Part of MDL-54987 epic. --- mod/choice/renderer.php | 219 ++++++++-------------------------------- 1 file changed, 41 insertions(+), 178 deletions(-) diff --git a/mod/choice/renderer.php b/mod/choice/renderer.php index 7cc98722f40..a1257f08050 100644 --- a/mod/choice/renderer.php +++ b/mod/choice/renderer.php @@ -117,11 +117,8 @@ class mod_choice_renderer extends plugin_renderer_base { if ($forcepublish) { //CHOICE_PUBLISH_NAMES return $this->display_publish_name_vertical($choices); - } else { //CHOICE_PUBLISH_ANONYMOUS'; - if ($displaylayout == DISPLAY_HORIZONTAL_LAYOUT) { - return $this->display_publish_anonymous_horizontal($choices); - } - return $this->display_publish_anonymous_vertical($choices); + } else { + return $this->display_publish_anonymous($choices, $displaylayout); } } @@ -282,200 +279,66 @@ class mod_choice_renderer extends plugin_renderer_base { /** * Returns HTML to display choices result + * @deprecated since 3.2 * @param object $choices * @return string */ public function display_publish_anonymous_horizontal($choices) { global $CHOICE_COLUMN_HEIGHT; - - $html = ''; - $table = new html_table(); - $table->cellpadding = 5; - $table->cellspacing = 0; - $table->attributes['class'] = 'results anonymous '; - $table->summary = get_string('responsesto', 'choice', format_string($choices->name)); - $table->data = array(); - - $count = 0; - ksort($choices->options); - $columns = array(); - $rows = array(); - - $headercelldefault = new html_table_cell(); - $headercelldefault->scope = 'row'; - $headercelldefault->header = true; - $headercelldefault->attributes = array('class'=>'header data'); - - // column header - $tableheader = clone($headercelldefault); - $tableheader->text = html_writer::tag('div', get_string('choiceoptions', 'choice'), array('class' => 'accesshide')); - $rows['header'][] = $tableheader; - - // graph row header - $graphheader = clone($headercelldefault); - $graphheader->text = html_writer::tag('div', get_string('responsesresultgraphheader', 'choice'), array('class' => 'accesshide')); - $rows['graph'][] = $graphheader; - - // user number row header - $usernumberheader = clone($headercelldefault); - $usernumberheader->text = get_string('numberofuser', 'choice'); - $rows['usernumber'][] = $usernumberheader; - - // user percentage row header - $userpercentageheader = clone($headercelldefault); - $userpercentageheader->text = get_string('numberofuserinpercentage', 'choice'); - $rows['userpercentage'][] = $userpercentageheader; - - $contentcelldefault = new html_table_cell(); - $contentcelldefault->attributes = array('class'=>'data'); - - foreach ($choices->options as $optionid => $option) { - // calculate display length - $height = $percentageamount = $numberofuser = 0; - $usernumber = $userpercentage = ''; - - if (!empty($option->user)) { - $numberofuser = count($option->user); - } - - if($choices->numberofuser > 0) { - $height = ($CHOICE_COLUMN_HEIGHT * ((float)$numberofuser / (float)$choices->numberofuser)); - $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0; - } - - $displaygraph = html_writer::tag('img','', array('style'=>'height:'.$height.'px;width:49px;', 'alt'=>'', 'src'=>$this->output->pix_url('column', 'choice'))); - - // header - $headercell = clone($contentcelldefault); - $headercell->text = $option->text; - $rows['header'][] = $headercell; - - // Graph - $graphcell = clone($contentcelldefault); - $graphcell->attributes = array('class'=>'graph vertical data'); - $graphcell->text = $displaygraph; - $rows['graph'][] = $graphcell; - - $usernumber .= html_writer::tag('div', ' '.$numberofuser.'', array('class'=>'numberofuser', 'title'=> get_string('numberofuser', 'choice'))); - $userpercentage .= html_writer::tag('div', format_float($percentageamount,1). '%', array('class'=>'percentage')); - - // number of user - $usernumbercell = clone($contentcelldefault); - $usernumbercell->text = $usernumber; - $rows['usernumber'][] = $usernumbercell; - - // percentage of user - $numbercell = clone($contentcelldefault); - $numbercell->text = $userpercentage; - $rows['userpercentage'][] = $numbercell; - } - - $table->head = $rows['header']; - $trgraph = new html_table_row($rows['graph']); - $trusernumber = new html_table_row($rows['usernumber']); - $truserpercentage = new html_table_row($rows['userpercentage']); - $table->data = array($trgraph, $trusernumber, $truserpercentage); - - $header = html_writer::tag('h3',format_string(get_string("responses", "choice"))); - $html .= html_writer::tag('div', $header, array('class'=>'responseheader')); - $html .= html_writer::tag('div', html_writer::table($table), array('class'=>'response')); - - return $html; + debugging(__FUNCTION__.'() is deprecated. Please use mod_choice_renderer::display_publish_anonymous() instead.', + DEBUG_DEVELOPER); + return $this->display_publish_anonymous($choices, CHOICE_DISPLAY_VERTICAL); } /** * Returns HTML to display choices result + * @deprecated since 3.2 * @param object $choices * @return string */ public function display_publish_anonymous_vertical($choices) { global $CHOICE_COLUMN_WIDTH; + debugging(__FUNCTION__.'() is deprecated. Please use mod_choice_renderer::display_publish_anonymous() instead.', + DEBUG_DEVELOPER); + return $this->display_publish_anonymous($choices, CHOICE_DISPLAY_HORIZONTAL); + } - $table = new html_table(); - $table->cellpadding = 5; - $table->cellspacing = 0; - $table->attributes['class'] = 'results anonymous '; - $table->summary = get_string('responsesto', 'choice', format_string($choices->name)); - $table->data = array(); - - $columnheaderdefault = new html_table_cell(); - $columnheaderdefault->scope = 'col'; - - $tableheadertext = clone($columnheaderdefault); - $tableheadertext->text = get_string('choiceoptions', 'choice'); - - $tableheadernumber = clone($columnheaderdefault); - $tableheadernumber->text = get_string('numberofuser', 'choice'); - - $tableheaderpercentage = clone($columnheaderdefault); - $tableheaderpercentage->text = get_string('numberofuserinpercentage', 'choice'); - - $tableheadergraph = clone($columnheaderdefault); - $tableheadergraph->text = get_string('responsesresultgraphheader', 'choice'); - - $table->head = array($tableheadertext, $tableheadernumber, $tableheaderpercentage, $tableheadergraph); - + /** + * 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; - ksort($choices->options); - - $columndefault = new html_table_cell(); - $columndefault->attributes['class'] = 'data'; - - $colheaderdefault = new html_table_cell(); - $colheaderdefault->scope = 'row'; - $colheaderdefault->header = true; - $colheaderdefault->attributes['class'] = 'header data'; - - $rows = array(); - foreach ($choices->options as $optionid => $options) { - $colheader = clone($colheaderdefault); - $colheader->text = $options->text; - - $graphcell = clone($columndefault); - $datacellnumber = clone($columndefault); - $datacellpercentage = clone($columndefault); - - $numberofuser = $width = $percentageamount = 0; - - if (!empty($options->user)) { - $numberofuser = count($options->user); - } - + $data = []; + $percentageamount = 0; + foreach ($choices->options as $optionid => $option) { if($choices->numberofuser > 0) { - $width = ($CHOICE_COLUMN_WIDTH * ((float)$numberofuser / (float)$choices->numberofuser)); - $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0; + $percentageamount = ((float)count($option->user)/(float)$choices->numberofuser)*100.0; } - $attributes = array(); - $attributes['style'] = 'height:50px; width:'.$width.'px'; - $attributes['alt'] = ''; - $attributes['src'] = $this->output->pix_url('row', 'choice'); - $displaydiagram = html_writer::tag('img','', $attributes); - - $graphcell->text = $displaydiagram; - $graphcell->attributes = array('class'=>'graph horizontal'); - - if($choices->numberofuser > 0) { - $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0; - } - - $datacellnumber->text = $numberofuser; - $datacellpercentage->text = format_float($percentageamount,1). '%'; - - - $row = new html_table_row(); - $row->cells = array($colheader, $datacellnumber, $datacellpercentage, $graphcell); - $rows[] = $row; + $data['labels'][$count] = $option->text; + $data['series'][$count] = count($option->user); + $data['series_labels'][$count] = '(' . format_float($percentageamount, 1) . '%)'; + $count++; } - $table->data = $rows; - - $html = ''; - $header = html_writer::tag('h3',format_string(get_string("responses", "choice"))); - $html .= html_writer::tag('div', $header, array('class'=>'responseheader')); - $html .= html_writer::table($table); - - return $html; + $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); } }