MDL-20353 adding missing data types of hidden form fields
[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->setType('subscribe', PARAM_INT);
38             $mform->setHelpButton('subscribemessage', array('subscription', get_string('subscription', 'forum'), 'forum'));
40         } else if (isset($forum->forcesubscribe)&& $forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE ||
41                     has_capability('moodle/course:manageactivities', $coursecontext)) {
43             $options = array();
44             $options[0] = get_string('subscribestop', 'forum');
45             $options[1] = get_string('subscribestart', 'forum');
47             $mform->addElement('select', 'subscribe', get_string('subscription', 'forum'), $options);
48             $mform->setHelpButton('subscribe', array('subscription', get_string('subscription', 'forum'), 'forum'));
49         } else if ($forum->forcesubscribe == FORUM_DISALLOWSUBSCRIBE) {
50             $mform->addElement('static', 'subscribemessage', get_string('subscription', 'forum'), get_string('disallowsubscribe', 'forum'));
51             $mform->addElement('hidden', 'subscribe');
52             $mform->setType('subscribe', PARAM_INT);
53             $mform->setHelpButton('subscribemessage', array('subscription', get_string('subscription', 'forum'), 'forum'));
54         }
56         if (!empty($forum->maxattachments) && $forum->maxbytes != 1 && has_capability('mod/forum:createattachment', $modcontext))  {  //  1 = No attachments at all
57             $mform->addElement('filemanager', 'attachments', get_string('attachment', 'forum'), null,
58                 array('subdirs'=>0,
59                       'maxbytes'=>$forum->maxbytes,
60                       'maxfiles'=>$forum->maxattachments,
61                       'filetypes'=>'*',
62                       'returnvalue'=>'ref_id'
63                 ));
64             $mform->setHelpButton('attachments', array('attachment2', get_string('attachment', 'forum'), 'forum'));
65         }
67         if (empty($post->id) && has_capability('moodle/course:manageactivities', $coursecontext)) { // hack alert
68             $mform->addElement('checkbox', 'mailnow', get_string('mailnow', 'forum'));
69         }
71         if (!empty($CFG->forum_enabletimedposts) && !$post->parent && has_capability('mod/forum:viewhiddentimedposts', $coursecontext)) { // hack alert
72             $mform->addElement('header', '', get_string('displayperiod', 'forum'));
74             $mform->addElement('date_selector', 'timestart', get_string('displaystart', 'forum'), array('optional'=>true));
75             $mform->setHelpButton('timestart', array('displayperiod', get_string('displayperiod', 'forum'), 'forum'));
77             $mform->addElement('date_selector', 'timeend', get_string('displayend', 'forum'), array('optional'=>true));
78             $mform->setHelpButton('timeend', array('displayperiod', get_string('displayperiod', 'forum'), 'forum'));
80         } else {
81             $mform->addElement('hidden', 'timestart');
82             $mform->setType('timestart', PARAM_INT);
83             $mform->addElement('hidden', 'timeend');
84             $mform->setType('timeend', PARAM_INT);
85             $mform->setConstants(array('timestart'=> 0, 'timeend'=>0));
86         }
88         if (groups_get_activity_groupmode($cm, $course)) { // hack alert
89             if (empty($post->groupid)) {
90                 $groupname = get_string('allparticipants');
91             } else {
92                 $group = groups_get_group($post->groupid);
93                 $groupname = format_string($group->name);
94             }
95             $mform->addElement('static', 'groupinfo', get_string('group'), $groupname);
96         }
98 //-------------------------------------------------------------------------------
99         // buttons
100         if (isset($post->edit)) { // hack alert
101             $submit_string = get_string('savechanges');
102         } else {
103             $submit_string = get_string('posttoforum', 'forum');
104         }
105         $this->add_action_buttons(false, $submit_string);
107         $mform->addElement('hidden', 'course');
108         $mform->setType('course', PARAM_INT);
110         $mform->addElement('hidden', 'forum');
111         $mform->setType('forum', PARAM_INT);
113         $mform->addElement('hidden', 'discussion');
114         $mform->setType('discussion', PARAM_INT);
116         $mform->addElement('hidden', 'parent');
117         $mform->setType('parent', PARAM_INT);
119         $mform->addElement('hidden', 'userid');
120         $mform->setType('userid', PARAM_INT);
122         $mform->addElement('hidden', 'groupid');
123         $mform->setType('groupid', PARAM_INT);
125         $mform->addElement('hidden', 'edit');
126         $mform->setType('edit', PARAM_INT);
128         $mform->addElement('hidden', 'reply');
129         $mform->setType('reply', PARAM_INT);
131     }
133     function validation($data, $files) {
134         $errors = parent::validation($data, $files);
135         if (($data['timeend']!=0) && ($data['timestart']!=0)
136             && $data['timeend'] <= $data['timestart']) {
137                 $errors['timeend'] = get_string('timestartenderror', 'forum');
138             }
139         return $errors;
140     }