Skip to content

Commit

Permalink
MDL-59060 analytics: Add paging to insights report
Browse files Browse the repository at this point in the history
Part of MDL-57791 epic.
  • Loading branch information
Damyon Wiese authored and David Monllao committed Jul 24, 2017
1 parent b3d6879 commit 68bfe1d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 deletions.
26 changes: 20 additions & 6 deletions analytics/classes/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1001,9 +1001,11 @@ public function predictions_exist(\context $context) {
* Gets the predictions for this context.
*
* @param \context $context
* @return \core_analytics\prediction[]
* @param int $page The page of results to fetch
* @param int $perpage The max number of results to fetch
* @return array($total, \core_analytics\prediction[])
*/
public function get_predictions(\context $context) {
public function get_predictions(\context $context, $page = 0, $perpage = 100) {
global $DB;

\core_analytics\manager::check_can_list_insights($context);
Expand Down Expand Up @@ -1032,6 +1034,12 @@ public function get_predictions(\context $context) {
list($unused, $samplesdata) = $this->get_analyser()->get_samples($sampleids);

// Add samples data as part of each prediction.
$paginated = [];

$current = 0;
$offset = $page * $perpage;
$limit = $offset + $perpage;

foreach ($predictions as $predictionid => $predictiondata) {

$sampleid = $predictiondata->sampleid;
Expand All @@ -1042,13 +1050,19 @@ public function get_predictions(\context $context) {
continue;
}

// Replace \stdClass object by \core_analytics\prediction objects.
$prediction = new \core_analytics\prediction($predictiondata, $samplesdata[$sampleid]);
// Return paginated dataset - we cannot paginate in the DB because we post filter the list.
if ($current >= $offset && $current < $limit) {
// Replace \stdClass object by \core_analytics\prediction objects.
$prediction = new \core_analytics\prediction($predictiondata, $samplesdata[$sampleid]);
$predictions[$predictionid] = $prediction;
} else {
unset($predictions[$predictionid]);
}

$predictions[$predictionid] = $prediction;
$current++;
}

return $predictions;
return [$current, $predictions];
}

/**
Expand Down
23 changes: 20 additions & 3 deletions report/insights/classes/output/insights_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,32 @@ class insights_list implements \renderable, \templatable {
*/
protected $othermodels;

/**
* @var int
*/
protected $page;

/**
* @var int
*/
protected $perpage;

/**
* Constructor
*
* @param \core_analytics\model $model
* @param \context $context
* @param \core_analytics\model[] $othermodels
* @param int $page
* @param int $perpage The max number of results to fetch
* @return void
*/
public function __construct(\core_analytics\model $model, \context $context, $othermodels) {
public function __construct(\core_analytics\model $model, \context $context, $othermodels, $page = 0, $perpage = 100) {
$this->model = $model;
$this->context = $context;
$this->othermodels = $othermodels;
$this->page = $page;
$this->perpage = $perpage;
}

/**
Expand All @@ -74,17 +88,18 @@ public function export_for_template(\renderer_base $output) {
global $PAGE;

$data = new \stdClass();
$total = 0;

if ($this->model->uses_insights()) {
$predictions = $this->model->get_predictions($this->context);
list($total, $predictions) = $this->model->get_predictions($this->context, $this->page, $this->perpage);

$data->insights = array();
foreach ($predictions as $prediction) {
$insightrenderable = new \report_insights\output\insight($prediction, $this->model, true);
$data->insights[] = $insightrenderable->export_for_template($output);
}

if (empty($data->insights)) {
if (empty($data->insights) && $this->page == 0) {
if ($this->model->any_prediction_obtained()) {
$data->noinsights = get_string('noinsights', 'analytics');
} else {
Expand Down Expand Up @@ -115,6 +130,8 @@ public function export_for_template(\renderer_base $output) {
$data->modelselector = $modelselector->export_for_template($output);
}

$data->pagingbar = $output->render(new \paging_bar($total, $this->page, $this->perpage, $PAGE->url));

return $data;
}
}
8 changes: 7 additions & 1 deletion report/insights/insights.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@

$contextid = required_param('contextid', PARAM_INT);
$modelid = optional_param('modelid', false, PARAM_INT);
$page = optional_param('page', 0, PARAM_INT);
$perpage = optional_param('perpage', 100, PARAM_INT);

if ($perpage > 1000) {
$perpage = 1000;
}

list($context, $course, $cm) = get_context_info_array($contextid);
require_login($course, false, $cm);
Expand Down Expand Up @@ -85,7 +91,7 @@

echo $OUTPUT->header();

$renderable = new \report_insights\output\insights_list($model, $context, $othermodels);
$renderable = new \report_insights\output\insights_list($model, $context, $othermodels, $page, $perpage);
echo $renderer->render($renderable);

echo $OUTPUT->footer();
7 changes: 6 additions & 1 deletion report/insights/templates/insights_list.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"predictionstyle": "alert alert-danger",
"predictiondisplayvalue": "This dev will not understand it"
}
]
],
"noinsights": false
}
}}

Expand All @@ -53,11 +54,15 @@
{{/modelselector}}

<h3>{{#str}} insights, report_insights {{/str}}</h3>
{{^noinsights}}
{{{ pagingbar }}}
<div class="insights-list">
{{#insights}}
{{> report_insights/insight}}
{{/insights}}
</div>
{{{ pagingbar }}}
{{/noinsights}}
{{#noinsights}}
<div class="m-t-2">
{{> core/notification_info}}
Expand Down

0 comments on commit 68bfe1d

Please sign in to comment.