comment_ajax.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /*
  17. * Handling all ajax request for comments API
  18. *
  19. * @package core
  20. * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. define('AJAX_SCRIPT', true);
  24. define('NO_DEBUG_DISPLAY', true);
  25. require_once('../config.php');
  26. require_once($CFG->dirroot . '/comment/lib.php');
  27. $contextid = optional_param('contextid', SYSCONTEXTID, PARAM_INT);
  28. $action = optional_param('action', '', PARAM_ALPHA);
  29. if (empty($CFG->usecomments)) {
  30. throw new comment_exception('commentsnotenabled', 'moodle');
  31. }
  32. list($context, $course, $cm) = get_context_info_array($contextid);
  33. if ( $contextid == SYSCONTEXTID ) {
  34. $course = $SITE;
  35. }
  36. $PAGE->set_url('/comment/comment_ajax.php');
  37. // Allow anonymous user to view comments providing forcelogin now enabled
  38. require_course_login($course, true, $cm);
  39. $PAGE->set_context($context);
  40. if (!empty($cm)) {
  41. $PAGE->set_cm($cm, $course);
  42. } else if (!empty($course)) {
  43. $PAGE->set_course($course);
  44. }
  45. if (!confirm_sesskey()) {
  46. $error = array('error'=>get_string('invalidsesskey', 'error'));
  47. die(json_encode($error));
  48. }
  49. $client_id = required_param('client_id', PARAM_ALPHANUM);
  50. $area = optional_param('area', '', PARAM_AREA);
  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. $component = optional_param('component', '', PARAM_COMPONENT);
  56. // initilising comment object
  57. $args = new stdClass;
  58. $args->context = $context;
  59. $args->course = $course;
  60. $args->cm = $cm;
  61. $args->area = $area;
  62. $args->itemid = $itemid;
  63. $args->client_id = $client_id;
  64. $args->component = $component;
  65. $manager = new comment($args);
  66. echo $OUTPUT->header(); // send headers
  67. // process ajax request
  68. switch ($action) {
  69. case 'add':
  70. if ($manager->can_post()) {
  71. $result = $manager->add($content);
  72. if (!empty($result) && is_object($result)) {
  73. $result->count = $manager->count();
  74. $result->client_id = $client_id;
  75. echo json_encode($result);
  76. die();
  77. }
  78. }
  79. break;
  80. case 'delete':
  81. $comment_record = $DB->get_record('comments', array('id'=>$commentid));
  82. if ($manager->can_delete($commentid) || $comment_record->userid == $USER->id) {
  83. if ($manager->delete($commentid)) {
  84. $result = array(
  85. 'client_id' => $client_id,
  86. 'commentid' => $commentid
  87. );
  88. echo json_encode($result);
  89. die();
  90. }
  91. }
  92. break;
  93. case 'get':
  94. default:
  95. if ($manager->can_view()) {
  96. $comments = $manager->get_comments($page);
  97. $result = array(
  98. 'list' => $comments,
  99. 'count' => $manager->count(),
  100. 'pagination' => $manager->get_pagination($page),
  101. 'client_id' => $client_id
  102. );
  103. echo json_encode($result);
  104. die();
  105. }
  106. break;
  107. }
  108. if (!isloggedin()) {
  109. // tell user to log in to view comments
  110. echo json_encode(array('error'=>'require_login'));
  111. }
  112. // ignore request
  113. die;