Skip to content

Commit

Permalink
MDL-49347 rating: Several fixes and code clean up
Browse files Browse the repository at this point in the history
- Use correct return values types
- Fix the warnings declaration
- Fix the pluginfile URL
  • Loading branch information
jleyva committed Apr 7, 2015
1 parent db5b697 commit 46e41e6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 37 deletions.
9 changes: 5 additions & 4 deletions lib/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -1007,10 +1007,11 @@

// 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'
),
);

Expand Down
81 changes: 48 additions & 33 deletions rating/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -47,54 +49,59 @@ 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
*/
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);

Expand Down Expand Up @@ -129,20 +136,27 @@ public static function get_item_ratings($contextlevel, $instanceid, $component,

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
);
}

/**
Expand All @@ -158,13 +172,14 @@ public static function get_item_ratings_returns() {
'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(),
)
Expand Down

0 comments on commit 46e41e6

Please sign in to comment.