MDL-66091 analytics: Targets choose if there should be a report or not

This commit is contained in:
David Monllaó 2019-08-22 15:50:59 +08:00
parent 21202090f7
commit 486e797c5f
8 changed files with 70 additions and 20 deletions

View File

@ -193,19 +193,23 @@ class insights_generator {
$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;
}

View File

@ -105,6 +105,15 @@ abstract class base extends \core_analytics\calculable {
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.
*
@ -147,7 +156,7 @@ abstract class base extends \core_analytics\calculable {
$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();

View File

@ -484,6 +484,9 @@ class manager {
/**
* 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[]
*/
@ -494,7 +497,7 @@ class manager {
$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]);
}
}

View File

@ -963,19 +963,22 @@ class model {
$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);
}
}
}
}

View File

@ -33,7 +33,8 @@
"text": "Moodle"
}, {
"url": "https://en.wikipedia.org/wiki/Noodle",
"text": "Noodle"
"text": "Noodle",
"opentoblank": 1
}
]
}
@ -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}}

View File

@ -261,6 +261,7 @@ $string['urlforical'] = 'URL for iCalendar export, for subscribing to calendar';
$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';

View File

@ -40,10 +40,24 @@ if ($context->contextlevel < CONTEXT_COURSE) {
// 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);
@ -89,6 +103,12 @@ if (!$modelid) {
$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();

View File

@ -160,6 +160,15 @@ class upcoming_activities_due extends \core_analytics\local\target\binary {
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.
*
@ -180,9 +189,9 @@ class upcoming_activities_due extends \core_analytics\local\target\binary {
// 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);
}