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.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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   * Representation of a suggested action.
  19   *
  20   * @package   core_analytics
  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 core_analytics;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Representation of a suggested action.
  31   *
  32   * @package   core_analytics
  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  abstract class action {
  37  
  38      /**
  39       * @var  Action type useful.
  40       */
  41      const TYPE_POSITIVE = 'useful';
  42  
  43      /**
  44       * @var  Action type notuseful.
  45       */
  46      const TYPE_NEGATIVE = 'notuseful';
  47  
  48      /**
  49       * @var  Action type neutral.
  50       */
  51      const TYPE_NEUTRAL = 'neutral';
  52  
  53      /**
  54       * @var string
  55       */
  56      protected $actionname = null;
  57  
  58      /**
  59       * @var \moodle_url
  60       */
  61      protected $url = null;
  62  
  63      /**
  64       * @var \renderable
  65       */
  66      protected $actionlink = null;
  67  
  68      /**
  69       * @var string
  70       */
  71      protected $text = null;
  72  
  73      /** @var string Store the action type. */
  74      protected string $type = '';
  75  
  76      /**
  77       * Returns the action name.
  78       *
  79       * @return string
  80       */
  81      public function get_action_name() {
  82          return $this->actionname;
  83      }
  84  
  85      /**
  86       * Returns the url to the action.
  87       *
  88       * @return \moodle_url
  89       */
  90      public function get_url() {
  91          return $this->url;
  92      }
  93  
  94      /**
  95       * Returns the link to the action.
  96       *
  97       * @return \renderable
  98       */
  99      public function get_action_link() {
 100          return $this->actionlink;
 101      }
 102  
 103      /**
 104       * Returns the action text.
 105       * @return string
 106       */
 107      public function get_text() {
 108          return $this->text;
 109      }
 110  
 111      /**
 112       * Sets the type of the action according to its positiveness.
 113       *
 114       * @throws \coding_exception
 115       * @param string|false $type \core_analytics\action::TYPE_POSITIVE, TYPE_NEGATIVE or TYPE_NEUTRAL
 116       */
 117      public function set_type($type = false) {
 118          if (!$type) {
 119              // Any non-standard action specified by a target is considered positive by default because that is what
 120              // they are meant to be.
 121              $type = self::TYPE_POSITIVE;
 122          }
 123  
 124          if ($type !== self::TYPE_POSITIVE && $type !== self::TYPE_NEUTRAL &&
 125                  $type !== self::TYPE_NEGATIVE) {
 126              throw new \coding_exception('The provided type must be ' . self::TYPE_POSITIVE . ', ' . self::TYPE_NEUTRAL .
 127                  ' or ' . self::TYPE_NEGATIVE);
 128          }
 129          $this->type = $type;
 130      }
 131  
 132      /**
 133       * Returns the type of action.
 134       *
 135       * @return string The positiveness of the action (self::TYPE_POSITIVE, self::TYPE_NEGATIVE or self::TYPE_NEUTRAL)
 136       */
 137      public function get_type() {
 138          return $this->type;
 139      }
 140  }