MDL-33948 mod_forum correctly passes files options to file_save_draft_area_files()
[moodle.git] / mod / forum / post_form.php
CommitLineData
1adbd2c3 1<?php
0e52f01f 2
8f685009
SH
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/>.
17
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 */
23
bfebaf64
MD
24if (!defined('MOODLE_INTERNAL')) {
25 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
26}
27
0e52f01f 28require_once($CFG->libdir.'/formslib.php');
29
f07b9627 30class mod_forum_post_form extends moodleform {
8adca237 31
61a339e5
MG
32 /**
33 * Returns the options array to use in filemanager for forum attachments
34 *
35 * @param stdClass $forum
36 * @return array
37 */
38 public static function attachment_options($forum) {
39 global $COURSE, $PAGE, $CFG;
40 $maxbytes = get_user_max_upload_file_size($PAGE->context, $CFG->maxbytes, $COURSE->maxbytes, $forum->maxbytes);
41 return array(
42 'subdirs' => 0,
43 'maxbytes' => $maxbytes,
44 'maxfiles' => $forum->maxattachments,
45 'accepted_types' => '*',
46 'return_types' => FILE_INTERNAL
47 );
48 }
49
50 /**
51 * Returns the options array to use in forum text editor
52 *
53 * @return array
54 */
55 public static function editor_options() {
56 global $COURSE, $PAGE, $CFG;
57 // TODO: add max files and max size support
58 $maxbytes = get_user_max_upload_file_size($PAGE->context, $CFG->maxbytes, $COURSE->maxbytes);
59 return array(
60 'maxfiles' => EDITOR_UNLIMITED_FILES,
61 'maxbytes' => $maxbytes,
62 'trusttext'=> true,
63 'return_types'=> FILE_INTERNAL | FILE_EXTERNAL
64 );
65 }
66
2ee60b49 67 function definition() {
8adca237 68
2ee60b49 69 global $CFG;
70 $mform =& $this->_form;
8adca237 71
c58efbf5 72 $course = $this->_customdata['course'];
4136c239 73 $cm = $this->_customdata['cm'];
2ee60b49 74 $coursecontext = $this->_customdata['coursecontext'];
c58efbf5 75 $modcontext = $this->_customdata['modcontext'];
0e52f01f 76 $forum = $this->_customdata['forum'];
5a4ba49c 77 $post = $this->_customdata['post'];
8adca237 78
195827d3 79 $mform->addElement('header', 'general', '');//fill in the data depending on page params later using set_data
2ee60b49 80 $mform->addElement('text', 'subject', get_string('subject', 'forum'), 'size="48"');
81 $mform->setType('subject', PARAM_TEXT);
82 $mform->addRule('subject', get_string('required'), 'required', null, 'client');
12fab708 83 $mform->addRule('subject', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
8adca237 84
61a339e5 85 $mform->addElement('editor', 'message', get_string('message', 'forum'), null, self::editor_options());
2ee60b49 86 $mform->setType('message', PARAM_RAW);
87 $mform->addRule('message', get_string('required'), 'required', null, 'client');
8adca237 88
90f4745c 89 if (isset($forum->id) && forum_is_forcesubscribed($forum)) {
8adca237 90
2ee60b49 91 $mform->addElement('static', 'subscribemessage', get_string('subscription', 'forum'), get_string('everyoneissubscribed', 'forum'));
8adca237 92 $mform->addElement('hidden', 'subscribe');
d18e0fe6 93 $mform->setType('subscribe', PARAM_INT);
530d2d72 94 $mform->addHelpButton('subscribemessage', 'subscription', 'forum');
8adca237 95
2ee60b49 96 } else if (isset($forum->forcesubscribe)&& $forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE ||
195827d3
DC
97 has_capability('moodle/course:manageactivities', $coursecontext)) {
98
99 $options = array();
100 $options[0] = get_string('subscribestop', 'forum');
101 $options[1] = get_string('subscribestart', 'forum');
102
103 $mform->addElement('select', 'subscribe', get_string('subscription', 'forum'), $options);
104 $mform->addHelpButton('subscribe', 'subscription', 'forum');
105 } else if ($forum->forcesubscribe == FORUM_DISALLOWSUBSCRIBE) {
106 $mform->addElement('static', 'subscribemessage', get_string('subscription', 'forum'), get_string('disallowsubscribe', 'forum'));
107 $mform->addElement('hidden', 'subscribe');
108 $mform->setType('subscribe', PARAM_INT);
109 $mform->addHelpButton('subscribemessage', 'subscription', 'forum');
110 }
8adca237 111
30a9aff5 112 if (!empty($forum->maxattachments) && $forum->maxbytes != 1 && has_capability('mod/forum:createattachment', $modcontext)) { // 1 = No attachments at all
61a339e5 113 $mform->addElement('filemanager', 'attachments', get_string('attachment', 'forum'), null, self::attachment_options($forum));
530d2d72 114 $mform->addHelpButton('attachments', 'attachment', 'forum');
c58efbf5 115 }
116
4136c239 117 if (empty($post->id) && has_capability('moodle/course:manageactivities', $coursecontext)) { // hack alert
0e52f01f 118 $mform->addElement('checkbox', 'mailnow', get_string('mailnow', 'forum'));
c58efbf5 119 }
8adca237 120
4136c239 121 if (!empty($CFG->forum_enabletimedposts) && !$post->parent && has_capability('mod/forum:viewhiddentimedposts', $coursecontext)) { // hack alert
0e52f01f 122 $mform->addElement('header', '', get_string('displayperiod', 'forum'));
123
2ee60b49 124 $mform->addElement('date_selector', 'timestart', get_string('displaystart', 'forum'), array('optional'=>true));
530d2d72 125 $mform->addHelpButton('timestart', 'displaystart', 'forum');
0e52f01f 126
2ee60b49 127 $mform->addElement('date_selector', 'timeend', get_string('displayend', 'forum'), array('optional'=>true));
530d2d72 128 $mform->addHelpButton('timeend', 'displayend', 'forum');
0e52f01f 129
2ee60b49 130 } else {
131 $mform->addElement('hidden', 'timestart');
132 $mform->setType('timestart', PARAM_INT);
133 $mform->addElement('hidden', 'timeend');
134 $mform->setType('timeend', PARAM_INT);
135 $mform->setConstants(array('timestart'=> 0, 'timeend'=>0));
136 }
4136c239 137
89d35c49 138 if (groups_get_activity_groupmode($cm, $course)) { // hack alert
2edcf21a 139 $groupdata = groups_get_activity_allowed_groups($cm);
140 $groupcount = count($groupdata);
141 $modulecontext = get_context_instance(CONTEXT_MODULE, $cm->id);
142 $contextcheck = has_capability('mod/forum:movediscussions', $modulecontext) && empty($post->parent) && $groupcount > 1;
143 if ($contextcheck) {
144 $groupinfo = array('0' => get_string('allparticipants'));
145 foreach ($groupdata as $grouptemp) {
146 $groupinfo[$grouptemp->id] = $grouptemp->name;
147 }
148 $mform->addElement('select','groupinfo', get_string('group'), $groupinfo);
149 $mform->setDefault('groupinfo', $post->groupid);
4136c239 150 } else {
2edcf21a 151 if (empty($post->groupid)) {
152 $groupname = get_string('allparticipants');
153 } else {
154 $groupname = format_string($groupdata[$post->groupid]->name);
155 }
156 $mform->addElement('static', 'groupinfo', get_string('group'), $groupname);
4136c239 157 }
4136c239 158 }
195827d3 159 //-------------------------------------------------------------------------------
a23f0aaf 160 // buttons
4136c239 161 if (isset($post->edit)) { // hack alert
2ee60b49 162 $submit_string = get_string('savechanges');
163 } else {
164 $submit_string = get_string('posttoforum', 'forum');
165 }
b6d15dde 166 $this->add_action_buttons(false, $submit_string);
8adca237 167
2ee60b49 168 $mform->addElement('hidden', 'course');
169 $mform->setType('course', PARAM_INT);
8adca237 170
2ee60b49 171 $mform->addElement('hidden', 'forum');
172 $mform->setType('forum', PARAM_INT);
8adca237 173
2ee60b49 174 $mform->addElement('hidden', 'discussion');
175 $mform->setType('discussion', PARAM_INT);
8adca237 176
2ee60b49 177 $mform->addElement('hidden', 'parent');
178 $mform->setType('parent', PARAM_INT);
8adca237 179
2ee60b49 180 $mform->addElement('hidden', 'userid');
181 $mform->setType('userid', PARAM_INT);
8adca237 182
2ee60b49 183 $mform->addElement('hidden', 'groupid');
184 $mform->setType('groupid', PARAM_INT);
8adca237 185
2ee60b49 186 $mform->addElement('hidden', 'edit');
187 $mform->setType('edit', PARAM_INT);
8adca237 188
2ee60b49 189 $mform->addElement('hidden', 'reply');
190 $mform->setType('reply', PARAM_INT);
2ee60b49 191 }
8adca237 192
a78890d5 193 function validation($data, $files) {
194 $errors = parent::validation($data, $files);
195827d3
DC
195 if (($data['timeend']!=0) && ($data['timestart']!=0) && $data['timeend'] <= $data['timestart']) {
196 $errors['timeend'] = get_string('timestartenderror', 'forum');
197 }
198 if (empty($data['message']['text'])) {
199 $errors['message'] = get_string('erroremptymessage', 'forum');
200 }
201 if (empty($data['subject'])) {
202 $errors['subject'] = get_string('erroremptysubject', 'forum');
203 }
a78890d5 204 return $errors;
2ee60b49 205 }
8adca237 206}
7de5a29a 207