Updated the HEAD build version to 20090911
[moodle.git] / blog / edit_form.php
CommitLineData
cae83708 1<?php
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/>.
f07b9627 17
18require_once($CFG->libdir.'/formslib.php');
19
20class blog_edit_form extends moodleform {
cae83708 21 public $modnames = array();
f07b9627 22
2ee60b49 23 function definition() {
cae83708 24 global $CFG, $COURSE, $USER, $DB, $PAGE;
45df7de3 25
2ee60b49 26 $mform =& $this->_form;
f07b9627 27
cae83708 28 $existing = $this->_customdata['existing'];
0a941490 29 $entryid = $this->_customdata['id'];
cae83708 30 $summaryoptions = $this->_customdata['textfieldoptions'];
31
32 if (!empty($this->_customdata['assignmentdata'])) {
33 $assignmentdata = $this->_customdata['assignmentdata'];
34 }
35
36 $existing = $this->_customdata['existing'];
f07b9627 37 $sitecontext = $this->_customdata['sitecontext'];
38
cae83708 39 //determine if content elements should be deactivated for a past due blog assignment
40 $noedit = false;
41 if (!empty($assignmentdata)) {
42 if ((time() > $assignmentdata->timedue && $assignmentdata->preventlate) || $assignmentdata->grade != -1) {
43 $noedit = true;
44 }
45 }
46
f07b9627 47 $mform->addElement('header', 'general', get_string('general', 'form'));
cae83708 48
49 if ($noedit) { //show disabled form elements, but provide hidden elements so that the data is transferred
50 $mform->addElement('text', 'fakesubject', get_string('entrytitle', 'blog'), array('size'=>60, 'disabled'=>'disabled'));
51 $mform->addElement('textarea', 'fakesummary', get_string('entrybody', 'blog'), array('rows'=>25, 'cols'=>40, 'disabled'=>'disabled'));
52 $mform->setHelpButton('fakesummary', array('writing', 'richtext'), false, 'editorhelpbutton');
53 $mform->addElement('hidden', 'subject');
54 $mform->addElement('hidden', 'summary');
55
56 } else { //insert normal form elements
57 $mform->addElement('text', 'subject', get_string('entrytitle', 'blog'), 'size="60"');
58 $textfieldoptions = array('trusttext'=>true, 'subdirs'=>true);
59 $mform->addElement('editor', 'summary_editor', get_string('entrybody', 'blog'), null, $summaryoptions);
60 }
61
2ee60b49 62 $mform->setType('subject', PARAM_TEXT);
63 $mform->addRule('subject', get_string('emptytitle', 'blog'), 'required', null, 'client');
f07b9627 64
cae83708 65 $mform->setType('summary_editor', PARAM_RAW);
66 $mform->addRule('summary_editor', get_string('emptybody', 'blog'), 'required', null, 'client');
67 $mform->setHelpButton('summary_editor', array('writing', 'richtext2'), false, 'editorhelpbutton');
f07b9627 68
69 $mform->addElement('format', 'format', get_string('format'));
70
b0e90a0c 71 $mform->addElement('filepicker', 'attachment', get_string('attachment', 'forum'), null, array('filetypes' => '*'));
f07b9627 72
cae83708 73 //disable publishstate options that are not allowed
74 $publishstates = array();
75 $i = 0;
76
77 foreach (blog_entry::get_applicable_publish_states() as $state => $desc) {
78 if (!empty($assignmentdata)) {
79 if ($i <= $assignmentdata->var2) { //var2 is the maximum publish state allowed
80 $publishstates[$state] = $desc;
81 }
82 } else {
83 $publishstates[$state] = $desc; //no maximum was set
84 }
85
86 $i++;
87 }
88
89 $mform->addElement('select', 'publishstate', get_string('publishto', 'blog'), $publishstates);
d4175332 90 $mform->setHelpButton('publishstate', array('publish_state', get_string('publishto', 'blog'), 'blog'));
f07b9627 91
93bbdd50 92
93 if (!empty($CFG->usetags)) {
3d535996 94 $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
fc23301c 95 $mform->addElement('tags', 'tags', get_string('tags'));
f07b9627 96 }
fc23301c 97
cae83708 98 $allmodnames = array();
99
100 if (!empty($CFG->useblogassociations)) {
101 $mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
102
103 if (has_capability('moodle/site:doanything', get_context_instance(CONTEXT_USER, $USER->id))) {
104 $courses = get_courses('all', 'visible DESC, fullname ASC');
105 } else {
106 $courses = get_my_courses($USER->id, 'visible DESC, fullname ASC');
107 }
108
109 $course_names[0] = 'none';
110
111 if (!empty($courses)) {
112
113 foreach ($courses as $course) {
114 $course_names[$course->context->id] = $course->fullname;
115 $modinfo = get_fast_modinfo($course, $USER->id);
116 $course_context_path = $DB->get_field('context', 'path', array('id' => $course->context->id));
117
118 foreach ($modinfo->instances as $modname => $instances) {
119
120 foreach ($instances as $modid => $mod) {
121 $mod_context_id = $DB->get_field_select('context', 'id',
122 'instanceid = '.$mod->id.' AND ' .
123 'contextlevel = ' . CONTEXT_MODULE . ' AND ' .
124 'path LIKE \''.$course_context_path.'/%\'');
125
126 $mod_string = $mod->name . ' (' . get_plugin_name($modname) . ')';
127 $this->modnames[$course->context->id][$mod_context_id] = $mod_string;
128 $allmodnames[$mod_context_id] = $course->shortname . " - " . $mod_string;
129 }
130 }
131 }
132 }
133 $mform->addElement('select', 'courseassoc', get_string('course'), $course_names, 'onchange="addCourseAssociations()"');
134 $selectassoc = &$mform->addElement('select', 'modassoc', get_string('managemodules'), $allmodnames);
135 $selectassoc->setMultiple(true);
136 $PAGE->requires->data_for_js('blog_edit_form_modnames', $this->modnames);
137
138 }
139
f07b9627 140 $this->add_action_buttons();
141
142 $mform->addElement('hidden', 'action');
2ee60b49 143 $mform->setType('action', PARAM_ACTION);
144 $mform->setDefault('action', '');
145
146 $mform->addElement('hidden', 'courseid');
147 $mform->setType('courseid', PARAM_INT);
148
0a941490 149 $mform->addElement('hidden', 'entryid');
150 $mform->setType('entryid', PARAM_INT);
151 $mform->setDefault('entryid', $entryid);
2ee60b49 152
cae83708 153 $mform->addElement('hidden', 'modid');
154 $mform->setType('modid', PARAM_INT);
155 $mform->setDefault('modid', 0);
156
157 $mform->addElement('hidden', 'courseid');
158 $mform->setType('courseid', PARAM_INT);
159 $mform->setDefault('courseid', 0);
160
161 if (!empty($assignmentdata)) { //dont allow associations for blog assignments
162 $courseassoc = $mform->getElement('courseassoc');
163 $modassoc = $mform->getElement('modassoc');
164 $courseassoc->updateAttributes(array('disabled' => 'disabled'));
165 $modassoc->updateAttributes(array('disabled' => 'disabled'));
166 }
167
168 if ($noedit) { //disable some other fields when editing is not allowed
169 $subject = $mform->getElement('subject');
170 $summary = $mform->getElement('summary');
171 $attachment = $mform->getElement('attachment');
172 $format = $mform->getElement('format');
173 $attachment->updateAttributes(array('disabled' => 'disabled'));
174 $format->updateAttributes(array('disabled' => 'disabled'));
175 }
176
177 $this->set_data($existing);
178 }
179
180 function validation($data, $files) {
181 global $CFG, $DB, $USER;
182
183 $errors = array();
184
185 //check to see if it's part of a submitted blog assignment
186 if ($blogassignment = $DB->get_record_sql('SELECT a.timedue, a.preventlate, a.emailteachers, a.var2, asub.grade
187 FROM {assignment} a, {assignment_submissions} as asub WHERE
188 a.id = asub.assignment AND userid = '.$USER->id.' AND a.assignmenttype = \'blog\'
0a941490 189 AND asub.data1 = \''.$data['entryid'].'\'')) {
cae83708 190
0a941490 191 $original = $DB->get_record('post', array('id' => $data['entryid']));
cae83708 192 //don't allow updates of the summary, subject, or attachment
193 $changed = ($original->summary != $data['summary'] ||
194 $original->subject != $data['subject'] ||
195 !empty($files));
196
197
198 //determine numeric value for publish state (for comparison purposes)
199 $postaccess = -1;
200 $i=0;
201
202 foreach (blog_applicable_publish_states() as $state => $desc) {
203 if ($state == $data['publishstate']) {
204 $postaccess = $i;
205 }
206 $publishstates[$i++] = $state;
207 }
208
209 //send an error if improper changes are being made
210 if (($changed and time() > $blogassignment->timedue and $blogassignment->preventlate = 1) or
211 ($changed and $blogassignment->grade != -1) or
212 (time() < $blogassignment->timedue and ($postaccess > $blogassignment->var2 || $postaccess == -1))) {
213
214 //too late to edit this entry
215 if ($original->subject != $data['subject']) {
216 $errors['subject'] = get_string('canteditblogassignment', 'blog');
217 }
218 if ($original->summary != $data['summary']) {
219 $errors['summary'] = get_string('canteditblogassignment', 'blog');
220 }
221 if (!empty($files)) {
222 $errors['attachment'] = get_string('canteditblogassignment', 'blog');
223 }
224 }
225
226 //insure the publishto value is within proper constraints
227
228 if (time() < $blogassignment->timedue and ($postaccess > $blogassignment->var2 || $postaccess == -1)) {
229 $errors['publishto'] = get_string('canteditblogassignment', 'blog');
230 }
231
232 } else {
233 if (empty($data['courseassoc']) && ($data['publishstate'] == 'course' || $data['publishstate'] == 'group') && !empty($CFG->useblogassociations)) {
234 return array('publishstate' => get_string('mustassociatecourse', 'blog'));
235 }
236 }
237
238
239 //validate course association
240 if (!empty($data['courseassoc'])) {
241 $coursecontext = $DB->get_record('context', array('id' => $data['courseassoc'], 'contextlevel' => CONTEXT_COURSE));
242
243 if ($coursecontext) { //insure associated course has a valid context id
244 //insure the user has access to this course
245
246 if (!has_capability('moodle/course:view', $coursecontext, $USER->id)) {
247 $errors['courseassoc'] = get_string('studentnotallowed', '', fullname($USER, true));
248 }
249 } else {
250 $errors['courseassoc'] = get_string('invalidcontextid', 'blog');
251 }
252 }
253
254 //validate mod associations
255 if (!empty($data['modassoc'])) {
256 //insure mods are valid
257
258 foreach ($data['modassoc'] as $modid) {
259 $modcontext = $DB->get_record('context', array('id' => $modid, 'contextlevel' => CONTEXT_MODULE));
260
261 if ($modcontext) { //insure associated mod has a valid context id
262 //get context of the mod's course
263 $path = split('/', $modcontext->path);
264 $coursecontext = $DB->get_record('context', array('id' => $path[3]));
265
266 //insure only one course is associated
267 if (!empty($data['courseassoc'])) {
268 if ($data['courseassoc'] != $coursecontext->id) {
269 $errors['modassoc'] = get_string('onlyassociateonecourse', 'blog');
270 }
271 } else {
272 $data['courseassoc'] = $coursecontext->id;
273 }
274
275 //insure the user has access to each mod's course
276 if (!has_capability('moodle/course:view', $coursecontext)) {
277 $errors['modassoc'] = get_string('studentnotallowed', '', fullname($USER, true));
278 }
279 } else {
280 $errors['modassoc'] = get_string('invalidcontextid', 'blog');
281 }
282 }
283 }
284
285 if ($errors) {
286 return $errors;
287 }
288 return true;
2ee60b49 289 }
290
291 /**
292 * This function sets up options of otag select element. This is called from definition and also
293 * after adding new official tags with the add tag button.
294 *
295 */
296 function otags_select_setup(){
47a2c30b 297 global $DB;
45df7de3 298
2ee60b49 299 $mform =& $this->_form;
bd1f4559 300 if ($otagsselect =& $mform->getElement('otags')) {
301 $otagsselect->removeOptions();
302 }
4c7fa6ce 303 $namefield = empty($CFG->keeptagnamecase) ? 'name' : 'rawname';
47a2c30b 304 if ($otags = $DB->get_records_sql_menu("SELECT id, $namefield FROM {tag} WHERE tagtype='official' ORDER by $namefield ASC")) {
f07b9627 305 $otagsselect->loadArray($otags);
306 }
47a2c30b 307
2ee60b49 308 }
f07b9627 309
310}