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
 * File containing the form definition to post in the forum.
19
 *
20
 * @package   mod_forum
21
 * @copyright Jamie Pratt <me@jamiep.org>
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
defined('MOODLE_INTERNAL') || die();
26
require_once($CFG->libdir . '/formslib.php');
27
require_once($CFG->dirroot . '/repository/lib.php');
28
 
29
/**
30
 * Class to post in a forum.
31
 *
32
 * @package   mod_forum
33
 * @copyright Jamie Pratt <me@jamiep.org>
34
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
class mod_forum_post_form extends moodleform {
37
 
38
    /**
39
     * Returns the options array to use in filemanager for forum attachments
40
     *
41
     * @param stdClass $forum
42
     * @return array
43
     */
44
    public static function attachment_options($forum) {
45
        global $COURSE, $PAGE, $CFG;
46
        $maxbytes = get_user_max_upload_file_size($PAGE->context, $CFG->maxbytes, $COURSE->maxbytes, $forum->maxbytes);
47
        return array(
48
            'subdirs' => 0,
49
            'maxbytes' => $maxbytes,
50
            'maxfiles' => $forum->maxattachments,
51
            'accepted_types' => '*',
52
            'return_types' => FILE_INTERNAL | FILE_CONTROLLED_LINK
53
        );
54
    }
55
 
56
    /**
57
     * Returns the options array to use in forum text editor
58
     *
59
     * @param context_module $context
60
     * @param int $postid post id, use null when adding new post
61
     * @return array
62
     */
63
    public static function editor_options(context_module $context, $postid) {
64
        global $COURSE, $PAGE, $CFG;
65
        // TODO: add max files and max size support
66
        $maxbytes = get_user_max_upload_file_size($PAGE->context, $CFG->maxbytes, $COURSE->maxbytes);
67
        return array(
68
            'maxfiles' => EDITOR_UNLIMITED_FILES,
69
            'maxbytes' => $maxbytes,
70
            'trusttext'=> true,
71
            'return_types'=> FILE_INTERNAL | FILE_EXTERNAL,
72
            'subdirs' => file_area_contains_subdirs($context, 'mod_forum', 'post', $postid)
73
        );
74
    }
75
 
76
    /**
77
     * Form definition
78
     *
79
     * @return void
80
     */
81
    function definition() {
82
        global $CFG, $OUTPUT;
83
 
84
        $mform =& $this->_form;
85
 
86
        $coursecontext = $this->_customdata['coursecontext'];
87
        $modcontext = $this->_customdata['modcontext'];
88
        $forum = $this->_customdata['forum'];
89
        $post = $this->_customdata['post'];
90
        $subscribe = $this->_customdata['subscribe'];
91
        $edit = $this->_customdata['edit'];
92
        $thresholdwarning = $this->_customdata['thresholdwarning'];
93
        $canreplyprivately = array_key_exists('canreplyprivately', $this->_customdata) ?
94
            $this->_customdata['canreplyprivately'] : false;
95
        $inpagereply = $this->_customdata['inpagereply'] ?? false;
96
 
97
        if (!$inpagereply) {
98
            // Fill in the data depending on page params later using set_data.
99
            $mform->addElement('header', 'general', '');
100
        }
101
 
102
        // If there is a warning message and we are not editing a post we need to handle the warning.
103
        if (!empty($thresholdwarning) && !$edit) {
104
            // Here we want to display a warning if they can still post but have reached the warning threshold.
105
            if ($thresholdwarning->canpost) {
106
                $message = get_string($thresholdwarning->errorcode, $thresholdwarning->module, $thresholdwarning->additional);
107
                $mform->addElement('html', $OUTPUT->notification($message, \core\output\notification::NOTIFY_INFO));
108
            }
109
        }
110
 
111
        $mform->addElement('text', 'subject', get_string('subject', 'forum'), 'size="48"');
112
        $mform->setType('subject', PARAM_TEXT);
113
        $mform->addRule('subject', get_string('required'), 'required', null, 'client');
114
        $mform->addRule('subject', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
115
 
116
        $mform->addElement('editor', 'message', get_string('message', 'forum'), null, self::editor_options($modcontext, (empty($post->id) ? null : $post->id)));
117
        $mform->setType('message', PARAM_RAW);
118
        $mform->addRule('message', get_string('required'), 'required', null, 'client');
119
 
120
        if ($inpagereply) {
121
            $this->add_group_elements_if_applicable($mform);
122
        } else {
123
            $manageactivities = has_capability('moodle/course:manageactivities', $coursecontext);
124
 
125
            if (\mod_forum\subscriptions::is_forcesubscribed($forum)) {
126
                $mform->addElement('checkbox', 'discussionsubscribe', get_string('discussionsubscription', 'forum'));
127
                $mform->freeze('discussionsubscribe');
128
                $mform->setDefaults('discussionsubscribe', 0);
129
                $mform->addHelpButton('discussionsubscribe', 'forcesubscribed', 'forum');
130
 
131
            } else if (\mod_forum\subscriptions::subscription_disabled($forum) && !$manageactivities) {
132
                $mform->addElement('checkbox', 'discussionsubscribe', get_string('discussionsubscription', 'forum'));
133
                $mform->freeze('discussionsubscribe');
134
                $mform->setDefaults('discussionsubscribe', 0);
135
                $mform->addHelpButton('discussionsubscribe', 'disallowsubscription', 'forum');
136
 
137
            } else {
138
                $mform->addElement('checkbox', 'discussionsubscribe', get_string('discussionsubscription', 'forum'));
139
                $mform->addHelpButton('discussionsubscribe', 'discussionsubscription', 'forum');
140
            }
141
 
142
            if (forum_can_create_attachment($forum, $modcontext)) {
143
                $mform->addElement('filemanager', 'attachments', get_string('attachment', 'forum'), null,
144
                    self::attachment_options($forum));
145
                $mform->addHelpButton('attachments', 'attachment', 'forum');
146
            }
147
 
148
            if (!$post->parent && has_capability('mod/forum:pindiscussions', $modcontext)) {
149
                $mform->addElement('checkbox', 'pinned', get_string('discussionpinned', 'forum'));
150
                $mform->addHelpButton('pinned', 'discussionpinned', 'forum');
151
            }
152
 
153
            if (empty($post->id) && ($manageactivities ||
154
                    ($forum->type == 'qanda' && has_capability('mod/forum:canmailnow', $modcontext)))
155
            ) {
156
                $mform->addElement('checkbox', 'mailnow', get_string('mailnow', 'forum'));
157
            }
158
 
159
            if ((empty($post->id) && $canreplyprivately) || (!empty($post) && !empty($post->privatereplyto))) {
160
                // Only show the option to change private reply settings if this is a new post and the user can reply
161
                // privately, or if this is already private reply, in which case the state is shown but is not editable.
162
                $mform->addElement('checkbox', 'isprivatereply', get_string('privatereply', 'forum'));
163
                $mform->addHelpButton('isprivatereply', 'privatereply', 'forum');
164
                if (!empty($post->privatereplyto)) {
165
                    $mform->setDefault('isprivatereply', 1);
166
                    $mform->freeze('isprivatereply');
167
                }
168
            }
169
 
170
            $this->add_group_elements_if_applicable($mform);
171
 
172
            if (!empty($CFG->forum_enabletimedposts) && !$post->parent &&
173
                has_capability('mod/forum:viewhiddentimedposts', $coursecontext)) {
174
                $mform->addElement('header', 'displayperiod', get_string('displayperiod', 'forum'));
175
 
176
                $mform->addElement('date_time_selector', 'timestart', get_string('displaystart', 'forum'),
177
                    array('optional' => true));
178
                $mform->addHelpButton('timestart', 'displaystart', 'forum');
179
 
180
                $mform->addElement('date_time_selector', 'timeend', get_string('displayend', 'forum'),
181
                    array('optional' => true));
182
                $mform->addHelpButton('timeend', 'displayend', 'forum');
183
 
184
            } else {
185
                $mform->addElement('hidden', 'timestart');
186
                $mform->setType('timestart', PARAM_INT);
187
                $mform->addElement('hidden', 'timeend');
188
                $mform->setType('timeend', PARAM_INT);
189
                $mform->setConstants(array('timestart' => 0, 'timeend' => 0));
190
            }
191
 
192
            if (core_tag_tag::is_enabled('mod_forum', 'forum_posts')) {
193
                $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
194
 
195
                $mform->addElement('tags', 'tags', get_string('tags'),
196
                    array('itemtype' => 'forum_posts', 'component' => 'mod_forum'));
197
            }
198
        }
199
 
200
        //-------------------------------------------------------------------------------
201
        // buttons
202
        if (isset($post->edit)) { // hack alert
203
            $submitstring = get_string('savechanges');
204
        } else {
205
            $submitstring = get_string('posttoforum', 'forum');
206
        }
207
 
208
        // Always register a no submit button so it can be picked up if redirecting to the original post form.
209
        $mform->registerNoSubmitButton('advancedadddiscussion');
210
 
211
        // This is an inpage add discussion which requires custom buttons.
212
        if ($inpagereply) {
213
            $mform->addElement('hidden', 'discussionsubscribe');
214
            $mform->setType('discussionsubscribe', PARAM_INT);
215
 
216
            $buttonarray = array();
217
            $buttonarray[] = &$mform->createElement('submit', 'submitbutton', $submitstring);
218
            $buttonarray[] = &$mform->createElement('button', 'cancelbtn',
219
                get_string('cancel', 'core'),
220
                // Additional attribs to handle collapsible div.
221
                ['data-toggle' => 'collapse', 'data-target' => "#collapseAddForm"]);
222
            $buttonarray[] = &$mform->createElement('submit', 'advancedadddiscussion',
223
                get_string('showadvancededitor'), null, null, ['customclassoverride' => 'btn-link']);
224
 
225
            $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
226
            $mform->closeHeaderBefore('buttonar');
227
        } else {
228
            $this->add_action_buttons(true, $submitstring);
229
        }
230
 
231
        $mform->addElement('hidden', 'course');
232
        $mform->setType('course', PARAM_INT);
233
 
234
        $mform->addElement('hidden', 'forum');
235
        $mform->setType('forum', PARAM_INT);
236
 
237
        $mform->addElement('hidden', 'discussion');
238
        $mform->setType('discussion', PARAM_INT);
239
 
240
        $mform->addElement('hidden', 'parent');
241
        $mform->setType('parent', PARAM_INT);
242
 
243
        $mform->addElement('hidden', 'groupid');
244
        $mform->setType('groupid', PARAM_INT);
245
 
246
        $mform->addElement('hidden', 'edit');
247
        $mform->setType('edit', PARAM_INT);
248
 
249
        $mform->addElement('hidden', 'reply');
250
        $mform->setType('reply', PARAM_INT);
251
    }
252
 
253
    /**
254
     * Form validation
255
     *
256
     * @param array $data data from the form.
257
     * @param array $files files uploaded.
258
     * @return array of errors.
259
     */
260
    function validation($data, $files) {
261
        $errors = parent::validation($data, $files);
262
        if (($data['timeend']!=0) && ($data['timestart']!=0) && $data['timeend'] <= $data['timestart']) {
263
            $errors['timeend'] = get_string('timestartenderror', 'forum');
264
        }
265
        if (empty($data['message']['text'])) {
266
            $errors['message'] = get_string('erroremptymessage', 'forum');
267
        }
268
        if (empty($data['subject'])) {
269
            $errors['subject'] = get_string('erroremptysubject', 'forum');
270
        }
271
        return $errors;
272
    }
273
 
274
    /**
275
     * Adds elements to control what groups will have access to the post or exits when no relevant group mode is set.
276
     *
277
     * @param MoodleQuickForm $mform
278
     */
279
    private function add_group_elements_if_applicable(MoodleQuickForm $mform): void {
280
        global $CFG;
281
 
282
        $course = $this->_customdata['course'];
283
        $cm = $this->_customdata['cm'];
284
        $forum = $this->_customdata['forum'];
285
        $modcontext = $this->_customdata['modcontext'];
286
        $post = $this->_customdata['post'];
287
 
288
        if (!groups_get_activity_groupmode($cm, $course)) {
289
            return;
290
        }
291
 
292
        $groupdata = groups_get_activity_allowed_groups($cm);
293
        $groupinfo = [];
294
        foreach ($groupdata as $groupid => $group) {
295
            // Check whether this user can post in this group.
296
            // We must make this check because all groups are returned for a visible grouped activity.
297
            if (forum_user_can_post_discussion($forum, $groupid, null, $cm, $modcontext)) {
298
                // Build the data for the groupinfo select.
299
                $groupinfo[$groupid] = format_string($group->name, true, ['context' => $modcontext]);
300
            } else {
301
                unset($groupdata[$groupid]);
302
            }
303
        }
304
        $groupcount = count($groupinfo);
305
 
306
        // Check whether a user can post to all of their own groups.
307
 
308
        // Posts to all of my groups are copied to each group that the user is a member of. Certain conditions must be met.
309
        // 1) It only makes sense to allow this when a user is in more than one group.
310
        // Note: This check must come before we consider adding accessallgroups, because that is not a real group.
311
        $canposttoowngroups = empty($post->edit) && $groupcount > 1;
312
 
313
        // 2) Important: You can *only* post to multiple groups for a top level post. Never any reply.
314
        $canposttoowngroups = $canposttoowngroups && empty($post->parent);
315
 
316
        // 3) You also need the canposttoowngroups capability.
317
        $canposttoowngroups = $canposttoowngroups && has_capability('mod/forum:canposttomygroups', $modcontext);
318
        if ($canposttoowngroups) {
319
            // This user is in multiple groups, and can post to all of their own groups.
320
            // Note: This is not the same as accessallgroups. This option will copy a post to all groups that a
321
            // user is a member of.
322
            $mform->addElement('checkbox', 'posttomygroups', get_string('posttomygroups', 'forum'));
323
            $mform->addHelpButton('posttomygroups', 'posttomygroups', 'forum');
324
            $mform->disabledIf('groupinfo', 'posttomygroups', 'checked');
325
        }
326
 
327
        // Check whether this user can post to all groups.
328
        // Posts to the 'All participants' group go to all groups, not to each group in a list.
329
        // It makes sense to allow this, even if there currently aren't any groups because there may be in the future.
330
        if (forum_user_can_post_discussion($forum, -1, null, $cm, $modcontext)) {
331
            // Note: We must reverse in this manner because array_unshift renumbers the array.
332
            $groupinfo = array_reverse($groupinfo, true);
333
            $groupinfo[-1] = get_string('allparticipants');
334
            $groupinfo = array_reverse($groupinfo, true);
335
            $groupcount++;
336
        }
337
 
338
        // Determine whether the user can select a group from the dropdown. The dropdown is available for several reasons.
339
        // 1) This is a new post (not an edit), and there are at least two groups to choose from.
340
        $canselectgroupfornew = empty($post->edit) && $groupcount > 1;
341
 
342
        // 2) This is editing of an existing post and the user is allowed to movediscussions.
343
        // We allow this because the post may have been moved from another forum where groups are not available.
344
        // We show this even if no groups are available as groups *may* have been available but now are not.
345
        $canselectgroupformove =
346
            $groupcount && !empty($post->edit) && has_capability('mod/forum:movediscussions', $modcontext);
347
 
348
        // Important: You can *only* change the group for a top level post. Never any reply.
349
        $canselectgroup = empty($post->parent) && ($canselectgroupfornew || $canselectgroupformove);
350
 
351
        if ($canselectgroup) {
352
            $mform->addElement('select', 'groupinfo', get_string('group'), $groupinfo);
353
            $mform->setDefault('groupinfo', $post->groupid);
354
            $mform->setType('groupinfo', PARAM_INT);
355
        } else {
356
            if (empty($post->groupid)) {
357
                $groupname = get_string('allparticipants');
358
            } else {
359
                $groupname = format_string($groupdata[$post->groupid]->name);
360
            }
361
            $mform->addElement('static', 'groupinfo', get_string('group'), $groupname);
362
        }
363
    }
364
}