diff --git a/analytics/classes/prediction.php b/analytics/classes/prediction.php index 6b0929e27072c..3b0aced6c9df9 100644 --- a/analytics/classes/prediction.php +++ b/analytics/classes/prediction.php @@ -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 * diff --git a/analytics/tests/prediction_actions_test.php b/analytics/tests/prediction_actions_test.php index f8f2866dcb896..8aa6c2408b957 100644 --- a/analytics/tests/prediction_actions_test.php +++ b/analytics/tests/prediction_actions_test.php @@ -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 */ diff --git a/analytics/upgrade.txt b/analytics/upgrade.txt index b23200c73d798..0798670ecbb61 100644 --- a/analytics/upgrade.txt +++ b/analytics/upgrade.txt @@ -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 === diff --git a/report/insights/classes/output/insight.php b/report/insights/classes/output/insight.php index 900a81e60e00e..cd15f4e1965d2 100644 --- a/report/insights/classes/output/insight.php +++ b/report/insights/classes/output/insight.php @@ -24,6 +24,8 @@ namespace report_insights\output; +use core_analytics\prediction; + defined('MOODLE_INTERNAL') || die(); /** @@ -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; }