Skip to content

Commit

Permalink
Merge branch 'MDL-59106_master' of git://github.com/markn86/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
snake committed Oct 12, 2017
2 parents 5bc84e9 + f473958 commit 0ce9026
Show file tree
Hide file tree
Showing 14 changed files with 150 additions and 314 deletions.
2 changes: 1 addition & 1 deletion admin/tool/analytics/lang/en/tool_analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
$string['cantguessstartdate'] = 'Can\'t guess the start date';
$string['cantguessenddate'] = 'Can\'t guess the end date';
$string['clienablemodel'] = 'You can enable the model by selecting a time-splitting method by its ID. Note that you can also enable it later using the web interface (\'none\' to exit).';
$string['clievaluationandpredictions'] = 'A cron task iterates through enabled models and gets predictions. Models evaluation via command line is disabled. You can allow these processes to be executed manually via web interface by enabling <a href="{$a}">\'onlycli\' analytics setting</a>';
$string['clievaluationandpredictions'] = 'A scheduled task iterates through enabled models and gets predictions. Models evaluation via the web interface is disabled. You can allow these processes to be executed manually via the web interface by disabling the <a href="{$a}">\'onlycli\'</a> analytics setting';
$string['editmodel'] = 'Edit "{$a}" model';
$string['edittrainedwarning'] = 'This model has already been trained. Note that changing its indicators or its time-splitting method will delete its previous predictions and start generating new predictions.';
$string['enabled'] = 'Enabled';
Expand Down
11 changes: 9 additions & 2 deletions analytics/classes/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -932,8 +932,6 @@ protected function get_static_predictions(&$indicatorcalculations) {
* @return \context
*/
protected function prepare_prediction_record($sampleid, $rangeindex, $prediction, $predictionscore, $calculations) {
global $DB;

$context = $this->get_analyser()->sample_access_context($sampleid);

$record = new \stdClass();
Expand All @@ -946,6 +944,15 @@ protected function prepare_prediction_record($sampleid, $rangeindex, $prediction
$record->calculations = $calculations;
$record->timecreated = time();

$analysable = $this->get_analyser()->get_sample_analysable($sampleid);
$timesplitting = $this->get_time_splitting();
$timesplitting->set_analysable($analysable);
$range = $timesplitting->get_range_by_index($rangeindex);
if ($range) {
$record->timestart = $range['start'];
$record->timeend = $range['end'];
}

return array($record, $context);
}

Expand Down
2 changes: 2 additions & 0 deletions lib/db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3623,6 +3623,8 @@
<FIELD NAME="predictionscore" TYPE="number" LENGTH="10" NOTNULL="true" SEQUENCE="false" DECIMALS="5"/>
<FIELD NAME="calculations" TYPE="text" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="timestart" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="timeend" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
Expand Down
22 changes: 22 additions & 0 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2676,6 +2676,28 @@ function xmldb_main_upgrade($oldversion) {

// Main savepoint reached.
upgrade_main_savepoint(true, 2017101000.01);
}

if ($oldversion < 2017101000.02) {
// Define field 'timestart' to be added to 'analytics_predictions'.
$table = new xmldb_table('analytics_predictions');
$field = new xmldb_field('timestart', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'timecreated');

// Conditionally launch add field 'timestart'.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Define field 'timeend' to be added to 'analytics_predictions'.
$field = new xmldb_field('timeend', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'timestart');

// Conditionally launch add field 'timeend'.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Main savepoint reached.
upgrade_main_savepoint(true, 2017101000.02);
}

return true;
Expand Down
23 changes: 18 additions & 5 deletions report/insights/classes/output/insight.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,23 @@ public function __construct(\core_analytics\prediction $prediction, \core_analyt
* @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);

Expand All @@ -84,10 +97,10 @@ public function export_for_template(\renderer_base $output) {
}

// 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);
Expand Down Expand Up @@ -124,7 +137,7 @@ public function export_for_template(\renderer_base $output) {
$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;
Expand All @@ -149,7 +162,7 @@ public function export_for_template(\renderer_base $output) {
* @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:
Expand Down
28 changes: 25 additions & 3 deletions report/insights/classes/output/insights_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,39 @@ public function export_for_template(\renderer_base $output) {
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 {
Expand Down
3 changes: 3 additions & 0 deletions report/insights/lang/en/report_insights.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@
$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...';
17 changes: 2 additions & 15 deletions report/insights/templates/insight.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,16 @@
Example context (json):
{
"sampleimage": "<a href=\"#\">Link</a>",
"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"
}
}}
<tr>
<td class="col-sm-6">
<td class="col-sm-10">
{{#sampleimage}}
{{{sampleimage}}}
{{/sampleimage}}
{{{sampledescription}}}
</td>
<td class="{{#style}}table-{{style}}{{/style}} col-sm-4">
{{#outcomeicon}}
{{> core/pix_icon}}
{{/outcomeicon}}
<span>{{predictiondisplayvalue}}</span>
</td>
<td class="col-sm-2">
{{#actions}}
{{> core/action_menu}}
Expand Down
34 changes: 29 additions & 5 deletions report/insights/templates/insight_details.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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": "<a href=\"#\">Link</a>",
"sampledescription": "Sample description",
"style": "success",
Expand Down Expand Up @@ -73,11 +75,18 @@

<h2 class="m-b-2">{{#str}}insightprediction, report_insights, {{insightname}} {{/str}}</h2>
<table class="generaltable insights-list">
<caption>{{#str}}insight, report_insights{{/str}}</caption>
<caption>
{{#str}}prediction, report_insights{{/str}}:
<span class="{{#style}}table-{{style}}{{/style}}">
{{#outcomeicon}}
{{> core/pix_icon}}
{{/outcomeicon}}
{{predictiondisplayvalue}}
</span>
</caption>
<thead>
<tr>
<th scope="col" class="col-sm-6">{{#str}}name{{/str}}</th>
<th scope="col" class="col-sm-4">{{#str}}prediction, report_insights{{/str}}</th>
<th scope="col" class="col-sm-10">{{#str}}name{{/str}}</th>
<th scope="col" class="col-sm-2">{{#str}}actions{{/str}}</th>
</tr>
</thead>
Expand All @@ -86,8 +95,23 @@
</tbody>
</table>

<table class="generaltable prediction-calculations">
<table class="generaltable prediction-timedetails">
<caption>{{#str}}predictiondetails, report_insights{{/str}}</caption>
<tbody>
<tr>
<td scope="col" class="col-sm-3">{{#str}}timecreated, report_insights{{/str}}</td>
<td scope="col" class="col-sm-9">{{timecreated}}</td>
</tr>
{{#timerange}}
<tr>
<td scope="col" class="col-sm-3">{{#str}}timerange, report_insights{{/str}}</td>
<td scope="col" class="col-sm-9">{{.}}</td>
</tr>
{{/timerange}}
</tbody>
</table>
<table class="generaltable prediction-calculations">
<caption class="accesshide">{{#str}}predictioncalculations, report_insights{{/str}}</caption>
<thead>
<tr>
<th scope="col" class="col-sm-8">{{#str}}indicator, report_insights{{/str}}</th>
Expand All @@ -99,7 +123,7 @@
<tr>
<td class="{{#style}}table-{{style}}{{/style}} col-sm-8">{{name}}</td>
<td class="{{#style}}table-{{style}}{{/style}} col-sm-4">{{#outcomeicon}}{{> core/pix_icon}}{{/outcomeicon}} {{displayvalue}}</td>
</td>
</tr>
{{/calculations}}
</tbody>
</table>
Expand Down
59 changes: 38 additions & 21 deletions report/insights/templates/insights_list.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,35 @@
Example context (json):
{
"insightname": "Best insight ever",
"insights": [
"predictions": [
{
"sampleimage": "<a href=\"#\">Link</a>",
"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": "<a href=\"#\">Link</a>",
"sampledescription": "Sample description"
}
]
}, {
"sampleimage": "<a href=\"#\">Any renderable</a>",
"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": "<a href=\"#\">Any renderable</a>",
"sampledescription": "Another sample description"
}
]
}
],
"noinsights": false
Expand All @@ -67,21 +75,30 @@
<h2 class="m-b-2">{{{insightname}}}</h2>
{{^noinsights}}
{{{ pagingbar }}}
<table class="generaltable insights-list">
<caption>{{#str}}insights, report_insights{{/str}}</caption>
<thead>
<tr>
<th scope="col" class="col-sm-6">{{#str}}name{{/str}}</th>
<th scope="col" class="col-sm-4">{{#str}}prediction, report_insights{{/str}}</th>
<th scope="col" class="col-sm-2">{{#str}}actions{{/str}}</th>
</tr>
</thead>
<tbody>
{{#insights}}
{{#predictions}}
<table class="generaltable insights-list">
<caption>
{{#str}}prediction, report_insights{{/str}}:
<span class="{{#style}}table-{{style}}{{/style}}">
{{#outcomeicon}}
{{> core/pix_icon}}
{{/outcomeicon}}
{{predictiondisplayvalue}}
</span>
</caption>
<thead>
<tr>
<th scope="col" class="col-sm-10">{{#str}}name{{/str}}</th>
<th scope="col" class="col-sm-2">{{#str}}actions{{/str}}</th>
</tr>
</thead>
{{#insights}}
<tbody>
{{> report_insights/insight}}
{{/insights}}
</tbody>
</table>
</tbody>
{{/insights}}
</table>
{{/predictions}}
{{{ pagingbar }}}
{{/noinsights}}
{{#noinsights}}
Expand Down
Loading

0 comments on commit 0ce9026

Please sign in to comment.