Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 402] [Versions 310 and 403]

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