diff --git a/admin/tool/analytics/classes/output/helper.php b/admin/tool/analytics/classes/output/helper.php index 6543022315d..861d6794f83 100644 --- a/admin/tool/analytics/classes/output/helper.php +++ b/admin/tool/analytics/classes/output/helper.php @@ -92,4 +92,17 @@ class helper { $PAGE->set_title($title); $PAGE->set_heading($title); } + + /** + * Resets the current page. + * + * Note that this function can only be used by analytics pages that work at the system context. + * + * @return null + */ + public static function reset_page() { + global $PAGE; + $PAGE->reset_theme_and_output(); + $PAGE->set_context(\context_system::instance()); + } } diff --git a/admin/tool/analytics/classes/task/predict_models.php b/admin/tool/analytics/classes/task/predict_models.php index 83894beb68b..91f3c850688 100644 --- a/admin/tool/analytics/classes/task/predict_models.php +++ b/admin/tool/analytics/classes/task/predict_models.php @@ -60,6 +60,10 @@ class predict_models extends \core\task\scheduled_task { foreach ($models as $model) { $result = $model->predict(); + + // Reset the page as some indicators may call external functions that overwrite the page context. + \tool_analytics\output\helper::reset_page(); + if ($result) { echo $OUTPUT->heading(get_string('modelresults', 'tool_analytics', $model->get_target()->get_name())); $renderer = $PAGE->get_renderer('tool_analytics'); diff --git a/admin/tool/analytics/classes/task/train_models.php b/admin/tool/analytics/classes/task/train_models.php index b017e9a812f..3c0c3c96019 100644 --- a/admin/tool/analytics/classes/task/train_models.php +++ b/admin/tool/analytics/classes/task/train_models.php @@ -71,6 +71,10 @@ class train_models extends \core\task\scheduled_task { } $result = $model->train(); + + // Reset the page as some indicators may call external functions that overwrite the page context. + \tool_analytics\output\helper::reset_page(); + if ($result) { echo $OUTPUT->heading(get_string('modelresults', 'tool_analytics', $model->get_target()->get_name())); diff --git a/admin/tool/analytics/cli/evaluate_model.php b/admin/tool/analytics/cli/evaluate_model.php index 2836ca76a49..8473aba6ab0 100644 --- a/admin/tool/analytics/cli/evaluate_model.php +++ b/admin/tool/analytics/cli/evaluate_model.php @@ -111,6 +111,9 @@ $analyseroptions = array( // Evaluate its suitability to predict accurately. $results = $model->evaluate($analyseroptions); +// Reset the page as some indicators may call external functions that overwrite the page context. +\tool_analytics\output\helper::reset_page(); + $renderer = $PAGE->get_renderer('tool_analytics'); echo $renderer->render_evaluate_results($results, $model->get_analyser()->get_logs()); diff --git a/admin/tool/analytics/model.php b/admin/tool/analytics/model.php index 3cca64ebda4..ab2fe61796a 100644 --- a/admin/tool/analytics/model.php +++ b/admin/tool/analytics/model.php @@ -156,8 +156,6 @@ switch ($action) { case 'evaluate': confirm_sesskey(); - echo $OUTPUT->header(); - if ($model->is_static()) { throw new moodle_exception('errornostaticevaluate', 'tool_analytics'); } @@ -176,6 +174,12 @@ switch ($action) { $options['mode'] = 'trainedmodel'; } $results = $model->evaluate($options); + + // We reset the theme and the output as some indicators may be using external functions + // which reset $PAGE. + \tool_analytics\output\helper::reset_page(); + echo $OUTPUT->header(); + $renderer = $PAGE->get_renderer('tool_analytics'); echo $renderer->render_evaluate_results($results, $model->get_analyser()->get_logs()); break; @@ -183,8 +187,6 @@ switch ($action) { case 'getpredictions': confirm_sesskey(); - echo $OUTPUT->header(); - if ($onlycli) { throw new moodle_exception('erroronlycli', 'tool_analytics'); } @@ -202,6 +204,11 @@ switch ($action) { $predictlogs = array(); } + // We reset the theme and the output as some indicators may be using external functions + // which reset $PAGE. + \tool_analytics\output\helper::reset_page(); + echo $OUTPUT->header(); + $renderer = $PAGE->get_renderer('tool_analytics'); echo $renderer->render_get_predictions_results($trainresults, $trainlogs, $predictresults, $predictlogs); break;