moodle/mod/choice/renderer.php

391 lines
16 KiB
PHP
Raw Normal View History

<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Moodle renderer used to display special elements of the lesson module
*
* @package Choice
* @copyright 2010 Rossiani Wijaya
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
**/
define ('DISPLAY_HORIZONTAL_LAYOUT', 0);
define ('DISPLAY_VERTICAL_LAYOUT', 1);
class mod_choice_renderer extends plugin_renderer_base {
/**
* Returns HTML to display choices of option
* @param object $options
* @param int $coursemoduleid
* @param bool $vertical
* @return string
*/
public function display_options($options, $coursemoduleid, $vertical = false) {
$layoutclass = 'horizontal';
if ($vertical) {
$layoutclass = 'vertical';
}
$target = new moodle_url('/mod/choice/view.php');
$attributes = array('method'=>'POST', 'action'=>$target, 'class'=> $layoutclass);
2010-09-18 12:31:20 +00:00
$html = html_writer::start_tag('form', $attributes);
$html .= html_writer::start_tag('ul', array('class'=>'choices' ));
2010-09-18 12:31:20 +00:00
$availableoption = count($options['options']);
2010-09-18 12:31:20 +00:00
foreach ($options['options'] as $option) {
$html .= html_writer::start_tag('li', array('class'=>'option'));
$option->attributes->name = 'answer';
$option->attributes->type = 'radio';
2010-09-18 12:31:20 +00:00
$labeltext = $option->text;
2010-09-18 12:31:20 +00:00
if (!empty($option->attributes->disabled)) {
$labeltext .= ' ' . get_string('full', 'choice');
$availableoption--;
2010-09-18 12:31:20 +00:00
}
$html .= html_writer::empty_tag('input', (array)$option->attributes);
$html .= html_writer::tag('label', $labeltext, array('for'=>$option->attributes->name));
$html .= html_writer::end_tag('li');
}
$html .= html_writer::tag('li','', array('class'=>'clearfloat'));
$html .= html_writer::end_tag('ul');
$html .= html_writer::tag('div', '', array('class'=>'clearfloat'));
$html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
$html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=>$coursemoduleid));
2010-09-18 12:31:20 +00:00
if (!empty($options['hascapability']) && ($options['hascapability'])) {
if ($availableoption < 1) {
$html .= html_writer::tag('label', get_string('choicefull', 'choice'));
} else {
$html .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('savemychoice','choice'), 'class'=>'button'));
}
if (!empty($options['allowupdate']) && ($options['allowupdate'])) {
$url = new moodle_url('view.php', array('id'=>$coursemoduleid, 'action'=>'delchoice', 'sesskey'=>sesskey()));
$html .= html_writer::link($url, get_string('removemychoice','choice'));
}
} else {
$html .= html_writer::tag('label', get_string('havetologin', 'choice'));
}
2010-09-18 12:31:20 +00:00
$html .= html_writer::end_tag('ul');
2010-09-18 12:31:20 +00:00
$html .= html_writer::end_tag('form');
return $html;
}
/**
* Returns HTML to display choices result
* @param object $choices
* @param bool $forcepublish
* @return string
*/
public function display_result($choices, $forcepublish = false) {
2010-09-18 12:31:20 +00:00
if (empty($forcepublish)) { //allow the publish setting to be overridden
$forcepublish = $choices->publish;
}
$displaylayout = $choices->display;
2010-09-18 12:31:20 +00:00
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);
2010-09-18 12:31:20 +00:00
}
}
/**
* Returns HTML to display choices result
* @param object $choices
* @param bool $forcepublish
* @return string
*/
public function display_publish_name_vertical($choices) {
global $PAGE;
$html ='';
$html .= html_writer::tag('h2',format_string(get_string("responses", "choice")), array('class'=>'main'));
2010-09-18 12:31:20 +00:00
$attributes = array('method'=>'POST');
$attributes['action'] = new moodle_url($PAGE->url);
$attributes['id'] = 'attemptsform';
2010-09-18 12:31:20 +00:00
if ($choices->viewresponsecapability) {
$html .= html_writer::start_tag('form', $attributes);
$html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=> $choices->coursemoduleid));
$html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=> sesskey()));
2010-09-18 12:31:20 +00:00
$html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'mode', 'value'=>'overview'));
}
$table = new html_table();
$table->cellpadding = 0;
$table->cellspacing = 0;
$table->attributes['class'] = 'results names ';
$table->tablealign = 'center';
$table->data = array();
$count = 0;
ksort($choices->options);
$columns = array();
foreach ($choices->options as $optionid => $options) {
2010-09-18 12:31:20 +00:00
$coldata = '';
if ($choices->showunanswered && $optionid == 0) {
$coldata .= html_writer::tag('div', format_string(get_string('notanswered', 'choice')), array('class'=>'option'));
} else if ($optionid > 0) {
$coldata .= html_writer::tag('div', format_string($choices->options[$optionid]->text), array('class'=>'option'));
}
$numberofuser = 0;
if (!empty($options->user) && count($options->user) > 0) {
2010-09-18 12:31:20 +00:00
$numberofuser = count($options->user);
}
$coldata .= html_writer::tag('div', ' ('.$numberofuser. ')', array('class'=>'numberofuser', 'title' => get_string('numberofuser', 'choice')));
$columns[] = $coldata;
}
2010-09-18 12:31:20 +00:00
$table->head = $columns;
$coldata = '';
$columns = array();
foreach ($choices->options as $optionid => $options) {
$coldata = '';
if ($choices->showunanswered || $optionid > 0) {
if (!empty($options->user)) {
2010-09-18 12:31:20 +00:00
foreach ($options->user as $user) {
$data = '';
if (empty($user->imagealt)){
$user->imagealt = '';
}
2010-09-18 12:31:20 +00:00
if ($choices->viewresponsecapability && $choices->deleterepsonsecapability && $optionid > 0) {
$attemptaction = html_writer::checkbox('attemptid[]', $user->id,'');
$data .= html_writer::tag('div', $attemptaction, array('class'=>'attemptaction'));
}
$userimage = $this->output->user_picture($user, array('courseid'=>$choices->courseid));
$data .= html_writer::tag('div', $userimage, array('class'=>'image'));
$userlink = new moodle_url('/user/view.php', array('id'=>$user->id,'course'=>$choices->courseid));
$name = html_writer::tag('a', fullname($user, $choices->fullnamecapability), array('href'=>$userlink, 'class'=>'username'));
$data .= html_writer::tag('div', $name, array('class'=>'fullname'));
$data .= html_writer::tag('div','', array('class'=>'clearfloat'));
2010-09-18 12:31:20 +00:00
$coldata .= html_writer::tag('div', $data, array('class'=>'user'));
}
}
}
2010-09-18 12:31:20 +00:00
$columns[] = $coldata;
$count++;
}
2010-09-18 12:31:20 +00:00
$table->data[] = $columns;
foreach ($columns as $d) {
$table->colclasses[] = 'data';
}
$html .= html_writer::tag('div', html_writer::table($table), array('class'=>'response'));
2010-09-18 12:31:20 +00:00
$actiondata = '';
2010-09-18 12:31:20 +00:00
if ($choices->viewresponsecapability && $choices->deleterepsonsecapability) {
$selecturl = new moodle_url('#');
$selectallactions = new component_action('click',"select_all_in", array('div',null,'tablecontainer'));
$selectall = new action_link($selecturl, get_string('selectall', 'quiz'), $selectallactions);
$actiondata .= $this->output->render($selectall) . ' / ';
$deselectallactions = new component_action('click',"deselect_all_in", array('div',null,'tablecontainer'));
$deselectall = new action_link($selecturl, get_string('selectnone', 'quiz'), $deselectallactions);
$actiondata .= $this->output->render($deselectall);
$actiondata .= html_writer::tag('label', ' ' . get_string('withselected', 'quiz') . ' ', array('for'=>'menuaction'));
2010-09-18 12:31:20 +00:00
$actionurl = new moodle_url($PAGE->url, array('sesskey'=>sesskey(), 'action'=>'delete_confirmation()'));
2010-11-19 02:48:39 +00:00
$select = new single_select($actionurl, 'action', array('delete'=>get_string('delete')), null, array(''=>get_string('chooseaction', 'choice')), 'attemptsform');
2010-09-18 12:31:20 +00:00
$actiondata .= $this->output->render($select);
}
$html .= html_writer::tag('div', $actiondata, array('class'=>'responseaction'));
2010-09-18 12:31:20 +00:00
if ($choices->viewresponsecapability) {
$html .= html_writer::end_tag('form');
}
2010-09-18 12:31:20 +00:00
return $html;
}
/**
* Returns HTML to display choices result
* @param object $choices
* @return string
*/
public function display_publish_anonymous_vertical($choices) {
global $CHOICE_COLUMN_HEIGHT;
2010-09-18 12:31:20 +00:00
$html = '';
$table = new html_table();
$table->cellpadding = 5;
2010-09-18 12:31:20 +00:00
$table->cellspacing = 0;
$table->attributes['class'] = 'results anonymous ';
$table->data = array();
$count = 0;
ksort($choices->options);
$columns = array();
$rows = array();
foreach ($choices->options as $optionid => $options) {
$numberofuser = 0;
if (!empty($options->user)) {
$numberofuser = count($options->user);
}
$height = 0;
$percentageamount = 0;
if($choices->numberofuser > 0) {
$height = ($CHOICE_COLUMN_HEIGHT * ((float)$numberofuser / (float)$choices->numberofuser));
$percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0;
}
$displaydiagram = html_writer::tag('img','', array('style'=>'height:'.$height.'px;width:49px;', 'alt'=>'', 'src'=>$this->output->pix_url('column', 'choice')));
$cell = new html_table_cell();
$cell->text = $displaydiagram;
$cell->attributes = array('class'=>'graph vertical data');
$columns[] = $cell;
}
$rowgraph = new html_table_row();
$rowgraph->cells = $columns;
$rows[] = $rowgraph;
2010-09-18 12:31:20 +00:00
$columns = array();
$printskiplink = true;
foreach ($choices->options as $optionid => $options) {
$columndata = '';
$numberofuser = 0;
if (!empty($options->user)) {
$numberofuser = count($options->user);
2010-09-18 12:31:20 +00:00
}
if ($printskiplink) {
$columndata .= html_writer::tag('div', '', array('class'=>'skip-block-to', 'id'=>'skipresultgraph'));
$printskiplink = false;
}
2010-09-18 12:31:20 +00:00
if ($choices->showunanswered && $optionid == 0) {
$columndata .= html_writer::tag('div', format_string(get_string('notanswered', 'choice')), array('class'=>'option'));
} else if ($optionid > 0) {
$columndata .= html_writer::tag('div', format_string($choices->options[$optionid]->text), array('class'=>'option'));
}
$columndata .= html_writer::tag('div', ' ('.$numberofuser.')', array('class'=>'numberofuser', 'title'=> get_string('numberofuser', 'choice')));
2010-09-18 12:31:20 +00:00
if($choices->numberofuser > 0) {
$percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0;
}
$columndata .= html_writer::tag('div', format_float($percentageamount,1). '%', array('class'=>'percentage'));
2010-09-18 12:31:20 +00:00
$cell = new html_table_cell();
$cell->text = $columndata;
$cell->attributes = array('class'=>'data header');
2010-09-18 12:31:20 +00:00
$columns[] = $cell;
}
$rowdata = new html_table_row();
$rowdata->cells = $columns;
$rows[] = $rowdata;
$table->data = $rows;
2010-09-18 12:31:20 +00:00
$header = html_writer::tag('h2',format_string(get_string("responses", "choice")));
$html .= html_writer::tag('div', $header, array('class'=>'responseheader'));
2010-09-18 12:31:20 +00:00
$html .= html_writer::tag('a', get_string('skipresultgraph', 'choice'), array('href'=>'#skipresultgraph', 'class'=>'skip-block'));
$html .= html_writer::tag('div', html_writer::table($table), array('class'=>'response'));
return $html;
}
/**
* Returns HTML to display choices result
* @param object $choices
* @return string
*/
public function display_publish_anonymous_horizontal($choices) {
global $CHOICE_COLUMN_WIDTH;
2010-09-18 12:31:20 +00:00
$table = new html_table();
$table->cellpadding = 5;
$table->cellspacing = 0;
$table->attributes['class'] = 'results anonymous ';
$table->data = array();
$count = 0;
ksort($choices->options);
$rows = array();
2010-09-18 12:31:20 +00:00
foreach ($choices->options as $optionid => $options) {
$numberofuser = 0;
$graphcell = new html_table_cell();
if (!empty($options->user)) {
$numberofuser = count($options->user);
}
$width = 0;
$percentageamount = 0;
$columndata = '';
if($choices->numberofuser > 0) {
$width = ($CHOICE_COLUMN_WIDTH * ((float)$numberofuser / (float)$choices->numberofuser));
$percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0;
}
$displaydiagram = html_writer::tag('img','', array('style'=>'height:50px; width:'.$width.'px', 'alt'=>'', 'src'=>$this->output->pix_url('row', 'choice')));
$skiplink = html_writer::tag('a', get_string('skipresultgraph', 'choice'), array('href'=>'#skipresultgraph'. $optionid, 'class'=>'skip-block'));
$skiphandler = html_writer::tag('span', '', array('class'=>'skip-block-to', 'id'=>'skipresultgraph'.$optionid));
2010-09-18 12:31:20 +00:00
$graphcell->text = $skiplink . $displaydiagram . $skiphandler;
$graphcell->attributes = array('class'=>'graph horizontal');
$datacell = new html_table_cell();
if ($choices->showunanswered && $optionid == 0) {
$columndata .= html_writer::tag('div', format_string(get_string('notanswered', 'choice')), array('class'=>'option'));
} else if ($optionid > 0) {
$columndata .= html_writer::tag('div', format_string($choices->options[$optionid]->text), array('class'=>'option'));
}
$columndata .= html_writer::tag('div', ' ('.$numberofuser.')', array('title'=> get_string('numberofuser', 'choice'), 'class'=>'numberofuser'));
if($choices->numberofuser > 0) {
$percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0;
}
2010-09-18 12:31:20 +00:00
$columndata .= html_writer::tag('div', format_float($percentageamount,1). '%', array('class'=>'percentage'));
$datacell->text = $columndata;
$datacell->attributes = array('class'=>'header');
$row = new html_table_row();
$row->cells = array($datacell, $graphcell);
$rows[] = $row;
}
2010-09-18 12:31:20 +00:00
$table->data = $rows;
2010-09-18 12:31:20 +00:00
$html = '';
$header = html_writer::tag('h2',format_string(get_string("responses", "choice")));
2010-09-18 12:31:20 +00:00
$html .= html_writer::tag('div', $header, array('class'=>'responseheader'));
$html .= html_writer::table($table);
2010-09-18 12:31:20 +00:00
return $html;
}
}