Count the templates using a competency.

@param int $competencyid The competency id
@param bool $onlyvisible If true, only count visible templates using this competency.
@return int
423364
public static function count_templates($competencyid, $onlyvisible) {
        global $DB;

        $sql = 'SELECT COUNT(tpl.id)
                  FROM {' . self::TABLE . '} tplcomp
                  JOIN {' . template::TABLE . '} tpl
                    ON tplcomp.templateid = tpl.id
                 WHERE tplcomp.competencyid = ? ';
        $params = array($competencyid);

        if ($onlyvisible) {
            $sql .= ' AND tpl.visible = ?';
            $params[] = 1;
        }

        $results = $DB->count_records_sql($sql, $params);

        return $results;
    }
701854
public static function add_competency_to_template($templateid, $competencyid) {
        global $PAGE;
        $params = self::validate_parameters(self::add_competency_to_template_parameters(), array(
            'templateid' => $templateid,
            'competencyid' => $competencyid,
        ));

        $template = api::read_template($params['templateid']);
        self::validate_context($template->get_context());

        return api::add_competency_to_template($params['templateid'], $params['competencyid']);
    }
28665
public static function competency_viewed($id) {
        $params = self::validate_parameters(self::competency_viewed_parameters(), array(
            'id' => $id
        ));
        return api::competency_viewed($params['id']);
    }
811677
public static function remove_competency_from_template($templateid, $competencyid) {
        static::require_enabled();
        // First we do a permissions check.
        $template = new template($templateid);
        if (!$template->can_manage()) {
            throw new required_capability_exception($template->get_context(), 'moodle/competency:templatemanage',
                'nopermissions', '');
        }

        $record = new stdClass();
        $record->templateid = $templateid;
        $record->competencyid = $competencyid;

        $competency = new competency($competencyid);

        $exists = template_competency::get_records(array('templateid' => $templateid, 'competencyid' => $competencyid));
        if ($exists) {
            $link = array_pop($exists);
            return $link->delete();
        }
        return false;
    }
129454
public static function user_competency_plan_viewed($competencyid, $userid, $planid) {
        $params = self::validate_parameters(self::user_competency_viewed_in_plan_parameters(), array(
            'competencyid' => $competencyid,
            'userid' => $userid,
            'planid' => $planid
        ));
        $pl = api::get_plan_competency($params['planid'], $params['competencyid']);
        $result = api::user_competency_plan_viewed($pl->usercompetencyplan);

        return $result;
    }
114148
public static function competency_framework_viewed($frameworkorid) {
        static::require_enabled();
        $framework = $frameworkorid;
        if (!is_object($framework)) {
            $framework = new competency_framework($framework);
        }
        if (!has_any_capability(array('moodle/competency:competencyview', 'moodle/competency:competencymanage'),
                $framework->get_context())) {
            throw new required_capability_exception($framework->get_context(), 'moodle/competency:competencyview',
                'nopermissions', '');
        }
        \core\event\competency_framework_viewed::create_from_framework($framework)->trigger();
        return true;
    }
367114
public static function user_competency_viewed_in_plan($competencyid, $userid, $planid) {
        $params = self::validate_parameters(self::user_competency_viewed_in_plan_parameters(), array(
            'competencyid' => $competencyid,
            'userid' => $userid,
            'planid' => $planid
        ));
        $pl = api::get_plan_competency($params['planid'], $params['competencyid']);
        $result = api::user_competency_viewed_in_plan($pl->usercompetency, $params['planid']);

        return $result;
    }
1100042
public static function get_course_competency($courseid, $competencyid) {
        global $DB;

        $sql = 'SELECT crscomp.*
                  FROM {' . self::TABLE . '} crscomp
                 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 course_competency(0, $result);
    }
31469
public function get_enrolment_instances($onlyenabled = false) {
        if ($this->_instances === null) {
            $this->_instances = enrol_get_instances($this->course->id, $onlyenabled);
        }
        return $this->_instances;
    }
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;
    }
211239
public static function user_competency_start_review($userid, $competencyid) {
        $params = self::validate_parameters(self::user_competency_start_review_parameters(), array(
            'userid' => $userid,
            'competencyid' => $competencyid
        ));

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

        return api::user_competency_start_review($userid, $competencyid);
    }
966882
public static function count_proficient_competencies($courseid, $userid) {
        global $DB;

        $sql = 'SELECT COUNT(comp.id)
                  FROM {' . self::TABLE . '} usercoursecomp
                  JOIN {' . course_competency::TABLE . '} cc
                    ON usercoursecomp.competencyid = cc.competencyid AND cc.courseid = usercoursecomp.courseid
                  JOIN {' . competency::TABLE . '} comp
                    ON usercoursecomp.competencyid = comp.id
                 WHERE usercoursecomp.courseid = ? AND usercoursecomp.userid = ? AND usercoursecomp.proficiency = ?';
        $params = array($courseid, $userid, true);

        $results = $DB->count_records_sql($sql, $params);

        return $results;
    }
790107
public static function reorder_template_competency($templateid, $competencyidfrom, $competencyidto) {
        $params = self::validate_parameters(self::reorder_template_competency_parameters(), array(
            'templateid' => $templateid,
            'competencyidfrom' => $competencyidfrom,
            'competencyidto' => $competencyidto,
        ));

        $template = api::read_template($params['templateid']);
        self::validate_context($template->get_context());

        return api::reorder_template_competency($params['templateid'], $params['competencyidfrom'], $params['competencyidto']);
    }
521489
protected function get_num_competencies_in_course ($courseid) {

        if (!isset($this->coursecompetencies[$courseid])) {
            $ccs = \core_competency\api::count_competencies_in_course($courseid);
            // Save the number of competencies per course to avoid another database access in calculate_sample().
            $this->coursecompetencies[$courseid] = $ccs;
        } else {
            $ccs = $this->coursecompetencies[$courseid];
        }
        return $ccs;
    }
213160
public static function create_multiple_from_competencyids($competencyids, $contextid) {
        $events = array();
        foreach ($competencyids as $id) {
            $events[$id] = static::create(array(
                'contextid' => $contextid,
                'objectid' => $id
            ));
        }
        return $events;
    }
1026635
public static function remove_competency_from_course($courseid, $competencyid) {
        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);

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

        $record = new stdClass();
        $record->courseid = $courseid;
        $record->competencyid = $competencyid;

        $coursecompetency = new course_competency();
        $exists = course_competency::get_record(array('courseid' => $courseid, 'competencyid' => $competencyid));
        if ($exists) {
            // Delete all course_module_competencies for this competency in this course.
            $cmcs = course_module_competency::get_records_by_competencyid_in_course($competencyid, $courseid);
            foreach ($cmcs as $cmc) {
                $cmc->delete();
            }
            return $exists->delete();
        }
        return false;
    }
1137829
public static function list_courses($competencyid) {
        global $DB;

        $results = $DB->get_records_sql('SELECT course.id, course.visible, course.shortname, course.idnumber,
                                                course.fullname, course.summary, course.summaryformat, course.startdate,
                                                course.enddate, course.category
                                           FROM {course} course
                                           JOIN {' . self::TABLE . '} coursecomp
                                             ON coursecomp.courseid = course.id
                                          WHERE coursecomp.competencyid = ? ', array($competencyid));

        return $results;
    }