$userrating = required_param('rating', PARAM_INT); $rateduserid = required_param('rateduserid', PARAM_INT); // The user being rated. Required to update their grade. $aggregationmethod = optional_param('aggregation', RATING_AGGREGATE_NONE, PARAM_INT); // Used to calculate the aggregate to return. $result = new stdClass(); // If session has expired and its an ajax request so we cant do a page redirect. if (!isloggedin()) { $result->error = get_string('sessionerroruser', 'error'); echo json_encode($result); die; } list($context, $course, $cm) = get_context_info_array($contextid); require_login($course, false, $cm); $contextid = null; // Now we have a context object, throw away the id from the user. $PAGE->set_context($context); $PAGE->set_url('/rating/rate_ajax.php', array('contextid' => $context->id)); if (!confirm_sesskey() || !has_capability('moodle/rating:rate', $context)) { echo $OUTPUT->header(); echo get_string('ratepermissiondenied', 'rating'); echo $OUTPUT->footer(); die; } $rm = new rating_manager(); $result = $rm->add_rating($cm, $context, $component, $ratingarea, $itemid, $scaleid, $userrating, $rateduserid, $aggregationmethod); // Return translated error. if (!empty($result->error)) { $result->error = get_string($result->error, 'rating'); } echo json_encode($result);
public function test_course_check_module_updates_since() { global $CFG, $DB, $USER; require_once $CFG->dirroot . '/mod/glossary/lib.php'; require_once $CFG->dirroot . '/rating/lib.php'; require_once $CFG->dirroot . '/comment/lib.php'; $this->resetAfterTest(true); $CFG->enablecompletion = true; $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1)); $glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course->id, 'completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionview' => 1, 'allowcomments' => 1, 'assessed' => RATING_AGGREGATE_AVERAGE, 'scale' => 100)); $glossarygenerator = $this->getDataGenerator()->get_plugin_generator('mod_glossary'); $context = context_module::instance($glossary->cmid); $modinfo = get_fast_modinfo($course); $cm = $modinfo->get_cm($glossary->cmid); $user = $this->getDataGenerator()->create_user(); $studentrole = $DB->get_record('role', array('shortname' => 'student')); $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id); $from = time(); $teacher = $this->getDataGenerator()->create_user(); $teacherrole = $DB->get_record('role', array('shortname' => 'teacher')); $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id); assign_capability('mod/glossary:viewanyrating', CAP_ALLOW, $studentrole->id, $context->id, true); // Check nothing changed right now. $updates = course_check_module_updates_since($cm, $from); $this->assertFalse($updates->configuration->updated); $this->assertFalse($updates->completion->updated); $this->assertFalse($updates->gradeitems->updated); $this->assertFalse($updates->comments->updated); $this->assertFalse($updates->ratings->updated); $this->assertFalse($updates->introfiles->updated); $this->assertFalse($updates->outcomes->updated); $this->waitForSecond(); // Do some changes. $this->setUser($user); $entry = $glossarygenerator->create_content($glossary); $this->setUser($teacher); // Name. set_coursemodule_name($glossary->cmid, 'New name'); // Add some ratings. $rm = new rating_manager(); $result = $rm->add_rating($cm, $context, 'mod_glossary', 'entry', $entry->id, 100, 50, $user->id, RATING_AGGREGATE_AVERAGE); // Change grades. $glossary->cmidnumber = $glossary->cmid; glossary_update_grades($glossary, $user->id); $this->setUser($user); // Completion status. glossary_view($glossary, $course, $cm, $context, 'letter'); // Add one comment. $args = new stdClass(); $args->context = $context; $args->course = $course; $args->cm = $cm; $args->area = 'glossary_entry'; $args->itemid = $entry->id; $args->client_id = 1; $args->component = 'mod_glossary'; $manager = new comment($args); $manager->add('blah blah blah'); // Check upgrade status. $updates = course_check_module_updates_since($cm, $from); $this->assertTrue($updates->configuration->updated); $this->assertTrue($updates->completion->updated); $this->assertTrue($updates->gradeitems->updated); $this->assertTrue($updates->comments->updated); $this->assertTrue($updates->ratings->updated); $this->assertFalse($updates->introfiles->updated); $this->assertFalse($updates->outcomes->updated); }
/** * Adds a rating to an item * * @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 int $rating the user rating * @param int $rateduserid the rated user id * @param int $aggregation the aggregation method * @return array result and possible warnings * @throws moodle_exception * @since Moodle 3.2 */ public static function add_rating($contextlevel, $instanceid, $component, $ratingarea, $itemid, $scaleid, $rating, $rateduserid, $aggregation = RATING_AGGREGATE_NONE) { $warnings = array(); $params = array('contextlevel' => $contextlevel, 'instanceid' => $instanceid, 'component' => $component, 'ratingarea' => $ratingarea, 'itemid' => $itemid, 'scaleid' => $scaleid, 'rating' => $rating, 'rateduserid' => $rateduserid, 'aggregation' => $aggregation); // Validate and normalize parameters. $params = self::validate_parameters(self::add_rating_parameters(), $params); $context = self::get_context_from_params($params); self::validate_context($context); $cm = get_coursemodule_from_id(false, $context->instanceid, 0, false, MUST_EXIST); require_capability('moodle/rating:rate', $context); $rm = new rating_manager(); $result = $rm->add_rating($cm, $context, $params['component'], $params['ratingarea'], $params['itemid'], $params['scaleid'], $params['rating'], $params['rateduserid'], $params['aggregation']); if (!empty($result->error)) { throw new moodle_exception($result->error, 'rating'); } $returndata = array('success' => $result->success, 'warnings' => $warnings); if (isset($result->aggregate)) { $returndata['aggregate'] = $result->aggregate; $returndata['count'] = $result->count; $returndata['itemid'] = $result->itemid; } return $returndata; }