| 1 | efrain | 1 | <?php
 | 
        
           |  |  | 2 | // This file is part of Moodle - http://moodle.org/
 | 
        
           |  |  | 3 | //
 | 
        
           |  |  | 4 | // Moodle is free software: you can redistribute it and/or modify
 | 
        
           |  |  | 5 | // it under the terms of the GNU General Public License as published by
 | 
        
           |  |  | 6 | // the Free Software Foundation, either version 3 of the License, or
 | 
        
           |  |  | 7 | // (at your option) any later version.
 | 
        
           |  |  | 8 | //
 | 
        
           |  |  | 9 | // Moodle is distributed in the hope that it will be useful,
 | 
        
           |  |  | 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
        
           |  |  | 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
        
           |  |  | 12 | // GNU General Public License for more details.
 | 
        
           |  |  | 13 | //
 | 
        
           |  |  | 14 | // You should have received a copy of the GNU General Public License
 | 
        
           |  |  | 15 | // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 | 
        
           |  |  | 16 |   | 
        
           |  |  | 17 | namespace report_insights;
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 | use core_external\external_api;
 | 
        
           |  |  | 20 | use core_external\external_value;
 | 
        
           |  |  | 21 | use core_external\external_single_structure;
 | 
        
           |  |  | 22 | use core_external\external_multiple_structure;
 | 
        
           |  |  | 23 | use core_external\external_function_parameters;
 | 
        
           |  |  | 24 | use core_external\external_warnings;
 | 
        
           |  |  | 25 |   | 
        
           |  |  | 26 | /**
 | 
        
           |  |  | 27 |  * This is the external API for this component.
 | 
        
           |  |  | 28 |  *
 | 
        
           |  |  | 29 |  * @package    report_insights
 | 
        
           |  |  | 30 |  * @copyright  2017 David Monllao {@link http://www.davidmonllao.com}
 | 
        
           |  |  | 31 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 32 |  */
 | 
        
           |  |  | 33 | class external extends external_api {
 | 
        
           |  |  | 34 |     /**
 | 
        
           |  |  | 35 |      * action_executed parameters.
 | 
        
           |  |  | 36 |      *
 | 
        
           |  |  | 37 |      * @return external_function_parameters
 | 
        
           |  |  | 38 |      * @since  Moodle 3.8
 | 
        
           |  |  | 39 |      */
 | 
        
           |  |  | 40 |     public static function action_executed_parameters() {
 | 
        
           |  |  | 41 |         return new external_function_parameters (
 | 
        
           |  |  | 42 |             array(
 | 
        
           |  |  | 43 |                 'actionname' => new external_value(PARAM_ALPHANUMEXT, 'The name of the action', VALUE_REQUIRED),
 | 
        
           |  |  | 44 |                 'predictionids' => new external_multiple_structure(
 | 
        
           |  |  | 45 |                      new external_value(PARAM_INT, 'Prediction id', VALUE_REQUIRED),
 | 
        
           |  |  | 46 |                      'Array of prediction ids'
 | 
        
           |  |  | 47 |                 ),
 | 
        
           |  |  | 48 |             )
 | 
        
           |  |  | 49 |         );
 | 
        
           |  |  | 50 |     }
 | 
        
           |  |  | 51 |   | 
        
           |  |  | 52 |     /**
 | 
        
           |  |  | 53 |      * Stores an action executed over a group of predictions.
 | 
        
           |  |  | 54 |      *
 | 
        
           |  |  | 55 |      * @param  string   $actionname
 | 
        
           |  |  | 56 |      * @param  array    $predictionids
 | 
        
           |  |  | 57 |      * @return array an array of warnings and a boolean
 | 
        
           |  |  | 58 |      * @since  Moodle 3.8
 | 
        
           |  |  | 59 |      */
 | 
        
           |  |  | 60 |     public static function action_executed(string $actionname, array $predictionids) {
 | 
        
           |  |  | 61 |   | 
        
           |  |  | 62 |         $params = self::validate_parameters(self::action_executed_parameters(),
 | 
        
           |  |  | 63 |             array('actionname' => $actionname, 'predictionids' => $predictionids));
 | 
        
           |  |  | 64 |   | 
        
           |  |  | 65 |         foreach ($params['predictionids'] as $predictionid) {
 | 
        
           |  |  | 66 |             list($model, $prediction, $context) = self::validate_prediction($predictionid);
 | 
        
           |  |  | 67 |   | 
        
           |  |  | 68 |             // The method action_executed checks that the provided action is valid.
 | 
        
           |  |  | 69 |             $prediction->action_executed($actionname, $model->get_target());
 | 
        
           |  |  | 70 |         }
 | 
        
           |  |  | 71 |   | 
        
           |  |  | 72 |         return array('warnings' => array());
 | 
        
           |  |  | 73 |     }
 | 
        
           |  |  | 74 |   | 
        
           |  |  | 75 |     /**
 | 
        
           |  |  | 76 |      * action_executed return
 | 
        
           |  |  | 77 |      *
 | 
        
           |  |  | 78 |      * @return \core_external\external_description
 | 
        
           |  |  | 79 |      * @since  Moodle 3.8
 | 
        
           |  |  | 80 |      */
 | 
        
           |  |  | 81 |     public static function action_executed_returns() {
 | 
        
           |  |  | 82 |         return new external_single_structure(
 | 
        
           |  |  | 83 |             array(
 | 
        
           |  |  | 84 |                 'warnings' => new external_warnings(),
 | 
        
           |  |  | 85 |             )
 | 
        
           |  |  | 86 |         );
 | 
        
           |  |  | 87 |     }
 | 
        
           |  |  | 88 |   | 
        
           |  |  | 89 |     /**
 | 
        
           |  |  | 90 |      * Validates access to the prediction and returns it.
 | 
        
           |  |  | 91 |      *
 | 
        
           |  |  | 92 |      * @param int $predictionid
 | 
        
           |  |  | 93 |      * @return array array($model, $prediction, $context)
 | 
        
           |  |  | 94 |      */
 | 
        
           |  |  | 95 |     protected static function validate_prediction($predictionid) {
 | 
        
           |  |  | 96 |   | 
        
           |  |  | 97 |         list($model, $prediction, $context) = \core_analytics\manager::get_prediction($predictionid);
 | 
        
           |  |  | 98 |   | 
        
           |  |  | 99 |         self::validate_context($context);
 | 
        
           |  |  | 100 |   | 
        
           |  |  | 101 |         return array($model, $prediction, $context);
 | 
        
           |  |  | 102 |     }
 | 
        
           |  |  | 103 | }
 |