navigation MDL-14632 Fix: Dock now respects block weight
[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
80555111 29require_login($course, true, $cm);
2696b976 30require_sesskey();
1bcb7eb5 31
1bcb7eb5 32$action = optional_param('action', '', PARAM_ALPHA);
33$area = optional_param('area', '', PARAM_ALPHAEXT);
34$client_id = optional_param('client_id', '', PARAM_RAW);
35$commentid = optional_param('commentid', -1, PARAM_INT);
36$content = optional_param('content', '', PARAM_RAW);
37$itemid = optional_param('itemid', '', PARAM_INT);
38$page = optional_param('page', 0, PARAM_INT);
39
40if (!empty($client_id)) {
41 $cmt = new stdclass;
866354a9
DC
42 $cmt->context = $context;
43 $cmt->course = $course;
44 $cmt->cm = $cm;
1bcb7eb5 45 $cmt->area = $area;
46 $cmt->itemid = $itemid;
47 $cmt->client_id = $client_id;
48 $comment = new comment($cmt);
866354a9
DC
49} else {
50 die;
1bcb7eb5 51}
866354a9 52
1bcb7eb5 53switch ($action) {
2696b976 54 case 'add':
15894c65 55 $cmt = $comment->add($content);
15894c65 56 if (!empty($cmt) && is_object($cmt)) {
866354a9 57 $cmt->count = $comment->count();
15894c65 58 $cmt->client_id = $client_id;
59 echo json_encode($cmt);
60 }
2696b976
PS
61 break;
62 case 'delete':
15894c65 63 $result = $comment->delete($commentid);
64 if ($result === true) {
65 echo json_encode(array('client_id'=>$client_id, 'commentid'=>$commentid));
66 }
2696b976
PS
67 break;
68 case 'get':
69 default:
70 $ret = array();
15894c65 71 $comments = $comment->get_comments($page);
72 $ret['list'] = $comments;
73 $ret['count'] = $comment->count();
74 $ret['pagination'] = $comment->get_pagination($page);
75 $ret['client_id'] = $client_id;
76 echo json_encode($ret);
1bcb7eb5 77}