Proyectos de Subversion Moodle

Rev

| 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
namespace mod_questionnaire;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
require_once($CFG->libdir . '/formslib.php');
22
require_once($CFG->dirroot.'/mod/questionnaire/lib.php');
23
 
24
/**
25
 * Print the form to add or edit a questionnaire-instance
26
 *
27
 * @package mod_questionnaire
28
 * @copyright  2016 Mike Churchward (mike.churchward@poetgroup.org)
29
 * @author Joseph Rezeau (based on Quiz by Tim Hunt)
30
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
31
 */
32
class feedback_section_form extends \moodleform {
33
 
34
    /** @var mixed $_feedbacks */
35
    protected $_feedbacks;
36
 
37
    /**
38
     * Form definition.
39
     */
40
    public function definition() {
41
        global $questionnaire;
42
 
43
        $feedbacksection = $this->_customdata->feedbacksection;
44
        $validquestions = $this->_customdata->validquestions;
45
        $survey = $this->_customdata->survey;
46
        $feedbacksections = $questionnaire->survey->feedbacksections;
47
        $this->_feedbacks = $feedbacksection->sectionfeedback;
48
        $this->context = $questionnaire->context;
49
        $mform    =& $this->_form;
50
 
51
        if ($survey->feedbacksections > 1) {
52
            $mform->addElement('header', 'feedbacksectionsheader', get_string('feedbacksections', 'questionnaire'));
53
            $sselectgroup = [];
54
            $sselect = $mform->createElement('select', 'navigatesections', get_string('navigatetosection', 'questionnaire'),
55
                $this->_customdata->sectionselect);
56
            $sselect->setSelected($feedbacksection->id);
57
            $sselectgroup[] = $sselect;
58
            $sselectgroup[] = $mform->createElement('submit', 'gotosection', get_string('navigatetosection', 'questionnaire'));
59
            $mform->addGroup($sselectgroup, '');
60
            $addnewsectionarray = [];
61
            $addnewsectionarray[] = $mform->createElement('text', 'newsectionlabel',
62
                get_string('feedbacksectionlabel', 'questionnaire'));
63
            $mform->setType('newsectionlabel', PARAM_TEXT);
64
            $addnewsectionarray[] = $mform->createElement('submit', 'addnewsection', get_string('addnewsection', 'questionnaire'));
65
            $mform->addGroup($addnewsectionarray, '', get_string('feedbacksectionlabel', 'questionnaire'));
66
        }
67
 
68
        if ($feedbacksections == 1) {
69
            $label = get_string('feedbackglobal', 'questionnaire');
70
            $feedbackheading = get_string('feedbackglobalheading', 'questionnaire');
71
            $feedbackmessages = get_string('feedbackglobalmessages', 'questionnaire');
72
        } else {
73
            $label = $feedbacksection->sectionlabel;
74
            $feedbackheading = get_string('feedbacksectionheading', 'questionnaire', $label);
75
            $feedbackmessages = get_string('feedbackmessages', 'questionnaire', $label);
76
        }
77
 
78
        $mform->addElement('header', 'contenthdr', $feedbackheading);
79
        $mform->addElement('text', 'sectionlabel', get_string('feedbacksectionlabel', 'questionnaire'),
80
            ['size' => '50', 'maxlength' => '50']);
81
        $mform->setType('sectionlabel', PARAM_TEXT);
82
        $mform->addRule('sectionlabel', null, 'required', null, 'client');
83
        $mform->addHelpButton('sectionlabel', 'feedbacksectionlabel', 'questionnaire');
84
 
85
        $editoroptions = ['maxfiles' => EDITOR_UNLIMITED_FILES, 'trusttext' => true];
86
        $mform->addElement('editor', 'sectionheading', get_string('feedbacksectionheadingtext', 'questionnaire'),
87
            null, $editoroptions);
88
        $mform->setType('sectionheading', PARAM_RAW);
89
        $mform->setDefault('feedbacknotes', $questionnaire->survey->feedbacknotes);
90
        $mform->addHelpButton('sectionheading', 'feedbackheading', 'questionnaire');
91
 
92
        if ($questionnaire->survey->feedbacksections > 0) {
93
            // Sections.
94
            if ($survey->feedbacksections > 1) {
95
                $mform->addElement('header', 'fbsection_' . $feedbacksection->id,
96
                    get_string('feedbacksectionquestions', 'questionnaire', $label));
97
                $qvalid = $validquestions;
98
                if (!empty($feedbacksection->scorecalculation)) {
99
                    $rsrc = $questionnaire->renderer->image_url('t/delete');
100
                    $strremove = get_string('remove', 'questionnaire');
101
                    $rextra = ['alt' => $strremove, 'title' => $strremove];
102
                    $counter = 1;
103
                    foreach ($feedbacksection->scorecalculation as $qid => $score) {
104
                        unset($qvalid[$qid]);
105
                        $questionactions = [];
106
                        if ((int)$score !== -1) {
107
                            $weight = '<input type="number" style="width: 4em;" id="weight' . $counter . '" ' .
108
                                'name="weight[' . $qid . ']" min="0.0" max="1.0" step="0.01" ' .
109
                                'value="' . $score . '">';
110
                        } else {
111
                            $weight = '<input type="hidden" id="weight' . $counter . '" name="weight[' . $qid . ']" '.
112
                                'value="' . $score . '">';
113
                        }
114
                        $questionactions[] = $mform->createElement('html', $weight);
115
                        $rextra['value'] = $qid;
116
                        unset($rextra['style']);
117
                        $questionactions[] = $mform->createElement('image', 'confirmremovequestion[' . $qid . ']', $rsrc, $rextra);
118
 
119
                        $mform->addGroup($questionactions, '', $questionnaire->questions[$qid]->name);
120
                        $counter++;
121
                    }
122
                }
123
                if (!empty($qvalid)) {
124
                    // Merge arrays maintaining keys.
125
                    $qselect = [];
126
                    $qselect[] = $mform->createElement('select', 'addquestionselect',
127
                        get_string('addquestiontosection', 'questionnaire'), $qvalid);
128
                    $qselect[] = $mform->createElement('submit', 'addquestion', get_string('addquestion', 'questionnaire'));
129
                    $mform->addGroup($qselect, '', get_string('addquestiontosection', 'questionnaire'));
130
                }
131
            }
132
        }
133
 
134
        // FEEDBACK FIELDS.
135
 
136
        $mform->addElement('header', 'feedbackhdr', $feedbackmessages);
137
        $mform->addHelpButton('feedbackhdr', 'feedback', 'questionnaire');
138
 
139
        $mform->addElement('static', 'scoreboundarystatic1', get_string('feedbackscoreboundary', 'questionnaire'), '100%');
140
 
141
        $repeatarray = [];
142
        $repeatedoptions = [];
143
 
144
        $repeatarray[] = $mform->createElement('editor', 'feedbacktext', get_string('feedback', 'questionnaire'), null,
145
            ['maxfiles' => EDITOR_UNLIMITED_FILES, 'noclean' => true, 'context' => $questionnaire->context]);
146
        $repeatarray[] = $mform->createElement(
147
            'text', 'feedbackboundaries', get_string('feedbackscoreboundary', 'questionnaire'), ['size' => 10]);
148
        $repeatedoptions['feedbacklabel']['type'] = PARAM_RAW;
149
        $repeatedoptions['feedbacktext']['type'] = PARAM_RAW;
150
        $repeatedoptions['feedbackboundaries']['type'] = PARAM_RAW;
151
 
152
        $numfeedbacks = max(count($this->_feedbacks) * 1, 3);
153
 
154
        $nextel = $this->repeat_elements($repeatarray, $numfeedbacks - 1, $repeatedoptions, 'boundary_repeats',
155
            'boundary_add_fields', 2, get_string('feedbackaddmorefeedbacks', 'questionnaire'), true);
156
 
157
        // Put some extra elements in before the button.
158
        $mform->insertElementBefore(
159
            $mform->createElement('editor', "feedbacktext[$nextel]", get_string('feedback', 'questionnaire'), null,
160
                ['maxfiles' => EDITOR_UNLIMITED_FILES, 'noclean' => true, 'context' => $questionnaire->context]),
161
            'boundary_add_fields');
162
        $mform->insertElementBefore(
163
            $mform->createElement('static', 'scoreboundarystatic2', get_string('feedbackscoreboundary', 'questionnaire'), '0%'),
164
            'boundary_add_fields');
165
 
166
        // Hidden fields.
167
        $mform->addElement('hidden', 'id', 0);
168
        $mform->setType('id', PARAM_INT);
169
        $mform->addElement('hidden', 'sectionid', 0);
170
        $mform->setType('sectionid', PARAM_INT);
171
 
172
        // Buttons.
173
        $buttonarray = [];
174
        $buttonarray[] = $mform->createElement('submit', 'submitbutton', get_string('savechanges'));
175
        $buttonarray[] = $mform->createElement('submit', 'confirmdeletesection', get_string('deletesection', 'questionnaire'));
176
        $buttonarray[] = $mform->createElement('cancel');
177
        $mform->addGroup($buttonarray, 'buttonar', '', ' ', false);
178
        $mform->closeHeaderBefore('buttonar');
179
    }
180
 
181
    /**
182
     * Form preprocessing.
183
     * @param array $toform
184
     */
185
    public function data_preprocessing(&$toform) {
186
        if (count($this->_feedbacks)) {
187
            $key = 0;
188
            foreach ($this->_feedbacks as $feedback) {
189
                $draftid = file_get_submitted_draft_itemid('feedbacktext['.$key.']');
190
                $toform['feedbacktext['.$key.']']['text'] = file_prepare_draft_area(
191
                    $draftid,               // Draftid.
192
                    $this->context->id,     // Context.
193
                    'mod_questionnaire',    // Component.
194
                    'feedback',             // Filarea.
195
                    !empty($feedback->id) ? (int)$feedback->id : null, // Itemid.
196
                    null,
197
                    $feedback->feedbacktext // Text.
198
                );
199
                $toform['feedbacktext['.$key.']']['format'] = 1;
200
                $toform['feedbacklabel['.$key.']'] = $feedback->feedbacklabel;
201
                $toform['feedbacktext['.$key.']']['itemid'] = $draftid;
202
 
203
                if ($feedback->minscore > 0) {
204
                    $toform['feedbackboundaries['.$key.']'] = (100.0 * $feedback->minscore / 100 ) . '%';
205
                }
206
                $key++;
207
            }
208
        }
209
    }
210
 
211
    /**
212
     * Form validation.
213
     * @param array $data
214
     * @param array $files
215
     * @return array
216
     */
217
    public function validation($data, $files) {
218
        $errors = parent::validation($data, $files);
219
 
220
        // Check the boundary value is a number or a percentage, and in range.
221
        $i = 0;
222
        while (!empty($data['feedbackboundaries'][$i])) {
223
            $boundary = trim($data['feedbackboundaries'][$i]);
224
            if (strlen($boundary) > 0 && $boundary[strlen($boundary) - 1] == '%') {
225
                $boundary = trim(substr($boundary, 0, -1));
226
                if (is_numeric($boundary)) {
227
                    $boundary = $boundary * 100 / 100.0;
228
                } else {
229
                    $errors["feedbackboundaries[$i]"] = get_string('feedbackerrorboundaryformat', 'quiz', $i + 1);
230
                }
231
            }
232
            if (is_numeric($boundary) && $boundary <= 0) {
233
                $errors["feedbackboundaries[$i]"] = get_string('feedbackerrorboundaryoutofrange', 'questionnaire', $i + 1);
234
            }
235
            if (is_numeric($boundary) && $i > 0 &&
236
                    $boundary >= $data['feedbackboundaries'][$i - 1]) {
237
                $errors["feedbackboundaries[$i]"] = get_string('feedbackerrororder', 'questionnaire', $i + 1);
238
            }
239
            $data['feedbackboundaries'][$i] = $boundary;
240
            $i += 1;
241
        }
242
        $numboundaries = $i;
243
 
244
        // Check there is nothing in the remaining unused fields.
245
        if (!empty($data['feedbackboundaries'])) {
246
            for ($i = $numboundaries; $i < count($data['feedbackboundaries']); $i += 1) {
247
                if (!empty($data['feedbackboundaries'][$i] ) &&
248
                        trim($data['feedbackboundaries'][$i] ) != '') {
249
                    $errors["feedbackboundaries[$i]"] = get_string('feedbackerrorjunkinboundary', 'questionnaire', $i + 1);
250
                }
251
            }
252
        }
253
        for ($i = $numboundaries + 1; $i < count($data['feedbacktext']); $i += 1) {
254
            if (!empty($data['feedbacktext'][$i]['text']) &&
255
                    trim($data['feedbacktext'][$i]['text'] ) != '') {
256
                $errors["feedbacktext[$i]"] = get_string('feedbackerrorjunkinfeedback', 'questionnaire', $i + 1);
257
            }
258
        }
259
        return $errors;
260
    }
261
 
262
    /**
263
     * Load in existing data as form defaults. Usually new entry defaults are stored directly in
264
     * form definition (new entry form); this function is used to load in data where values
265
     * already exist and data is being edited (edit entry form).
266
     *
267
     * @param array $defaultvalues
268
     */
269
    public function set_data($defaultvalues) {
270
        if (is_object($defaultvalues)) {
271
            $defaultvalues = (array)$defaultvalues;
272
        }
273
        $this->data_preprocessing($defaultvalues);
274
        parent::set_data($defaultvalues);
275
    }
276
}