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
 * This file contains the forms to create and edit an instance of this module
19
 *
20
 * @package    mod_reengagement
21
 * @author     Peter Bulmer <peter.bulmer@catlayst.net.nz>
22
 * @copyright  2016 Catalyst IT {@link http://www.catalyst.net.nz}
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
defined('MOODLE_INTERNAL') || die();
27
 
28
require_once($CFG->dirroot.'/course/moodleform_mod.php');
29
 
30
/**
31
 * Moodleform class.
32
 *
33
 * @package    mod_reengagement
34
 * @author     Peter Bulmer <peter.bulmer@catlayst.net.nz>
35
 * @copyright  2016 Catalyst IT {@link http://www.catalyst.net.nz}
36
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
38
class mod_reengagement_mod_form extends moodleform_mod {
39
 
40
    /**
41
     * Called to define this moodle form
42
     *
43
     * @return void
44
     */
45
    public function definition() {
46
 
47
        global $COURSE, $CFG;
48
        $mform =& $this->_form;
49
        // Make sure completion and restriction is enabled.
50
        if (empty($CFG->enablecompletion) || empty($CFG->enableavailability)) {
51
            throw new moodle_exception('mustenablecompletionavailability', 'mod_reengagement');
52
        }
53
        // Adding the "general" fieldset, where all the common settings are shown.
54
        $mform->addElement('header', 'general', get_string('general', 'form'));
55
        if (!$COURSE->enablecompletion) {
56
            $coursecontext = context_course::instance($COURSE->id);
57
            if (has_capability('moodle/course:update', $coursecontext)) {
58
                $mform->addElement('static', 'completionwillturnon', get_string('completion', 'reengagement'),
59
                                   get_string('completionwillturnon', 'reengagement'));
60
            }
61
        }
62
 
63
        $istotara = false;
64
        if (file_exists($CFG->dirroot.'/totara')) {
65
            $istotara = true;
66
        }
67
 
68
        // Adding the standard "name" field.
69
        $mform->addElement('text', 'name', get_string('reengagementname', 'reengagement'), array('size' => '64'));
70
        $mform->setType('name', PARAM_TEXT);
71
        $mform->addRule('name', null, 'required', null, 'client');
72
        $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
73
 
74
        // Adding the rest of reengagement settings, spreeading all them into this fieldset
75
        // or adding more fieldsets ('header' elements) if needed for better logic.
76
        $mform->addElement('header', 'reengagementfieldset', get_string('reengagementfieldset', 'reengagement'));
77
        $mform->setExpanded('reengagementfieldset', true);
78
 
79
        // Adding email detail fields.
80
        $emailuseroptions = array(); // The sorts of emailing this module might do.
81
        $emailuseroptions[REENGAGEMENT_EMAILUSER_NEVER] = get_string('never', 'reengagement');
82
        $emailuseroptions[REENGAGEMENT_EMAILUSER_COMPLETION] = get_string('oncompletion', 'reengagement');
83
        $emailuseroptions[REENGAGEMENT_EMAILUSER_TIME] = get_string('afterdelay', 'reengagement');
84
 
85
        $mform->addElement('select', 'emailuser', get_string('emailuser', 'reengagement'), $emailuseroptions);
86
        $mform->addHelpButton('emailuser', 'emailuser', 'reengagement');
87
 
88
        if ($istotara) {
89
            // Add options to control who any notifications should go to.
90
            $emailrecipientoptions = array(); // The message recipient options.
91
            $emailrecipientoptions[REENGAGEMENT_RECIPIENT_USER] = get_string('user');
92
            $emailrecipientoptions[REENGAGEMENT_RECIPIENT_MANAGER] = get_string('manager', 'role');
93
            $emailrecipientoptions[REENGAGEMENT_RECIPIENT_BOTH] = get_string('userandmanager', 'reengagement');
94
 
95
            $mform->addElement('select', 'emailrecipient', get_string('emailrecipient', 'reengagement'), $emailrecipientoptions);
96
            $mform->addHelpButton('emailrecipient', 'emailrecipient', 'reengagement');
97
        } else {
98
            $mform->addElement('hidden', 'emailrecipient', REENGAGEMENT_RECIPIENT_USER);
99
            $mform->setType('emailrecipient', PARAM_INT);
100
        }
101
 
102
        $mform->addElement('text', 'thirdpartyemails', get_string('thirdpartyemails', 'reengagement'), array('size' => '80'));
103
        $mform->addHelpButton('thirdpartyemails', 'thirdpartyemails', 'reengagement');
104
        $mform->setType('thirdpartyemails', PARAM_TEXT);
105
 
106
        // Add a group of controls to specify after how long an email should be sent.
107
        $emaildelay = array();
108
        $periods = array();
109
        $periods[60] = get_string('minutes', 'reengagement');
110
        $periods[3600] = get_string('hours', 'reengagement');
111
        $periods[86400] = get_string('days', 'reengagement');
112
        $periods[604800] = get_string('weeks', 'reengagement');
113
        $emaildelay[] = $mform->createElement('text', 'emailperiodcount', '', array('class="emailperiodcount"'));
114
        $emaildelay[] = $mform->createElement('select', 'emailperiod', '', $periods);
115
        $mform->addGroup($emaildelay, 'emaildelay', get_string('emaildelay', 'reengagement'), array(' '), false);
116
        $mform->addHelpButton('emaildelay', 'emaildelay', 'reengagement');
117
        $mform->setType('emailperiodcount', PARAM_INT);
118
        $mform->setDefault('emailperiodcount', '1');
119
        $mform->setDefault('emailperiod', '604800');
120
        $mform->hideif('emaildelay', 'emailuser', 'neq', REENGAGEMENT_EMAILUSER_TIME);
121
 
122
        // Add frequency of e-mails.
123
        $mform->addElement('text', 'remindercount', get_string('remindercount', 'reengagement'), array('maxlength' => '2'));
124
        $mform->setType('remindercount', PARAM_INT);
125
        $mform->setDefault('remindercount', '1');
126
        $mform->addRule('remindercount', get_string('err_numeric', 'form'), 'numeric', '', 'client');
127
        $mform->addHelpButton('remindercount', 'remindercount', 'reengagement');
128
        $mform->hideif('remindercount', 'emailuser', 'neq', REENGAGEMENT_EMAILUSER_TIME);
129
 
130
        $mform->addElement('text', 'emailsubject', get_string('emailsubject', 'reengagement'), array('size' => '64'));
131
        $mform->setType('emailsubject', PARAM_TEXT);
132
        $mform->addRule('emailsubject', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
133
        $mform->hideif('emailsubject', 'emailuser', 'eq', REENGAGEMENT_EMAILUSER_NEVER);
134
        $mform->addHelpButton('emailsubject', 'emailsubject', 'reengagement');
135
        $mform->addElement('editor', 'emailcontent', get_string('emailcontent', 'reengagement'), null, null);
136
        $mform->setDefault('emailcontent', get_string('emailcontentdefaultvalue', 'reengagement'));
137
        $mform->setType('emailcontent', PARAM_RAW);
138
        $mform->hideif('emailcontent', 'emailuser', 'eq', REENGAGEMENT_EMAILUSER_NEVER);
139
 
140
        if ($istotara) {
141
            $mform->addElement('text', 'emailsubjectmanager', get_string('emailsubjectmanager', 'reengagement'),
142
                               array('size' => '64'));
143
            $mform->setType('emailsubjectmanager', PARAM_TEXT);
144
            $mform->addRule('emailsubjectmanager', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
145
            $mform->hideif('emailsubjectmanager', 'emailuser', 'eq', REENGAGEMENT_EMAILUSER_NEVER);
146
            $mform->addHelpButton('emailsubjectmanager', 'emailsubjectmanager', 'reengagement');
147
            $mform->addElement('editor', 'emailcontentmanager', get_string('emailcontentmanager', 'reengagement'), null, null);
148
            $mform->setDefault('emailcontentmanager', get_string('emailcontentmanagerdefaultvalue', 'reengagement'));
149
            $mform->setType('emailcontentmanager', PARAM_RAW);
150
            $mform->addHelpButton('emailcontentmanager', 'emailcontentmanager', 'reengagement');
151
        } else {
152
            $mform->addElement('hidden', 'emailsubjectmanager');
153
            $mform->setType('emailsubjectmanager', PARAM_ALPHA);
154
            $mform->addElement('hidden', 'emailcontentmanager');
155
            $mform->setType('emailcontentmanager', PARAM_ALPHA);
156
        }
157
 
158
        $mform->addElement('text', 'emailsubjectthirdparty',
159
            get_string('emailsubjectthirdparty', 'reengagement'), array('size' => '64'));
160
        $mform->setType('emailsubjectthirdparty', PARAM_TEXT);
161
        $mform->addRule('emailsubjectthirdparty', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
162
        $mform->addHelpButton('emailsubjectthirdparty', 'emailsubjectthirdparty', 'reengagement');
163
        $mform->addElement('editor', 'emailcontentthirdparty', get_string('emailcontentthirdparty', 'reengagement'), null, null);
164
        $mform->setDefault('emailcontentthirdparty', get_string('emailcontentthirdpartydefaultvalue', 'reengagement'));
165
        $mform->setType('emailcontentthirdparty', PARAM_CLEANHTML);
166
        $mform->addHelpButton('emailcontentthirdparty', 'emailcontentthirdparty', 'reengagement');
167
 
168
        $mform->addElement('advcheckbox', 'suppressemail', get_string('suppressemail', 'reengagement'));
169
        $mform->hideif('suppressemail', 'emailuser', 'eq', REENGAGEMENT_EMAILUSER_NEVER);
170
        $mform->addHelpbutton('suppressemail', 'suppressemail', 'reengagement');
171
        $truemods = get_fast_modinfo($COURSE->id);
172
        $mods = array();
173
        $mods[0] = get_string('nosuppresstarget', 'reengagement');
174
        foreach ($truemods->cms as $mod) {
175
            $mods[$mod->id] = $mod->name;
176
        }
177
        $mform->addElement('select', 'suppresstarget', get_string('suppresstarget', 'reengagement'), $mods);
178
        $mform->hideif('suppresstarget', 'emailuser', 'eq', REENGAGEMENT_EMAILUSER_NEVER);
179
        $mform->hideif('suppresstarget', 'suppressemail', 'notchecked');
180
        $mform->addHelpbutton('suppresstarget', 'suppresstarget', 'reengagement');
181
 
182
        // Add standard elements, common to all modules.
183
        $this->standard_coursemodule_elements();
184
        if ($mform->elementExists('completion')) {
185
            $mform->setDefault('completion', COMPLETION_TRACKING_AUTOMATIC);
186
            $mform->freeze('completion');
187
        }
188
        // Hide some elements not relevant to this activity (student visibility).
189
        if ($mform->elementExists('visible')) {
190
            $mform->removeElement('visible');
191
            $mform->addElement('hidden', 'visible', 0);
192
            $mform->setType('visible', PARAM_INT);
193
            if ($mform->elementExists('visibleoncoursepage')) {
194
                $mform->removeElement('visibleoncoursepage');
195
            }
196
            $mform->addElement('hidden', 'visibleoncoursepage', 1);
197
            $mform->setType('visibleoncoursepage', PARAM_INT);
198
        }
199
 
200
        // Add standard buttons, common to all modules.
201
        $this->add_action_buttons();
202
 
203
    }
204
 
205
    /**
206
     * Load in existing data as form defaults
207
     *
208
     * @param stdClass|array $toform object or array of default values
209
     */
210
    public function set_data($toform) {
211
        global $CFG;
212
        $istotara = false;
213
        if (file_exists($CFG->dirroot.'/totara')) {
214
            $istotara = true;
215
        }
216
        // Form expects durations as a number of periods eg 5 minutes.
217
        // Process dbtime (seconds) into form-appropraite times.
218
        if (!empty($toform->duration)) {
219
            list ($periodcount, $period) = reengagement_get_readable_duration($toform->duration);
220
            $toform->period = $period;
221
            $toform->periodcount = $periodcount;
222
            unset($toform->duration);
223
        }
224
        if (!empty($toform->emaildelay)) {
225
            list ($periodcount, $period) = reengagement_get_readable_duration($toform->emaildelay);
226
            $toform->emailperiod = $period;
227
            $toform->emailperiodcount = $periodcount;
228
            unset($toform->emaildelay);
229
        }
230
        if (!isset($toform->emailcontent)) {
231
            $toform->emailcontent = get_string('emailcontentdefaultvalue', 'reengagement');
232
        }
233
        if (!isset($toform->emailcontentformat)) {
234
            $toform->emailcontentformat = 1;
235
        }
236
        $toform->emailcontent = array('text' => $toform->emailcontent, 'format' => $toform->emailcontentformat);
237
        if ($istotara) {
238
            if (!isset($toform->emailcontentmanager)) {
239
                $toform->emailcontentmanager = get_string('emailcontentmanagerdefaultvalue', 'reengagement');
240
            }
241
            if (!isset($toform->emailcontentmanagerformat)) {
242
                $toform->emailcontentmanagerformat = 1;
243
            }
244
            $toform->emailcontentmanager = array('text' => $toform->emailcontentmanager,
245
                'format' => $toform->emailcontentmanagerformat);
246
        }
247
 
248
        if (!isset($toform->emailcontentthirdparty)) {
249
            $toform->emailcontentthirdparty = get_string('emailcontentthirdpartydefaultvalue', 'reengagement');
250
        }
251
        if (!isset($toform->emailcontentthirdpartyformat)) {
252
            $toform->emailcontentthirdpartyformat = 1;
253
        }
254
        $toform->emailcontentthirdparty = array('text' => $toform->emailcontentthirdparty,
255
                                                'format' => $toform->emailcontentthirdpartyformat);
256
 
257
        if (empty($toform->suppresstarget)) {
258
            // There is no target activity specified.
259
            // Configure the box to have this dropdown disabled by default.
260
            $toform->suppressemail = 0;
261
        } else {
262
            // There is a target activity specified, enable the target selector so that the user can change it if desired.
263
            $toform->suppressemail = 1;
264
        }
265
        // Force completion tracking to automatic.
266
        $toform->completion = COMPLETION_TRACKING_AUTOMATIC;
267
        // Force activity to hidden.
268
        $toform->visible = 0;
269
 
270
        $result = parent::set_data($toform);
271
        return $result;
272
    }
273
 
274
    /**
275
     * Return the data that will be used upon saving.
276
     *
277
     * @return null|array
278
     */
279
    public function get_data() {
280
        global $CFG;
281
        $istotara = false;
282
        if (file_exists($CFG->dirroot.'/totara')) {
283
            $istotara = true;
284
        }
285
        $fromform = parent::get_data();
286
        if (!empty($fromform)) {
287
            // Force completion tracking to automatic.
288
            $fromform->completion = COMPLETION_TRACKING_AUTOMATIC;
289
            // Force activity to hidden.
290
            $fromform->visible = 0;
291
            if (!empty($fromform->completionunlocked)) {
292
                // Format, regulate module duration.
293
                if (isset($fromform->period) && isset($fromform->periodcount)) {
294
                    $fromform->duration = $fromform->period * $fromform->periodcount;
295
                }
296
                if (empty($fromform->duration)) {
297
                    $fromform->duration = 300;
298
                }
299
                if ($fromform->duration < 0) {
300
                    $fromform->duration = 0;
301
                }
302
                unset($fromform->period);
303
                unset($fromform->periodcount);
304
            }
305
            // Format, regulate email notification delay.
306
            if (isset($fromform->emailperiod) && isset($fromform->emailperiodcount)) {
307
                $fromform->emaildelay = $fromform->emailperiod * $fromform->emailperiodcount;
308
            }
309
            if (empty($fromform->emaildelay)) {
310
                $fromform->emaildelay = 300;
311
            }
312
            if ($fromform->emaildelay < 0) {
313
                $fromform->emaildelay = 0;
314
            }
315
            unset($fromform->emailperiod);
316
            unset($fromform->emailperiodcount);
317
            // Some special handling for the wysiwyg editor field.
318
            $fromform->emailcontentformat = $fromform->emailcontent['format'];
319
            $fromform->emailcontent = $fromform->emailcontent['text'];
320
            if ($istotara) {
321
                $fromform->emailcontentmanagerformat = $fromform->emailcontentmanager['format'];
322
                $fromform->emailcontentmanager = $fromform->emailcontentmanager['text'];
323
            }
324
            $fromform->emailcontentthirdpartyformat = $fromform->emailcontentthirdparty['format'];
325
            $fromform->emailcontentthirdparty = $fromform->emailcontentthirdparty['text'];
326
        }
327
        return $fromform;
328
    }
329
    /**
330
     *  Add custom completion rules for reengagement.
331
     *
332
     * @return array Array of string IDs of added items, empty array if none
333
     */
334
    public function add_completion_rules() {
335
        $mform =& $this->_form;
336
        $periods = array();
337
        $periods[1] = get_string('seconds', 'reengagement');
338
        $periods[MINSECS] = get_string('minutes', 'reengagement');
339
        $periods[HOURSECS] = get_string('hours', 'reengagement');
340
        $periods[DAYSECS] = get_string('days', 'reengagement');
341
        $periods[WEEKSECS] = get_string('weeks', 'reengagement');
342
        $duration[] = &$mform->createElement('text', 'periodcount', '', array('class="periodcount"'));
343
        $mform->setType('periodcount', PARAM_INT);
344
        $duration[] = &$mform->createElement('select', 'period', '', $periods);
345
        $mform->addGroup($duration, 'duration', get_string('reengagementduration', 'reengagement'), array(' '), false);
346
        $mform->addHelpButton('duration', 'duration', 'reengagement');
347
        $mform->setDefault('periodcount', '1');
348
        $mform->setDefault('period', '604800');
349
        return array('duration');
350
    }
351
 
352
    /**
353
     * A custom completion rule is enabled by reengagement.
354
     *
355
     * @param array $data Input data (not yet validated)
356
     * @return bool True if one or more rules is enabled, false if none are;
357
     *   default returns false
358
     */
359
    public function completion_rule_enabled($data) {
360
        return true;
361
    }
362
 
363
    /**
364
     * Perform validation on the settings form
365
     * @param array $data
366
     * @param array $files
367
     */
368
    public function validation($data, $files) {
369
        $errors = parent::validation($data, $files);
370
        if (!empty($data['emailperiod'])) {
371
            $duration = $data['emailperiod'] * $data['emailperiodcount'];
372
 
373
            if ($duration < (DAYSECS) && $data['remindercount'] > 2) {
374
                // If less than 24hrs, make sure only 2 e-mails can be sent.
375
                $errors['remindercount'] = get_string('frequencytoohigh', 'reengagement', 2);
376
            } else if ($duration < (5 * DAYSECS) && $data['remindercount'] > 10) {
377
                // If less than 5 days, make sure only 10 e-mails can be sent.
378
                $errors['remindercount'] = get_string('frequencytoohigh', 'reengagement', 10);
379
            } else if ($duration < (15 * DAYSECS) && $data['remindercount'] > 26) {
380
                // If less than 15 days, make sure only 26 e-mails can be sent.
381
                $errors['remindercount'] = get_string('frequencytoohigh', 'reengagement', 26);
382
            } else if ($data['remindercount'] > 40) {
383
                // Maximum number of reminders is set to 40 - we don't want to be emailing users for several years.
384
                $errors['remindercount'] = get_string('frequencytoohigh', 'reengagement', 40);
385
            }
386
        }
387
 
388
        if ($data['emailperiod'] * $data['emailperiodcount'] < 300) {
389
            $errors['emaildelay'] = get_string('periodtoolow', 'reengagement');
390
        }
391
 
392
        return $errors;
393
    }
394
}