Skip to content

Commit

Permalink
MDL-57791 analytics: Predictions deleted context management
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonllao committed Jul 26, 2017
1 parent 9e27e29 commit 4a210b0
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
5 changes: 0 additions & 5 deletions admin/tool/analytics/classes/output/models_list.php
Expand Up @@ -74,11 +74,6 @@ public function export_for_template(\renderer_base $output) {
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) {
Expand Down
9 changes: 6 additions & 3 deletions analytics/classes/manager.php
Expand Up @@ -370,14 +370,17 @@ public static function get_prediction($predictionid, $requirelogin = false) {
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);
Expand Down
14 changes: 8 additions & 6 deletions analytics/classes/model.php
Expand Up @@ -986,7 +986,9 @@ public function mark_as_trained() {
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));
}

Expand Down Expand Up @@ -1044,16 +1046,16 @@ public function get_predictions(\context $context, $page = false, $perpage = 100
\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();
Expand Down
2 changes: 2 additions & 0 deletions lang/en/analytics.php
Expand Up @@ -42,7 +42,9 @@
$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}';
Expand Down
1 change: 0 additions & 1 deletion report/insights/lang/en/report_insights.php
Expand Up @@ -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';
Expand Down

0 comments on commit 4a210b0

Please sign in to comment.