|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00018 if (!defined('MOODLE_INTERNAL')) { 00019 die('Direct access to this script is forbidden.'); 00020 } 00021 00022 require_once($CFG->libdir.'/formslib.php'); 00023 00024 class blog_edit_form extends moodleform { 00025 public $modnames = array(); 00026 00027 function definition() { 00028 global $CFG, $DB; 00029 00030 $mform =& $this->_form; 00031 00032 $entry = $this->_customdata['entry']; 00033 $courseid = $this->_customdata['courseid']; 00034 $modid = $this->_customdata['modid']; 00035 $summaryoptions = $this->_customdata['summaryoptions']; 00036 $attachmentoptions = $this->_customdata['attachmentoptions']; 00037 $sitecontext = $this->_customdata['sitecontext']; 00038 00039 $mform->addElement('header', 'general', get_string('general', 'form')); 00040 00041 $mform->addElement('text', 'subject', get_string('entrytitle', 'blog'), 'size="60"'); 00042 $mform->addElement('editor', 'summary_editor', get_string('entrybody', 'blog'), null, $summaryoptions); 00043 00044 $mform->setType('subject', PARAM_TEXT); 00045 $mform->addRule('subject', get_string('emptytitle', 'blog'), 'required', null, 'client'); 00046 00047 $mform->setType('summary_editor', PARAM_RAW); 00048 $mform->addRule('summary_editor', get_string('emptybody', 'blog'), 'required', null, 'client'); 00049 00050 $mform->addElement('filemanager', 'attachment_filemanager', get_string('attachment', 'forum'), null, $attachmentoptions); 00051 00052 //disable publishstate options that are not allowed 00053 $publishstates = array(); 00054 $i = 0; 00055 00056 foreach (blog_entry::get_applicable_publish_states() as $state => $desc) { 00057 $publishstates[$state] = $desc; //no maximum was set 00058 $i++; 00059 } 00060 00061 $mform->addElement('select', 'publishstate', get_string('publishto', 'blog'), $publishstates); 00062 $mform->addHelpButton('publishstate', 'publishto', 'blog'); 00063 $mform->setDefault('publishstate', 0); 00064 00065 if (!empty($CFG->usetags)) { 00066 $mform->addElement('header', 'tagshdr', get_string('tags', 'tag')); 00067 $mform->addElement('tags', 'tags', get_string('tags')); 00068 } 00069 00070 $allmodnames = array(); 00071 00072 if (!empty($CFG->useblogassociations)) { 00073 if ((!empty($entry->courseassoc) || (!empty($courseid) && empty($modid))) && has_capability('moodle/blog:associatecourse', $sitecontext)) { 00074 if (!empty($courseid)) { 00075 $course = $DB->get_record('course', array('id' => $courseid)); 00076 $mform->addElement('header', 'assochdr', get_string('associations', 'blog')); 00077 $context = get_context_instance(CONTEXT_COURSE, $courseid); 00078 $a = new stdClass(); 00079 $a->coursename = format_string($course->fullname, true, array('context' => $context)); 00080 $contextid = $context->id; 00081 } else { 00082 $sql = 'SELECT fullname FROM {course} cr LEFT JOIN {context} ct ON ct.instanceid = cr.id WHERE ct.id = ?'; 00083 $a = new stdClass(); 00084 $a->coursename = $DB->get_field_sql($sql, array($entry->courseassoc)); 00085 $contextid = $entry->courseassoc; 00086 } 00087 00088 $mform->addElement('advcheckbox', 'courseassoc', get_string('associatewithcourse', 'blog', $a), null, null, array(0, $contextid)); 00089 $mform->setDefault('courseassoc', $contextid); 00090 } else if ((!empty($entry->modassoc) || !empty($modid)) && has_capability('moodle/blog:associatemodule', $sitecontext)) { 00091 if (!empty($modid)) { 00092 $mod = get_coursemodule_from_id(false, $modid); 00093 $a = new stdClass(); 00094 $a->modtype = get_string('modulename', $mod->modname); 00095 $a->modname = $mod->name; 00096 $context = get_context_instance(CONTEXT_MODULE, $modid); 00097 } else { 00098 $context = get_context_instance_by_id($entry->modassoc); 00099 $cm = $DB->get_record('course_modules', array('id' => $context->instanceid)); 00100 $a = new stdClass(); 00101 $a->modtype = $DB->get_field('modules', 'name', array('id' => $cm->module)); 00102 $a->modname = $DB->get_field($a->modtype, 'name', array('id' => $cm->instance)); 00103 } 00104 00105 $mform->addElement('header', 'assochdr', get_string('associations', 'blog')); 00106 $mform->addElement('advcheckbox', 'modassoc', get_string('associatewithmodule', 'blog', $a), null, null, array(0, $context->id)); 00107 $mform->setDefault('modassoc', $context->id); 00108 } 00109 } 00110 00111 $this->add_action_buttons(); 00112 $mform->addElement('hidden', 'action'); 00113 $mform->setType('action', PARAM_ACTION); 00114 $mform->setDefault('action', ''); 00115 00116 $mform->addElement('hidden', 'entryid'); 00117 $mform->setType('entryid', PARAM_INT); 00118 $mform->setDefault('entryid', $entry->id); 00119 00120 $mform->addElement('hidden', 'modid'); 00121 $mform->setType('modid', PARAM_INT); 00122 $mform->setDefault('modid', $modid); 00123 00124 $mform->addElement('hidden', 'courseid'); 00125 $mform->setType('courseid', PARAM_INT); 00126 $mform->setDefault('courseid', $courseid); 00127 } 00128 00129 function validation($data, $files) { 00130 global $CFG, $DB, $USER; 00131 00132 $errors = array(); 00133 $sitecontext = get_context_instance(CONTEXT_SYSTEM); 00134 00135 // validate course association 00136 if (!empty($data['courseassoc']) && has_capability('moodle/blog:associatecourse', $sitecontext)) { 00137 $coursecontext = context::instance_by_id($data['courseassoc'], IGNORE_MISSING); 00138 00139 if ($coursecontext and $coursecontext->contextlevel == CONTEXT_COURSE) { 00140 if (!is_enrolled($coursecontext) and !is_viewing($coursecontext)) { 00141 $errors['courseassoc'] = get_string('studentnotallowed', '', fullname($USER, true)); 00142 } 00143 } else { 00144 $errors['courseassoc'] = get_string('error'); 00145 } 00146 } 00147 00148 // validate mod association 00149 if (!empty($data['modassoc'])) { 00150 $modcontextid = $data['modassoc']; 00151 $modcontext = context::instance_by_id($modcontextid, IGNORE_MISSING); 00152 00153 if ($modcontext and $modcontext->contextlevel == CONTEXT_MODULE) { 00154 // get context of the mod's course 00155 $coursecontext = $modcontext->get_course_context(true); 00156 00157 // ensure only one course is associated 00158 if (!empty($data['courseassoc'])) { 00159 if ($data['courseassoc'] != $coursecontext->id) { 00160 $errors['modassoc'] = get_string('onlyassociateonecourse', 'blog'); 00161 } 00162 } else { 00163 $data['courseassoc'] = $coursecontext->id; 00164 } 00165 00166 // ensure the user has access to each mod's course 00167 if (!is_enrolled($modcontext) and !is_viewing($modcontext)) { 00168 $errors['modassoc'] = get_string('studentnotallowed', '', fullname($USER, true)); 00169 } 00170 } else { 00171 $errors['modassoc'] = get_string('error'); 00172 } 00173 } 00174 00175 if ($errors) { 00176 return $errors; 00177 } 00178 return true; 00179 } 00180 }