"MDL-23915 fixed js error in comment"
[moodle.git] / comment / comment_ajax.php
CommitLineData
1bcb7eb5 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
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.
9//
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.
14//
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/>.
17
18/*
19 * Handling all ajax request for comments API
20 */
2696b976
PS
21define('AJAX_SCRIPT', true);
22
1bcb7eb5 23require_once('../config.php');
36051c9e 24require_once($CFG->dirroot . '/comment/lib.php');
1bcb7eb5 25
1bcb7eb5 26$contextid = optional_param('contextid', SYSCONTEXTID, PARAM_INT);
57588523 27list($context, $course, $cm) = get_context_info_array($contextid);
1bcb7eb5 28
467c85d7
DC
29$PAGE->set_context($context);
30$PAGE->set_url('/comment/comment_ajax.php');
31
80555111 32require_login($course, true, $cm);
2696b976 33require_sesskey();
1bcb7eb5 34
467c85d7
DC
35$action = optional_param('action', '', PARAM_ALPHA);
36$area = optional_param('area', '', PARAM_ALPHAEXT);
37$client_id = optional_param('client_id', '', PARAM_RAW);
38$commentid = optional_param('commentid', -1, PARAM_INT);
39$content = optional_param('content', '', PARAM_RAW);
40$itemid = optional_param('itemid', '', PARAM_INT);
41$page = optional_param('page', 0, PARAM_INT);
1bcb7eb5 42
bce08d9a
PS
43echo $OUTPUT->header(); // send headers
44
467c85d7 45// initilising comment object
1bcb7eb5 46if (!empty($client_id)) {
467c85d7
DC
47 $args = new stdclass;
48 $args->context = $context;
49 $args->course = $course;
50 $args->cm = $cm;
51 $args->area = $area;
52 $args->itemid = $itemid;
53 $args->client_id = $client_id;
54 $manager = new comment($args);
866354a9
DC
55} else {
56 die;
1bcb7eb5 57}
866354a9 58
467c85d7 59// process ajax request
1bcb7eb5 60switch ($action) {
2696b976 61 case 'add':
467c85d7
DC
62 $result = $manager->add($content);
63 if (!empty($result) && is_object($result)) {
64 $result->count = $manager->count();
65 $result->client_id = $client_id;
66 echo json_encode($result);
15894c65 67 }
2696b976
PS
68 break;
69 case 'delete':
467c85d7 70 $result = $manager->delete($commentid);
15894c65 71 if ($result === true) {
72 echo json_encode(array('client_id'=>$client_id, 'commentid'=>$commentid));
73 }
2696b976
PS
74 break;
75 case 'get':
76 default:
467c85d7
DC
77 $result = array();
78 $comments = $manager->get_comments($page);
79 $result['list'] = $comments;
80 $result['count'] = $manager->count();
81 $result['pagination'] = $manager->get_pagination($page);
82 $result['client_id'] = $client_id;
83 echo json_encode($result);
1bcb7eb5 84}