| 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 | namespace core\check;
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 | /**
 | 
        
           |  |  | 20 |  * A check object returns a result object
 | 
        
           |  |  | 21 |  *
 | 
        
           |  |  | 22 |  * Most checks can use this an instance of this directly but if you have a
 | 
        
           |  |  | 23 |  * 'details' which is computationally expensive then extend this and overide
 | 
        
           |  |  | 24 |  * the get_details() method so that it is only called when it will be needed.
 | 
        
           |  |  | 25 |  *
 | 
        
           | 1441 | ariadna | 26 |  * @package    core
 | 
        
           |  |  | 27 |  * @category   check
 | 
        
           | 1 | efrain | 28 |  * @copyright  2020 Brendan Heywood <brendan@catalyst-au.net>
 | 
        
           |  |  | 29 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 30 |  */
 | 
        
           |  |  | 31 | class result implements \renderable {
 | 
        
           |  |  | 32 |   | 
        
           |  |  | 33 |     /**
 | 
        
           |  |  | 34 |      * This is used to notify if a check does not apply.
 | 
        
           |  |  | 35 |      *
 | 
        
           |  |  | 36 |      * In most cases if a check doesn't apply a check object shouldn't be made.
 | 
        
           |  |  | 37 |      * This state exists for when you always want visibilty of the check itself.
 | 
        
           |  |  | 38 |      * Can be useful for a check which depends on another check and it helps
 | 
        
           |  |  | 39 |      * focus on the other check which matters more.
 | 
        
           |  |  | 40 |      */
 | 
        
           |  |  | 41 |     const NA = 'na';
 | 
        
           |  |  | 42 |   | 
        
           |  |  | 43 |     /**
 | 
        
           |  |  | 44 |      * Ideally all checks should be ok.
 | 
        
           |  |  | 45 |      */
 | 
        
           |  |  | 46 |     const OK = 'ok';
 | 
        
           |  |  | 47 |   | 
        
           |  |  | 48 |     /**
 | 
        
           |  |  | 49 |      * This is used to show info for a check.
 | 
        
           |  |  | 50 |      *
 | 
        
           |  |  | 51 |      * This is equivalent to OK but could be used for alerting to potential
 | 
        
           |  |  | 52 |      * future warnings such as a deprecation in a service.
 | 
        
           |  |  | 53 |      */
 | 
        
           |  |  | 54 |     const INFO = 'info';
 | 
        
           |  |  | 55 |   | 
        
           |  |  | 56 |     /**
 | 
        
           |  |  | 57 |      * This means we could not determine the state.
 | 
        
           |  |  | 58 |      *
 | 
        
           |  |  | 59 |      * An example might be an expensive check done via cron, and it has never run.
 | 
        
           |  |  | 60 |      * It would be prudent to consider an unknown check as a warning or error.
 | 
        
           |  |  | 61 |      */
 | 
        
           |  |  | 62 |     const UNKNOWN = 'unknown';
 | 
        
           |  |  | 63 |   | 
        
           |  |  | 64 |     /**
 | 
        
           |  |  | 65 |      * Warnings
 | 
        
           |  |  | 66 |      *
 | 
        
           |  |  | 67 |      * Something is not ideal and should be addressed, eg usability or the
 | 
        
           |  |  | 68 |      * speed of the site may be affected, but it may self heal (eg a load spike)
 | 
        
           |  |  | 69 |      */
 | 
        
           |  |  | 70 |     const WARNING = 'warning';
 | 
        
           |  |  | 71 |   | 
        
           |  |  | 72 |     /**
 | 
        
           |  |  | 73 |      * This is used to notify if a check failed.
 | 
        
           |  |  | 74 |      *
 | 
        
           |  |  | 75 |      * Something is wrong with a component and a feature is not working.
 | 
        
           |  |  | 76 |      */
 | 
        
           |  |  | 77 |     const ERROR = 'error';
 | 
        
           |  |  | 78 |   | 
        
           |  |  | 79 |     /**
 | 
        
           |  |  | 80 |      * This is used to notify if a check is a major critical issue.
 | 
        
           |  |  | 81 |      *
 | 
        
           |  |  | 82 |      * An error which is affecting everyone in a major way.
 | 
        
           |  |  | 83 |      */
 | 
        
           |  |  | 84 |     const CRITICAL = 'critical';
 | 
        
           |  |  | 85 |   | 
        
           |  |  | 86 |     /**
 | 
        
           |  |  | 87 |      * @var string $status - status
 | 
        
           |  |  | 88 |      */
 | 
        
           |  |  | 89 |     protected $status = self::UNKNOWN;
 | 
        
           |  |  | 90 |   | 
        
           |  |  | 91 |     /**
 | 
        
           |  |  | 92 |      * @var string summary - should be roughly 1 line of plain text and may change depending on the state.
 | 
        
           |  |  | 93 |      */
 | 
        
           |  |  | 94 |     protected $summary = '';
 | 
        
           |  |  | 95 |   | 
        
           |  |  | 96 |     /**
 | 
        
           |  |  | 97 |      * @var string details about check.
 | 
        
           |  |  | 98 |      *
 | 
        
           |  |  | 99 |      * This may be a large amount of preformatted html text, possibly describing all the
 | 
        
           |  |  | 100 |      * different states and actions to address them.
 | 
        
           |  |  | 101 |      */
 | 
        
           |  |  | 102 |     protected $details = '';
 | 
        
           |  |  | 103 |   | 
        
           |  |  | 104 |     /**
 | 
        
           |  |  | 105 |      * Constructor
 | 
        
           |  |  | 106 |      *
 | 
        
           |  |  | 107 |      * @param string $status code
 | 
        
           |  |  | 108 |      * @param string $summary a 1 liner summary
 | 
        
           |  |  | 109 |      * @param string $details as a html chunk
 | 
        
           |  |  | 110 |      */
 | 
        
           |  |  | 111 |     public function __construct($status, $summary, $details = '') {
 | 
        
           |  |  | 112 |         $this->status = $status;
 | 
        
           |  |  | 113 |         $this->summary = $summary;
 | 
        
           |  |  | 114 |         $this->details = $details;
 | 
        
           |  |  | 115 |     }
 | 
        
           |  |  | 116 |   | 
        
           |  |  | 117 |     /**
 | 
        
           |  |  | 118 |      * Get the check status
 | 
        
           |  |  | 119 |      *
 | 
        
           |  |  | 120 |      * @return string one of the consts eg result::OK
 | 
        
           |  |  | 121 |      */
 | 
        
           |  |  | 122 |     public function get_status(): string {
 | 
        
           |  |  | 123 |         return $this->status;
 | 
        
           |  |  | 124 |     }
 | 
        
           |  |  | 125 |   | 
        
           |  |  | 126 |     /**
 | 
        
           |  |  | 127 |      * Summary of the check
 | 
        
           |  |  | 128 |      * @return string formatted html
 | 
        
           |  |  | 129 |      */
 | 
        
           |  |  | 130 |     public function get_summary(): string {
 | 
        
           |  |  | 131 |         return $this->summary;
 | 
        
           |  |  | 132 |     }
 | 
        
           |  |  | 133 |   | 
        
           |  |  | 134 |     /**
 | 
        
           |  |  | 135 |      * Get the check detailed info
 | 
        
           |  |  | 136 |      * @return string formatted html
 | 
        
           |  |  | 137 |      */
 | 
        
           |  |  | 138 |     public function get_details(): string {
 | 
        
           |  |  | 139 |         return $this->details;
 | 
        
           |  |  | 140 |     }
 | 
        
           |  |  | 141 |   | 
        
           |  |  | 142 |     /**
 | 
        
           |  |  | 143 |      * Export this data so it can be used as the context for a mustache template.
 | 
        
           |  |  | 144 |      *
 | 
        
           |  |  | 145 |      * @param \renderer_base $output typically, the renderer that's calling this function
 | 
        
           |  |  | 146 |      * @return array data context for a mustache template
 | 
        
           |  |  | 147 |      */
 | 
        
           |  |  | 148 |     public function export_for_template(\renderer_base $output) {
 | 
        
           |  |  | 149 |         return array(
 | 
        
           |  |  | 150 |             'status'        => clean_text(get_string('status' . $this->status)),
 | 
        
           |  |  | 151 |             'isna'          => $this->status === self::NA,
 | 
        
           |  |  | 152 |             'isok'          => $this->status === self::OK,
 | 
        
           |  |  | 153 |             'isinfo'        => $this->status === self::INFO,
 | 
        
           |  |  | 154 |             'isunknown'     => $this->status === self::UNKNOWN,
 | 
        
           |  |  | 155 |             'iswarning'     => $this->status === self::WARNING,
 | 
        
           |  |  | 156 |             'iserror'       => $this->status === self::ERROR,
 | 
        
           |  |  | 157 |             'iscritical'    => $this->status === self::CRITICAL,
 | 
        
           |  |  | 158 |         );
 | 
        
           |  |  | 159 |     }
 | 
        
           |  |  | 160 |   | 
        
           |  |  | 161 |     /**
 | 
        
           |  |  | 162 |      * Which mustache template?
 | 
        
           |  |  | 163 |      *
 | 
        
           |  |  | 164 |      * @return string path to mustache template
 | 
        
           |  |  | 165 |      */
 | 
        
           |  |  | 166 |     public function get_template_name(): string {
 | 
        
           |  |  | 167 |         return 'core/check/result';
 | 
        
           |  |  | 168 |     }
 | 
        
           |  |  | 169 | }
 | 
        
           |  |  | 170 |   |