"MDL-19118, comments api"
[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 */
21require_once('../config.php');
22require_once($CFG->libdir.'/commentlib.php');
23
24$courseid = optional_param('courseid', SITEID, PARAM_INT);
25$contextid = optional_param('contextid', SYSCONTEXTID, PARAM_INT);
26
27$context = get_context_instance_by_id($contextid);
28$cm = get_coursemodule_from_id('', $context->instanceid);
29require_login($courseid, true, $cm);
30
31$err = new stdclass;
32
33if (!confirm_sesskey()) {
34 $err->error = get_string('invalidsesskey');
35 die(json_encode($err));
36}
37
38if (!isloggedin()){
39 $err->error = get_string('loggedinnot');
40 die(json_encode($err));
41}
42
43if (isguestuser()) {
44 $err->error = get_string('loggedinnot');
45 die(json_encode($err));
46}
47
48$action = optional_param('action', '', PARAM_ALPHA);
49$area = optional_param('area', '', PARAM_ALPHAEXT);
50$client_id = optional_param('client_id', '', PARAM_RAW);
51$commentid = optional_param('commentid', -1, PARAM_INT);
52$content = optional_param('content', '', PARAM_RAW);
53$itemid = optional_param('itemid', '', PARAM_INT);
54$page = optional_param('page', 0, PARAM_INT);
55
56if (!empty($client_id)) {
57 $cmt = new stdclass;
58 $cmt->contextid = $contextid;
59 $cmt->courseid = $courseid;
60 $cmt->area = $area;
61 $cmt->itemid = $itemid;
62 $cmt->client_id = $client_id;
63 $comment = new comment($cmt);
64}
65switch ($action) {
66case 'add':
67 $cmt = $comment->add($content);
68 if (!empty($cmt) && is_object($cmt)) {
69 $cmt->client_id = $client_id;
70 echo json_encode($cmt);
71 } else if ($cmt === COMMENT_ERROR_DB) {
72 $err->error = get_string('dbupdatefailed');
73 echo json_encode($err);
74 } else if ($cmt === COMMENT_ERROR_MODULE_REJECT) {
75 $err->error = get_string('modulererejectcomment');
76 echo json_encode($err);
77 } else if ($cmt === COMMENT_ERROR_INSUFFICIENT_CAPS) {
78 $err->error = get_string('nopermissiontocomment');
79 echo json_encode($err);
80 }
81 break;
82case 'delete':
83 $result = $comment->delete($commentid);
84 if ($result === true) {
85 echo json_encode(array('client_id'=>$client_id, 'commentid'=>$commentid));
86 } else if ($result == COMMENT_ERROR_INSUFFICIENT_CAPS) {
87 $err->error = get_string('nopermissiontoeditcomment');
88 echo json_encode($err);
89 } else if ($result == COMMENT_ERROR_DB) {
90 $err->error = get_string('dbupdatefailed');
91 echo json_encode($err);
92 }
93 break;
94case 'get':
95default:
96 $ret = array();
97 $comments = $comment->get_comments($page);
98 $ret['list'] = $comments;
99 $ret['pagination'] = $comment->get_pagination($page);
100 $ret['client_id'] = $client_id;
101 echo json_encode($ret);
102 exit;
103}