| 1 | efrain | 1 | <?php
 | 
        
           |  |  | 2 | // This file is part of Moodle - http://moodle.org/
 | 
        
           |  |  | 3 | //
 | 
        
           |  |  | 4 | // Moodle is free software: you can redistribute it and/or modify
 | 
        
           |  |  | 5 | // it under the terms of the GNU General Public License as published by
 | 
        
           |  |  | 6 | // the Free Software Foundation, either version 3 of the License, or
 | 
        
           |  |  | 7 | // (at your option) any later version.
 | 
        
           |  |  | 8 | //
 | 
        
           |  |  | 9 | // Moodle is distributed in the hope that it will be useful,
 | 
        
           |  |  | 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
        
           |  |  | 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
        
           |  |  | 12 | // GNU General Public License for more details.
 | 
        
           |  |  | 13 | //
 | 
        
           |  |  | 14 | // You should have received a copy of the GNU General Public License
 | 
        
           |  |  | 15 | // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 | 
        
           |  |  | 16 |   | 
        
           |  |  | 17 |   | 
        
           |  |  | 18 | /**
 | 
        
           |  |  | 19 |  * Blog entry edit page
 | 
        
           |  |  | 20 |  *
 | 
        
           |  |  | 21 |  * @package    moodlecore
 | 
        
           |  |  | 22 |  * @subpackage blog
 | 
        
           |  |  | 23 |  * @copyright  2009 Nicolas Connault
 | 
        
           |  |  | 24 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 25 |  */
 | 
        
           |  |  | 26 | require_once(__DIR__ . '/../config.php');
 | 
        
           |  |  | 27 | require_once($CFG->dirroot . '/blog/lib.php');
 | 
        
           |  |  | 28 | require_once($CFG->dirroot . '/blog/locallib.php');
 | 
        
           |  |  | 29 | require_once($CFG->dirroot . '/comment/lib.php');
 | 
        
           |  |  | 30 | require_once($CFG->dirroot . '/blog/edit_form.php');
 | 
        
           |  |  | 31 |   | 
        
           |  |  | 32 | $action   = required_param('action', PARAM_ALPHA);
 | 
        
           |  |  | 33 | $id       = optional_param('entryid', 0, PARAM_INT);
 | 
        
           |  |  | 34 | $confirm  = optional_param('confirm', 0, PARAM_BOOL);
 | 
        
           |  |  | 35 | $modid = optional_param('modid', 0, PARAM_INT); // To associate the entry with a module instance.
 | 
        
           |  |  | 36 | $courseid = optional_param('courseid', 0, PARAM_INT); // To associate the entry with a course.
 | 
        
           |  |  | 37 |   | 
        
           |  |  | 38 | if ($action == 'edit') {
 | 
        
           |  |  | 39 |     $id = required_param('entryid', PARAM_INT);
 | 
        
           |  |  | 40 | }
 | 
        
           |  |  | 41 |   | 
        
           |  |  | 42 | $PAGE->set_url('/blog/edit.php', array('action' => $action,
 | 
        
           |  |  | 43 |                                        'entryid' => $id,
 | 
        
           |  |  | 44 |                                        'confirm' => $confirm,
 | 
        
           |  |  | 45 |                                        'modid' => $modid,
 | 
        
           |  |  | 46 |                                        'courseid' => $courseid));
 | 
        
           |  |  | 47 |   | 
        
           |  |  | 48 | // If action is add, we ignore $id to avoid any further problems.
 | 
        
           |  |  | 49 | if (!empty($id) && $action == 'add') {
 | 
        
           |  |  | 50 |     $id = null;
 | 
        
           |  |  | 51 | }
 | 
        
           |  |  | 52 |   | 
        
           |  |  | 53 | $entry = new stdClass();
 | 
        
           |  |  | 54 | $entry->id = null;
 | 
        
           |  |  | 55 |   | 
        
           |  |  | 56 | if ($id) {
 | 
        
           |  |  | 57 |     $entry = new blog_entry($id);   // Will trigger exception if not found.
 | 
        
           |  |  | 58 |     $userid = $entry->userid;
 | 
        
           |  |  | 59 | } else {
 | 
        
           |  |  | 60 |     $userid = $USER->id;
 | 
        
           |  |  | 61 | }
 | 
        
           |  |  | 62 |   | 
        
           |  |  | 63 | $sitecontext = context_system::instance();
 | 
        
           |  |  | 64 | $usercontext = context_user::instance($userid);
 | 
        
           |  |  | 65 |   | 
        
           |  |  | 66 | require_login($courseid);
 | 
        
           |  |  | 67 |   | 
        
           |  |  | 68 | if (empty($CFG->enableblogs)) {
 | 
        
           |  |  | 69 |     throw new \moodle_exception('blogdisable', 'blog');
 | 
        
           |  |  | 70 | }
 | 
        
           |  |  | 71 |   | 
        
           |  |  | 72 | if (isguestuser()) {
 | 
        
           |  |  | 73 |     throw new \moodle_exception('noguest');
 | 
        
           |  |  | 74 | }
 | 
        
           |  |  | 75 |   | 
        
           | 1441 | ariadna | 76 | if ($modid) {
 | 
        
           |  |  | 77 |     $PAGE->set_context($sitecontext);
 | 
        
           |  |  | 78 | } else {
 | 
        
           |  |  | 79 |     $PAGE->set_context($usercontext);
 | 
        
           |  |  | 80 |     $blognode = $PAGE->settingsnav->find('blogadd', null);
 | 
        
           |  |  | 81 |     $blognode->make_active();
 | 
        
           |  |  | 82 | }
 | 
        
           |  |  | 83 |   | 
        
           | 1 | efrain | 84 | $returnurl = new moodle_url('/blog/index.php');
 | 
        
           |  |  | 85 | if (!empty($courseid) && empty($modid)) {
 | 
        
           |  |  | 86 |     $returnurl->param('courseid', $courseid);
 | 
        
           |  |  | 87 | }
 | 
        
           |  |  | 88 |   | 
        
           |  |  | 89 | // If a modid is given, guess courseid.
 | 
        
           |  |  | 90 | if (!empty($modid)) {
 | 
        
           |  |  | 91 |     $returnurl->param('modid', $modid);
 | 
        
           |  |  | 92 |     $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid));
 | 
        
           |  |  | 93 |     $returnurl->param('courseid', $courseid);
 | 
        
           |  |  | 94 | }
 | 
        
           |  |  | 95 |   | 
        
           |  |  | 96 | $blogheaders = blog_get_headers();
 | 
        
           |  |  | 97 |   | 
        
           |  |  | 98 | if (!has_capability('moodle/blog:create', $sitecontext) && !has_capability('moodle/blog:manageentries', $sitecontext)) {
 | 
        
           |  |  | 99 |     throw new \moodle_exception('cannoteditentryorblog', 'blog');
 | 
        
           |  |  | 100 | }
 | 
        
           |  |  | 101 |   | 
        
           |  |  | 102 | // Make sure that the person trying to edit has access right.
 | 
        
           |  |  | 103 | if ($id) {
 | 
        
           |  |  | 104 |     if (!blog_user_can_edit_entry($entry)) {
 | 
        
           |  |  | 105 |         throw new \moodle_exception('notallowedtoedit', 'blog');
 | 
        
           |  |  | 106 |     }
 | 
        
           |  |  | 107 |     $entry->subject      = clean_text($entry->subject);
 | 
        
           |  |  | 108 |     $entry->summary      = clean_text($entry->summary, $entry->format);
 | 
        
           |  |  | 109 | } else {
 | 
        
           |  |  | 110 |     if (!has_capability('moodle/blog:create', $sitecontext)) {
 | 
        
           |  |  | 111 |         throw new \moodle_exception('noentry', 'blog'); // The capability "manageentries" is not enough for adding.
 | 
        
           |  |  | 112 |     }
 | 
        
           |  |  | 113 | }
 | 
        
           |  |  | 114 | $returnurl->param('userid', $userid);
 | 
        
           |  |  | 115 |   | 
        
           |  |  | 116 | // Blog renderer.
 | 
        
           |  |  | 117 | $output = $PAGE->get_renderer('blog');
 | 
        
           |  |  | 118 |   | 
        
           |  |  | 119 | $strblogs = get_string('blogs', 'blog');
 | 
        
           |  |  | 120 |   | 
        
           |  |  | 121 | if ($action === 'delete') {
 | 
        
           |  |  | 122 |     // Init comment JS strings.
 | 
        
           |  |  | 123 |     comment::init();
 | 
        
           |  |  | 124 |   | 
        
           |  |  | 125 |     if (empty($entry->id)) {
 | 
        
           |  |  | 126 |         throw new \moodle_exception('wrongentryid');
 | 
        
           |  |  | 127 |     }
 | 
        
           |  |  | 128 |     if (data_submitted() && $confirm && confirm_sesskey()) {
 | 
        
           |  |  | 129 |         // Make sure the current user is the author of the blog entry, or has some deleteanyentry capability.
 | 
        
           |  |  | 130 |         if (!blog_user_can_edit_entry($entry)) {
 | 
        
           |  |  | 131 |             throw new \moodle_exception('nopermissionstodeleteentry', 'blog');
 | 
        
           |  |  | 132 |         } else {
 | 
        
           |  |  | 133 |             $entry->delete();
 | 
        
           |  |  | 134 |             blog_rss_delete_file($userid);
 | 
        
           |  |  | 135 |             redirect($returnurl);
 | 
        
           |  |  | 136 |         }
 | 
        
           |  |  | 137 |     } else if (blog_user_can_edit_entry($entry)) {
 | 
        
           |  |  | 138 |         $optionsyes = array('entryid' => $id,
 | 
        
           |  |  | 139 |                             'action' => 'delete',
 | 
        
           |  |  | 140 |                             'confirm' => 1,
 | 
        
           |  |  | 141 |                             'sesskey' => sesskey(),
 | 
        
           |  |  | 142 |                             'courseid' => $courseid);
 | 
        
           |  |  | 143 |         $optionsno = array('userid' => $entry->userid, 'courseid' => $courseid);
 | 
        
           |  |  | 144 |         $PAGE->set_title($strblogs);
 | 
        
           |  |  | 145 |         $PAGE->set_heading($SITE->fullname);
 | 
        
           |  |  | 146 |         echo $OUTPUT->header();
 | 
        
           |  |  | 147 |   | 
        
           |  |  | 148 |         // Output edit mode title.
 | 
        
           |  |  | 149 |         echo $OUTPUT->heading($strblogs . ': ' . get_string('deleteentry', 'blog'), 2);
 | 
        
           |  |  | 150 |   | 
        
           |  |  | 151 |         echo $OUTPUT->confirm(get_string('blogdeleteconfirm', 'blog', format_string($entry->subject)),
 | 
        
           |  |  | 152 |                               new moodle_url('edit.php', $optionsyes),
 | 
        
           |  |  | 153 |                               new moodle_url('index.php', $optionsno));
 | 
        
           |  |  | 154 |   | 
        
           |  |  | 155 |         echo '<br />';
 | 
        
           |  |  | 156 |         // Output the entry.
 | 
        
           |  |  | 157 |         $entry->prepare_render();
 | 
        
           |  |  | 158 |         echo $output->render($entry);
 | 
        
           |  |  | 159 |   | 
        
           |  |  | 160 |         echo $OUTPUT->footer();
 | 
        
           |  |  | 161 |         die;
 | 
        
           |  |  | 162 |     }
 | 
        
           |  |  | 163 | } else if ($action == 'add') {
 | 
        
           |  |  | 164 |     $editmodetitle = $strblogs . ': ' . get_string('addnewentry', 'blog');
 | 
        
           |  |  | 165 |     $PAGE->set_title($editmodetitle);
 | 
        
           |  |  | 166 |     $PAGE->set_heading(fullname($USER));
 | 
        
           |  |  | 167 | } else if ($action == 'edit') {
 | 
        
           |  |  | 168 |     $editmodetitle = $strblogs . ': ' . get_string('editentry', 'blog');
 | 
        
           |  |  | 169 |     $PAGE->set_title($editmodetitle);
 | 
        
           |  |  | 170 |     $PAGE->set_heading(fullname($USER));
 | 
        
           |  |  | 171 | }
 | 
        
           |  |  | 172 |   | 
        
           |  |  | 173 | if (!empty($entry->id)) {
 | 
        
           |  |  | 174 |     if ($CFG->useblogassociations && ($blogassociations = $DB->get_records('blog_association', array('blogid' => $entry->id)))) {
 | 
        
           |  |  | 175 |   | 
        
           |  |  | 176 |         foreach ($blogassociations as $assocrec) {
 | 
        
           |  |  | 177 |             $context = context::instance_by_id($assocrec->contextid);
 | 
        
           |  |  | 178 |   | 
        
           |  |  | 179 |             switch ($context->contextlevel) {
 | 
        
           |  |  | 180 |                 case CONTEXT_COURSE:
 | 
        
           |  |  | 181 |                     $entry->courseassoc = $assocrec->contextid;
 | 
        
           |  |  | 182 |                     break;
 | 
        
           |  |  | 183 |                 case CONTEXT_MODULE:
 | 
        
           |  |  | 184 |                     $entry->modassoc = $assocrec->contextid;
 | 
        
           |  |  | 185 |                     break;
 | 
        
           |  |  | 186 |             }
 | 
        
           |  |  | 187 |         }
 | 
        
           |  |  | 188 |     }
 | 
        
           |  |  | 189 | }
 | 
        
           |  |  | 190 |   | 
        
           |  |  | 191 | [$summaryoptions, $attachmentoptions] = blog_get_editor_options($entry);
 | 
        
           |  |  | 192 |   | 
        
           |  |  | 193 | $blogeditform = new blog_edit_form(null, compact('entry',
 | 
        
           |  |  | 194 |                                                  'summaryoptions',
 | 
        
           |  |  | 195 |                                                  'attachmentoptions',
 | 
        
           |  |  | 196 |                                                  'sitecontext',
 | 
        
           |  |  | 197 |                                                  'courseid',
 | 
        
           |  |  | 198 |                                                  'modid'));
 | 
        
           |  |  | 199 |   | 
        
           |  |  | 200 | $entry = file_prepare_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id);
 | 
        
           |  |  | 201 | $entry = file_prepare_standard_filemanager($entry,
 | 
        
           |  |  | 202 |                                            'attachment',
 | 
        
           |  |  | 203 |                                            $attachmentoptions,
 | 
        
           |  |  | 204 |                                            $sitecontext,
 | 
        
           |  |  | 205 |                                            'blog',
 | 
        
           |  |  | 206 |                                            'attachment',
 | 
        
           |  |  | 207 |                                            $entry->id);
 | 
        
           |  |  | 208 |   | 
        
           |  |  | 209 | if (!empty($entry->id)) {
 | 
        
           |  |  | 210 |     $entry->tags = core_tag_tag::get_item_tags_array('core', 'post', $entry->id);
 | 
        
           |  |  | 211 | }
 | 
        
           |  |  | 212 |   | 
        
           |  |  | 213 | $entry->action = $action;
 | 
        
           |  |  | 214 | // Set defaults.
 | 
        
           |  |  | 215 | $blogeditform->set_data($entry);
 | 
        
           |  |  | 216 |   | 
        
           |  |  | 217 | if ($blogeditform->is_cancelled()) {
 | 
        
           |  |  | 218 |     redirect($returnurl);
 | 
        
           |  |  | 219 |   | 
        
           |  |  | 220 | } else if ($data = $blogeditform->get_data()) {
 | 
        
           |  |  | 221 |   | 
        
           |  |  | 222 |     switch ($action) {
 | 
        
           |  |  | 223 |         case 'add':
 | 
        
           |  |  | 224 |             $blogentry = new blog_entry(null, $data, $blogeditform);
 | 
        
           |  |  | 225 |             $blogentry->add();
 | 
        
           |  |  | 226 |             $blogentry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
 | 
        
           |  |  | 227 |         break;
 | 
        
           |  |  | 228 |   | 
        
           |  |  | 229 |         case 'edit':
 | 
        
           |  |  | 230 |             if (empty($entry->id)) {
 | 
        
           |  |  | 231 |                 throw new \moodle_exception('wrongentryid');
 | 
        
           |  |  | 232 |             }
 | 
        
           |  |  | 233 |   | 
        
           |  |  | 234 |             $entry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
 | 
        
           |  |  | 235 |         break;
 | 
        
           |  |  | 236 |   | 
        
           |  |  | 237 |         default :
 | 
        
           |  |  | 238 |             throw new \moodle_exception('invalidaction');
 | 
        
           |  |  | 239 |     }
 | 
        
           |  |  | 240 |   | 
        
           |  |  | 241 |     redirect($returnurl);
 | 
        
           |  |  | 242 | }
 | 
        
           |  |  | 243 |   | 
        
           |  |  | 244 |   | 
        
           |  |  | 245 | // GUI setup.
 | 
        
           |  |  | 246 | switch ($action) {
 | 
        
           |  |  | 247 |     case 'add':
 | 
        
           |  |  | 248 |         // Prepare new empty form.
 | 
        
           |  |  | 249 |         $entry->publishstate = 'site';
 | 
        
           |  |  | 250 |         $strformheading = get_string('addnewentry', 'blog');
 | 
        
           |  |  | 251 |         $entry->action       = $action;
 | 
        
           |  |  | 252 |   | 
        
           |  |  | 253 |         if ($CFG->useblogassociations) {
 | 
        
           |  |  | 254 |   | 
        
           |  |  | 255 |             // Pre-select the course for associations.
 | 
        
           |  |  | 256 |             if ($courseid) {
 | 
        
           |  |  | 257 |                 $context = context_course::instance($courseid);
 | 
        
           |  |  | 258 |                 $entry->courseassoc = $context->id;
 | 
        
           |  |  | 259 |             }
 | 
        
           |  |  | 260 |   | 
        
           |  |  | 261 |             // Pre-select the mod for associations.
 | 
        
           |  |  | 262 |             if ($modid) {
 | 
        
           |  |  | 263 |                 $context = context_module::instance($modid);
 | 
        
           |  |  | 264 |                 $entry->modassoc = $context->id;
 | 
        
           |  |  | 265 |             }
 | 
        
           |  |  | 266 |         }
 | 
        
           |  |  | 267 |         break;
 | 
        
           |  |  | 268 |   | 
        
           |  |  | 269 |     case 'edit':
 | 
        
           |  |  | 270 |         if (empty($entry->id)) {
 | 
        
           |  |  | 271 |             throw new \moodle_exception('wrongentryid');
 | 
        
           |  |  | 272 |         }
 | 
        
           |  |  | 273 |         $strformheading = get_string('updateentrywithid', 'blog');
 | 
        
           |  |  | 274 |   | 
        
           |  |  | 275 |         break;
 | 
        
           |  |  | 276 |   | 
        
           |  |  | 277 |     default :
 | 
        
           |  |  | 278 |         throw new \moodle_exception('unknowaction');
 | 
        
           |  |  | 279 | }
 | 
        
           |  |  | 280 |   | 
        
           |  |  | 281 | $entry->modid = $modid;
 | 
        
           |  |  | 282 | $entry->courseid = $courseid;
 | 
        
           |  |  | 283 |   | 
        
           |  |  | 284 | echo $OUTPUT->header();
 | 
        
           |  |  | 285 | // Output title for editing mode.
 | 
        
           |  |  | 286 | if (isset($editmodetitle)) {
 | 
        
           |  |  | 287 |     echo $OUTPUT->heading($editmodetitle, 2);
 | 
        
           |  |  | 288 | }
 | 
        
           |  |  | 289 | $blogeditform->display();
 | 
        
           |  |  | 290 | echo $OUTPUT->footer();
 | 
        
           |  |  | 291 |   | 
        
           |  |  | 292 | die;
 |