Commit | Line | Data |
---|---|---|
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 |
18 | if (!defined('MOODLE_INTERNAL')) { |
19 | die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page | |
20 | } | |
21 | ||
f07b9627 | 22 | require_once($CFG->libdir.'/formslib.php'); |
23 | ||
24 | class 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 | |
5f4d4d80 | 41 | $mform->addElement('text', 'subject', get_string('entrytitle', 'blog'), 'size="60"'); |
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'); | |
f07b9627 | 46 | |
1c7b8b93 NC |
47 | $mform->setType('summary_editor', PARAM_RAW); |
48 | $mform->addRule('summary_editor', get_string('emptybody', 'blog'), 'required', null, 'client'); | |
f07b9627 | 49 | |
1c7b8b93 | 50 | $mform->addElement('filemanager', 'attachment_filemanager', get_string('attachment', 'forum'), null, $attachmentoptions); |
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')); |
1c7b8b93 | 63 | $mform->setDefault('publishstate', 0); |
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)) { | |
1c7b8b93 NC |
73 | if ((!empty($entry->courseassoc) || (!empty($courseid) && empty($modid))) && has_capability('moodle/blog:associatecourse', $sitecontext)) { |
74 | if (!empty($courseid)) { | |
75 | $course = $DB->get_record('course', array('id' => $courseid)); | |
76 | $mform->addElement('header', 'assochdr', get_string('associations', 'blog')); | |
77 | $context = get_context_instance(CONTEXT_COURSE, $courseid); | |
78 | $a->coursename = $course->fullname; | |
79 | $contextid = $context->id; | |
80 | } else { | |
81 | $sql = 'SELECT fullname FROM {course} cr LEFT JOIN {context} ct ON ct.instanceid = cr.id WHERE ct.id = ?'; | |
82 | $a->coursename = $DB->get_field_sql($sql, array($entry->courseassoc)); | |
83 | $contextid = $entry->courseassoc; | |
84 | } | |
cae83708 | 85 | |
1c7b8b93 NC |
86 | $mform->addElement('advcheckbox', 'courseassoc', get_string('associatewithcourse', 'blog', $a), null, null, array(0, $contextid)); |
87 | $mform->setDefault('courseassoc', $contextid); | |
88 | } else if ((!empty($entry->modassoc) || !empty($modid)) && has_capability('moodle/blog:associatemodule', $sitecontext)) { | |
89 | if (!empty($modid)) { | |
90 | $mod = get_coursemodule_from_id(false, $modid); | |
91 | $a->modtype = get_string('modulename', $mod->modname); | |
92 | $a->modname = $mod->name; | |
93 | $context = get_context_instance(CONTEXT_MODULE, $modid); | |
94 | } else { | |
95 | $context = $DB->get_record('context', array('id' => $entry->modassoc)); | |
96 | $cm = $DB->get_record('course_modules', array('id' => $context->instanceid)); | |
97 | $a->modtype = $DB->get_field('modules', 'name', array('id' => $cm->module)); | |
98 | $a->modname = $DB->get_field($a->modtype, 'name', array('id' => $cm->instance)); | |
cae83708 | 99 | } |
cae83708 | 100 | |
1c7b8b93 NC |
101 | $mform->addElement('header', 'assochdr', get_string('associations', 'blog')); |
102 | $mform->addElement('advcheckbox', 'modassoc', get_string('associatewithmodule', 'blog', $a), null, null, array(0, $context->id)); | |
103 | $mform->setDefault('modassoc', $context->id); | |
104 | } | |
cae83708 | 105 | } |
106 | ||
f07b9627 | 107 | $this->add_action_buttons(); |
f07b9627 | 108 | $mform->addElement('hidden', 'action'); |
2ee60b49 | 109 | $mform->setType('action', PARAM_ACTION); |
110 | $mform->setDefault('action', ''); | |
111 | ||
0a941490 | 112 | $mform->addElement('hidden', 'entryid'); |
113 | $mform->setType('entryid', PARAM_INT); | |
1c7b8b93 | 114 | $mform->setDefault('entryid', $entry->id); |
2ee60b49 | 115 | |
cae83708 | 116 | $mform->addElement('hidden', 'modid'); |
117 | $mform->setType('modid', PARAM_INT); | |
1c7b8b93 | 118 | $mform->setDefault('modid', $modid); |
cae83708 | 119 | |
120 | $mform->addElement('hidden', 'courseid'); | |
121 | $mform->setType('courseid', PARAM_INT); | |
1c7b8b93 | 122 | $mform->setDefault('courseid', $courseid); |
cae83708 | 123 | } |
124 | ||
125 | function validation($data, $files) { | |
126 | global $CFG, $DB, $USER; | |
127 | ||
128 | $errors = array(); | |
1c7b8b93 | 129 | $sitecontext = get_context_instance(CONTEXT_SYSTEM); |
cae83708 | 130 | |
1c7b8b93 NC |
131 | // validate course association |
132 | if (!empty($data['courseassoc']) && has_capability('moodle/blog:associatecourse', $sitecontext)) { | |
cae83708 | 133 | $coursecontext = $DB->get_record('context', array('id' => $data['courseassoc'], 'contextlevel' => CONTEXT_COURSE)); |
134 | ||
1c7b8b93 | 135 | if ($coursecontext) { |
4f0c2d00 | 136 | if (!is_enrolled($coursecontext) and !is_viewing($coursecontext)) { |
cae83708 | 137 | $errors['courseassoc'] = get_string('studentnotallowed', '', fullname($USER, true)); |
138 | } | |
139 | } else { | |
140 | $errors['courseassoc'] = get_string('invalidcontextid', 'blog'); | |
141 | } | |
142 | } | |
143 | ||
1c7b8b93 | 144 | // validate mod association |
cae83708 | 145 | if (!empty($data['modassoc'])) { |
1c7b8b93 | 146 | $modcontextid = $data['modassoc']; |
cae83708 | 147 | |
1c7b8b93 NC |
148 | $modcontext = $DB->get_record('context', array('id' => $modcontextid, 'contextlevel' => CONTEXT_MODULE)); |
149 | ||
150 | if ($modcontext) { | |
151 | // get context of the mod's course | |
152 | $path = split('/', $modcontext->path); | |
153 | $coursecontext = $DB->get_record('context', array('id' => $path[(count($path) - 2)])); | |
154 | ||
155 | // ensure only one course is associated | |
156 | if (!empty($data['courseassoc'])) { | |
157 | if ($data['courseassoc'] != $coursecontext->id) { | |
158 | $errors['modassoc'] = get_string('onlyassociateonecourse', 'blog'); | |
cae83708 | 159 | } |
160 | } else { | |
1c7b8b93 NC |
161 | $data['courseassoc'] = $coursecontext->id; |
162 | } | |
163 | ||
164 | // ensure the user has access to each mod's course | |
4f0c2d00 | 165 | if (!is_enrolled($modcontext) and !is_viewing($modcontext)) { |
1c7b8b93 | 166 | $errors['modassoc'] = get_string('studentnotallowed', '', fullname($USER, true)); |
cae83708 | 167 | } |
1c7b8b93 NC |
168 | } else { |
169 | $errors['modassoc'] = get_string('invalidcontextid', 'blog'); | |
cae83708 | 170 | } |
171 | } | |
172 | ||
173 | if ($errors) { | |
174 | return $errors; | |
175 | } | |
176 | return true; | |
2ee60b49 | 177 | } |
f07b9627 | 178 | } |