3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Handling all ajax request for comments API
21 define('AJAX_SCRIPT', true);
23 require_once('../config.php');
24 require_once($CFG->dirroot . '/comment/lib.php');
26 $contextid = optional_param('contextid', SYSCONTEXTID, PARAM_INT);
27 list($context, $course, $cm) = get_context_info_array($contextid);
29 $PAGE->set_context($context);
30 $PAGE->set_url('/comment/comment_ajax.php');
32 // XXX: display comments in frontpage without login
33 if ($context->id != get_context_instance(CONTEXT_COURSE, SITEID)->id
35 or $action == 'delete') {
36 require_login($course, true, $cm);
40 $action = optional_param('action', '', PARAM_ALPHA);
41 $area = optional_param('area', '', PARAM_ALPHAEXT);
42 $client_id = optional_param('client_id', '', PARAM_RAW);
43 $commentid = optional_param('commentid', -1, PARAM_INT);
44 $content = optional_param('content', '', PARAM_RAW);
45 $itemid = optional_param('itemid', '', PARAM_INT);
46 $page = optional_param('page', 0, PARAM_INT);
48 echo $OUTPUT->header(); // send headers
50 // initilising comment object
51 if (!empty($client_id)) {
53 $args->context = $context;
54 $args->course = $course;
57 $args->itemid = $itemid;
58 $args->client_id = $client_id;
59 $manager = new comment($args);
64 // process ajax request
67 $result = $manager->add($content);
68 if (!empty($result) && is_object($result)) {
69 $result->count = $manager->count();
70 $result->client_id = $client_id;
71 echo json_encode($result);
75 $result = $manager->delete($commentid);
76 if ($result === true) {
77 echo json_encode(array('client_id'=>$client_id, 'commentid'=>$commentid));
83 $comments = $manager->get_comments($page);
84 $result['list'] = $comments;
85 $result['count'] = $manager->count();
86 $result['pagination'] = $manager->get_pagination($page);
87 $result['client_id'] = $client_id;
88 echo json_encode($result);