moodle/blog/edit.php

249 lines
8.4 KiB
PHP
Raw Normal View History

2009-09-04 00:36:43 +00:00
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
2006-03-10 06:53:01 +00:00
2009-09-04 00:36:43 +00:00
/**
* Blog entry edit page
*
* @package moodlecore
* @subpackage blog
* @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(dirname(__FILE__)).'/config.php');
2006-03-10 06:53:01 +00:00
include_once('lib.php');
2009-09-04 00:36:43 +00:00
include_once('locallib.php');
include_once($CFG->dirroot.'/tag/lib.php');
2006-04-10 07:27:03 +00:00
$action = required_param('action', PARAM_ALPHA);
$id = optional_param('entryid', 0, PARAM_INT);
$confirm = optional_param('confirm', 0, PARAM_BOOL);
2009-09-04 00:36:43 +00:00
$modid = optional_param('modid', 0, PARAM_INT);
$courseid = optional_param('courseid', 0, PARAM_INT); // needed for user tab - does nothing here
$PAGE->set_url('blog/edit.php', array('action' => $action, 'entryid' => $id, 'confirm' => $confirm, 'modid' => $modid, 'courseid' => $courseid));
2009-09-04 00:36:43 +00:00
2009-09-09 03:26:18 +00:00
$blog_headers = blog_get_headers();
require_login($courseid);
2006-04-10 07:27:03 +00:00
if ($action == 'edit') {
$id = required_param('entryid', PARAM_INT);
}
if (empty($CFG->bloglevel)) {
2008-04-24 02:24:49 +00:00
print_error('blogdisable', 'blog');
}
if (isguest()) {
2009-09-04 00:36:43 +00:00
print_error('noguestentry', 'blog');
2006-03-10 06:53:01 +00:00
}
$sitecontext = get_context_instance(CONTEXT_SYSTEM);
2009-09-04 00:36:43 +00:00
if (!has_capability('moodle/blog:create', $sitecontext) && !has_capability('moodle/blog:manageentries', $sitecontext)) {
print_error('cannoteditentryorblog');
2006-03-10 06:53:01 +00:00
}
2009-09-04 00:36:43 +00:00
$returnurl = new moodle_url($CFG->wwwroot . '/blog/index.php');
// Make sure that the person trying to edit have access right
if ($id) {
2009-09-04 00:36:43 +00:00
if (!$existing = new blog_entry($id)) {
print_error('wrongentryid', 'blog');
2006-03-10 06:53:01 +00:00
}
2006-04-12 08:58:49 +00:00
2009-09-04 00:36:43 +00:00
if (!blog_user_can_edit_entry($existing)) {
print_error('notallowedtoedit', 'blog');
}
$userid = $existing->userid;
2009-09-04 00:36:43 +00:00
$returnurl->param('userid', $existing->userid);
2006-03-10 06:53:01 +00:00
} else {
if (!has_capability('moodle/blog:create', $sitecontext)) {
2009-09-04 00:36:43 +00:00
print_error('noentry', 'blog'); // manageentries is not enough for adding
}
$existing = false;
$userid = $USER->id;
2009-09-04 00:36:43 +00:00
$returnurl->param('userid', $userid);
2006-03-10 06:53:01 +00:00
}
2009-09-04 00:36:43 +00:00
if (!empty($courseid) && empty($modid)) {
$returnurl->param('courseid', $courseid);
$PAGE->set_context(get_context_instance(CONTEXT_COURSE, $courseid));
2006-04-20 02:14:17 +00:00
}
2009-09-04 00:36:43 +00:00
// If a modid is given, guess courseid
if (!empty($modid)) {
$returnurl->param('modid', $modid);
2009-09-07 10:01:26 +00:00
$courseid = $DB->get_field('course_modules', 'course', array('id' => $modid));
2009-09-04 00:36:43 +00:00
$returnurl->param('courseid', $courseid);
$PAGE->set_context(get_context_instance(CONTEXT_MODULE, $modid));
}
2006-04-20 02:14:17 +00:00
$strblogs = get_string('blogs','blog');
2006-03-10 06:53:01 +00:00
2008-06-01 13:48:12 +00:00
if ($action === 'delete'){
if (!$existing) {
2009-09-04 00:36:43 +00:00
print_error('wrongentryid', 'blog');
}
2009-09-04 00:36:43 +00:00
if (data_submitted() && $confirm && confirm_sesskey()) {
$existing->delete();
redirect($returnurl);
} else {
$optionsyes = array('entryid'=>$id, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'courseid'=>$courseid);
$optionsno = array('userid'=>$existing->userid, 'courseid'=>$courseid);
2009-09-07 10:01:26 +00:00
$PAGE->set_title("$SITE->shortname: $strblogs");
$PAGE->set_heading($SITE->fullname);
echo $OUTPUT->header();
2009-09-04 00:36:43 +00:00
//blog_print_entry($existing);
$existing->print_html();
echo '<br />';
echo $OUTPUT->confirm(get_string('blogdeleteconfirm', 'blog'), new moodle_url('edit.php', $optionsyes),new moodle_url( 'index.php', $optionsno));
echo $OUTPUT->footer();
die;
}
}
2006-04-12 08:58:49 +00:00
require_once('edit_form.php');
2009-09-04 00:36:43 +00:00
if (!empty($existing)) {
if ($blogassociations = $DB->get_records('blog_association', array('blogid' => $existing->id))) {
foreach ($blogassociations as $assocrec) {
$contextrec = $DB->get_record('context', array('id' => $assocrec->contextid));
switch ($contextrec->contextlevel) {
case CONTEXT_COURSE:
$existing->courseassoc = $assocrec->contextid;
break;
case CONTEXT_MODULE:
$existing->modassoc[] = $assocrec->contextid;
break;
}
}
}
}
$textfieldoptions = array('trusttext'=>true, 'subdirs'=>true);
2009-09-11 01:45:16 +00:00
$blogeditform = new blog_edit_form(null, compact('existing', 'sitecontext', 'textfieldoptions', 'id'));
2009-09-11 05:52:22 +00:00
$draftitemid = file_get_submitted_draft_itemid('attachments');
file_prepare_draft_area($draftitemid, $PAGE->context->id, 'blog_attachment', empty($id)?null:$id);
2009-09-04 00:36:43 +00:00
$editordraftid = file_get_submitted_draft_itemid('summary');
$currenttext = file_prepare_draft_area($editordraftid, $PAGE->context->id, 'blog_post', empty($id) ? null : $id, array('subdirs'=>true), @$existing->summary);
$data = array('id'=>$id, 'summary'=>array('text'=>$currenttext, 'format'=>FORMAT_HTML, 'itemid' => $editordraftid));
$blogeditform->set_data($data); // set defaults
if ($blogeditform->is_cancelled()){
redirect($returnurl);
2008-06-09 16:53:30 +00:00
} else if ($fromform = $blogeditform->get_data()){
2009-09-04 00:36:43 +00:00
//save stuff in db
switch ($action) {
case 'add':
$blogentry = new blog_entry($fromform, $blogeditform);
$blogentry->summary = file_save_draft_area_files($fromform->summary['itemid'], $PAGE->context->id, 'blog_post', $blogentry->id, array('subdirs'=>true), $fromform->summary['text']);
$blogentry->add();
break;
case 'edit':
if (!$existing) {
2009-09-04 00:36:43 +00:00
print_error('wrongentryid', 'blog');
}
2009-09-04 00:36:43 +00:00
$existing->edit($fromform, $blogeditform);
break;
default :
2008-04-24 02:24:49 +00:00
print_error('invalidaction');
}
redirect($returnurl);
}
2006-04-20 02:14:17 +00:00
// gui setup
switch ($action) {
case 'add':
// prepare new empty form
2009-09-04 00:36:43 +00:00
$entry->publishstate = 'site';
$strformheading = get_string('addnewentry', 'blog');
2009-09-04 00:36:43 +00:00
$entry->action = $action;
if ($courseid) { //pre-select the course for associations
$context = get_context_instance(CONTEXT_COURSE, $courseid);
$entry->courseassoc = $context->id;
}
if ($modid) { //pre-select the mod for associations
$context = get_context_instance(CONTEXT_MODULE, $modid);
$entry->modassoc = array($context->id);
}
break;
2006-03-10 06:53:01 +00:00
case 'edit':
if (!$existing) {
2009-09-04 00:36:43 +00:00
print_error('wrongentryid', 'blog');
}
2009-09-04 00:36:43 +00:00
$entry->id = $existing->id;
$entry->subject = $existing->subject;
$entry->fakesubject = $existing->subject;
$entry->summary = $existing->summary;
$entry->fakesummary = $existing->summary;
$entry->publishstate = $existing->publishstate;
$entry->format = $existing->format;
$entry->tags = tag_get_tags_array('blog_entries', $entry->id);
2009-09-04 00:36:43 +00:00
$entry->action = $action;
if (!empty($existing->courseassoc)) {
$entry->courseassoc = $existing->courseassoc;
}
if (!empty($existing->modassoc)) {
$entry->modassoc = $existing->modassoc;
}
$strformheading = get_string('updateentrywithid', 'blog');
2006-03-10 06:53:01 +00:00
2009-09-04 00:36:43 +00:00
break;
default :
2008-04-24 02:24:49 +00:00
print_error('unknowaction');
2006-03-10 06:53:01 +00:00
}
2009-09-04 00:36:43 +00:00
$entry->modid = $modid;
$entry->courseid = $courseid;
2009-09-11 05:52:22 +00:00
$entry->attachments = $draftitemid;
$entry->summary = array('text' => @$existing->summary, 'format' => empty($existing->summaryformat) ? FORMAT_HTML : $existing->summaryformat, 'itemid' => $editordraftid);
2009-09-11 05:52:22 +00:00
$entry->summaryformat = (empty($existing->summaryformat)) ? FORMAT_HTML : $existing->summaryformat;
2009-09-04 00:36:43 +00:00
$PAGE->requires->data_for_js('blog_edit_existing', $entry);
// done here in order to allow deleting of entries with wrong user id above
2008-06-01 13:48:12 +00:00
if (!$user = $DB->get_record('user', array('id'=>$userid))) {
2008-04-24 02:24:49 +00:00
print_error('invaliduserid');
2006-03-10 06:53:01 +00:00
}
2009-09-04 00:36:43 +00:00
2009-09-07 10:01:26 +00:00
$PAGE->requires->js('blog/edit_form.js');
echo $OUTPUT->header();
2006-03-10 06:53:01 +00:00
2009-09-04 00:36:43 +00:00
$blogeditform->set_data($entry);
$blogeditform->display();
2006-03-10 06:53:01 +00:00
2009-09-04 00:36:43 +00:00
$PAGE->requires->js_function_call('select_initial_course');
echo $OUTPUT->footer();
die;