forked from ndunand/moodle-mod_choicegroup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.php
387 lines (323 loc) · 18.3 KB
/
renderer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<?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 Choicegroup
* @copyright 2012 Nicolas Dunand
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
**/
define ('CHOICEGROUP_DISPLAY_HORIZONTAL_LAYOUT', 0);
define ('CHOICEGROUP_DISPLAY_VERTICAL_LAYOUT', 1);
class mod_choicegroup_renderer extends plugin_renderer_base {
/**
* Returns HTML to display choicegroups of option
* @param object $options
* @param int $coursemoduleid
* @param bool $vertical
* @return string
*/
public function display_options($options, $coursemoduleid, $vertical = true, $publish = false, $limitanswers = false, $showresults = false, $current = false, $choicegroupopen = false, $disabled = false) {
global $DB, $PAGE, $course;
$PAGE->requires->js('/mod/choicegroup/javascript.js');
$layoutclass = 'vertical';
$target = new moodle_url('/mod/choicegroup/view.php');
$attributes = array('method'=>'POST', 'action'=>$target, 'class'=> $layoutclass);
$html = html_writer::start_tag('form', $attributes);
$html .= html_writer::start_tag('table', array('class'=>'choicegroups' ));
$html .= html_writer::start_tag('tr');
$html .= html_writer::tag('th', get_string('choice', 'choicegroup'));
$group = get_string('group');
$group .= html_writer::tag('a', get_string('showdescription', 'choicegroup'), array('class' => 'choicegroup-descriptiondisplay choicegroup-descriptionshow', 'href' => '#'));
$group .= html_writer::tag('a', get_string('hidedescription', 'choicegroup'), array('class' => 'choicegroup-descriptiondisplay choicegroup-descriptionhide hidden', 'href' => '#'));
$html .= html_writer::tag('th', $group);
if ( $showresults == CHOICEGROUP_SHOWRESULTS_ALWAYS or
($showresults == CHOICEGROUP_SHOWRESULTS_AFTER_ANSWER and $current) or
($showresults == CHOICEGROUP_SHOWRESULTS_AFTER_CLOSE and !$choicegroupopen)) {
if ($limitanswers) {
$html .= html_writer::tag('th', get_string('members/max', 'choicegroup'));
}
else {
$html .= html_writer::tag('th', get_string('members/', 'choicegroup'));
}
if ($publish == CHOICEGROUP_PUBLISH_NAMES) {
$membersdisplay_html = html_writer::tag('a', get_string('show'), array('class' => 'choicegroup-memberdisplay choicegroup-membershow', 'href' => '#'));
$membersdisplay_html .= html_writer::tag('a', get_string('hide'), array('class' => 'choicegroup-memberdisplay choicegroup-memberhide hidden', 'href' => '#'));
$html .= html_writer::tag('th', get_string('groupmembers', 'choicegroup') . $membersdisplay_html);
}
}
$html .= html_writer::end_tag('tr');
$availableoption = count($options['options']);
foreach ($options['options'] as $option) {
$group = $DB->get_record('groups', array('id' => $option->groupid, 'courseid' => $course->id));
if (!$group) {
$colspan = 2;
if ( $showresults == CHOICEGROUP_SHOWRESULTS_ALWAYS or ($showresults == CHOICEGROUP_SHOWRESULTS_AFTER_ANSWER and $current) or ($showresults == CHOICEGROUP_SHOWRESULTS_AFTER_CLOSE and !$choicegroupopen)) {
$colspan++;
if ($publish == CHOICEGROUP_PUBLISH_NAMES) {
$colspan++;
}
}
$cell = html_writer::tag('td', get_string('groupdoesntexist', 'choicegroup'), array('colspan' => $colspan));
$html .= html_writer::tag('tr', $cell);
break;
}
$html .= html_writer::start_tag('tr', array('class'=>'option'));
$html .= html_writer::start_tag('td', array());
$option->attributes->name = 'answer';
$option->attributes->type = 'radio';
$labeltext = html_writer::tag('label', $group->name, array('for' => 'choiceid_' . $option->attributes->value));
$group_members = $DB->get_records('groups_members', array('groupid' => $group->id));
$group_members_names = array();
foreach ($group_members as $group_member) {
$group_user = $DB->get_record('user', array('id' => $group_member->userid));
$group_members_names[] = $group_user->lastname . ', ' . $group_user->firstname;
}
sort($group_members_names);
if (!empty($option->attributes->disabled) || ($limitanswers && sizeof($group_members) >= $option->maxanswers)) {
$labeltext .= ' ' . html_writer::tag('em', get_string('full', 'choicegroup'));
$option->attributes->disabled=true;
$availableoption--;
}
$labeltext .= html_writer::tag('div', $group->description, array('class' => 'choicegroups-descriptions hidden'));
if ($disabled) {
$option->attributes->disabled=true;
}
$attributes = (array) $option->attributes;
$attributes['id'] = 'choiceid_' . $option->attributes->value;
$html .= html_writer::empty_tag('input', $attributes);
$html .= html_writer::end_tag('td');
$html .= html_writer::tag('td', $labeltext, array('for'=>$option->attributes->name));
if ( $showresults == CHOICEGROUP_SHOWRESULTS_ALWAYS or
($showresults == CHOICEGROUP_SHOWRESULTS_AFTER_ANSWER and $current) or
($showresults == CHOICEGROUP_SHOWRESULTS_AFTER_CLOSE and !$choicegroupopen)) {
$maxanswers = ($limitanswers) ? (' / '.$option->maxanswers) : ('');
$html .= html_writer::tag('td', sizeof($group_members_names).$maxanswers, array('class' => 'center'));
if ($publish == CHOICEGROUP_PUBLISH_NAMES) {
$group_members_html = html_writer::tag('div', implode('<br />', $group_members_names), array('class' => 'choicegroups-membersnames hidden', 'id' => 'choicegroup_'.$option->attributes->value));
$html .= html_writer::tag('td', $group_members_html, array('class' => 'center'));
}
}
$html .= html_writer::end_tag('tr');
}
$html .= html_writer::end_tag('table');
$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));
if (!empty($options['hascapability']) && ($options['hascapability'])) {
if ($availableoption < 1) {
$html .= html_writer::tag('td', get_string('choicegroupfull', 'choicegroup'));
} else {
if (!$disabled) {
$html .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('savemychoicegroup','choicegroup'), 'class'=>'button'));
}
}
if (!empty($options['allowupdate']) && ($options['allowupdate'])) {
$url = new moodle_url('view.php', array('id'=>$coursemoduleid, 'action'=>'delchoicegroup', 'sesskey'=>sesskey()));
$html .= ' ' . html_writer::link($url, get_string('removemychoicegroup','choicegroup'));
}
} else {
$html .= html_writer::tag('td', get_string('havetologin', 'choicegroup'));
}
$html .= html_writer::end_tag('table');
$html .= html_writer::end_tag('form');
return $html;
}
/**
* Returns HTML to display choicegroups result
* @param object $choicegroups
* @param bool $forcepublish
* @return string
*/
public function display_result($choicegroups, $forcepublish = false) {
if (empty($forcepublish)) { //allow the publish setting to be overridden
$forcepublish = $choicegroups->publish;
}
$displaylayout = ($choicegroups) ? ($choicegroups->display) : (CHOICEGROUP_DISPLAY_HORIZONTAL);
if ($forcepublish) { //CHOICEGROUP_PUBLISH_NAMES
return $this->display_publish_name_vertical($choicegroups);
} else { //CHOICEGROUP_PUBLISH_ANONYMOUS';
if ($displaylayout == CHOICEGROUP_DISPLAY_HORIZONTAL_LAYOUT) {
return $this->display_publish_anonymous_horizontal($choicegroups);
}
return $this->display_publish_anonymous_vertical($choicegroups);
}
}
/**
* Returns HTML to display choicegroups result
* @param object $choicegroups
* @param bool $forcepublish
* @return string
*/
public function display_publish_name_vertical($choicegroups) {
global $PAGE;
global $DB;
global $context;
if (!has_capability('mod/choicegroup:downloadresponses', $context)) {
return; // only the (editing)teacher can see the diagram
}
if (!$choicegroups) {
return; // no answers yet, so don't bother
}
$html ='';
$html .= html_writer::tag('h2',format_string(get_string("responses", "choicegroup")), array('class'=>'main'));
$attributes = array('method'=>'POST');
$attributes['action'] = new moodle_url($PAGE->url);
$attributes['id'] = 'attemptsform';
if ($choicegroups->viewresponsecapability) {
$html .= html_writer::start_tag('form', $attributes);
$html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=> $choicegroups->coursemoduleid));
$html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=> sesskey()));
$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($choicegroups->options);
$columns = array();
foreach ($choicegroups->options as $optionid => $options) {
$coldata = '';
if ($choicegroups->showunanswered && $optionid == 0) {
$coldata .= html_writer::tag('div', format_string(get_string('notanswered', 'choicegroup')), array('class'=>'option'));
} else if ($optionid > 0) {
$coldata .= html_writer::tag('div', format_string(choicegroup_get_option_text($choicegroups, $choicegroups->options[$optionid]->groupid)), array('class'=>'option'));
}
$numberofuser = 0;
if (!empty($options->user) && count($options->user) > 0) {
$numberofuser = count($options->user);
}
$coldata .= html_writer::tag('div', ' ('.$numberofuser. ')', array('class'=>'numberofuser', 'title' => get_string('numberofuser', 'choicegroup')));
$columns[] = $coldata;
}
$table->head = $columns;
$coldata = '';
$columns = array();
foreach ($choicegroups->options as $optionid => $options) {
$coldata = '';
if ($choicegroups->showunanswered || $optionid > 0) {
if (!empty($options->user)) {
foreach ($options->user as $user) {
$data = '';
if (empty($user->imagealt)){
$user->imagealt = '';
}
if ($choicegroups->viewresponsecapability && $choicegroups->deleterepsonsecapability && $optionid > 0) {
$attemptaction = html_writer::checkbox('userid[]', $user->id,'');
$data .= html_writer::tag('div', $attemptaction, array('class'=>'attemptaction'));
}
$userimage = $this->output->user_picture($user, array('courseid'=>$choicegroups->courseid));
$data .= html_writer::tag('div', $userimage, array('class'=>'image'));
$userlink = new moodle_url('/user/view.php', array('id'=>$user->id,'course'=>$choicegroups->courseid));
$name = html_writer::tag('a', fullname($user, $choicegroups->fullnamecapability), array('href'=>$userlink, 'class'=>'username'));
$data .= html_writer::tag('div', $name, array('class'=>'fullname'));
$data .= html_writer::tag('div','', array('class'=>'clearfloat'));
$coldata .= html_writer::tag('div', $data, array('class'=>'user'));
}
}
}
$columns[] = $coldata;
$count++;
}
$table->data[] = $columns;
foreach ($columns as $d) {
$table->colclasses[] = 'data';
}
$html .= html_writer::tag('div', html_writer::table($table), array('class'=>'response'));
$actiondata = '';
if ($choicegroups->viewresponsecapability && $choicegroups->deleterepsonsecapability) {
$selecturl = new moodle_url('#');
$selectallactions = new component_action('click',"checkall");
$selectall = new action_link($selecturl, get_string('selectall'), $selectallactions);
$actiondata .= $this->output->render($selectall) . ' / ';
$deselectallactions = new component_action('click',"checknone");
$deselectall = new action_link($selecturl, get_string('deselectall'), $deselectallactions);
$actiondata .= $this->output->render($deselectall);
$actiondata .= html_writer::tag('label', ' ' . get_string('withselected', 'choice') . ' ', array('for'=>'menuaction'));
$actionurl = new moodle_url($PAGE->url, array('sesskey'=>sesskey(), 'action'=>'delete_confirmation()'));
$select = new single_select($actionurl, 'action', array('delete'=>get_string('delete')), null, array(''=>get_string('chooseaction', 'choicegroup')), 'attemptsform');
$actiondata .= $this->output->render($select);
}
$html .= html_writer::tag('div', $actiondata, array('class'=>'responseaction'));
if ($choicegroups->viewresponsecapability) {
$html .= html_writer::end_tag('form');
}
return $html;
}
/**
* Returns HTML to display choicegroups result
* @param object $choicegroups
* @return string
*/
public function display_publish_anonymous_horizontal($choicegroups) {
global $context, $DB, $CHOICEGROUP_COLUMN_WIDTH;
if (!has_capability('mod/choicegroup:downloadresponses', $context)) {
return; // only the (editing)teacher can see the diagram
}
$table = new html_table();
$table->cellpadding = 5;
$table->cellspacing = 0;
$table->attributes['class'] = 'results anonymous ';
$table->data = array();
$count = 0;
ksort($choicegroups->options);
$rows = array();
foreach ($choicegroups->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($choicegroups->numberofuser > 0) {
$width = ($CHOICEGROUP_COLUMN_WIDTH * ((float)$numberofuser / (float)$choicegroups->numberofuser));
$percentageamount = ((float)$numberofuser/(float)$choicegroups->numberofuser)*100.0;
}
$displaydiagram = html_writer::tag('img','', array('style'=>'height:50px; width:'.$width.'px', 'alt'=>'', 'src'=>$this->output->pix_url('row', 'choicegroup')));
$skiplink = html_writer::tag('a', get_string('skipresultgraph', 'choicegroup'), array('href'=>'#skipresultgraph'. $optionid, 'class'=>'skip-block'));
$skiphandler = html_writer::tag('span', '', array('class'=>'skip-block-to', 'id'=>'skipresultgraph'.$optionid));
$graphcell->text = $skiplink . $displaydiagram . $skiphandler;
$graphcell->attributes = array('class'=>'graph horizontal');
$datacell = new html_table_cell();
if ($choicegroups->showunanswered && $optionid == 0) {
$columndata .= html_writer::tag('div', format_string(get_string('notanswered', 'choicegroup')), array('class'=>'option'));
} else if ($optionid > 0) {
$columndata .= html_writer::tag('div', format_string(choicegroup_get_option_text($choicegroups, $choicegroups->options[$optionid]->groupid)), array('class'=>'option'));
}
$columndata .= html_writer::tag('div', ' ('.$numberofuser.')', array('title'=> get_string('numberofuser', 'choicegroup'), 'class'=>'numberofuser'));
if($choicegroups->numberofuser > 0) {
$percentageamount = ((float)$numberofuser/(float)$choicegroups->numberofuser)*100.0;
}
$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;
}
$table->data = $rows;
$html = '';
$header = html_writer::tag('h2',format_string(get_string("responses", "choicegroup")));
$html .= html_writer::tag('div', $header, array('class'=>'responseheader'));
$html .= html_writer::table($table);
return $html;
}
}