Presentation fixes to formfactor and canvas
[moodle.git] / mod / forum / mod_form.php
CommitLineData
535e0ab6 1<?php
8f685009
SH
2
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
23a7718b 24require_once ($CFG->dirroot.'/course/moodleform_mod.php');
e24b7f85 25
f07b9627 26class mod_forum_mod_form extends moodleform_mod {
535e0ab6 27
2ee60b49 28 function definition() {
c18269c7 29 global $CFG, $COURSE, $DB;
535e0ab6 30
2ee60b49 31 $mform =& $this->_form;
535e0ab6 32
33//-------------------------------------------------------------------------------
34 $mform->addElement('header', 'general', get_string('general', 'form'));
35
26de8d35 36 $mform->addElement('text', 'name', get_string('forumname', 'forum'), array('size'=>'64'));
8eb1d25f 37 if (!empty($CFG->formatstringstriptags)) {
4e781c7b 38 $mform->setType('name', PARAM_TEXT);
8eb1d25f 39 } else {
40 $mform->setType('name', PARAM_CLEAN);
41 }
2ee60b49 42 $mform->addRule('name', null, 'required', null, 'client');
b230cac1 43 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
535e0ab6 44
65bcf17b 45 $forum_types = forum_get_forum_types();
46
47 asort($forum_types);
48 $mform->addElement('select', 'type', get_string('forumtype', 'forum'), $forum_types);
2ee60b49 49 $mform->setHelpButton('type', array('forumtype', get_string('forumtype', 'forum'), 'forum'));
50 $mform->setDefault('type', 'general');
535e0ab6 51
4ba2221a 52 $this->add_intro_editor(true, get_string('forumintro', 'forum'));
b2a9505b 53
535e0ab6 54 $options = array();
afef965e
SH
55 $options[FORUM_CHOOSESUBSCRIBE] = get_string('subscriptionoptional', 'forum');
56 $options[FORUM_FORCESUBSCRIBE] = get_string('subscriptionforced', 'forum');
57 $options[FORUM_INITIALSUBSCRIBE] = get_string('subscriptionauto', 'forum');
58 $options[FORUM_DISALLOWSUBSCRIBE] = get_string('subscriptiondisabled','forum');
59 $mform->addElement('select', 'forcesubscribe', get_string('subscriptionmode', 'forum'), $options);
2ee60b49 60 $mform->setHelpButton('forcesubscribe', array('subscription2', get_string('forcesubscribeq', 'forum'), 'forum'));
535e0ab6 61
62 $options = array();
63 $options[FORUM_TRACKING_OPTIONAL] = get_string('trackingoptional', 'forum');
64 $options[FORUM_TRACKING_OFF] = get_string('trackingoff', 'forum');
65 $options[FORUM_TRACKING_ON] = get_string('trackingon', 'forum');
66 $mform->addElement('select', 'trackingtype', get_string('trackingtype', 'forum'), $options);
2ee60b49 67 $mform->setHelpButton('trackingtype', array('trackingtype', get_string('trackingtype', 'forum'), 'forum'));
535e0ab6 68
a02c77dc 69 $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
535e0ab6 70 $choices[1] = get_string('uploadnotallowed');
a02c77dc 71 $choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')';
535e0ab6 72 $mform->addElement('select', 'maxbytes', get_string('maxattachmentsize', 'forum'), $choices);
2ee60b49 73 $mform->setHelpButton('maxbytes', array('maxattachmentsize', get_string('maxattachmentsize', 'forum'), 'forum'));
74 $mform->setDefault('maxbytes', $CFG->forum_maxbytes);
535e0ab6 75
30a9aff5 76 $choices = array(0,1,2,3,4,5,6,7,8,9);
77 $mform->addElement('select', 'maxattachments', get_string('maxattachments', 'forum'), $choices);
78 $mform->setHelpButton('maxattachments', array('maxattachments', get_string('maxattachments', 'forum'), 'forum'));
79 $mform->setDefault('maxattachments', $CFG->forum_maxattachments);
80
535e0ab6 81 if ($CFG->enablerssfeeds && isset($CFG->forum_enablerssfeeds) && $CFG->forum_enablerssfeeds) {
a02c77dc 82//-------------------------------------------------------------------------------
535e0ab6 83 $mform->addElement('header', '', get_string('rss'));
84 $choices = array();
85 $choices[0] = get_string('none');
86 $choices[1] = get_string('discussions', 'forum');
87 $choices[2] = get_string('posts', 'forum');
88 $mform->addElement('select', 'rsstype', get_string('rsstype'), $choices);
89 $mform->setHelpButton('rsstype', array('rsstype', get_string('rsstype'), 'forum'));
90
91 $choices = array();
92 $choices[0] = '0';
93 $choices[1] = '1';
94 $choices[2] = '2';
95 $choices[3] = '3';
96 $choices[4] = '4';
97 $choices[5] = '5';
98 $choices[10] = '10';
99 $choices[15] = '15';
100 $choices[20] = '20';
101 $choices[25] = '25';
102 $choices[30] = '30';
103 $choices[40] = '40';
104 $choices[50] = '50';
105 $mform->addElement('select', 'rssarticles', get_string('rssarticles'), $choices);
106 $mform->setHelpButton('rssarticles', array('rssarticles', get_string('rssarticles'), 'forum'));
107 }
108
a02c77dc 109//-------------------------------------------------------------------------------
535e0ab6 110 $mform->addElement('header', '', get_string('blockafter', 'forum'));
2ee60b49 111 $options = array();
535e0ab6 112 $options[0] = get_string('blockperioddisabled','forum');
113 $options[60*60*24] = '1 '.get_string('day');
114 $options[60*60*24*2] = '2 '.get_string('days');
115 $options[60*60*24*3] = '3 '.get_string('days');
116 $options[60*60*24*4] = '4 '.get_string('days');
117 $options[60*60*24*5] = '5 '.get_string('days');
118 $options[60*60*24*6] = '6 '.get_string('days');
119 $options[60*60*24*7] = '1 '.get_string('week');
120 $mform->addElement('select', 'blockperiod', get_string("blockperiod", "forum") , $options);
2ee60b49 121 $mform->setHelpButton('blockperiod', array('manageposts', get_string('blockperiod', 'forum'),'forum'));
535e0ab6 122
2ee60b49 123 $mform->addElement('text', 'blockafter', get_string('blockafter', 'forum'));
124 $mform->setType('blockafter', PARAM_INT);
125 $mform->setDefault('blockafter', '0');
126 $mform->addRule('blockafter', null, 'numeric', null, 'client');
127 $mform->setHelpButton('blockafter', array('manageposts', get_string('blockafter', 'forum'),'forum'));
128 $mform->disabledIf('blockafter', 'blockperiod', 'eq', 0);
9c03bbaa 129
130
131 $mform->addElement('text', 'warnafter', get_string('warnafter', 'forum'));
2ee60b49 132 $mform->setType('warnafter', PARAM_INT);
133 $mform->setDefault('warnafter', '0');
134 $mform->addRule('warnafter', null, 'numeric', null, 'client');
135 $mform->setHelpButton('warnafter', array('manageposts', get_string('warnafter', 'forum'),'forum'));
136 $mform->disabledIf('warnafter', 'blockperiod', 'eq', 0);
9c03bbaa 137
a02c77dc 138//-------------------------------------------------------------------------------
42f103be 139 $this->standard_coursemodule_elements();
a23f0aaf 140//-------------------------------------------------------------------------------
2ee60b49 141// buttons
a23f0aaf 142 $this->add_action_buttons();
535e0ab6 143
2ee60b49 144 }
535e0ab6 145
4ba2221a 146 function definition_after_data() {
4050572d 147 parent::definition_after_data();
148 $mform =& $this->_form;
149 $type =& $mform->getElement('type');
150 $typevalue = $mform->getElementValue('type');
151
d24206d6 152 //we don't want to have these appear as possible selections in the form but
153 //we want the form to display them if they are set.
4ba2221a 154 if ($typevalue[0]=='news') {
d24206d6 155 $type->addOption(get_string('namenews', 'forum'), 'news');
4bcc5118 156 $mform->setHelpButton('type', array('forumtypenews', get_string('forumtype', 'forum'), 'forum'));
d24206d6 157 $type->freeze();
158 $type->setPersistantFreeze(true);
159 }
4ba2221a 160 if ($typevalue[0]=='social') {
d24206d6 161 $type->addOption(get_string('namesocial', 'forum'), 'social');
535e0ab6 162 $type->freeze();
163 $type->setPersistantFreeze(true);
164 }
02f8f37d 165
2ee60b49 166 }
535e0ab6 167
4ba2221a 168 function data_preprocessing(&$default_values) {
4e781c7b 169 // Set up the completion checkboxes which aren't part of standard data.
170 // We also make the default value (if you turn on the checkbox) for those
171 // numbers to be 1, this will not apply unless checkbox is ticked.
172 $default_values['completiondiscussionsenabled']=
173 !empty($default_values['completiondiscussions']) ? 1 : 0;
4ba2221a 174 if (empty($default_values['completiondiscussions'])) {
4e781c7b 175 $default_values['completiondiscussions']=1;
176 }
177 $default_values['completionrepliesenabled']=
178 !empty($default_values['completionreplies']) ? 1 : 0;
4ba2221a 179 if (empty($default_values['completionreplies'])) {
4e781c7b 180 $default_values['completionreplies']=1;
181 }
182 $default_values['completionpostsenabled']=
183 !empty($default_values['completionposts']) ? 1 : 0;
4ba2221a 184 if (empty($default_values['completionposts'])) {
4e781c7b 185 $default_values['completionposts']=1;
186 }
2ee60b49 187 }
e24b7f85 188
4454447d 189 function add_completion_rules() {
4e781c7b 190 $mform =& $this->_form;
191
192 $group=array();
193 $group[] =& $mform->createElement('checkbox', 'completionpostsenabled', '', get_string('completionposts','forum'));
194 $group[] =& $mform->createElement('text', 'completionposts', '', array('size'=>3));
195 $mform->setType('completionposts',PARAM_INT);
196 $mform->addGroup($group, 'completionpostsgroup', get_string('completionpostsgroup','forum'), array(' '), false);
197 $mform->setHelpButton('completionpostsgroup', array('completion', get_string('completionpostshelp', 'forum'), 'forum'));
198 $mform->disabledIf('completionposts','completionpostsenabled','notchecked');
199
200 $group=array();
201 $group[] =& $mform->createElement('checkbox', 'completiondiscussionsenabled', '', get_string('completiondiscussions','forum'));
202 $group[] =& $mform->createElement('text', 'completiondiscussions', '', array('size'=>3));
203 $mform->setType('completiondiscussions',PARAM_INT);
204 $mform->addGroup($group, 'completiondiscussionsgroup', get_string('completiondiscussionsgroup','forum'), array(' '), false);
205 $mform->setHelpButton('completiondiscussionsgroup', array('completion', get_string('completiondiscussionshelp', 'forum'), 'forum'));
206 $mform->disabledIf('completiondiscussions','completiondiscussionsenabled','notchecked');
207
208 $group=array();
209 $group[] =& $mform->createElement('checkbox', 'completionrepliesenabled', '', get_string('completionreplies','forum'));
210 $group[] =& $mform->createElement('text', 'completionreplies', '', array('size'=>3));
211 $mform->setType('completionreplies',PARAM_INT);
212 $mform->addGroup($group, 'completionrepliesgroup', get_string('completionrepliesgroup','forum'), array(' '), false);
213 $mform->setHelpButton('completionrepliesgroup', array('completion', get_string('completionreplieshelp', 'forum'), 'forum'));
214 $mform->disabledIf('completionreplies','completionrepliesenabled','notchecked');
215
216 return array('completiondiscussionsgroup','completionrepliesgroup','completionpostsgroup');
217 }
218
219 function completion_rule_enabled($data) {
220 return (!empty($data['completiondiscussionsenabled']) && $data['completiondiscussions']!=0) ||
221 (!empty($data['completionrepliesenabled']) && $data['completionreplies']!=0) ||
222 (!empty($data['completionpostsenabled']) && $data['completionposts']!=0);
223 }
224
49f6e5f4 225 function get_data() {
4ba2221a 226 $data = parent::get_data();
227 if (!$data) {
4e781c7b 228 return false;
229 }
230 // Turn off completion settings if the checkboxes aren't ticked
4ba2221a 231 $autocompletion = !empty($data->completion) && $data->completion==COMPLETION_TRACKING_AUTOMATIC;
232 if (empty($data->completiondiscussionsenabled) || !$autocompletion) {
233 $data->completiondiscussions = 0;
4e781c7b 234 }
4ba2221a 235 if (empty($data->completionrepliesenabled) || !$autocompletion) {
236 $data->completionreplies = 0;
4e781c7b 237 }
4ba2221a 238 if (empty($data->completionpostsenabled) || !$autocompletion) {
239 $data->completionposts = 0;
4e781c7b 240 }
241 return $data;
242 }
535e0ab6 243}
1adbd2c3 244