Cancel a user competency review request.

@param  int $userid       The user ID.
@param  int $competencyid The competency ID.
@return bool
545677
public static function user_competency_cancel_review_request($userid, $competencyid) {
        static::require_enabled();
        $context = context_user::instance($userid);
        $uc = user_competency::get_record(array('userid' => $userid, 'competencyid' => $competencyid));
        if (!$uc || !$uc->can_read()) {
            throw new required_capability_exception($context, 'moodle/competency:usercompetencyview', 'nopermissions', '');
        } else if ($uc->get('status') != user_competency::STATUS_WAITING_FOR_REVIEW) {
            throw new coding_exception('The competency can not be cancel review request at this stage.');
        } else if (!$uc->can_request_review()) {
            throw new required_capability_exception($context, 'moodle/competency:usercompetencyrequestreview', 'nopermissions', '');
        }

        $uc->set('status', user_competency::STATUS_IDLE);
        $result = $uc->update();
        if ($result) {
            \core\event\competency_user_competency_review_request_cancelled::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']);
    }
144522
public static function get_user_competencies_by_userevidenceid($userevidenceid) {
        global $DB;

        $sql = "SELECT uc.*
                  FROM {" . user_competency::TABLE . "} uc
                  JOIN {" . self::TABLE . "} uec
                    ON uc.competencyid = uec.competencyid
                  JOIN {" . user_evidence::TABLE . "} ue
                    ON uec.userevidenceid = ue.id
                   AND uc.userid = ue.userid
                   AND ue.id = ?
              ORDER BY uc.id ASC";

        $usercompetencies = array();
        $records = $DB->get_recordset_sql($sql, array($userevidenceid));
        foreach ($records as $record) {
            $usercompetencies[] = new user_competency(0, $record);
        }
        $records->close();
        return $usercompetencies;
    }
886974
public static function user_competency_viewed($usercompetencyorid) {
        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', '');
        }

        \core\event\competency_user_competency_viewed::create_from_user_competency_viewed($uc)->trigger();
        return true;
    }
692380
public static function user_competency_viewed_in_course($competencyid, $userid, $courseid) {
        $params = self::validate_parameters(self::user_competency_viewed_in_course_parameters(), array(
            'competencyid' => $competencyid,
            'userid' => $userid,
            'courseid' => $courseid
        ));
        $ucc = api::get_user_competency_in_course($params['courseid'], $params['userid'], $params['competencyid']);
        $result = api::user_competency_viewed_in_course($ucc);

        return $result;
    }
1045470
public static function count_proficient_competencies_in_course_for_user($courseid, $userid) {
        static::require_enabled();
        // Check the user have access to the course.
        self::validate_course($courseid);

        // First we do a permissions check.
        $context = context_course::instance($courseid);

        $capabilities = array('moodle/competency:coursecompetencyview', 'moodle/competency:coursecompetencymanage');
        if (!has_any_capability($capabilities, $context)) {
             throw new required_capability_exception($context, 'moodle/competency:coursecompetencyview', 'nopermissions', '');
        }

        // OK - all set.
        return user_competency_course::count_proficient_competencies($courseid, $userid);
    }
60912
public static function request_review_of_user_evidence_linked_competencies($id) {
        $params = self::validate_parameters(self::request_review_of_user_evidence_linked_competencies_parameters(), array(
            'id' => $id
        ));

        $userevidence = api::read_user_evidence($id);
        self::validate_context($userevidence->get_context());

        return api::request_review_of_user_evidence_linked_competencies($id);
    }
1106118
protected static function export_user_data_competencies_related_to_me($userid, context_user $context) {
        global $DB;

        $path = [
            get_string('competencies', 'core_competency'),
            get_string('privacy:path:relatedtome', 'core_competency'),
            get_string('competencies', 'core_competency'),
        ];
        $competencies = [];
        $helper = new performance_helper();
        $cfields = competency::get_sql_fields('c', 'c_');
        $ucfields = user_competency::get_sql_fields('uc', 'uc_');
        $efields = evidence::get_sql_fields('e', 'e_');

        $initcompetency = function($record) use (&$competencies) {
            $competency = new competency(null, competency::extract_record($record, 'c_'));
            $competencies[$competency->get('id')] = array_merge(static::transform_competency_brief($competency), [
                'evidence' => []
            ]);
        };

        $initusercomp = function($competency, $record) use (&$competencies, $userid, $helper) {
            $competencyid = $competency->get('id');
            $uc = new user_competency(null, user_competency::extract_record($record, 'uc_'));
            $competencies[$competencyid]['uc_id'] = $uc->get('id');
            $competencies[$competencyid]['rating'] = static::transform_user_competency($userid, $uc, $competency, $helper);
        };

        // Look for evidence.
        $sql = "
            SELECT $efields, $ucfields, $cfields
              FROM {" . evidence::TABLE . "} e
              JOIN {" . user_competency::TABLE . "} uc
                ON uc.id = e.usercompetencyid
              JOIN {" . competency::TABLE . "} c
                ON c.id = uc.competencyid
             WHERE uc.userid = :targetuserid
               AND (e.usermodified = :userid1
                OR e.actionuserid = :userid2)
          ORDER BY c.id, e.id";
        $params = [
            'targetuserid' => $context->instanceid,
            'userid1' => $userid,
            'userid2' => $userid,
        ];
        $recordset = $DB->get_recordset_sql($sql, $params);
        foreach ($recordset as $record) {
            $competencyid = $record->c_id;
            $competency = new competency(null, competency::extract_record($record, 'c_'));

            if (!isset($competencies[$competencyid])) {
                $initcompetency($record);
            }

            if (!array_key_exists('rating', $competencies[$competencyid])) {
                $competencies[$competencyid]['rating'] = null;
                if ($record->uc_reviewerid == $userid || $record->uc_usermodified == $userid) {
                    $initusercomp($competency, $record);
                }
            }

            $evidence = new evidence(null, evidence::extract_record($record, 'e_'));
            $competencies[$competencyid]['evidence'][] = static::transform_evidence($userid, $evidence, $competency, $helper);
        }
        $recordset->close();

        // Look for user competency we modified and didn't catch.
        $insql = ' > 0';
        $inparams = [];
        if (!empty($competencies)) {
            list($insql, $inparams) = $DB->get_in_or_equal(array_keys($competencies), SQL_PARAMS_NAMED, 'param', false);
        }
        $sql = "
            SELECT $ucfields, $cfields
              FROM {" . user_competency::TABLE . "} uc
              JOIN {" . competency::TABLE . "} c
                ON c.id = uc.competencyid
         LEFT JOIN {comments} cmt
                ON cmt.contextid = :contextid
               AND cmt.commentarea = :ucarea
               AND cmt.component = :competency
               AND cmt.itemid = uc.id
             WHERE uc.userid = :targetuserid
               AND (uc.usermodified = :userid1
                OR uc.reviewerid = :userid2
                OR cmt.userid = :userid3)
               AND uc.competencyid $insql
          ORDER BY c.id, uc.id";
        $params = array_merge($inparams, [
            'targetuserid' => $context->instanceid,
            'userid1' => $userid,
            'userid2' => $userid,
            'userid3' => $userid,
            'contextid' => $context->id,
            'ucarea' => 'user_competency',
            'competency' => 'competency',
        ]);

        $recordset = $DB->get_recordset_sql($sql, $params);
        foreach ($recordset as $record) {
            $competency = new competency(null, competency::extract_record($record, 'c_'));
            if (!isset($competencies[$competency->get('id')])) {
                $initcompetency($record);
                $initusercomp($competency, $record);
            }
        }
        $recordset->close();

        // Export each competency on its own.
        foreach ($competencies as $competencyid => $competency) {
            $comppath = array_merge($path, ["{$competency['name']} ({$competencyid})"]);
            $ucid = isset($competency['uc_id']) ? $competency['uc_id'] : null;
            unset($competency['uc_id']);

            // Send to writer.
            writer::with_context($context)->export_data($comppath, (object) $competency);
            if ($ucid) {
                \core_comment\privacy\provider::export_comments($context, 'competency', 'user_competency', $ucid, $comppath, true);
            }
        }
    }
87485
public static function remove_competency_from_template($templateid, $competencyid) {
        $params = self::validate_parameters(self::remove_competency_from_template_parameters(), array(
            'templateid' => $templateid,
            'competencyid' => $competencyid,
        ));
        $template = api::read_template($params['templateid']);
        self::validate_context($template->get_context());

        return api::remove_competency_from_template($params['templateid'], $params['competencyid']);
    }
471216
public static function remove_competency_from_plan($planid, $competencyid) {
        $params = self::validate_parameters(self::remove_competency_from_plan_parameters(), array(
            'planid' => $planid,
            'competencyid' => $competencyid,
        ));
        $plan = api::read_plan($params['planid']);
        self::validate_context($plan->get_context());

        return api::remove_competency_from_plan($params['planid'], $params['competencyid']);
    }
686044
public function cancel($requestUuid)
    {
        if (empty($requestUuid)) {
            throw new PlivoValidationException(
                "Which call request to cancel? No requestUuid given");
        }
        $this->client->delete(
            "Account/".
            $this->pathParams['authId'].
            "/Request/".
            $requestUuid.
            '/',
            []
        );
    }
447778
public function canCancel($userId)
    {
        return $this->isOwner($userId) ||
            ($this->isIssuer($userId) && $this->isOnTimeToCancel()) ||
            ($this->isTarget($userId) && $this->isOnTimeToCancel());
    }
807642
public static function delete_competency($id) {
        global $DB;
        static::require_enabled();
        $competency = new competency($id);

        // First we do a permissions check.
        require_capability('moodle/competency:competencymanage', $competency->get_context());

        $events = array();
        $competencyids = array(intval($competency->get('id')));
        $contextid = $competency->get_context()->id;
        $competencyids = array_merge(competency::get_descendants_ids($competency), $competencyids);
        if (!competency::can_all_be_deleted($competencyids)) {
            return false;
        }
        $transaction = $DB->start_delegated_transaction();

        try {

            // Reset the rule of the parent.
            $parent = $competency->get_parent();
            if ($parent) {
                $parent->reset_rule();
                $parent->update();
            }

            // Delete the competency separately so the after_delete event can be triggered.
            $competency->delete();

            // Delete the competencies.
            competency::delete_multiple($competencyids);

            // Delete the competencies relation.
            related_competency::delete_multiple_relations($competencyids);

            // Delete competency evidences.
            user_evidence_competency::delete_by_competencyids($competencyids);

            // Register the competencies deleted events.
            $events = \core\event\competency_deleted::create_multiple_from_competencyids($competencyids, $contextid);

        } catch (\Exception $e) {
            $transaction->rollback($e);
        }

        $transaction->allow_commit();
        // Trigger events.
        foreach ($events as $event) {
            $event->trigger();
        }

        return true;
    }
611003
public static function delete_user_evidence_competency($userevidenceid, $competencyid) {
        $params = self::validate_parameters(self::delete_user_evidence_competency_parameters(), array(
            'userevidenceid' => $userevidenceid,
            'competencyid' => $competencyid,
        ));

        $userevidence = api::read_user_evidence($params['userevidenceid']);
        self::validate_context($userevidence->get_context());

        return api::delete_user_evidence_competency($userevidence, $params['competencyid']);
    }
783172
public static function remove_competency_from_course_module($cmorid, $competencyid) {
        static::require_enabled();
        $cm = $cmorid;
        if (!is_object($cmorid)) {
            $cm = get_coursemodule_from_id('', $cmorid, 0, true, MUST_EXIST);
        }
        // Check the user have access to the course module.
        self::validate_course_module($cm);

        // First we do a permissions check.
        $context = context_module::instance($cm->id);

        require_capability('moodle/competency:coursecompetencymanage', $context);

        $record = new stdClass();
        $record->cmid = $cm->id;
        $record->competencyid = $competencyid;

        $competency = new competency($competencyid);
        $exists = course_module_competency::get_record(array('cmid' => $cm->id, 'competencyid' => $competencyid));
        if ($exists) {
            return $exists->delete();
        }
        return false;
    }
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;
    }
1318414
public static function user_competency_viewed($usercompetencyid) {
        $params = self::validate_parameters(self::user_competency_viewed_parameters(), array(
            'usercompetencyid' => $usercompetencyid
        ));

        $uc = api::get_user_competency_by_id($params['usercompetencyid']);
        $result = api::user_competency_viewed($uc);

        return $result;
    }