Manually grade a user competency from the plans page.

@param mixed $planorid
@param int $competencyid
@param int $grade
@param string $note A note to attach to the evidence
@return array of \core_competency\user_competency
497705
public static function grade_competency_in_plan($planorid, $competencyid, $grade, $note = null) {
        global $USER;
        static::require_enabled();

        $plan = $planorid;
        if (!is_object($planorid)) {
            $plan = new plan($planorid);
        }

        $context = $plan->get_context();
        if (!user_competency::can_grade_user($plan->get('userid'))) {
            throw new required_capability_exception($context, 'moodle/competency:competencygrade', 'nopermissions', '');
        }

        // Throws exception if competency not in plan.
        $competency = $plan->get_competency($competencyid);
        $competencycontext = $competency->get_context();
        if (!has_any_capability(array('moodle/competency:competencyview', 'moodle/competency:competencymanage'),
                $competencycontext)) {
            throw new required_capability_exception($competencycontext, 'moodle/competency:competencyview', 'nopermissions', '');
        }

        $action = evidence::ACTION_OVERRIDE;
        $desckey = 'evidence_manualoverrideinplan';

        $result = self::add_evidence($plan->get('userid'),
                                  $competency,
                                  $context->id,
                                  $action,
                                  $desckey,
                                  'core_competency',
                                  $plan->get('name'),
                                  false,
                                  null,
                                  $grade,
                                  $USER->id,
                                  $note);
        if ($result) {
            $uc = static::get_user_competency($plan->get('userid'), $competency->get('id'));
            $event = \core\event\competency_user_competency_rated_in_plan::create_from_user_competency($uc, $plan->get('id'));
            $event->trigger();
        }
        return $result;
    }
1180802
public static function list_competencies($planid, $userid) {
        global $DB;

        $sql = 'SELECT c.*
                  FROM {' . competency::TABLE . '} c
                  JOIN {' . self::TABLE . '} ucp
                    ON ucp.competencyid = c.id
                   AND ucp.userid = :userid
                 WHERE ucp.planid = :planid
              ORDER BY ucp.sortorder ASC,
                       ucp.id ASC';
        $params = array('userid' => $userid, 'planid' => $planid);

        $results = $DB->get_recordset_sql($sql, $params);
        $instances = array();
        foreach ($results as $key => $result) {
             $instances[$key] = new competency(0, $result);
        }
        $results->close();

        return $instances;
    }
1284171
public static function list_courses_using_competency($competencyid) {
        static::require_enabled();

        // OK - all set.
        $courses = course_competency::list_courses($competencyid);
        $result = array();

        // Now check permissions on each course.
        foreach ($courses as $id => $course) {
            $context = context_course::instance($course->id);
            $capabilities = array('moodle/competency:coursecompetencyview', 'moodle/competency:coursecompetencymanage');
            if (!has_any_capability($capabilities, $context)) {
                unset($courses[$id]);
                continue;
            }
            if (!self::validate_course($course, false)) {
                unset($courses[$id]);
                continue;
            }
            array_push($result, $course);
        }

        return $result;
    }
879595
public static function grade_competency($userid, $competencyid, $grade, $note = null) {
        global $USER;
        static::require_enabled();

        $uc = static::get_user_competency($userid, $competencyid);
        $context = $uc->get_context();
        if (!user_competency::can_grade_user($uc->get('userid'))) {
            throw new required_capability_exception($context, 'moodle/competency:competencygrade', 'nopermissions', '');
        }

        // Throws exception if competency not in plan.
        $competency = $uc->get_competency();
        $competencycontext = $competency->get_context();
        if (!has_any_capability(array('moodle/competency:competencyview', 'moodle/competency:competencymanage'),
                $competencycontext)) {
            throw new required_capability_exception($competencycontext, 'moodle/competency:competencyview', 'nopermissions', '');
        }

        $action = evidence::ACTION_OVERRIDE;
        $desckey = 'evidence_manualoverride';

        $result = self::add_evidence($uc->get('userid'),
                                  $competency,
                                  $context->id,
                                  $action,
                                  $desckey,
                                  'core_competency',
                                  null,
                                  false,
                                  null,
                                  $grade,
                                  $USER->id,
                                  $note);
        if ($result) {
            $uc->read();
            $event = \core\event\competency_user_competency_rated::create_from_user_competency($uc);
            $event->trigger();
        }
        return $result;
    }
407873
public static function grade_competency_in_course($courseid, $userid, $competencyid, $grade, $note = null) {
        global $USER, $PAGE, $DB;

        $params = self::validate_parameters(self::grade_competency_in_course_parameters(), array(
            'courseid' => $courseid,
            'userid' => $userid,
            'competencyid' => $competencyid,
            'grade' => $grade,
            'note' => $note
        ));

        $course = $DB->get_record('course', array('id' => $params['courseid']));
        $context = context_course::instance($course->id);
        self::validate_context($context);
        $output = $PAGE->get_renderer('core');

        $evidence = api::grade_competency_in_course(
                $params['courseid'],
                $params['userid'],
                $params['competencyid'],
                $params['grade'],
                $params['note']
        );
        $competency = api::read_competency($params['competencyid']);
        $scale = $competency->get_scale();
        $exporter = new evidence_exporter($evidence, array(
            'actionuser' => $USER,
            'scale' => $scale,
            'usercompetency' => null,
            'usercompetencyplan' => null,
            'context' => $evidence->get_context(),
        ));
        return $exporter->export($output);
    }
1113500
public static function list_plans_with_competency($userid, $competencyorid) {
        global $USER;

        static::require_enabled();
        $competencyid = $competencyorid;
        $competency = null;
        if (is_object($competencyid)) {
            $competency = $competencyid;
            $competencyid = $competency->get('id');
        }

        $plans = plan::get_by_user_and_competency($userid, $competencyid);
        foreach ($plans as $index => $plan) {
            // Filter plans we cannot read.
            if (!$plan->can_read()) {
                unset($plans[$index]);
            }
        }
        return $plans;
    }
1358618
public static function user_competency_request_review($userid, $competencyid) {
        static::require_enabled();
        $uc = user_competency::get_record(array('userid' => $userid, 'competencyid' => $competencyid));
        if (!$uc) {
            $uc = user_competency::create_relation($userid, $competencyid);
            $uc->create();
        }

        if (!$uc->can_read()) {
            throw new required_capability_exception($uc->get_context(), 'moodle/competency:usercompetencyview',
                'nopermissions', '');
        } else if ($uc->get('status') != user_competency::STATUS_IDLE) {
            throw new coding_exception('The competency can not be sent for review at this stage.');
        } else if (!$uc->can_request_review()) {
            throw new required_capability_exception($uc->get_context(), 'moodle/competency:usercompetencyrequestreview',
                'nopermissions', '');
        }

        $uc->set('status', user_competency::STATUS_WAITING_FOR_REVIEW);
        $result = $uc->update();
        if ($result) {
            \core\event\competency_user_competency_review_requested::create_from_user_competency($uc)->trigger();
        }
        return $result;
    }
243961
public static function add_competency_to_course($courseid, $competencyid) {
        $params = self::validate_parameters(self::add_competency_to_course_parameters(), array(
            'courseid' => $courseid,
            'competencyid' => $competencyid,
        ));
        self::validate_context(context_course::instance($params['courseid']));
        return api::add_competency_to_course($params['courseid'], $params['competencyid']);
    }
348892
public static function get_competency($courseid, $competencyid) {
        global $DB;

        $sql = 'SELECT comp.*
                  FROM {' . competency::TABLE . '} comp
                  JOIN {' . self::TABLE . '} crscomp
                    ON crscomp.competencyid = comp.id
                 WHERE crscomp.courseid = ? AND crscomp.competencyid = ?';
        $params = array($courseid, $competencyid);

        $result = $DB->get_record_sql($sql, $params);
        if (!$result) {
            throw new coding_exception('The competency does not belong to this course: ' . $competencyid . ', ' . $courseid);
        }

        return new competency(0, $result);
    }
1181929
public function findOthersByCompetencyAndLevel(
        Competency $competency,
        Level $level,
        Ability $abilityToExclude
    ) {
        return $this->createQueryBuilder('a')
            ->select('a')
            ->join('a.competencyAbilities', 'ca')
            ->join('ca.competency', 'c')
            ->join('ca.level', 'l')
            ->where('c = :competency')
            ->andWhere('l = :level')
            ->andWhere('a <> :excluded')
            ->orderBy('l.value, a.id')
            ->setParameters([
                ':competency' => $competency,
                ':level' => $level,
                ':excluded' => $abilityToExclude,
            ])
            ->getQuery()
            ->getResult();
    }
300618
public static function can_grade_user_in_course($userid, $courseid) {
        $ratecap = 'moodle/competency:competencygrade';
        return has_capability($ratecap, context_course::instance($courseid))
            || static::can_grade_user($userid);
    }
402980
public static function get_user_competency($userid, $competencyid) {
        static::require_enabled();
        $existing = user_competency::get_multiple($userid, array($competencyid));
        $uc = array_pop($existing);

        if (!$uc) {
            $uc = user_competency::create_relation($userid, $competencyid);
            $uc->create();
        }

        if (!$uc->can_read()) {
            throw new required_capability_exception($uc->get_context(), 'moodle/competency:usercompetencyview',
                'nopermissions', '');
        }
        return $uc;
    }
1113078
public function get_competency($competencyid) {
        $competency = null;

        if ($this->get('status') == self::STATUS_COMPLETE) {
            // Get the competency from the archive of the plan.
            $competency = user_competency_plan::get_competency_by_planid($this->get('id'), $competencyid);
        } else if ($this->is_based_on_template()) {
            // Get the competency from the template.
            $competency = template_competency::get_competency($this->get('templateid'), $competencyid);
        } else {
            // Get the competency from the plan.
            $competency = plan_competency::get_competency($this->get('id'), $competencyid);
        }
        return $competency;
    }
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;
    }
1334371
public static function add_competency_to_plan($planid, $competencyid) {
        $params = self::validate_parameters(self::add_competency_to_plan_parameters(), array(
            'planid' => $planid,
            'competencyid' => $competencyid,
        ));

        $plan = api::read_plan($params['planid']);
        self::validate_context($plan->get_context());

        return api::add_competency_to_plan($params['planid'], $params['competencyid']);
    }
1334600
public static function grade_competency($userid, $competencyid, $grade, $note = null) {
        global $USER, $PAGE;
        $params = self::validate_parameters(self::grade_competency_parameters(), array(
            'userid' => $userid,
            'competencyid' => $competencyid,
            'grade' => $grade,
            'note' => $note
        ));

        $uc = api::get_user_competency($params['userid'], $params['competencyid']);
        self::validate_context($uc->get_context());

        $output = $PAGE->get_renderer('core');
        $evidence = api::grade_competency(
                $uc->get('userid'),
                $uc->get('competencyid'),
                $params['grade'],
                $params['note']
        );

        $scale = $uc->get_competency()->get_scale();
        $exporter = new evidence_exporter($evidence, [
            'actionuser' => $USER,
            'scale' => $scale,
            'usercompetency' => $uc,
            'usercompetencyplan' => null,
            'context' => $evidence->get_context()
        ]);
        return $exporter->export($output);
    }
33688
public static function list_templates_using_competency($competencyid) {
        global $PAGE;

        $params = self::validate_parameters(self::list_templates_using_competency_parameters(), array(
            'id' => $competencyid,
        ));

        $competency = api::read_competency($params['id']);
        self::validate_context($competency->get_context());
        $output = $PAGE->get_renderer('core');

        $templates = api::list_templates_using_competency($params['id']);
        $records = array();

        foreach ($templates as $template) {
            $exporter = new template_exporter($template);
            $record = $exporter->export($output);
            $records[] = $record;
        }

        return $records;
    }