From d8039ecd8a10a40641cd0bfa469ac8937af8b638 Mon Sep 17 00:00:00 2001 From: Brendan Heywood Date: Tue, 21 Dec 2021 17:39:37 +1100 Subject: [PATCH] Added environment check #185 --- classes/check/envage.php | 94 +++++++++++++++++++++++++++++++++++ lang/en/local_envbar.php | 1 + lib.php | 11 ++++ service/updatelastrefresh.php | 2 + version.php | 6 +-- 5 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 classes/check/envage.php diff --git a/classes/check/envage.php b/classes/check/envage.php new file mode 100644 index 0000000..4437d5d --- /dev/null +++ b/classes/check/envage.php @@ -0,0 +1,94 @@ +. + +/** + * Check env refresh age + * + * @package local_envbar + * @author 2021 Brendan Heywood (brendan@catalyst-au.net) + * @copyright Catalyst IT Pty Ltd + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace local_envbar\check; + +defined('MOODLE_INTERNAL') || die(); + +use core\check\check; +use core\check\result; +use local_envbar\local\envbarlib; + +/** + * Check env refresh age + * + * @package local_envbar + * @author 2021 Brendan Heywood (brendan@catalyst-au.net) + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class envage extends check { + + /** + * Constructor + */ + public function __construct() { + global $CFG; + $this->id = 'envage'; + $this->name = get_string('checkenvage', 'local_envbar'); + } + + /** + * A link to a place to action this + * + * @return \action_link|null + */ + public function get_action_link(): ?\action_link { + return new \action_link( + new \moodle_url('/local/envbar/index.php'), + get_string('menuenvsettings', 'local_envbar')); + } + + /** + * Return result + * @return result + */ + public function get_result(): result { + global $CFG; + + if (envbarlib::getprodwwwroot() === $CFG->wwwroot) { + return new result(result::NA, get_string('prodwwwroottext', 'local_envbar'), ''); + } + + $lastrefresh = get_config('local_envbar', 'prodlastcheck'); + + // This takes the lastrefresh timestamp and displays it similar to how it appears in + // the headers such as 'Friday, 27 August 2021 - 144 days old.'. + if ($lastrefresh && $lastrefresh > 0) { + $format = get_string('strftimedatemonthabbr', 'langconfig'); + $summary = userdate($lastrefresh, get_string('strftimedaydate', 'langconfig')); + + $show = format_time(time() - $lastrefresh); + $num = strtok($show, ' '); + $unit = strtok(' '); + $show = "$num $unit"; + $summary .= ' - ' . get_string('refreshedago', 'local_envbar', $show); + } else { + $summary = get_string('refreshednever', 'local_envbar'); + } + + return new result(result::INFO, $summary, ''); + } +} + diff --git a/lang/en/local_envbar.php b/lang/en/local_envbar.php index d5fc707..efd388f 100644 --- a/lang/en/local_envbar.php +++ b/lang/en/local_envbar.php @@ -26,6 +26,7 @@ $string['addfields'] = 'Add another environment'; $string['bgcolour'] = 'Background colour'; $string['cachedef_records'] = 'The environment bar database records'; +$string['checkenvage'] = 'Check environment refresh age'; $string['colourerror'] = 'Invalid HTML color code specified.'; $string['colourplaceholder'] = 'HTML color code. e.g. #fff OR #000000'; $string['configureinprod'] = 'Edit config'; diff --git a/lib.php b/lib.php index b75c6a0..7763636 100644 --- a/lib.php +++ b/lib.php @@ -26,6 +26,17 @@ use local_envbar\local\envbarlib; +/** + * Add cron related service status checks + * + * @return array of check objects + */ +function local_envbar_security_checks(): array { + return [ + new \local_envbar\check\envage(), + ]; +} + /** * This is the hook enables the plugin to insert a chunk of html at the start of the html document. */ diff --git a/service/updatelastrefresh.php b/service/updatelastrefresh.php index 1d17056..75bfc09 100644 --- a/service/updatelastrefresh.php +++ b/service/updatelastrefresh.php @@ -28,6 +28,8 @@ use local_envbar\local\envbarlib; +define('NO_MOODLE_COOKIES', true); + require_once(dirname(__FILE__) . '/../../../config.php'); $wwwroot = required_param('wwwroot', PARAM_RAW); diff --git a/version.php b/version.php index 0cf2044..099a73c 100644 --- a/version.php +++ b/version.php @@ -29,9 +29,9 @@ die('Direct access to this script is forbidden.'); // It must be included from a Moodle page. } -$plugin->version = 2022011400; // The current plugin version (Date: YYYYMMDDXX). -$plugin->release = 2022011400; // Same as version +$plugin->version = 2022011401; // The current plugin version (Date: YYYYMMDDXX). +$plugin->release = 2022011401; // Same as version $plugin->requires = 2014051200; // Requires Moodle 2.7 or later. -$plugin->component = "local_envbar"; +$plugin->component = 'local_envbar'; $plugin->maturity = MATURITY_STABLE; $plugin->supported = [310, 311]; // A range of branch numbers of supported moodle versions.