Commit 51008e7e authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Make recent comments editable


Signed-off-by: default avatarRichard Mansfield <richardm@catalyst.net.nz>
parent 6287ecc9
<?php
/**
* Mahara: Electronic portfolio, weblog, resume builder and social networking
* Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
* http://wiki.mahara.org/Contributors
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package mahara
* @subpackage artefact-comment
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
*
*/
define('INTERNAL', 1);
define('MENUITEM', 'myportfolio');
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
define('TITLE', get_string('editcomment', 'artefact.comment'));
safe_require('artefact', 'comment');
$id = param_integer('id');
$viewid = param_integer('view');
$comment = new ArtefactTypeComment($id);
if ($USER->get('id') != $comment->get('author')) {
throw new AccessDeniedException(get_string('canteditnotauthor', 'artefact.comment'));
}
$onview = $comment->get('onview');
if ($onview && $onview != $viewid) {
throw new NotFoundException(get_string('commentnotinview', 'artefact.comment', $id, $viewid));
}
$maxage = (int) get_config_plugin('artefact', 'comment', 'commenteditabletime');
$editableafter = time() - 60 * $maxage;
$onartefact = $comment->get('onartefact');
if ($onartefact) {
$goto = get_config('wwwroot') . 'view/artefact.php?artefact=' . $onartefact . '&view='.$viewid;
}
else {
$goto = get_config('wwwroot') . 'view/view.php?id='.$viewid;
}
if ($comment->get('ctime') < $editableafter) {
$SESSION->add_error_msg(get_string('cantedittooold', 'artefact.comment', $maxage));
redirect($goto);
}
$lastcomment = ArtefactTypeComment::last_public_comment($viewid, $comment->get('onartefact'));
if (!$comment->get('private') && $id != $lastcomment->id) {
$SESSION->add_error_msg(get_string('cantedithasreplies', 'artefact.comment'));
redirect($goto);
}
$form = pieform(array(
'name' => 'edit_comment',
'method' => 'post',
'plugintype' => 'artefact',
'pluginname' => 'comment',
'elements' => array(
'message' => array(
'type' => 'wysiwyg',
'title' => get_string('message'),
'rows' => 5,
'cols' => 80,
'defaultvalue' => $comment->get('description'),
),
'ispublic' => array(
'type' => 'checkbox',
'title' => get_string('makepublic', 'artefact.comment'),
'defaultvalue' => !$comment->get('private'),
),
'submit' => array(
'type' => 'submitcancel',
'value' => array(get_string('save'), get_string('cancel')),
'goto' => $goto,
),
)
));
function edit_comment_submit(Pieform $form, $values) {
global $viewid, $comment, $onartefact, $SESSION, $goto;
db_begin();
$comment->set('description', $values['message']);
$comment->set('private', 1 - (int) $values['ispublic']);
$comment->commit();
require_once('activity.php');
$data = (object) array(
'message' => html2text($comment->get('description')),
'view' => $viewid,
);
if ($onartefact) {
$data->onartefact = $onartefact;
}
else {
$data->onview = $viewid;
}
activity_occurred('feedback', $data);
db_commit();
$SESSION->add_ok_msg(get_string('commentupdated', 'artefact.comment'));
redirect($goto);
}
$smarty = smarty();
$smarty->assign('PAGEHEADING', hsc(TITLE));
$smarty->assign('strdescription', get_string('editcommentdescription', 'artefact.comment', $maxage));
$smarty->assign('form', $form);
$smarty->display('artefact:comment:edit.tpl');
?>
......@@ -34,10 +34,17 @@ $string['comments'] = 'comments';
$string['attachfile'] = "Attach file";
$string['Attachments'] = "Attachments";
$string['cantedithasreplies'] = 'You can only edit the most recent comment';
$string['canteditnotauthor'] = 'You are not the author of this comment';
$string['cantedittooold'] = 'You can only edit comments that are less than %d minutes old';
$string['commentnotinview'] = 'Comment %d not in view %d';
$string['commentremoved'] = 'Comment removed';
$string['commentremovedbyauthor'] = 'Comment removed by the author';
$string['commentremovedbyowner'] = 'Comment removed by the owner';
$string['commentremovedbyadmin'] = 'Comment removed by an administrator';
$string['commentupdated'] = 'Comment updated';
$string['editcomment'] = 'Edit Comment';
$string['editcommentdescription'] = 'You can update your comments if they are less than %d minutes old and have had no newer replies added. After this time you may still be able to delete your comments and add new ones.';
$string['feedback'] = 'Feedback';
$string['feedbackattachdirname'] = 'commentattachments';
$string['feedbackattachdirdesc'] = 'Files attached to comments on your portfolio';
......
......@@ -52,6 +52,7 @@ class PluginArtefactComment extends PluginArtefact {
public static function postinst($prevversion) {
if ($prevversion == 0) {
set_config_plugin('artefact', 'comment', 'commenteditabletime', 10);
foreach(ArtefactTypeComment::deleted_types() as $type) {
insert_record('artefact_comment_deletedby', (object)array('name' => $type));
}
......@@ -232,6 +233,24 @@ class ArtefactTypeComment extends ArtefactType {
return $result;
}
public static function last_public_comment($view=null, $artefact=null) {
if (!empty($artefact)) {
$where = 'c.onartefact = ?';
$values = array($artefact);
}
else {
$where = 'c.onview = ?';
$values = array($view);
}
$newest = get_records_sql_array('
SELECT a.id, a.ctime
FROM {artefact} a INNER JOIN {artefact_comment_comment} c ON a.id = c.artefact
WHERE c.private = 0 AND ' . $where . '
ORDER BY a.ctime DESC', $values, 0, 1
);
return $newest[0];
}
public static function build_html(&$data) {
global $USER;
$candelete = $data->canedit || $USER->get('admin');
......@@ -241,8 +260,12 @@ class ArtefactTypeComment extends ArtefactType {
'admin' => get_string('commentremovedbyadmin', 'artefact.comment'),
);
$authors = array();
$lastcomment = self::last_public_comment($data->view, $data->artefact);
$editableafter = time() - 60 * get_config_plugin('artefact', 'comment', 'commenteditabletime');
foreach ($data->data as &$item) {
$item->date = format_date(strtotime($item->ctime), 'strftimedatetime');
$item->ts = strtotime($item->ctime);
$item->date = format_date($item->ts, 'strftimedatetime');
$item->isauthor = $item->author && $item->author == $USER->get('id');
if (!empty($item->attachments)) {
if ($data->isowner) {
$item->attachmessage = get_string(
......@@ -260,15 +283,19 @@ class ArtefactTypeComment extends ArtefactType {
if ($item->private) {
$item->pubmessage = get_string('thisfeedbackisprivate', 'artefact.comment');
}
else if (!$item->private && $data->canedit) {
$item->pubmessage = get_string('thisfeedbackispublic', 'artefact.comment');
$item->makeprivateform = pieform(self::make_private_form($item->id));
// Comment authors can edit recent comments if they're private or if no one has replied yet.
if ($item->isauthor && ($item->private || $item->id == $lastcomment->id) && $item->ts > $editableafter) {
$item->canedit = 1;
}
// $item->makeprivateform = pieform(self::make_private_form($item->id));
if ($item->deletedby) {
$item->deleted = 1;
$item->deletedmessage = $deletedmessage[$item->deletedby];
}
else if ($candelete || ($item->author && $item->author == $USER->get('id'))) {
else if ($candelete || $item->isauthor) {
$item->deleteform = pieform(self::delete_comment_form($item->id));
}
if ($item->author) {
......@@ -306,6 +333,7 @@ class ArtefactTypeComment extends ArtefactType {
$smarty = smarty_core();
$smarty->assign_by_ref('data', $data->data);
$smarty->assign('canedit', $data->canedit);
$smarty->assign('viewid', $data->view);
$smarty->assign('baseurl', $data->baseurl);
$data->tablerows = $smarty->fetch('artefact:comment:commentlist.tpl');
$pagination = build_pagination(array(
......
......@@ -29,6 +29,7 @@
{if $.foreach.attachments.first} | {str tag=Attachments section=artefact.comment}:{else},{/if} <a href="{$WWWROOT}artefact/file/download.php?file={$a->attachid}">{$a->attachtitle|escape}</a> ({$a->attachsize|escape})
{/foreach}
{/strip}
{if $item->canedit} | <a href="{$WWWROOT}artefact/comment/edit.php?id={$item->id}&view={$viewid}">{str tag=edit}</a>{/if}
{/if}
</div>
</td>
......
{include file="header.tpl"}
<p>{$strdescription}</p>
{$form}
{include file="footer.tpl"}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment