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 numeric 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 numerical extends question {
28
 
29
    /**
30
     * Constructor. Use to set any default properties.
31
     * @param int $id
32
     * @param \stdClass $question
33
     * @param string $context
34
     * @param array $params
35
     */
36
    public function __construct($id = 0, $question = null, $context = null, $params = []) {
37
        $this->length = 10;
38
        return parent::__construct($id, $question, $context, $params);
39
    }
40
 
41
    /**
42
     * Each question type must define its response class.
43
     *
44
     * @return string The response object based off of questionnaire_response_base.
45
     *
46
     */
47
    protected function responseclass() {
48
        return '\\mod_questionnaire\\responsetype\\numericaltext';
49
    }
50
 
51
    /**
52
     * Short name for this question type - no spaces, etc..
53
     * @return string
54
     */
55
    public function helpname() {
56
        return 'numeric';
57
    }
58
 
59
    /**
60
     * Override and return a form template if provided. Output of question_survey_display is iterpreted based on this.
61
     * @return string
62
     */
63
    public function question_template() {
64
        return 'mod_questionnaire/question_numeric';
65
    }
66
 
67
    /**
68
     * Override and return a response template if provided. Output of response_survey_display is iterpreted based on this.
69
     * @return string
70
     */
71
    public function response_template() {
72
        return 'mod_questionnaire/response_numeric';
73
    }
74
 
75
    /**
76
     * Return the context tags for the check question template.
77
     * @param \mod_questionnaire\responsetype\response\response $response
78
     * @param array $descendantsdata
79
     * @param boolean $blankquestionnaire
80
     * @return \stdClass The check question context tags.
81
     */
82
    protected function question_survey_display($response, $descendantsdata, $blankquestionnaire=false) {
83
        // Numeric.
84
        $questiontags = new \stdClass();
85
        $precision = $this->precise;
86
        $a = '';
87
        if (isset($response->answers[$this->id][0])) {
88
            $mynumber = $response->answers[$this->id][0]->value;
89
            if ($mynumber != '') {
90
                $mynumber0 = $mynumber;
91
                if (!is_numeric($mynumber) ) {
92
                    $msg = get_string('notanumber', 'questionnaire', $mynumber);
93
                    $this->add_notification($msg);
94
                } else {
95
                    if ($precision) {
96
                        $pos = strpos($mynumber, '.');
97
                        if (!$pos) {
98
                            if (strlen($mynumber) > $this->length) {
99
                                $mynumber = substr($mynumber, 0 , $this->length);
100
                            }
101
                        }
102
                        $this->length += (1 + $precision); // To allow for n numbers after decimal point.
103
                    }
104
                    $mynumber = number_format($mynumber, $precision , '.', '');
105
                    if ( $mynumber != $mynumber0) {
106
                        $a->number = $mynumber0;
107
                        $a->precision = $precision;
108
                        $msg = get_string('numberfloat', 'questionnaire', $a);
109
                        $this->add_notification($msg);
110
                    }
111
                }
112
            }
113
            if ($mynumber != '') {
114
                $response->answers[$this->id][0]->value = $mynumber;
115
            }
116
        }
117
 
118
        $choice = new \stdClass();
119
        $choice->onkeypress = 'return event.keyCode != 13;';
120
        $choice->size = $this->length;
121
        // Add a 'thousands separator' instruction if there is a size setting greater than three.
122
        $choice->instruction = (empty($choice->size) || ($choice->size > 3)) ? get_string('thousands', 'mod_questionnaire') : '';
123
        $choice->name = 'q'.$this->id;
124
        $choice->maxlength = $this->length;
125
        $choice->value = (isset($response->answers[$this->id][0]) ? $response->answers[$this->id][0]->value : '');
126
        $choice->id = self::qtypename($this->type_id) . $this->id;
127
        $questiontags->qelements = new \stdClass();
128
        $questiontags->qelements->choice = $choice;
129
        return $questiontags;
130
    }
131
 
132
    /**
133
     * Check question's form data for valid response. Override this is type has specific format requirements.
134
     *
135
     * @param \stdClass $responsedata The data entered into the response.
136
     * @return boolean
137
     */
138
    public function response_valid($responsedata) {
139
        $responseval = false;
140
        if (is_a($responsedata, 'mod_questionnaire\responsetype\response\response')) {
141
            // If $responsedata is a response object, look through the answers.
142
            if (isset($responsedata->answers[$this->id]) && !empty($responsedata->answers[$this->id])) {
143
                $answer = $responsedata->answers[$this->id][0];
144
                $responseval = $answer->value;
145
            }
146
        } else if (isset($responsedata->{'q'.$this->id})) {
147
            $responseval = $responsedata->{'q' . $this->id};
148
        }
149
        if ($responseval !== false) {
150
            // If commas are present, replace them with periods, in case that was meant as the European decimal place.
151
            $responseval = str_replace(',', '.', $responseval);
152
            return (($responseval == '') || is_numeric($responseval));
153
        } else {
154
            return parent::response_valid($responsedata);
155
        }
156
    }
157
 
158
    /**
159
     * Return the context tags for the numeric response template.
160
     * @param \mod_questionnaire\responsetype\response\response $response
161
     * @return \stdClass The numeric question response context tags.
162
     */
163
    protected function response_survey_display($response) {
164
        $resptags = new \stdClass();
165
        if (isset($response->answers[$this->id])) {
166
            $answer = reset($response->answers[$this->id]);
167
            $resptags->content = $answer->value;
168
        }
169
        return $resptags;
170
    }
171
 
172
    /**
173
     * Return the length form element.
174
     * @param \MoodleQuickForm $mform
175
     * @param string $helptext
176
     */
177
    protected function form_length(\MoodleQuickForm $mform, $helptext = '') {
178
        $this->length = isset($this->length) ? $this->length : 10;
179
        return parent::form_length($mform, 'maxdigitsallowed');
180
    }
181
 
182
    /**
183
     * Return the precision form element.
184
     * @param \MoodleQuickForm $mform
185
     * @param string $helptext
186
     */
187
    protected function form_precise(\MoodleQuickForm $mform, $helptext = '') {
188
        return parent::form_precise($mform, 'numberofdecimaldigits');
189
    }
190
 
191
    /**
192
     * True if question provides mobile support.
193
     * @return bool
194
     */
195
    public function supports_mobile() {
196
        return true;
197
    }
198
 
199
    /**
200
     * Return the mobile question display.
201
     * @param int $qnum
202
     * @param bool $autonum
203
     * @return \stdClass
204
     */
205
    public function mobile_question_display($qnum, $autonum = false) {
206
        $mobiledata = parent::mobile_question_display($qnum, $autonum);
207
        $mobiledata->isnumeric = true;
208
        return $mobiledata;
209
    }
210
 
211
    /**
212
     * Return the mobile question choices display.
213
     * @return array
214
     */
215
    public function mobile_question_choices_display() {
216
        $choices = [];
217
        $choices[0] = new \stdClass();
218
        $choices[0]->id = 0;
219
        $choices[0]->choice_id = 0;
220
        $choices[0]->question_id = $this->id;
221
        $choices[0]->content = '';
222
        $choices[0]->value = null;
223
        return $choices;
224
    }
225
}