Merge branch 'MDL-35147_22' of git://github.com/timhunt/moodle into MOODLE_22_STABLE
[moodle.git] / blog / edit_form.php
blob55ce91dc338b1034b61bc20d31b717526add68ab
1 <?php
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.
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/>.
18 if (!defined('MOODLE_INTERNAL')) {
19 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
22 require_once($CFG->libdir.'/formslib.php');
24 class blog_edit_form extends moodleform {
25 public $modnames = array();
27 function definition() {
28 global $CFG, $DB;
30 $mform =& $this->_form;
32 $entry = $this->_customdata['entry'];
33 $courseid = $this->_customdata['courseid'];
34 $modid = $this->_customdata['modid'];
35 $summaryoptions = $this->_customdata['summaryoptions'];
36 $attachmentoptions = $this->_customdata['attachmentoptions'];
37 $sitecontext = $this->_customdata['sitecontext'];
39 $mform->addElement('header', 'general', get_string('general', 'form'));
41 $mform->addElement('text', 'subject', get_string('entrytitle', 'blog'), array('size' => 60, 'maxlength' => 128));
42 $mform->addElement('editor', 'summary_editor', get_string('entrybody', 'blog'), null, $summaryoptions);
44 $mform->setType('subject', PARAM_TEXT);
45 $mform->addRule('subject', get_string('emptytitle', 'blog'), 'required', null, 'client');
46 $mform->addRule('subject', get_string('maximumchars', '', 128), 'maxlength', 128, 'client');
48 $mform->setType('summary_editor', PARAM_RAW);
49 $mform->addRule('summary_editor', get_string('emptybody', 'blog'), 'required', null, 'client');
51 $mform->addElement('filemanager', 'attachment_filemanager', get_string('attachment', 'forum'), null, $attachmentoptions);
53 //disable publishstate options that are not allowed
54 $publishstates = array();
55 $i = 0;
57 foreach (blog_entry::get_applicable_publish_states() as $state => $desc) {
58 $publishstates[$state] = $desc; //no maximum was set
59 $i++;
62 $mform->addElement('select', 'publishstate', get_string('publishto', 'blog'), $publishstates);
63 $mform->addHelpButton('publishstate', 'publishto', 'blog');
64 $mform->setDefault('publishstate', 0);
66 if (!empty($CFG->usetags)) {
67 $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
68 $mform->addElement('tags', 'tags', get_string('tags'));
71 $allmodnames = array();
73 if (!empty($CFG->useblogassociations)) {
74 if ((!empty($entry->courseassoc) || (!empty($courseid) && empty($modid))) && has_capability('moodle/blog:associatecourse', $sitecontext)) {
75 if (!empty($courseid)) {
76 $course = $DB->get_record('course', array('id' => $courseid));
77 $mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
78 $context = get_context_instance(CONTEXT_COURSE, $courseid);
79 $a = new stdClass();
80 $a->coursename = format_string($course->fullname, true, array('context' => $context));
81 $contextid = $context->id;
82 } else {
83 $sql = 'SELECT fullname FROM {course} cr LEFT JOIN {context} ct ON ct.instanceid = cr.id WHERE ct.id = ?';
84 $a = new stdClass();
85 $a->coursename = $DB->get_field_sql($sql, array($entry->courseassoc));
86 $contextid = $entry->courseassoc;
89 $mform->addElement('advcheckbox', 'courseassoc', get_string('associatewithcourse', 'blog', $a), null, null, array(0, $contextid));
90 $mform->setDefault('courseassoc', $contextid);
91 } else if ((!empty($entry->modassoc) || !empty($modid)) && has_capability('moodle/blog:associatemodule', $sitecontext)) {
92 if (!empty($modid)) {
93 $mod = get_coursemodule_from_id(false, $modid);
94 $a = new stdClass();
95 $a->modtype = get_string('modulename', $mod->modname);
96 $a->modname = $mod->name;
97 $context = get_context_instance(CONTEXT_MODULE, $modid);
98 } else {
99 $context = get_context_instance_by_id($entry->modassoc);
100 $cm = $DB->get_record('course_modules', array('id' => $context->instanceid));
101 $a = new stdClass();
102 $a->modtype = $DB->get_field('modules', 'name', array('id' => $cm->module));
103 $a->modname = $DB->get_field($a->modtype, 'name', array('id' => $cm->instance));
104 $modid = $context->instanceid;
107 $mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
108 $mform->addElement('advcheckbox', 'modassoc', get_string('associatewithmodule', 'blog', $a), null, null, array(0, $context->id));
109 $mform->setDefault('modassoc', $context->id);
113 $this->add_action_buttons();
114 $mform->addElement('hidden', 'action');
115 $mform->setType('action', PARAM_ACTION);
116 $mform->setDefault('action', '');
118 $mform->addElement('hidden', 'entryid');
119 $mform->setType('entryid', PARAM_INT);
120 $mform->setDefault('entryid', $entry->id);
122 $mform->addElement('hidden', 'modid');
123 $mform->setType('modid', PARAM_INT);
124 $mform->setDefault('modid', $modid);
126 $mform->addElement('hidden', 'courseid');
127 $mform->setType('courseid', PARAM_INT);
128 $mform->setDefault('courseid', $courseid);
131 function validation($data, $files) {
132 global $CFG, $DB, $USER;
134 $errors = array();
135 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
137 // validate course association
138 if (!empty($data['courseassoc']) && has_capability('moodle/blog:associatecourse', $sitecontext)) {
139 $coursecontext = context::instance_by_id($data['courseassoc'], IGNORE_MISSING);
141 if ($coursecontext and $coursecontext->contextlevel == CONTEXT_COURSE) {
142 if (!is_enrolled($coursecontext) and !is_viewing($coursecontext)) {
143 $errors['courseassoc'] = get_string('studentnotallowed', '', fullname($USER, true));
145 } else {
146 $errors['courseassoc'] = get_string('error');
150 // validate mod association
151 if (!empty($data['modassoc'])) {
152 $modcontextid = $data['modassoc'];
153 $modcontext = context::instance_by_id($modcontextid, IGNORE_MISSING);
155 if ($modcontext and $modcontext->contextlevel == CONTEXT_MODULE) {
156 // get context of the mod's course
157 $coursecontext = $modcontext->get_course_context(true);
159 // ensure only one course is associated
160 if (!empty($data['courseassoc'])) {
161 if ($data['courseassoc'] != $coursecontext->id) {
162 $errors['modassoc'] = get_string('onlyassociateonecourse', 'blog');
164 } else {
165 $data['courseassoc'] = $coursecontext->id;
168 // ensure the user has access to each mod's course
169 if (!is_enrolled($modcontext) and !is_viewing($modcontext)) {
170 $errors['modassoc'] = get_string('studentnotallowed', '', fullname($USER, true));
172 } else {
173 $errors['modassoc'] = get_string('error');
177 if ($errors) {
178 return $errors;
180 return true;