2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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
24 * @copyright 2006 Jamie Pratt <me@jamiep.org>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 require_once "$CFG->libdir/form/select.php";
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
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 */
46 /** @var int activity state: visible=1, visibleoncoursepage=1 */
49 /** @var int activity state: visible=1, visibleoncoursepage=0 */
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';
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);
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
83 public function onQuickFormEvent($event, $arg, &$caller) {
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;
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');
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);
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()) {
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);
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
143 public function exportValue(&$submitvalues, $assoc = false) {
145 $value = parent
::exportValue($submitvalues, $assoc);
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));
153 return parent
::exportValue($submitvalues, $assoc);