From 4a210b068811cc9a043fb47177e945a4a735da6b Mon Sep 17 00:00:00 2001 From: David Monllao Date: Wed, 26 Jul 2017 09:17:58 +0200 Subject: [PATCH] MDL-57791 analytics: Predictions deleted context management --- .../tool/analytics/classes/output/models_list.php | 5 ----- analytics/classes/manager.php | 9 ++++++--- analytics/classes/model.php | 14 ++++++++------ lang/en/analytics.php | 2 ++ report/insights/lang/en/report_insights.php | 1 - 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/admin/tool/analytics/classes/output/models_list.php b/admin/tool/analytics/classes/output/models_list.php index d495554ba88..181b48dc48f 100644 --- a/admin/tool/analytics/classes/output/models_list.php +++ b/admin/tool/analytics/classes/output/models_list.php @@ -74,11 +74,6 @@ class models_list implements \renderable, \templatable { foreach ($predictioncontexts as $contextid => $unused) { // We prepare this to be used as single_select template options. $context = \context::instance_by_id($contextid); - if (empty($context)) { - // The context may have been deleted. - unset($predictioncontexts[$contextid]); - continue; - } // Special name for system level predictions as showing "System is not visually nice". if ($contextid == SYSCONTEXTID) { diff --git a/analytics/classes/manager.php b/analytics/classes/manager.php index 152f5a76582..581f917ac78 100644 --- a/analytics/classes/manager.php +++ b/analytics/classes/manager.php @@ -370,14 +370,17 @@ class manager { global $DB; if (!$predictionobj = $DB->get_record('analytics_predictions', array('id' => $predictionid))) { - throw new \moodle_exception('errorpredictionnotfound', 'report_insights'); + throw new \moodle_exception('errorpredictionnotfound', 'analytics'); + } + + $context = \context::instance_by_id($predictionobj->contextid, IGNORE_MISSING); + if (!$context) { + throw new \moodle_exception('errorpredictioncontextnotavailable', 'analytics'); } if ($requirelogin) { list($context, $course, $cm) = get_context_info_array($predictionobj->contextid); require_login($course, false, $cm); - } else { - $context = \context::instance_by_id($predictionobj->contextid); } self::check_can_list_insights($context); diff --git a/analytics/classes/model.php b/analytics/classes/model.php index fecb10a41e1..a76acd5c096 100644 --- a/analytics/classes/model.php +++ b/analytics/classes/model.php @@ -986,7 +986,9 @@ class model { public function get_predictions_contexts() { global $DB; - $sql = "SELECT DISTINCT contextid FROM {analytics_predictions} WHERE modelid = ?"; + $sql = "SELECT DISTINCT ap.contextid FROM {analytics_predictions} ap + JOIN {context} ctx ON ctx.id = ap.contextid + WHERE ap.modelid = ?"; return $DB->get_records_sql($sql, array($this->model->id)); } @@ -1044,16 +1046,16 @@ class model { \core_analytics\manager::check_can_list_insights($context); // Filters out previous predictions keeping only the last time range one. - $sql = "SELECT tip.* - FROM {analytics_predictions} tip + $sql = "SELECT ap.* + FROM {analytics_predictions} ap JOIN ( SELECT sampleid, max(rangeindex) AS rangeindex FROM {analytics_predictions} WHERE modelid = ? and contextid = ? GROUP BY sampleid - ) tipsub - ON tip.sampleid = tipsub.sampleid AND tip.rangeindex = tipsub.rangeindex - WHERE tip.modelid = ? and tip.contextid = ?"; + ) apsub + ON ap.sampleid = apsub.sampleid AND ap.rangeindex = apsub.rangeindex + WHERE ap.modelid = ? and ap.contextid = ?"; $params = array($this->model->id, $context->id, $this->model->id, $context->id); if (!$predictions = $DB->get_records_sql($sql, $params)) { return array(); diff --git a/lang/en/analytics.php b/lang/en/analytics.php index fc368aaa7d1..6ce2124c940 100644 --- a/lang/en/analytics.php +++ b/lang/en/analytics.php @@ -42,7 +42,9 @@ $string['errornopredictresults'] = 'No results returned from the predictions pro $string['errornotimesplittings'] = 'This model does not have any time splitting method'; $string['errornoroles'] = 'Student or teacher roles have not been defined. Define them in analytics settings page.'; $string['errornotarget'] = 'This model does not have any target'; +$string['errorpredictioncontextnotavailable'] = 'This prediction context is not available anymore'; $string['errorpredictionformat'] = 'Wrong prediction calculations format'; +$string['errorpredictionnotfound'] = 'Prediction not found'; $string['errorpredictionsprocessor'] = 'Predictions processor error: {$a}'; $string['errorpredictwrongformat'] = 'The predictions processor return can not be decoded: "{$a}"'; $string['errorprocessornotready'] = 'The selected predictions processor is not ready: {$a}'; diff --git a/report/insights/lang/en/report_insights.php b/report/insights/lang/en/report_insights.php index a5e1d75bb76..17af8e99018 100644 --- a/report/insights/lang/en/report_insights.php +++ b/report/insights/lang/en/report_insights.php @@ -24,7 +24,6 @@ $string['disabledmodel'] = 'Sorry, this model has been disabled by the administrator'; -$string['errorpredictionnotfound'] = 'Prediction not found'; $string['insights'] = 'Insights'; $string['pluginname'] = 'Insights'; $string['prediction'] = 'Prediction';