Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.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  /**
  19   * Drop down form element to select visibility in an activity mod update form
  20   *
  21   * Contains HTML class for a drop down element to select visibility in an activity mod update form
  22   *
  23   * @package   core_form
  24   * @copyright 2006 Jamie Pratt <me@jamiep.org>
  25   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  global $CFG;
  29  require_once "$CFG->libdir/form/select.php";
  30  
  31  /**
  32   * Drop down form element to select visibility in an activity mod update form
  33   *
  34   * HTML class for a drop down element to select visibility in an activity mod update form
  35   *
  36   * @package   core_form
  37   * @category  form
  38   * @copyright 2006 Jamie Pratt <me@jamiep.org>
  39   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class MoodleQuickForm_modvisible extends MoodleQuickForm_select{
  42  
  43      /** @var int activity state: visible=0, visibleoncoursepage=any */
  44      const HIDE = 0;
  45  
  46      /** @var int activity state: visible=1, visibleoncoursepage=1 */
  47      const SHOW = 1;
  48  
  49      /** @var int activity state: visible=1, visibleoncoursepage=0 */
  50      const STEALTH = -1;
  51  
  52      /**
  53       * Class constructor
  54       *
  55       * @param string $elementName Select name attribute
  56       * @param mixed $elementLabel Label(s) for the select
  57       * @param mixed $attributes Either a typical HTML attribute string or an associative array
  58       * @param array $options ignored
  59       */
  60      public function __construct($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
  61          parent::__construct($elementName, $elementLabel, null, $attributes);
  62          $this->_type = 'modvisible';
  63      }
  64  
  65      /**
  66       * Old syntax of class constructor. Deprecated in PHP7.
  67       *
  68       * @deprecated since Moodle 3.1
  69       */
  70      public function MoodleQuickForm_modvisible($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
  71          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  72          self::__construct($elementName, $elementLabel, $attributes, $options);
  73      }
  74  
  75      /**
  76       * Called by HTML_QuickForm whenever form event is made on this element
  77       *
  78       * @param string $event Name of event
  79       * @param mixed $arg event arguments
  80       * @param object $caller calling object
  81       * @return bool
  82       */
  83      public function onQuickFormEvent($event, $arg, &$caller) {
  84          switch ($event) {
  85              case 'createElement':
  86                  $options = is_array($arg[3]) ? $arg[3] : [];
  87                  $sectionvisible = array_key_exists('sectionvisible', $options) ? $options['sectionvisible'] : 1;
  88                  $cm = !empty($options['cm']) ? cm_info::create($options['cm']) : null;
  89                  $choices = array();
  90                  if (!$sectionvisible) {
  91                      // If section is not visible the activity is hidden by default but it can also be made available.
  92                      $choices[self::HIDE] = get_string('hidefromstudents');
  93                      if (!$cm || $cm->has_view()) {
  94                          $choices[self::SHOW] = get_string('hideoncoursepage');
  95                      }
  96                  } else {
  97                      $choices[self::SHOW] = get_string('showoncoursepage');
  98                      $choices[self::HIDE] = get_string('hidefromstudents');
  99                      if (!empty($options['allowstealth']) && (!$cm || $cm->has_view())) {
 100                          // If allowed in this course/section, add a third visibility option
 101                          // "Available but not displayed on course page".
 102                          $choices[self::STEALTH] = get_string('hideoncoursepage');
 103                      }
 104                  }
 105                  $this->load($choices);
 106                  break;
 107              case 'updateValue':
 108                  // Given two bool values of 'visible' and 'visibleoncoursepage' convert to a single
 109                  // three-state value (show, hide, hide-on-course-page).
 110                  $name = $this->getName();
 111                  $value = $this->_findValue($caller->_constantValues);
 112                  if (!empty($value) && isset($caller->_constantValues[$name.'oncoursepage']) &&
 113                          !$caller->_constantValues[$name.'oncoursepage']) {
 114                      $value = self::STEALTH;
 115                  }
 116                  if (null === $value) {
 117                      if ($caller->isSubmitted()) {
 118                          break;
 119                      }
 120                      $value = $this->_findValue($caller->_defaultValues);
 121                      if (!empty($value) && isset($caller->_defaultValues[$name.'oncoursepage']) &&
 122                              !$caller->_defaultValues[$name.'oncoursepage']) {
 123                          $value = self::STEALTH;
 124                      }
 125                  }
 126                  if ($value !== null) {
 127                      $this->setSelected($value);
 128                  }
 129                  return true;
 130  
 131          }
 132          return parent::onQuickFormEvent($event, $arg, $caller);
 133      }
 134  
 135      /**
 136       * As usual, to get the group's value we access its elements and call
 137       * their exportValue() methods
 138       *
 139       * @param array $submitvalues submitted values
 140       * @param bool $assoc if true the retured value is associated array
 141       * @return mixed
 142       */
 143      public function exportValue(&$submitvalues, $assoc = false) {
 144          if ($assoc) {
 145              $value = parent::exportValue($submitvalues, $assoc);
 146              $key = key($value);
 147              $v = $value[$key];
 148              // Convert three-state dropdown value (show, hide, hide-on-course-page) into the array of two bool values:
 149              // array('visible' => x, 'visibleoncoursepage' => y).
 150              return array($key => ($v == self::HIDE ? 0 : 1),
 151                  $key . 'oncoursepage' => ($v == self::STEALTH ? 0 : 1));
 152          } else {
 153              return parent::exportValue($submitvalues, $assoc);
 154          }
 155      }
 156  }