chamilo /
chamilo-lms
| 1 | <?php |
||
| 2 | |||
| 3 | /* For licensing terms, see /license.txt */ |
||
| 4 | |||
| 5 | use Chamilo\CourseBundle\Entity\CQuizAnswer; |
||
| 6 | use ChamiloSession as Session; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * Class UniqueAnswer. |
||
| 10 | * |
||
| 11 | * This class allows to instantiate an object of type UNIQUE_ANSWER |
||
| 12 | * (MULTIPLE CHOICE, UNIQUE ANSWER), |
||
| 13 | * extending the class question |
||
| 14 | * |
||
| 15 | * @author Eric Marguin |
||
| 16 | * @author Julio Montoya |
||
| 17 | */ |
||
| 18 | class UniqueAnswer extends Question |
||
| 19 | { |
||
| 20 | public $typePicture = 'mcua.png'; |
||
| 21 | public $explanationLangVar = 'UniqueSelect'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Constructor. |
||
| 25 | */ |
||
| 26 | public function __construct() |
||
| 27 | { |
||
| 28 | parent::__construct(); |
||
| 29 | $this->type = UNIQUE_ANSWER; |
||
| 30 | $this->isContent = $this->getIsContent(); |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * {@inheritdoc} |
||
| 35 | */ |
||
| 36 | public function createAnswersForm($form) |
||
| 37 | { |
||
| 38 | // Getting the exercise list |
||
| 39 | /** @var Exercise $obj_ex */ |
||
| 40 | $obj_ex = Session::read('objExercise'); |
||
| 41 | |||
| 42 | $editor_config = [ |
||
| 43 | 'ToolbarSet' => 'TestProposedAnswer', |
||
| 44 | 'Width' => '100%', |
||
| 45 | 'Height' => '125', |
||
| 46 | ]; |
||
| 47 | |||
| 48 | //this line defines how many questions by default appear when creating a choice question |
||
| 49 | // The previous default value was 2. See task #1759. |
||
| 50 | $nb_answers = isset($_POST['nb_answers']) ? (int) $_POST['nb_answers'] : 4; |
||
| 51 | $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0)); |
||
| 52 | |||
| 53 | $feedback_title = ''; |
||
| 54 | switch ($obj_ex->getFeedbackType()) { |
||
| 55 | case EXERCISE_FEEDBACK_TYPE_DIRECT: |
||
| 56 | // Scenario |
||
| 57 | $comment_title = '<th width="20%">'.get_lang('Comment').'</th>'; |
||
| 58 | $feedback_title = '<th width="20%">'.get_lang('Scenario').'</th>'; |
||
| 59 | break; |
||
| 60 | case EXERCISE_FEEDBACK_TYPE_POPUP: |
||
| 61 | $comment_title = '<th width="20%">'.get_lang('Comment').'</th>'; |
||
| 62 | break; |
||
| 63 | default: |
||
| 64 | $comment_title = '<th width="40%">'.get_lang('Comment').'</th>'; |
||
| 65 | break; |
||
| 66 | } |
||
| 67 | |||
| 68 | $html = '<table class="table table-striped table-hover"> |
||
| 69 | <thead> |
||
| 70 | <tr style="text-align: center;"> |
||
| 71 | <th width="5%">'.get_lang('Number').'</th> |
||
| 72 | <th width="5%"> '.get_lang('True').'</th> |
||
| 73 | <th width="40%">'.get_lang('Answer').'</th> |
||
| 74 | '.$comment_title.' |
||
| 75 | '.$feedback_title.' |
||
| 76 | <th width="10%">'.get_lang('Weighting').'</th> |
||
| 77 | </tr> |
||
| 78 | </thead> |
||
| 79 | <tbody>'; |
||
| 80 | |||
| 81 | $form->addHeader(get_lang('Answers')); |
||
| 82 | $form->addHtml($html); |
||
| 83 | |||
| 84 | $defaults = []; |
||
| 85 | |||
| 86 | $correct = 0; |
||
| 87 | if (!empty($this->iid)) { |
||
| 88 | $answer = new Answer($this->iid); |
||
| 89 | $answer->read(); |
||
| 90 | if ($answer->nbrAnswers > 0 && !$form->isSubmitted()) { |
||
| 91 | $nb_answers = $answer->nbrAnswers; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | $form->addElement('hidden', 'nb_answers'); |
||
| 95 | |||
| 96 | $obj_ex->setQuestionList(true); |
||
| 97 | $question_list = $obj_ex->getQuestionList(); |
||
| 98 | $select_question = []; |
||
| 99 | $select_question[0] = get_lang('SelectTargetQuestion'); |
||
| 100 | if (is_array($question_list)) { |
||
| 101 | foreach ($question_list as $key => $questionid) { |
||
| 102 | //To avoid warning messages |
||
| 103 | if (!is_numeric($questionid)) { |
||
| 104 | continue; |
||
| 105 | } |
||
| 106 | $question = Question::read($questionid); |
||
| 107 | $questionTitle = strip_tags($question->selectTitle()); |
||
| 108 | $select_question[$questionid] = "Q$key: $questionTitle"; |
||
| 109 | } |
||
| 110 | } |
||
| 111 | $select_question[-1] = get_lang('ExitTest'); |
||
| 112 | |||
| 113 | $list = new LearnpathList(api_get_user_id()); |
||
| 114 | $flat_list = $list->get_flat_list(); |
||
| 115 | $select_lp_id = []; |
||
| 116 | $select_lp_id[0] = get_lang('SelectTargetLP'); |
||
| 117 | |||
| 118 | foreach ($flat_list as $id => $details) { |
||
| 119 | $select_lp_id[$id] = cut($details['lp_name'], 20); |
||
| 120 | } |
||
| 121 | |||
| 122 | $temp_scenario = []; |
||
| 123 | if ($nb_answers < 1) { |
||
| 124 | $nb_answers = 1; |
||
| 125 | echo Display::return_message( |
||
| 126 | get_lang('YouHaveToCreateAtLeastOneAnswer') |
||
| 127 | ); |
||
| 128 | } |
||
| 129 | |||
| 130 | for ($i = 1; $i <= $nb_answers; $i++) { |
||
| 131 | $form->addHtml('<tr>'); |
||
| 132 | if (isset($answer) && is_object($answer)) { |
||
| 133 | if (isset($answer->correct[$i]) && $answer->correct[$i]) { |
||
| 134 | $correct = $i; |
||
| 135 | } |
||
| 136 | $defaults['answer['.$i.']'] = isset($answer->answer[$i]) ? $answer->answer[$i] : ''; |
||
| 137 | $defaults['comment['.$i.']'] = isset($answer->comment[$i]) ? $answer->comment[$i] : ''; |
||
| 138 | $defaults['weighting['.$i.']'] = isset($answer->weighting[$i]) ? float_format($answer->weighting[$i], 1) : 0; |
||
| 139 | $item_list = []; |
||
| 140 | if (isset($answer->destination[$i])) { |
||
| 141 | $item_list = explode('@@', $answer->destination[$i]); |
||
| 142 | } |
||
| 143 | $try = isset($item_list[0]) ? $item_list[0] : ''; |
||
| 144 | $lp = isset($item_list[1]) ? $item_list[1] : ''; |
||
| 145 | $list_dest = isset($item_list[2]) ? $item_list[2] : ''; |
||
| 146 | $url = isset($item_list[3]) ? $item_list[3] : ''; |
||
| 147 | |||
| 148 | if ($try == 0) { |
||
| 149 | $try_result = 0; |
||
| 150 | } else { |
||
| 151 | $try_result = 1; |
||
| 152 | } |
||
| 153 | if ($url == 0) { |
||
| 154 | $url_result = ''; |
||
| 155 | } else { |
||
| 156 | $url_result = $url; |
||
| 157 | } |
||
| 158 | |||
| 159 | $temp_scenario['url'.$i] = $url_result; |
||
| 160 | $temp_scenario['try'.$i] = $try_result; |
||
| 161 | $temp_scenario['lp'.$i] = $lp; |
||
| 162 | $temp_scenario['destination'.$i] = $list_dest; |
||
| 163 | } else { |
||
| 164 | $defaults['answer[1]'] = get_lang('DefaultUniqueAnswer1'); |
||
| 165 | $defaults['weighting[1]'] = 10; |
||
| 166 | $defaults['answer[2]'] = get_lang('DefaultUniqueAnswer2'); |
||
| 167 | $defaults['weighting[2]'] = 0; |
||
| 168 | $temp_scenario['destination'.$i] = ['0']; |
||
| 169 | $temp_scenario['lp'.$i] = ['0']; |
||
| 170 | } |
||
| 171 | $defaults['scenario'] = $temp_scenario; |
||
| 172 | |||
| 173 | $renderer = $form->defaultRenderer(); |
||
| 174 | |||
| 175 | $renderer->setElementTemplate( |
||
| 176 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
| 177 | 'correct' |
||
| 178 | ); |
||
| 179 | $renderer->setElementTemplate( |
||
| 180 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
| 181 | 'counter['.$i.']' |
||
| 182 | ); |
||
| 183 | $renderer->setElementTemplate( |
||
| 184 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
| 185 | 'answer['.$i.']' |
||
| 186 | ); |
||
| 187 | $renderer->setElementTemplate( |
||
| 188 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
| 189 | 'comment['.$i.']' |
||
| 190 | ); |
||
| 191 | $renderer->setElementTemplate( |
||
| 192 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
| 193 | 'weighting['.$i.']' |
||
| 194 | ); |
||
| 195 | |||
| 196 | $answer_number = $form->addElement( |
||
| 197 | 'text', |
||
| 198 | 'counter['.$i.']', |
||
| 199 | null, |
||
| 200 | ' value = "'.$i.'"' |
||
| 201 | ); |
||
| 202 | $answer_number->freeze(); |
||
| 203 | $form->addElement( |
||
| 204 | 'radio', |
||
| 205 | 'correct', |
||
| 206 | null, |
||
| 207 | null, |
||
| 208 | $i, |
||
| 209 | 'class="checkbox"' |
||
| 210 | ); |
||
| 211 | |||
| 212 | $form->addHtmlEditor('answer['.$i.']', null, null, false, $editor_config); |
||
| 213 | |||
| 214 | $form->addRule( |
||
| 215 | 'answer['.$i.']', |
||
| 216 | get_lang('ThisFieldIsRequired'), |
||
| 217 | 'required' |
||
| 218 | ); |
||
| 219 | |||
| 220 | switch ($obj_ex->getFeedbackType()) { |
||
| 221 | case EXERCISE_FEEDBACK_TYPE_DIRECT: |
||
| 222 | $this->setDirectOptions($i, $form, $renderer, $select_lp_id, $select_question); |
||
| 223 | break; |
||
| 224 | case EXERCISE_FEEDBACK_TYPE_POPUP: |
||
| 225 | default: |
||
| 226 | $form->addHtmlEditor('comment['.$i.']', null, null, false, $editor_config); |
||
| 227 | break; |
||
| 228 | } |
||
| 229 | $form->addText('weighting['.$i.']', null, null, ['value' => '0']); |
||
| 230 | $form->addHtml('</tr>'); |
||
| 231 | } |
||
| 232 | |||
| 233 | $form->addHtml('</tbody>'); |
||
| 234 | $form->addHtml('</table>'); |
||
| 235 | |||
| 236 | global $text; |
||
| 237 | $buttonGroup = []; |
||
| 238 | |||
| 239 | if (true === $obj_ex->edit_exercise_in_lp || (empty($this->exerciseList) && empty($obj_ex->iid))) { |
||
| 240 | //setting the save button here and not in the question class.php |
||
| 241 | $buttonGroup[] = $form->addButtonDelete(get_lang('LessAnswer'), 'lessAnswers', true); |
||
| 242 | $buttonGroup[] = $form->addButtonCreate(get_lang('PlusAnswer'), 'moreAnswers', true); |
||
| 243 | $buttonGroup[] = $form->addButton( |
||
| 244 | 'submitQuestion', |
||
| 245 | $text, |
||
| 246 | 'check', |
||
| 247 | 'primary', |
||
| 248 | 'default', |
||
| 249 | null, |
||
| 250 | ['id' => 'submit-question'], |
||
| 251 | true |
||
| 252 | ); |
||
| 253 | $form->addGroup($buttonGroup); |
||
| 254 | } |
||
| 255 | |||
| 256 | // We check the first radio button to be sure a radio button will be check |
||
| 257 | if ($correct == 0) { |
||
| 258 | $correct = 1; |
||
| 259 | } |
||
| 260 | |||
| 261 | if (isset($_POST) && isset($_POST['correct'])) { |
||
| 262 | $correct = (int) $_POST['correct']; |
||
| 263 | } |
||
| 264 | |||
| 265 | $defaults['correct'] = $correct; |
||
| 266 | |||
| 267 | if (!empty($this->iid)) { |
||
| 268 | $form->setDefaults($defaults); |
||
| 269 | } else { |
||
| 270 | if ($this->isContent == 1) { |
||
| 271 | // Default sample content. |
||
| 272 | $form->setDefaults($defaults); |
||
| 273 | } else { |
||
| 274 | $correct = 1; |
||
| 275 | if (isset($_POST) && isset($_POST['correct'])) { |
||
| 276 | $correct = (int) $_POST['correct']; |
||
| 277 | } |
||
| 278 | |||
| 279 | $form->setDefaults(['correct' => $correct]); |
||
| 280 | } |
||
| 281 | } |
||
| 282 | $form->setConstants(['nb_answers' => $nb_answers]); |
||
| 283 | } |
||
| 284 | |||
| 285 | public function setDirectOptions($i, FormValidator $form, $renderer, $select_lp_id, $select_question) |
||
| 286 | { |
||
| 287 | $editor_config = [ |
||
| 288 | 'ToolbarSet' => 'TestProposedAnswer', |
||
| 289 | 'Width' => '100%', |
||
| 290 | 'Height' => '125', |
||
| 291 | ]; |
||
| 292 | |||
| 293 | $form->addHtmlEditor( |
||
| 294 | 'comment['.$i.']', |
||
| 295 | null, |
||
| 296 | null, |
||
| 297 | false, |
||
| 298 | $editor_config |
||
| 299 | ); |
||
| 300 | // Direct feedback |
||
| 301 | //Adding extra feedback fields |
||
| 302 | $group = []; |
||
| 303 | $group['try'.$i] = $form->createElement( |
||
| 304 | 'checkbox', |
||
| 305 | 'try'.$i, |
||
| 306 | null, |
||
| 307 | get_lang('TryAgain') |
||
| 308 | ); |
||
| 309 | $group['lp'.$i] = $form->createElement( |
||
| 310 | 'select', |
||
| 311 | 'lp'.$i, |
||
| 312 | get_lang('SeeTheory').': ', |
||
| 313 | $select_lp_id |
||
| 314 | ); |
||
| 315 | $group['destination'.$i] = $form->createElement( |
||
| 316 | 'select', |
||
| 317 | 'destination'.$i, |
||
| 318 | get_lang('GoToQuestion').': ', |
||
| 319 | $select_question |
||
| 320 | ); |
||
| 321 | $group['url'.$i] = $form->createElement( |
||
| 322 | 'text', |
||
| 323 | 'url'.$i, |
||
| 324 | get_lang('Other').': ', |
||
| 325 | [ |
||
| 326 | 'class' => 'col-md-2', |
||
| 327 | 'placeholder' => get_lang('Other'), |
||
| 328 | ] |
||
| 329 | ); |
||
| 330 | $form->addGroup($group, 'scenario'); |
||
| 331 | |||
| 332 | $renderer->setElementTemplate( |
||
| 333 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}', |
||
| 334 | 'scenario' |
||
| 335 | ); |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Validate question answers before saving. |
||
| 340 | * |
||
| 341 | * @return bool|string True if valid, error message if invalid. |
||
| 342 | */ |
||
| 343 | public function validateAnswers($form) |
||
| 344 | { |
||
| 345 | $correct = $form->getSubmitValue('correct'); |
||
| 346 | $nb_answers = $form->getSubmitValue('nb_answers'); |
||
| 347 | $hasCorrectAnswer = false; |
||
| 348 | $hasPositiveScore = false; |
||
| 349 | $errors = []; |
||
| 350 | $error_fields = []; |
||
| 351 | |||
| 352 | for ($i = 1; $i <= $nb_answers; $i++) { |
||
| 353 | $answer = trim($form->getSubmitValue("answer[$i]") ?? ''); |
||
| 354 | $weighting = trim($form->getSubmitValue("weighting[$i]") ?? ''); |
||
| 355 | $isCorrect = ($correct == $i); |
||
| 356 | if (empty($answer)) { |
||
| 357 | $errors[] = sprintf(get_lang('NoAnswerCanBeEmpty'), $i); |
||
| 358 | $error_fields[] = "answer[$i]"; |
||
| 359 | } |
||
| 360 | |||
| 361 | if ($weighting === '' || !is_numeric($weighting)) { |
||
| 362 | $errors[] = sprintf(get_lang('ScoreMustBeNumeric'), $i); |
||
| 363 | $error_fields[] = "weighting[$i]"; |
||
| 364 | } |
||
| 365 | |||
| 366 | if ($isCorrect) { |
||
| 367 | $hasCorrectAnswer = true; |
||
| 368 | if (floatval($weighting) > 0) { |
||
| 369 | $hasPositiveScore = true; |
||
| 370 | } |
||
| 371 | } |
||
| 372 | } |
||
| 373 | |||
| 374 | if (!$hasCorrectAnswer) { |
||
| 375 | $errors[] = get_lang('ACorrectAnswerIsRequired'); |
||
| 376 | $error_fields[] = "correct"; |
||
| 377 | } |
||
| 378 | |||
| 379 | if (!$hasPositiveScore) { |
||
| 380 | $errors[] = get_lang('TheCorrectAnswerMustHaveAPositiveScore'); |
||
| 381 | } |
||
| 382 | |||
| 383 | return empty($errors) ? true : ['errors' => $errors, 'error_fields' => $error_fields]; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * {@inheritdoc} |
||
| 388 | */ |
||
| 389 | public function processAnswersCreation($form, $exercise) |
||
| 390 | { |
||
| 391 | $validationResult = $this->validateAnswers($form); |
||
| 392 | if ($validationResult !== true) { |
||
| 393 | Display::addFlash(Display::return_message(implode("<br>", $validationResult['errors']), 'error')); |
||
| 394 | |||
| 395 | return; |
||
| 396 | } |
||
| 397 | |||
| 398 | $questionWeighting = $nbrGoodAnswers = 0; |
||
| 399 | $correct = $form->getSubmitValue('correct'); |
||
| 400 | $objAnswer = new Answer($this->iid); |
||
| 401 | $nb_answers = $form->getSubmitValue('nb_answers'); |
||
| 402 | |||
| 403 | for ($i = 1; $i <= $nb_answers; $i++) { |
||
| 404 | $answer = trim($form->getSubmitValue('answer['.$i.']')); |
||
| 405 | $comment = trim($form->getSubmitValue('comment['.$i.']')); |
||
| 406 | $weighting = trim($form->getSubmitValue('weighting['.$i.']')); |
||
| 407 | $scenario = $form->getSubmitValue('scenario'); |
||
| 408 | |||
| 409 | $try = null; |
||
| 410 | $lp = null; |
||
| 411 | $destination = null; |
||
| 412 | $url = null; |
||
| 413 | if (isset($scenario['try'.$i])) { |
||
| 414 | $try = !empty($scenario['try'.$i]); |
||
| 415 | } |
||
| 416 | |||
| 417 | if (isset($scenario['lp'.$i])) { |
||
| 418 | $lp = $scenario['lp'.$i]; |
||
| 419 | } |
||
| 420 | |||
| 421 | if (isset($scenario['destination'.$i])) { |
||
| 422 | $destination = $scenario['destination'.$i]; |
||
| 423 | } |
||
| 424 | |||
| 425 | if (isset($scenario['url'.$i])) { |
||
| 426 | $url = trim($scenario['url'.$i]); |
||
| 427 | } |
||
| 428 | |||
| 429 | /* |
||
| 430 | How we are going to parse the destination value |
||
| 431 | |||
| 432 | here we parse the destination value which is a string |
||
| 433 | 1@@3@@2;4;4;@@http://www.chamilo.org |
||
| 434 | |||
| 435 | where: try_again@@lp_id@@selected_questions@@url |
||
| 436 | |||
| 437 | try_again = is 1 || 0 |
||
| 438 | lp_id = id of a learning path (0 if dont select) |
||
| 439 | selected_questions= ids of questions |
||
| 440 | url= an url |
||
| 441 | |||
| 442 | $destination_str=''; |
||
| 443 | foreach ($list_destination as $destination_id) |
||
| 444 | { |
||
| 445 | $destination_str.=$destination_id.';'; |
||
| 446 | }*/ |
||
| 447 | |||
| 448 | $goodAnswer = $correct == $i ? true : false; |
||
| 449 | |||
| 450 | if ($goodAnswer) { |
||
| 451 | $nbrGoodAnswers++; |
||
| 452 | $weighting = abs($weighting); |
||
| 453 | if ($weighting > 0) { |
||
| 454 | $questionWeighting += $weighting; |
||
| 455 | } |
||
| 456 | } |
||
| 457 | |||
| 458 | if (empty($try)) { |
||
| 459 | $try = 0; |
||
| 460 | } |
||
| 461 | |||
| 462 | if (empty($lp)) { |
||
| 463 | $lp = 0; |
||
| 464 | } |
||
| 465 | |||
| 466 | if (empty($destination)) { |
||
| 467 | $destination = 0; |
||
| 468 | } |
||
| 469 | |||
| 470 | if ($url == '') { |
||
| 471 | $url = 0; |
||
| 472 | } |
||
| 473 | |||
| 474 | //1@@1;2;@@2;4;4;@@http://www.chamilo.org |
||
| 475 | $dest = $try.'@@'.$lp.'@@'.$destination.'@@'.$url; |
||
| 476 | $objAnswer->createAnswer( |
||
| 477 | $answer, |
||
| 478 | $goodAnswer, |
||
| 479 | $comment, |
||
| 480 | $weighting, |
||
| 481 | $i, |
||
| 482 | null, |
||
| 483 | null, |
||
| 484 | $dest |
||
| 485 | ); |
||
| 486 | } |
||
| 487 | |||
| 488 | // saves the answers into the data base |
||
| 489 | $objAnswer->save(); |
||
| 490 | |||
| 491 | // sets the total weighting of the question |
||
| 492 | $this->updateWeighting($questionWeighting); |
||
| 493 | $this->save($exercise); |
||
| 494 | } |
||
| 495 | |||
| 496 | /** |
||
| 497 | * {@inheritdoc} |
||
| 498 | */ |
||
| 499 | public function return_header(Exercise $exercise, $counter = null, $score = []) |
||
| 500 | { |
||
| 501 | $header = parent::return_header($exercise, $counter, $score); |
||
| 502 | $header .= '<table class="'.$this->question_table_class.'"><tr>'; |
||
| 503 | |||
| 504 | $header .= '<th>'.get_lang('Choice').'</th>'; |
||
| 505 | if ($exercise->showExpectedChoiceColumn()) { |
||
| 506 | $header .= '<th>'.get_lang('ExpectedChoice').'</th>'; |
||
| 507 | } |
||
| 508 | |||
| 509 | $header .= '<th>'.get_lang('Answer').'</th>'; |
||
| 510 | if ($exercise->showExpectedChoice()) { |
||
| 511 | $header .= '<th>'.get_lang('Status').'</th>'; |
||
| 512 | } |
||
| 513 | if (false === $exercise->hideComment) { |
||
| 514 | $header .= '<th>'.get_lang('Comment').'</th>'; |
||
| 515 | } |
||
| 516 | $header .= '</tr>'; |
||
| 517 | |||
| 518 | return $header; |
||
| 519 | } |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Saves one answer to the database. |
||
| 523 | * |
||
| 524 | * @param int $id The ID of the answer (has to be calculated for this course) |
||
| 525 | * @param int $question_id The question ID (to which the answer is attached) |
||
| 526 | * @param string $title The text of the answer |
||
| 527 | * @param string $comment The feedback for the answer |
||
| 528 | * @param float $score The score you get when picking this answer |
||
| 529 | * @param int $correct Whether this answer is considered *the* correct one (this is the unique answer type) |
||
| 530 | */ |
||
| 531 | public function addAnswer( |
||
| 532 | $id, |
||
| 533 | $question_id, |
||
| 534 | $title, |
||
| 535 | $comment, |
||
| 536 | $score = 0.0, |
||
| 537 | $correct = 0 |
||
| 538 | ) { |
||
| 539 | $em = Database::getManager(); |
||
| 540 | $tbl_quiz_answer = Database::get_course_table(TABLE_QUIZ_ANSWER); |
||
| 541 | $tbl_quiz_question = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
| 542 | $course_id = api_get_course_int_id(); |
||
| 543 | $question_id = (int) $question_id; |
||
| 544 | $score = floatval($score); |
||
| 545 | $correct = intval($correct); |
||
| 546 | $title = Database::escape_string($title); |
||
| 547 | $comment = Database::escape_string($comment); |
||
| 548 | // Get the max position. |
||
| 549 | $sql = "SELECT max(position) as max_position |
||
| 550 | FROM $tbl_quiz_answer |
||
| 551 | WHERE |
||
| 552 | question_id = $question_id"; |
||
| 553 | $rs_max = Database::query($sql); |
||
| 554 | $row_max = Database::fetch_object($rs_max); |
||
| 555 | $position = $row_max->max_position + 1; |
||
| 556 | |||
| 557 | // Insert a new answer |
||
| 558 | $quizAnswer = new CQuizAnswer(); |
||
| 559 | $quizAnswer |
||
| 560 | ->setCId($course_id) |
||
| 561 | ->setId($id) |
||
| 562 | ->setQuestionId($question_id) |
||
| 563 | ->setAnswer($title) |
||
| 564 | ->setCorrect($correct) |
||
| 565 | ->setComment($comment) |
||
| 566 | ->setPonderation($score) |
||
| 567 | ->setPosition($position) |
||
| 568 | ->setDestination(CQuizAnswer::DEFAULT_DESTINATION); |
||
| 569 | |||
| 570 | $em->persist($quizAnswer); |
||
| 571 | $em->flush(); |
||
| 572 | |||
| 573 | $id = $quizAnswer->getId(); |
||
| 574 | |||
| 575 | if ($id) { |
||
| 576 | $quizAnswer |
||
| 577 | ->setId($id); |
||
| 578 | |||
| 579 | $em->merge($quizAnswer); |
||
| 580 | $em->flush(); |
||
| 581 | } |
||
| 582 | |||
| 583 | if ($correct) { |
||
| 584 | $sql = "UPDATE $tbl_quiz_question |
||
| 585 | SET ponderation = (ponderation + $score) |
||
| 586 | WHERE iid = $question_id"; |
||
| 587 | Database::query($sql); |
||
| 588 | } |
||
| 589 | } |
||
| 590 | } |
||
| 591 |