| Total Complexity | 62 |
| Total Lines | 341 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like survey_question often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use survey_question, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class survey_question |
||
| 10 | { |
||
| 11 | /** @var FormValidator */ |
||
| 12 | private $form; |
||
| 13 | public $buttonList = array(); |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Generic part of any survey question: the question field |
||
| 17 | * @param array $surveyData |
||
| 18 | * @param array $formData |
||
| 19 | * |
||
| 20 | * @return FormValidator |
||
| 21 | */ |
||
| 22 | public function createForm($surveyData, $formData) |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Adds submit button |
||
| 130 | * |
||
| 131 | */ |
||
| 132 | public function renderForm() |
||
| 133 | { |
||
| 134 | if (isset($_GET['question_id']) and !empty($_GET['question_id'])) { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Check if survey has answers first before update it, this is because if you update it, the question |
||
| 138 | * options will delete and re-insert in database loosing the iid and question_id to verify the correct answers |
||
| 139 | */ |
||
| 140 | $surveyId = isset($_GET['survey_id']) ? intval($_GET['survey_id']) : 0; |
||
| 141 | $answersChecker = SurveyUtil::checkIfSurveyHasAnswers($surveyId); |
||
| 142 | |||
| 143 | if (!$answersChecker) { |
||
| 144 | $this->buttonList[] = $this->getForm()->addButtonUpdate(get_lang('ModifyQuestionSurvey'), 'save', true); |
||
| 145 | } else { |
||
| 146 | $this->getForm()->addHtml(' |
||
| 147 | <div class="form-group"> |
||
| 148 | <label class="col-sm-2 control-label"></label> |
||
| 149 | <div class="col-sm-8"> |
||
| 150 | <div class="alert alert-info">' . get_lang('YouCantNotEditThisQuestionBecauseAlreadyExistAnswers').'</div> |
||
| 151 | </div> |
||
| 152 | <div class="col-sm-2"></div> |
||
| 153 | </div> |
||
| 154 | '); |
||
| 155 | } |
||
| 156 | } else { |
||
| 157 | $this->buttonList[] = $this->getForm()->addButtonSave(get_lang('CreateQuestionSurvey'), 'save', true); |
||
| 158 | } |
||
| 159 | |||
| 160 | $this->getForm()->addGroup($this->buttonList, 'buttons'); |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @return FormValidator |
||
| 165 | */ |
||
| 166 | public function getForm() |
||
| 167 | { |
||
| 168 | return $this->form; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param FormValidator $form |
||
| 173 | */ |
||
| 174 | public function setForm($form) |
||
| 175 | { |
||
| 176 | $this->form = $form; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param array $formData |
||
| 181 | * |
||
| 182 | * @return mixed |
||
| 183 | */ |
||
| 184 | public function preSave($formData) |
||
| 185 | { |
||
| 186 | $counter = Session::read('answer_count'); |
||
| 187 | $answerList = Session::read('answer_list'); |
||
| 188 | |||
| 189 | if (empty($answerList)) { |
||
| 190 | $answerList = isset($formData['answers']) ? $formData['answers'] : array(); |
||
| 191 | Session::write('answer_list', $answerList); |
||
| 192 | } |
||
| 193 | |||
| 194 | if (isset($_POST['answers'])) { |
||
| 195 | $formData['answers'] = $_POST['answers']; |
||
| 196 | } |
||
| 197 | |||
| 198 | if (empty($counter)) { |
||
| 199 | $counter = count($answerList) - 1; |
||
| 200 | Session::write('answer_count', $counter); |
||
| 201 | } |
||
| 202 | |||
| 203 | // Moving an answer up |
||
| 204 | if (isset($_POST['move_up']) && $_POST['move_up']) { |
||
| 205 | foreach ($_POST['move_up'] as $key => & $value) { |
||
| 206 | $id1 = $key; |
||
| 207 | $content1 = $formData['answers'][$id1]; |
||
| 208 | $id2 = $key - 1; |
||
| 209 | $content2 = $formData['answers'][$id2]; |
||
| 210 | $formData['answers'][$id1] = $content2; |
||
| 211 | $formData['answers'][$id2] = $content1; |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | // Moving an answer down |
||
| 216 | if (isset($_POST['move_down']) && $_POST['move_down']) { |
||
| 217 | foreach ($_POST['move_down'] as $key => & $value) { |
||
| 218 | $id1 = $key; |
||
| 219 | $content1 = $formData['answers'][$id1]; |
||
| 220 | $id2 = $key + 1; |
||
| 221 | $content2 = $formData['answers'][$id2]; |
||
| 222 | $formData['answers'][$id1] = $content2; |
||
| 223 | $formData['answers'][$id2] = $content1; |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * This solution is a little bit strange but I could not find a different solution. |
||
| 229 | */ |
||
| 230 | if (isset($_POST['delete_answer'])) { |
||
| 231 | $deleted = false; |
||
| 232 | foreach ($_POST['delete_answer'] as $key => & $value) { |
||
| 233 | $deleted = $key; |
||
| 234 | $counter--; |
||
| 235 | Session::write('answer_count', $counter); |
||
| 236 | } |
||
| 237 | |||
| 238 | foreach ($formData['answers'] as $key => & $value) { |
||
| 239 | if ($key > $deleted) { |
||
| 240 | $formData['answers'][$key - 1] = $formData['answers'][$key]; |
||
| 241 | unset($formData['answers'][$key]); |
||
| 242 | } |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | // Adding an answer |
||
| 247 | if (isset($_POST['buttons']) && isset($_POST['buttons']['add_answer'])) { |
||
| 248 | $counter++; |
||
| 249 | Session::write('answer_count', $counter); |
||
| 250 | } |
||
| 251 | |||
| 252 | // Removing an answer |
||
| 253 | if (isset($_POST['buttons']) && isset($_POST['buttons']['remove_answer'])) { |
||
| 254 | $counter--; |
||
| 255 | Session::write('answer_count', $counter); |
||
| 256 | foreach ($formData['answers'] as $index => &$data) { |
||
| 257 | if ($index > $counter) { |
||
| 258 | unset($formData['answers'][$index]); |
||
| 259 | } |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | if (!isset($_POST['delete_answer'])) { |
||
| 264 | if (isset($formData['answers'])) { |
||
| 265 | foreach ($formData['answers'] as $index => $data) { |
||
| 266 | if ($index > $counter) { |
||
| 267 | unset($formData['answers'][$index]); |
||
| 268 | } |
||
| 269 | } |
||
| 270 | |||
| 271 | for ($i = 0; $i <= $counter; $i++) { |
||
| 272 | if (!isset($formData['answers'][$i])) { |
||
| 273 | $formData['answers'][$i] = ''; |
||
| 274 | } |
||
| 275 | } |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | $formData['answers'] = isset($formData['answers']) ? $formData['answers'] : []; |
||
| 280 | Session::write('answer_list', $formData['answers']); |
||
| 281 | |||
| 282 | return $formData; |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param array $surveyData |
||
| 287 | * @param array $formData |
||
| 288 | * |
||
| 289 | * @return mixed |
||
| 290 | */ |
||
| 291 | public function save($surveyData, $formData) |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Adds two buttons. One to add an option, one to remove an option |
||
| 313 | * |
||
| 314 | * @param array $data |
||
| 315 | * |
||
| 316 | */ |
||
| 317 | public function addRemoveButtons($data) |
||
| 339 | ); |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @param FormValidator $form |
||
| 344 | * @param array $questionData |
||
| 345 | * @param array $answers |
||
| 346 | */ |
||
| 347 | public function render(FormValidator $form, $questionData = array(), $answers = array()) |
||
| 350 | } |
||
| 351 | } |
||
| 352 | |||
| 353 |