Переключаем оценку пользователя у материала.

@param float $rating
@param int|null $userId Если пусто, то берется текущий пользователь.
@return \InetStudio\Rating\Contracts\Models\Traits\RateableContract
436344
public function rateToggle($rating, $userId = null)
    {
        return app(RatingServiceContract::class)->toggleRateOf($this, $rating, $userId);
    }
201110
public static function is_user_gradable_in_course($courseid, $userid) {
        global $CFG;

        $gradableuser = false;
        $coursecontext = \context_course::instance($courseid);
        if (is_enrolled($coursecontext, $userid, '', false)) {
            $roles = get_user_roles($coursecontext, $userid);
            $gradebookroles = explode(',', $CFG->gradebookroles);
            foreach ($roles as $role) {
                foreach ($gradebookroles as $gradebookrole) {
                    if ($role->roleid = $gradebookrole) {
                        $gradableuser = true;
                    }
                }
            }
        }

        return $gradableuser;
    }
542277
public static function user_competency_viewed_in_plan($usercompetencyorid, $planid) {
        static::require_enabled();
        $uc = $usercompetencyorid;
        if (!is_object($uc)) {
            $uc = new user_competency($uc);
        }

        if (!$uc || !$uc->can_read()) {
            throw new required_capability_exception($uc->get_context(), 'moodle/competency:usercompetencyview',
                'nopermissions', '');
        }
        $plan = new plan($planid);
        if ($plan->get('status') == plan::STATUS_COMPLETE) {
            throw new coding_exception('To log the user competency in completed plan use user_competency_plan_viewed method.');
        }

        \core\event\competency_user_competency_viewed_in_plan::create_from_user_competency_viewed_in_plan($uc, $planid)->trigger();
        return true;
    }
170968
public function getReviewAndRatingList($userId)
    {
        $reviewAndRatingList = $this->getMergedReviewAndRatingList($userId);
        $reviewAndRatingList = $this->sortReviewAndRatingList($reviewAndRatingList);

        return $reviewAndRatingList;
    }
888021
public static function get_course_grades($userid = 0) {
        global $USER;

        $warnings = array();

        // Validate the parameter.
        $params = self::validate_parameters(self::get_course_grades_parameters(),
            array(
                'userid' => $userid
            )
        );

        $userid = $params['userid'];
        if (empty($userid)) {
            $userid = $USER->id;
        }

        $systemcontext = context_system::instance();
        self::validate_context($systemcontext);

        if ($USER->id != $userid) {
            // We must check if the current user can view other users grades.
            $user = core_user::get_user($userid, '*', MUST_EXIST);
            core_user::require_active_user($user);
            require_capability('moodle/grade:viewall', $systemcontext);
        }

        // We need the site course, and course context.
        $course = get_course(SITEID);
        $context = context_course::instance($course->id);

        // Force a regrade if required.
        grade_regrade_final_grades_if_required($course);
        // Get the course final grades now.
        $gpr = new grade_plugin_return(array('type' => 'report', 'plugin' => 'overview', 'courseid' => $course->id,
                                        'userid' => $userid));
        $report = new grade_report_overview($userid, $gpr, $context);
        $coursesgrades = $report->setup_courses_data(true);

        $grades = array();
        foreach ($coursesgrades as $coursegrade) {
            $gradeinfo = array(
                'courseid' => $coursegrade['course']->id,
                'grade' => grade_format_gradevalue($coursegrade['finalgrade'], $coursegrade['courseitem'], true),
                'rawgrade' => $coursegrade['finalgrade'],
            );
            if (isset($coursegrade['rank'])) {
                $gradeinfo['rank'] = $coursegrade['rank'];
            }
            $grades[] = $gradeinfo;
        }

        $result = array();
        $result['grades'] = $grades;
        $result['warnings'] = $warnings;
        return $result;
    }
333810
public static function create_user_evidence_competency($userevidenceorid, $competencyid) {
        global $USER;
        static::require_enabled();

        $userevidence = $userevidenceorid;
        if (!is_object($userevidence)) {
            $userevidence = self::read_user_evidence($userevidence);
        }

        // Perform user evidence capability checks.
        if (!$userevidence->can_manage()) {
            $context = $userevidence->get_context();
            throw new required_capability_exception($context, 'moodle/competency:userevidencemanage', 'nopermissions', '');
        }

        // Perform competency capability checks.
        $competency = self::read_competency($competencyid);

        // Get (and create) the relation.
        $relation = user_evidence_competency::get_relation($userevidence->get('id'), $competency->get('id'));
        if (!$relation->get('id')) {
            $relation->create();

            $link = url::user_evidence($userevidence->get('id'));
            self::add_evidence(
                $userevidence->get('userid'),
                $competency,
                $userevidence->get_context(),
                evidence::ACTION_LOG,
                'evidence_evidenceofpriorlearninglinked',
                'core_competency',
                $userevidence->get('name'),
                false,
                $link->out(false),
                null,
                $USER->id
            );
        }

        return $relation;
    }
773214
public function deleted(RatingModelContract $rating)
    {
        event(new ModelWasUnRated($rating->rateable, $rating->user_id));
        app(RatingServiceContract::class)->updateRating($rating->rateable, -($rating->rating));
    }
389391
public static function request_review_of_user_evidence_linked_competencies($id) {
        $userevidence = new user_evidence($id);
        $context = $userevidence->get_context();
        $userid = $userevidence->get('userid');

        if (!$userevidence->can_manage()) {
            throw new required_capability_exception($context, 'moodle/competency:userevidencemanage', 'nopermissions', '');
        }

        $usercompetencies = user_evidence_competency::get_user_competencies_by_userevidenceid($id);
        foreach ($usercompetencies as $usercompetency) {
            if ($usercompetency->get('status') == user_competency::STATUS_IDLE) {
                static::user_competency_request_review($userid, $usercompetency->get('competencyid'));
            }
        }

        return true;
    }
888223
public function created(RatingModelContract $rating)
    {
        event(new ModelWasRated($rating->rateable, $rating->user_id));
        app(RatingServiceContract::class)->updateRating($rating->rateable, $rating->rating);
    }
506857
public function create($data, $userId = null)
    {
        if (isset($userId)) {
            return $this->client()->put($data, 'users/{userId}', ['userId' => $userId]);
        }

        return $this->client()->post($data, 'users');
    }
1297941
public static function user_competency_request_review($userid, $competencyid) {
        $params = self::validate_parameters(self::user_competency_request_review_parameters(), array(
            'userid' => $userid,
            'competencyid' => $competencyid,
        ));

        $context = context_user::instance($params['userid']);
        self::validate_context($context);

        return api::user_competency_request_review($userid, $competencyid);
    }
68107
public function update_rating($rating) {
        global $DB;

        $time = time();

        $data = new stdClass;
        $data->rating       = $rating;
        $data->timemodified = $time;

        $item = new stdclass();
        $item->id = $this->itemid;
        $items = array($item);

        $ratingoptions = new stdClass;
        $ratingoptions->context = $this->context;
        $ratingoptions->component = $this->component;
        $ratingoptions->ratingarea = $this->ratingarea;
        $ratingoptions->items = $items;
        $ratingoptions->aggregate = RATING_AGGREGATE_AVERAGE; // We dont actually care what aggregation method is applied.
        $ratingoptions->scaleid = $this->scaleid;
        $ratingoptions->userid = $this->userid;

        $rm = new rating_manager();
        $items = $rm->get_ratings($ratingoptions);
        $firstitem = $items[0]->rating;

        if (empty($firstitem->id)) {
            // Insert a new rating.
            $data->contextid    = $this->context->id;
            $data->component    = $this->component;
            $data->ratingarea   = $this->ratingarea;
            $data->rating       = $rating;
            $data->scaleid      = $this->scaleid;
            $data->userid       = $this->userid;
            $data->itemid       = $this->itemid;
            $data->timecreated  = $time;
            $data->timemodified = $time;
            $DB->insert_record('rating', $data);
        } else {
            // Update the rating.
            $data->id           = $firstitem->id;
            $DB->update_record('rating', $data);
        }
    }
579074
private function getRatingById($ratingId)
    {
        $rating = oxNew(Rating::class);
        $doesRatingExist = $rating->load($ratingId);

        if (!$doesRatingExist) {
            throw new EntryDoesNotExistDaoException();
        }

        return $rating;
    }
734757
public function createRating(Model $ratingable, $data, Model $author)
    {
        $rating = new static();
        $rating->fill(array_merge($data, [
            'author_id' => $author->id,
            'author_type' => get_class($author),
        ]));

        $ratingable->ratings()->save($rating);

        return $rating;
    }
104087
public function userRate(RateableContract $rateable, $userId): ?float
    {
        $userId = $this->getRaterUserId($userId);

        $rate = $rateable->ratings()->where([
            'user_id' => $userId,
        ])->first();

        return ($rate) ? $rate->rating : null;
    }
83140
public function deleteReview($userId, $reviewId)
    {
        $review = $this->getReviewById($reviewId);

        $this->validateUserPermissionsToManageReview($review, $userId);

        $review->delete();
    }
106662
public function addRating($videoId, $rating)
    {
        $this->validateRating($rating);
        $customMetaData = $this->getVideo($videoId)->getCustomMetadata();

        $average = $this->getRatingAverage($videoId);
        $count = $this->getRatingCount($videoId);
        $newCount = $count + 1;

        $customMetaData[$this->metadataFieldCount] = $newCount;
        $customMetaData[$this->metadataFieldAverage] = (($average * $count) + $rating) / $newCount;

        $this->storeCustomMetaData($customMetaData, $videoId);
    }