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
 * print the form to add or edit a feedback-instance
19
 *
20
 * @author Andreas Grabs
21
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
22
 * @package mod_feedback
23
 */
24
 
25
//It must be included from a Moodle page
26
if (!defined('MOODLE_INTERNAL')) {
27
    die('Direct access to this script is forbidden.');
28
}
29
 
30
require_once($CFG->dirroot.'/course/moodleform_mod.php');
31
 
32
class mod_feedback_mod_form extends moodleform_mod {
33
 
34
    public function definition() {
35
        global $CFG, $DB;
36
 
37
        $editoroptions = feedback_get_editor_options();
38
 
39
        $mform    =& $this->_form;
40
 
41
        //-------------------------------------------------------------------------------
42
        $mform->addElement('header', 'general', get_string('general', 'form'));
43
 
44
        $mform->addElement('text', 'name', get_string('name', 'feedback'), array('size'=>'64'));
45
        $mform->setType('name', PARAM_TEXT);
46
        $mform->addRule('name', null, 'required', null, 'client');
47
        $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
48
 
49
        $this->standard_intro_elements(get_string('description', 'feedback'));
50
 
51
        //-------------------------------------------------------------------------------
52
        $mform->addElement('header', 'timinghdr', get_string('availability'));
53
 
54
        $mform->addElement('date_time_selector', 'timeopen', get_string('feedbackopen', 'feedback'),
55
            array('optional' => true));
56
 
57
        $mform->addElement('date_time_selector', 'timeclose', get_string('feedbackclose', 'feedback'),
58
            array('optional' => true));
59
 
60
        //-------------------------------------------------------------------------------
61
        $mform->addElement('header', 'feedbackhdr', get_string('questionandsubmission', 'feedback'));
62
 
63
        $options=array();
64
        $options[1]  = get_string('anonymous', 'feedback');
65
        $options[2]  = get_string('non_anonymous', 'feedback');
66
        $mform->addElement('select',
67
                           'anonymous',
68
                           get_string('anonymous_edit', 'feedback'),
69
                           $options);
70
 
71
        // check if there is existing responses to this feedback
72
        if (is_numeric($this->_instance) AND
73
                    $this->_instance AND
74
                    $feedback = $DB->get_record("feedback", array("id"=>$this->_instance))) {
75
 
76
            $completed_feedback_count = feedback_get_completeds_group_count($feedback);
77
        } else {
78
            $completed_feedback_count = false;
79
        }
80
 
81
        if ($completed_feedback_count) {
82
            $multiple_submit_value = $feedback->multiple_submit ? get_string('yes') : get_string('no');
83
            $mform->addElement('text',
84
                               'multiple_submit_static',
85
                               get_string('multiplesubmit', 'feedback'),
86
                               array('size'=>'4',
87
                                    'disabled'=>'disabled',
88
                                    'value'=>$multiple_submit_value));
89
            $mform->setType('multiple_submit_static', PARAM_RAW);
90
 
91
            $mform->addElement('hidden', 'multiple_submit', '');
92
            $mform->setType('multiple_submit', PARAM_INT);
93
            $mform->addHelpButton('multiple_submit_static', 'multiplesubmit', 'feedback');
94
        } else {
95
            $mform->addElement('selectyesno',
96
                               'multiple_submit',
97
                               get_string('multiplesubmit', 'feedback'));
98
 
99
            $mform->addHelpButton('multiple_submit', 'multiplesubmit', 'feedback');
100
        }
101
 
102
        $mform->addElement('selectyesno', 'email_notification', get_string('email_notification', 'feedback'));
103
        $mform->addHelpButton('email_notification', 'email_notification', 'feedback');
104
 
105
        $mform->addElement('selectyesno', 'autonumbering', get_string('autonumbering', 'feedback'));
106
        $mform->addHelpButton('autonumbering', 'autonumbering', 'feedback');
107
 
108
        //-------------------------------------------------------------------------------
109
        $mform->addElement('header', 'aftersubmithdr', get_string('after_submit', 'feedback'));
110
 
111
        $mform->addElement('selectyesno', 'publish_stats', get_string('show_analysepage_after_submit', 'feedback'));
112
 
113
        $mform->addElement('editor',
114
                           'page_after_submit_editor',
115
                           get_string("page_after_submit", "feedback"),
116
                           null,
117
                           $editoroptions);
118
 
119
        $mform->setType('page_after_submit_editor', PARAM_RAW);
120
 
121
        $mform->addElement('text',
122
                           'site_after_submit',
123
                           get_string('url_for_continue', 'feedback'),
124
                           array('size'=>'64', 'maxlength'=>'255'));
125
 
126
        $mform->setType('site_after_submit', PARAM_TEXT);
127
        $mform->addHelpButton('site_after_submit', 'url_for_continue', 'feedback');
128
        //-------------------------------------------------------------------------------
129
        $this->standard_coursemodule_elements();
130
        //-------------------------------------------------------------------------------
131
        // buttons
132
        $this->add_action_buttons();
133
    }
134
 
135
    public function data_preprocessing(&$default_values) {
136
 
137
        $editoroptions = feedback_get_editor_options();
138
 
139
        if ($this->current->instance) {
140
            // editing an existing feedback - let us prepare the added editor elements (intro done automatically)
141
            $draftitemid = file_get_submitted_draft_itemid('page_after_submit');
142
            $default_values['page_after_submit_editor']['text'] =
143
                                    file_prepare_draft_area($draftitemid, $this->context->id,
144
                                    'mod_feedback', 'page_after_submit', false,
145
                                    $editoroptions,
146
                                    $default_values['page_after_submit']);
147
 
148
            $default_values['page_after_submit_editor']['format'] = $default_values['page_after_submitformat'];
149
            $default_values['page_after_submit_editor']['itemid'] = $draftitemid;
150
        } else {
151
            // adding a new feedback instance
152
            $draftitemid = file_get_submitted_draft_itemid('page_after_submit_editor');
153
 
154
            // no context yet, itemid not used
155
            file_prepare_draft_area($draftitemid, null, 'mod_feedback', 'page_after_submit', false);
156
            $default_values['page_after_submit_editor']['text'] = '';
157
            $default_values['page_after_submit_editor']['format'] = editors_get_preferred_format();
158
            $default_values['page_after_submit_editor']['itemid'] = $draftitemid;
159
        }
160
 
161
    }
162
 
163
    /**
164
     * Allows module to modify the data returned by form get_data().
165
     * This method is also called in the bulk activity completion form.
166
     *
167
     * Only available on moodleform_mod.
168
     *
169
     * @param stdClass $data the form data to be modified.
170
     */
171
    public function data_postprocessing($data) {
172
        parent::data_postprocessing($data);
173
        if (isset($data->page_after_submit_editor)) {
174
            $data->page_after_submitformat = $data->page_after_submit_editor['format'];
175
            $data->page_after_submit = $data->page_after_submit_editor['text'];
176
 
177
            if (!empty($data->completionunlocked)) {
178
                // Turn off completion settings if the checkboxes aren't ticked.
179
                $suffix = $this->get_suffix();
180
                $completion = $data->{'completion' . $suffix};
181
                $autocompletion = !empty($completion) && $completion == COMPLETION_TRACKING_AUTOMATIC;
182
                if (!$autocompletion || empty($data->{'completionsubmit' . $suffix})) {
183
                    $data->{'completionsubmit' . $suffix} = 0;
184
                }
185
            }
186
        }
187
    }
188
 
189
    /**
190
     * Enforce validation rules here
191
     *
192
     * @param array $data array of ("fieldname"=>value) of submitted data
193
     * @param array $files array of uploaded files "element_name"=>tmp_file_path
194
     * @return array
195
     **/
196
    public function validation($data, $files) {
197
        $errors = parent::validation($data, $files);
198
 
199
        // Check open and close times are consistent.
200
        if ($data['timeopen'] && $data['timeclose'] &&
201
                $data['timeclose'] < $data['timeopen']) {
202
            $errors['timeclose'] = get_string('closebeforeopen', 'feedback');
203
        }
204
        return $errors;
205
    }
206
 
207
    public function add_completion_rules() {
208
        $mform =& $this->_form;
209
 
210
        $suffix = $this->get_suffix();
211
        $completionsubmitel = 'completionsubmit' . $suffix;
212
        $mform->addElement('checkbox',
213
            $completionsubmitel,
214
            '',
215
            get_string('completionsubmit', 'feedback')
216
        );
217
        // Enable this completion rule by default.
218
        $mform->setDefault($completionsubmitel, 1);
219
        return [$completionsubmitel];
220
    }
221
 
222
    public function completion_rule_enabled($data) {
223
        $suffix = $this->get_suffix();
224
        return !empty($data['completionsubmit' . $suffix]);
225
    }
226
}