From bf25fb166c3024cdd1532f36a4f1702466b5c834 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Monlla=C3=B3?= Date: Mon, 13 May 2019 14:42:10 +0200 Subject: [PATCH] MDL-65585 analytics: Global on/off switch --- admin/settings/analytics.php | 2 +- admin/settings/subsystems.php | 3 +++ .../analytics/classes/output/renderer.php | 22 +++++++++++++++++++ .../analytics/classes/task/predict_models.php | 5 +++++ .../analytics/classes/task/train_models.php | 5 +++++ admin/tool/analytics/cli/enable_model.php | 5 +++++ admin/tool/analytics/cli/evaluate_model.php | 5 +++++ admin/tool/analytics/createmodel.php | 7 ++++++ admin/tool/analytics/importmodel.php | 9 +++++++- admin/tool/analytics/model.php | 7 ++++++ admin/tool/analytics/restoredefault.php | 7 ++++++ admin/tool/analytics/settings.php | 6 +++-- analytics/classes/manager.php | 16 ++++++++++++++ lang/en/admin.php | 2 ++ lang/en/analytics.php | 1 + lib/classes/task/analytics_cleanup_task.php | 5 +++++ report/insights/action.php | 7 ++++++ report/insights/classes/output/renderer.php | 21 ++++++++++++++++++ report/insights/done.php | 7 ++++++ report/insights/insights.php | 6 +++++ report/insights/lib.php | 22 ++++++++++--------- report/insights/prediction.php | 7 ++++++ report/insights/settings.php | 12 +++++----- version.php | 2 +- 24 files changed, 171 insertions(+), 20 deletions(-) diff --git a/admin/settings/analytics.php b/admin/settings/analytics.php index 52707453ea6..a7222444e5b 100644 --- a/admin/settings/analytics.php +++ b/admin/settings/analytics.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -if ($hassiteconfig) { +if ($hassiteconfig && \core_analytics\manager::is_analytics_enabled()) { $settings = new admin_settingpage('analyticssite', new lang_string('analyticssiteinfo', 'analytics')); $ADMIN->add('analytics', $settings); diff --git a/admin/settings/subsystems.php b/admin/settings/subsystems.php index de5f75be993..597e36b08cb 100644 --- a/admin/settings/subsystems.php +++ b/admin/settings/subsystems.php @@ -48,4 +48,7 @@ if ($hassiteconfig) { // speedup for non-admins, add all caps used on this page $optionalsubsystems->add(new admin_setting_configcheckbox('allowstealth', new lang_string('allowstealthmodules'), new lang_string('allowstealthmodules_help'), 0, 1, 0)); + + $optionalsubsystems->add(new admin_setting_configcheckbox('enableanalytics', new lang_string('enableanalytics', 'admin'), + new lang_string('configenableanalytics', 'admin'), 1, 1, 0)); } diff --git a/admin/tool/analytics/classes/output/renderer.php b/admin/tool/analytics/classes/output/renderer.php index b4f7a5108e5..edd165dfa35 100644 --- a/admin/tool/analytics/classes/output/renderer.php +++ b/admin/tool/analytics/classes/output/renderer.php @@ -229,4 +229,26 @@ class renderer extends plugin_renderer_base { $data = $invalidanalysables->export_for_template($this); return parent::render_from_template('tool_analytics/invalid_analysables', $data); } + + /** + * Renders an analytics disabled notification. + * + * @return string HTML + */ + public function render_analytics_disabled() { + global $OUTPUT, $PAGE, $FULLME; + + $PAGE->set_url($FULLME); + $PAGE->set_title(get_string('pluginname', 'tool_analytics')); + $PAGE->set_heading(get_string('pluginname', 'tool_analytics')); + + $output = $OUTPUT->header(); + $output .= $OUTPUT->notification(get_string('analyticsdisabled', 'analytics'), \core\output\notification::NOTIFY_INFO); + $output .= \html_writer::tag('a', get_string('continue'), ['class' => 'btn btn-primary', + 'href' => (new \moodle_url('/'))->out()]); + $output .= $OUTPUT->footer(); + + return $output; + } + } diff --git a/admin/tool/analytics/classes/task/predict_models.php b/admin/tool/analytics/classes/task/predict_models.php index 4551e79a8a9..20bf8d3c861 100644 --- a/admin/tool/analytics/classes/task/predict_models.php +++ b/admin/tool/analytics/classes/task/predict_models.php @@ -52,6 +52,11 @@ class predict_models extends \core\task\scheduled_task { public function execute() { global $OUTPUT, $PAGE; + if (!\core_analytics\manager::is_analytics_enabled()) { + mtrace(get_string('analyticsdisabled', 'analytics')); + return; + } + $models = \core_analytics\manager::get_all_models(true, true); if (!$models) { mtrace(get_string('errornoenabledandtrainedmodels', 'tool_analytics')); diff --git a/admin/tool/analytics/classes/task/train_models.php b/admin/tool/analytics/classes/task/train_models.php index c9387d3906b..f2ce98faed7 100644 --- a/admin/tool/analytics/classes/task/train_models.php +++ b/admin/tool/analytics/classes/task/train_models.php @@ -52,6 +52,11 @@ class train_models extends \core\task\scheduled_task { public function execute() { global $OUTPUT, $PAGE; + if (!\core_analytics\manager::is_analytics_enabled()) { + mtrace(get_string('analyticsdisabled', 'analytics')); + return; + } + $models = \core_analytics\manager::get_all_models(true); if (!$models) { mtrace(get_string('errornoenabledmodels', 'tool_analytics')); diff --git a/admin/tool/analytics/cli/enable_model.php b/admin/tool/analytics/cli/enable_model.php index ed8e3fc21ab..5da4e1f8a98 100644 --- a/admin/tool/analytics/cli/enable_model.php +++ b/admin/tool/analytics/cli/enable_model.php @@ -57,6 +57,11 @@ if ($options['help']) { exit(0); } +if (!\core_analytics\manager::is_analytics_enabled()) { + echo get_string('analyticsdisabled', 'analytics') . PHP_EOL; + exit(0); +} + if ($options['list'] || $options['modelid'] === false) { \tool_analytics\clihelper::list_models(); exit(0); diff --git a/admin/tool/analytics/cli/evaluate_model.php b/admin/tool/analytics/cli/evaluate_model.php index 601599e8fd8..3094ffaaec2 100644 --- a/admin/tool/analytics/cli/evaluate_model.php +++ b/admin/tool/analytics/cli/evaluate_model.php @@ -67,6 +67,11 @@ if ($options['help']) { exit(0); } +if (!\core_analytics\manager::is_analytics_enabled()) { + echo get_string('analyticsdisabled', 'analytics') . PHP_EOL; + exit(0); +} + if ($options['list']) { \tool_analytics\clihelper::list_models(); exit(0); diff --git a/admin/tool/analytics/createmodel.php b/admin/tool/analytics/createmodel.php index e5b9b926bcf..ad4fc836e32 100644 --- a/admin/tool/analytics/createmodel.php +++ b/admin/tool/analytics/createmodel.php @@ -27,6 +27,13 @@ require_once(__DIR__ . '/../../../config.php'); require_login(); \core_analytics\manager::check_can_manage_models(); +if (!\core_analytics\manager::is_analytics_enabled()) { + $PAGE->set_context(\context_system::instance()); + $renderer = $PAGE->get_renderer('tool_analytics'); + echo $renderer->render_analytics_disabled(); + exit(0); +} + $returnurl = new \moodle_url('/admin/tool/analytics/index.php'); $url = new \moodle_url('/admin/tool/analytics/createmodel.php'); $title = get_string('createmodel', 'tool_analytics'); diff --git a/admin/tool/analytics/importmodel.php b/admin/tool/analytics/importmodel.php index eedeaa0531a..0d4cd591663 100644 --- a/admin/tool/analytics/importmodel.php +++ b/admin/tool/analytics/importmodel.php @@ -27,6 +27,13 @@ require_once(__DIR__ . '/../../../config.php'); require_login(); \core_analytics\manager::check_can_manage_models(); +if (!\core_analytics\manager::is_analytics_enabled()) { + $PAGE->set_context(\context_system::instance()); + $renderer = $PAGE->get_renderer('tool_analytics'); + echo $renderer->render_analytics_disabled(); + exit(0); +} + $returnurl = new \moodle_url('/admin/tool/analytics/index.php'); $url = new \moodle_url('/admin/tool/analytics/importmodel.php'); $title = get_string('importmodel', 'tool_analytics'); @@ -56,4 +63,4 @@ if ($form->is_cancelled()) { echo $OUTPUT->header(); $form->display(); -echo $OUTPUT->footer(); \ No newline at end of file +echo $OUTPUT->footer(); diff --git a/admin/tool/analytics/model.php b/admin/tool/analytics/model.php index 7397de1783d..4886ff5ffb0 100644 --- a/admin/tool/analytics/model.php +++ b/admin/tool/analytics/model.php @@ -33,6 +33,13 @@ require_login(); $model = new \core_analytics\model($id); \core_analytics\manager::check_can_manage_models(); +if (!\core_analytics\manager::is_analytics_enabled()) { + $PAGE->set_context(\context_system::instance()); + $renderer = $PAGE->get_renderer('tool_analytics'); + echo $renderer->render_analytics_disabled(); + exit(0); +} + $returnurl = new \moodle_url('/admin/tool/analytics/index.php'); $params = array('id' => $id, 'action' => $action); $url = new \moodle_url('/admin/tool/analytics/model.php', $params); diff --git a/admin/tool/analytics/restoredefault.php b/admin/tool/analytics/restoredefault.php index 28b1b02071c..ce0f8ae4e50 100644 --- a/admin/tool/analytics/restoredefault.php +++ b/admin/tool/analytics/restoredefault.php @@ -27,6 +27,13 @@ require_once(__DIR__ . '/../../../config.php'); require_login(); \core_analytics\manager::check_can_manage_models(); +if (!\core_analytics\manager::is_analytics_enabled()) { + $PAGE->set_context(\context_system::instance()); + $renderer = $PAGE->get_renderer('tool_analytics'); + echo $renderer->render_analytics_disabled(); + exit(0); +} + $confirmed = optional_param('confirmed', false, PARAM_BOOL); $restoreids = optional_param_array('restoreid', [], PARAM_ALPHANUM); diff --git a/admin/tool/analytics/settings.php b/admin/tool/analytics/settings.php index aad459a5023..1f7adbe16b0 100644 --- a/admin/tool/analytics/settings.php +++ b/admin/tool/analytics/settings.php @@ -24,5 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$ADMIN->add('analytics', new admin_externalpage('analyticmodels', get_string('analyticmodels', 'tool_analytics'), - "$CFG->wwwroot/$CFG->admin/tool/analytics/index.php", 'moodle/analytics:managemodels')); +if (\core_analytics\manager::is_analytics_enabled()) { + $ADMIN->add('analytics', new admin_externalpage('analyticmodels', get_string('analyticmodels', 'tool_analytics'), + "$CFG->wwwroot/$CFG->admin/tool/analytics/index.php", 'moodle/analytics:managemodels')); +} diff --git a/analytics/classes/manager.php b/analytics/classes/manager.php index 00a3d580f38..60f1f4597fd 100644 --- a/analytics/classes/manager.php +++ b/analytics/classes/manager.php @@ -99,6 +99,22 @@ class manager { } } + /** + * Is analytics enabled globally? + * + * return bool + */ + public static function is_analytics_enabled(): bool { + global $CFG; + + if (isset($CFG->enableanalytics)) { + return $CFG->enableanalytics; + } + + // Enabled by default. + return true; + } + /** * Returns all system models that match the provided filters. * diff --git a/lang/en/admin.php b/lang/en/admin.php index c31faaddea2..ecfb46fb2c2 100644 --- a/lang/en/admin.php +++ b/lang/en/admin.php @@ -209,6 +209,7 @@ $string['configdefaultuserroleid'] = 'All logged in users will be given the capa $string['configdeleteincompleteusers'] = 'After this period, any account without the first name, last name or email field filled in is deleted.'; $string['configdeleteunconfirmed'] = 'For certain authentication methods, such as email-based self-registration, users must confirm their account within a certain time. After this period, any old unconfirmed accounts are deleted.'; $string['configdenyemailaddresses'] = 'To deny email addresses from particular domains list them here in the same way. All other domains will be accepted. To deny subdomains add the domain with a preceding \'.\'. eg hotmail.com yahoo.co.uk .live.com'; +$string['configenableanalytics'] = 'Analytics models, such as \'Students at risk of dropping out\' or \'Upcoming activities due\', can generate predictions, send insight notifications and offer further actions such as messaging users.'; $string['configenableblogs'] = 'This switch provides all site users with their own blog.'; $string['configenabledevicedetection'] = 'Enables detection of mobiles, smartphones, tablets or default devices (desktop PCs, laptops, etc) for the application of themes and other features.'; $string['configdisableuserimages'] = 'Disable the ability for users to change user profile images.'; @@ -514,6 +515,7 @@ $string['emoticons_desc'] = 'This form defines the emoticons (or smileys) used a $string['emoticonsreset'] = 'Reset emoticons setting to default values'; $string['emptysettingvalue'] = 'Empty'; $string['enableactivitychooser'] = 'Enable activity chooser'; +$string['enableanalytics'] = 'Analytics'; $string['enableblogs'] = 'Enable blogs'; $string['enablecalendarexport'] = 'Enable calendar export'; $string['enablecomments'] = 'Enable comments'; diff --git a/lang/en/analytics.php b/lang/en/analytics.php index af65a287c6c..1c72cb1b07a 100644 --- a/lang/en/analytics.php +++ b/lang/en/analytics.php @@ -26,6 +26,7 @@ $string['analysablenotused'] = 'Analysable {$a->analysableid} not used: {$a->err $string['analysablenotvalidfortarget'] = 'Analysable {$a->analysableid} is not valid for this target: {$a->result}'; $string['analysisinprogress'] = 'Still being analysed by a previous execution'; $string['analytics'] = 'Analytics'; +$string['analyticsdisabled'] = 'Analytics is disabled. You can enable it in "Site administration > Advanced features".'; $string['analyticslogstore'] = 'Log store used for analytics'; $string['analyticslogstore_help'] = 'The log store that will be used by the analytics API to read users\' activity.'; $string['analyticssettings'] = 'Analytics settings'; diff --git a/lib/classes/task/analytics_cleanup_task.php b/lib/classes/task/analytics_cleanup_task.php index 0824d93e299..90e6e13a4c1 100644 --- a/lib/classes/task/analytics_cleanup_task.php +++ b/lib/classes/task/analytics_cleanup_task.php @@ -50,6 +50,11 @@ class analytics_cleanup_task extends \core\task\scheduled_task { * @return void */ public function execute() { + + if (!\core_analytics\manager::is_analytics_enabled()) { + mtrace(get_string('analyticsdisabled', 'analytics')); + return; + } $models = \core_analytics\manager::cleanup(); } } diff --git a/report/insights/action.php b/report/insights/action.php index 6f2d333e0a8..b2000d2f44f 100644 --- a/report/insights/action.php +++ b/report/insights/action.php @@ -28,6 +28,13 @@ $predictionid = required_param('predictionid', PARAM_INT); $actionname = required_param('action', PARAM_ALPHANUMEXT); $forwardurl = required_param('forwardurl', PARAM_LOCALURL); +if (!\core_analytics\manager::is_analytics_enabled()) { + $PAGE->set_context(\context_system::instance()); + $renderer = $PAGE->get_renderer('report_insights'); + echo $renderer->render_analytics_disabled(); + exit(0); +} + list($model, $prediction, $context) = \core_analytics\manager::get_prediction($predictionid, true); if ($context->contextlevel < CONTEXT_COURSE) { // Only for higher levels than course. diff --git a/report/insights/classes/output/renderer.php b/report/insights/classes/output/renderer.php index e79055c9170..4613825fe31 100644 --- a/report/insights/classes/output/renderer.php +++ b/report/insights/classes/output/renderer.php @@ -121,4 +121,25 @@ class renderer extends plugin_renderer_base { return $output; } + + /** + * Renders an analytics disabled notification. + * + * @return string HTML + */ + public function render_analytics_disabled() { + global $OUTPUT, $PAGE, $FULLME; + + $PAGE->set_url($FULLME); + $PAGE->set_title(get_string('pluginname', 'report_insights')); + $PAGE->set_heading(get_string('pluginname', 'report_insights')); + + $output = $OUTPUT->header(); + $output .= $OUTPUT->notification(get_string('analyticsdisabled', 'analytics'), \core\output\notification::NOTIFY_INFO); + $output .= \html_writer::tag('a', get_string('continue'), ['class' => 'btn btn-primary', + 'href' => (new \moodle_url('/'))->out()]); + $output .= $OUTPUT->footer(); + + return $output; + } } diff --git a/report/insights/done.php b/report/insights/done.php index 4878ef0092d..e11aff98843 100644 --- a/report/insights/done.php +++ b/report/insights/done.php @@ -30,6 +30,13 @@ $actionvisiblename = required_param('actionvisiblename', PARAM_NOTAGS); $PAGE->set_pagelayout('popup'); $PAGE->set_context(\context_system::instance()); + +if (!\core_analytics\manager::is_analytics_enabled()) { + $renderer = $PAGE->get_renderer('report_insights'); + echo $renderer->render_analytics_disabled(); + exit(0); +} + $PAGE->set_title(get_site()->fullname); $PAGE->set_url(new \moodle_url('/report/insights/done.php')); diff --git a/report/insights/insights.php b/report/insights/insights.php index 0e4e3a8ea92..bcbd0e936ed 100644 --- a/report/insights/insights.php +++ b/report/insights/insights.php @@ -41,6 +41,12 @@ if ($context->contextlevel < CONTEXT_COURSE) { $PAGE->set_context($context); } +if (!\core_analytics\manager::is_analytics_enabled()) { + $renderer = $PAGE->get_renderer('report_insights'); + echo $renderer->render_analytics_disabled(); + exit(0); +} + \core_analytics\manager::check_can_list_insights($context); // Get all models that are enabled, trained and have predictions at this context. diff --git a/report/insights/lib.php b/report/insights/lib.php index eb7b4f42139..9581d2518c7 100644 --- a/report/insights/lib.php +++ b/report/insights/lib.php @@ -34,7 +34,7 @@ defined('MOODLE_INTERNAL') || die; */ function report_insights_extend_navigation_course($navigation, $course, $context) { - if (has_capability('moodle/analytics:listinsights', $context)) { + if (\core_analytics\manager::is_analytics_enabled() && has_capability('moodle/analytics:listinsights', $context)) { $modelids = \core_analytics\manager::cached_models_with_insights($context); if (!empty($modelids)) { @@ -58,15 +58,17 @@ function report_insights_extend_navigation_course($navigation, $course, $context */ function report_insights_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) { - $context = \context_user::instance($user->id); - if (\core_analytics\manager::check_can_list_insights($context, true)) { + if (\core_analytics\manager::is_analytics_enabled()) { + $context = \context_user::instance($user->id); + if (\core_analytics\manager::check_can_list_insights($context, true)) { - $modelids = \core_analytics\manager::cached_models_with_insights($context); - if (!empty($modelids)) { - $url = new moodle_url('/report/insights/insights.php', array('contextid' => $context->id)); - $node = new core_user\output\myprofile\node('reports', 'insights', get_string('insights', 'report_insights'), - null, $url); - $tree->add_node($node); + $modelids = \core_analytics\manager::cached_models_with_insights($context); + if (!empty($modelids)) { + $url = new moodle_url('/report/insights/insights.php', array('contextid' => $context->id)); + $node = new core_user\output\myprofile\node('reports', 'insights', get_string('insights', 'report_insights'), + null, $url); + $tree->add_node($node); + } } } } @@ -80,7 +82,7 @@ function report_insights_myprofile_navigation(core_user\output\myprofile\tree $t */ function report_insights_extend_navigation_category_settings($navigation, $context) { - if (has_capability('moodle/analytics:listinsights', $context)) { + if (\core_analytics\manager::is_analytics_enabled() && has_capability('moodle/analytics:listinsights', $context)) { $modelids = \core_analytics\manager::cached_models_with_insights($context); if (!empty($modelids)) { diff --git a/report/insights/prediction.php b/report/insights/prediction.php index 32043f70c88..9f09cf22528 100644 --- a/report/insights/prediction.php +++ b/report/insights/prediction.php @@ -27,6 +27,13 @@ require_once($CFG->libdir . '/adminlib.php'); $predictionid = required_param('id', PARAM_INT); +if (!\core_analytics\manager::is_analytics_enabled()) { + $PAGE->set_context(\context_system::instance()); + $renderer = $PAGE->get_renderer('report_insights'); + echo $renderer->render_analytics_disabled(); + exit(0); +} + list($model, $prediction, $context) = \core_analytics\manager::get_prediction($predictionid, true); if ($context->contextlevel < CONTEXT_COURSE) { // Only for higher levels than course. diff --git a/report/insights/settings.php b/report/insights/settings.php index 49a4559d7a1..d131860f762 100644 --- a/report/insights/settings.php +++ b/report/insights/settings.php @@ -26,9 +26,11 @@ defined('MOODLE_INTERNAL') || die; -// Just a link to course report. -$ADMIN->add('reports', new admin_externalpage('reportinsights', get_string('insights', 'report_insights'), - $CFG->wwwroot . "/report/insights/insights.php?contextid=" . SYSCONTEXTID, 'moodle/analytics:listinsights')); +if (\core_analytics\manager::is_analytics_enabled()) { + // Just a link to course report. + $ADMIN->add('reports', new admin_externalpage('reportinsights', get_string('insights', 'report_insights'), + $CFG->wwwroot . "/report/insights/insights.php?contextid=" . SYSCONTEXTID, 'moodle/analytics:listinsights')); -// No report settings. -$settings = null; + // No report settings. + $settings = null; +} diff --git a/version.php b/version.php index 4caf05b5059..441013d48ab 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2019100800.00; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2019100800.04; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes.