Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   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  /**
  18   * Output helper to export actions for rendering.
  19   *
  20   * @package   report_insights
  21   * @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace report_insights\output;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Output helper to export actions for rendering.
  31   *
  32   * @package   report_insights
  33   * @copyright 2019 David Monllao {@link http://www.davidmonllao.com}
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class actions_exporter {
  37  
  38      /**
  39       * Add the prediction actions renderable.
  40       *
  41       * @param  \core_analytics\local\target\base $target
  42       * @param  \renderer_base                    $output
  43       * @param  \core_analytics\prediction        $prediction
  44       * @param  bool                              $includedetailsaction
  45       * @return \stdClass|false
  46       */
  47      public static function add_prediction_actions(\core_analytics\local\target\base $target, \renderer_base $output,
  48              \core_analytics\prediction $prediction, bool $includedetailsaction = false) {
  49  
  50          $actions = $target->prediction_actions($prediction, $includedetailsaction);
  51          if ($actions) {
  52              $actionsmenu = new \action_menu();
  53  
  54              // Add all actions defined by the target.
  55              foreach ($actions as $action) {
  56                  $actionsmenu->add_primary_action($action->get_action_link());
  57              }
  58  
  59              return $actionsmenu->export_for_template($output);
  60          }
  61  
  62          return false;
  63      }
  64  
  65      /**
  66       * Add bulk actions renderables.
  67       *
  68       * Note that if you are planning to render the bulk actions, the provided predictions must share the same predicted value.
  69       *
  70       * @param  \core_analytics\local\target\base $target
  71       * @param  \renderer_base                    $output
  72       * @param  \core_analytics\prediction[]      $predictions   Bulk actions for this set of predictions.
  73       * @param  \context                          $context       The context of these predictions.
  74       * @return \stdClass[]|false
  75       */
  76      public static function add_bulk_actions(\core_analytics\local\target\base $target, \renderer_base $output, array $predictions,
  77              \context $context) {
  78          global $USER;
  79  
  80          $bulkactions = $target->bulk_actions($predictions);
  81  
  82          if ($context->contextlevel === CONTEXT_USER) {
  83              // Remove useful / notuseful if the current user is not part of the users who receive the insight (e.g. a site manager
  84              // who looks at the generated insights for a particular user).
  85  
  86              $insightusers = $target->get_insights_users($context);
  87              if (empty($insightusers[$USER->id])) {
  88                  foreach ($bulkactions as $key => $action) {
  89                      if ($action->get_action_name() === 'useful' || $action->get_action_name() === 'notuseful') {
  90                          unset($bulkactions[$key]);
  91                      }
  92                  }
  93              }
  94  
  95          }
  96  
  97          if (!$bulkactions) {
  98              return false;
  99          }
 100  
 101          $actionsmenu = [];
 102  
 103          // All the predictions share a common predicted value.
 104          $predictionvalue = reset($predictions)->get_prediction_data()->prediction;
 105  
 106          // Add all actions defined by the target.
 107          foreach ($bulkactions as $action) {
 108              $action->get_action_link()->set_attribute('data-togglegroup', 'insight-bulk-action-' . $predictionvalue);
 109              $actionsmenu[] = $action->get_action_link()->export_for_template($output);
 110          }
 111  
 112          if (empty($actionsmenu)) {
 113              return false;
 114          }
 115  
 116          return $actionsmenu;
 117      }
 118  
 119  }
 120