MDL-63050 cachestore_redis: Update hExists to check empty
[moodle.git] / lib / form / modvisible.php
blob8a3fadcea9f0019cc643ec852de50ff2330072f9
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.
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/>.
18 /**
19 * Drop down form element to select visibility in an activity mod update form
21 * Contains HTML class for a drop down element to select visibility in an activity mod update form
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
28 global $CFG;
29 require_once "$CFG->libdir/form/select.php";
31 /**
32 * Drop down form element to select visibility in an activity mod update form
34 * HTML class for a drop down element to select visibility in an activity mod update form
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
41 class MoodleQuickForm_modvisible extends MoodleQuickForm_select{
43 /** @var int activity state: visible=0, visibleoncoursepage=any */
44 const HIDE = 0;
46 /** @var int activity state: visible=1, visibleoncoursepage=1 */
47 const SHOW = 1;
49 /** @var int activity state: visible=1, visibleoncoursepage=0 */
50 const STEALTH = -1;
52 /**
53 * Class constructor
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
60 public function __construct($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
61 parent::__construct($elementName, $elementLabel, null, $attributes);
62 $this->_type = 'modvisible';
65 /**
66 * Old syntax of class constructor. Deprecated in PHP7.
68 * @deprecated since Moodle 3.1
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);
75 /**
76 * Called by HTML_QuickForm whenever form event is made on this element
78 * @param string $event Name of event
79 * @param mixed $arg event arguments
80 * @param object $caller calling object
81 * @return bool
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');
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');
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;
116 if (null === $value) {
117 if ($caller->isSubmitted()) {
118 break;
120 $value = $this->_findValue($caller->_defaultValues);
121 if (!empty($value) && isset($caller->_defaultValues[$name.'oncoursepage']) &&
122 !$caller->_defaultValues[$name.'oncoursepage']) {
123 $value = self::STEALTH;
126 if ($value !== null) {
127 $this->setSelected($value);
129 return true;
132 return parent::onQuickFormEvent($event, $arg, $caller);
136 * As usual, to get the group's value we access its elements and call
137 * their exportValue() methods
139 * @param array $submitvalues submitted values
140 * @param bool $assoc if true the retured value is associated array
141 * @return mixed
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);