|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00025 define ('DISPLAY_HORIZONTAL_LAYOUT', 0); 00026 define ('DISPLAY_VERTICAL_LAYOUT', 1); 00027 00028 class mod_choice_renderer extends plugin_renderer_base { 00029 00037 public function display_options($options, $coursemoduleid, $vertical = false) { 00038 $layoutclass = 'horizontal'; 00039 if ($vertical) { 00040 $layoutclass = 'vertical'; 00041 } 00042 $target = new moodle_url('/mod/choice/view.php'); 00043 $attributes = array('method'=>'POST', 'action'=>$target, 'class'=> $layoutclass); 00044 00045 $html = html_writer::start_tag('form', $attributes); 00046 $html .= html_writer::start_tag('ul', array('class'=>'choices' )); 00047 00048 $availableoption = count($options['options']); 00049 $choicecount = 0; 00050 foreach ($options['options'] as $option) { 00051 $choicecount++; 00052 $html .= html_writer::start_tag('li', array('class'=>'option')); 00053 $option->attributes->name = 'answer'; 00054 $option->attributes->type = 'radio'; 00055 $option->attributes->id = 'choice_'.$choicecount; 00056 00057 $labeltext = $option->text; 00058 if (!empty($option->attributes->disabled)) { 00059 $labeltext .= ' ' . get_string('full', 'choice'); 00060 $availableoption--; 00061 } 00062 00063 $html .= html_writer::empty_tag('input', (array)$option->attributes); 00064 $html .= html_writer::tag('label', $labeltext, array('for'=>$option->attributes->id)); 00065 $html .= html_writer::end_tag('li'); 00066 } 00067 $html .= html_writer::tag('li','', array('class'=>'clearfloat')); 00068 $html .= html_writer::end_tag('ul'); 00069 $html .= html_writer::tag('div', '', array('class'=>'clearfloat')); 00070 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey())); 00071 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=>$coursemoduleid)); 00072 00073 if (!empty($options['hascapability']) && ($options['hascapability'])) { 00074 if ($availableoption < 1) { 00075 $html .= html_writer::tag('label', get_string('choicefull', 'choice')); 00076 } else { 00077 $html .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('savemychoice','choice'), 'class'=>'button')); 00078 } 00079 00080 if (!empty($options['allowupdate']) && ($options['allowupdate'])) { 00081 $url = new moodle_url('view.php', array('id'=>$coursemoduleid, 'action'=>'delchoice', 'sesskey'=>sesskey())); 00082 $html .= html_writer::link($url, get_string('removemychoice','choice')); 00083 } 00084 } else { 00085 $html .= html_writer::tag('label', get_string('havetologin', 'choice')); 00086 } 00087 00088 $html .= html_writer::end_tag('ul'); 00089 $html .= html_writer::end_tag('form'); 00090 00091 return $html; 00092 } 00093 00100 public function display_result($choices, $forcepublish = false) { 00101 if (empty($forcepublish)) { //allow the publish setting to be overridden 00102 $forcepublish = $choices->publish; 00103 } 00104 00105 $displaylayout = $choices->display; 00106 00107 if ($forcepublish) { //CHOICE_PUBLISH_NAMES 00108 return $this->display_publish_name_vertical($choices); 00109 } else { //CHOICE_PUBLISH_ANONYMOUS'; 00110 if ($displaylayout == DISPLAY_HORIZONTAL_LAYOUT) { 00111 return $this->display_publish_anonymous_horizontal($choices); 00112 } 00113 return $this->display_publish_anonymous_vertical($choices); 00114 } 00115 } 00116 00123 public function display_publish_name_vertical($choices) { 00124 global $PAGE; 00125 $html =''; 00126 $html .= html_writer::tag('h2',format_string(get_string("responses", "choice")), array('class'=>'main')); 00127 00128 $attributes = array('method'=>'POST'); 00129 $attributes['action'] = new moodle_url($PAGE->url); 00130 $attributes['id'] = 'attemptsform'; 00131 00132 if ($choices->viewresponsecapability) { 00133 $html .= html_writer::start_tag('form', $attributes); 00134 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=> $choices->coursemoduleid)); 00135 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=> sesskey())); 00136 $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'mode', 'value'=>'overview')); 00137 } 00138 00139 $table = new html_table(); 00140 $table->cellpadding = 0; 00141 $table->cellspacing = 0; 00142 $table->attributes['class'] = 'results names '; 00143 $table->tablealign = 'center'; 00144 $table->data = array(); 00145 00146 $count = 0; 00147 ksort($choices->options); 00148 00149 $columns = array(); 00150 foreach ($choices->options as $optionid => $options) { 00151 $coldata = ''; 00152 if ($choices->showunanswered && $optionid == 0) { 00153 $coldata .= html_writer::tag('div', format_string(get_string('notanswered', 'choice')), array('class'=>'option')); 00154 } else if ($optionid > 0) { 00155 $coldata .= html_writer::tag('div', format_string($choices->options[$optionid]->text), array('class'=>'option')); 00156 } 00157 $numberofuser = 0; 00158 if (!empty($options->user) && count($options->user) > 0) { 00159 $numberofuser = count($options->user); 00160 } 00161 00162 $coldata .= html_writer::tag('div', ' ('.$numberofuser. ')', array('class'=>'numberofuser', 'title' => get_string('numberofuser', 'choice'))); 00163 $columns[] = $coldata; 00164 } 00165 00166 $table->head = $columns; 00167 00168 $coldata = ''; 00169 $columns = array(); 00170 foreach ($choices->options as $optionid => $options) { 00171 $coldata = ''; 00172 if ($choices->showunanswered || $optionid > 0) { 00173 if (!empty($options->user)) { 00174 foreach ($options->user as $user) { 00175 $data = ''; 00176 if (empty($user->imagealt)){ 00177 $user->imagealt = ''; 00178 } 00179 00180 if ($choices->viewresponsecapability && $choices->deleterepsonsecapability && $optionid > 0) { 00181 $attemptaction = html_writer::checkbox('attemptid[]', $user->id,''); 00182 $data .= html_writer::tag('div', $attemptaction, array('class'=>'attemptaction')); 00183 } 00184 $userimage = $this->output->user_picture($user, array('courseid'=>$choices->courseid)); 00185 $data .= html_writer::tag('div', $userimage, array('class'=>'image')); 00186 00187 $userlink = new moodle_url('/user/view.php', array('id'=>$user->id,'course'=>$choices->courseid)); 00188 $name = html_writer::tag('a', fullname($user, $choices->fullnamecapability), array('href'=>$userlink, 'class'=>'username')); 00189 $data .= html_writer::tag('div', $name, array('class'=>'fullname')); 00190 $data .= html_writer::tag('div','', array('class'=>'clearfloat')); 00191 $coldata .= html_writer::tag('div', $data, array('class'=>'user')); 00192 } 00193 } 00194 } 00195 00196 $columns[] = $coldata; 00197 $count++; 00198 } 00199 00200 $table->data[] = $columns; 00201 foreach ($columns as $d) { 00202 $table->colclasses[] = 'data'; 00203 } 00204 $html .= html_writer::tag('div', html_writer::table($table), array('class'=>'response')); 00205 00206 $actiondata = ''; 00207 if ($choices->viewresponsecapability && $choices->deleterepsonsecapability) { 00208 $selecturl = new moodle_url('#'); 00209 00210 $selectallactions = new component_action('click',"checkall"); 00211 $selectall = new action_link($selecturl, get_string('selectall'), $selectallactions); 00212 $actiondata .= $this->output->render($selectall) . ' / '; 00213 00214 $deselectallactions = new component_action('click',"checknone"); 00215 $deselectall = new action_link($selecturl, get_string('deselectall'), $deselectallactions); 00216 $actiondata .= $this->output->render($deselectall); 00217 00218 $actiondata .= html_writer::tag('label', ' ' . get_string('withselected', 'choice') . ' ', array('for'=>'menuaction')); 00219 00220 $actionurl = new moodle_url($PAGE->url, array('sesskey'=>sesskey(), 'action'=>'delete_confirmation()')); 00221 $select = new single_select($actionurl, 'action', array('delete'=>get_string('delete')), null, array(''=>get_string('chooseaction', 'choice')), 'attemptsform'); 00222 00223 $actiondata .= $this->output->render($select); 00224 } 00225 $html .= html_writer::tag('div', $actiondata, array('class'=>'responseaction')); 00226 00227 if ($choices->viewresponsecapability) { 00228 $html .= html_writer::end_tag('form'); 00229 } 00230 00231 return $html; 00232 } 00233 00234 00240 public function display_publish_anonymous_vertical($choices) { 00241 global $CHOICE_COLUMN_HEIGHT; 00242 00243 $html = ''; 00244 $table = new html_table(); 00245 $table->cellpadding = 5; 00246 $table->cellspacing = 0; 00247 $table->attributes['class'] = 'results anonymous '; 00248 $table->data = array(); 00249 $count = 0; 00250 ksort($choices->options); 00251 $columns = array(); 00252 $rows = array(); 00253 00254 foreach ($choices->options as $optionid => $options) { 00255 $numberofuser = 0; 00256 if (!empty($options->user)) { 00257 $numberofuser = count($options->user); 00258 } 00259 $height = 0; 00260 $percentageamount = 0; 00261 if($choices->numberofuser > 0) { 00262 $height = ($CHOICE_COLUMN_HEIGHT * ((float)$numberofuser / (float)$choices->numberofuser)); 00263 $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0; 00264 } 00265 00266 $displaydiagram = html_writer::tag('img','', array('style'=>'height:'.$height.'px;width:49px;', 'alt'=>'', 'src'=>$this->output->pix_url('column', 'choice'))); 00267 00268 $cell = new html_table_cell(); 00269 $cell->text = $displaydiagram; 00270 $cell->attributes = array('class'=>'graph vertical data'); 00271 $columns[] = $cell; 00272 } 00273 $rowgraph = new html_table_row(); 00274 $rowgraph->cells = $columns; 00275 $rows[] = $rowgraph; 00276 00277 $columns = array(); 00278 $printskiplink = true; 00279 foreach ($choices->options as $optionid => $options) { 00280 $columndata = ''; 00281 $numberofuser = 0; 00282 if (!empty($options->user)) { 00283 $numberofuser = count($options->user); 00284 } 00285 00286 if ($printskiplink) { 00287 $columndata .= html_writer::tag('div', '', array('class'=>'skip-block-to', 'id'=>'skipresultgraph')); 00288 $printskiplink = false; 00289 } 00290 00291 if ($choices->showunanswered && $optionid == 0) { 00292 $columndata .= html_writer::tag('div', format_string(get_string('notanswered', 'choice')), array('class'=>'option')); 00293 } else if ($optionid > 0) { 00294 $columndata .= html_writer::tag('div', format_string($choices->options[$optionid]->text), array('class'=>'option')); 00295 } 00296 $columndata .= html_writer::tag('div', ' ('.$numberofuser.')', array('class'=>'numberofuser', 'title'=> get_string('numberofuser', 'choice'))); 00297 00298 if($choices->numberofuser > 0) { 00299 $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0; 00300 } 00301 $columndata .= html_writer::tag('div', format_float($percentageamount,1). '%', array('class'=>'percentage')); 00302 00303 $cell = new html_table_cell(); 00304 $cell->text = $columndata; 00305 $cell->attributes = array('class'=>'data header'); 00306 $columns[] = $cell; 00307 } 00308 $rowdata = new html_table_row(); 00309 $rowdata->cells = $columns; 00310 $rows[] = $rowdata; 00311 00312 $table->data = $rows; 00313 00314 $header = html_writer::tag('h2',format_string(get_string("responses", "choice"))); 00315 $html .= html_writer::tag('div', $header, array('class'=>'responseheader')); 00316 $html .= html_writer::tag('a', get_string('skipresultgraph', 'choice'), array('href'=>'#skipresultgraph', 'class'=>'skip-block')); 00317 $html .= html_writer::tag('div', html_writer::table($table), array('class'=>'response')); 00318 00319 return $html; 00320 } 00321 00327 public function display_publish_anonymous_horizontal($choices) { 00328 global $CHOICE_COLUMN_WIDTH; 00329 00330 $table = new html_table(); 00331 $table->cellpadding = 5; 00332 $table->cellspacing = 0; 00333 $table->attributes['class'] = 'results anonymous '; 00334 $table->data = array(); 00335 00336 $count = 0; 00337 ksort($choices->options); 00338 00339 $rows = array(); 00340 foreach ($choices->options as $optionid => $options) { 00341 $numberofuser = 0; 00342 $graphcell = new html_table_cell(); 00343 if (!empty($options->user)) { 00344 $numberofuser = count($options->user); 00345 } 00346 00347 $width = 0; 00348 $percentageamount = 0; 00349 $columndata = ''; 00350 if($choices->numberofuser > 0) { 00351 $width = ($CHOICE_COLUMN_WIDTH * ((float)$numberofuser / (float)$choices->numberofuser)); 00352 $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0; 00353 } 00354 $displaydiagram = html_writer::tag('img','', array('style'=>'height:50px; width:'.$width.'px', 'alt'=>'', 'src'=>$this->output->pix_url('row', 'choice'))); 00355 00356 $skiplink = html_writer::tag('a', get_string('skipresultgraph', 'choice'), array('href'=>'#skipresultgraph'. $optionid, 'class'=>'skip-block')); 00357 $skiphandler = html_writer::tag('span', '', array('class'=>'skip-block-to', 'id'=>'skipresultgraph'.$optionid)); 00358 00359 $graphcell->text = $skiplink . $displaydiagram . $skiphandler; 00360 $graphcell->attributes = array('class'=>'graph horizontal'); 00361 00362 $datacell = new html_table_cell(); 00363 if ($choices->showunanswered && $optionid == 0) { 00364 $columndata .= html_writer::tag('div', format_string(get_string('notanswered', 'choice')), array('class'=>'option')); 00365 } else if ($optionid > 0) { 00366 $columndata .= html_writer::tag('div', format_string($choices->options[$optionid]->text), array('class'=>'option')); 00367 } 00368 $columndata .= html_writer::tag('div', ' ('.$numberofuser.')', array('title'=> get_string('numberofuser', 'choice'), 'class'=>'numberofuser')); 00369 00370 if($choices->numberofuser > 0) { 00371 $percentageamount = ((float)$numberofuser/(float)$choices->numberofuser)*100.0; 00372 } 00373 $columndata .= html_writer::tag('div', format_float($percentageamount,1). '%', array('class'=>'percentage')); 00374 00375 $datacell->text = $columndata; 00376 $datacell->attributes = array('class'=>'header'); 00377 00378 $row = new html_table_row(); 00379 $row->cells = array($datacell, $graphcell); 00380 $rows[] = $row; 00381 } 00382 00383 $table->data = $rows; 00384 00385 $html = ''; 00386 $header = html_writer::tag('h2',format_string(get_string("responses", "choice"))); 00387 $html .= html_writer::tag('div', $header, array('class'=>'responseheader')); 00388 $html .= html_writer::table($table); 00389 00390 return $html; 00391 } 00392 } 00393