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
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Contains class mod_feedback_complete_form
19
 *
20
 * @package   mod_feedback
21
 * @copyright 2016 Marina Glancy
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
defined('MOODLE_INTERNAL') || die();
26
 
27
/**
28
 * Class mod_feedback_complete_form
29
 *
30
 * @package   mod_feedback
31
 * @copyright 2016 Marina Glancy
32
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
34
class mod_feedback_complete_form extends moodleform {
35
 
36
    /** @var int */
37
    const MODE_COMPLETE = 1;
38
    /** @var int */
39
    const MODE_PRINT = 2;
40
    /** @var int */
41
    const MODE_EDIT = 3;
42
    /** @var int */
43
    const MODE_VIEW_RESPONSE = 4;
44
    /** @var int */
45
    const MODE_VIEW_TEMPLATE = 5;
46
 
47
    /** @var int */
48
    protected $mode;
49
    /** @var mod_feedback_structure|mod_feedback_completion */
50
    protected $structure;
51
    /** @var mod_feedback_completion */
52
    protected $completion;
53
    /** @var int */
54
    protected $gopage;
55
    /** @var bool */
56
    protected $hasrequired = false;
57
 
58
    /**
59
     * Constructor
60
     *
61
     * @param int $mode
62
     * @param mod_feedback_structure $structure
63
     * @param string $formid CSS id attribute of the form
64
     * @param array $customdata
65
     */
66
    public function __construct($mode, mod_feedback_structure $structure, $formid, $customdata = null) {
67
        $this->mode = $mode;
68
        $this->structure = $structure;
69
        $this->gopage = isset($customdata['gopage']) ? $customdata['gopage'] : 0;
70
        $isanonymous = $this->structure->is_anonymous() ? ' ianonymous' : '';
71
        parent::__construct(null, $customdata, 'POST', '',
72
                array('id' => $formid, 'class' => 'feedback_form' . $isanonymous), true);
73
        $this->set_display_vertical();
74
    }
75
 
76
    /**
77
     * Form definition
78
     */
79
    public function definition() {
80
        $mform = $this->_form;
81
        $mform->addElement('hidden', 'id', $this->get_cm()->id);
82
        $mform->setType('id', PARAM_INT);
83
        $mform->addElement('hidden', 'courseid', $this->get_current_course_id());
84
        $mform->setType('courseid', PARAM_INT);
85
        $mform->addElement('hidden', 'gopage');
86
        $mform->setType('gopage', PARAM_INT);
87
        $mform->addElement('hidden', 'lastpage');
88
        $mform->setType('lastpage', PARAM_INT);
89
        $mform->addElement('hidden', 'startitempos');
90
        $mform->setType('startitempos', PARAM_INT);
91
        $mform->addElement('hidden', 'lastitempos');
92
        $mform->setType('lastitempos', PARAM_INT);
93
 
94
        if (isloggedin() && !isguestuser() && $this->mode != self::MODE_EDIT && $this->mode != self::MODE_VIEW_TEMPLATE &&
95
                    $this->mode != self::MODE_VIEW_RESPONSE) {
96
            // Output information about the current mode (anonymous or not) in some modes.
97
            if ($this->structure->is_anonymous()) {
98
                $anonymousmodeinfo = get_string('anonymous', 'feedback');
99
            } else {
100
                $anonymousmodeinfo = get_string('non_anonymous', 'feedback');
101
            }
102
            $element = $mform->addElement('static', 'anonymousmode', '',
103
                    get_string('mode', 'feedback') . ': ' . $anonymousmodeinfo);
104
            $element->setAttributes($element->getAttributes() + ['class' => 'feedback_mode']);
105
        }
106
 
107
        // Add buttons to go to previous/next pages and submit the feedback.
108
        if ($this->mode == self::MODE_COMPLETE) {
109
            $buttonarray = array();
110
            $buttonarray[] = &$mform->createElement('submit', 'gopreviouspage', get_string('previous_page', 'feedback'));
111
            $buttonarray[] = &$mform->createElement('submit', 'gonextpage', get_string('next_page', 'feedback'),
112
                    array('class' => 'form-submit'));
113
            $buttonarray[] = &$mform->createElement('submit', 'savevalues', get_string('save_entries', 'feedback'),
114
                    array('class' => 'form-submit'));
115
            $buttonarray[] = &$mform->createElement('cancel');
116
            $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
117
            $mform->closeHeaderBefore('buttonar');
118
        }
119
 
120
        if ($this->mode == self::MODE_COMPLETE) {
121
            $this->definition_complete();
122
        } else {
123
            $this->definition_preview();
124
        }
125
 
126
        // Set data.
127
        $this->set_data(array('gopage' => $this->gopage));
128
    }
129
 
130
    /**
131
     * Called from definition_after_data() in the completion mode
132
     *
133
     * This will add only items from a current page to the feedback and adjust the buttons
134
     */
135
    protected function definition_complete() {
136
        if (!$this->structure instanceof mod_feedback_completion) {
137
            // We should not really be here but just in case.
138
            return;
139
        }
140
        $pages = $this->structure->get_pages();
141
        $gopage = $this->gopage;
142
        $pageitems = $pages[$gopage];
143
        $hasnextpage = $gopage < count($pages) - 1; // Until we complete this page we can not trust get_next_page().
144
        $hasprevpage = $gopage && ($this->structure->get_previous_page($gopage, false) !== null);
145
 
146
        // Add elements.
147
        foreach ($pageitems as $item) {
148
            $itemobj = feedback_get_item_class($item->typ);
149
            $itemobj->complete_form_element($item, $this);
150
        }
151
 
152
        // Remove invalid buttons (for example, no "previous page" if we are on the first page).
153
        if (!$hasprevpage) {
154
            $this->remove_button('gopreviouspage');
155
        }
156
        if (!$hasnextpage) {
157
            $this->remove_button('gonextpage');
158
        }
159
        if ($hasnextpage) {
160
            $this->remove_button('savevalues');
161
        }
162
    }
163
 
164
    /**
165
     * Called from definition_after_data() in all modes except for completion
166
     *
167
     * This will add all items to the form, including pagebreaks as horizontal rules.
168
     */
169
    protected function definition_preview() {
170
        foreach ($this->structure->get_items() as $feedbackitem) {
171
            $itemobj = feedback_get_item_class($feedbackitem->typ);
172
            $itemobj->complete_form_element($feedbackitem, $this);
173
        }
174
    }
175
 
176
    /**
177
     * Removes the button that is not applicable for the current page
178
     *
179
     * @param string $buttonname
180
     */
181
    private function remove_button($buttonname) {
182
        $el = $this->_form->getElement('buttonar');
183
        foreach ($el->_elements as $idx => $button) {
184
            if ($button instanceof MoodleQuickForm_submit && $button->getName() === $buttonname) {
185
                unset($el->_elements[$idx]);
186
                return;
187
            }
188
        }
189
    }
190
 
191
    /**
192
     * Returns value for this element that is already stored in temporary or permanent table,
193
     * usually only available when user clicked "Previous page". Null means no value is stored.
194
     *
195
     * @param stdClass $item
196
     * @return string
197
     */
198
    public function get_item_value($item) {
199
        if ($this->structure instanceof mod_feedback_completion) {
200
            return $this->structure->get_item_value($item);
201
        }
202
        return null;
203
    }
204
 
205
    /**
206
     * Can be used by the items to get the course id for which feedback is taken
207
     *
208
     * This function returns 0 for feedbacks that are located inside the courses.
209
     * $this->get_feedback()->course will return the course where feedback is located.
210
     * $this->get_current_course_id() will return the course where user was before taking the feedback
211
     *
212
     * @return int
213
     */
214
    public function get_course_id() {
215
        return $this->structure->get_courseid();
216
    }
217
 
218
    /**
219
     * Record from 'feedback' table corresponding to the current feedback
220
     * @return stdClass
221
     */
222
    public function get_feedback() {
223
        return $this->structure->get_feedback();
224
    }
225
 
226
    /**
227
     * Current feedback mode, see constants on the top of this class
228
     * @return int
229
     */
230
    public function get_mode() {
231
        return $this->mode;
232
    }
233
 
234
    /**
235
     * Returns whether the form is frozen, some items may prefer to change the element
236
     * type in case of frozen form. For example, text or textarea element does not look
237
     * nice when frozen
238
     *
239
     * @return bool
240
     */
241
    public function is_frozen() {
242
        return $this->mode == self::MODE_VIEW_RESPONSE;
243
    }
244
 
245
    /**
246
     * Returns the current course module
247
     * @return cm_info
248
     */
249
    public function get_cm() {
250
        return $this->structure->get_cm();
251
    }
252
 
253
    /**
254
     * Returns the course where user was before taking the feedback.
255
     *
256
     * For feedbacks inside the course it will be the same as $this->get_feedback()->course.
257
     * For feedbacks on the frontpage it will be the same as $this->get_course_id()
258
     *
259
     * @return int
260
     */
261
    public function get_current_course_id() {
262
        return $this->structure->get_courseid() ?: $this->get_feedback()->course;
263
    }
264
 
265
    /**
266
     * CSS class for the item
267
     * @param stdClass $item
268
     * @return string
269
     */
270
    protected function get_suggested_class($item) {
271
        $class = "feedback_itemlist feedback-item-{$item->typ}";
272
        if ($item->dependitem) {
273
            $class .= " feedback_is_dependent";
274
        }
275
        if ($item->typ !== 'pagebreak') {
276
            $itemobj = feedback_get_item_class($item->typ);
277
            if ($itemobj->get_hasvalue()) {
278
                $class .= " feedback_hasvalue";
279
            }
280
        }
281
        return $class;
282
    }
283
 
284
    /**
285
     * Adds an element to this form - to be used by items in their complete_form_element() method
286
     *
287
     * @param stdClass $item
288
     * @param HTML_QuickForm_element|array $element either completed form element or an array that
289
     *      can be passed as arguments to $this->_form->createElement() function
290
     * @param bool $addrequiredrule automatically add 'required' rule
291
     * @param bool $setdefaultvalue automatically set default value for element
292
     * @return HTML_QuickForm_element
293
     */
294
    public function add_form_element($item, $element, $addrequiredrule = true, $setdefaultvalue = true) {
295
        global $OUTPUT;
296
 
297
        if (is_array($element) && $element[0] == 'group') {
298
            // For groups, use the mforms addGroup API.
299
            // $element looks like: ['group', $groupinputname, $name, $objects, $separator, $appendname],
300
            $element = $this->_form->addGroup($element[3], $element[1], $element[2], $element[4], $element[5]);
301
        } else {
302
            // Add non-group element to the form.
303
            if (is_array($element)) {
304
                $element = call_user_func_array(array($this->_form, 'createElement'), $element);
305
            }
306
            $element = $this->_form->addElement($element);
307
        }
308
 
309
        // Prepend standard CSS classes to the element classes.
310
        $attributes = $element->getAttributes();
311
        $class = !empty($attributes['class']) ? ' ' . $attributes['class'] : '';
312
        $attributes['class'] = $this->get_suggested_class($item) . $class;
313
        $element->setAttributes($attributes);
314
 
315
        // Add required rule.
316
        if ($item->required && $addrequiredrule) {
317
            $this->_form->addRule($element->getName(), get_string('required'), 'required', null, 'client');
318
        }
319
 
320
        // Set default value.
321
        if ($setdefaultvalue && ($tmpvalue = $this->get_item_value($item))) {
11 efrain 322
            $this->_form->setDefault($element->getName(), htmlspecialchars_decode($tmpvalue, ENT_QUOTES));
1 efrain 323
        }
324
 
325
        // Freeze if needed.
326
        if ($this->is_frozen()) {
327
            $element->freeze();
328
        }
329
 
330
        // Add red asterisks on required fields.
331
        if ($item->required) {
332
            $required = $OUTPUT->pix_icon('req', get_string('requiredelement', 'form'));
333
            $element->setLabel($element->getLabel() . $required);
334
            $this->hasrequired = true;
335
        }
336
 
337
        // Add different useful stuff to the question name.
338
        $this->add_item_label($item, $element);
339
        $this->add_item_dependencies($item, $element);
340
        $this->add_item_number($item, $element);
341
 
342
        if ($this->mode == self::MODE_EDIT) {
343
            $this->enhance_name_for_edit($item, $element);
344
        }
345
 
346
        return $element;
347
    }
348
 
349
    /**
350
     * Adds a group element to this form - to be used by items in their complete_form_element() method
351
     *
352
     * @param stdClass $item
353
     * @param string $groupinputname name for the form element
354
     * @param string $name question text
355
     * @param array $elements array of arrays that can be passed to $this->_form->createElement()
356
     * @param string $separator separator between group elements
357
     * @param string $class additional CSS classes for the form element
358
     * @return HTML_QuickForm_element
359
     */
360
    public function add_form_group_element($item, $groupinputname, $name, $elements, $separator,
361
            $class = '') {
362
        $objects = array();
363
        foreach ($elements as $element) {
364
            $object = call_user_func_array(array($this->_form, 'createElement'), $element);
365
            $objects[] = $object;
366
        }
367
        $element = $this->add_form_element($item,
368
                ['group', $groupinputname, $name, $objects, $separator, false],
369
                false,
370
                false);
371
        if ($class !== '') {
372
            $attributes = $element->getAttributes();
373
            $attributes['class'] .= ' ' . $class;
374
            $element->setAttributes($attributes);
375
        }
376
        return $element;
377
    }
378
 
379
    /**
380
     * Adds an item number to the question name (if feedback autonumbering is on)
381
     * @param stdClass $item
382
     * @param HTML_QuickForm_element $element
383
     */
384
    protected function add_item_number($item, $element) {
385
        if ($this->get_feedback()->autonumbering && !empty($item->itemnr)) {
386
            $name = $element->getLabel();
387
            $element->setLabel(html_writer::span($item->itemnr. '.', 'itemnr') . ' ' . $name);
388
        }
389
    }
390
 
391
    /**
392
     * Adds an item label to the question name
393
     * @param stdClass $item
394
     * @param HTML_QuickForm_element $element
395
     */
396
    protected function add_item_label($item, $element) {
397
        if (strlen($item->label) && ($this->mode == self::MODE_EDIT || $this->mode == self::MODE_VIEW_TEMPLATE)) {
398
            $name = get_string('nameandlabelformat', 'mod_feedback',
399
                (object)['label' => format_string($item->label), 'name' => $element->getLabel()]);
400
            $element->setLabel($name);
401
        }
402
    }
403
 
404
    /**
405
     * Adds a dependency description to the question name
406
     * @param stdClass $item
407
     * @param HTML_QuickForm_element $element
408
     */
409
    protected function add_item_dependencies($item, $element) {
410
        $allitems = $this->structure->get_items();
411
        if ($item->dependitem && ($this->mode == self::MODE_EDIT || $this->mode == self::MODE_VIEW_TEMPLATE)) {
412
            if (isset($allitems[$item->dependitem])) {
413
                $dependitem = $allitems[$item->dependitem];
414
                $name = $element->getLabel();
415
                $name .= html_writer::span(' ('.format_string($dependitem->label).'-&gt;'.$item->dependvalue.')',
416
                        'feedback_depend');
417
                $element->setLabel($name);
418
            }
419
        }
420
    }
421
 
422
    /**
423
     * Returns the CSS id attribute that will be assigned by moodleform later to this element
424
     * @param stdClass $item
425
     * @param HTML_QuickForm_element $element
426
     */
427
    protected function guess_element_id($item, $element) {
428
        if (!$id = $element->getAttribute('id')) {
429
            $attributes = $element->getAttributes();
430
            $id = $attributes['id'] = 'feedback_item_' . $item->id;
431
            $element->setAttributes($attributes);
432
        }
433
        if ($element->getType() === 'group') {
434
            return 'fgroup_' . $id;
435
        }
436
        return 'fitem_' . $id;
437
    }
438
 
439
    /**
440
     * Adds editing actions to the question name in the edit mode
441
     * @param stdClass $item
442
     * @param HTML_QuickForm_element $element
443
     */
444
    protected function enhance_name_for_edit($item, $element) {
445
        global $OUTPUT;
446
        $menu = new action_menu();
447
        $menu->set_owner_selector('#' . $this->guess_element_id($item, $element));
448
        $menu->set_menu_trigger(get_string('edit'));
449
        $menu->prioritise = true;
450
 
451
        $itemobj = feedback_get_item_class($item->typ);
452
        $actions = $itemobj->edit_actions($item, $this->get_feedback(), $this->get_cm());
453
        foreach ($actions as $action) {
454
            $menu->add($action);
455
        }
456
        $editmenu = $OUTPUT->render($menu);
457
 
458
        $name = $element->getLabel();
459
 
460
        $name = html_writer::span('', 'itemdd', array('id' => 'feedback_item_box_' . $item->id)) .
461
                html_writer::span($name, 'itemname') .
462
                html_writer::span($editmenu, 'itemactions');
463
        $element->setLabel(html_writer::span($name, 'itemtitle'));
464
    }
465
 
466
    /**
467
     * Sets the default value for form element - alias to $this->_form->setDefault()
468
     * @param HTML_QuickForm_element|string $element
469
     * @param mixed $defaultvalue
470
     */
471
    public function set_element_default($element, $defaultvalue) {
472
        if ($element instanceof HTML_QuickForm_element) {
473
            $element = $element->getName();
474
        }
475
        $this->_form->setDefault($element, $defaultvalue);
476
    }
477
 
478
 
479
    /**
480
     * Sets the default value for form element - wrapper to $this->_form->setType()
481
     * @param HTML_QuickForm_element|string $element
482
     * @param int $type
483
     */
484
    public function set_element_type($element, $type) {
485
        if ($element instanceof HTML_QuickForm_element) {
486
            $element = $element->getName();
487
        }
488
        $this->_form->setType($element, $type);
489
    }
490
 
491
    /**
492
     * Adds a validation rule for the given field - wrapper for $this->_form->addRule()
493
     *
494
     * Do not use for 'required' rule!
495
     * Required * will be added automatically, if additional validation is needed
496
     * use method {@link self::add_validation_rule()}
497
     *
498
     * @param string $element Form element name
499
     * @param string $message Message to display for invalid data
500
     * @param string $type Rule type, use getRegisteredRules() to get types
501
     * @param string $format (optional)Required for extra rule data
502
     * @param string $validation (optional)Where to perform validation: "server", "client"
503
     * @param bool $reset Client-side validation: reset the form element to its original value if there is an error?
504
     * @param bool $force Force the rule to be applied, even if the target form element does not exist
505
     */
506
    public function add_element_rule($element, $message, $type, $format = null, $validation = 'server',
507
            $reset = false, $force = false) {
508
        if ($element instanceof HTML_QuickForm_element) {
509
            $element = $element->getName();
510
        }
511
        $this->_form->addRule($element, $message, $type, $format, $validation, $reset, $force);
512
    }
513
 
514
    /**
515
     * Adds a validation rule to the form
516
     *
517
     * @param callable $callback with arguments ($values, $files)
518
     */
519
    public function add_validation_rule(callable $callback) {
520
        if ($this->mode == self::MODE_COMPLETE) {
521
            $this->_form->addFormRule($callback);
522
        }
523
    }
524
 
525
    /**
526
     * Returns a reference to the element - wrapper for function $this->_form->getElement()
527
     *
528
     * @param string $elementname Element name
529
     * @return HTML_QuickForm_element reference to element
530
     */
531
    public function get_form_element($elementname) {
532
        return $this->_form->getElement($elementname);
533
    }
534
 
535
    /**
536
     * Displays the form
537
     */
538
    public function display() {
539
        global $OUTPUT, $PAGE;
540
        // Finalize the form definition if not yet done.
541
        if (!$this->_definition_finalized) {
542
            $this->_definition_finalized = true;
543
            $this->definition_after_data();
544
        }
545
 
546
        $mform = $this->_form;
547
 
548
        // Add "This form has required fields" text in the bottom of the form.
549
        if (($mform->_required || $this->hasrequired) &&
550
               ($this->mode == self::MODE_COMPLETE || $this->mode == self::MODE_PRINT || $this->mode == self::MODE_VIEW_TEMPLATE)) {
551
            $element = $mform->addElement('static', 'requiredfields', '',
552
                    get_string('somefieldsrequired', 'form',
553
                            $OUTPUT->pix_icon('req', get_string('requiredelement', 'form'))));
554
            $element->setAttributes($element->getAttributes() + ['class' => 'requirednote']);
555
        }
556
 
557
        // Reset _required array so the default red * are not displayed.
558
        $mform->_required = array();
559
 
560
        // Move buttons to the end of the form.
561
        if ($this->mode == self::MODE_COMPLETE) {
562
            $mform->addElement('hidden', '__dummyelement');
563
            $buttons = $mform->removeElement('buttonar', false);
564
            $mform->insertElementBefore($buttons, '__dummyelement');
565
            $mform->removeElement('__dummyelement');
566
        }
567
 
568
        $this->_form->display();
569
 
570
        if ($this->mode == self::MODE_EDIT) {
571
            $PAGE->requires->js_call_amd('mod_feedback/edit', 'setup');
572
        }
573
    }
574
}