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\question;
18
 
19
/**
20
 * This file contains the parent class for yesno question types.
21
 *
22
 * @author Mike Churchward
23
 * @copyright  2016 onward Mike Churchward (mike.churchward@poetopensource.org)
24
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
25
 * @package mod_questionnaire
26
 */
27
class yesno extends question {
28
 
29
    /**
30
     * Each question type must define its response class.
31
     * @return object The response object based off of questionnaire_response_base.
32
     */
33
    protected function responseclass() {
34
        return '\\mod_questionnaire\\responsetype\\boolean';
35
    }
36
 
37
    /**
38
     * Short name for this question type - no spaces, etc..
39
     * @return string
40
     */
41
    public function helpname() {
42
        return 'yesno';
43
    }
44
 
45
    /**
46
     * Override and return a form template if provided. Output of question_survey_display is iterpreted based on this.
47
     * @return string
48
     */
49
    public function question_template() {
50
        return 'mod_questionnaire/question_yesno';
51
    }
52
 
53
    /**
54
     * Override and return a response template if provided. Output of question_survey_display is iterpreted based on this.
55
     * @return string
56
     */
57
    public function response_template() {
58
        return 'mod_questionnaire/response_yesno';
59
    }
60
 
61
    /**
62
     * Override this and return true if the question type allows dependent questions.
63
     * @return bool
64
     */
65
    public function allows_dependents() {
66
        return true;
67
    }
68
 
69
    /**
70
     * True if question type supports feedback options. False by default.
71
     * @return bool
72
     */
73
    public function supports_feedback() {
74
        return true;
75
    }
76
 
77
    /**
78
     * True if the question supports feedback and has valid settings for feedback. Override if the default logic is not enough.
79
     * @return bool
80
     */
81
    public function valid_feedback() {
82
        return $this->required();
83
    }
84
 
85
    /**
86
     * Get the maximum score possible for feedback if appropriate. Override if default behaviour is not correct.
87
     * @return int | boolean
88
     */
89
    public function get_feedback_maxscore() {
90
        if ($this->valid_feedback()) {
91
            $maxscore = 1;
92
        } else {
93
            $maxscore = false;
94
        }
95
        return $maxscore;
96
    }
97
 
98
    /**
99
     * Returns an array of dependency options for the question as an array of id value / display value pairs. Override in specific
100
     * question types that support this.
101
     * @return array An array of valid pair options.
102
     */
103
    protected function get_dependency_options() {
104
        $options = [];
105
        if ($this->name != '') {
106
            $options[$this->id . ',0'] = $this->name . '->' . get_string('yes');
107
            $options[$this->id . ',1'] = $this->name . '->' . get_string('no');
108
        }
109
        return $options;
110
    }
111
 
112
    /**
113
     * Return the context tags for the check question template.
114
     * @param \mod_questionnaire\responsetype\response\response $response
115
     * @param array $dependants Array of all questions/choices depending on this question.
116
     * @param boolean $blankquestionnaire
117
     * @return object The check question context tags.
118
     * @throws \coding_exception
119
     */
120
    protected function question_survey_display($response, $dependants=[], $blankquestionnaire=false) {
121
        global $idcounter;  // To make sure all radio buttons have unique ids. // JR 20 NOV 2007.
122
 
123
        $stryes = get_string('yes');
124
        $strno = get_string('no');
125
 
126
        $val1 = 'y';
127
        $val2 = 'n';
128
 
129
        if ($blankquestionnaire) {
130
            $stryes = ' (1) '.$stryes;
131
            $strno = ' (0) '.$strno;
132
        }
133
 
134
        $options = [$val1 => $stryes, $val2 => $strno];
135
        $name = 'q'.$this->id;
136
        $checked = (isset($response->answers[$this->id][0]) ? $response->answers[$this->id][0]->value : '');
137
        $ischecked = false;
138
 
139
        $choicetags = new \stdClass();
140
        $choicetags->qelements = new \stdClass();
141
        $choicetags->qelements->choice = [];
142
 
143
        foreach ($options as $value => $label) {
144
            $htmlid = 'auto-rb'.sprintf('%04d', ++$idcounter);
145
            $option = new \stdClass();
146
            $option->name = $name;
147
            $option->id = $htmlid;
148
            $option->value = $value;
149
            $option->label = $label;
150
            if ($value == $checked) {
151
                $option->checked = true;
152
                $ischecked = true;
153
            }
154
            if ($blankquestionnaire) {
155
                $option->disabled = true;
156
            }
157
            $choicetags->qelements->choice[] = $option;
158
        }
159
        // CONTRIB-846.
160
        if (!$this->required()) {
161
            $id = '';
162
            $htmlid = 'auto-rb'.sprintf('%04d', ++$idcounter);
163
            $content = get_string('noanswer', 'questionnaire');
164
            $option = new \stdClass();
165
            $option->name = $name;
166
            $option->id = $htmlid;
167
            $option->value = $id;
168
            $option->label = format_text($content, FORMAT_HTML, ['noclean' => true]);
169
            if (!$ischecked && !$blankquestionnaire) {
170
                $option->checked = true;
171
            }
172
            $choicetags->qelements->choice[] = $option;
173
        }
174
        // End CONTRIB-846.
175
 
176
        return $choicetags;
177
    }
178
 
179
    /**
180
     * Return the context tags for the text response template.
181
     * @param \mod_questionnaire\responsetype\response\response $response
182
     * @return object The radio question response context tags.
183
     * @throws \coding_exception
184
     */
185
    protected function response_survey_display($response) {
186
        static $uniquetag = 0;  // To make sure all radios have unique names.
187
 
188
        $resptags = new \stdClass();
189
 
190
        $resptags->yesname = 'q'.$this->id.$uniquetag++.'y';
191
        $resptags->noname = 'q'.$this->id.$uniquetag++.'n';
192
        $resptags->stryes = get_string('yes');
193
        $resptags->strno = get_string('no');
194
        if (!isset($response->answers[$this->id])) {
195
            $response->answers[$this->id][] = new \mod_questionnaire\responsetype\answer\answer();
196
        }
197
        $answer = reset($response->answers[$this->id]);
198
        if ($answer->value == 'y') {
199
            $resptags->yesselected = 1;
200
        }
201
        if ($answer->value == 'n') {
202
            $resptags->noselected = 1;
203
        }
204
 
205
        return $resptags;
206
    }
207
 
208
    /**
209
     * Return the length form element.
210
     * @param \MoodleQuickForm $mform
211
     * @param string $helpname
212
     */
213
    protected function form_length(\MoodleQuickForm $mform, $helpname = '') {
214
        return question::form_length_hidden($mform);
215
    }
216
 
217
    /**
218
     * Return the precision form element.
219
     * @param \MoodleQuickForm $mform
220
     * @param string $helpname
221
     */
222
    protected function form_precise(\MoodleQuickForm $mform, $helpname = '') {
223
        return question::form_precise_hidden($mform);
224
    }
225
 
226
    /**
227
     * True if question provides mobile support.
228
     *
229
     * @return bool
230
     */
231
    public function supports_mobile() {
232
        return true;
233
    }
234
 
235
    /**
236
     * Override and return false if not supporting mobile app.
237
     * @param int $qnum
238
     * @param bool $autonum
239
     * @return \stdClass
240
     */
241
    public function mobile_question_display($qnum, $autonum = false) {
242
        $mobiledata = parent::mobile_question_display($qnum, $autonum);
243
        $mobiledata->isbool = true;
244
        return $mobiledata;
245
    }
246
 
247
    /**
248
     * Override and return false if not supporting mobile app.
249
     * @return array
250
     */
251
    public function mobile_question_choices_display() {
252
        $choices = [];
253
        $choices[0] = new \stdClass();
254
        $choices[0]->id = 0;
255
        $choices[0]->choice_id = 'n';
256
        $choices[0]->question_id = $this->id;
257
        $choices[0]->value = null;
258
        $choices[0]->content = get_string('no');
259
        $choices[0]->isbool = true;
260
        $choices[1] = new \stdClass();
261
        $choices[1]->id = 1;
262
        $choices[1]->choice_id = 'y';
263
        $choices[1]->question_id = $this->id;
264
        $choices[1]->value = null;
265
        $choices[1]->content = get_string('yes');
266
        $choices[1]->isbool = true;
267
        if ($this->required()) {
268
            $choices[1]->value = 'y';
269
            $choices[1]->firstone = true;
270
        }
271
 
272
        return $choices;
273
    }
274
 
275
    /**
276
     * Return the mobile response data.
277
     * @param response $response
278
     * @return array
279
     */
280
    public function get_mobile_response_data($response) {
281
        $resultdata = [];
282
        if (isset($response->answers[$this->id][0]) && ($response->answers[$this->id][0]->value == 'n')) {
283
            $resultdata[$this->mobile_fieldkey()] = 0;
284
        } else if (isset($response->answers[$this->id][0]) && ($response->answers[$this->id][0]->value == 'y')) {
285
            $resultdata[$this->mobile_fieldkey()] = 1;
286
        }
287
 
288
        return $resultdata;
289
    }
290
}