MDL-27814 blogs: Deprecate blog_get_context_url()
[moodle.git] / blog / edit_form.php
CommitLineData
cae83708 1<?php
2
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.
14//
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/>.
f07b9627 17
bfebaf64
MD
18if (!defined('MOODLE_INTERNAL')) {
19 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
20}
21
f07b9627 22require_once($CFG->libdir.'/formslib.php');
23
24class blog_edit_form extends moodleform {
cae83708 25 public $modnames = array();
f07b9627 26
2ee60b49 27 function definition() {
1c7b8b93 28 global $CFG, $DB;
45df7de3 29
1c7b8b93 30 $mform =& $this->_form;
f07b9627 31
1c7b8b93
NC
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'];
f07b9627 37 $sitecontext = $this->_customdata['sitecontext'];
38
f07b9627 39 $mform->addElement('header', 'general', get_string('general', 'form'));
cae83708 40
3e0ce607 41 $mform->addElement('text', 'subject', get_string('entrytitle', 'blog'), array('size' => 60, 'maxlength' => 128));
1c7b8b93 42 $mform->addElement('editor', 'summary_editor', get_string('entrybody', 'blog'), null, $summaryoptions);
cae83708 43
2ee60b49 44 $mform->setType('subject', PARAM_TEXT);
45 $mform->addRule('subject', get_string('emptytitle', 'blog'), 'required', null, 'client');
3e0ce607 46 $mform->addRule('subject', get_string('maximumchars', '', 128), 'maxlength', 128, 'client');
f07b9627 47
1c7b8b93
NC
48 $mform->setType('summary_editor', PARAM_RAW);
49 $mform->addRule('summary_editor', get_string('emptybody', 'blog'), 'required', null, 'client');
f07b9627 50
1c7b8b93 51 $mform->addElement('filemanager', 'attachment_filemanager', get_string('attachment', 'forum'), null, $attachmentoptions);
f07b9627 52
cae83708 53 //disable publishstate options that are not allowed
54 $publishstates = array();
55 $i = 0;
56
57 foreach (blog_entry::get_applicable_publish_states() as $state => $desc) {
5f4d4d80 58 $publishstates[$state] = $desc; //no maximum was set
cae83708 59 $i++;
60 }
61
62 $mform->addElement('select', 'publishstate', get_string('publishto', 'blog'), $publishstates);
2650f44d 63 $mform->addHelpButton('publishstate', 'publishto', 'blog');
1c7b8b93 64 $mform->setDefault('publishstate', 0);
93bbdd50 65
66 if (!empty($CFG->usetags)) {
3d535996 67 $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
fc23301c 68 $mform->addElement('tags', 'tags', get_string('tags'));
f07b9627 69 }
fc23301c 70
cae83708 71 $allmodnames = array();
72
73 if (!empty($CFG->useblogassociations)) {
1b30a9fa 74 if ((!empty($entry->courseassoc) || (!empty($courseid) && empty($modid)))) {
1c7b8b93
NC
75 if (!empty($courseid)) {
76 $course = $DB->get_record('course', array('id' => $courseid));
41b38360 77 $context = context_course::instance($courseid);
e463f508 78 $a = new stdClass();
91d284c1 79 $a->coursename = format_string($course->fullname, true, array('context' => $context));
1c7b8b93
NC
80 $contextid = $context->id;
81 } else {
1b30a9fa 82 $context = context::instance_by_id($entry->courseassoc);
1c7b8b93 83 $sql = 'SELECT fullname FROM {course} cr LEFT JOIN {context} ct ON ct.instanceid = cr.id WHERE ct.id = ?';
e463f508 84 $a = new stdClass();
1c7b8b93
NC
85 $a->coursename = $DB->get_field_sql($sql, array($entry->courseassoc));
86 $contextid = $entry->courseassoc;
87 }
cae83708 88
1b30a9fa 89 if (has_capability('moodle/blog:associatecourse', $context)) {
9e7e1b1c
EL
90 $mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
91 $mform->addElement('advcheckbox', 'courseassoc', get_string('associatewithcourse', 'blog', $a), null, null, array(0, $contextid));
1b30a9fa
DM
92 $mform->setDefault('courseassoc', $contextid);
93 }
94
95 } else if ((!empty($entry->modassoc) || !empty($modid))) {
1c7b8b93
NC
96 if (!empty($modid)) {
97 $mod = get_coursemodule_from_id(false, $modid);
e463f508 98 $a = new stdClass();
1c7b8b93
NC
99 $a->modtype = get_string('modulename', $mod->modname);
100 $a->modname = $mod->name;
41b38360 101 $context = context_module::instance($modid);
1c7b8b93 102 } else {
41b38360 103 $context = context::instance_by_id($entry->modassoc);
1c7b8b93 104 $cm = $DB->get_record('course_modules', array('id' => $context->instanceid));
e463f508 105 $a = new stdClass();
1c7b8b93
NC
106 $a->modtype = $DB->get_field('modules', 'name', array('id' => $cm->module));
107 $a->modname = $DB->get_field($a->modtype, 'name', array('id' => $cm->instance));
432a6880 108 $modid = $context->instanceid;
cae83708 109 }
cae83708 110
1b30a9fa
DM
111 if (has_capability('moodle/blog:associatemodule', $context)) {
112 $mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
113 $mform->addElement('advcheckbox', 'modassoc', get_string('associatewithmodule', 'blog', $a), null, null, array(0, $context->id));
114 $mform->setDefault('modassoc', $context->id);
115 }
1c7b8b93 116 }
cae83708 117 }
118
f07b9627 119 $this->add_action_buttons();
f07b9627 120 $mform->addElement('hidden', 'action');
405aca35 121 $mform->setType('action', PARAM_ALPHANUMEXT);
2ee60b49 122 $mform->setDefault('action', '');
123
0a941490 124 $mform->addElement('hidden', 'entryid');
125 $mform->setType('entryid', PARAM_INT);
1c7b8b93 126 $mform->setDefault('entryid', $entry->id);
2ee60b49 127
cae83708 128 $mform->addElement('hidden', 'modid');
129 $mform->setType('modid', PARAM_INT);
1c7b8b93 130 $mform->setDefault('modid', $modid);
cae83708 131
132 $mform->addElement('hidden', 'courseid');
133 $mform->setType('courseid', PARAM_INT);
1c7b8b93 134 $mform->setDefault('courseid', $courseid);
cae83708 135 }
136
137 function validation($data, $files) {
138 global $CFG, $DB, $USER;
139
140 $errors = array();
141
1c7b8b93 142 // validate course association
1b30a9fa 143 if (!empty($data['courseassoc'])) {
68fc1cc2 144 $coursecontext = context::instance_by_id($data['courseassoc']);
cae83708 145
1b30a9fa
DM
146 $canassociatecourse = has_capability('moodle/blog:associatecourse', $coursecontext);
147 if ($coursecontext->contextlevel == CONTEXT_COURSE && $canassociatecourse) {
4f0c2d00 148 if (!is_enrolled($coursecontext) and !is_viewing($coursecontext)) {
cae83708 149 $errors['courseassoc'] = get_string('studentnotallowed', '', fullname($USER, true));
150 }
151 } else {
ff53a58b 152 $errors['courseassoc'] = get_string('error');
cae83708 153 }
154 }
155
1c7b8b93 156 // validate mod association
cae83708 157 if (!empty($data['modassoc'])) {
1c7b8b93 158 $modcontextid = $data['modassoc'];
68fc1cc2 159 $modcontext = context::instance_by_id($modcontextid);
cae83708 160
207b6fc5
DM
161 $canassociatemodule = has_capability('moodle/blog:associatemodule', $modcontext);
162 if ($canassociatemodule) {
1c7b8b93 163 // get context of the mod's course
ff53a58b 164 $coursecontext = $modcontext->get_course_context(true);
1c7b8b93
NC
165
166 // ensure only one course is associated
167 if (!empty($data['courseassoc'])) {
168 if ($data['courseassoc'] != $coursecontext->id) {
169 $errors['modassoc'] = get_string('onlyassociateonecourse', 'blog');
cae83708 170 }
171 } else {
1c7b8b93
NC
172 $data['courseassoc'] = $coursecontext->id;
173 }
174
175 // ensure the user has access to each mod's course
4f0c2d00 176 if (!is_enrolled($modcontext) and !is_viewing($modcontext)) {
1c7b8b93 177 $errors['modassoc'] = get_string('studentnotallowed', '', fullname($USER, true));
cae83708 178 }
1c7b8b93 179 } else {
ff53a58b 180 $errors['modassoc'] = get_string('error');
cae83708 181 }
182 }
183
184 if ($errors) {
185 return $errors;
186 }
187 return true;
2ee60b49 188 }
f07b9627 189}