Skip to content

Commit

Permalink
MDL-65553 core_analytics: Don't allow to flag predictions several times
Browse files Browse the repository at this point in the history
Even if a prediction is hidden from the report once is flagged,
it can be flagged several times if the user visits detailed view via URL.
We remove the checkbox to select a prediction and flag it
once it has already been flagged.
  • Loading branch information
Amaia Anabitarte committed Apr 15, 2021
1 parent 511a87f commit 5af5368
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 19 deletions.
27 changes: 27 additions & 0 deletions analytics/classes/prediction.php
Expand Up @@ -178,6 +178,33 @@ public function action_executed($actionname, \core_analytics\local\target\base $
\core\event\prediction_action_started::create($eventdata)->trigger();
}

/**
* Get the executed actions.
*
* Actions could be filtered by actionname.
*
* @param array $actionnamefilter Limit the results obtained to this list of action names.
* @param int $userid the user id. Current user by default.
* @return array of actions.
*/
public function get_executed_actions(array $actionnamefilter = null, int $userid = 0): array {
global $USER, $DB;

$conditions[] = "predictionid = :predictionid";
$params['predictionid'] = $this->get_prediction_data()->id;
if (!$userid) {
$userid = $USER->id;
}
$conditions[] = "userid = :userid";
$params['userid'] = $userid;
if ($actionnamefilter) {
list($actionsql, $actionparams) = $DB->get_in_or_equal($actionnamefilter, SQL_PARAMS_NAMED);
$conditions[] = "actionname $actionsql";
$params = $params + $actionparams;
}
return $DB->get_records_select('analytics_prediction_actions', implode(' AND ', $conditions), $params);
}

/**
* format_calculations
*
Expand Down
80 changes: 80 additions & 0 deletions analytics/tests/prediction_actions_test.php
Expand Up @@ -112,6 +112,86 @@ public function test_action_executed() {
$this->assertEquals(2, $DB->count_records('analytics_prediction_actions'));
}

/**
* Data provider for test_get_executed_actions.
*
* @return array
*/
public function execute_actions_provider(): array {
return [
'Empty actions with no filter' => [
[],
[],
0
],
'Empty actions with filter' => [
[],
[\core_analytics\prediction::ACTION_FIXED],
0
],
'Multiple actions with no filter' => [
[
\core_analytics\prediction::ACTION_FIXED,
\core_analytics\prediction::ACTION_FIXED,
\core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED
],
[],
3
],
'Multiple actions applying filter' => [
[
\core_analytics\prediction::ACTION_FIXED,
\core_analytics\prediction::ACTION_FIXED,
\core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED
],
[\core_analytics\prediction::ACTION_FIXED],
2
],
'Multiple actions not applying filter' => [
[
\core_analytics\prediction::ACTION_FIXED,
\core_analytics\prediction::ACTION_FIXED,
\core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED
],
[\core_analytics\prediction::ACTION_NOT_APPLICABLE],
0
],
'Multiple actions with multiple filter' => [
[
\core_analytics\prediction::ACTION_FIXED,
\core_analytics\prediction::ACTION_FIXED,
\core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED
],
[\core_analytics\prediction::ACTION_FIXED, \core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED],
3
],
];
}

/**
* Tests for get_executed_actions() function.
*
* @dataProvider execute_actions_provider
* @param array $actionstoexecute An array of actions to execute
* @param array $actionnamefilter Actions to filter
* @param int $returned Number of actions returned
*
* @covers \core_analytics\prediction::get_executed_actions
*/
public function test_get_executed_actions(array $actionstoexecute, array $actionnamefilter, int $returned) {

$this->setUser($this->teacher2);
list($ignored, $predictions) = $this->model->get_predictions($this->context, true);
$prediction = reset($predictions);
$target = $this->model->get_target();
foreach($actionstoexecute as $action) {
$prediction->action_executed($action, $target);
}

$filteredactions = $prediction->get_executed_actions($actionnamefilter);
$this->assertCount($returned, $filteredactions);
}

/**
* test_get_predictions
*/
Expand Down
2 changes: 2 additions & 0 deletions analytics/upgrade.txt
Expand Up @@ -11,6 +11,8 @@ information provided here is intended especially for developers.
by updating the lib/db/analytics.php file and bumping the core version.
* Final deprecation - get_analysables(). Please see get_analysables_interator() instead.
get_analysables_iterator() needs to be overridden by the child class.
* A new function get_executed_actions() has been added to \core_analytics\prediction class
to get all (or filtered by action name) executed actions of a prediction

=== 3.8 ===

Expand Down
49 changes: 30 additions & 19 deletions report/insights/classes/output/insight.php
Expand Up @@ -24,6 +24,8 @@

namespace report_insights\output;

use core_analytics\prediction;

defined('MOODLE_INTERNAL') || die();

/**
Expand Down Expand Up @@ -173,25 +175,34 @@ public function export_for_template(\renderer_base $output) {
);
}

// This is only rendered in report_insights/insight_details template. We need it to automatically enable
// the bulk action buttons in report/insights/prediction.php.
$toggleall = new \core\output\checkbox_toggleall('insight-bulk-action-' . $predictedvalue, true, [
'id' => 'id-toggle-all-' . $predictedvalue,
'name' => 'toggle-all-' . $predictedvalue,
'classes' => 'hidden',
'label' => get_string('selectall'),
'labelclasses' => 'sr-only',
'checked' => false
]);
$data->hiddencheckboxtoggleall = $output->render($toggleall);

$toggle = new \core\output\checkbox_toggleall('insight-bulk-action-' . $predictedvalue, false, [
'id' => 'id-select-' . $data->predictionid,
'name' => 'select-' . $data->predictionid,
'label' => get_string('selectprediction', 'report_insights', $data->sampledescription),
'labelclasses' => 'accesshide',
]);
$data->toggleslave = $output->render($toggle);
// This is only rendered in report_insights/insight_details template for predictions with no action.
// We need it to automatically enable the bulk action buttons in report/insights/prediction.php.
$filtered = [
\core_analytics\prediction::ACTION_FIXED,
\core_analytics\prediction::ACTION_NOT_USEFUL,
\core_analytics\prediction::ACTION_USEFUL,
\core_analytics\prediction::ACTION_NOT_APPLICABLE,
\core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED,
];
if (!$this->prediction->get_executed_actions($filtered)) {
$toggleall = new \core\output\checkbox_toggleall('insight-bulk-action-' . $predictedvalue, true, [
'id' => 'id-toggle-all-' . $predictedvalue,
'name' => 'toggle-all-' . $predictedvalue,
'classes' => 'hidden',
'label' => get_string('selectall'),
'labelclasses' => 'sr-only',
'checked' => false,
]);
$data->hiddencheckboxtoggleall = $output->render($toggleall);

$toggle = new \core\output\checkbox_toggleall('insight-bulk-action-' . $predictedvalue, false, [
'id' => 'id-select-' . $data->predictionid,
'name' => 'select-' . $data->predictionid,
'label' => get_string('selectprediction', 'report_insights', $data->sampledescription),
'labelclasses' => 'accesshide',
]);
$data->toggleslave = $output->render($toggle);
}

return $data;
}
Expand Down

0 comments on commit 5af5368

Please sign in to comment.