MDL-26854 COMMENT
1. added pluginname_comment_validate callback to comments api
2. change permission callback defaults to false
3. Tidied up serveal areas of comments and implemented callback to
ignore system permissions for view (credits to Sam Hemelryk)
AMOS BEGIN
MOV [modulerejectcomment,error],[callbackrejectcomment,error]
AMOS END
This commit is contained in:
+64
-56
@@ -1,5 +1,4 @@
|
||||
<?php
|
||||
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
@@ -24,87 +23,96 @@ require_once('../config.php');
|
||||
require_once($CFG->dirroot . '/comment/lib.php');
|
||||
|
||||
$contextid = optional_param('contextid', SYSCONTEXTID, PARAM_INT);
|
||||
$action = optional_param('action', '', PARAM_ALPHA);
|
||||
|
||||
if (empty($CFG->usecomments)) {
|
||||
throw new comment_exception('commentsnotenabled', 'moodle');
|
||||
}
|
||||
|
||||
list($context, $course, $cm) = get_context_info_array($contextid);
|
||||
|
||||
$PAGE->set_context($context);
|
||||
$PAGE->set_url('/comment/comment_ajax.php');
|
||||
|
||||
$action = optional_param('action', '', PARAM_ALPHA);
|
||||
// Allow anonymous user to view comments providing forcelogin now enabled
|
||||
require_course_login($course, true, $cm);
|
||||
$PAGE->set_context($context);
|
||||
if (!empty($cm)) {
|
||||
$PAGE->set_cm($cm, $course);
|
||||
} else if (!empty($course)) {
|
||||
$PAGE->set_course($course);
|
||||
}
|
||||
|
||||
if (!confirm_sesskey()) {
|
||||
$error = array('error'=>get_string('invalidsesskey'));
|
||||
$error = array('error'=>get_string('invalidsesskey', 'error'));
|
||||
die(json_encode($error));
|
||||
}
|
||||
|
||||
if (!isloggedin()) {
|
||||
// display comments on front page without permission check
|
||||
if ($action == 'get') {
|
||||
if ($context->id == get_context_instance(CONTEXT_COURSE, SITEID)->id) {
|
||||
$ignore_permission = true;
|
||||
} else {
|
||||
// tell user to log in to view comments
|
||||
$ignore_permission = false;
|
||||
echo json_encode(array('error'=>'require_login'));
|
||||
die;
|
||||
}
|
||||
} else {
|
||||
// ignore request
|
||||
die;
|
||||
}
|
||||
} else {
|
||||
$ignore_permission = false;
|
||||
}
|
||||
|
||||
$client_id = required_param('client_id', PARAM_ALPHANUM);
|
||||
$area = optional_param('area', '', PARAM_ALPHAEXT);
|
||||
$client_id = optional_param('client_id', '', PARAM_RAW);
|
||||
$commentid = optional_param('commentid', -1, PARAM_INT);
|
||||
$content = optional_param('content', '', PARAM_RAW);
|
||||
$itemid = optional_param('itemid', '', PARAM_INT);
|
||||
$page = optional_param('page', 0, PARAM_INT);
|
||||
$component = optional_param('component', '', PARAM_ALPHAEXT);
|
||||
|
||||
echo $OUTPUT->header(); // send headers
|
||||
|
||||
// initilising comment object
|
||||
if (!empty($client_id)) {
|
||||
$args = new stdClass();
|
||||
$args->context = $context;
|
||||
$args->course = $course;
|
||||
$args->cm = $cm;
|
||||
$args->area = $area;
|
||||
$args->itemid = $itemid;
|
||||
$args->client_id = $client_id;
|
||||
$args->component = $component;
|
||||
// only for comments in frontpage
|
||||
$args->ignore_permission = $ignore_permission;
|
||||
$manager = new comment($args);
|
||||
} else {
|
||||
die;
|
||||
}
|
||||
$args = new stdClass;
|
||||
$args->context = $context;
|
||||
$args->course = $course;
|
||||
$args->cm = $cm;
|
||||
$args->area = $area;
|
||||
$args->itemid = $itemid;
|
||||
$args->client_id = $client_id;
|
||||
$args->component = $component;
|
||||
$manager = new comment($args);
|
||||
|
||||
echo $OUTPUT->header(); // send headers
|
||||
|
||||
// process ajax request
|
||||
switch ($action) {
|
||||
case 'add':
|
||||
$result = $manager->add($content);
|
||||
if (!empty($result) && is_object($result)) {
|
||||
$result->count = $manager->count();
|
||||
$result->client_id = $client_id;
|
||||
echo json_encode($result);
|
||||
if ($manager->can_post()) {
|
||||
$result = $manager->add($content);
|
||||
if (!empty($result) && is_object($result)) {
|
||||
$result->count = $manager->count();
|
||||
$result->client_id = $client_id;
|
||||
echo json_encode($result);
|
||||
die();
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'delete':
|
||||
$result = $manager->delete($commentid);
|
||||
if ($result === true) {
|
||||
echo json_encode(array('client_id'=>$client_id, 'commentid'=>$commentid));
|
||||
$comment_record = $DB->get_record('comments', array('id'=>$commentid));
|
||||
if ($manager->can_delete($commentid) || $comment_record->userid == $USER->id) {
|
||||
if ($manager->delete($commentid)) {
|
||||
$result = array(
|
||||
'client_id' => $client_id,
|
||||
'commentid' => $commentid
|
||||
);
|
||||
echo json_encode($result);
|
||||
die();
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'get':
|
||||
default:
|
||||
$result = array();
|
||||
$comments = $manager->get_comments($page);
|
||||
$result['list'] = $comments;
|
||||
$result['count'] = $manager->count();
|
||||
$result['pagination'] = $manager->get_pagination($page);
|
||||
$result['client_id'] = $client_id;
|
||||
echo json_encode($result);
|
||||
if ($manager->can_view()) {
|
||||
$comments = $manager->get_comments($page);
|
||||
$result = array(
|
||||
'list' => $comments,
|
||||
'count' => $manager->count(),
|
||||
'pagination' => $manager->get_pagination($page),
|
||||
'client_id' => $client_id
|
||||
);
|
||||
echo json_encode($result);
|
||||
die();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (!isloggedin()) {
|
||||
// tell user to log in to view comments
|
||||
echo json_encode(array('error'=>'require_login'));
|
||||
}
|
||||
// ignore request
|
||||
die;
|
||||
|
||||
Reference in New Issue
Block a user