Merge branch 'MDL-29821' of git://github.com/bostelm/moodle
[moodle.git] / blog / edit_form.php
blob04c67e5b4cc6bcd14c6bbb2621a13d1720c7e6d4
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'), 'size="60"');
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');
47 $mform->setType('summary_editor', PARAM_RAW);
48 $mform->addRule('summary_editor', get_string('emptybody', 'blog'), 'required', null, 'client');
50 $mform->addElement('filemanager', 'attachment_filemanager', get_string('attachment', 'forum'), null, $attachmentoptions);
52 //disable publishstate options that are not allowed
53 $publishstates = array();
54 $i = 0;
56 foreach (blog_entry::get_applicable_publish_states() as $state => $desc) {
57 $publishstates[$state] = $desc; //no maximum was set
58 $i++;
61 $mform->addElement('select', 'publishstate', get_string('publishto', 'blog'), $publishstates);
62 $mform->addHelpButton('publishstate', 'publishto', 'blog');
63 $mform->setDefault('publishstate', 0);
65 if (!empty($CFG->usetags)) {
66 $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
67 $mform->addElement('tags', 'tags', get_string('tags'));
70 $allmodnames = array();
72 if (!empty($CFG->useblogassociations)) {
73 if ((!empty($entry->courseassoc) || (!empty($courseid) && empty($modid))) && has_capability('moodle/blog:associatecourse', $sitecontext)) {
74 if (!empty($courseid)) {
75 $course = $DB->get_record('course', array('id' => $courseid));
76 $mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
77 $context = get_context_instance(CONTEXT_COURSE, $courseid);
78 $a = new stdClass();
79 $a->coursename = format_string($course->fullname, true, array('context' => $context));
80 $contextid = $context->id;
81 } else {
82 $sql = 'SELECT fullname FROM {course} cr LEFT JOIN {context} ct ON ct.instanceid = cr.id WHERE ct.id = ?';
83 $a = new stdClass();
84 $a->coursename = $DB->get_field_sql($sql, array($entry->courseassoc));
85 $contextid = $entry->courseassoc;
88 $mform->addElement('advcheckbox', 'courseassoc', get_string('associatewithcourse', 'blog', $a), null, null, array(0, $contextid));
89 $mform->setDefault('courseassoc', $contextid);
90 } else if ((!empty($entry->modassoc) || !empty($modid)) && has_capability('moodle/blog:associatemodule', $sitecontext)) {
91 if (!empty($modid)) {
92 $mod = get_coursemodule_from_id(false, $modid);
93 $a = new stdClass();
94 $a->modtype = get_string('modulename', $mod->modname);
95 $a->modname = $mod->name;
96 $context = get_context_instance(CONTEXT_MODULE, $modid);
97 } else {
98 $context = get_context_instance_by_id($entry->modassoc);
99 $cm = $DB->get_record('course_modules', array('id' => $context->instanceid));
100 $a = new stdClass();
101 $a->modtype = $DB->get_field('modules', 'name', array('id' => $cm->module));
102 $a->modname = $DB->get_field($a->modtype, 'name', array('id' => $cm->instance));
105 $mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
106 $mform->addElement('advcheckbox', 'modassoc', get_string('associatewithmodule', 'blog', $a), null, null, array(0, $context->id));
107 $mform->setDefault('modassoc', $context->id);
111 $this->add_action_buttons();
112 $mform->addElement('hidden', 'action');
113 $mform->setType('action', PARAM_ACTION);
114 $mform->setDefault('action', '');
116 $mform->addElement('hidden', 'entryid');
117 $mform->setType('entryid', PARAM_INT);
118 $mform->setDefault('entryid', $entry->id);
120 $mform->addElement('hidden', 'modid');
121 $mform->setType('modid', PARAM_INT);
122 $mform->setDefault('modid', $modid);
124 $mform->addElement('hidden', 'courseid');
125 $mform->setType('courseid', PARAM_INT);
126 $mform->setDefault('courseid', $courseid);
129 function validation($data, $files) {
130 global $CFG, $DB, $USER;
132 $errors = array();
133 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
135 // validate course association
136 if (!empty($data['courseassoc']) && has_capability('moodle/blog:associatecourse', $sitecontext)) {
137 $coursecontext = get_context_instance(CONTEXT_COURSE, $data['courseassoc']);
139 if ($coursecontext) {
140 if (!is_enrolled($coursecontext) and !is_viewing($coursecontext)) {
141 $errors['courseassoc'] = get_string('studentnotallowed', '', fullname($USER, true));
143 } else {
144 $errors['courseassoc'] = get_string('invalidcontextid', 'blog');
148 // validate mod association
149 if (!empty($data['modassoc'])) {
150 $modcontextid = $data['modassoc'];
152 $modcontext = get_context_instance(CONTEXT_MODULE, $modcontextid);
154 if ($modcontext) {
155 // get context of the mod's course
156 $path = explode('/', $modcontext->path);
157 $coursecontext = get_context_instance_by_id($path[(count($path) - 2)]);
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('invalidcontextid', 'blog');
177 if ($errors) {
178 return $errors;
180 return true;