navigation MDL-20521 Fixed caching issue with admin tree
[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);
21afd046 28if ($context->contextlevel == CONTEXT_MODULE) {
29 $cm = get_coursemodule_from_id('', $context->instanceid);
30} else {
31 $cm = null;
32}
1bcb7eb5 33require_login($courseid, true, $cm);
34
35$err = new stdclass;
36
37if (!confirm_sesskey()) {
38 $err->error = get_string('invalidsesskey');
39 die(json_encode($err));
40}
41
42if (!isloggedin()){
43 $err->error = get_string('loggedinnot');
44 die(json_encode($err));
45}
46
47if (isguestuser()) {
48 $err->error = get_string('loggedinnot');
49 die(json_encode($err));
50}
51
52$action = optional_param('action', '', PARAM_ALPHA);
53$area = optional_param('area', '', PARAM_ALPHAEXT);
54$client_id = optional_param('client_id', '', PARAM_RAW);
55$commentid = optional_param('commentid', -1, PARAM_INT);
56$content = optional_param('content', '', PARAM_RAW);
57$itemid = optional_param('itemid', '', PARAM_INT);
58$page = optional_param('page', 0, PARAM_INT);
59
60if (!empty($client_id)) {
61 $cmt = new stdclass;
62 $cmt->contextid = $contextid;
63 $cmt->courseid = $courseid;
64 $cmt->area = $area;
65 $cmt->itemid = $itemid;
66 $cmt->client_id = $client_id;
67 $comment = new comment($cmt);
68}
69switch ($action) {
70case 'add':
71 $cmt = $comment->add($content);
b9f6ca70 72 $cmt->count = $comment->count();
1bcb7eb5 73 if (!empty($cmt) && is_object($cmt)) {
74 $cmt->client_id = $client_id;
75 echo json_encode($cmt);
76 } else if ($cmt === COMMENT_ERROR_DB) {
77 $err->error = get_string('dbupdatefailed');
78 echo json_encode($err);
79 } else if ($cmt === COMMENT_ERROR_MODULE_REJECT) {
80 $err->error = get_string('modulererejectcomment');
81 echo json_encode($err);
82 } else if ($cmt === COMMENT_ERROR_INSUFFICIENT_CAPS) {
83 $err->error = get_string('nopermissiontocomment');
84 echo json_encode($err);
85 }
86 break;
87case 'delete':
88 $result = $comment->delete($commentid);
89 if ($result === true) {
90 echo json_encode(array('client_id'=>$client_id, 'commentid'=>$commentid));
91 } else if ($result == COMMENT_ERROR_INSUFFICIENT_CAPS) {
92 $err->error = get_string('nopermissiontoeditcomment');
93 echo json_encode($err);
94 } else if ($result == COMMENT_ERROR_DB) {
95 $err->error = get_string('dbupdatefailed');
96 echo json_encode($err);
97 }
98 break;
99case 'get':
100default:
101 $ret = array();
102 $comments = $comment->get_comments($page);
103 $ret['list'] = $comments;
b9f6ca70 104 $ret['count'] = $comment->count();
1bcb7eb5 105 $ret['pagination'] = $comment->get_pagination($page);
106 $ret['client_id'] = $client_id;
107 echo json_encode($ret);
108 exit;
109}