/** * Return grade for given user or all users. * * @global object * @param object $dataplus * @param int $userid optional user id, 0 means all users * @return array array of grades, false if none */ function oublog_get_user_grades($oublog, $userid = 0) { global $CFG, $DB; require_once $CFG->dirroot . '/rating/lib.php'; require_once $CFG->dirroot . '/mod/oublog/locallib.php'; $options = new stdClass(); $options->component = 'mod_oublog'; $options->ratingarea = 'post'; $options->modulename = 'oublog'; $options->moduleid = $oublog->id; $options->userid = $userid; $options->aggregationmethod = $oublog->assessed; $options->scaleid = $oublog->scale; $options->cmid = $oublog->cmidnumber; // There now follows a lift of get_user_grades() from rating lib // but with the requirement for items modified. $rm = new rating_manager(); if (!isset($options->component)) { throw new coding_exception('The component option is now a required option when getting user grades from ratings.'); } if (!isset($options->ratingarea)) { throw new coding_exception('The ratingarea option is now a required option when getting user grades from ratings.'); } // Going direct to the db for the context id seemed wrong. $context = context_module::instance($options->cmid); $params = array(); $params['contextid'] = $context->id; $params['component'] = $options->component; $params['ratingarea'] = $options->ratingarea; $scaleid = $options->scaleid; $aggregationstring = $rm->get_aggregation_method($options->aggregationmethod); // If userid is not 0 we only want the grade for a single user. $singleuserwhere = ''; if ($options->userid != 0) { // Get the grades for the {posts} the user is responsible for. $cm = get_coursemodule_from_id('oublog', $oublog->cmidnumber); list($posts, $recordcount) = oublog_get_posts($oublog, $context, 0, $cm, 0, $options->userid); foreach ($posts as $post) { $postids[] = (int) $post->id; } $params['userid'] = $userid; $singleuserwhere = " AND i.userid = :userid"; } $sql = "SELECT u.id as id, u.id AS userid, {$aggregationstring}(r.rating) AS rawgrade\n FROM {oublog} o\n JOIN {oublog_instances} i ON i.oublogid = o.id\n JOIN {oublog_posts} p ON p.oubloginstancesid = i.id\n JOIN {rating} r ON r.itemid = p.id\n JOIN {user} u ON i.userid = u.id\n WHERE r.contextid = :contextid\n AND r.component = :component\n AND r.ratingarea = :ratingarea\n {$singleuserwhere}\n GROUP BY u.id"; $results = $DB->get_records_sql($sql, $params); if ($results) { $scale = null; $max = 0; if ($options->scaleid >= 0) { // Numeric. $max = $options->scaleid; } else { // Custom scales. $scale = $DB->get_record('scale', array('id' => -$options->scaleid)); if ($scale) { $scale = explode(',', $scale->scale); $max = count($scale); } else { debugging('rating_manager::get_user_grades() received a scale ID that doesnt exist'); } } // It could throw off the grading if count and sum returned a rawgrade higher than scale // so to prevent it we review the results and ensure that rawgrade does not exceed // the scale, if it does we set rawgrade = scale (i.e. full credit). foreach ($results as $rid => $result) { if ($options->scaleid >= 0) { // Numeric. if ($result->rawgrade > $options->scaleid) { $results[$rid]->rawgrade = $options->scaleid; } } else { // Scales. if (!empty($scale) && $result->rawgrade > $max) { $results[$rid]->rawgrade = $max; } } } } return $results; }