MDL-22388 Added some checks to kill these scripts dead with an unequivocal notice...
[moodle.git] / mod / forum / 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  * @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  */
24 if (!defined('MOODLE_INTERNAL')) {
25     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
26 }
28 require_once($CFG->libdir.'/formslib.php');
30 class mod_forum_post_form extends moodleform {
32     function definition() {
34         global $CFG;
35         $mform    =& $this->_form;
37         $course        = $this->_customdata['course'];
38         $cm            = $this->_customdata['cm'];
39         $coursecontext = $this->_customdata['coursecontext'];
40         $modcontext    = $this->_customdata['modcontext'];
41         $forum         = $this->_customdata['forum'];
42         $post          = $this->_customdata['post'];
43         // TODO: add max files and max size support
44         $editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'trusttext'=>true, 'context'=>$modcontext);
46         $mform->addElement('header', 'general', '');//fill in the data depending on page params
47                                                     //later using set_data
48         $mform->addElement('text', 'subject', get_string('subject', 'forum'), 'size="48"');
49         $mform->setType('subject', PARAM_TEXT);
50         $mform->addRule('subject', get_string('required'), 'required', null, 'client');
51         $mform->addRule('subject', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
53         $mform->addElement('editor', 'message', get_string('message', 'forum'), null, $editoroptions);
54         $mform->setType('message', PARAM_RAW);
55         $mform->addRule('message', get_string('required'), 'required', null, 'client');
57         if (isset($forum->id) && forum_is_forcesubscribed($forum)) {
59             $mform->addElement('static', 'subscribemessage', get_string('subscription', 'forum'), get_string('everyoneissubscribed', 'forum'));
60             $mform->addElement('hidden', 'subscribe');
61             $mform->setType('subscribe', PARAM_INT);
62             $mform->addHelpButton('subscribemessage', 'subscription', 'forum');
64         } else if (isset($forum->forcesubscribe)&& $forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE ||
65                     has_capability('moodle/course:manageactivities', $coursecontext)) {
67             $options = array();
68             $options[0] = get_string('subscribestop', 'forum');
69             $options[1] = get_string('subscribestart', 'forum');
71             $mform->addElement('select', 'subscribe', get_string('subscription', 'forum'), $options);
72             $mform->addHelpButton('subscribe', 'subscription', 'forum');
73         } else if ($forum->forcesubscribe == FORUM_DISALLOWSUBSCRIBE) {
74             $mform->addElement('static', 'subscribemessage', get_string('subscription', 'forum'), get_string('disallowsubscribe', 'forum'));
75             $mform->addElement('hidden', 'subscribe');
76             $mform->setType('subscribe', PARAM_INT);
77             $mform->addHelpButton('subscribemessage', 'subscription', 'forum');
78         }
80         if (!empty($forum->maxattachments) && $forum->maxbytes != 1 && has_capability('mod/forum:createattachment', $modcontext))  {  //  1 = No attachments at all
81             $mform->addElement('filemanager', 'attachments', get_string('attachment', 'forum'), null,
82                 array('subdirs'=>0,
83                       'maxbytes'=>$forum->maxbytes,
84                       'maxfiles'=>$forum->maxattachments,
85                       'accepted_types'=>'*',
86                       'return_types'=>FILE_INTERNAL
87                 ));
88             $mform->addHelpButton('attachments', 'attachment', 'forum');
89         }
91         if (empty($post->id) && has_capability('moodle/course:manageactivities', $coursecontext)) { // hack alert
92             $mform->addElement('checkbox', 'mailnow', get_string('mailnow', 'forum'));
93         }
95         if (!empty($CFG->forum_enabletimedposts) && !$post->parent && has_capability('mod/forum:viewhiddentimedposts', $coursecontext)) { // hack alert
96             $mform->addElement('header', '', get_string('displayperiod', 'forum'));
98             $mform->addElement('date_selector', 'timestart', get_string('displaystart', 'forum'), array('optional'=>true));
99             $mform->addHelpButton('timestart', 'displaystart', 'forum');
101             $mform->addElement('date_selector', 'timeend', get_string('displayend', 'forum'), array('optional'=>true));
102             $mform->addHelpButton('timeend', 'displayend', 'forum');
104         } else {
105             $mform->addElement('hidden', 'timestart');
106             $mform->setType('timestart', PARAM_INT);
107             $mform->addElement('hidden', 'timeend');
108             $mform->setType('timeend', PARAM_INT);
109             $mform->setConstants(array('timestart'=> 0, 'timeend'=>0));
110         }
112         if (groups_get_activity_groupmode($cm, $course)) { // hack alert
113             if (empty($post->groupid)) {
114                 $groupname = get_string('allparticipants');
115             } else {
116                 $group = groups_get_group($post->groupid);
117                 $groupname = format_string($group->name);
118             }
119             $mform->addElement('static', 'groupinfo', get_string('group'), $groupname);
120         }
122 //-------------------------------------------------------------------------------
123         // buttons
124         if (isset($post->edit)) { // hack alert
125             $submit_string = get_string('savechanges');
126         } else {
127             $submit_string = get_string('posttoforum', 'forum');
128         }
129         $this->add_action_buttons(false, $submit_string);
131         $mform->addElement('hidden', 'course');
132         $mform->setType('course', PARAM_INT);
134         $mform->addElement('hidden', 'forum');
135         $mform->setType('forum', PARAM_INT);
137         $mform->addElement('hidden', 'discussion');
138         $mform->setType('discussion', PARAM_INT);
140         $mform->addElement('hidden', 'parent');
141         $mform->setType('parent', PARAM_INT);
143         $mform->addElement('hidden', 'userid');
144         $mform->setType('userid', PARAM_INT);
146         $mform->addElement('hidden', 'groupid');
147         $mform->setType('groupid', PARAM_INT);
149         $mform->addElement('hidden', 'edit');
150         $mform->setType('edit', PARAM_INT);
152         $mform->addElement('hidden', 'reply');
153         $mform->setType('reply', PARAM_INT);
155     }
157     function validation($data, $files) {
158         $errors = parent::validation($data, $files);
159         if (($data['timeend']!=0) && ($data['timestart']!=0)
160             && $data['timeend'] <= $data['timestart']) {
161                 $errors['timeend'] = get_string('timestartenderror', 'forum');
162             }
163         return $errors;
164     }