2009-07-24 02:44:44 +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/>.
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handling all ajax request for comments API
|
|
|
|
*/
|
2010-03-28 09:52:50 +00:00
|
|
|
define('AJAX_SCRIPT', true);
|
|
|
|
|
2009-07-24 02:44:44 +00:00
|
|
|
require_once('../config.php');
|
2010-03-15 07:59:28 +00:00
|
|
|
require_once($CFG->dirroot . '/comment/lib.php');
|
2009-07-24 02:44:44 +00:00
|
|
|
|
|
|
|
$contextid = optional_param('contextid', SYSCONTEXTID, PARAM_INT);
|
2010-03-16 07:57:45 +00:00
|
|
|
list($context, $course, $cm) = get_context_info_array($contextid);
|
2009-07-24 02:44:44 +00:00
|
|
|
|
2010-04-14 05:01:43 +00:00
|
|
|
$PAGE->set_context($context);
|
|
|
|
$PAGE->set_url('/comment/comment_ajax.php');
|
|
|
|
|
2010-10-07 08:18:56 +00:00
|
|
|
$action = optional_param('action', '', PARAM_ALPHA);
|
2010-08-25 05:56:48 +00:00
|
|
|
|
2010-10-07 08:18:56 +00:00
|
|
|
if (!confirm_sesskey()) {
|
|
|
|
$error = array('error'=>get_string('invalidsesskey'));
|
|
|
|
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;
|
|
|
|
}
|
2010-08-26 09:25:13 +00:00
|
|
|
} else {
|
2010-10-07 08:18:56 +00:00
|
|
|
$ignore_permission = false;
|
2010-08-25 03:50:19 +00:00
|
|
|
}
|
2009-07-24 02:44:44 +00:00
|
|
|
|
2010-04-14 05:01:43 +00:00
|
|
|
$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);
|
2010-08-25 06:24:17 +00:00
|
|
|
$component = optional_param('component', '', PARAM_ALPHAEXT);
|
2009-07-24 02:44:44 +00:00
|
|
|
|
2010-08-10 08:50:08 +00:00
|
|
|
echo $OUTPUT->header(); // send headers
|
|
|
|
|
2010-04-14 05:01:43 +00:00
|
|
|
// initilising comment object
|
2009-07-24 02:44:44 +00:00
|
|
|
if (!empty($client_id)) {
|
2010-09-21 08:54:01 +00:00
|
|
|
$args = new stdClass();
|
2010-04-14 05:01:43 +00:00
|
|
|
$args->context = $context;
|
|
|
|
$args->course = $course;
|
|
|
|
$args->cm = $cm;
|
|
|
|
$args->area = $area;
|
|
|
|
$args->itemid = $itemid;
|
|
|
|
$args->client_id = $client_id;
|
2010-08-25 06:24:17 +00:00
|
|
|
$args->component = $component;
|
2010-08-25 07:47:22 +00:00
|
|
|
// only for comments in frontpage
|
|
|
|
$args->ignore_permission = $ignore_permission;
|
2010-04-14 05:01:43 +00:00
|
|
|
$manager = new comment($args);
|
2010-04-09 09:03:51 +00:00
|
|
|
} else {
|
|
|
|
die;
|
2009-07-24 02:44:44 +00:00
|
|
|
}
|
2010-04-09 09:03:51 +00:00
|
|
|
|
2010-04-14 05:01:43 +00:00
|
|
|
// process ajax request
|
2009-07-24 02:44:44 +00:00
|
|
|
switch ($action) {
|
2010-03-28 09:52:50 +00:00
|
|
|
case 'add':
|
2010-04-14 05:01:43 +00:00
|
|
|
$result = $manager->add($content);
|
|
|
|
if (!empty($result) && is_object($result)) {
|
|
|
|
$result->count = $manager->count();
|
|
|
|
$result->client_id = $client_id;
|
|
|
|
echo json_encode($result);
|
2009-10-12 05:46:34 +00:00
|
|
|
}
|
2010-03-28 09:52:50 +00:00
|
|
|
break;
|
|
|
|
case 'delete':
|
2010-04-14 05:01:43 +00:00
|
|
|
$result = $manager->delete($commentid);
|
2009-10-12 05:46:34 +00:00
|
|
|
if ($result === true) {
|
|
|
|
echo json_encode(array('client_id'=>$client_id, 'commentid'=>$commentid));
|
|
|
|
}
|
2010-03-28 09:52:50 +00:00
|
|
|
break;
|
|
|
|
case 'get':
|
|
|
|
default:
|
2010-04-14 05:01:43 +00:00
|
|
|
$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);
|
2009-07-24 02:44:44 +00:00
|
|
|
}
|