MDL-18044 - updating missed scalestip string to scalestip2
[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;
42 $cmt->contextid = $contextid;
08a22be6
DC
43 if (!empty($course)) {
44 $cmt->courseid = $course->id;
45 }
1bcb7eb5 46 $cmt->area = $area;
47 $cmt->itemid = $itemid;
48 $cmt->client_id = $client_id;
49 $comment = new comment($cmt);
50}
51switch ($action) {
2696b976 52 case 'add':
15894c65 53 $cmt = $comment->add($content);
54 $cmt->count = $comment->count();
55 if (!empty($cmt) && is_object($cmt)) {
56 $cmt->client_id = $client_id;
57 echo json_encode($cmt);
58 }
2696b976
PS
59 break;
60 case 'delete':
15894c65 61 $result = $comment->delete($commentid);
62 if ($result === true) {
63 echo json_encode(array('client_id'=>$client_id, 'commentid'=>$commentid));
64 }
2696b976
PS
65 break;
66 case 'get':
67 default:
68 $ret = array();
15894c65 69 $comments = $comment->get_comments($page);
70 $ret['list'] = $comments;
71 $ret['count'] = $comment->count();
72 $ret['pagination'] = $comment->get_pagination($page);
73 $ret['client_id'] = $client_id;
74 echo json_encode($ret);
1bcb7eb5 75}