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