Merge branch 'master_MDL-65116' of https://github.com/yao9394/moodle
[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
abea2c5d 68 if (core_tag_tag::is_enabled('core', 'post')) {
3d535996 69 $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
f07b9627 70 }
abea2c5d
MG
71 $mform->addElement('tags', 'tags', get_string('tags'),
72 array('itemtype' => 'post', 'component' => 'core'));
fc23301c 73
cae83708 74 $allmodnames = array();
75
76 if (!empty($CFG->useblogassociations)) {
1b30a9fa 77 if ((!empty($entry->courseassoc) || (!empty($courseid) && empty($modid)))) {
1c7b8b93
NC
78 if (!empty($courseid)) {
79 $course = $DB->get_record('course', array('id' => $courseid));
41b38360 80 $context = context_course::instance($courseid);
e463f508 81 $a = new stdClass();
91d284c1 82 $a->coursename = format_string($course->fullname, true, array('context' => $context));
1c7b8b93
NC
83 $contextid = $context->id;
84 } else {
1b30a9fa 85 $context = context::instance_by_id($entry->courseassoc);
1c7b8b93 86 $sql = 'SELECT fullname FROM {course} cr LEFT JOIN {context} ct ON ct.instanceid = cr.id WHERE ct.id = ?';
e463f508 87 $a = new stdClass();
1c7b8b93
NC
88 $a->coursename = $DB->get_field_sql($sql, array($entry->courseassoc));
89 $contextid = $entry->courseassoc;
90 }
cae83708 91
4ef08298 92 $mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
2b6e53e8
AD
93 $mform->addElement('advcheckbox',
94 'courseassoc',
95 get_string('associatewithcourse', 'blog', $a),
96 null,
97 null,
98 array(0, $contextid));
4ef08298 99 $mform->setDefault('courseassoc', $contextid);
1b30a9fa
DM
100
101 } else if ((!empty($entry->modassoc) || !empty($modid))) {
1c7b8b93
NC
102 if (!empty($modid)) {
103 $mod = get_coursemodule_from_id(false, $modid);
e463f508 104 $a = new stdClass();
1c7b8b93
NC
105 $a->modtype = get_string('modulename', $mod->modname);
106 $a->modname = $mod->name;
41b38360 107 $context = context_module::instance($modid);
1c7b8b93 108 } else {
41b38360 109 $context = context::instance_by_id($entry->modassoc);
1c7b8b93 110 $cm = $DB->get_record('course_modules', array('id' => $context->instanceid));
e463f508 111 $a = new stdClass();
1c7b8b93
NC
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));
432a6880 114 $modid = $context->instanceid;
cae83708 115 }
cae83708 116
4ef08298 117 $mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
2b6e53e8
AD
118 $mform->addElement('advcheckbox',
119 'modassoc',
120 get_string('associatewithmodule', 'blog', $a),
121 null,
122 null,
123 array(0, $context->id));
4ef08298 124 $mform->setDefault('modassoc', $context->id);
1c7b8b93 125 }
cae83708 126 }
127
f07b9627 128 $this->add_action_buttons();
f07b9627 129 $mform->addElement('hidden', 'action');
405aca35 130 $mform->setType('action', PARAM_ALPHANUMEXT);
2ee60b49 131 $mform->setDefault('action', '');
132
0a941490 133 $mform->addElement('hidden', 'entryid');
134 $mform->setType('entryid', PARAM_INT);
1c7b8b93 135 $mform->setDefault('entryid', $entry->id);
2ee60b49 136
cae83708 137 $mform->addElement('hidden', 'modid');
138 $mform->setType('modid', PARAM_INT);
1c7b8b93 139 $mform->setDefault('modid', $modid);
cae83708 140
141 $mform->addElement('hidden', 'courseid');
142 $mform->setType('courseid', PARAM_INT);
1c7b8b93 143 $mform->setDefault('courseid', $courseid);
cae83708 144 }
145
2b6e53e8
AD
146 /**
147 * Validate the blog form data.
2c3d6f24
SH
148 * @param array $data Data to be validated
149 * @param array $files unused
150 * @return array|bool
2b6e53e8
AD
151 */
152 public function validation($data, $files) {
cae83708 153 global $CFG, $DB, $USER;
154
f3f920d2 155 $errors = parent::validation($data, $files);
cae83708 156
2b6e53e8 157 // Validate course association.
1b30a9fa 158 if (!empty($data['courseassoc'])) {
68fc1cc2 159 $coursecontext = context::instance_by_id($data['courseassoc']);
cae83708 160
f42d2a22 161 if ($coursecontext->contextlevel != CONTEXT_COURSE) {
ff53a58b 162 $errors['courseassoc'] = get_string('error');
cae83708 163 }
164 }
165
2b6e53e8 166 // Validate mod association.
cae83708 167 if (!empty($data['modassoc'])) {
1c7b8b93 168 $modcontextid = $data['modassoc'];
68fc1cc2 169 $modcontext = context::instance_by_id($modcontextid);
cae83708 170
4ef08298 171 if ($modcontext->contextlevel == CONTEXT_MODULE) {
2b6e53e8 172 // Get context of the mod's course.
ff53a58b 173 $coursecontext = $modcontext->get_course_context(true);
1c7b8b93 174
2b6e53e8 175 // Ensure only one course is associated.
1c7b8b93
NC
176 if (!empty($data['courseassoc'])) {
177 if ($data['courseassoc'] != $coursecontext->id) {
178 $errors['modassoc'] = get_string('onlyassociateonecourse', 'blog');
cae83708 179 }
180 } else {
1c7b8b93
NC
181 $data['courseassoc'] = $coursecontext->id;
182 }
1c7b8b93 183 } else {
ff53a58b 184 $errors['modassoc'] = get_string('error');
cae83708 185 }
186 }
187
188 if ($errors) {
189 return $errors;
190 }
191 return true;
2ee60b49 192 }
f07b9627 193}