| 1 | efrain | 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 |  * Base class for checks
 | 
        
           |  |  | 19 |  *
 | 
        
           |  |  | 20 |  * @package    core
 | 
        
           |  |  | 21 |  * @category   check
 | 
        
           |  |  | 22 |  * @copyright  2020 Brendan Heywood <brendan@catalyst-au.net>
 | 
        
           |  |  | 23 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 24 |  */
 | 
        
           |  |  | 25 | namespace core\check;
 | 
        
           |  |  | 26 |   | 
        
           |  |  | 27 | use coding_exception;
 | 
        
           |  |  | 28 |   | 
        
           |  |  | 29 | /**
 | 
        
           |  |  | 30 |  * Base class for checks
 | 
        
           |  |  | 31 |  *
 | 
        
           |  |  | 32 |  * @copyright  2020 Brendan Heywood <brendan@catalyst-au.net>
 | 
        
           |  |  | 33 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 34 |  */
 | 
        
           |  |  | 35 | abstract class check {
 | 
        
           |  |  | 36 |   | 
        
           |  |  | 37 |     /**
 | 
        
           |  |  | 38 |      * @var string $component - The component / plugin this task belongs to.
 | 
        
           |  |  | 39 |      *
 | 
        
           |  |  | 40 |      * This can be autopopulated by the check manager.
 | 
        
           |  |  | 41 |      * Otherwise, it is dynamically determined by get_component().
 | 
        
           |  |  | 42 |      */
 | 
        
           |  |  | 43 |     protected $component = '';
 | 
        
           |  |  | 44 |   | 
        
           |  |  | 45 |     /**
 | 
        
           |  |  | 46 |      * Get the frankenstyle component name
 | 
        
           |  |  | 47 |      *
 | 
        
           |  |  | 48 |      * @return string
 | 
        
           |  |  | 49 |      */
 | 
        
           |  |  | 50 |     public function get_component(): string {
 | 
        
           |  |  | 51 |         // Return component if has been set by the manager.
 | 
        
           |  |  | 52 |         if (!empty($this->component)) {
 | 
        
           |  |  | 53 |             return $this->component;
 | 
        
           |  |  | 54 |         }
 | 
        
           |  |  | 55 |   | 
        
           |  |  | 56 |         // Else work it out based on the classname.
 | 
        
           |  |  | 57 |         // Because the first part of the classname is always the component.
 | 
        
           |  |  | 58 |         $parts = explode("\\", get_called_class());
 | 
        
           |  |  | 59 |   | 
        
           |  |  | 60 |         if (empty($parts)) {
 | 
        
           |  |  | 61 |             throw new coding_exception("Unable to determine component for check");
 | 
        
           |  |  | 62 |         }
 | 
        
           |  |  | 63 |   | 
        
           |  |  | 64 |         return $parts[0];
 | 
        
           |  |  | 65 |     }
 | 
        
           |  |  | 66 |   | 
        
           |  |  | 67 |     /**
 | 
        
           |  |  | 68 |      * Get the frankenstyle component name
 | 
        
           |  |  | 69 |      *
 | 
        
           |  |  | 70 |      * @param string $component name
 | 
        
           |  |  | 71 |      */
 | 
        
           |  |  | 72 |     public function set_component(string $component) {
 | 
        
           |  |  | 73 |         $this->component = $component;
 | 
        
           |  |  | 74 |     }
 | 
        
           |  |  | 75 |   | 
        
           |  |  | 76 |     /**
 | 
        
           |  |  | 77 |      * Get the check's id
 | 
        
           |  |  | 78 |      *
 | 
        
           |  |  | 79 |      * This defaults to the base name of the class which is ok in the most
 | 
        
           |  |  | 80 |      * cases but if you have a check which can have multiple instances then
 | 
        
           |  |  | 81 |      * you should override this to be unique.
 | 
        
           |  |  | 82 |      *
 | 
        
           |  |  | 83 |      * @return string must be unique within a component
 | 
        
           |  |  | 84 |      */
 | 
        
           |  |  | 85 |     public function get_id(): string {
 | 
        
           |  |  | 86 |         $class = get_class($this);
 | 
        
           |  |  | 87 |         $id = explode("\\", $class);
 | 
        
           |  |  | 88 |         return end($id);
 | 
        
           |  |  | 89 |     }
 | 
        
           |  |  | 90 |   | 
        
           |  |  | 91 |     /**
 | 
        
           |  |  | 92 |      * Get the check reference
 | 
        
           |  |  | 93 |      *
 | 
        
           |  |  | 94 |      * @return string must be globally unique
 | 
        
           |  |  | 95 |      */
 | 
        
           |  |  | 96 |     public function get_ref(): string {
 | 
        
           |  |  | 97 |         $ref = $this->get_component();
 | 
        
           |  |  | 98 |         if (!empty($ref)) {
 | 
        
           |  |  | 99 |             $ref .= '_';
 | 
        
           |  |  | 100 |         }
 | 
        
           |  |  | 101 |         $ref .= $this->get_id();
 | 
        
           |  |  | 102 |         return $ref;
 | 
        
           |  |  | 103 |     }
 | 
        
           |  |  | 104 |   | 
        
           |  |  | 105 |     /**
 | 
        
           |  |  | 106 |      * Get the short check name
 | 
        
           |  |  | 107 |      *
 | 
        
           |  |  | 108 |      * @return string
 | 
        
           |  |  | 109 |      */
 | 
        
           |  |  | 110 |     public function get_name(): string {
 | 
        
           |  |  | 111 |         $id = $this->get_id();
 | 
        
           |  |  | 112 |         return get_string("check{$id}", $this->get_component());
 | 
        
           |  |  | 113 |     }
 | 
        
           |  |  | 114 |   | 
        
           |  |  | 115 |     /**
 | 
        
           |  |  | 116 |      * A link to a place to action this
 | 
        
           |  |  | 117 |      *
 | 
        
           |  |  | 118 |      * @return \action_link|null
 | 
        
           |  |  | 119 |      */
 | 
        
           |  |  | 120 |     public function get_action_link(): ?\action_link {
 | 
        
           |  |  | 121 |         return null;
 | 
        
           |  |  | 122 |     }
 | 
        
           |  |  | 123 |   | 
        
           |  |  | 124 |     /**
 | 
        
           |  |  | 125 |      * Return the result
 | 
        
           |  |  | 126 |      *
 | 
        
           |  |  | 127 |      * @return result object
 | 
        
           |  |  | 128 |      */
 | 
        
           |  |  | 129 |     abstract public function get_result(): result;
 | 
        
           |  |  | 130 |   | 
        
           |  |  | 131 | }
 | 
        
           |  |  | 132 |   |