PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/comment/classes/external.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 153 lines | 80 code | 16 blank | 57 comment | 2 complexity | 7f4a4d68bbcec7d9f6ff74a23e840de6 MD5 | raw file
  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. * External comment API
  18. *
  19. * @package core_comment
  20. * @category external
  21. * @copyright Costantino Cito <ccito@cvaconsulting.com>
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. * @since Moodle 2.9
  24. */
  25. defined('MOODLE_INTERNAL') || die();
  26. require_once("$CFG->libdir/externallib.php");
  27. require_once("$CFG->dirroot/comment/lib.php");
  28. /**
  29. * External comment API functions
  30. *
  31. * @package core_comment
  32. * @category external
  33. * @copyright Costantino Cito <ccito@cvaconsulting.com>
  34. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35. * @since Moodle 2.9
  36. */
  37. class core_comment_external extends external_api {
  38. /**
  39. * Returns description of method parameters
  40. *
  41. * @return external_function_parameters
  42. * @since Moodle 2.9
  43. */
  44. public static function get_comments_parameters() {
  45. return new external_function_parameters(
  46. array(
  47. 'contextlevel' => new external_value(PARAM_ALPHA, 'contextlevel system, course, user...'),
  48. 'instanceid' => new external_value(PARAM_INT, 'the Instance id of item associated with the context level'),
  49. 'component' => new external_value(PARAM_COMPONENT, 'component'),
  50. 'itemid' => new external_value(PARAM_INT, 'associated id'),
  51. 'area' => new external_value(PARAM_AREA, 'string comment area', VALUE_DEFAULT, ''),
  52. 'page' => new external_value(PARAM_INT, 'page number (0 based)', VALUE_DEFAULT, 0),
  53. )
  54. );
  55. }
  56. /**
  57. * Return a list of comments
  58. *
  59. * @param string $contextlevel ('system, course, user', etc..)
  60. * @param int $instanceid
  61. * @param string $component the name of the component
  62. * @param int $itemid the item id
  63. * @param string $area comment area
  64. * @param int $page page number
  65. * @return array of comments and warnings
  66. * @since Moodle 2.9
  67. */
  68. public static function get_comments($contextlevel, $instanceid, $component, $itemid, $area = '', $page = 0) {
  69. $warnings = array();
  70. $arrayparams = array(
  71. 'contextlevel' => $contextlevel,
  72. 'instanceid' => $instanceid,
  73. 'component' => $component,
  74. 'itemid' => $itemid,
  75. 'area' => $area,
  76. 'page' => $page
  77. );
  78. $params = self::validate_parameters(self::get_comments_parameters(), $arrayparams);
  79. $context = self::get_context_from_params($params);
  80. self::validate_context($context);
  81. require_capability('moodle/comment:view', $context);
  82. $args = new stdClass;
  83. $args->context = $context;
  84. $args->area = $params['area'];
  85. $args->itemid = $params['itemid'];
  86. $args->component = $params['component'];
  87. $commentobject = new comment($args);
  88. $comments = $commentobject->get_comments($params['page']);
  89. // False means no permissions to see comments.
  90. if ($comments === false) {
  91. throw new moodle_exception('nopermissions', 'error', '', 'view comments');
  92. }
  93. foreach ($comments as $key => $comment) {
  94. list($comments[$key]->content, $comments[$key]->format) = external_format_text($comment->content,
  95. $comment->format,
  96. $context->id,
  97. $params['component'],
  98. '',
  99. 0);
  100. }
  101. $results = array(
  102. 'comments' => $comments,
  103. 'warnings' => $warnings
  104. );
  105. return $results;
  106. }
  107. /**
  108. * Returns description of method result value
  109. *
  110. * @return external_description
  111. * @since Moodle 2.9
  112. */
  113. public static function get_comments_returns() {
  114. return new external_single_structure(
  115. array(
  116. 'comments' => new external_multiple_structure(
  117. new external_single_structure(
  118. array(
  119. 'id' => new external_value(PARAM_INT, 'Comment ID'),
  120. 'content' => new external_value(PARAM_RAW, 'The content text formated'),
  121. 'format' => new external_format_value('content'),
  122. 'timecreated' => new external_value(PARAM_INT, 'Time created (timestamp)'),
  123. 'strftimeformat' => new external_value(PARAM_NOTAGS, 'Time format'),
  124. 'profileurl' => new external_value(PARAM_URL, 'URL profile'),
  125. 'fullname' => new external_value(PARAM_NOTAGS, 'fullname'),
  126. 'time' => new external_value(PARAM_NOTAGS, 'Time in human format'),
  127. 'avatar' => new external_value(PARAM_RAW, 'HTML user picture'),
  128. 'userid' => new external_value(PARAM_INT, 'User ID'),
  129. 'delete' => new external_value(PARAM_BOOL, 'Permission to delete=true/false', VALUE_OPTIONAL)
  130. ), 'comment'
  131. ), 'List of comments'
  132. ),
  133. 'warnings' => new external_warnings()
  134. )
  135. );
  136. }
  137. }