227b82050155425f0b210e684f706959827d9150
[moodle.git] / mod / forum / post_form.php
1 <?php  // $Id$
3 require_once($CFG->libdir.'/formslib.php');
5 class mod_forum_post_form extends moodleform {
7     function definition() {
9         global $CFG;
10         $mform    =& $this->_form;
12         $course        = $this->_customdata['course'];
13         $cm            = $this->_customdata['cm'];
14         $coursecontext = $this->_customdata['coursecontext'];
15         $modcontext    = $this->_customdata['modcontext'];
16         $forum         = $this->_customdata['forum'];
17         $post          = $this->_customdata['post'];
18         // TODO: add max files and max size support
19         $editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'trusttext'=>true, 'context'=>$modcontext);
21         $mform->addElement('header', 'general', '');//fill in the data depending on page params
22                                                     //later using set_data
23         $mform->addElement('text', 'subject', get_string('subject', 'forum'), 'size="48"');
24         $mform->setType('subject', PARAM_TEXT);
25         $mform->addRule('subject', get_string('required'), 'required', null, 'client');
26         $mform->addRule('subject', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
28         $mform->addElement('editor', 'message', get_string('message', 'forum'), null, $editoroptions);
29         $mform->setType('message', PARAM_RAW);
30         $mform->addRule('message', get_string('required'), 'required', null, 'client');
31         $mform->setHelpButton('message', array('reading', 'writing', 'questions', 'richtext2'), false, 'editorhelpbutton');
33         if (isset($forum->id) && forum_is_forcesubscribed($forum)) {
35             $mform->addElement('static', 'subscribemessage', get_string('subscription', 'forum'), get_string('everyoneissubscribed', 'forum'));
36             $mform->addElement('hidden', 'subscribe');
37             $mform->setHelpButton('subscribemessage', array('subscription', get_string('subscription', 'forum'), 'forum'));
39         } else if (isset($forum->forcesubscribe)&& $forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE ||
40                     has_capability('moodle/course:manageactivities', $coursecontext)) {
42             $options = array();
43             $options[0] = get_string('subscribestop', 'forum');
44             $options[1] = get_string('subscribestart', 'forum');
46             $mform->addElement('select', 'subscribe', get_string('subscription', 'forum'), $options);
47             $mform->setHelpButton('subscribe', array('subscription', get_string('subscription', 'forum'), 'forum'));
48         } else if ($forum->forcesubscribe == FORUM_DISALLOWSUBSCRIBE) {
49             $mform->addElement('static', 'subscribemessage', get_string('subscription', 'forum'), get_string('disallowsubscribe', 'forum'));
50             $mform->addElement('hidden', 'subscribe');
51             $mform->setHelpButton('subscribemessage', array('subscription', get_string('subscription', 'forum'), 'forum'));
52         }
54         if (!empty($forum->maxattachments) && $forum->maxbytes != 1 && has_capability('mod/forum:createattachment', $modcontext))  {  //  1 = No attachments at all
55             $mform->addElement('filemanager', 'attachments', get_string('attachment', 'forum'), null,
56                 array('subdirs'=>0,
57                       'maxbytes'=>$forum->maxbytes,
58                       'maxfiles'=>$forum->maxattachments,
59                       'filetypes'=>'*',
60                       'returnvalue'=>'ref_id'
61                 ));
62             $mform->setHelpButton('attachments', array('attachment2', get_string('attachment', 'forum'), 'forum'));
63         }
65         if (empty($post->id) && has_capability('moodle/course:manageactivities', $coursecontext)) { // hack alert
66             $mform->addElement('checkbox', 'mailnow', get_string('mailnow', 'forum'));
67         }
69         if (!empty($CFG->forum_enabletimedposts) && !$post->parent && has_capability('mod/forum:viewhiddentimedposts', $coursecontext)) { // hack alert
70             $mform->addElement('header', '', get_string('displayperiod', 'forum'));
72             $mform->addElement('date_selector', 'timestart', get_string('displaystart', 'forum'), array('optional'=>true));
73             $mform->setHelpButton('timestart', array('displayperiod', get_string('displayperiod', 'forum'), 'forum'));
75             $mform->addElement('date_selector', 'timeend', get_string('displayend', 'forum'), array('optional'=>true));
76             $mform->setHelpButton('timeend', array('displayperiod', get_string('displayperiod', 'forum'), 'forum'));
78         } else {
79             $mform->addElement('hidden', 'timestart');
80             $mform->setType('timestart', PARAM_INT);
81             $mform->addElement('hidden', 'timeend');
82             $mform->setType('timeend', PARAM_INT);
83             $mform->setConstants(array('timestart'=> 0, 'timeend'=>0));
84         }
86         if (groups_get_activity_groupmode($cm, $course)) { // hack alert
87             if (empty($post->groupid)) {
88                 $groupname = get_string('allparticipants');
89             } else {
90                 $group = groups_get_group($post->groupid);
91                 $groupname = format_string($group->name);
92             }
93             $mform->addElement('static', 'groupinfo', get_string('group'), $groupname);
94         }
96 //-------------------------------------------------------------------------------
97         // buttons
98         if (isset($post->edit)) { // hack alert
99             $submit_string = get_string('savechanges');
100         } else {
101             $submit_string = get_string('posttoforum', 'forum');
102         }
103         $this->add_action_buttons(false, $submit_string);
105         $mform->addElement('hidden', 'course');
106         $mform->setType('course', PARAM_INT);
108         $mform->addElement('hidden', 'forum');
109         $mform->setType('forum', PARAM_INT);
111         $mform->addElement('hidden', 'discussion');
112         $mform->setType('discussion', PARAM_INT);
114         $mform->addElement('hidden', 'parent');
115         $mform->setType('parent', PARAM_INT);
117         $mform->addElement('hidden', 'userid');
118         $mform->setType('userid', PARAM_INT);
120         $mform->addElement('hidden', 'groupid');
121         $mform->setType('groupid', PARAM_INT);
123         $mform->addElement('hidden', 'edit');
124         $mform->setType('edit', PARAM_INT);
126         $mform->addElement('hidden', 'reply');
127         $mform->setType('reply', PARAM_INT);
129     }
131     function validation($data, $files) {
132         $errors = parent::validation($data, $files);
133         if (($data['timeend']!=0) && ($data['timestart']!=0)
134             && $data['timeend'] <= $data['timestart']) {
135                 $errors['timeend'] = get_string('timestartenderror', 'forum');
136             }
137         return $errors;
138     }