"MDL-21146, improve file license code"
[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() {
1c7b8b93 24 global $CFG, $DB;
45df7de3 25
1c7b8b93 26 $mform =& $this->_form;
f07b9627 27
1c7b8b93
NC
28 $entry = $this->_customdata['entry'];
29 $courseid = $this->_customdata['courseid'];
30 $modid = $this->_customdata['modid'];
31 $summaryoptions = $this->_customdata['summaryoptions'];
32 $attachmentoptions = $this->_customdata['attachmentoptions'];
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"');
1c7b8b93 38 $mform->addElement('editor', 'summary_editor', get_string('entrybody', 'blog'), null, $summaryoptions);
cae83708 39
2ee60b49 40 $mform->setType('subject', PARAM_TEXT);
41 $mform->addRule('subject', get_string('emptytitle', 'blog'), 'required', null, 'client');
f07b9627 42
1c7b8b93
NC
43 $mform->setType('summary_editor', PARAM_RAW);
44 $mform->addRule('summary_editor', get_string('emptybody', 'blog'), 'required', null, 'client');
45 $mform->setHelpButton('summary_editor', array('writing', 'richtext2'), false, 'editorhelpbutton');
f07b9627 46
5d50868a 47 $mform->addElement('format', 'summaryformat', get_string('format'));
f07b9627 48
1c7b8b93 49 $mform->addElement('filemanager', 'attachment_filemanager', get_string('attachment', 'forum'), null, $attachmentoptions);
f07b9627 50
cae83708 51 //disable publishstate options that are not allowed
52 $publishstates = array();
53 $i = 0;
54
55 foreach (blog_entry::get_applicable_publish_states() as $state => $desc) {
5f4d4d80 56 $publishstates[$state] = $desc; //no maximum was set
cae83708 57 $i++;
58 }
59
60 $mform->addElement('select', 'publishstate', get_string('publishto', 'blog'), $publishstates);
d4175332 61 $mform->setHelpButton('publishstate', array('publish_state', get_string('publishto', 'blog'), 'blog'));
1c7b8b93 62 $mform->setDefault('publishstate', 0);
93bbdd50 63
64 if (!empty($CFG->usetags)) {
3d535996 65 $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
fc23301c 66 $mform->addElement('tags', 'tags', get_string('tags'));
f07b9627 67 }
fc23301c 68
cae83708 69 $allmodnames = array();
70
71 if (!empty($CFG->useblogassociations)) {
1c7b8b93
NC
72 if ((!empty($entry->courseassoc) || (!empty($courseid) && empty($modid))) && has_capability('moodle/blog:associatecourse', $sitecontext)) {
73 if (!empty($courseid)) {
74 $course = $DB->get_record('course', array('id' => $courseid));
75 $mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
76 $context = get_context_instance(CONTEXT_COURSE, $courseid);
77 $a->coursename = $course->fullname;
78 $contextid = $context->id;
79 } else {
80 $sql = 'SELECT fullname FROM {course} cr LEFT JOIN {context} ct ON ct.instanceid = cr.id WHERE ct.id = ?';
81 $a->coursename = $DB->get_field_sql($sql, array($entry->courseassoc));
82 $contextid = $entry->courseassoc;
83 }
cae83708 84
1c7b8b93
NC
85 $mform->addElement('advcheckbox', 'courseassoc', get_string('associatewithcourse', 'blog', $a), null, null, array(0, $contextid));
86 $mform->setDefault('courseassoc', $contextid);
87 } else if ((!empty($entry->modassoc) || !empty($modid)) && has_capability('moodle/blog:associatemodule', $sitecontext)) {
88 if (!empty($modid)) {
89 $mod = get_coursemodule_from_id(false, $modid);
90 $a->modtype = get_string('modulename', $mod->modname);
91 $a->modname = $mod->name;
92 $context = get_context_instance(CONTEXT_MODULE, $modid);
93 } else {
94 $context = $DB->get_record('context', array('id' => $entry->modassoc));
95 $cm = $DB->get_record('course_modules', array('id' => $context->instanceid));
96 $a->modtype = $DB->get_field('modules', 'name', array('id' => $cm->module));
97 $a->modname = $DB->get_field($a->modtype, 'name', array('id' => $cm->instance));
cae83708 98 }
cae83708 99
1c7b8b93
NC
100 $mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
101 $mform->addElement('advcheckbox', 'modassoc', get_string('associatewithmodule', 'blog', $a), null, null, array(0, $context->id));
102 $mform->setDefault('modassoc', $context->id);
103 }
cae83708 104 }
105
f07b9627 106 $this->add_action_buttons();
f07b9627 107 $mform->addElement('hidden', 'action');
2ee60b49 108 $mform->setType('action', PARAM_ACTION);
109 $mform->setDefault('action', '');
110
0a941490 111 $mform->addElement('hidden', 'entryid');
112 $mform->setType('entryid', PARAM_INT);
1c7b8b93 113 $mform->setDefault('entryid', $entry->id);
2ee60b49 114
cae83708 115 $mform->addElement('hidden', 'modid');
116 $mform->setType('modid', PARAM_INT);
1c7b8b93 117 $mform->setDefault('modid', $modid);
cae83708 118
119 $mform->addElement('hidden', 'courseid');
120 $mform->setType('courseid', PARAM_INT);
1c7b8b93 121 $mform->setDefault('courseid', $courseid);
cae83708 122 }
123
124 function validation($data, $files) {
125 global $CFG, $DB, $USER;
126
127 $errors = array();
1c7b8b93 128 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
cae83708 129
1c7b8b93
NC
130 // validate course association
131 if (!empty($data['courseassoc']) && has_capability('moodle/blog:associatecourse', $sitecontext)) {
cae83708 132 $coursecontext = $DB->get_record('context', array('id' => $data['courseassoc'], 'contextlevel' => CONTEXT_COURSE));
133
1c7b8b93 134 if ($coursecontext) {
cae83708 135 if (!has_capability('moodle/course:view', $coursecontext, $USER->id)) {
136 $errors['courseassoc'] = get_string('studentnotallowed', '', fullname($USER, true));
137 }
138 } else {
139 $errors['courseassoc'] = get_string('invalidcontextid', 'blog');
140 }
141 }
142
1c7b8b93 143 // validate mod association
cae83708 144 if (!empty($data['modassoc'])) {
1c7b8b93 145 $modcontextid = $data['modassoc'];
cae83708 146
1c7b8b93
NC
147 $modcontext = $DB->get_record('context', array('id' => $modcontextid, 'contextlevel' => CONTEXT_MODULE));
148
149 if ($modcontext) {
150 // get context of the mod's course
151 $path = split('/', $modcontext->path);
152 $coursecontext = $DB->get_record('context', array('id' => $path[(count($path) - 2)]));
153
154 // ensure only one course is associated
155 if (!empty($data['courseassoc'])) {
156 if ($data['courseassoc'] != $coursecontext->id) {
157 $errors['modassoc'] = get_string('onlyassociateonecourse', 'blog');
cae83708 158 }
159 } else {
1c7b8b93
NC
160 $data['courseassoc'] = $coursecontext->id;
161 }
162
163 // ensure the user has access to each mod's course
164 if (!has_capability('moodle/course:view', $coursecontext)) {
165 $errors['modassoc'] = get_string('studentnotallowed', '', fullname($USER, true));
cae83708 166 }
1c7b8b93
NC
167 } else {
168 $errors['modassoc'] = get_string('invalidcontextid', 'blog');
cae83708 169 }
170 }
171
172 if ($errors) {
173 return $errors;
174 }
175 return true;
2ee60b49 176 }
f07b9627 177}