Check if any badge has records for competencies.

@param array $competencyids Array of competencies ids.
@return boolean Return true if competencies were found in any badge.
189599
public static function has_records_for_competencies($competencyids) {
        global $DB;
        list($insql, $params) = $DB->get_in_or_equal($competencyids, SQL_PARAMS_NAMED);
        $sql = "SELECT DISTINCT bc.badgeid
                    FROM {badge_criteria} bc
                    JOIN {badge_criteria_param} bcp ON bc.id = bcp.critid
                    WHERE bc.criteriatype = :criteriatype AND value $insql";
        $params['criteriatype'] = BADGE_CRITERIA_TYPE_COMPETENCY;

        return self::record_exists_sql($sql, $params);
    }
1120124
public static function delete_multiple_relations($competencyids) {
        global $DB;
        if (empty($competencyids)) {
            return true;
        }

        list($insql, $params) = $DB->get_in_or_equal($competencyids);
        return $DB->delete_records_select(self::TABLE,
                                            "competencyid $insql OR relatedcompetencyid $insql",
                                            array_merge($params, $params)
                                            );
    }
364699
public static function competency_viewed($competencyorid) {
        static::require_enabled();
        $competency = $competencyorid;
        if (!is_object($competency)) {
            $competency = new competency($competency);
        }

        if (!has_any_capability(array('moodle/competency:competencyview', 'moodle/competency:competencymanage'),
                $competency->get_context())) {
            throw new required_capability_exception($competency->get_context(), 'moodle/competency:competencyview',
                'nopermissions', '');
        }

        \core\event\competency_viewed::create_from_competency($competency)->trigger();
        return true;
    }
316079
public static function list_course_competencies($courseorid) {
        static::require_enabled();
        $course = $courseorid;
        if (!is_object($courseorid)) {
            $course = get_course($courseorid);
        }

        // Check the user have access to the course.
        self::validate_course($course);
        $context = context_course::instance($course->id);

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

        $result = array();

        // TODO We could improve the performance of this into one single query.
        $coursecompetencies = course_competency::list_course_competencies($course->id);
        $competencies = course_competency::list_competencies($course->id);

        // Build the return values.
        foreach ($coursecompetencies as $key => $coursecompetency) {
            $result[] = array(
                'competency' => $competencies[$coursecompetency->get('competencyid')],
                'coursecompetency' => $coursecompetency
            );
        }

        return $result;
    }
1215244
public static function count_courses_using_competency($competencyid) {
        static::require_enabled();

        // OK - all set.
        $courses = course_competency::list_courses_min($competencyid);
        $count = 0;

        // Now check permissions on each course.
        foreach ($courses as $course) {
            if (!self::validate_course($course, false)) {
                continue;
            }

            $context = context_course::instance($course->id);
            $capabilities = array('moodle/competency:coursecompetencyview', 'moodle/competency:coursecompetencymanage');
            if (!has_any_capability($capabilities, $context)) {
                continue;
            }

            $count++;
        }

        return $count;
    }
386109
public static function list_course_competencies($courseid) {
        global $DB;

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

        $sql .= ' ORDER BY coursecomp.sortorder ASC';
        $results = $DB->get_recordset_sql($sql, $params);
        $instances = array();
        foreach ($results as $result) {
            array_push($instances, new course_competency(0, $result));
        }
        $results->close();

        return $instances;
    }
475233
public function associateCompetencyToResources(Competency $competency, array $nodes)
    {
        $associatedNodes = [];

        foreach ($nodes as $node) {
            if (!$competency->isLinkedToResource($node)) {
                $competency->linkResource($node);
                $associatedNodes[] = $node;
            }
        }
        $this->om->persist($competency);
        $this->om->flush();

        return $associatedNodes;
    }
406557
public function findByCompetencyAndLevel(Competency $competency, Level $level)
    {
        return $this->createQueryBuilder('a')
            ->select('a')
            ->join('a.competencyAbilities', 'ca')
            ->join('ca.competency', 'c')
            ->join('ca.level', 'l')
            ->where('c = :competency')
            ->andWhere('l = :level')
            ->orderBy('l.value, a.id')
            ->setParameters([
                ':competency' => $competency,
                ':level' => $level,
            ])
            ->getQuery()
            ->getResult();
    }
630984
public function has_related() {
        global $DB;
        $sql = "SELECT DISTINCT b.id
                    FROM {badge_related} br
                    JOIN {badge} b ON (br.relatedbadgeid = b.id OR br.badgeid = b.id)
                   WHERE (br.badgeid = :badgeid OR br.relatedbadgeid = :badgeid2) AND b.id != :badgeid3";
        return $DB->record_exists_sql($sql, ['badgeid' => $this->id, 'badgeid2' => $this->id, 'badgeid3' => $this->id]);
    }
185352
public static function count_competencies($filters) {
        $params = self::validate_parameters(self::count_competencies_parameters(), array(
            'filters' => $filters
        ));

        $safefilters = array();
        $validcolumns = array('id', 'shortname', 'description', 'sortorder', 'idnumber', 'parentid', 'competencyframeworkid');
        foreach ($params['filters'] as $filter) {
            if (!in_array($filter['column'], $validcolumns)) {
                throw new invalid_parameter_exception('Filter column was invalid');
            }
            $safefilters[$filter['column']] = $filter['value'];
        }

        $context = null;
        if (isset($safefilters['competencyframeworkid'])) {
            $framework = api::read_framework($safefilters['competencyframeworkid']);
            $context = $framework->get_context();
        } else {
            $context = context_system::instance();
        }

        self::validate_context($context);

        return api::count_competencies($safefilters);
    }
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;
    }
10022
public static function list_related_competencies($competencyid) {
        static::require_enabled();
        $competency = new competency($competencyid);

        if (!has_any_capability(array('moodle/competency:competencyview', 'moodle/competency:competencymanage'),
                $competency->get_context())) {
            throw new required_capability_exception($competency->get_context(), 'moodle/competency:competencyview',
                'nopermissions', '');
        }

        return $competency->get_related_competencies();
    }
278230
public static function list_competencies_in_template($templateid) {
        global $PAGE;

        $params = self::validate_parameters(self::list_competencies_in_template_parameters(), array(
            'id' => $templateid,
        ));

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

        $competencies = api::list_competencies_in_template($params['id']);
        $results = array();

        $helper = new performance_helper();
        foreach ($competencies as $competency) {
            $context = $helper->get_context_from_competency($competency);
            $exporter = new competency_exporter($competency, array('context' => $context));
            $record = $exporter->export($output);
            array_push($results, $record);
        }
        return $results;
    }
239527
public static function add_related_competency($competencyid, $relatedcompetencyid) {
        $params = self::validate_parameters(self::add_related_competency_parameters(), array(
            'competencyid' => $competencyid,
            'relatedcompetencyid' => $relatedcompetencyid
        ));
        $competency = api::read_competency($params['competencyid']);
        self::validate_context($competency->get_context());

        return api::add_related_competency($params['competencyid'], $params['relatedcompetencyid']);
    }
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;
    }
261733
public function add_related_badges($relatedids) {
        global $DB;
        $relatedbadges = array();
        foreach ($relatedids as $relatedid) {
            $relatedbadge = new stdClass();
            $relatedbadge->badgeid = $this->id;
            $relatedbadge->relatedbadgeid = $relatedid;
            $relatedbadges[] = $relatedbadge;
        }
        $DB->insert_records('badge_related', $relatedbadges);
    }
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;
    }