MDL-46350 blog: call parent validation
[moodle.git] / blog / edit_form.php
CommitLineData
cae83708 1<?php
cae83708 2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
f07b9627 16
bfebaf64 17if (!defined('MOODLE_INTERNAL')) {
2b6e53e8 18 die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
bfebaf64
MD
19}
20
f07b9627 21require_once($CFG->libdir.'/formslib.php');
22
23class blog_edit_form extends moodleform {
cae83708 24 public $modnames = array();
f07b9627 25
2b6e53e8
AD
26 /**
27 * Blog form definition.
28 */
29 public function definition() {
1c7b8b93 30 global $CFG, $DB;
45df7de3 31
1c7b8b93 32 $mform =& $this->_form;
f07b9627 33
1c7b8b93
NC
34 $entry = $this->_customdata['entry'];
35 $courseid = $this->_customdata['courseid'];
36 $modid = $this->_customdata['modid'];
37 $summaryoptions = $this->_customdata['summaryoptions'];
38 $attachmentoptions = $this->_customdata['attachmentoptions'];
f07b9627 39 $sitecontext = $this->_customdata['sitecontext'];
40
f07b9627 41 $mform->addElement('header', 'general', get_string('general', 'form'));
cae83708 42
3e0ce607 43 $mform->addElement('text', 'subject', get_string('entrytitle', 'blog'), array('size' => 60, 'maxlength' => 128));
1c7b8b93 44 $mform->addElement('editor', 'summary_editor', get_string('entrybody', 'blog'), null, $summaryoptions);
cae83708 45
2ee60b49 46 $mform->setType('subject', PARAM_TEXT);
47 $mform->addRule('subject', get_string('emptytitle', 'blog'), 'required', null, 'client');
3e0ce607 48 $mform->addRule('subject', get_string('maximumchars', '', 128), 'maxlength', 128, 'client');
f07b9627 49
1c7b8b93
NC
50 $mform->setType('summary_editor', PARAM_RAW);
51 $mform->addRule('summary_editor', get_string('emptybody', 'blog'), 'required', null, 'client');
f07b9627 52
1c7b8b93 53 $mform->addElement('filemanager', 'attachment_filemanager', get_string('attachment', 'forum'), null, $attachmentoptions);
f07b9627 54
2b6e53e8 55 // Disable publishstate options that are not allowed.
cae83708 56 $publishstates = array();
57 $i = 0;
58
59 foreach (blog_entry::get_applicable_publish_states() as $state => $desc) {
2b6e53e8 60 $publishstates[$state] = $desc; // No maximum was set.
cae83708 61 $i++;
62 }
63
64 $mform->addElement('select', 'publishstate', get_string('publishto', 'blog'), $publishstates);
2650f44d 65 $mform->addHelpButton('publishstate', 'publishto', 'blog');
1c7b8b93 66 $mform->setDefault('publishstate', 0);
93bbdd50 67
68 if (!empty($CFG->usetags)) {
3d535996 69 $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
fc23301c 70 $mform->addElement('tags', 'tags', get_string('tags'));
f07b9627 71 }
fc23301c 72
cae83708 73 $allmodnames = array();
74
75 if (!empty($CFG->useblogassociations)) {
1b30a9fa 76 if ((!empty($entry->courseassoc) || (!empty($courseid) && empty($modid)))) {
1c7b8b93
NC
77 if (!empty($courseid)) {
78 $course = $DB->get_record('course', array('id' => $courseid));
41b38360 79 $context = context_course::instance($courseid);
e463f508 80 $a = new stdClass();
91d284c1 81 $a->coursename = format_string($course->fullname, true, array('context' => $context));
1c7b8b93
NC
82 $contextid = $context->id;
83 } else {
1b30a9fa 84 $context = context::instance_by_id($entry->courseassoc);
1c7b8b93 85 $sql = 'SELECT fullname FROM {course} cr LEFT JOIN {context} ct ON ct.instanceid = cr.id WHERE ct.id = ?';
e463f508 86 $a = new stdClass();
1c7b8b93
NC
87 $a->coursename = $DB->get_field_sql($sql, array($entry->courseassoc));
88 $contextid = $entry->courseassoc;
89 }
cae83708 90
4ef08298 91 $mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
2b6e53e8
AD
92 $mform->addElement('advcheckbox',
93 'courseassoc',
94 get_string('associatewithcourse', 'blog', $a),
95 null,
96 null,
97 array(0, $contextid));
4ef08298 98 $mform->setDefault('courseassoc', $contextid);
1b30a9fa
DM
99
100 } else if ((!empty($entry->modassoc) || !empty($modid))) {
1c7b8b93
NC
101 if (!empty($modid)) {
102 $mod = get_coursemodule_from_id(false, $modid);
e463f508 103 $a = new stdClass();
1c7b8b93
NC
104 $a->modtype = get_string('modulename', $mod->modname);
105 $a->modname = $mod->name;
41b38360 106 $context = context_module::instance($modid);
1c7b8b93 107 } else {
41b38360 108 $context = context::instance_by_id($entry->modassoc);
1c7b8b93 109 $cm = $DB->get_record('course_modules', array('id' => $context->instanceid));
e463f508 110 $a = new stdClass();
1c7b8b93
NC
111 $a->modtype = $DB->get_field('modules', 'name', array('id' => $cm->module));
112 $a->modname = $DB->get_field($a->modtype, 'name', array('id' => $cm->instance));
432a6880 113 $modid = $context->instanceid;
cae83708 114 }
cae83708 115
4ef08298 116 $mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
2b6e53e8
AD
117 $mform->addElement('advcheckbox',
118 'modassoc',
119 get_string('associatewithmodule', 'blog', $a),
120 null,
121 null,
122 array(0, $context->id));
4ef08298 123 $mform->setDefault('modassoc', $context->id);
1c7b8b93 124 }
cae83708 125 }
126
f07b9627 127 $this->add_action_buttons();
f07b9627 128 $mform->addElement('hidden', 'action');
405aca35 129 $mform->setType('action', PARAM_ALPHANUMEXT);
2ee60b49 130 $mform->setDefault('action', '');
131
0a941490 132 $mform->addElement('hidden', 'entryid');
133 $mform->setType('entryid', PARAM_INT);
1c7b8b93 134 $mform->setDefault('entryid', $entry->id);
2ee60b49 135
cae83708 136 $mform->addElement('hidden', 'modid');
137 $mform->setType('modid', PARAM_INT);
1c7b8b93 138 $mform->setDefault('modid', $modid);
cae83708 139
140 $mform->addElement('hidden', 'courseid');
141 $mform->setType('courseid', PARAM_INT);
1c7b8b93 142 $mform->setDefault('courseid', $courseid);
cae83708 143 }
144
2b6e53e8
AD
145 /**
146 * Validate the blog form data.
2c3d6f24
SH
147 * @param array $data Data to be validated
148 * @param array $files unused
149 * @return array|bool
2b6e53e8
AD
150 */
151 public function validation($data, $files) {
cae83708 152 global $CFG, $DB, $USER;
153
f3f920d2 154 $errors = parent::validation($data, $files);
cae83708 155
2b6e53e8 156 // Validate course association.
1b30a9fa 157 if (!empty($data['courseassoc'])) {
68fc1cc2 158 $coursecontext = context::instance_by_id($data['courseassoc']);
cae83708 159
f42d2a22 160 if ($coursecontext->contextlevel != CONTEXT_COURSE) {
ff53a58b 161 $errors['courseassoc'] = get_string('error');
cae83708 162 }
163 }
164
2b6e53e8 165 // Validate mod association.
cae83708 166 if (!empty($data['modassoc'])) {
1c7b8b93 167 $modcontextid = $data['modassoc'];
68fc1cc2 168 $modcontext = context::instance_by_id($modcontextid);
cae83708 169
4ef08298 170 if ($modcontext->contextlevel == CONTEXT_MODULE) {
2b6e53e8 171 // Get context of the mod's course.
ff53a58b 172 $coursecontext = $modcontext->get_course_context(true);
1c7b8b93 173
2b6e53e8 174 // Ensure only one course is associated.
1c7b8b93
NC
175 if (!empty($data['courseassoc'])) {
176 if ($data['courseassoc'] != $coursecontext->id) {
177 $errors['modassoc'] = get_string('onlyassociateonecourse', 'blog');
cae83708 178 }
179 } else {
1c7b8b93
NC
180 $data['courseassoc'] = $coursecontext->id;
181 }
1c7b8b93 182 } else {
ff53a58b 183 $errors['modassoc'] = get_string('error');
cae83708 184 }
185 }
186
187 if ($errors) {
188 return $errors;
189 }
190 return true;
2ee60b49 191 }
f07b9627 192}