1e76e93edf5926c36307fcf9c2793dd03841e491
[moodle.git] / mod / forum / classes / post_form.php
1 <?php
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/>.
18 /**
19  * File containing the form definition to post in the forum.
20  *
21  * @package   mod_forum
22  * @copyright Jamie Pratt <me@jamiep.org>
23  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 defined('MOODLE_INTERNAL') || die();
27 require_once($CFG->libdir . '/formslib.php');
28 require_once($CFG->dirroot . '/repository/lib.php');
30 /**
31  * Class to post in a forum.
32  *
33  * @package   mod_forum
34  * @copyright Jamie Pratt <me@jamiep.org>
35  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36  */
37 class mod_forum_post_form extends moodleform {
39     /**
40      * Returns the options array to use in filemanager for forum attachments
41      *
42      * @param stdClass $forum
43      * @return array
44      */
45     public static function attachment_options($forum) {
46         global $COURSE, $PAGE, $CFG;
47         $maxbytes = get_user_max_upload_file_size($PAGE->context, $CFG->maxbytes, $COURSE->maxbytes, $forum->maxbytes);
48         return array(
49             'subdirs' => 0,
50             'maxbytes' => $maxbytes,
51             'maxfiles' => $forum->maxattachments,
52             'accepted_types' => '*',
53             'return_types' => FILE_INTERNAL
54         );
55     }
57     /**
58      * Returns the options array to use in forum text editor
59      *
60      * @param context_module $context
61      * @param int $postid post id, use null when adding new post
62      * @return array
63      */
64     public static function editor_options(context_module $context, $postid) {
65         global $COURSE, $PAGE, $CFG;
66         // TODO: add max files and max size support
67         $maxbytes = get_user_max_upload_file_size($PAGE->context, $CFG->maxbytes, $COURSE->maxbytes);
68         return array(
69             'maxfiles' => EDITOR_UNLIMITED_FILES,
70             'maxbytes' => $maxbytes,
71             'trusttext'=> true,
72             'return_types'=> FILE_INTERNAL | FILE_EXTERNAL,
73             'subdirs' => file_area_contains_subdirs($context, 'mod_forum', 'post', $postid)
74         );
75     }
77     /**
78      * Form definition
79      *
80      * @return void
81      */
82     function definition() {
83         global $CFG, $OUTPUT;
85         $mform =& $this->_form;
87         $course = $this->_customdata['course'];
88         $cm = $this->_customdata['cm'];
89         $coursecontext = $this->_customdata['coursecontext'];
90         $modcontext = $this->_customdata['modcontext'];
91         $forum = $this->_customdata['forum'];
92         $post = $this->_customdata['post'];
93         $subscribe = $this->_customdata['subscribe'];
94         $edit = $this->_customdata['edit'];
95         $thresholdwarning = $this->_customdata['thresholdwarning'];
97         $mform->addElement('header', 'general', '');//fill in the data depending on page params later using set_data
99         // If there is a warning message and we are not editing a post we need to handle the warning.
100         if (!empty($thresholdwarning) && !$edit) {
101             // Here we want to display a warning if they can still post but have reached the warning threshold.
102             if ($thresholdwarning->canpost) {
103                 $message = get_string($thresholdwarning->errorcode, $thresholdwarning->module, $thresholdwarning->additional);
104                 $mform->addElement('html', $OUTPUT->notification($message));
105             }
106         }
108         $mform->addElement('text', 'subject', get_string('subject', 'forum'), 'size="48"');
109         $mform->setType('subject', PARAM_TEXT);
110         $mform->addRule('subject', get_string('required'), 'required', null, 'client');
111         $mform->addRule('subject', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
113         $mform->addElement('editor', 'message', get_string('message', 'forum'), null, self::editor_options($modcontext, (empty($post->id) ? null : $post->id)));
114         $mform->setType('message', PARAM_RAW);
115         $mform->addRule('message', get_string('required'), 'required', null, 'client');
117         $manageactivities = has_capability('moodle/course:manageactivities', $coursecontext);
119         if (\mod_forum\subscriptions::is_forcesubscribed($forum)) {
121             $mform->addElement('static', 'subscribemessage', get_string('subscription', 'forum'), get_string('everyoneissubscribed', 'forum'));
122             $mform->addElement('hidden', 'subscribe');
123             $mform->setType('subscribe', PARAM_INT);
124             $mform->addHelpButton('subscribemessage', 'forcesubscribed', 'forum');
126         } else if (\mod_forum\subscriptions::subscription_disabled($forum) && !$manageactivities) {
127             $mform->addElement('static', 'subscribemessage', get_string('subscription', 'forum'), get_string('disallowsubscribe', 'forum'));
128             $mform->addElement('hidden', 'discussionsubscribe');
129             $mform->setType('discussionsubscribe', PARAM_INT);
130             $mform->addHelpButton('subscribemessage', 'disallowsubscription', 'forum');
132         } else {
133             $options = array();
134             $options[0] = get_string('discussionsubscribestop', 'forum');
135             $options[1] = get_string('discussionsubscribestart', 'forum');
137             $mform->addElement('select', 'discussionsubscribe', get_string('discussionsubscription', 'forum'), $options);
138             $mform->addHelpButton('discussionsubscribe', 'discussionsubscription', 'forum');
139         }
141         if (!empty($forum->maxattachments) && $forum->maxbytes != 1 && has_capability('mod/forum:createattachment', $modcontext))  {  //  1 = No attachments at all
142             $mform->addElement('filemanager', 'attachments', get_string('attachment', 'forum'), null, self::attachment_options($forum));
143             $mform->addHelpButton('attachments', 'attachment', 'forum');
144         }
146         if (empty($post->id) && $manageactivities) {
147             $mform->addElement('checkbox', 'mailnow', get_string('mailnow', 'forum'));
148         }
150         if (!empty($CFG->forum_enabletimedposts) && !$post->parent && has_capability('mod/forum:viewhiddentimedposts', $coursecontext)) { // hack alert
151             $mform->addElement('header', 'displayperiod', get_string('displayperiod', 'forum'));
153             $mform->addElement('date_selector', 'timestart', get_string('displaystart', 'forum'), array('optional'=>true));
154             $mform->addHelpButton('timestart', 'displaystart', 'forum');
156             $mform->addElement('date_selector', 'timeend', get_string('displayend', 'forum'), array('optional'=>true));
157             $mform->addHelpButton('timeend', 'displayend', 'forum');
159         } else {
160             $mform->addElement('hidden', 'timestart');
161             $mform->setType('timestart', PARAM_INT);
162             $mform->addElement('hidden', 'timeend');
163             $mform->setType('timeend', PARAM_INT);
164             $mform->setConstants(array('timestart'=> 0, 'timeend'=>0));
165         }
167         if ($groupmode = groups_get_activity_groupmode($cm, $course)) { // hack alert
168             $groupdata = groups_get_activity_allowed_groups($cm);
169             $groupcount = count($groupdata);
170             $groupinfo = array();
171             $modulecontext = context_module::instance($cm->id);
173             // Check whether the user has access to all groups in this forum from the accessallgroups cap.
174             if ($groupmode == VISIBLEGROUPS || has_capability('moodle/site:accessallgroups', $modulecontext)) {
175                 // Only allow posting to all groups if the user has access to all groups.
176                 $groupinfo = array('0' => get_string('allparticipants'));
177                 $groupcount++;
178             }
180             $contextcheck = has_capability('mod/forum:movediscussions', $modulecontext) && empty($post->parent) && $groupcount > 1;
181             if ($contextcheck) {
182                 foreach ($groupdata as $grouptemp) {
183                     $groupinfo[$grouptemp->id] = $grouptemp->name;
184                 }
185                 $mform->addElement('select','groupinfo', get_string('group'), $groupinfo);
186                 $mform->setDefault('groupinfo', $post->groupid);
187                 $mform->setType('groupinfo', PARAM_INT);
188             } else {
189                 if (empty($post->groupid)) {
190                     $groupname = get_string('allparticipants');
191                 } else {
192                     $groupname = format_string($groupdata[$post->groupid]->name);
193                 }
194                 $mform->addElement('static', 'groupinfo', get_string('group'), $groupname);
195             }
196         }
197         //-------------------------------------------------------------------------------
198         // buttons
199         if (isset($post->edit)) { // hack alert
200             $submit_string = get_string('savechanges');
201         } else {
202             $submit_string = get_string('posttoforum', 'forum');
203         }
204         $this->add_action_buttons(false, $submit_string);
206         $mform->addElement('hidden', 'course');
207         $mform->setType('course', PARAM_INT);
209         $mform->addElement('hidden', 'forum');
210         $mform->setType('forum', PARAM_INT);
212         $mform->addElement('hidden', 'discussion');
213         $mform->setType('discussion', PARAM_INT);
215         $mform->addElement('hidden', 'parent');
216         $mform->setType('parent', PARAM_INT);
218         $mform->addElement('hidden', 'userid');
219         $mform->setType('userid', PARAM_INT);
221         $mform->addElement('hidden', 'groupid');
222         $mform->setType('groupid', PARAM_INT);
224         $mform->addElement('hidden', 'edit');
225         $mform->setType('edit', PARAM_INT);
227         $mform->addElement('hidden', 'reply');
228         $mform->setType('reply', PARAM_INT);
229     }
231     /**
232      * Form validation
233      *
234      * @param array $data data from the form.
235      * @param array $files files uploaded.
236      * @return array of errors.
237      */
238     function validation($data, $files) {
239         $errors = parent::validation($data, $files);
240         if (($data['timeend']!=0) && ($data['timestart']!=0) && $data['timeend'] <= $data['timestart']) {
241             $errors['timeend'] = get_string('timestartenderror', 'forum');
242         }
243         if (empty($data['message']['text'])) {
244             $errors['message'] = get_string('erroremptymessage', 'forum');
245         }
246         if (empty($data['subject'])) {
247             $errors['subject'] = get_string('erroremptysubject', 'forum');
248         }
249         return $errors;
250     }