1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Competency rule base.
19 *
20 * @package    core_competency
21 * @copyright  2015 Frédéric Massart - FMCorz.net
22 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace core_competency;
26defined('MOODLE_INTERNAL') || die();
27
28use coding_exception;
29
30/**
31 * Competency rule base abstract class.
32 *
33 * Rules are attached to a competency and then tested against a user competency
34 * to determine whether or not it matches.
35 *
36 * @package    core_competency
37 * @copyright  2015 Frédéric Massart - FMCorz.net
38 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40abstract class competency_rule {
41
42    /** @var competency The competency. */
43    protected $competency;
44
45    /**
46     * Constructor.
47     *
48     * @param  competency $competency The competency.
49     */
50    public function __construct(competency $competency) {
51        $class = $competency->get('ruletype');
52        if (!$class || !($this instanceof $class)) {
53            throw new coding_exception('This competency does not use this rule.');
54        }
55
56        $this->competency = $competency;
57    }
58
59    /**
60     * Get the rule config.
61     *
62     * @return mixed
63     */
64    protected function get_config() {
65        return $this->competency->get('ruleconfig');
66    }
67
68    /**
69     * Whether or not the rule is matched.
70     *
71     * @param user_competency $usercompetency The user competency to test against.
72     * @return bool
73     */
74    abstract public function matches(user_competency $usercompetency);
75
76    /**
77     * Validate the rule config.
78     *
79     * @param string $value The value to validate.
80     * @return bool
81     */
82    abstract public function validate_config($value);
83
84    /**
85     * The name of the rule.
86     *
87     * @return lang_string
88     */
89    public static function get_name() {
90        throw new coding_exception('Method not implemented.');
91    }
92
93    /**
94     * Migrate rule config from one set of competencies to another.
95     *
96     * Exceptions should be thrown when the migration can not be performed.
97     *
98     * @param string $config Original config rule of a competency.
99     * @param array $mappings Array that matches the original competency IDs with the new competencies objects.
100     * @return string New configuration.
101     * @throws Exception
102     */
103    public static function migrate_config($config, $mappings) {
104        return $config;
105    }
106
107}
108