MDL-20523 Script for upgrading sites that use the old bloglevel settings.
[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
18require_once($CFG->libdir.'/formslib.php');
19
20class blog_edit_form extends moodleform {
cae83708 21 public $modnames = array();
f07b9627 22
2ee60b49 23 function definition() {
cae83708 24 global $CFG, $COURSE, $USER, $DB, $PAGE;
45df7de3 25
2ee60b49 26 $mform =& $this->_form;
f07b9627 27
b73d1ca4 28 $entryid = $this->_customdata['id'];
cae83708 29 $existing = $this->_customdata['existing'];
f07b9627 30 $sitecontext = $this->_customdata['sitecontext'];
31
f07b9627 32 $mform->addElement('header', 'general', get_string('general', 'form'));
cae83708 33
5f4d4d80 34 $mform->addElement('text', 'subject', get_string('entrytitle', 'blog'), 'size="60"');
b73d1ca4 35 $mform->addElement('editor', 'summary', get_string('entrybody', 'blog'), null, array('trusttext'=>true, 'subdirs'=>true, 'maxfiles' => -1));
cae83708 36
2ee60b49 37 $mform->setType('subject', PARAM_TEXT);
38 $mform->addRule('subject', get_string('emptytitle', 'blog'), 'required', null, 'client');
f07b9627 39
5d50868a 40 $mform->setType('summary', PARAM_RAW);
41 $mform->addRule('summary', get_string('emptybody', 'blog'), 'required', null, 'client');
42 $mform->setHelpButton('summary', array('writing', 'richtext2'), false, 'editorhelpbutton');
f07b9627 43
5d50868a 44 $mform->addElement('format', 'summaryformat', get_string('format'));
f07b9627 45
947eaf57 46 $mform->addElement('filemanager', 'attachments', get_string('attachment', 'forum'));
f07b9627 47
cae83708 48 //disable publishstate options that are not allowed
49 $publishstates = array();
50 $i = 0;
51
52 foreach (blog_entry::get_applicable_publish_states() as $state => $desc) {
5f4d4d80 53 $publishstates[$state] = $desc; //no maximum was set
cae83708 54 $i++;
55 }
56
57 $mform->addElement('select', 'publishstate', get_string('publishto', 'blog'), $publishstates);
d4175332 58 $mform->setHelpButton('publishstate', array('publish_state', get_string('publishto', 'blog'), 'blog'));
f07b9627 59
93bbdd50 60
61 if (!empty($CFG->usetags)) {
3d535996 62 $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
fc23301c 63 $mform->addElement('tags', 'tags', get_string('tags'));
f07b9627 64 }
fc23301c 65
cae83708 66 $allmodnames = array();
67
68 if (!empty($CFG->useblogassociations)) {
69 $mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
b73d1ca4 70 $mform->addElement('static', 'assocdescription', '', get_string('assocdescription', 'blog'));
cae83708 71 if (has_capability('moodle/site:doanything', get_context_instance(CONTEXT_USER, $USER->id))) {
72 $courses = get_courses('all', 'visible DESC, fullname ASC');
73 } else {
74 $courses = get_my_courses($USER->id, 'visible DESC, fullname ASC');
75 }
76
5f4d4d80 77 $coursenames[0] = 'none';
cae83708 78
79 if (!empty($courses)) {
80
81 foreach ($courses as $course) {
5f4d4d80 82 $coursenames[$course->context->id] = $course->fullname;
cae83708 83 $modinfo = get_fast_modinfo($course, $USER->id);
5f4d4d80 84 $coursecontextpath = $DB->get_field('context', 'path', array('id' => $course->context->id));
cae83708 85
86 foreach ($modinfo->instances as $modname => $instances) {
87
88 foreach ($instances as $modid => $mod) {
5f4d4d80 89 $modcontextid = $DB->get_field_select('context', 'id',
cae83708 90 'instanceid = '.$mod->id.' AND ' .
91 'contextlevel = ' . CONTEXT_MODULE . ' AND ' .
5f4d4d80 92 'path LIKE \''.$coursecontextpath.'/%\'');
cae83708 93
5f4d4d80 94 $modstring = $mod->name . ' (' . get_plugin_name($modname) . ')';
95 $this->modnames[$course->context->id][$modcontextid] = $modstring;
96 $allmodnames[$modcontextid] = $course->shortname . " - " . $modstring;
cae83708 97 }
98 }
99 }
100 }
5f4d4d80 101 $mform->addElement('select', 'courseassoc', get_string('course'), $coursenames, 'onchange="addCourseAssociations()"');
b73d1ca4 102 $mform->setAdvanced('courseassoc');
cae83708 103 $selectassoc = &$mform->addElement('select', 'modassoc', get_string('managemodules'), $allmodnames);
b73d1ca4 104 $mform->setAdvanced('modassoc');
cae83708 105 $selectassoc->setMultiple(true);
106 $PAGE->requires->data_for_js('blog_edit_form_modnames', $this->modnames);
107
108 }
109
f07b9627 110 $this->add_action_buttons();
f07b9627 111 $mform->addElement('hidden', 'action');
2ee60b49 112 $mform->setType('action', PARAM_ACTION);
113 $mform->setDefault('action', '');
114
115 $mform->addElement('hidden', 'courseid');
116 $mform->setType('courseid', PARAM_INT);
117
0a941490 118 $mform->addElement('hidden', 'entryid');
119 $mform->setType('entryid', PARAM_INT);
120 $mform->setDefault('entryid', $entryid);
2ee60b49 121
cae83708 122 $mform->addElement('hidden', 'modid');
123 $mform->setType('modid', PARAM_INT);
124 $mform->setDefault('modid', 0);
125
126 $mform->addElement('hidden', 'courseid');
127 $mform->setType('courseid', PARAM_INT);
128 $mform->setDefault('courseid', 0);
129
b73d1ca4 130 // $this->set_data($existing);
cae83708 131 }
132
133 function validation($data, $files) {
134 global $CFG, $DB, $USER;
135
136 $errors = array();
137
5f4d4d80 138 if (empty($data['courseassoc']) && ($data['publishstate'] == 'course' || $data['publishstate'] == 'group') && !empty($CFG->useblogassociations)) {
139 return array('publishstate' => get_string('mustassociatecourse', 'blog'));
cae83708 140 }
141
cae83708 142 //validate course association
143 if (!empty($data['courseassoc'])) {
144 $coursecontext = $DB->get_record('context', array('id' => $data['courseassoc'], 'contextlevel' => CONTEXT_COURSE));
145
146 if ($coursecontext) { //insure associated course has a valid context id
147 //insure the user has access to this course
148
149 if (!has_capability('moodle/course:view', $coursecontext, $USER->id)) {
150 $errors['courseassoc'] = get_string('studentnotallowed', '', fullname($USER, true));
151 }
152 } else {
153 $errors['courseassoc'] = get_string('invalidcontextid', 'blog');
154 }
155 }
156
157 //validate mod associations
158 if (!empty($data['modassoc'])) {
159 //insure mods are valid
160
161 foreach ($data['modassoc'] as $modid) {
162 $modcontext = $DB->get_record('context', array('id' => $modid, 'contextlevel' => CONTEXT_MODULE));
163
164 if ($modcontext) { //insure associated mod has a valid context id
165 //get context of the mod's course
166 $path = split('/', $modcontext->path);
5f4d4d80 167 $coursecontext = $DB->get_record('context', array('id' => $path[(count($path) - 2)]));
cae83708 168
169 //insure only one course is associated
170 if (!empty($data['courseassoc'])) {
171 if ($data['courseassoc'] != $coursecontext->id) {
172 $errors['modassoc'] = get_string('onlyassociateonecourse', 'blog');
173 }
174 } else {
175 $data['courseassoc'] = $coursecontext->id;
176 }
177
178 //insure the user has access to each mod's course
179 if (!has_capability('moodle/course:view', $coursecontext)) {
180 $errors['modassoc'] = get_string('studentnotallowed', '', fullname($USER, true));
181 }
182 } else {
183 $errors['modassoc'] = get_string('invalidcontextid', 'blog');
184 }
185 }
186 }
187
188 if ($errors) {
189 return $errors;
190 }
191 return true;
2ee60b49 192 }
193
194 /**
195 * This function sets up options of otag select element. This is called from definition and also
196 * after adding new official tags with the add tag button.
197 *
198 */
199 function otags_select_setup(){
47a2c30b 200 global $DB;
45df7de3 201
2ee60b49 202 $mform =& $this->_form;
bd1f4559 203 if ($otagsselect =& $mform->getElement('otags')) {
204 $otagsselect->removeOptions();
205 }
4c7fa6ce 206 $namefield = empty($CFG->keeptagnamecase) ? 'name' : 'rawname';
47a2c30b 207 if ($otags = $DB->get_records_sql_menu("SELECT id, $namefield FROM {tag} WHERE tagtype='official' ORDER by $namefield ASC")) {
f07b9627 208 $otagsselect->loadArray($otags);
209 }
47a2c30b 210
2ee60b49 211 }
f07b9627 212
213}