Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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   * Base class to make it easier to implement actions that are menuable_actions.
  19   *
  20   * @package   core_question
  21   * @copyright 2019 Tim Hunt
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_question\local\bank;
  26  
  27  /**
  28   * Base class to make it easier to implement actions that are menuable_actions.
  29   *
  30   * Use this class if your action is simple (defined by just a URL, label and icon).
  31   * If your action is not simple enough to fit into the pattern that this
  32   * class implements, then you will have to implement the menuable_action
  33   * interface yourself.
  34   *
  35   * @copyright 2019 Tim Hunt
  36   * @author    2021 Safat Shahin <safatshahin@catalyst-au.net>
  37   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  abstract class question_action_base extends view_component {
  40  
  41      /**
  42       * Get the information required to display this action either as a menu item or a separate action column.
  43       *
  44       * For most actions, it should be sufficient to override just this method. {@see get_action_menu_link()} is the public interface
  45       * and handles building a renderable menu link object from this data.
  46       *
  47       * If this action cannot apply to this question (e.g. because the user does not have
  48       * permission, then return [null, null, null].
  49       *
  50       * @param \stdClass $question the row from the $question table, augmented with extra information.
  51       * @return array with three elements.
  52       *      $url - the URL to perform the action.
  53       *      $icon - the icon for this action. E.g. 't/delete'.
  54       *      $label - text label to display in the UI (either in the menu, or as a tool-tip on the icon)
  55       */
  56      protected function get_url_icon_and_label(\stdClass $question): array {
  57          return [null, null, null];
  58      }
  59  
  60      /**
  61       * Return the action menu link for this action on the supplied question.
  62       *
  63       * For most actions, you will just need to override {@see get_url_icon_and_label()}. You only need to override
  64       * this method if you need to pass additional attributes to {@see action_menu_link_secondary}, or use a different class to
  65       * render the link.
  66       *
  67       * @param \stdClass $question
  68       * @return \action_menu_link|null
  69       */
  70      public function get_action_menu_link(\stdClass $question): ?\action_menu_link {
  71          [$url, $icon, $label] = $this->get_url_icon_and_label($question);
  72          if (!$url) {
  73              return null;
  74          }
  75          return new \action_menu_link_secondary($url, new \pix_icon($icon, ''), $label);
  76      }
  77  }