Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
if (!defined('MOODLE_INTERNAL')) {
3
    die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
4
}
5
 
6
require_once ($CFG->dirroot.'/course/moodleform_mod.php');
7
 
8
class mod_choice_mod_form extends moodleform_mod {
9
 
10
    function definition() {
1441 ariadna 11
        global $CFG, $DB;
1 efrain 12
 
13
        $mform    =& $this->_form;
14
 
15
//-------------------------------------------------------------------------------
16
        $mform->addElement('header', 'general', get_string('general', 'form'));
17
 
18
        $mform->addElement('text', 'name', get_string('choicename', 'choice'), array('size'=>'64'));
19
        if (!empty($CFG->formatstringstriptags)) {
20
            $mform->setType('name', PARAM_TEXT);
21
        } else {
22
            $mform->setType('name', PARAM_CLEANHTML);
23
        }
24
        $mform->addRule('name', null, 'required', null, 'client');
25
        $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
26
 
27
        $this->standard_intro_elements(get_string('description', 'choice'));
28
 
1441 ariadna 29
        $mform->addElement('select', 'display', get_string("displaymode", "choice"), [
30
            CHOICE_DISPLAY_HORIZONTAL   => get_string('displayhorizontal', 'choice'),
31
            CHOICE_DISPLAY_VERTICAL     => get_string('displayvertical', 'choice'),
32
        ]);
1 efrain 33
 
34
        //-------------------------------------------------------------------------------
35
        $mform->addElement('header', 'optionhdr', get_string('options', 'choice'));
36
 
37
        $mform->addElement('selectyesno', 'allowupdate', get_string("allowupdate", "choice"));
38
 
39
        $mform->addElement('selectyesno', 'allowmultiple', get_string('allowmultiple', 'choice'));
40
        if ($this->_instance) {
41
            if ($DB->count_records('choice_answers', array('choiceid' => $this->_instance)) > 0) {
42
                // Prevent user from toggeling the number of allowed answers once there are submissions.
43
                $mform->freeze('allowmultiple');
44
            }
45
        }
46
 
47
        $mform->addElement('selectyesno', 'limitanswers', get_string('limitanswers', 'choice'));
48
        $mform->addHelpButton('limitanswers', 'limitanswers', 'choice');
49
 
50
        $mform->addElement('selectyesno', 'showavailable', get_string('showavailable', 'choice'));
51
        $mform->addHelpButton('showavailable', 'showavailable', 'choice');
52
        $mform->hideIf('showavailable', 'limitanswers', 'eq', 0);
53
 
54
        $repeatarray = array();
55
        $repeatarray[] = $mform->createElement('text', 'option', get_string('optionno', 'choice'));
56
        $repeatarray[] = $mform->createElement('text', 'limit', get_string('limitno', 'choice'));
57
        $repeatarray[] = $mform->createElement('hidden', 'optionid', 0);
58
 
59
        if ($this->_instance){
60
            $repeatno = $DB->count_records('choice_options', array('choiceid'=>$this->_instance));
61
            $repeatno += 2;
62
        } else {
63
            $repeatno = 5;
64
        }
65
 
66
        $repeateloptions = array();
67
        $repeateloptions['limit']['default'] = 0;
68
        $repeateloptions['limit']['hideif'] = array('limitanswers', 'eq', 0);
69
        $repeateloptions['limit']['rule'] = 'numeric';
70
        $repeateloptions['limit']['type'] = PARAM_INT;
71
 
72
        $repeateloptions['option']['helpbutton'] = array('choiceoptions', 'choice');
73
        $mform->setType('option', PARAM_CLEANHTML);
74
 
75
        $mform->setType('optionid', PARAM_INT);
76
 
77
        $this->repeat_elements($repeatarray, $repeatno,
78
                    $repeateloptions, 'option_repeats', 'option_add_fields', 3, null, true);
79
 
80
        // Make the first option required
81
        if ($mform->elementExists('option[0]')) {
82
            $mform->addRule('option[0]', get_string('atleastoneoption', 'choice'), 'required', null, 'client');
83
        }
84
 
85
//-------------------------------------------------------------------------------
86
        $mform->addElement('header', 'availabilityhdr', get_string('availability'));
87
        $mform->addElement('date_time_selector', 'timeopen', get_string("choiceopen", "choice"),
88
            array('optional' => true));
89
 
90
        $mform->addElement('date_time_selector', 'timeclose', get_string("choiceclose", "choice"),
91
            array('optional' => true));
92
 
93
        $mform->addElement('advcheckbox', 'showpreview', get_string('showpreview', 'choice'));
94
        $mform->addHelpButton('showpreview', 'showpreview', 'choice');
95
        $mform->disabledIf('showpreview', 'timeopen[enabled]');
96
 
97
//-------------------------------------------------------------------------------
98
        $mform->addElement('header', 'resultshdr', get_string('results', 'choice'));
99
 
1441 ariadna 100
        $mform->addElement('select', 'showresults', get_string("publish", "choice"), [
101
            CHOICE_SHOWRESULTS_NOT          => get_string('publishnot', 'choice'),
102
            CHOICE_SHOWRESULTS_AFTER_ANSWER => get_string('publishafteranswer', 'choice'),
103
            CHOICE_SHOWRESULTS_AFTER_CLOSE  => get_string('publishafterclose', 'choice'),
104
            CHOICE_SHOWRESULTS_ALWAYS       => get_string('publishalways', 'choice'),
105
        ]);
1 efrain 106
 
1441 ariadna 107
        $mform->addElement('select', 'publish', get_string("privacy", "choice"), [
108
            CHOICE_PUBLISH_ANONYMOUS  => get_string('publishanonymous', 'choice'),
109
            CHOICE_PUBLISH_NAMES      => get_string('publishnames', 'choice'),
110
        ]);
111
 
1 efrain 112
        $mform->hideIf('publish', 'showresults', 'eq', 0);
113
 
114
        $mform->addElement('selectyesno', 'showunanswered', get_string("showunanswered", "choice"));
115
 
116
        $mform->addElement('selectyesno', 'includeinactive', get_string('includeinactive', 'choice'));
117
        $mform->setDefault('includeinactive', 0);
118
 
119
//-------------------------------------------------------------------------------
120
        $this->standard_coursemodule_elements();
121
//-------------------------------------------------------------------------------
122
        $this->add_action_buttons();
123
    }
124
 
125
    function data_preprocessing(&$default_values){
126
        global $DB;
127
        if (!empty($this->_instance) && ($options = $DB->get_records_menu('choice_options',array('choiceid'=>$this->_instance), 'id', 'id,text'))
128
               && ($options2 = $DB->get_records_menu('choice_options', array('choiceid'=>$this->_instance), 'id', 'id,maxanswers')) ) {
129
            $choiceids=array_keys($options);
130
            $options=array_values($options);
131
            $options2=array_values($options2);
132
 
133
            foreach (array_keys($options) as $key){
134
                $default_values['option['.$key.']'] = $options[$key];
135
                $default_values['limit['.$key.']'] = $options2[$key];
136
                $default_values['optionid['.$key.']'] = $choiceids[$key];
137
            }
138
 
139
        }
140
 
141
    }
142
 
143
    /**
144
     * Allows module to modify the data returned by form get_data().
145
     * This method is also called in the bulk activity completion form.
146
     *
147
     * Only available on moodleform_mod.
148
     *
149
     * @param stdClass $data the form data to be modified.
150
     */
151
    public function data_postprocessing($data) {
152
        parent::data_postprocessing($data);
153
        // Set up completion section even if checkbox is not ticked.
154
        if (!empty($data->completionunlocked)) {
155
            $suffix = $this->get_suffix();
156
            if (empty($data->{'completionsubmit' . $suffix})) {
157
                $data->{'completionsubmit' . $suffix} = 0;
158
            }
159
        }
160
    }
161
 
162
    /**
163
     * Enforce validation rules here
164
     *
165
     * @param array $data array of ("fieldname"=>value) of submitted data
166
     * @param array $files array of uploaded files "element_name"=>tmp_file_path
167
     * @return array
168
     **/
169
    public function validation($data, $files) {
170
        $errors = parent::validation($data, $files);
171
 
172
        // Check open and close times are consistent.
173
        if ($data['timeopen'] && $data['timeclose'] &&
174
                $data['timeclose'] < $data['timeopen']) {
175
            $errors['timeclose'] = get_string('closebeforeopen', 'choice');
176
        }
177
 
178
        return $errors;
179
    }
180
 
181
    public function add_completion_rules() {
182
        $mform =& $this->_form;
183
 
184
        $suffix = $this->get_suffix();
185
        $completionsubmitel = 'completionsubmit' . $suffix;
186
        $mform->addElement('checkbox', $completionsubmitel, '', get_string('completionsubmit', 'choice'));
187
        // Enable this completion rule by default.
188
        $mform->setDefault($completionsubmitel, 1);
189
        return [$completionsubmitel];
190
    }
191
 
192
    public function completion_rule_enabled($data) {
193
        $suffix = $this->get_suffix();
194
        return !empty($data['completionsubmit' . $suffix]);
195
    }
196
}