PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/rating/classes/external/util.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 189 lines | 130 code | 13 blank | 46 comment | 8 complexity | 4880860a2a4af3ef5273e1eb7ba01b6c 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. * Rating external functions utility class.
  18. *
  19. * @package core_rating
  20. * @copyright 2017 Juan Leyva
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. namespace core_rating\external;
  24. defined('MOODLE_INTERNAL') || die();
  25. require_once($CFG->dirroot . '/rating/lib.php');
  26. require_once($CFG->libdir . '/externallib.php');
  27. use external_multiple_structure;
  28. use external_single_structure;
  29. use external_value;
  30. use rating_manager;
  31. use stdClass;
  32. /**
  33. * Rating external functions utility class.
  34. *
  35. * @package core_rating
  36. * @copyright 2017 Juan Leyva
  37. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38. * @since Moodle 3.4
  39. */
  40. class util {
  41. /**
  42. * Returns the ratings definition for external functions.
  43. */
  44. public static function external_ratings_structure() {
  45. return new external_single_structure (
  46. [
  47. 'contextid' => new external_value(PARAM_INT, 'Context id.'),
  48. 'component' => new external_value(PARAM_COMPONENT, 'Context name.'),
  49. 'ratingarea' => new external_value(PARAM_AREA, 'Rating area name.'),
  50. 'canviewall' => new external_value(PARAM_BOOL, 'Whether the user can view all the individual ratings.',
  51. VALUE_OPTIONAL),
  52. 'canviewany' => new external_value(PARAM_BOOL, 'Whether the user can view aggregate of ratings of others.',
  53. VALUE_OPTIONAL),
  54. 'scales' => new external_multiple_structure(
  55. new external_single_structure (
  56. [
  57. 'id' => new external_value(PARAM_INT, 'Scale id.'),
  58. 'courseid' => new external_value(PARAM_INT, 'Course id.', VALUE_OPTIONAL),
  59. 'name' => new external_value(PARAM_TEXT, 'Scale name (when a real scale is used).', VALUE_OPTIONAL),
  60. 'max' => new external_value(PARAM_INT, 'Max value for the scale.'),
  61. 'isnumeric' => new external_value(PARAM_BOOL, 'Whether is a numeric scale.'),
  62. 'items' => new external_multiple_structure(
  63. new external_single_structure (
  64. [
  65. 'value' => new external_value(PARAM_INT, 'Scale value/option id.'),
  66. 'name' => new external_value(PARAM_NOTAGS, 'Scale name.'),
  67. ]
  68. ), 'Scale items. Only returned for not numerical scales.', VALUE_OPTIONAL
  69. )
  70. ], 'Scale information'
  71. ), 'Different scales used information', VALUE_OPTIONAL
  72. ),
  73. 'ratings' => new external_multiple_structure(
  74. new external_single_structure (
  75. [
  76. 'itemid' => new external_value(PARAM_INT, 'Item id.'),
  77. 'scaleid' => new external_value(PARAM_INT, 'Scale id.', VALUE_OPTIONAL),
  78. 'userid' => new external_value(PARAM_INT, 'User who rated id.', VALUE_OPTIONAL),
  79. 'aggregate' => new external_value(PARAM_FLOAT, 'Aggregated ratings grade.', VALUE_OPTIONAL),
  80. 'aggregatestr' => new external_value(PARAM_NOTAGS, 'Aggregated ratings as string.', VALUE_OPTIONAL),
  81. 'aggregatelabel' => new external_value(PARAM_NOTAGS, 'The aggregation label.', VALUE_OPTIONAL),
  82. 'count' => new external_value(PARAM_INT, 'Ratings count (used when aggregating).', VALUE_OPTIONAL),
  83. 'rating' => new external_value(PARAM_INT, 'The rating the user gave.', VALUE_OPTIONAL),
  84. 'canrate' => new external_value(PARAM_BOOL, 'Whether the user can rate the item.', VALUE_OPTIONAL),
  85. 'canviewaggregate' => new external_value(PARAM_BOOL, 'Whether the user can view the aggregated grade.',
  86. VALUE_OPTIONAL),
  87. ]
  88. ), 'The ratings', VALUE_OPTIONAL
  89. ),
  90. ], 'Rating information', VALUE_OPTIONAL
  91. );
  92. }
  93. /**
  94. * Returns rating information inside a data structure like the one defined by external_ratings_structure.
  95. *
  96. * @param stdClass $mod course module object
  97. * @param stdClass $context context object
  98. * @param str $component component name
  99. * @param str $ratingarea rating area
  100. * @param array $items items to add ratings
  101. * @return array ratings ready to be returned by external functions.
  102. */
  103. public static function get_rating_info($mod, $context, $component, $ratingarea, $items) {
  104. global $USER;
  105. $ratinginfo = [
  106. 'contextid' => $context->id,
  107. 'component' => $component,
  108. 'ratingarea' => $ratingarea,
  109. 'canviewall' => null,
  110. 'canviewany' => null,
  111. 'scales' => [],
  112. 'ratings' => [],
  113. ];
  114. if ($mod->assessed != RATING_AGGREGATE_NONE) {
  115. $ratingoptions = new stdClass;
  116. $ratingoptions->context = $context;
  117. $ratingoptions->component = $component;
  118. $ratingoptions->ratingarea = $ratingarea;
  119. $ratingoptions->items = $items;
  120. $ratingoptions->aggregate = $mod->assessed;
  121. $ratingoptions->scaleid = $mod->scale;
  122. $ratingoptions->userid = $USER->id;
  123. $ratingoptions->assesstimestart = $mod->assesstimestart;
  124. $ratingoptions->assesstimefinish = $mod->assesstimefinish;
  125. $rm = new rating_manager();
  126. $allitems = $rm->get_ratings($ratingoptions);
  127. foreach ($allitems as $item) {
  128. if (empty($item->rating)) {
  129. continue;
  130. }
  131. $rating = [
  132. 'itemid' => $item->rating->itemid,
  133. 'scaleid' => $item->rating->scaleid,
  134. 'userid' => $item->rating->userid,
  135. 'rating' => $item->rating->rating,
  136. 'canrate' => $item->rating->user_can_rate(),
  137. 'canviewaggregate' => $item->rating->user_can_view_aggregate(),
  138. ];
  139. // Fill the capabilities fields the first time (the rest are the same values because they are not item dependent).
  140. if ($ratinginfo['canviewall'] === null) {
  141. $ratinginfo['canviewall'] = $item->rating->settings->permissions->viewall &&
  142. $item->rating->settings->pluginpermissions->viewall;
  143. $ratinginfo['canviewany'] = $item->rating->settings->permissions->viewany &&
  144. $item->rating->settings->pluginpermissions->viewany;
  145. }
  146. // Return only the information the user can see.
  147. if ($rating['canviewaggregate']) {
  148. $rating['aggregate'] = $item->rating->aggregate;
  149. $rating['aggregatestr'] = $item->rating->get_aggregate_string();
  150. $rating['aggregatelabel'] = $rm->get_aggregate_label($item->rating->settings->aggregationmethod);
  151. $rating['count'] = $item->rating->count;
  152. }
  153. // If the user can rate, return the scale information only one time.
  154. if ($rating['canrate'] &&
  155. !empty($item->rating->settings->scale->id) &&
  156. !isset($ratinginfo['scales'][$item->rating->settings->scale->id])) {
  157. $scale = $item->rating->settings->scale;
  158. // Return only non numeric scales (to avoid return lots of data just including items from 0 to $scale->max).
  159. if (!$scale->isnumeric) {
  160. $scaleitems = [];
  161. foreach ($scale->scaleitems as $value => $name) {
  162. $scaleitems[] = [
  163. 'name' => $name,
  164. 'value' => $value,
  165. ];
  166. }
  167. $scale->items = $scaleitems;
  168. }
  169. $ratinginfo['scales'][$item->rating->settings->scale->id] = (array) $scale;
  170. }
  171. $ratinginfo['ratings'][] = $rating;
  172. }
  173. }
  174. return $ratinginfo;
  175. }
  176. }