Skip to content

Commit

Permalink
MDL-66091 analytics: Targets choose if there should be a report or not
Browse files Browse the repository at this point in the history
  • Loading branch information
David Monllaó committed Sep 18, 2019
1 parent 5e17f2f commit b2d9aaa
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 20 deletions.
6 changes: 5 additions & 1 deletion analytics/classes/insights_generator.php
Expand Up @@ -193,19 +193,23 @@ private function prediction_info(\core_analytics\prediction $prediction) {
$insighturl = null;
foreach ($predictionactions as $action) {
$actionurl = $action->get_url();
$opentoblank = false;
if (!$actionurl->get_param('forwardurl')) {

$params = ['actionvisiblename' => $action->get_text(), 'target' => '_blank'];
$actiondoneurl = new \moodle_url('/report/insights/done.php', $params);
// Set the forward url to the 'done' script.
$actionurl->param('forwardurl', $actiondoneurl->out(false));

$opentoblank = true;
}

if (empty($insighturl)) {
// We use the primary action url as insight url so we log that the user followed the provided link.
$insighturl = $action->get_url();
}
$actiondata = (object)['url' => $action->get_url()->out(false), 'text' => $action->get_text()];
$actiondata = (object)['url' => $action->get_url()->out(false), 'text' => $action->get_text(),
'opentoblank' => $opentoblank];
$fullmessageplaintext .= get_string('insightinfomessageaction', 'analytics', $actiondata) . PHP_EOL;
$messageactions[] = $actiondata;
}
Expand Down
11 changes: 10 additions & 1 deletion analytics/classes/local/target/base.php
Expand Up @@ -95,6 +95,15 @@ public static function uses_insights() {
return true;
}

/**
* Should the insights of this model be linked from reports?
*
* @return bool
*/
public function link_insights_report(): bool {
return true;
}

/**
* Based on facts (processed by machine learning backends) by default.
*
Expand Down Expand Up @@ -137,7 +146,7 @@ public function prediction_actions(\core_analytics\prediction $prediction, $incl

$actions = array();

if ($includedetailsaction) {
if ($this->link_insights_report() && $includedetailsaction) {

$predictionurl = new \moodle_url('/report/insights/prediction.php', array('id' => $predictionid));
$detailstext = $this->get_view_details_text();
Expand Down
5 changes: 4 additions & 1 deletion analytics/classes/manager.php
Expand Up @@ -480,6 +480,9 @@ public static function get_indicator_calculations($analysable, $starttime, $endt
/**
* Returns the models with insights at the provided context.
*
* Note that this method is used for display purposes. It filters out models whose insights
* are not linked from the reports page.
*
* @param \context $context
* @return \core_analytics\model[]
*/
Expand All @@ -490,7 +493,7 @@ public static function get_models_with_insights(\context $context) {
$models = self::get_all_models(true, true, $context);
foreach ($models as $key => $model) {
// Check that it not only have predictions but also generates insights from them.
if (!$model->uses_insights()) {
if (!$model->uses_insights() || !$model->get_target()->link_insights_report()) {
unset($models[$key]);
}
}
Expand Down
29 changes: 16 additions & 13 deletions analytics/classes/model.php
Expand Up @@ -933,19 +933,22 @@ protected function trigger_insights($samplecontexts, $predictionrecords) {

$this->get_target()->generate_insight_notifications($this->model->id, $samplecontexts, $predictions);

// Update cache.
$cache = \cache::make('core', 'contextwithinsights');
foreach ($samplecontexts as $context) {
$modelids = $cache->get($context->id);
if (!$modelids) {
// The cache is empty, but we don't know if it is empty because there are no insights
// in this context or because cache/s have been purged, we need to be conservative and
// "pay" 1 db read to fill up the cache.
$models = \core_analytics\manager::get_models_with_insights($context);
$cache->set($context->id, array_keys($models));
} else if (!in_array($this->get_id(), $modelids)) {
array_push($modelids, $this->get_id());
$cache->set($context->id, $modelids);
if ($this->get_target()->link_insights_report()) {

// Update cache.
$cache = \cache::make('core', 'contextwithinsights');
foreach ($samplecontexts as $context) {
$modelids = $cache->get($context->id);
if (!$modelids) {
// The cache is empty, but we don't know if it is empty because there are no insights
// in this context or because cache/s have been purged, we need to be conservative and
// "pay" 1 db read to fill up the cache.
$models = \core_analytics\manager::get_models_with_insights($context);
$cache->set($context->id, array_keys($models));
} else if (!in_array($this->get_id(), $modelids)) {
array_push($modelids, $this->get_id());
$cache->set($context->id, $modelids);
}
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions analytics/templates/insight_info_message_prediction.mustache
Expand Up @@ -33,7 +33,8 @@
"text": "Moodle"
}, {
"url": "https://en.wikipedia.org/wiki/Noodle",
"text": "Noodle"
"text": "Noodle",
"opentoblank": 1
}
]
}
Expand Down Expand Up @@ -65,5 +66,5 @@ body:not(.dir-ltr):not(.dir-rtl) .btn-insight {
<br/>
{{#actions}}
<a class="btn btn-default m-r-1 m-b-1 btn-insight" href="{{url}}">{{text}}</a>
<a class="btn btn-default m-r-1 m-b-1 btn-insight" {{#opentoblank}}target="_blank" {{/opentoblank}}href="{{url}}">{{text}}</a>
{{/actions}}
1 change: 1 addition & 0 deletions lang/en/calendar.php
Expand Up @@ -259,6 +259,7 @@
$string['user'] = 'User';
$string['userevent'] = 'User event';
$string['userevents'] = 'User events';
$string['viewupcomingactivitiesdue'] = 'View the upcoming activities due';
$string['wed'] = 'Wed';
$string['wednesday'] = 'Wednesday';
$string['weekly'] = 'Weekly';
Expand Down
20 changes: 20 additions & 0 deletions report/insights/insights.php
Expand Up @@ -40,10 +40,24 @@
// Only for higher levels than course.
$PAGE->set_context($context);
}

\core_analytics\manager::check_can_list_insights($context);

// Get all models that are enabled, trained and have predictions at this context.
$othermodels = \core_analytics\manager::get_all_models(true, true, $context);
array_filter($othermodels, function($model) use ($context) {

// Discard insights that are not linked unless you are a manager.
if (!$model->get_target()->link_insights_report()) {
try {
\core_analytics\manager::check_can_manage_models();
} catch (\required_capability_exception $e) {
return false;
}
}
return true;
});

if (!$modelid && count($othermodels)) {
// Autoselect the only available model.
$model = reset($othermodels);
Expand Down Expand Up @@ -89,6 +103,12 @@

$model = new \core_analytics\model($modelid);

if (!$model->get_target()->link_insights_report()) {

// Only manager access if this target does not link the insights report.
\core_analytics\manager::check_can_manage_models();
}

$insightinfo = new stdClass();
$insightinfo->contextname = $context->get_context_name();
$insightinfo->insightname = $model->get_target()->get_name();
Expand Down
13 changes: 11 additions & 2 deletions user/classes/analytics/target/upcoming_activities_due.php
Expand Up @@ -150,6 +150,15 @@ protected function calculate_sample($sampleid, \core_analytics\analysable $analy
return 0;
}

/**
* No need to link to the insights report in this case.
*
* @return bool
*/
public function link_insights_report(): bool {
return false;
}

/**
* Adds a view upcoming events action.
*
Expand All @@ -170,9 +179,9 @@ public function prediction_actions(\core_analytics\prediction $prediction, $incl

// We force a lookahead of 30 days so we are sure that the upcoming activities due are shown.
$url = new \moodle_url('/calendar/view.php', ['view' => 'upcoming', 'lookahead' => '30']);
$pix = new \pix_icon('i/calendar', get_string('upcomingevents', 'calendar'));
$pix = new \pix_icon('i/calendar', get_string('viewupcomingactivitiesdue', 'calendar'));
$action = new \core_analytics\prediction_action('viewupcoming', $prediction,
$url, $pix, get_string('upcomingevents', 'calendar'));
$url, $pix, get_string('viewupcomingactivitiesdue', 'calendar'));

return array_merge([$action], $parentactions);
}
Expand Down

0 comments on commit b2d9aaa

Please sign in to comment.