Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
// This file is part of Moodle - http://moodle.org/
4
//
5
// Moodle is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
9
//
10
// Moodle is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
// GNU General Public License for more details.
14
//
15
// You should have received a copy of the GNU General Public License
16
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17
 
18
/**
19
 * @package   mod_forum
20
 * @copyright Jamie Pratt <me@jamiep.org>
21
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
if (!defined('MOODLE_INTERNAL')) {
25
    die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
26
}
27
 
28
require_once ($CFG->dirroot.'/course/moodleform_mod.php');
29
 
30
use core_grades\component_gradeitems;
31
 
32
class mod_forum_mod_form extends moodleform_mod {
33
 
34
    function definition() {
35
        global $CFG, $COURSE, $DB;
36
 
37
        $mform    =& $this->_form;
38
 
39
//-------------------------------------------------------------------------------
40
        $mform->addElement('header', 'general', get_string('general', 'form'));
41
 
42
        $mform->addElement('text', 'name', get_string('forumname', 'forum'), array('size'=>'64'));
43
        if (!empty($CFG->formatstringstriptags)) {
44
            $mform->setType('name', PARAM_TEXT);
45
        } else {
46
            $mform->setType('name', PARAM_CLEANHTML);
47
        }
48
        $mform->addRule('name', null, 'required', null, 'client');
49
        $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
50
 
51
        $this->standard_intro_elements(get_string('forumintro', 'forum'));
52
 
53
        $forumtypes = forum_get_forum_types();
54
        core_collator::asort($forumtypes, core_collator::SORT_STRING);
55
        $mform->addElement('select', 'type', get_string('forumtype', 'forum'), $forumtypes);
56
        $mform->addHelpButton('type', 'forumtype', 'forum');
57
        $mform->setDefault('type', 'general');
58
 
59
        $mform->addElement('header', 'availability', get_string('availability', 'forum'));
60
 
61
        $name = get_string('duedate', 'forum');
62
        $mform->addElement('date_time_selector', 'duedate', $name, array('optional' => true));
63
        $mform->addHelpButton('duedate', 'duedate', 'forum');
64
 
65
        $name = get_string('cutoffdate', 'forum');
66
        $mform->addElement('date_time_selector', 'cutoffdate', $name, array('optional' => true));
67
        $mform->addHelpButton('cutoffdate', 'cutoffdate', 'forum');
68
 
69
        // Attachments and word count.
70
        $mform->addElement('header', 'attachmentswordcounthdr', get_string('attachmentswordcount', 'forum'));
71
 
72
        $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes, 0, $CFG->forum_maxbytes);
73
        $choices[1] = get_string('uploadnotallowed');
74
        $mform->addElement('select', 'maxbytes', get_string('maxattachmentsize', 'forum'), $choices);
75
        $mform->addHelpButton('maxbytes', 'maxattachmentsize', 'forum');
76
        $mform->setDefault('maxbytes', $CFG->forum_maxbytes);
77
 
78
        $choices = array(
79
 
80
            1 => 1,
81
            2 => 2,
82
            3 => 3,
83
            4 => 4,
84
            5 => 5,
85
            6 => 6,
86
            7 => 7,
87
            8 => 8,
88
            9 => 9,
89
            10 => 10,
90
            20 => 20,
91
            50 => 50,
92
            100 => 100
93
        );
94
        $mform->addElement('select', 'maxattachments', get_string('maxattachments', 'forum'), $choices);
95
        $mform->addHelpButton('maxattachments', 'maxattachments', 'forum');
96
        $mform->setDefault('maxattachments', $CFG->forum_maxattachments);
97
 
98
        $mform->addElement('selectyesno', 'displaywordcount', get_string('displaywordcount', 'forum'));
99
        $mform->addHelpButton('displaywordcount', 'displaywordcount', 'forum');
100
        $mform->setDefault('displaywordcount', 0);
101
 
102
        // Subscription and tracking.
103
        $mform->addElement('header', 'subscriptionandtrackinghdr', get_string('subscriptionandtracking', 'forum'));
104
 
105
        $options = forum_get_subscriptionmode_options();
106
        $mform->addElement('select', 'forcesubscribe', get_string('subscriptionmode', 'forum'), $options);
107
        $mform->addHelpButton('forcesubscribe', 'subscriptionmode', 'forum');
108
        if (isset($CFG->forum_subscription)) {
109
            $defaultforumsubscription = $CFG->forum_subscription;
110
        } else {
111
            $defaultforumsubscription = FORUM_CHOOSESUBSCRIBE;
112
        }
113
        $mform->setDefault('forcesubscribe', $defaultforumsubscription);
114
 
115
        $options = array();
116
        $options[FORUM_TRACKING_OPTIONAL] = get_string('trackingoptional', 'forum');
117
        $options[FORUM_TRACKING_OFF] = get_string('trackingoff', 'forum');
118
        if ($CFG->forum_allowforcedreadtracking) {
119
            $options[FORUM_TRACKING_FORCED] = get_string('trackingon', 'forum');
120
        }
121
        $mform->addElement('select', 'trackingtype', get_string('trackingtype', 'forum'), $options);
122
        $mform->addHelpButton('trackingtype', 'trackingtype', 'forum');
123
        $default = $CFG->forum_trackingtype;
124
        if ((!$CFG->forum_allowforcedreadtracking) && ($default == FORUM_TRACKING_FORCED)) {
125
            $default = FORUM_TRACKING_OPTIONAL;
126
        }
127
        $mform->setDefault('trackingtype', $default);
128
 
129
        if ($CFG->enablerssfeeds && isset($CFG->forum_enablerssfeeds) && $CFG->forum_enablerssfeeds) {
130
//-------------------------------------------------------------------------------
131
            $mform->addElement('header', 'rssheader', get_string('rss'));
132
            $choices = array();
133
            $choices[0] = get_string('none');
134
            $choices[1] = get_string('discussions', 'forum');
135
            $choices[2] = get_string('posts', 'forum');
136
            $mform->addElement('select', 'rsstype', get_string('rsstype', 'forum'), $choices);
137
            $mform->addHelpButton('rsstype', 'rsstype', 'forum');
138
            if (isset($CFG->forum_rsstype)) {
139
                $mform->setDefault('rsstype', $CFG->forum_rsstype);
140
            }
141
 
142
            $choices = array();
143
            $choices[0] = '0';
144
            $choices[1] = '1';
145
            $choices[2] = '2';
146
            $choices[3] = '3';
147
            $choices[4] = '4';
148
            $choices[5] = '5';
149
            $choices[10] = '10';
150
            $choices[15] = '15';
151
            $choices[20] = '20';
152
            $choices[25] = '25';
153
            $choices[30] = '30';
154
            $choices[40] = '40';
155
            $choices[50] = '50';
156
            $mform->addElement('select', 'rssarticles', get_string('rssarticles'), $choices);
157
            $mform->addHelpButton('rssarticles', 'rssarticles', 'forum');
158
            $mform->hideIf('rssarticles', 'rsstype', 'eq', '0');
159
            if (isset($CFG->forum_rssarticles)) {
160
                $mform->setDefault('rssarticles', $CFG->forum_rssarticles);
161
            }
162
        }
163
 
164
        $mform->addElement('header', 'discussionlocking', get_string('discussionlockingheader', 'forum'));
165
        $options = [
166
 
167
            1   * DAYSECS   => get_string('numday', 'core', 1),
168
            1   * WEEKSECS  => get_string('numweek', 'core', 1),
169
            2   * WEEKSECS  => get_string('numweeks', 'core', 2),
170
            30  * DAYSECS   => get_string('nummonth', 'core', 1),
171
            60  * DAYSECS   => get_string('nummonths', 'core', 2),
172
            90  * DAYSECS   => get_string('nummonths', 'core', 3),
173
            180 * DAYSECS   => get_string('nummonths', 'core', 6),
174
            1   * YEARSECS  => get_string('numyear', 'core', 1),
175
        ];
176
        $mform->addElement('select', 'lockdiscussionafter', get_string('lockdiscussionafter', 'forum'), $options);
177
        $mform->addHelpButton('lockdiscussionafter', 'lockdiscussionafter', 'forum');
178
        $mform->disabledIf('lockdiscussionafter', 'type', 'eq', 'single');
179
 
180
//-------------------------------------------------------------------------------
181
        $mform->addElement('header', 'blockafterheader', get_string('blockafter', 'forum'));
182
        $options = array();
183
        $options[0] = get_string('blockperioddisabled','forum');
184
        $options[60*60*24]   = '1 '.get_string('day');
185
        $options[60*60*24*2] = '2 '.get_string('days');
186
        $options[60*60*24*3] = '3 '.get_string('days');
187
        $options[60*60*24*4] = '4 '.get_string('days');
188
        $options[60*60*24*5] = '5 '.get_string('days');
189
        $options[60*60*24*6] = '6 '.get_string('days');
190
        $options[60*60*24*7] = '1 '.get_string('week');
191
        $mform->addElement('select', 'blockperiod', get_string('blockperiod', 'forum'), $options);
192
        $mform->addHelpButton('blockperiod', 'blockperiod', 'forum');
193
 
194
        $mform->addElement('text', 'blockafter', get_string('blockafter', 'forum'));
195
        $mform->setType('blockafter', PARAM_INT);
196
        $mform->setDefault('blockafter', '0');
197
        $mform->addRule('blockafter', null, 'numeric', null, 'client');
198
        $mform->addHelpButton('blockafter', 'blockafter', 'forum');
199
        $mform->hideIf('blockafter', 'blockperiod', 'eq', 0);
200
 
201
        $mform->addElement('text', 'warnafter', get_string('warnafter', 'forum'));
202
        $mform->setType('warnafter', PARAM_INT);
203
        $mform->setDefault('warnafter', '0');
204
        $mform->addRule('warnafter', null, 'numeric', null, 'client');
205
        $mform->addHelpButton('warnafter', 'warnafter', 'forum');
206
        $mform->hideIf('warnafter', 'blockperiod', 'eq', 0);
207
 
208
//-------------------------------------------------------------------------------
209
 
210
        // Add the whole forum grading options.
211
        $this->add_forum_grade_settings($mform, 'forum');
212
 
213
        $this->standard_coursemodule_elements();
214
//-------------------------------------------------------------------------------
215
// buttons
216
        $this->add_action_buttons();
217
    }
218
 
219
    /**
220
     * Add the whole forum grade settings to the mform.
221
     *
222
     * @param   \mform $mform
223
     * @param   string $itemname
224
     */
225
    private function add_forum_grade_settings($mform, string $itemname) {
226
        global $COURSE;
227
 
228
        $component = "mod_{$this->_modname}";
229
        $defaultgradingvalue = 0;
230
 
231
        $itemnumber = component_gradeitems::get_itemnumber_from_itemname($component, $itemname);
232
        $gradefieldname = component_gradeitems::get_field_name_for_itemnumber($component, $itemnumber, 'grade');
233
        $gradecatfieldname = component_gradeitems::get_field_name_for_itemnumber($component, $itemnumber, 'gradecat');
234
        $gradepassfieldname = component_gradeitems::get_field_name_for_itemnumber($component, $itemnumber, 'gradepass');
235
 
236
        // The advancedgradingmethod is different in that it is suffixed with an area name... which is not the
237
        // itemnumber.
238
        $methodfieldname = "advancedgradingmethod_{$itemname}";
239
 
240
        $headername = "{$gradefieldname}_header";
241
        $mform->addElement('header', $headername, get_string("grade_{$itemname}_header", $component));
242
 
243
        $isupdate = !empty($this->_cm);
244
        $gradeoptions = [
245
            'isupdate' => $isupdate,
246
            'currentgrade' => false,
247
            'hasgrades' => false,
248
            'canrescale' => false,
249
            'useratings' => false,
250
        ];
251
 
252
        if ($isupdate) {
253
            $gradeitem = grade_item::fetch([
254
                'itemtype' => 'mod',
255
                'itemmodule' => $this->_cm->modname,
256
                'iteminstance' => $this->_cm->instance,
257
                'itemnumber' => $itemnumber,
258
                'courseid' => $COURSE->id,
259
            ]);
260
            if ($gradeitem) {
261
                $gradeoptions['currentgrade'] = $gradeitem->grademax;
262
                $gradeoptions['currentgradetype'] = $gradeitem->gradetype;
263
                $gradeoptions['currentscaleid'] = $gradeitem->scaleid;
264
                $gradeoptions['hasgrades'] = $gradeitem->has_grades();
265
            }
266
        }
267
        $mform->addElement(
268
            'modgrade',
269
            $gradefieldname,
270
            get_string("{$gradefieldname}_title", $component),
271
            $gradeoptions
272
        );
273
        $mform->addHelpButton($gradefieldname, 'modgrade', 'grades');
274
        $mform->setDefault($gradefieldname, $defaultgradingvalue);
275
 
276
        if (!empty($this->current->_advancedgradingdata['methods']) && !empty($this->current->_advancedgradingdata['areas'])) {
277
            $areadata = $this->current->_advancedgradingdata['areas'][$itemname];
278
            $mform->addElement(
279
                'select',
280
                $methodfieldname,
281
                get_string('gradingmethod', 'core_grading'),
282
                $this->current->_advancedgradingdata['methods']
283
            );
284
            $mform->addHelpButton($methodfieldname, 'gradingmethod', 'core_grading');
285
            $mform->hideIf($methodfieldname, "{$gradefieldname}[modgrade_type]", 'eq', 'none');
286
        }
287
 
288
        // Grade category.
289
        $mform->addElement(
290
            'select',
291
            $gradecatfieldname,
292
            get_string('gradecategoryonmodform', 'grades'),
293
            grade_get_categories_menu($COURSE->id, $this->_outcomesused)
294
        );
295
        $mform->addHelpButton($gradecatfieldname, 'gradecategoryonmodform', 'grades');
296
        $mform->hideIf($gradecatfieldname, "{$gradefieldname}[modgrade_type]", 'eq', 'none');
297
 
298
        // Grade to pass.
299
        $mform->addElement('text', $gradepassfieldname, get_string('gradepass', 'grades'));
300
        $mform->addHelpButton($gradepassfieldname, 'gradepass', 'grades');
301
        $mform->setDefault($gradepassfieldname, '');
302
        $mform->setType($gradepassfieldname, PARAM_RAW);
303
        $mform->hideIf($gradepassfieldname, "{$gradefieldname}[modgrade_type]", 'eq', 'none');
304
 
305
        $mform->addElement('selectyesno', 'grade_forum_notify', get_string('sendstudentnotificationsdefault', 'forum'));
306
        $mform->addHelpButton('grade_forum_notify', 'sendstudentnotificationsdefault', 'forum');
307
        $mform->hideIf('grade_forum_notify', "{$gradefieldname}[modgrade_type]", 'eq', 'none');
308
    }
309
 
310
    function definition_after_data() {
311
        parent::definition_after_data();
312
        $mform     =& $this->_form;
313
        $type      =& $mform->getElement('type');
314
        $typevalue = $mform->getElementValue('type');
315
 
316
        //we don't want to have these appear as possible selections in the form but
317
        //we want the form to display them if they are set.
318
        if ($typevalue[0]=='news') {
319
            $type->addOption(get_string('namenews', 'forum'), 'news');
320
            $mform->addHelpButton('type', 'namenews', 'forum');
321
            $type->freeze();
322
            $type->setPersistantFreeze(true);
323
        }
324
        if ($typevalue[0]=='social') {
325
            $type->addOption(get_string('namesocial', 'forum'), 'social');
326
            $type->freeze();
327
            $type->setPersistantFreeze(true);
328
        }
329
    }
330
 
331
    public function validation($data, $files) {
332
        $errors = parent::validation($data, $files);
333
 
334
        if ($data['type'] === 'single' && $data['groupmode'] == SEPARATEGROUPS) {
335
            $errors['type'] = get_string('cannotusesingletopicandseperategroups', 'forum');
336
            $errors['groupmode'] = get_string('cannotuseseperategroupsandsingletopic', 'forum');
337
        }
338
 
339
        if ($data['duedate'] && $data['cutoffdate']) {
340
            if ($data['duedate'] > $data['cutoffdate']) {
341
                $errors['cutoffdate'] = get_string('cutoffdatevalidation', 'forum');
342
            }
343
        }
344
 
345
        $this->validation_forum_grade($data, $files, $errors);
346
 
347
        return $errors;
348
    }
349
 
350
    /**
351
     * Handle definition after data for grade settings.
352
     *
353
     * @param array $data
354
     * @param array $files
355
     * @param array $errors
356
     */
357
    private function validation_forum_grade(array $data, array $files, array $errors) {
358
        global $COURSE;
359
 
360
        $mform =& $this->_form;
361
 
362
        $component = "mod_forum";
363
        $itemname = 'forum';
364
        $itemnumber = component_gradeitems::get_itemnumber_from_itemname($component, $itemname);
365
        $gradefieldname = component_gradeitems::get_field_name_for_itemnumber($component, $itemnumber, 'grade');
366
        $gradepassfieldname = component_gradeitems::get_field_name_for_itemnumber($component, $itemnumber, 'grade');
367
 
368
        $gradeitem = grade_item::fetch([
369
            'itemtype' => 'mod',
370
            'itemmodule' => $data['modulename'],
371
            'iteminstance' => $data['instance'],
372
            'itemnumber' => $itemnumber,
373
            'courseid' => $COURSE->id,
374
        ]);
375
 
376
        if ($mform->elementExists('cmidnumber') && $this->_cm) {
377
            if (!grade_verify_idnumber($data['cmidnumber'], $COURSE->id, $gradeitem, $this->_cm)) {
378
                $errors['cmidnumber'] = get_string('idnumbertaken');
379
            }
380
        }
381
 
382
        // Check that the grade pass is a valid number.
383
        $gradepassvalid = false;
384
        if (isset($data[$gradepassfieldname])) {
385
            if (unformat_float($data[$gradepassfieldname], true) === false) {
386
                $errors[$gradepassfieldname] = get_string('err_numeric', 'form');
387
            } else {
388
                $gradepassvalid = true;
389
            }
390
        }
391
 
392
        // Grade to pass: ensure that the grade to pass is valid for points and scales.
393
        // If we are working with a scale, convert into a positive number for validation.
394
        if ($gradepassvalid && isset($data[$gradepassfieldname]) && (!empty($data[$gradefieldname]))) {
395
            $grade = $data[$gradefieldname];
396
            if (unformat_float($data[$gradepassfieldname]) > $grade) {
397
                $errors[$gradepassfieldname] = get_string('gradepassgreaterthangrade', 'grades', $grade);
398
            }
399
        }
400
    }
401
 
402
    public function data_preprocessing(&$defaultvalues) {
403
        parent::data_preprocessing($defaultvalues);
404
 
405
        $suffix = $this->get_suffix();
406
        $completiondiscussionsenabledel = 'completiondiscussionsenabled' . $suffix;
407
        $completiondiscussionsel = 'completiondiscussions' . $suffix;
408
        $completionrepliesenabledel = 'completionrepliesenabled' . $suffix;
409
        $completionrepliesel = 'completionreplies' . $suffix;
410
        $completionpostsel = 'completionposts' . $suffix;
411
        $completionpostsenabledel = 'completionpostsenabled' . $suffix;
412
 
413
        // Set up the completion checkboxes which aren't part of standard data.
414
        // We also make the default value (if you turn on the checkbox) for those
415
        // numbers to be 1, this will not apply unless checkbox is ticked.
416
        $defaultvalues[$completiondiscussionsenabledel] = !empty($defaultvalues[$completiondiscussionsel]) ? 1 : 0;
417
        if (empty($defaultvalues[$completiondiscussionsel])) {
418
            $defaultvalues[$completiondiscussionsel] = 1;
419
        }
420
        $defaultvalues[$completionrepliesenabledel] = !empty($defaultvalues[$completionrepliesel]) ? 1 : 0;
421
        if (empty($defaultvalues[$completionrepliesel])) {
422
            $defaultvalues[$completionrepliesel] = 1;
423
        }
424
        // Tick by default if Add mode or if completion posts settings is set to 1 or more.
425
        if (empty($this->_instance) || !empty($defaultvalues[$completionpostsel])) {
426
            $defaultvalues[$completionpostsenabledel] = 1;
427
        } else {
428
            $defaultvalues[$completionpostsenabledel] = 0;
429
        }
430
        if (empty($defaultvalues[$completionpostsel])) {
431
            $defaultvalues[$completionpostsel] = 1;
432
        }
433
    }
434
 
435
    /**
436
     * Add custom completion rules.
437
     *
438
     * @return array Array of string IDs of added items, empty array if none
439
     */
440
    public function add_completion_rules() {
441
        $mform = $this->_form;
442
 
443
        $suffix = $this->get_suffix();
444
 
445
        $group = [];
446
        $completionpostsenabledel = 'completionpostsenabled' . $suffix;
447
        $group[] =& $mform->createElement('checkbox', $completionpostsenabledel, '', get_string('completionposts', 'forum'));
448
        $completionpostsel = 'completionposts' . $suffix;
449
        $group[] =& $mform->createElement('text', $completionpostsel, '', ['size' => 3]);
450
        $mform->setType($completionpostsel, PARAM_INT);
451
        $completionpostsgroupel = 'completionpostsgroup' . $suffix;
452
        $mform->addGroup($group, $completionpostsgroupel, '', ' ', false);
453
        $mform->hideIf($completionpostsel, $completionpostsenabledel, 'notchecked');
454
 
455
        $group = [];
456
        $completiondiscussionsenabledel = 'completiondiscussionsenabled' . $suffix;
457
        $group[] =& $mform->createElement(
458
            'checkbox',
459
            $completiondiscussionsenabledel,
460
            '',
461
            get_string('completiondiscussions', 'forum')
462
        );
463
        $completiondiscussionsel = 'completiondiscussions' . $suffix;
464
        $group[] =& $mform->createElement('text', $completiondiscussionsel, '', ['size' => 3]);
465
        $mform->setType($completiondiscussionsel, PARAM_INT);
466
        $completiondiscussionsgroupel = 'completiondiscussionsgroup' . $suffix;
467
        $mform->addGroup($group, $completiondiscussionsgroupel, '', ' ', false);
468
        $mform->hideIf($completiondiscussionsel, $completiondiscussionsenabledel, 'notchecked');
469
 
470
        $group = [];
471
        $completionrepliesenabledel = 'completionrepliesenabled' . $suffix;
472
        $group[] =& $mform->createElement('checkbox', $completionrepliesenabledel, '', get_string('completionreplies', 'forum'));
473
        $completionrepliesel = 'completionreplies' . $suffix;
474
        $group[] =& $mform->createElement('text', $completionrepliesel, '', ['size' => 3]);
475
        $mform->setType($completionrepliesel, PARAM_INT);
476
        $completionrepliesgroupel = 'completionrepliesgroup' . $suffix;
477
        $mform->addGroup($group, $completionrepliesgroupel, '', ' ', false);
478
        $mform->hideIf($completionrepliesel, $completionrepliesenabledel, 'notchecked');
479
 
480
        return [$completiondiscussionsgroupel, $completionrepliesgroupel, $completionpostsgroupel];
481
    }
482
 
483
    public function completion_rule_enabled($data) {
484
        $suffix = $this->get_suffix();
485
        return (!empty($data['completiondiscussionsenabled' . $suffix]) && $data['completiondiscussions' . $suffix] != 0) ||
486
            (!empty($data['completionrepliesenabled' . $suffix]) && $data['completionreplies' . $suffix] != 0) ||
487
            (!empty($data['completionpostsenabled' . $suffix]) && $data['completionposts' . $suffix] != 0);
488
    }
489
 
490
    /**
491
     * Return submitted data if properly submitted or returns NULL if validation fails or
492
     * if there is no submitted data.
493
     *
494
     * Do not override this method, override data_postprocessing() instead.
495
     *
496
     * @return object submitted data; NULL if not valid or not submitted or cancelled
497
     */
498
    public function get_data() {
499
        $data = parent::get_data();
500
        if ($data) {
501
            $itemname = 'forum';
502
            $component = 'mod_forum';
503
            $gradepassfieldname = component_gradeitems::get_field_name_for_itemname($component, $itemname, 'gradepass');
504
 
505
            // Convert the grade pass value - we may be using a language which uses commas,
506
            // rather than decimal points, in numbers. These need to be converted so that
507
            // they can be added to the DB.
508
            if (isset($data->{$gradepassfieldname})) {
509
                $data->{$gradepassfieldname} = unformat_float($data->{$gradepassfieldname});
510
            }
511
        }
512
 
513
        return $data;
514
    }
515
 
516
    /**
517
     * Allows module to modify the data returned by form get_data().
518
     * This method is also called in the bulk activity completion form.
519
     *
520
     * Only available on moodleform_mod.
521
     *
522
     * @param stdClass $data the form data to be modified.
523
     */
524
    public function data_postprocessing($data) {
525
        parent::data_postprocessing($data);
526
        // Turn off completion settings if the checkboxes aren't ticked.
527
        if (!empty($data->completionunlocked)) {
528
            $suffix = $this->get_suffix();
529
            $completion = $data->{'completion' . $suffix};
530
            $autocompletion = !empty($completion) && $completion == COMPLETION_TRACKING_AUTOMATIC;
531
            if (empty($data->{'completiondiscussionsenabled' . $suffix}) || !$autocompletion) {
532
                $data->{'completiondiscussions' . $suffix} = 0;
533
            }
534
            if (empty($data->{'completionrepliesenabled' . $suffix}) || !$autocompletion) {
535
                $data->{'completionreplies' . $suffix} = 0;
536
            }
537
            if (empty($data->{'completionpostsenabled' . $suffix}) || !$autocompletion) {
538
                $data->{'completionposts' . $suffix} = 0;
539
            }
540
        }
541
    }
542
}