Moodle release 3.3rc1
[moodle.git] / blog / edit_form.php
blob7060c932b5a5d3a1d2a24e754a5c0ec88b7f4b6f
1 <?php
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.
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/>.
17 if (!defined('MOODLE_INTERNAL')) {
18 die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
21 require_once($CFG->libdir.'/formslib.php');
23 class blog_edit_form extends moodleform {
24 public $modnames = array();
26 /**
27 * Blog form definition.
29 public function definition() {
30 global $CFG, $DB;
32 $mform =& $this->_form;
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'];
39 $sitecontext = $this->_customdata['sitecontext'];
41 $mform->addElement('header', 'general', get_string('general', 'form'));
43 $mform->addElement('text', 'subject', get_string('entrytitle', 'blog'), array('size' => 60, 'maxlength' => 128));
44 $mform->addElement('editor', 'summary_editor', get_string('entrybody', 'blog'), null, $summaryoptions);
46 $mform->setType('subject', PARAM_TEXT);
47 $mform->addRule('subject', get_string('emptytitle', 'blog'), 'required', null, 'client');
48 $mform->addRule('subject', get_string('maximumchars', '', 128), 'maxlength', 128, 'client');
50 $mform->setType('summary_editor', PARAM_RAW);
51 $mform->addRule('summary_editor', get_string('emptybody', 'blog'), 'required', null, 'client');
53 $mform->addElement('filemanager', 'attachment_filemanager', get_string('attachment', 'forum'), null, $attachmentoptions);
55 // Disable publishstate options that are not allowed.
56 $publishstates = array();
57 $i = 0;
59 foreach (blog_entry::get_applicable_publish_states() as $state => $desc) {
60 $publishstates[$state] = $desc; // No maximum was set.
61 $i++;
64 $mform->addElement('select', 'publishstate', get_string('publishto', 'blog'), $publishstates);
65 $mform->addHelpButton('publishstate', 'publishto', 'blog');
66 $mform->setDefault('publishstate', 0);
68 if (core_tag_tag::is_enabled('core', 'post')) {
69 $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
71 $mform->addElement('tags', 'tags', get_string('tags'),
72 array('itemtype' => 'post', 'component' => 'core'));
74 $allmodnames = array();
76 if (!empty($CFG->useblogassociations)) {
77 if ((!empty($entry->courseassoc) || (!empty($courseid) && empty($modid)))) {
78 if (!empty($courseid)) {
79 $course = $DB->get_record('course', array('id' => $courseid));
80 $context = context_course::instance($courseid);
81 $a = new stdClass();
82 $a->coursename = format_string($course->fullname, true, array('context' => $context));
83 $contextid = $context->id;
84 } else {
85 $context = context::instance_by_id($entry->courseassoc);
86 $sql = 'SELECT fullname FROM {course} cr LEFT JOIN {context} ct ON ct.instanceid = cr.id WHERE ct.id = ?';
87 $a = new stdClass();
88 $a->coursename = $DB->get_field_sql($sql, array($entry->courseassoc));
89 $contextid = $entry->courseassoc;
92 $mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
93 $mform->addElement('advcheckbox',
94 'courseassoc',
95 get_string('associatewithcourse', 'blog', $a),
96 null,
97 null,
98 array(0, $contextid));
99 $mform->setDefault('courseassoc', $contextid);
101 } else if ((!empty($entry->modassoc) || !empty($modid))) {
102 if (!empty($modid)) {
103 $mod = get_coursemodule_from_id(false, $modid);
104 $a = new stdClass();
105 $a->modtype = get_string('modulename', $mod->modname);
106 $a->modname = $mod->name;
107 $context = context_module::instance($modid);
108 } else {
109 $context = context::instance_by_id($entry->modassoc);
110 $cm = $DB->get_record('course_modules', array('id' => $context->instanceid));
111 $a = new stdClass();
112 $a->modtype = $DB->get_field('modules', 'name', array('id' => $cm->module));
113 $a->modname = $DB->get_field($a->modtype, 'name', array('id' => $cm->instance));
114 $modid = $context->instanceid;
117 $mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
118 $mform->addElement('advcheckbox',
119 'modassoc',
120 get_string('associatewithmodule', 'blog', $a),
121 null,
122 null,
123 array(0, $context->id));
124 $mform->setDefault('modassoc', $context->id);
128 $this->add_action_buttons();
129 $mform->addElement('hidden', 'action');
130 $mform->setType('action', PARAM_ALPHANUMEXT);
131 $mform->setDefault('action', '');
133 $mform->addElement('hidden', 'entryid');
134 $mform->setType('entryid', PARAM_INT);
135 $mform->setDefault('entryid', $entry->id);
137 $mform->addElement('hidden', 'modid');
138 $mform->setType('modid', PARAM_INT);
139 $mform->setDefault('modid', $modid);
141 $mform->addElement('hidden', 'courseid');
142 $mform->setType('courseid', PARAM_INT);
143 $mform->setDefault('courseid', $courseid);
147 * Validate the blog form data.
148 * @param array $data Data to be validated
149 * @param array $files unused
150 * @return array|bool
152 public function validation($data, $files) {
153 global $CFG, $DB, $USER;
155 $errors = parent::validation($data, $files);
157 // Validate course association.
158 if (!empty($data['courseassoc'])) {
159 $coursecontext = context::instance_by_id($data['courseassoc']);
161 if ($coursecontext->contextlevel != CONTEXT_COURSE) {
162 $errors['courseassoc'] = get_string('error');
166 // Validate mod association.
167 if (!empty($data['modassoc'])) {
168 $modcontextid = $data['modassoc'];
169 $modcontext = context::instance_by_id($modcontextid);
171 if ($modcontext->contextlevel == CONTEXT_MODULE) {
172 // Get context of the mod's course.
173 $coursecontext = $modcontext->get_course_context(true);
175 // Ensure only one course is associated.
176 if (!empty($data['courseassoc'])) {
177 if ($data['courseassoc'] != $coursecontext->id) {
178 $errors['modassoc'] = get_string('onlyassociateonecourse', 'blog');
180 } else {
181 $data['courseassoc'] = $coursecontext->id;
183 } else {
184 $errors['modassoc'] = get_string('error');
188 if ($errors) {
189 return $errors;
191 return true;