From c614073940925880dd7d6c843dee7018d93215f7 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Fri, 6 Oct 2017 17:20:55 +0800 Subject: [PATCH] MDL-59106 report_insights: show time created and time range used --- report/insights/classes/output/insight.php | 23 ++++++-- .../insights/classes/output/insights_list.php | 28 ++++++++- report/insights/lang/en/report_insights.php | 3 + report/insights/templates/insight.mustache | 17 +----- .../templates/insight_details.mustache | 34 +++++++++-- .../insights/templates/insights_list.mustache | 59 ++++++++++++------- 6 files changed, 115 insertions(+), 49 deletions(-) diff --git a/report/insights/classes/output/insight.php b/report/insights/classes/output/insight.php index 525129fd8f0..d0ffa6e2114 100644 --- a/report/insights/classes/output/insight.php +++ b/report/insights/classes/output/insight.php @@ -71,10 +71,23 @@ class insight implements \renderable, \templatable { * @return \stdClass */ public function export_for_template(\renderer_base $output) { + // Get the prediction data. + $predictiondata = $this->prediction->get_prediction_data(); $data = new \stdClass(); $data->insightname = format_string($this->model->get_target()->get_name()); + // Get the details. + $data->timecreated = userdate($predictiondata->timecreated); + $data->timerange = ''; + + if (!empty($predictiondata->timestart) && !empty($predictiondata->timeend)) { + $timerange = new \stdClass(); + $timerange->timestart = userdate($predictiondata->timestart); + $timerange->timeend = userdate($predictiondata->timeend); + $data->timerange = get_string('timerangewithdata', 'report_insights', $timerange); + } + // Sample info (determined by the analyser). list($data->sampledescription, $samplerenderable) = $this->model->prediction_sample_description($this->prediction); @@ -84,10 +97,10 @@ class insight implements \renderable, \templatable { } // Prediction info. - $predictedvalue = $this->prediction->get_prediction_data()->prediction; - $predictionid = $this->prediction->get_prediction_data()->id; + $predictedvalue = $predictiondata->prediction; + $predictionid = $predictiondata->id; $data->predictiondisplayvalue = $this->model->get_target()->get_display_value($predictedvalue); - list($data->style, $data->outcomeicon) = $this->get_calculation_display($this->model->get_target(), + list($data->style, $data->outcomeicon) = self::get_calculation_display($this->model->get_target(), floatval($predictedvalue), $output); $actions = $this->model->get_target()->prediction_actions($this->prediction, $this->includedetailsaction); @@ -124,7 +137,7 @@ class insight implements \renderable, \templatable { $obj = new \stdClass(); $obj->name = call_user_func(array($calculation->indicator, 'get_name')); $obj->displayvalue = $calculation->indicator->get_display_value($calculation->value, $calculation->subtype); - list($obj->style, $obj->outcomeicon) = $this->get_calculation_display($calculation->indicator, + list($obj->style, $obj->outcomeicon) = self::get_calculation_display($calculation->indicator, floatval($calculation->value), $output, $calculation->subtype); $data->calculations[] = $obj; @@ -149,7 +162,7 @@ class insight implements \renderable, \templatable { * @param string|false $subtype * @return array The style as 'success', 'info', 'warning' or 'danger' and pix_icon */ - protected function get_calculation_display(\core_analytics\calculable $calculable, $value, $output, $subtype = false) { + public static function get_calculation_display(\core_analytics\calculable $calculable, $value, $output, $subtype = false) { $outcome = $calculable->get_calculation_outcome($value, $subtype); switch ($outcome) { case \core_analytics\calculable::OUTCOME_NEUTRAL: diff --git a/report/insights/classes/output/insights_list.php b/report/insights/classes/output/insights_list.php index 1da683eb2b7..158e8bebb1c 100644 --- a/report/insights/classes/output/insights_list.php +++ b/report/insights/classes/output/insights_list.php @@ -95,17 +95,39 @@ class insights_list implements \renderable, \templatable { if ($this->model->uses_insights()) { $predictionsdata = $this->model->get_predictions($this->context, true, $this->page, $this->perpage); - $data->insights = array(); + $data->predictions = array(); + $predictionvalues = array(); + $insights = array(); if ($predictionsdata) { list($total, $predictions) = $predictionsdata; foreach ($predictions as $prediction) { + $predictedvalue = $prediction->get_prediction_data()->prediction; + + // Only need to fill this data once. + if (!isset($predictionvalues[$predictedvalue])) { + $preddata = array(); + $preddata['predictiondisplayvalue'] = $this->model->get_target()->get_display_value($predictedvalue); + list($preddata['style'], $preddata['outcomeicon']) = + insight::get_calculation_display($this->model->get_target(), $predictedvalue, $output); + $predictionvalues[$predictedvalue] = $preddata; + } + $insightrenderable = new \report_insights\output\insight($prediction, $this->model, true); - $data->insights[] = $insightrenderable->export_for_template($output); + $insights[$predictedvalue][] = $insightrenderable->export_for_template($output); + } + + // Ok, now we have all the data we want, put it into a format that mustache can handle. + foreach ($predictionvalues as $key => $prediction) { + if (isset($insights[$key])) { + $prediction['insights'] = $insights[$key]; + } + + $data->predictions[] = $prediction; } } - if (empty($data->insights) && $this->page == 0) { + if (empty($insights) && $this->page == 0) { if ($this->model->any_prediction_obtained()) { $data->noinsights = get_string('noinsights', 'analytics'); } else { diff --git a/report/insights/lang/en/report_insights.php b/report/insights/lang/en/report_insights.php index a69600b98cf..68827ab9b36 100644 --- a/report/insights/lang/en/report_insights.php +++ b/report/insights/lang/en/report_insights.php @@ -40,4 +40,7 @@ $string['prediction'] = 'Prediction'; $string['predictioncalculations'] = 'Indicator calculations'; $string['predictiondetails'] = 'Prediction details'; $string['nodetailsavailable'] = 'No prediction details are relevant.'; +$string['timecreated'] = 'Time predicted'; +$string['timerange'] = 'Time range'; +$string['timerangewithdata'] = '{$a->timestart} to {$a->timeend}'; $string['selectotherinsights'] = 'Select other insights...'; diff --git a/report/insights/templates/insight.mustache b/report/insights/templates/insight.mustache index ad24102fc3e..693549d7f50 100644 --- a/report/insights/templates/insight.mustache +++ b/report/insights/templates/insight.mustache @@ -31,29 +31,16 @@ Example context (json): { "sampleimage": "Link", - "sampledescription": "Sample description", - "style": "success", - "outcomeicon": { - "attributes": [ - {"name": "src", "value": "https://moodle.org/logo/moodle-logo.svg" } - ] - }, - "predictiondisplayvalue": "This dev will understand it" + "sampledescription": "Sample description" } }} - + {{#sampleimage}} {{{sampleimage}}} {{/sampleimage}} {{{sampledescription}}} - - {{#outcomeicon}} - {{> core/pix_icon}} - {{/outcomeicon}} - {{predictiondisplayvalue}} - {{#actions}} {{> core/action_menu}} diff --git a/report/insights/templates/insight_details.mustache b/report/insights/templates/insight_details.mustache index 72940c9e1fb..f5a6493d263 100644 --- a/report/insights/templates/insight_details.mustache +++ b/report/insights/templates/insight_details.mustache @@ -31,6 +31,8 @@ Example context (json): { "insightname": "Best insight ever", + "timecreated": "Thursday, 5 October 2017, 4:16 PM", + "timerange": "Monday, 4 September 2017, 6:00 PM to Thursday, 5 October 2017, 12:00 AM", "sampleimage": "Link", "sampledescription": "Sample description", "style": "success", @@ -73,11 +75,18 @@

{{#str}}insightprediction, report_insights, {{insightname}} {{/str}}

- + - - + @@ -86,8 +95,23 @@
{{#str}}insight, report_insights{{/str}} + {{#str}}prediction, report_insights{{/str}}: + + {{#outcomeicon}} + {{> core/pix_icon}} + {{/outcomeicon}} + {{predictiondisplayvalue}} + +
{{#str}}name{{/str}}{{#str}}prediction, report_insights{{/str}}{{#str}}name{{/str}} {{#str}}actions{{/str}}
- +
+ + + + + + {{#timerange}} + + + + + {{/timerange}} + +
{{#str}}predictiondetails, report_insights{{/str}}
{{#str}}timecreated, report_insights{{/str}}{{timecreated}}
{{#str}}timerange, report_insights{{/str}}{{.}}
+ + @@ -99,7 +123,7 @@ - + {{/calculations}}
{{#str}}predictioncalculations, report_insights{{/str}}
{{#str}}indicator, report_insights{{/str}}
{{name}} {{#outcomeicon}}{{> core/pix_icon}}{{/outcomeicon}} {{displayvalue}}
diff --git a/report/insights/templates/insights_list.mustache b/report/insights/templates/insights_list.mustache index ecb26899d65..2c6c2008252 100644 --- a/report/insights/templates/insights_list.mustache +++ b/report/insights/templates/insights_list.mustache @@ -31,27 +31,35 @@ Example context (json): { "insightname": "Best insight ever", - "insights": [ + "predictions": [ { - "sampleimage": "Link", - "sampledescription": "Sample description", + "predictiondisplayvalue": "This dev will understand it", "style": "success", "outcomeicon": { "attributes": [ {"name": "src", "value": "https://moodle.org/logo/moodle-logo.svg" } ] }, - "predictiondisplayvalue": "This dev will understand it" + "insights": [ + { + "sampleimage": "Link", + "sampledescription": "Sample description" + } + ] }, { - "sampleimage": "Any renderable", - "sampledescription": "Another sample description", + "predictiondisplayvalue": "This dev will not understand it", "style": "danger", "outcomeicon": { "attributes": [ {"name": "src", "value": "https://moodle.org/logo/moodle-logo.svg" } ] }, - "predictiondisplayvalue": "This dev will not understand it" + "insights": [ + { + "sampleimage": "Any renderable", + "sampledescription": "Another sample description" + } + ] } ], "noinsights": false @@ -67,21 +75,30 @@

{{{insightname}}}

{{^noinsights}} {{{ pagingbar }}} - - - - - - - - - - - {{#insights}} +{{#predictions}} +
{{#str}}insights, report_insights{{/str}}
{{#str}}name{{/str}}{{#str}}prediction, report_insights{{/str}}{{#str}}actions{{/str}}
+ + + + + + + + {{#insights}} + {{> report_insights/insight}} - {{/insights}} - -
+ {{#str}}prediction, report_insights{{/str}}: + + {{#outcomeicon}} + {{> core/pix_icon}} + {{/outcomeicon}} + {{predictiondisplayvalue}} + +
{{#str}}name{{/str}}{{#str}}actions{{/str}}
+ + {{/insights}} + +{{/predictions}} {{{ pagingbar }}} {{/noinsights}} {{#noinsights}}