mirror of
https://github.com/moodle/moodle.git
synced 2025-04-14 13:02:07 +02:00
MDL-49347 rating: Several fixes and code clean up
- Use correct return values types - Fix the warnings declaration - Fix the pluginfile URL
This commit is contained in:
parent
db5b697158
commit
46e41e6339
@ -1007,10 +1007,11 @@ $functions = array(
|
||||
|
||||
// Rating functions.
|
||||
'core_rating_get_item_ratings' => array(
|
||||
'classname' => 'core_rating_external',
|
||||
'methodname' => 'get_item_ratings',
|
||||
'description' => 'Retrieving all the ratings for an item.',
|
||||
'type' => 'read',
|
||||
'classname' => 'core_rating_external',
|
||||
'methodname' => 'get_item_ratings',
|
||||
'description' => 'Retrieve all the ratings for an item.',
|
||||
'type' => 'read',
|
||||
'capabilities' => 'moodle/rating:view'
|
||||
),
|
||||
);
|
||||
|
||||
|
@ -15,7 +15,7 @@
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Completion external API
|
||||
* Rating external API
|
||||
*
|
||||
* @package core_rating
|
||||
* @category external
|
||||
@ -24,11 +24,13 @@
|
||||
* @since Moodle 2.9
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die;
|
||||
|
||||
require_once("$CFG->libdir/externallib.php");
|
||||
require_once("$CFG->dirroot/rating/lib.php");
|
||||
|
||||
/**
|
||||
* Completion external functions
|
||||
* Rating external functions
|
||||
*
|
||||
* @package core_rating
|
||||
* @category external
|
||||
@ -47,26 +49,27 @@ class core_rating_external extends external_api {
|
||||
public static function get_item_ratings_parameters() {
|
||||
return new external_function_parameters (
|
||||
array(
|
||||
'contextlevel' => new external_value(PARAM_ALPHA, 'contextlevel'),
|
||||
'instanceid' => new external_value(PARAM_INT, 'The Instance id of item associated with the context level'),
|
||||
'component' => new external_value(PARAM_COMPONENT, 'component'),
|
||||
'ratingarea' => new external_value(PARAM_AREA, 'Rating area', VALUE_DEFAULT, ''),
|
||||
'itemid' => new external_value(PARAM_INT, 'Associated id'),
|
||||
'scaleid' => new external_value(PARAM_INT, 'Scale id'),
|
||||
'sort' => new external_value(PARAM_TEXT, 'Sort order', VALUE_DEFAULT, ''),
|
||||
'contextlevel' => new external_value(PARAM_ALPHA, 'context level: course, module, user, etc...'),
|
||||
'instanceid' => new external_value(PARAM_INT, 'the instance id of item associated with the context level'),
|
||||
'component' => new external_value(PARAM_COMPONENT, 'component'),
|
||||
'ratingarea' => new external_value(PARAM_AREA, 'rating area'),
|
||||
'itemid' => new external_value(PARAM_INT, 'associated id'),
|
||||
'scaleid' => new external_value(PARAM_INT, 'scale id'),
|
||||
'sort' => new external_value(PARAM_ALPHA, 'sort order (firstname, rating or timemodified)')
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Getting list of ratings for a given item (forum post etc)
|
||||
* @param string $contextlevel ('context_course', etc..)
|
||||
* @param int $instanceid (eg. the 'id' in the 'book' table)
|
||||
* Retrieve a list of ratings for a given item (forum post etc)
|
||||
*
|
||||
* @param string $contextlevel course, module, user...
|
||||
* @param int $instanceid the instance if for the context element
|
||||
* @param string $component the name of the component
|
||||
* @param string|null $ratingarea
|
||||
* @param string $ratingarea rating area
|
||||
* @param int $itemid the item id
|
||||
* @param int $scaleid the scale id
|
||||
* @param string $sort sql order
|
||||
* @param string $sort sql order (firstname, rating or timemodified)
|
||||
* @return array Result and possible warnings
|
||||
* @throws moodle_exception
|
||||
* @since Moodle 2.9
|
||||
@ -74,27 +77,31 @@ class core_rating_external extends external_api {
|
||||
public static function get_item_ratings($contextlevel, $instanceid, $component, $ratingarea, $itemid, $scaleid, $sort) {
|
||||
global $USER;
|
||||
|
||||
$warnings = array();
|
||||
|
||||
$arrayparams = array(
|
||||
'contextlevel' => $contextlevel,
|
||||
'instanceid' => $instanceid,
|
||||
'component' => $component,
|
||||
'ratingarea' => $ratingarea,
|
||||
'itemid' => $itemid,
|
||||
'scaleid' => $scaleid,
|
||||
'sort' => $sort);
|
||||
'contextlevel' => $contextlevel,
|
||||
'instanceid' => $instanceid,
|
||||
'component' => $component,
|
||||
'ratingarea' => $ratingarea,
|
||||
'itemid' => $itemid,
|
||||
'scaleid' => $scaleid,
|
||||
'sort' => $sort
|
||||
);
|
||||
|
||||
// Validate and normalize parameters.
|
||||
$params = self::validate_parameters(self::get_item_ratings_parameters(), $arrayparams);
|
||||
|
||||
$context = self::get_context_from_params($params);
|
||||
self::validate_context($context);
|
||||
list($context, $course, $cm) = get_context_info_array($context->id);
|
||||
|
||||
// Minimal capability required.
|
||||
if (!has_capability('moodle/rating:view', $context)) {
|
||||
throw new moodle_exception('noviewrate', 'rating');
|
||||
}
|
||||
|
||||
list($context, $course, $cm) = get_context_info_array($context->id);
|
||||
|
||||
// Can we see all ratings?
|
||||
$canviewallratings = has_capability('moodle/rating:viewall', $context);
|
||||
|
||||
@ -129,20 +136,27 @@ class core_rating_external extends external_api {
|
||||
|
||||
foreach ($ratings as $rating) {
|
||||
if ($canviewallratings || $USER->id == $rating->userid) {
|
||||
$result = array();
|
||||
if ($rating->rating > $maxrating) {
|
||||
$rating->rating = $maxrating;
|
||||
}
|
||||
$usercontext = context_user::instance($rating->userid);
|
||||
$profileimageurl = moodle_url::make_pluginfile_url($usercontext->id, 'user', 'icon', null, '/', 'f1');
|
||||
$profileimageurl = moodle_url::make_webservice_pluginfile_url($usercontext->id, 'user', 'icon', null, '/', 'f1');
|
||||
|
||||
$result = array();
|
||||
$result['id'] = $rating->id;
|
||||
$result['userid'] = $rating->userid;
|
||||
$result['userpictureurl'] = $profileimageurl->out(false);
|
||||
$result['fullname'] = fullname($rating);
|
||||
$result['userfullname'] = fullname($rating);
|
||||
$result['rating'] = $scalemenu[$rating->rating];
|
||||
$result['timemodified'] = $rating->timemodified;
|
||||
$results[] = $result;
|
||||
}
|
||||
}
|
||||
|
||||
$warnings = array();
|
||||
return array('ratings' => $results, 'warning' => $warnings);
|
||||
return array(
|
||||
'ratings' => $results,
|
||||
'warnings' => $warnings
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -158,13 +172,14 @@ class core_rating_external extends external_api {
|
||||
'ratings' => new external_multiple_structure(
|
||||
new external_single_structure(
|
||||
array(
|
||||
'userid' => new external_value(PARAM_INT, 'User id'),
|
||||
'id' => new external_value(PARAM_INT, 'rating id'),
|
||||
'userid' => new external_value(PARAM_INT, 'user id'),
|
||||
'userpictureurl' => new external_value(PARAM_URL, 'URL user picture'),
|
||||
'fullname' => new external_value(PARAM_TEXT, 'fullname'),
|
||||
'rating' => new external_value(PARAM_TEXT, 'Rating on scale'),
|
||||
'timemodified' => new external_value(PARAM_INT, 'Time modified (timestamp)')
|
||||
), 'Ratings'
|
||||
), 'List of ratings'
|
||||
'userfullname' => new external_value(PARAM_NOTAGS, 'user fullname'),
|
||||
'rating' => new external_value(PARAM_NOTAGS, 'rating on scale'),
|
||||
'timemodified' => new external_value(PARAM_INT, 'time modified (timestamp)')
|
||||
), 'Rating'
|
||||
), 'list of ratings'
|
||||
),
|
||||
'warnings' => new external_warnings(),
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user