3 // This file is part of Moodle - http://moodle.org/
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.
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() {
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('format', 'summaryformat', get_string('format'));
52 $mform->addElement('filemanager', 'attachment_filemanager', get_string('attachment', 'forum'), null, $attachmentoptions);
54 //disable publishstate options that are not allowed
55 $publishstates = array();
58 foreach (blog_entry::get_applicable_publish_states() as $state => $desc) {
59 $publishstates[$state] = $desc; //no maximum was set
63 $mform->addElement('select', 'publishstate', get_string('publishto', 'blog'), $publishstates);
64 $mform->setHelpButton('publishstate', array('publish_state', get_string('publishto', 'blog'), 'blog'));
65 $mform->setDefault('publishstate', 0);
67 if (!empty($CFG->usetags)) {
68 $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
69 $mform->addElement('tags', 'tags', get_string('tags'));
72 $allmodnames = array();
74 if (!empty($CFG->useblogassociations)) {
75 if ((!empty($entry->courseassoc) || (!empty($courseid) && empty($modid))) && has_capability('moodle/blog:associatecourse', $sitecontext)) {
76 if (!empty($courseid)) {
77 $course = $DB->get_record('course', array('id' => $courseid));
78 $mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
79 $context = get_context_instance(CONTEXT_COURSE, $courseid);
80 $a->coursename = $course->fullname;
81 $contextid = $context->id;
83 $sql = 'SELECT fullname FROM {course} cr LEFT JOIN {context} ct ON ct.instanceid = cr.id WHERE ct.id = ?';
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)) {
92 $mod = get_coursemodule_from_id(false, $modid);
93 $a->modtype = get_string('modulename', $mod->modname);
94 $a->modname = $mod->name;
95 $context = get_context_instance(CONTEXT_MODULE, $modid);
97 $context = $DB->get_record('context', array('id' => $entry->modassoc));
98 $cm = $DB->get_record('course_modules', array('id' => $context->instanceid));
99 $a->modtype = $DB->get_field('modules', 'name', array('id' => $cm->module));
100 $a->modname = $DB->get_field($a->modtype, 'name', array('id' => $cm->instance));
103 $mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
104 $mform->addElement('advcheckbox', 'modassoc', get_string('associatewithmodule', 'blog', $a), null, null, array(0, $context->id));
105 $mform->setDefault('modassoc', $context->id);
109 $this->add_action_buttons();
110 $mform->addElement('hidden', 'action');
111 $mform->setType('action', PARAM_ACTION);
112 $mform->setDefault('action', '');
114 $mform->addElement('hidden', 'entryid');
115 $mform->setType('entryid', PARAM_INT);
116 $mform->setDefault('entryid', $entry->id);
118 $mform->addElement('hidden', 'modid');
119 $mform->setType('modid', PARAM_INT);
120 $mform->setDefault('modid', $modid);
122 $mform->addElement('hidden', 'courseid');
123 $mform->setType('courseid', PARAM_INT);
124 $mform->setDefault('courseid', $courseid);
127 function validation($data, $files) {
128 global $CFG, $DB, $USER;
131 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
133 // validate course association
134 if (!empty($data['courseassoc']) && has_capability('moodle/blog:associatecourse', $sitecontext)) {
135 $coursecontext = $DB->get_record('context', array('id' => $data['courseassoc'], 'contextlevel' => CONTEXT_COURSE));
137 if ($coursecontext) {
138 if (!is_enrolled($coursecontext) and !is_viewing($coursecontext)) {
139 $errors['courseassoc'] = get_string('studentnotallowed', '', fullname($USER, true));
142 $errors['courseassoc'] = get_string('invalidcontextid', 'blog');
146 // validate mod association
147 if (!empty($data['modassoc'])) {
148 $modcontextid = $data['modassoc'];
150 $modcontext = $DB->get_record('context', array('id' => $modcontextid, 'contextlevel' => CONTEXT_MODULE));
153 // get context of the mod's course
154 $path = split('/', $modcontext->path);
155 $coursecontext = $DB->get_record('context', array('id' => $path[(count($path) - 2)]));
157 // ensure only one course is associated
158 if (!empty($data['courseassoc'])) {
159 if ($data['courseassoc'] != $coursecontext->id) {
160 $errors['modassoc'] = get_string('onlyassociateonecourse', 'blog');
163 $data['courseassoc'] = $coursecontext->id;
166 // ensure the user has access to each mod's course
167 if (!is_enrolled($modcontext) and !is_viewing($modcontext)) {
168 $errors['modassoc'] = get_string('studentnotallowed', '', fullname($USER, true));
171 $errors['modassoc'] = get_string('invalidcontextid', 'blog');