Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 402] [Versions 311 and 403]

   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   * Block target.
  19   *
  20   * @package    tool_usertours
  21   * @copyright  2016 Andrew Nicols <andrew@nicols.co.uk>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace tool_usertours\local\target;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use tool_usertours\step;
  30  
  31  /**
  32   * Block target.
  33   *
  34   * @copyright  2016 Andrew Nicols <andrew@nicols.co.uk>
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class block extends base {
  38  
  39      /**
  40       * Convert the target value to a valid CSS selector for use in the
  41       * output configuration.
  42       *
  43       * @return string
  44       */
  45      public function convert_to_css() {
  46          // The block has the following CSS class selector style:
  47          // .block-region .block_[name] .
  48          return sprintf('.block-region .block_%s', $this->step->get_targetvalue());
  49      }
  50  
  51      /**
  52       * Convert the step target to a friendly name for use in the UI.
  53       *
  54       * @return string
  55       */
  56      public function get_displayname() {
  57          return get_string('block_named', 'tool_usertours', $this->get_block_name());
  58      }
  59  
  60      /**
  61       * Get the translated name of the block.
  62       *
  63       * @return string
  64       */
  65      protected function get_block_name() {
  66          return get_string('pluginname', self::get_frankenstyle($this->step->get_targetvalue()));
  67      }
  68  
  69      /**
  70       * Get the frankenstyle name of the block.
  71       *
  72       * @param   string  $block  The block name.
  73       * @return                  The frankenstyle block name.
  74       */
  75      protected static function get_frankenstyle($block) {
  76          return sprintf('block_%s', $block);
  77      }
  78  
  79      /**
  80       * Add the target type configuration to the form.
  81       *
  82       * @param   MoodleQuickForm $mform      The form to add configuration to.
  83       * @return  $this
  84       */
  85      public static function add_config_to_form(\MoodleQuickForm $mform) {
  86          global $PAGE;
  87  
  88          $blocks = [];
  89          foreach ($PAGE->blocks->get_installed_blocks() as $block) {
  90              $blocks[$block->name] = get_string('pluginname', 'block_' . $block->name);
  91          }
  92  
  93          \core_collator::asort($blocks);
  94  
  95          $mform->addElement('select', 'targetvalue_block', get_string('block', 'tool_usertours'), $blocks);
  96      }
  97  
  98      /**
  99       * Add the disabledIf values.
 100       *
 101       * @param   MoodleQuickForm $mform      The form to add configuration to.
 102       */
 103      public static function add_disabled_constraints_to_form(\MoodleQuickForm $mform) {
 104          $mform->hideIf('targetvalue_block', 'targettype', 'noteq',
 105                  \tool_usertours\target::get_target_constant_for_class(get_class()));
 106      }
 107  
 108      /**
 109       * Prepare data to submit to the form.
 110       *
 111       * @param   object          $data       The data being passed to the form
 112       */
 113      public function prepare_data_for_form($data) {
 114          $data->targetvalue_block = $this->step->get_targetvalue();
 115      }
 116  
 117      /**
 118       * Fetch the targetvalue from the form for this target type.
 119       *
 120       * @param   stdClass        $data       The data submitted in the form
 121       * @return  string
 122       */
 123      public function get_value_from_form($data) {
 124          return $data->targetvalue_block;
 125      }
 126  }