Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/blog/edit.php
Go to the documentation of this file.
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 
00027 require_once(dirname(dirname(__FILE__)).'/config.php');
00028 include_once('lib.php');
00029 include_once('locallib.php');
00030 
00031 $action   = required_param('action', PARAM_ALPHA);
00032 $id       = optional_param('entryid', 0, PARAM_INT);
00033 $confirm  = optional_param('confirm', 0, PARAM_BOOL);
00034 $modid    = optional_param('modid', 0, PARAM_INT); // To associate the entry with a module instance
00035 $courseid = optional_param('courseid', 0, PARAM_INT); // To associate the entry with a course
00036 
00037 $PAGE->set_url('/blog/edit.php', array('action' => $action, 'entryid' => $id, 'confirm' => $confirm, 'modid' => $modid, 'courseid' => $courseid));
00038 
00039 // If action is add, we ignore $id to avoid any further problems
00040 if (!empty($id) && $action == 'add') {
00041     $id = null;
00042 }
00043 
00044 $returnurl = new moodle_url('/blog/index.php');
00045 
00046 if (!empty($courseid) && empty($modid)) {
00047     $returnurl->param('courseid', $courseid);
00048     $PAGE->set_context(get_context_instance(CONTEXT_COURSE, $courseid));
00049 }
00050 
00051 // If a modid is given, guess courseid
00052 if (!empty($modid)) {
00053     $returnurl->param('modid', $modid);
00054     $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid));
00055     $returnurl->param('courseid', $courseid);
00056     $PAGE->set_context(get_context_instance(CONTEXT_MODULE, $modid));
00057 }
00058 
00059 // If courseid is empty use the system context
00060 if (empty($courseid)) {
00061     $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
00062 }
00063 
00064 $blogheaders = blog_get_headers();
00065 
00066 require_login($courseid);
00067 
00068 if ($action == 'edit') {
00069     $id = required_param('entryid', PARAM_INT);
00070 }
00071 
00072 if (empty($CFG->bloglevel)) {
00073     print_error('blogdisable', 'blog');
00074 }
00075 
00076 if (isguestuser()) {
00077     print_error('noguestentry', 'blog');
00078 }
00079 
00080 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
00081 if (!has_capability('moodle/blog:create', $sitecontext) && !has_capability('moodle/blog:manageentries', $sitecontext)) {
00082     print_error('cannoteditentryorblog');
00083 }
00084 
00085 // Make sure that the person trying to edit has access right
00086 if ($id) {
00087     if (!$entry = new blog_entry($id)) {
00088         print_error('wrongentryid', 'blog');
00089     }
00090 
00091     if (!blog_user_can_edit_entry($entry)) {
00092         print_error('notallowedtoedit', 'blog');
00093     }
00094     $userid = $entry->userid;
00095     $entry->subject      = clean_text($entry->subject);
00096     $entry->summary      = clean_text($entry->summary, $entry->format);
00097 
00098 } else {
00099     if (!has_capability('moodle/blog:create', $sitecontext)) {
00100         print_error('noentry', 'blog'); // manageentries is not enough for adding
00101     }
00102     $entry  = new stdClass();
00103     $entry->id = null;
00104     $userid = $USER->id;
00105 }
00106 $returnurl->param('userid', $userid);
00107 
00108 $strblogs = get_string('blogs','blog');
00109 
00110 if ($action === 'delete'){
00111     if (empty($entry->id)) {
00112         print_error('wrongentryid', 'blog');
00113     }
00114     if (data_submitted() && $confirm && confirm_sesskey()) {
00115         // Make sure the current user is the author of the blog entry, or has some deleteanyentry capability
00116         if (!blog_user_can_edit_entry($entry)) {
00117             print_error('nopermissionstodeleteentry', 'blog');
00118         } else {
00119             $entry->delete();
00120             redirect($returnurl);
00121         }
00122     } else if (blog_user_can_edit_entry($entry)) {
00123         $optionsyes = array('entryid'=>$id, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'courseid'=>$courseid);
00124         $optionsno = array('userid'=>$entry->userid, 'courseid'=>$courseid);
00125         $PAGE->set_title("$SITE->shortname: $strblogs");
00126         $PAGE->set_heading($SITE->fullname);
00127         echo $OUTPUT->header();
00128         $entry->print_html();
00129         echo '<br />';
00130         echo $OUTPUT->confirm(get_string('blogdeleteconfirm', 'blog'), new moodle_url('edit.php', $optionsyes),new moodle_url( 'index.php', $optionsno));
00131         echo $OUTPUT->footer();
00132         die;
00133     }
00134 } else if ($action == 'add') {
00135     $PAGE->set_title("$SITE->shortname: $strblogs: " . get_string('addnewentry', 'blog'));
00136     $PAGE->set_heading($SITE->shortname);
00137 } else if ($action == 'edit') {
00138     $PAGE->set_title("$SITE->shortname: $strblogs: " . get_string('editentry', 'blog'));
00139     $PAGE->set_heading($SITE->shortname);
00140 }
00141 
00142 if (!empty($entry->id)) {
00143     if ($CFG->useblogassociations && ($blogassociations = $DB->get_records('blog_association', array('blogid' => $entry->id)))) {
00144 
00145         foreach ($blogassociations as $assocrec) {
00146             $context = get_context_instance_by_id($assocrec->contextid);
00147 
00148             switch ($context->contextlevel) {
00149                 case CONTEXT_COURSE:
00150                     $entry->courseassoc = $assocrec->contextid;
00151                     break;
00152                 case CONTEXT_MODULE:
00153                     $entry->modassoc = $assocrec->contextid;
00154                     break;
00155             }
00156         }
00157     }
00158 }
00159 
00160 require_once('edit_form.php');
00161 $summaryoptions = array('subdirs'=>false, 'maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>true, 'context'=>$sitecontext);
00162 $attachmentoptions = array('subdirs'=>false, 'maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes);
00163 
00164 $blogeditform = new blog_edit_form(null, compact('entry', 'summaryoptions', 'attachmentoptions', 'sitecontext', 'courseid', 'modid'));
00165 
00166 $entry = file_prepare_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id);
00167 $entry = file_prepare_standard_filemanager($entry, 'attachment', $attachmentoptions, $sitecontext, 'blog', 'attachment', $entry->id);
00168 
00169 if (!empty($CFG->usetags) && !empty($entry->id)) {
00170     include_once($CFG->dirroot.'/tag/lib.php');
00171     $entry->tags = tag_get_tags_array('post', $entry->id);
00172 }
00173 
00174 $entry->action = $action;
00175 // set defaults
00176 $blogeditform->set_data($entry);
00177 
00178 if ($blogeditform->is_cancelled()) {
00179     redirect($returnurl);
00180 
00181 } else if ($data = $blogeditform->get_data()){
00182 
00183     switch ($action) {
00184         case 'add':
00185             $blogentry = new blog_entry(null, $data, $blogeditform);
00186             $blogentry->add();
00187             $blogentry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
00188         break;
00189 
00190         case 'edit':
00191             if (empty($entry->id)) {
00192                 print_error('wrongentryid', 'blog');
00193             }
00194 
00195             $entry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
00196         break;
00197 
00198         default :
00199             print_error('invalidaction');
00200     }
00201 
00202     redirect($returnurl);
00203 }
00204 
00205 
00206 // gui setup
00207 switch ($action) {
00208     case 'add':
00209         // prepare new empty form
00210         $entry->publishstate = 'site';
00211         $strformheading = get_string('addnewentry', 'blog');
00212         $entry->action       = $action;
00213 
00214         if ($CFG->useblogassociations) {
00215 
00216             //pre-select the course for associations
00217             if ($courseid) {
00218                 $context = get_context_instance(CONTEXT_COURSE, $courseid);
00219                 $entry->courseassoc = $context->id;
00220             }
00221 
00222             //pre-select the mod for associations
00223             if ($modid) {
00224                 $context = get_context_instance(CONTEXT_MODULE, $modid);
00225                 $entry->modassoc = $context->id;
00226             }
00227         }
00228         break;
00229 
00230     case 'edit':
00231         if (empty($entry->id)) {
00232             print_error('wrongentryid', 'blog');
00233         }
00234         $entry->tags = tag_get_tags_array('post', $entry->id);
00235         $strformheading = get_string('updateentrywithid', 'blog');
00236 
00237         break;
00238 
00239     default :
00240         print_error('unknowaction');
00241 }
00242 
00243 $entry->modid = $modid;
00244 $entry->courseid = $courseid;
00245 
00246 echo $OUTPUT->header();
00247 $blogeditform->display();
00248 echo $OUTPUT->footer();
00249 
00250 die;
 All Data Structures Namespaces Files Functions Variables Enumerations