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
defined('MOODLE_INTERNAL') || die();
18
 
19
require_once($CFG->dirroot.'/course/moodleform_mod.php');
20
require_once($CFG->dirroot.'/mod/questionnaire/questionnaire.class.php');
21
require_once($CFG->dirroot.'/mod/questionnaire/locallib.php');
22
 
23
/**
24
 * print the form to add or edit a questionnaire-instance
25
 *
26
 * @package mod_questionnaire
27
 * @author Mike Churchward
28
 * @copyright  2016 onward Mike Churchward (mike.churchward@poetgroup.org)
29
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
30
 */
31
class mod_questionnaire_mod_form extends moodleform_mod {
32
 
33
    /**
34
     * Form definition.
35
     */
36
    protected function definition() {
37
        global $COURSE;
38
        global $questionnairetypes, $questionnairerespondents, $questionnaireresponseviewers, $autonumbering;
39
 
40
        $questionnaire = new questionnaire($COURSE, $this->_cm, $this->_instance, null);
41
 
42
        $mform    =& $this->_form;
43
 
44
        $mform->addElement('header', 'general', get_string('general', 'form'));
45
 
46
        $mform->addElement('text', 'name', get_string('name', 'questionnaire'), array('size' => '64'));
47
        $mform->setType('name', PARAM_TEXT);
48
        $mform->addRule('name', null, 'required', null, 'client');
49
 
50
        $this->standard_intro_elements(get_string('description'));
51
 
52
        $mform->addElement('header', 'availabilityhdr', get_string('availability'));
53
        $mform->addElement('date_time_selector', 'opendate', get_string('opendate', 'questionnaire'), ['optional' => true]);
54
        $mform->addElement('date_time_selector', 'closedate', get_string('closedate', 'questionnaire'), ['optional' => true]);
55
 
56
        $mform->addElement('header', 'questionnairehdr', get_string('responseoptions', 'questionnaire'));
57
 
58
        $mform->addElement('select', 'qtype', get_string('qtype', 'questionnaire'), $questionnairetypes);
59
        $mform->addHelpButton('qtype', 'qtype', 'questionnaire');
60
 
61
        $mform->addElement('hidden', 'cannotchangerespondenttype');
62
        $mform->setType('cannotchangerespondenttype', PARAM_INT);
63
        $mform->addElement('select', 'respondenttype', get_string('respondenttype', 'questionnaire'), $questionnairerespondents);
64
        $mform->addHelpButton('respondenttype', 'respondenttype', 'questionnaire');
65
        $mform->disabledIf('respondenttype', 'cannotchangerespondenttype', 'eq', 1);
66
 
67
        $mform->addElement('select', 'resp_view', get_string('responseview', 'questionnaire'), $questionnaireresponseviewers);
68
        $mform->addHelpButton('resp_view', 'responseview', 'questionnaire');
69
 
70
        $notificationoptions = array(0 => get_string('no'), 1 => get_string('notificationsimple', 'questionnaire'),
71
            2 => get_string('notificationfull', 'questionnaire'));
72
        $mform->addElement('select', 'notifications', get_string('notifications', 'questionnaire'), $notificationoptions);
73
        $mform->addHelpButton('notifications', 'notifications', 'questionnaire');
74
 
75
        $options = array('0' => get_string('no'), '1' => get_string('yes'));
76
        $mform->addElement('select', 'resume', get_string('resume', 'questionnaire'), $options);
77
        $mform->addHelpButton('resume', 'resume', 'questionnaire');
78
 
79
        $options = array('0' => get_string('no'), '1' => get_string('yes'));
80
        $mform->addElement('select', 'navigate', get_string('navigate', 'questionnaire'), $options);
81
        $mform->addHelpButton('navigate', 'navigate', 'questionnaire');
82
 
83
        $mform->addElement('select', 'autonum', get_string('autonumbering', 'questionnaire'), $autonumbering);
84
        $mform->addHelpButton('autonum', 'autonumbering', 'questionnaire');
85
        // Default = autonumber both questions and pages.
86
        $mform->setDefault('autonum', 3);
87
 
88
        $mform->addElement('advcheckbox', 'progressbar', get_string('progressbar', 'questionnaire'));
89
 
90
        // Removed potential scales from list of grades. CONTRIB-3167.
91
        $grades[0] = get_string('nograde');
92
        for ($i = 100; $i >= 1; $i--) {
93
            $grades[$i] = $i;
94
        }
95
        $mform->addElement('select', 'grade', get_string('grade', 'questionnaire'), $grades);
96
 
97
        if (empty($questionnaire->sid)) {
98
            if (!isset($questionnaire->id)) {
99
                $questionnaire->id = 0;
100
            }
101
 
102
            $mform->addElement('header', 'contenthdr', get_string('contentoptions', 'questionnaire'));
103
            $mform->addHelpButton('contenthdr', 'createcontent', 'questionnaire');
104
 
105
            $mform->addElement('radio', 'create', get_string('createnew', 'questionnaire'), '', 'new-0');
106
 
107
            // Retrieve existing private questionnaires from current course.
108
            $surveys = questionnaire_get_survey_select($COURSE->id, 'private');
109
            if (!empty($surveys)) {
110
                $prelabel = get_string('useprivate', 'questionnaire');
111
                foreach ($surveys as $value => $label) {
112
                    $mform->addElement('radio', 'create', $prelabel, $label, $value);
113
                    $prelabel = '';
114
                }
115
            }
116
            // Retrieve existing template questionnaires from this site.
117
            $surveys = questionnaire_get_survey_select($COURSE->id, 'template');
118
            if (!empty($surveys)) {
119
                $prelabel = get_string('usetemplate', 'questionnaire');
120
                foreach ($surveys as $value => $label) {
121
                    $mform->addElement('radio', 'create', $prelabel, $label, $value);
122
                    $prelabel = '';
123
                }
124
            } else {
125
                $mform->addElement('static', 'usetemplate', get_string('usetemplate', 'questionnaire'),
126
                                '('.get_string('notemplatesurveys', 'questionnaire').')');
127
            }
128
 
129
            // Retrieve existing public questionnaires from this site.
130
            $surveys = questionnaire_get_survey_select($COURSE->id, 'public');
131
            if (!empty($surveys)) {
132
                $prelabel = get_string('usepublic', 'questionnaire');
133
                foreach ($surveys as $value => $label) {
134
                    $mform->addElement('radio', 'create', $prelabel, $label, $value);
135
                    $prelabel = '';
136
                }
137
            } else {
138
                $mform->addElement('static', 'usepublic', get_string('usepublic', 'questionnaire'),
139
                                   '('.get_string('nopublicsurveys', 'questionnaire').')');
140
            }
141
 
142
            $mform->setDefault('create', 'new-0');
143
        }
144
 
145
        $this->standard_coursemodule_elements();
146
 
147
        // Buttons.
148
        $this->add_action_buttons();
149
    }
150
 
151
    /**
152
     * Pre-process form data.
153
     * @param array $defaultvalues
154
     */
155
    public function data_preprocessing(&$defaultvalues) {
156
        global $DB;
157
        if (empty($defaultvalues['opendate'])) {
158
            $defaultvalues['useopendate'] = 0;
159
        } else {
160
            $defaultvalues['useopendate'] = 1;
161
        }
162
        if (empty($defaultvalues['closedate'])) {
163
            $defaultvalues['useclosedate'] = 0;
164
        } else {
165
            $defaultvalues['useclosedate'] = 1;
166
        }
167
        // Prevent questionnaire set to "anonymous" to be reverted to "full name".
168
        $defaultvalues['cannotchangerespondenttype'] = 0;
169
        if (!empty($defaultvalues['respondenttype']) && $defaultvalues['respondenttype'] == "anonymous") {
170
            // If this questionnaire has responses.
171
            $numresp = $DB->count_records('questionnaire_response',
172
                            array('questionnaireid' => $defaultvalues['instance'], 'complete' => 'y'));
173
            if ($numresp) {
174
                $defaultvalues['cannotchangerespondenttype'] = 1;
175
            }
176
        }
177
    }
178
 
179
    /**
180
     * Enforce validation rules here
181
     * @param array $data array of ("fieldname"=>value) of submitted data
182
     * @param array $files array of uploaded files "element_name"=>tmp_file_path
183
     * @return array
184
     **/
185
    public function validation($data, $files) {
186
        $errors = parent::validation($data, $files);
187
 
188
        // Check open and close times are consistent.
189
        if ($data['opendate'] && $data['closedate'] &&
190
            $data['closedate'] < $data['opendate']) {
191
            $errors['closedate'] = get_string('closebeforeopen', 'questionnaire');
192
        }
193
 
194
        return $errors;
195
    }
196
 
197
    /**
198
     * Add any completion rules for the form.
199
     * @return string[]
200
     */
201
    public function add_completion_rules() {
202
        $mform =& $this->_form;
203
        $mform->addElement('checkbox', 'completionsubmit', '', get_string('completionsubmit', 'questionnaire'));
204
        return array('completionsubmit');
205
    }
206
 
207
    /**
208
     * True if the completion rule is enabled.
209
     * @param array $data
210
     * @return bool
211
     */
212
    public function completion_rule_enabled($data) {
213
        return !empty($data['completionsubmit']);
214
    }
215
 
216
}