Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
53.19% |
25 / 47 |
|
20.00% |
1 / 5 |
CRAP | |
0.00% |
0 / 1 |
| tao_helpers_form_xhtml_Form | |
53.19% |
25 / 47 |
|
20.00% |
1 / 5 |
61.02 | |
0.00% |
0 / 1 |
| getValues | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
42 | |||
| evaluate | |
50.00% |
4 / 8 |
|
0.00% |
0 / 1 |
4.12 | |||
| render | |
80.00% |
16 / 20 |
|
0.00% |
0 / 1 |
5.20 | |||
| validate | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| preValidate | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program 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 this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
| 19 | * (under the project TAO-TRANSFER); |
| 20 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 21 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 22 | * Copyright (c) 2022 (original work) Open Assessment Technologies SA; |
| 23 | */ |
| 24 | |
| 25 | use oat\oatbox\log\LoggerAwareTrait; |
| 26 | |
| 27 | /** |
| 28 | * Short description of class tao_helpers_form_xhtml_Form |
| 29 | * |
| 30 | * @access public |
| 31 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 32 | * @package tao |
| 33 | |
| 34 | */ |
| 35 | // phpcs:ignore |
| 36 | class tao_helpers_form_xhtml_Form extends tao_helpers_form_Form |
| 37 | { |
| 38 | use LoggerAwareTrait; |
| 39 | |
| 40 | /** |
| 41 | * Short description of method getValues |
| 42 | * |
| 43 | * @access public |
| 44 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 45 | * @param string $groupName |
| 46 | * @param array $filterProperties List of properties which values are unneeded and must be filtered |
| 47 | * @return array |
| 48 | */ |
| 49 | public function getValues($groupName = '') |
| 50 | { |
| 51 | $returnValue = []; |
| 52 | |
| 53 | foreach ($this->elements as $element) { |
| 54 | if (!empty($this->systemElements) && in_array($element->getName(), $this->systemElements)) { |
| 55 | continue; |
| 56 | } |
| 57 | if ( |
| 58 | empty($groupName) |
| 59 | || in_array($element->getName(), $this->groups[$groupName]['elements']) |
| 60 | ) { |
| 61 | $returnValue[tao_helpers_Uri::decode($element->getName())] = $element->getEvaluatedValue(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | |
| 66 | return (array) $returnValue; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Evaluate the form |
| 71 | */ |
| 72 | public function evaluate() |
| 73 | { |
| 74 | $this->initElements(); |
| 75 | |
| 76 | if (!isset($_POST[$this->name . '_sent'])) { |
| 77 | $this->preValidate(); |
| 78 | |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | $this->submited = true; |
| 83 | |
| 84 | // Set posted values |
| 85 | foreach ($this->elements as $id => $element) { |
| 86 | $this->elements[$id]->feed(); |
| 87 | } |
| 88 | |
| 89 | $this->validate(); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Short description of method render |
| 94 | * |
| 95 | * @access public |
| 96 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 97 | * @return string |
| 98 | */ |
| 99 | public function render() |
| 100 | { |
| 101 | $returnValue = ''; |
| 102 | |
| 103 | $requestUri = $_SERVER['REQUEST_URI'] ?? ''; |
| 104 | $action = strpos($requestUri, '?') > 0 |
| 105 | ? substr($requestUri, 0, strpos($requestUri, '?')) |
| 106 | : $requestUri; |
| 107 | |
| 108 | // Defensive code, prevent double leading slashes issue. |
| 109 | if (strpos($action, '//') === 0) { |
| 110 | $action = substr($action, 1); |
| 111 | } |
| 112 | |
| 113 | $returnValue .= "<div class='xhtml_form'>\n"; |
| 114 | |
| 115 | $returnValue .= "<form method='post' id='{$this->name}' name='{$this->name}' action='$action' "; |
| 116 | if ($this->hasFileUpload()) { |
| 117 | $returnValue .= "enctype='multipart/form-data' "; |
| 118 | } |
| 119 | |
| 120 | $returnValue .= ">\n"; |
| 121 | |
| 122 | $returnValue .= "<input type='hidden' class='global' name='{$this->name}_sent' value='1' />\n"; |
| 123 | |
| 124 | if (!empty($this->error)) { |
| 125 | $returnValue .= '<div class="xhtml_form_error">' . $this->error . '</div>'; |
| 126 | } |
| 127 | |
| 128 | $returnValue .= $this->renderElements(); |
| 129 | |
| 130 | $returnValue .= $this->renderActions(); |
| 131 | |
| 132 | $returnValue .= "</form>\n"; |
| 133 | $returnValue .= "</div>\n"; |
| 134 | |
| 135 | |
| 136 | |
| 137 | return $returnValue; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Validate the form |
| 142 | * |
| 143 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 144 | * @return bool |
| 145 | */ |
| 146 | protected function validate() |
| 147 | { |
| 148 | $returnValue = true; |
| 149 | $this->valid = true; |
| 150 | |
| 151 | foreach ($this->elements as $element) { |
| 152 | if (!$element->validate()) { |
| 153 | $this->valid = false; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | return $returnValue; |
| 158 | } |
| 159 | |
| 160 | private function preValidate(): void |
| 161 | { |
| 162 | $this->valid = true; |
| 163 | |
| 164 | foreach ($this->elements as $element) { |
| 165 | $element->preValidate(); |
| 166 | |
| 167 | if (!$element->isValid()) { |
| 168 | $this->valid = false; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | } |