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
|
|
|
|
*/
|
2009-09-08 05:36:45 +00:00
|
|
|
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');
|
2007-08-27 08:46:00 +00:00
|
|
|
include_once($CFG->dirroot.'/tag/lib.php');
|
2006-04-10 07:27:03 +00:00
|
|
|
|
2006-10-06 10:11:52 +00:00
|
|
|
$action = required_param('action', PARAM_ALPHA);
|
2009-09-08 03:05:09 +00:00
|
|
|
$id = optional_param('entryid', 0, PARAM_INT);
|
2006-10-06 10:11:52 +00:00
|
|
|
$confirm = optional_param('confirm', 0, PARAM_BOOL);
|
2009-09-04 00:36:43 +00:00
|
|
|
$modid = optional_param('modid', 0, PARAM_INT);
|
2006-10-06 10:11:52 +00:00
|
|
|
$courseid = optional_param('courseid', 0, PARAM_INT); // needed for user tab - does nothing here
|
|
|
|
|
2009-09-08 03:05:09 +00:00
|
|
|
$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();
|
|
|
|
|
2007-10-07 09:27:13 +00:00
|
|
|
require_login($courseid);
|
2006-04-10 07:27:03 +00:00
|
|
|
|
2009-09-08 03:05:09 +00:00
|
|
|
if ($action == 'edit') {
|
|
|
|
$id = required_param('entryid', PARAM_INT);
|
|
|
|
}
|
|
|
|
|
2006-10-03 19:21:35 +00:00
|
|
|
if (empty($CFG->bloglevel)) {
|
2008-04-24 02:24:49 +00:00
|
|
|
print_error('blogdisable', 'blog');
|
2006-10-03 19:21:35 +00:00
|
|
|
}
|
|
|
|
|
2006-10-06 10:11:52 +00:00
|
|
|
if (isguest()) {
|
2009-09-04 00:36:43 +00:00
|
|
|
print_error('noguestentry', 'blog');
|
2006-03-10 06:53:01 +00:00
|
|
|
}
|
|
|
|
|
2008-04-30 04:46:46 +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');
|
|
|
|
|
2006-08-08 05:13:06 +00:00
|
|
|
// Make sure that the person trying to edit have access right
|
2006-10-06 10:11:52 +00:00
|
|
|
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)) {
|
2008-04-04 02:54:20 +00:00
|
|
|
print_error('notallowedtoedit', 'blog');
|
2006-10-06 10:11:52 +00:00
|
|
|
}
|
|
|
|
$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 {
|
2006-10-06 10:11:52 +00:00
|
|
|
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
|
2006-10-06 10:11:52 +00:00
|
|
|
}
|
|
|
|
$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
|
|
|
|
2006-10-06 10:11:52 +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'){
|
2006-12-28 09:32:45 +00:00
|
|
|
if (!$existing) {
|
2009-09-04 00:36:43 +00:00
|
|
|
print_error('wrongentryid', 'blog');
|
2006-12-28 09:32:45 +00:00
|
|
|
}
|
2009-09-04 00:36:43 +00:00
|
|
|
if (data_submitted() && $confirm && confirm_sesskey()) {
|
|
|
|
$existing->delete();
|
2006-12-28 09:32:45 +00:00
|
|
|
redirect($returnurl);
|
|
|
|
} else {
|
2009-09-08 03:05:09 +00:00
|
|
|
$optionsyes = array('entryid'=>$id, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'courseid'=>$courseid);
|
2006-12-28 09:32:45 +00:00
|
|
|
$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();
|
2006-12-28 09:32:45 +00:00
|
|
|
echo '<br />';
|
2009-08-20 08:39:52 +00:00
|
|
|
echo $OUTPUT->confirm(get_string('blogdeleteconfirm', 'blog'), new moodle_url('edit.php', $optionsyes),new moodle_url( 'index.php', $optionsno));
|
2009-08-06 14:22:50 +00:00
|
|
|
echo $OUTPUT->footer();
|
2006-12-28 09:32:45 +00:00
|
|
|
die;
|
|
|
|
}
|
|
|
|
}
|
2006-04-12 08:58:49 +00:00
|
|
|
|
2006-12-28 09:32:45 +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');
|
2009-09-16 10:05:34 +00:00
|
|
|
file_prepare_draft_area($draftitemid, $PAGE->context->id, 'blog_attachment', empty($id)?null:$id);
|
2009-09-04 00:36:43 +00:00
|
|
|
|
2009-09-16 10:05:34 +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
|
2006-12-28 09:32:45 +00:00
|
|
|
|
2006-12-28 15:43:47 +00:00
|
|
|
if ($blogeditform->is_cancelled()){
|
2006-12-28 09:32:45 +00:00
|
|
|
redirect($returnurl);
|
2008-06-09 16:53:30 +00:00
|
|
|
} else if ($fromform = $blogeditform->get_data()){
|
2009-09-04 00:36:43 +00:00
|
|
|
|
2006-12-28 09:32:45 +00:00
|
|
|
//save stuff in db
|
|
|
|
switch ($action) {
|
|
|
|
case 'add':
|
2009-09-16 10:05:34 +00:00
|
|
|
$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();
|
2006-12-28 09:32:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'edit':
|
|
|
|
if (!$existing) {
|
2009-09-04 00:36:43 +00:00
|
|
|
print_error('wrongentryid', 'blog');
|
2006-10-06 10:11:52 +00:00
|
|
|
}
|
2009-09-04 00:36:43 +00:00
|
|
|
$existing->edit($fromform, $blogeditform);
|
2006-12-28 09:32:45 +00:00
|
|
|
break;
|
|
|
|
default :
|
2008-04-24 02:24:49 +00:00
|
|
|
print_error('invalidaction');
|
2006-12-28 09:32:45 +00:00
|
|
|
}
|
|
|
|
redirect($returnurl);
|
|
|
|
}
|
2006-04-20 02:14:17 +00:00
|
|
|
|
2006-12-28 09:32:45 +00:00
|
|
|
|
|
|
|
// gui setup
|
|
|
|
switch ($action) {
|
|
|
|
case 'add':
|
|
|
|
// prepare new empty form
|
2009-09-04 00:36:43 +00:00
|
|
|
$entry->publishstate = 'site';
|
2006-10-06 10:11:52 +00:00
|
|
|
$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
|
|
|
|
2006-10-06 10:11:52 +00:00
|
|
|
case 'edit':
|
|
|
|
if (!$existing) {
|
2009-09-04 00:36:43 +00:00
|
|
|
print_error('wrongentryid', 'blog');
|
2006-10-06 10:11:52 +00:00
|
|
|
}
|
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;
|
2009-09-18 06:57:52 +00:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2006-10-06 10:11:52 +00:00
|
|
|
$strformheading = get_string('updateentrywithid', 'blog');
|
2006-03-10 06:53:01 +00:00
|
|
|
|
2009-09-04 00:36:43 +00:00
|
|
|
break;
|
2006-12-28 09:32:45 +00:00
|
|
|
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;
|
2009-09-16 10:05:34 +00:00
|
|
|
$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);
|
2006-12-28 15:43:47 +00:00
|
|
|
$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');
|
2006-10-06 10:11:52 +00:00
|
|
|
|
2009-08-06 14:22:50 +00:00
|
|
|
echo $OUTPUT->footer();
|
2006-10-06 10:11:52 +00:00
|
|
|
|
|
|
|
die;
|