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
/**
18
 * The form class for editing questions.
19
 * @package mod_questionnaire
20
 * @copyright  2016 Mike Churchward (mike.churchward@poetgroup.org)
21
 * @author Mike Churchward & Joseph Rézeau
22
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
23
 */
24
 
25
namespace mod_questionnaire;
26
 
27
defined('MOODLE_INTERNAL') || die();
28
 
29
require_once($CFG->libdir . '/formslib.php');
30
 
31
/**
32
 * Class edit_question_form
33
 * @package mod_questionnaire
34
 * @property \MoodleQuickForm _form
35
 * @property array _customdata
36
 */
37
class edit_question_form extends \moodleform {
38
 
39
    /**
40
     * Form definition.
41
     */
42
    public function definition() {
43
        // TODO - Find a way to not use globals. Maybe the base class allows more parameters to be passed?
44
        global $questionnaire, $question, $SESSION;
45
 
46
        // TODO - Is there a better way to do this without session global?
47
        // The 'sticky' required response value for further new questions.
48
        if (isset($SESSION->questionnaire->required) && !isset($question->qid)) {
49
            $question->required = $SESSION->questionnaire->required;
50
        }
51
        if (!isset($question->type_id)) {
52
            throw new \moodle_exception('undefinedquestiontype', 'mod_questionnaire');
53
        }
54
 
55
        // Each question can provide its own form elements to the provided form, or use the default ones.
56
        if (!$question->edit_form($this, $questionnaire)) {
57
            throw new \moodle_exception('Question type had an unknown error in the edit_form method.', 'mod_questionnaire');
58
        }
59
    }
60
 
61
    /**
62
     * Form validation.
63
     *
64
     * @param array $data array of ("fieldname"=>value) of submitted data
65
     * @param array $files array of uploaded files "element_name"=>tmp_file_path
66
     * @return array of "element_name"=>"error_description" if there are errors,
67
     *         or an empty array if everything is OK (true allowed for backwards compatibility too).
68
     */
69
    public function validation($data, $files) {
70
        $errors = parent::validation($data, $files);
71
 
72
        // If this is a rate question.
73
        if ($data['type_id'] == QUESRATE) {
74
            if ($data['length'] < 2) {
75
                $errors["length"] = get_string('notenoughscaleitems', 'questionnaire');
76
            }
77
            // If this is a rate question with no duplicates option.
78
            if ($data['precise'] == 2 ) {
79
                $allchoices = $data['allchoices'];
80
                $allchoices = explode("\n", $allchoices);
81
                $nbvalues = 0;
82
                foreach ($allchoices as $choice) {
83
                    if ($choice && !preg_match("/^[0-9]{1,3}=/", $choice)) {
84
                            $nbvalues++;
85
                    }
86
                }
87
                if ($nbvalues < 2) {
88
                    $errors["allchoices"] = get_string('noduplicateschoiceserror', 'questionnaire');
89
                }
90
            }
91
        }
92
 
93
        // If this is a slider question.
94
        if ($data['type_id'] == QUESSLIDER) {
95
            if (isset($data['minrange']) && isset($data['maxrange']) && isset($data['startingvalue']) &&
96
                    isset($data['stepvalue'])) {
97
                if ($data['minrange'] >= $data['maxrange']) {
98
                    $errors['maxrange'] = get_string('invalidrange', 'questionnaire');
99
                }
100
 
101
                if (($data['startingvalue'] > $data['maxrange']) || ($data['startingvalue'] < $data['minrange'])) {
102
                    $errors['startingvalue'] = get_string('invalidstartingvalue', 'questionnaire');
103
                }
104
 
105
                if ($data['startingvalue'] > 100 || $data['startingvalue'] < -100) {
106
                    $errors['startingvalue'] = get_string('invalidstartingvalue', 'questionnaire');
107
                }
108
 
109
                if (($data['stepvalue'] > $data['maxrange']) || $data['stepvalue'] < 1) {
110
                    $errors['stepvalue'] = get_string('invalidincrement', 'questionnaire');
111
                }
112
 
113
                if ($data['minrange'] < -100) {
114
                    $errors['minrange'] = get_string('invalidminmaxrange', 'questionnaire');
115
                }
116
 
117
                if ($data['maxrange'] > 100) {
118
                    $errors['maxrange'] = get_string('invalidminmaxrange', 'questionnaire');
119
                }
120
            }
121
        }
122
 
123
        return $errors;
124
    }
125
 
126
    /**
127
     * Magic method for getting the protected $_form MoodleQuickForm and $_customdata array properties.
128
     * @param string $name
129
     * @return mixed
130
     */
131
    public function __get($name) {
132
        if ($name == '_form') {
133
            return $this->_form;
134
        } else if ($name == '_customdata') {
135
            return $this->_customdata;
136
        } else {
137
            throw new \coding_exception($name.' is not a publicly accessible property of '.get_class($this));
138
        }
139
    }
140
}