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