Example #1
0
/**
 * Returns a list of ratings for a particular post - sorted.
 *
 * TODO: Check if this function is actually used anywhere.
 * Up until the fix for MDL-27471 this function wasn't even returning.
 *
 * @param stdClass $context
 * @param int $postid
 * @param string $sort
 * @return array Array of ratings or false
 */
function forum_get_ratings($context, $postid, $sort = "u.firstname ASC") {
    $options = new stdClass;
    $options->context = $context;
    $options->component = 'mod_forum';
    $options->ratingarea = 'post';
    $options->itemid = $postid;
    $options->sort = "ORDER BY $sort";

    $rm = new rating_manager();
    return $rm->get_all_ratings_for_item($options);
}
Example #2
0
/**
 * Returns a list of ratings for a particular post - sorted.
 *
 * @global object
 * @global object
 * @param int $postid
 * @param string $sort
 * @return array Array of ratings or false
 */
function forum_get_ratings($context, $postid, $sort="u.firstname ASC") {
    global $PAGE;

    $options = new stdclass();
    $options->context = $PAGE->context;
    $options->itemid = $postid;
    $options->sort = "ORDER BY $sort";

    $rm = new rating_manager();
    $rm->get_all_ratings_for_item($options);
}
Example #3
0
$strrating  = get_string('rating', 'rating');
$strname    = get_string('name');
$strtime    = get_string('time');

$PAGE->set_title(get_string('allratingsforitem','rating'));
echo $OUTPUT->header();

$ratingoptions = new stdClass;
$ratingoptions->context = $context;
$ratingoptions->component = $component;
$ratingoptions->ratingarea = $ratingarea;
$ratingoptions->itemid = $itemid;
$ratingoptions->sort = $sqlsort;

$rm = new rating_manager();
$ratings = $rm->get_all_ratings_for_item($ratingoptions);
if (!$ratings) {
    $msg = get_string('noratings','rating');
    echo html_writer::tag('div', $msg, array('class'=>'mdl-align'));
} else {
    $sorturl  = new moodle_url('/index.php', array('contextid' => $contextid, 'itemid' => $itemid, 'scaleid' => $scaleid));
    if ($popup) {
        $sorturl->param('popup', $popup);
    }

    $table = new html_table;
    $table->cellpadding = 3;
    $table->cellspacing = 3;
    $table->attributes['class'] = 'generalbox ratingtable';
    $table->head = array(
        '',
Example #4
0
/**
 * Returns a list of ratings for a particular post - sorted.
 *
 * @param stdClass $context
 * @param int $postid
 * @param string $sort
 * @return array Array of ratings or false
 * @deprecated since Moodle 2.0 MDL-21657 - please do not use this function any more.
 */
function quora_get_ratings($context, $postid, $sort = "u.firstname ASC")
{
    debugging('quora_get_ratings() is deprecated.', DEBUG_DEVELOPER);
    $options = new stdClass();
    $options->context = $context;
    $options->component = 'mod_quora';
    $options->ratingarea = 'post';
    $options->itemid = $postid;
    $options->sort = "ORDER BY {$sort}";
    $rm = new rating_manager();
    return $rm->get_all_ratings_for_item($options);
}
Example #5
0
 /**
  * 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 $ratingarea rating area
  * @param int $itemid the item id
  * @param int $scaleid the scale id
  * @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, $PAGE;
     $warnings = array();
     $arrayparams = array('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);
     // Minimal capability required.
     $callbackparams = array('contextid' => $context->id, 'component' => $component, 'ratingarea' => $ratingarea, 'itemid' => $itemid, 'scaleid' => $scaleid);
     if (!has_capability('moodle/rating:view', $context) || !component_callback($component, 'rating_can_see_item_ratings', array($callbackparams), true)) {
         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);
     // Create the Sql sort order string.
     switch ($params['sort']) {
         case 'firstname':
             $sqlsort = "u.firstname ASC";
             break;
         case 'rating':
             $sqlsort = "r.rating ASC";
             break;
         default:
             $sqlsort = "r.timemodified ASC";
     }
     $ratingoptions = new stdClass();
     $ratingoptions->context = $context;
     $ratingoptions->component = $params['component'];
     $ratingoptions->ratingarea = $params['ratingarea'];
     $ratingoptions->itemid = $params['itemid'];
     $ratingoptions->sort = $sqlsort;
     $rm = new rating_manager();
     $ratings = $rm->get_all_ratings_for_item($ratingoptions);
     $scalemenu = make_grades_menu($params['scaleid']);
     // If the scale was changed after ratings were submitted some ratings may have a value above the current maximum.
     // We can't just do count($scalemenu) - 1 as custom scales start at index 1, not 0.
     $maxrating = max(array_keys($scalemenu));
     $results = array();
     foreach ($ratings as $rating) {
         if ($canviewallratings || $USER->id == $rating->userid) {
             if ($rating->rating > $maxrating) {
                 $rating->rating = $maxrating;
             }
             // The rating object has all the required fields for generating the picture url.
             $userpicture = new user_picture($rating);
             $userpicture->size = 1;
             // Size f1.
             $profileimageurl = $userpicture->get_url($PAGE)->out(false);
             $result = array();
             $result['id'] = $rating->id;
             $result['userid'] = $rating->userid;
             $result['userpictureurl'] = $profileimageurl;
             $result['userfullname'] = fullname($rating);
             $result['rating'] = $scalemenu[$rating->rating];
             $result['timemodified'] = $rating->timemodified;
             $results[] = $result;
         }
     }
     return array('ratings' => $results, 'warnings' => $warnings);
 }
Example #6
0
 /**
  * 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 $ratingarea rating area
  * @param int $itemid the item id
  * @param int $scaleid the scale id
  * @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);
     // 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);
     // 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);
     // Create the Sql sort order string.
     switch ($params['sort']) {
         case 'firstname':
             $sqlsort = "u.firstname ASC";
             break;
         case 'rating':
             $sqlsort = "r.rating ASC";
             break;
         default:
             $sqlsort = "r.timemodified ASC";
     }
     $ratingoptions = new stdClass();
     $ratingoptions->context = $context;
     $ratingoptions->component = $params['component'];
     $ratingoptions->ratingarea = $params['ratingarea'];
     $ratingoptions->itemid = $params['itemid'];
     $ratingoptions->sort = $sqlsort;
     $rm = new rating_manager();
     $ratings = $rm->get_all_ratings_for_item($ratingoptions);
     $scalemenu = make_grades_menu($params['scaleid']);
     // If the scale was changed after ratings were submitted some ratings may have a value above the current maximum.
     // We can't just do count($scalemenu) - 1 as custom scales start at index 1, not 0.
     $maxrating = max(array_keys($scalemenu));
     $results = array();
     foreach ($ratings as $rating) {
         if ($canviewallratings || $USER->id == $rating->userid) {
             if ($rating->rating > $maxrating) {
                 $rating->rating = $maxrating;
             }
             $profileimageurl = '';
             // We can have ratings from deleted users. In this case, those users don't have a valid context.
             $usercontext = context_user::instance($rating->userid, IGNORE_MISSING);
             if ($usercontext) {
                 $profileimageurl = moodle_url::make_webservice_pluginfile_url($usercontext->id, 'user', 'icon', null, '/', 'f1')->out(false);
             }
             $result = array();
             $result['id'] = $rating->id;
             $result['userid'] = $rating->userid;
             $result['userpictureurl'] = $profileimageurl;
             $result['userfullname'] = fullname($rating);
             $result['rating'] = $scalemenu[$rating->rating];
             $result['timemodified'] = $rating->timemodified;
             $results[] = $result;
         }
     }
     return array('ratings' => $results, 'warnings' => $warnings);
 }