Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
// This file is part of Moodle - http://moodle.org/
4
//
5
// Moodle is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
9
//
10
// Moodle is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
// GNU General Public License for more details.
14
//
15
// You should have received a copy of the GNU General Public License
16
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17
 
18
/**
19
 * Moodle renderer used to display special elements of the lesson module
20
 *
21
 * @package   mod_choice
22
 * @copyright 2010 Rossiani Wijaya
23
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 **/
25
class mod_choice_renderer extends plugin_renderer_base {
26
 
27
    /**
28
     * Returns HTML to display choices of option
29
     * @param object $options
30
     * @param int  $coursemoduleid
31
     * @param bool $vertical
32
     * @return string
33
     */
34
    public function display_options($options, $coursemoduleid, $vertical = false, $multiple = false) {
35
        $layoutclass = 'horizontal';
36
        if ($vertical) {
37
            $layoutclass = 'vertical';
38
        }
39
        $target = new moodle_url('/mod/choice/view.php');
40
        $attributes = array('method'=>'POST', 'action'=>$target, 'class'=> $layoutclass);
41
        $disabled = empty($options['previewonly']) ? array() : array('disabled' => 'disabled');
42
 
43
        $html = html_writer::start_tag('form', $attributes);
44
        $html .= html_writer::start_tag('ul', array('class' => 'choices list-unstyled unstyled'));
45
 
46
        $availableoption = count($options['options']);
47
        $choicecount = 0;
48
        foreach ($options['options'] as $option) {
49
            $choicecount++;
1441 ariadna 50
            $html .= html_writer::start_tag('li', array('class' => 'option me-3'));
1 efrain 51
            if ($multiple) {
52
                $option->attributes->name = 'answer[]';
53
                $option->attributes->type = 'checkbox';
54
            } else {
55
                $option->attributes->name = 'answer';
56
                $option->attributes->type = 'radio';
57
            }
58
            $option->attributes->id = 'choice_'.$choicecount;
59
            $option->attributes->class = 'mx-1';
60
 
61
            $labeltext = $option->text;
62
            if (!empty($option->attributes->disabled)) {
63
                $labeltext .= ' ' . get_string('full', 'choice');
64
                $availableoption--;
65
            }
66
 
67
            if (!empty($options['limitanswers']) && !empty($options['showavailable'])) {
68
                $labeltext .= html_writer::empty_tag('br');
69
                $labeltext .= get_string("responsesa", "choice", $option->countanswers);
70
                $labeltext .= html_writer::empty_tag('br');
71
                $labeltext .= get_string("limita", "choice", $option->maxanswers);
72
            }
73
 
74
            $html .= html_writer::empty_tag('input', (array)$option->attributes + $disabled);
75
            $html .= html_writer::tag('label', $labeltext, array('for'=>$option->attributes->id));
76
            $html .= html_writer::end_tag('li');
77
        }
78
        $html .= html_writer::tag('li','', array('class'=>'clearfloat'));
79
        $html .= html_writer::end_tag('ul');
80
        $html .= html_writer::tag('div', '', array('class'=>'clearfloat'));
81
        $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
82
        $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'action', 'value'=>'makechoice'));
83
        $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=>$coursemoduleid));
84
 
85
        if (empty($options['previewonly'])) {
86
            if (!empty($options['hascapability']) && ($options['hascapability'])) {
87
                if ($availableoption < 1) {
88
                    $html .= html_writer::tag('label', get_string('choicefull', 'choice'));
89
                } else {
90
                    $html .= html_writer::empty_tag('input', array(
91
                        'type' => 'submit',
92
                        'value' => get_string('savemychoice', 'choice'),
93
                        'class' => 'btn btn-primary'
94
                    ));
95
                }
96
 
97
                if (!empty($options['allowupdate']) && ($options['allowupdate'])) {
98
                    $url = new moodle_url('view.php',
99
                            array('id' => $coursemoduleid, 'action' => 'delchoice', 'sesskey' => sesskey()));
1441 ariadna 100
                    $html .= html_writer::link($url, get_string('removemychoice', 'choice'), array('class' => 'ms-1'));
1 efrain 101
                }
102
            } else {
103
                $html .= html_writer::tag('label', get_string('havetologin', 'choice'));
104
            }
105
        }
106
 
107
        $html .= html_writer::end_tag('form');
108
 
109
        return $html;
110
    }
111
 
112
    /**
113
     * Returns HTML to display choices result
114
     * @param object $choices
115
     * @param bool $forcepublish
116
     * @return string
117
     */
118
    public function display_result($choices, $forcepublish = false) {
119
        if (empty($forcepublish)) { //allow the publish setting to be overridden
120
            $forcepublish = $choices->publish;
121
        }
122
 
123
        $displaylayout = $choices->display;
124
 
125
        if ($forcepublish) {  //CHOICE_PUBLISH_NAMES
126
            return $this->display_publish_name_vertical($choices);
127
        } else {
128
            return $this->display_publish_anonymous($choices, $displaylayout);
129
        }
130
    }
131
 
132
    /**
133
     * Returns HTML to display choices result
134
     * @param object $choices
135
     * @return string
136
     */
137
    public function display_publish_name_vertical($choices) {
138
        $html ='';
139
 
140
        $attributes = array('method'=>'POST');
141
        $attributes['action'] = new moodle_url($this->page->url);
142
        $attributes['id'] = 'attemptsform';
143
 
144
        if ($choices->viewresponsecapability) {
145
            $html .= html_writer::start_tag('form', $attributes);
146
            $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=> $choices->coursemoduleid));
147
            $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=> sesskey()));
148
            $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'mode', 'value'=>'overview'));
149
        }
150
 
151
        $table = new html_table();
152
        $table->cellpadding = 0;
153
        $table->cellspacing = 0;
154
        $table->attributes['class'] = 'results names table table-bordered';
155
        $table->tablealign = 'center';
156
        $table->summary = get_string('responsesto', 'choice', format_string($choices->name));
157
        $table->data = array();
158
 
159
        $count = 0;
160
        ksort($choices->options);
161
 
162
        $columns = array();
163
        $celldefault = new html_table_cell();
164
        $celldefault->attributes['class'] = 'data';
165
 
166
        // This extra cell is needed in order to support accessibility for screenreader. MDL-30816
167
        $accessiblecell = new html_table_cell();
168
        $accessiblecell->scope = 'row';
169
        $accessiblecell->text = get_string('choiceoptions', 'choice');
170
        $columns['options'][] = $accessiblecell;
171
 
172
        $usernumberheader = clone($celldefault);
173
        $usernumberheader->header = true;
174
        $usernumberheader->attributes['class'] = 'header data';
175
        $usernumberheader->text = get_string('numberofuser', 'choice');
176
        $columns['usernumber'][] = $usernumberheader;
177
 
178
        $optionsnames = [];
179
        foreach ($choices->options as $optionid => $options) {
180
            $celloption = clone($celldefault);
181
            $cellusernumber = clone($celldefault);
182
 
183
            if ($choices->showunanswered && $optionid == 0) {
184
                $headertitle = get_string('notanswered', 'choice');
185
            } else if ($optionid > 0) {
186
                $headertitle = format_string($choices->options[$optionid]->text);
187
                if (!empty($choices->options[$optionid]->user) && count($choices->options[$optionid]->user) > 0) {
188
                    if (
189
                        $choices->limitanswers &&
190
                        (count($choices->options[$optionid]->user) == $choices->options[$optionid]->maxanswer)
191
                    ) {
192
                        $headertitle .= ' ' . get_string('full', 'choice');
193
                    }
194
                }
195
            }
196
            $celltext = $headertitle;
197
 
198
            // Render select/deselect all checkbox for this option.
199
            if ($choices->viewresponsecapability && $choices->deleterepsonsecapability) {
200
 
201
                // Build the select/deselect all for this option.
202
                $selectallid = 'select-response-option-' . $optionid;
203
                $togglegroup = 'responses response-option-' . $optionid;
204
                $selectalltext = get_string('selectalloption', 'choice', $headertitle);
205
                $deselectalltext = get_string('deselectalloption', 'choice', $headertitle);
206
                $mastercheckbox = new \core\output\checkbox_toggleall($togglegroup, true, [
207
                    'id' => $selectallid,
208
                    'name' => $selectallid,
209
                    'value' => 1,
210
                    'selectall' => $selectalltext,
211
                    'deselectall' => $deselectalltext,
212
                    'label' => $selectalltext,
213
                    'labelclasses' => 'accesshide',
214
                ]);
215
 
216
                $celltext .= html_writer::div($this->output->render($mastercheckbox));
217
            }
218
            $numberofuser = 0;
219
            if (!empty($options->user) && count($options->user) > 0) {
220
                $numberofuser = count($options->user);
221
            }
222
            if (($choices->limitanswers) && ($choices->showavailable)) {
223
                $numberofuser .= html_writer::empty_tag('br');
224
                $numberofuser .= get_string("limita", "choice", $options->maxanswer);
225
            }
226
            $celloption->text = html_writer::div($celltext, 'text-center');
227
            $optionsnames[$optionid] = $celltext;
228
            $cellusernumber->text = html_writer::div($numberofuser, 'text-center');
229
 
230
            $columns['options'][] = $celloption;
231
            $columns['usernumber'][] = $cellusernumber;
232
        }
233
 
234
        $table->head = $columns['options'];
235
        $table->data[] = new html_table_row($columns['usernumber']);
236
 
237
        $columns = array();
238
 
239
        // This extra cell is needed in order to support accessibility for screenreader. MDL-30816
240
        $accessiblecell = new html_table_cell();
241
        $accessiblecell->text = get_string('userchoosethisoption', 'choice');
242
        $accessiblecell->header = true;
243
        $accessiblecell->scope = 'row';
244
        $accessiblecell->attributes['class'] = 'header data';
245
        $columns[] = $accessiblecell;
246
 
247
        foreach ($choices->options as $optionid => $options) {
248
            $cell = new html_table_cell();
249
            $cell->attributes['class'] = 'data';
250
 
251
            if ($choices->showunanswered || $optionid > 0) {
252
                if (!empty($options->user)) {
253
                    $optionusers = '';
254
                    foreach ($options->user as $user) {
255
                        $data = '';
256
                        if (empty($user->imagealt)) {
257
                            $user->imagealt = '';
258
                        }
259
 
260
                        $userfullname = fullname($user, $choices->fullnamecapability);
261
                        $checkbox = '';
262
                        if ($choices->viewresponsecapability && $choices->deleterepsonsecapability) {
263
                            $checkboxid = 'attempt-user' . $user->id . '-option' . $optionid;
264
                            if ($optionid > 0) {
265
                                $checkboxname = 'attemptid[]';
266
                                $checkboxvalue = $user->answerid;
267
                            } else {
268
                                $checkboxname = 'userid[]';
269
                                $checkboxvalue = $user->id;
270
                            }
271
 
272
                            $togglegroup = 'responses response-option-' . $optionid;
273
                            $slavecheckbox = new \core\output\checkbox_toggleall($togglegroup, false, [
274
                                'id' => $checkboxid,
275
                                'name' => $checkboxname,
1441 ariadna 276
                                'classes' => 'me-1',
1 efrain 277
                                'value' => $checkboxvalue,
278
                                'label' => $userfullname . ' ' . $options->text,
279
                                'labelclasses' => 'accesshide',
280
                            ]);
281
                            $checkbox = $this->output->render($slavecheckbox);
282
                        }
283
                        $userimage = $this->output->user_picture($user, array('courseid' => $choices->courseid, 'link' => false));
284
                        $profileurl = new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $choices->courseid));
285
                        $profilelink = html_writer::link($profileurl, $userimage . $userfullname);
286
                        $data .= html_writer::div($checkbox . $profilelink, 'mb-1');
287
 
288
                        $optionusers .= $data;
289
                    }
290
                    $cell->text = $optionusers;
291
                }
292
            }
293
            $columns[] = $cell;
294
            $count++;
295
        }
296
        $row = new html_table_row($columns);
297
        $table->data[] = $row;
298
 
299
        $html .= html_writer::tag('div', html_writer::table($table), array('class'=>'response'));
300
 
301
        $actiondata = '';
302
        if ($choices->viewresponsecapability && $choices->deleterepsonsecapability) {
303
            // Build the select/deselect all for all of options.
304
            $selectallid = 'select-all-responses';
305
            $togglegroup = 'responses';
306
            $selectallcheckbox = new \core\output\checkbox_toggleall($togglegroup, true, [
307
                'id' => $selectallid,
308
                'name' => $selectallid,
309
                'value' => 1,
310
                'label' => get_string('selectall'),
1441 ariadna 311
                'classes' => 'btn-secondary me-1'
1 efrain 312
            ], true);
313
            $actiondata .= $this->output->render($selectallcheckbox);
314
 
315
            $actionurl = new moodle_url($this->page->url,
316
                    ['sesskey' => sesskey(), 'action' => 'delete_confirmation()']);
317
            $actionoptions = array('delete' => get_string('delete'));
318
            foreach ($choices->options as $optionid => $option) {
319
                if ($optionid > 0) {
320
                    $actionoptions['choose_'.$optionid] = get_string('chooseoption', 'choice', $option->text);
321
                }
322
            }
323
            $selectattributes = [
324
                'data-action' => 'toggle',
325
                'data-togglegroup' => 'responses',
326
                'data-toggle' => 'action',
327
            ];
328
            $selectnothing = ['' => get_string('chooseaction', 'choice')];
329
            $select = new single_select($actionurl, 'action', $actionoptions, null, $selectnothing, 'attemptsform');
330
            $select->set_label(get_string('withselected', 'choice'));
331
            $select->disabled = true;
332
            $select->attributes = $selectattributes;
333
 
334
            $actiondata .= $this->output->render($select);
335
        }
336
        $html .= html_writer::tag('div', $actiondata, array('class'=>'responseaction'));
337
 
338
        if ($choices->viewresponsecapability) {
339
            $html .= html_writer::end_tag('form');
340
        }
341
 
342
        return $html;
343
    }
344
 
345
 
346
    /**
347
     * Returns HTML to display choices result
348
     * @deprecated since 3.2
349
     * @param object $choices
350
     * @return string
351
     */
352
    public function display_publish_anonymous_horizontal($choices) {
353
        debugging(__FUNCTION__.'() is deprecated. Please use mod_choice_renderer::display_publish_anonymous() instead.',
354
                DEBUG_DEVELOPER);
355
        return $this->display_publish_anonymous($choices, CHOICE_DISPLAY_VERTICAL);
356
    }
357
 
358
    /**
359
     * Returns HTML to display choices result
360
     * @deprecated since 3.2
361
     * @param object $choices
362
     * @return string
363
     */
364
    public function display_publish_anonymous_vertical($choices) {
365
        debugging(__FUNCTION__.'() is deprecated. Please use mod_choice_renderer::display_publish_anonymous() instead.',
366
                DEBUG_DEVELOPER);
367
        return $this->display_publish_anonymous($choices, CHOICE_DISPLAY_HORIZONTAL);
368
    }
369
 
370
    /**
371
     * Generate the choice result chart.
372
     *
373
     * Can be displayed either in the vertical or horizontal position.
374
     *
375
     * @param stdClass $choices Choices responses object.
376
     * @param int $displaylayout The constants CHOICE_DISPLAY_HORIZONTAL or CHOICE_DISPLAY_VERTICAL.
377
     * @return string the rendered chart.
378
     */
379
    public function display_publish_anonymous($choices, $displaylayout) {
380
        $count = 0;
381
        $data = [];
382
        $numberofuser = 0;
383
        $percentageamount = 0;
384
        foreach ($choices->options as $optionid => $option) {
385
            if (!empty($option->user)) {
386
                $numberofuser = count($option->user);
387
            }
388
            if($choices->numberofuser > 0) {
389
                $percentageamount = ((float)$numberofuser / (float)$choices->numberofuser) * 100.0;
390
            }
391
            $data['labels'][$count] = $option->text;
392
            $data['series'][$count] = $numberofuser;
393
            $data['series_labels'][$count] = $numberofuser . ' (' . format_float($percentageamount, 1) . '%)';
394
            $count++;
395
            $numberofuser = 0;
396
        }
397
 
398
        $chart = new \core\chart_bar();
399
        if ($displaylayout == CHOICE_DISPLAY_VERTICAL) {
400
            $chart->set_horizontal(true); // Horizontal bars when choices are vertical.
401
        }
402
        $series = new \core\chart_series(format_string(get_string("responses", "choice")), $data['series']);
403
        $series->set_labels($data['series_labels']);
404
        $chart->add_series($series);
405
        $chart->set_labels($data['labels']);
406
        $yaxis = $chart->get_yaxis(0, true);
407
        $yaxis->set_stepsize(max(1, round(max($data['series']) / 10)));
408
        return $this->output->render($chart);
409
    }
410
}
411