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