| Conditions | 102 |
| Paths | > 20000 |
| Total Lines | 501 |
| Code Lines | 341 |
| Lines | 14 |
| Ratio | 2.79 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 133 | function lp_upload_quiz_action_handling() { |
||
| 134 | global $debug; |
||
| 135 | $_course = api_get_course_info(); |
||
| 136 | $courseId = $_course['real_id']; |
||
| 137 | |||
| 138 | if (!isset($_POST['submit_upload_quiz'])) { |
||
| 139 | return; |
||
| 140 | } |
||
| 141 | |||
| 142 | // Get the extension of the document. |
||
| 143 | $path_info = pathinfo($_FILES['user_upload_quiz']['name']); |
||
| 144 | |||
| 145 | // Check if the document is an Excel document |
||
| 146 | if ($path_info['extension'] != 'xls') { |
||
| 147 | return; |
||
| 148 | } |
||
| 149 | |||
| 150 | // Read the Excel document |
||
| 151 | $data = new Spreadsheet_Excel_Reader(); |
||
| 152 | // Set output Encoding. |
||
| 153 | $data->setOutputEncoding(api_get_system_encoding()); |
||
| 154 | // Reading the xls document. |
||
| 155 | $data->read($_FILES['user_upload_quiz']['tmp_name']); |
||
| 156 | |||
| 157 | $correctScore = isset($_POST['correct_score']) ? $_POST['correct_score'] : null; |
||
| 158 | $incorrectScore = isset($_POST['incorrect_score']) ? $_POST['incorrect_score'] : null; |
||
| 159 | $useCustomScore = isset($_POST['user_custom_score']) ? true : false; |
||
| 160 | |||
| 161 | $propagateNegative = 0; |
||
| 162 | if ($useCustomScore && !empty($incorrectScore)) { |
||
| 163 | if ($incorrectScore < 0) { |
||
| 164 | $propagateNegative = 1; |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | // Variables |
||
| 169 | $quiz_index = 0; |
||
| 170 | $question_title_index = array(); |
||
| 171 | $question_name_index_init = array(); |
||
| 172 | $question_name_index_end = array(); |
||
| 173 | $score_index = array(); |
||
| 174 | $feedback_true_index = array(); |
||
| 175 | $feedback_false_index = array(); |
||
| 176 | $number_questions = 0; |
||
| 177 | $question_description_index = array(); |
||
| 178 | $noNegativeScoreIndex = array(); |
||
| 179 | $questionTypeList = array(); |
||
| 180 | $questionTypeIndex = array(); |
||
| 181 | $categoryList = array(); |
||
| 182 | |||
| 183 | // Reading all the first column items sequentially to create breakpoints |
||
| 184 | for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) { |
||
| 185 | if ($data->sheets[0]['cells'][$i][1] == 'Quiz' && $i == 1) { |
||
| 186 | $quiz_index = $i; // Quiz title position, only occurs once |
||
| 187 | } elseif ($data->sheets[0]['cells'][$i][1] == 'Question') { |
||
| 188 | $question_title_index[] = $i; // Question title position line |
||
| 189 | $question_name_index_init[] = $i + 1; // Questions name 1st position line |
||
| 190 | $number_questions++; |
||
| 191 | } elseif ($data->sheets[0]['cells'][$i][1] == 'Score') { |
||
| 192 | $question_name_index_end[] = $i - 1; // Question name position |
||
| 193 | $score_index[] = $i; // Question score position |
||
| 194 | } elseif ($data->sheets[0]['cells'][$i][1] == 'FeedbackTrue') { |
||
| 195 | $feedback_true_index[] = $i; // FeedbackTrue position (line) |
||
| 196 | } elseif ($data->sheets[0]['cells'][$i][1] == 'FeedbackFalse') { |
||
| 197 | $feedback_false_index[] = $i; // FeedbackFalse position (line) |
||
| 198 | } elseif ($data->sheets[0]['cells'][$i][1] == 'EnrichQuestion') { |
||
| 199 | $question_description_index[] = $i; |
||
| 200 | } elseif ($data->sheets[0]['cells'][$i][1] == 'NoNegativeScore') { |
||
| 201 | $noNegativeScoreIndex[] = $i; |
||
| 202 | } elseif ($data->sheets[0]['cells'][$i][1] == 'QuestionType') { |
||
| 203 | $questionTypeIndex[] = $i; |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | // Variables |
||
| 208 | $quiz = array(); |
||
| 209 | $question = array(); |
||
| 210 | $new_answer = array(); |
||
| 211 | $score_list = array(); |
||
| 212 | $feedback_true_list = array(); |
||
| 213 | $feedback_false_list = array(); |
||
| 214 | $question_description = array(); |
||
| 215 | $noNegativeScoreList = array(); |
||
| 216 | |||
| 217 | // Getting questions. |
||
| 218 | $k = $z = $q = $l = $m = $n = 0; |
||
| 219 | for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) { |
||
| 220 | if (is_array($data->sheets[0]['cells'][$i])) { |
||
| 221 | $column_data = $data->sheets[0]['cells'][$i]; |
||
| 222 | // Fill all column with data to have a full array |
||
| 223 | View Code Duplication | for ($x = 1; $x <= $data->sheets[0]['numCols']; $x++) { |
|
| 224 | if (empty($column_data[$x])) { |
||
| 225 | $data->sheets[0]['cells'][$i][$x] = ''; |
||
| 226 | } |
||
| 227 | } |
||
| 228 | // Array filled with data |
||
| 229 | $column_data = $data->sheets[0]['cells'][$i]; |
||
| 230 | } else { |
||
| 231 | $column_data = ''; |
||
| 232 | } |
||
| 233 | |||
| 234 | // Fill quiz data |
||
| 235 | if ($quiz_index == $i) { |
||
| 236 | // The title always in the first position |
||
| 237 | $quiz = $column_data; |
||
| 238 | } elseif (in_array($i, $question_title_index)) { |
||
| 239 | //a complete line where 1st column is 'Question' |
||
| 240 | $question[$k] = $column_data; |
||
| 241 | |||
| 242 | for ($counter = 0; $counter < 12; $counter++) { |
||
| 243 | $myData = isset($data->sheets[0]['cells'][$i + $counter]) ? $data->sheets[0]['cells'][$i + $counter] : null; |
||
| 244 | if (isset($myData[1]) && $myData[1] == 'QuestionType') { |
||
| 245 | $questionTypeList[$k] = $myData[3]; |
||
| 246 | } |
||
| 247 | if (isset($myData[1]) && $myData[1] == 'Category') { |
||
| 248 | $categoryList[$k] = $myData[2]; |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | if (!isset($questionTypeList[$k])) { |
||
| 253 | $questionTypeList[$k] = null; |
||
| 254 | } |
||
| 255 | |||
| 256 | $k++; |
||
| 257 | } elseif (in_array($i, $score_index)) { |
||
| 258 | //a complete line where 1st column is 'Score' |
||
| 259 | $score_list[$z] = $column_data; |
||
| 260 | $z++; |
||
| 261 | } elseif (in_array($i, $feedback_true_index)) { |
||
| 262 | //a complete line where 1st column is 'FeedbackTrue' |
||
| 263 | $feedback_true_list[$q] = $column_data; |
||
| 264 | $q++; |
||
| 265 | } elseif (in_array($i, $feedback_false_index)) { |
||
| 266 | //a complete line where 1st column is 'FeedbackFalse' for wrong answers |
||
| 267 | $feedback_false_list[$l] = $column_data; |
||
| 268 | $l++; |
||
| 269 | } elseif (in_array($i, $question_description_index)) { |
||
| 270 | //a complete line where 1st column is 'EnrichQuestion' |
||
| 271 | $question_description[$m] = $column_data; |
||
| 272 | $m++; |
||
| 273 | } elseif (in_array($i, $noNegativeScoreIndex)) { |
||
| 274 | //a complete line where 1st column is 'NoNegativeScore' |
||
| 275 | $noNegativeScoreList[$z-1] = $column_data; |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | // Get answers |
||
| 280 | for ($i = 0; $i < count($question_name_index_init); $i++) { |
||
| 281 | for ($j = $question_name_index_init[$i]; $j <= $question_name_index_end[$i]; $j++) { |
||
| 282 | if (is_array($data->sheets[0]['cells'][$j])) { |
||
| 283 | $column_data = $data->sheets[0]['cells'][$j]; |
||
| 284 | // Fill all column with data |
||
| 285 | View Code Duplication | for ($x = 1; $x <= $data->sheets[0]['numCols']; $x++) { |
|
| 286 | if (empty($column_data[$x])) { |
||
| 287 | $data->sheets[0]['cells'][$j][$x] = ''; |
||
| 288 | } |
||
| 289 | } |
||
| 290 | $column_data = $data->sheets[0]['cells'][$j]; |
||
| 291 | // Array filled of data |
||
| 292 | if (is_array($column_data) && count($column_data) > 0) { |
||
| 293 | $new_answer[$i][$j] = $column_data; |
||
| 294 | } |
||
| 295 | } |
||
| 296 | } |
||
| 297 | } |
||
| 298 | |||
| 299 | // Quiz title. |
||
| 300 | $quiz_title = $quiz[2]; |
||
| 301 | |||
| 302 | if ($quiz_title != '') { |
||
| 303 | // Variables |
||
| 304 | $type = 2; |
||
| 305 | $random = $active = $results = $max_attempt = $expired_time = 0; |
||
| 306 | // Make sure feedback is enabled (3 to disable), otherwise the fields |
||
| 307 | // added to the XLS are not shown, which is confusing |
||
| 308 | $feedback = 0; |
||
| 309 | |||
| 310 | // Quiz object |
||
| 311 | $exercise = new Exercise(); |
||
| 312 | // |
||
| 313 | $quiz_id = $exercise->createExercise( |
||
| 314 | $quiz_title, |
||
| 315 | $expired_time, |
||
| 316 | $type, |
||
| 317 | $random, |
||
| 318 | $active, |
||
| 319 | $results, |
||
| 320 | $max_attempt, |
||
| 321 | $feedback, |
||
| 322 | $propagateNegative |
||
| 323 | ); |
||
| 324 | |||
| 325 | if ($quiz_id) { |
||
| 326 | |||
| 327 | // insert into the item_property table |
||
| 328 | api_item_property_update( |
||
| 329 | $_course, |
||
| 330 | TOOL_QUIZ, |
||
| 331 | $quiz_id, |
||
| 332 | 'QuizAdded', |
||
| 333 | api_get_user_id() |
||
| 334 | ); |
||
| 335 | |||
| 336 | // Import questions. |
||
| 337 | for ($i = 0; $i < $number_questions; $i++) { |
||
| 338 | // Question name |
||
| 339 | $question_title = $question[$i][2]; |
||
| 340 | $description = isset($question_description[$i][2]) ? $question_description[$i][2] : ''; |
||
| 341 | $categoryId = null; |
||
| 342 | if (isset($categoryList[$i]) && !empty($categoryList[$i])) { |
||
| 343 | $categoryName = $categoryList[$i]; |
||
| 344 | $categoryId = TestCategory::get_category_id_for_title($categoryName, $courseId); |
||
| 345 | if (empty($categoryId)) { |
||
| 346 | $category = new TestCategory(null, $categoryName, ''); |
||
| 347 | $categoryId = $category->addCategoryInBDD(); |
||
| 348 | } |
||
| 349 | } |
||
| 350 | |||
| 351 | $question_description_text = "<p></p>"; |
||
| 352 | if (!empty($description)) { |
||
| 353 | // Question description. |
||
| 354 | $question_description_text = "<p>".$description."</p>"; |
||
| 355 | } |
||
| 356 | |||
| 357 | // Unique answers are the only question types available for now |
||
| 358 | // through xls-format import |
||
| 359 | $answerList = isset($new_answer[$i]) ? $new_answer[$i] : ''; |
||
| 360 | |||
| 361 | $question_id = null; |
||
| 362 | if (isset($questionTypeList[$i])) { |
||
| 363 | $detectQuestionType = intval($questionTypeList[$i]); |
||
| 364 | } else { |
||
| 365 | $detectQuestionType = detectQuestionType( |
||
| 366 | $answerList, |
||
| 367 | $score_list |
||
| 368 | ); |
||
| 369 | } |
||
| 370 | |||
| 371 | /** @var Question $answer */ |
||
| 372 | switch ($detectQuestionType) { |
||
| 373 | case FREE_ANSWER: |
||
| 374 | $answer = new FreeAnswer(); |
||
| 375 | break; |
||
| 376 | case GLOBAL_MULTIPLE_ANSWER: |
||
| 377 | $answer = new GlobalMultipleAnswer(); |
||
| 378 | break; |
||
| 379 | case MULTIPLE_ANSWER: |
||
| 380 | $answer = new MultipleAnswer(); |
||
| 381 | break; |
||
| 382 | case FILL_IN_BLANKS: |
||
| 383 | $answer = new FillBlanks(); |
||
| 384 | $question_description_text = ''; |
||
| 385 | break; |
||
| 386 | case MATCHING: |
||
| 387 | $answer = new Matching(); |
||
| 388 | break; |
||
| 389 | case UNIQUE_ANSWER: |
||
| 390 | default: |
||
| 391 | $answer = new UniqueAnswer(); |
||
| 392 | break; |
||
| 393 | } |
||
| 394 | |||
| 395 | if ($question_title != '') { |
||
| 396 | $question_id = $answer->create_question( |
||
| 397 | $quiz_id, |
||
| 398 | $question_title, |
||
| 399 | $question_description_text, |
||
| 400 | 0, // max score |
||
| 401 | $answer->type |
||
| 402 | ); |
||
| 403 | if (!empty($categoryId)) { |
||
| 404 | TestCategory::add_category_for_question_id( |
||
| 405 | $categoryId, |
||
| 406 | $question_id, |
||
| 407 | $courseId |
||
| 408 | ); |
||
| 409 | } |
||
| 410 | } |
||
| 411 | |||
| 412 | switch ($detectQuestionType) { |
||
| 413 | case GLOBAL_MULTIPLE_ANSWER: |
||
| 414 | case MULTIPLE_ANSWER: |
||
| 415 | case UNIQUE_ANSWER: |
||
| 416 | $total = 0; |
||
| 417 | if (is_array($answerList) && !empty($question_id)) { |
||
| 418 | $id = 1; |
||
| 419 | $globalScore = null; |
||
| 420 | $objAnswer = new Answer($question_id, $courseId); |
||
| 421 | $globalScore = $score_list[$i][3]; |
||
| 422 | |||
| 423 | // Calculate the number of correct answers to divide the |
||
| 424 | // score between them when importing from CSV |
||
| 425 | $numberRightAnswers = 0; |
||
| 426 | foreach ($answerList as $answer_data) { |
||
| 427 | if (strtolower($answer_data[3]) == 'x') { |
||
| 428 | $numberRightAnswers++; |
||
| 429 | } |
||
| 430 | } |
||
| 431 | |||
| 432 | foreach ($answerList as $answer_data) { |
||
| 433 | $answerValue = $answer_data[2]; |
||
| 434 | $correct = 0; |
||
| 435 | $score = 0; |
||
| 436 | if (strtolower($answer_data[3]) == 'x') { |
||
| 437 | $correct = 1; |
||
| 438 | $score = $score_list[$i][3]; |
||
| 439 | $comment = $feedback_true_list[$i][2]; |
||
| 440 | } else { |
||
| 441 | $comment = $feedback_false_list[$i][2]; |
||
| 442 | $floatVal = (float)$answer_data[3]; |
||
| 443 | if (is_numeric($floatVal)) { |
||
| 444 | $score = $answer_data[3]; |
||
| 445 | } |
||
| 446 | } |
||
| 447 | |||
| 448 | if ($useCustomScore) { |
||
| 449 | if ($correct) { |
||
| 450 | $score = $correctScore; |
||
| 451 | } else { |
||
| 452 | $score = $incorrectScore; |
||
| 453 | } |
||
| 454 | } |
||
| 455 | |||
| 456 | // Fixing scores: |
||
| 457 | switch ($detectQuestionType) { |
||
| 458 | case GLOBAL_MULTIPLE_ANSWER: |
||
| 459 | if (isset($noNegativeScoreList[$i][3])) { |
||
| 460 | if (!(strtolower($noNegativeScoreList[$i][3]) == 'x') && |
||
| 461 | !$correct |
||
| 462 | ) { |
||
| 463 | $score = $score_list[$i][3] * -1; |
||
| 464 | } |
||
| 465 | } else { |
||
| 466 | $score = $score_list[$i][3] * -1; |
||
| 467 | } |
||
| 468 | $score /= $numberRightAnswers; |
||
| 469 | break; |
||
| 470 | case UNIQUE_ANSWER: |
||
| 471 | break; |
||
| 472 | case MULTIPLE_ANSWER: |
||
| 473 | if (!$correct) { |
||
| 474 | //$total = $total - $score; |
||
| 475 | } |
||
| 476 | break; |
||
| 477 | } |
||
| 478 | |||
| 479 | $objAnswer->createAnswer( |
||
| 480 | $answerValue, |
||
| 481 | $correct, |
||
| 482 | $comment, |
||
| 483 | $score, |
||
| 484 | $id |
||
| 485 | ); |
||
| 486 | |||
| 487 | $total += $score; |
||
| 488 | $id++; |
||
| 489 | } |
||
| 490 | |||
| 491 | $objAnswer->save(); |
||
| 492 | |||
| 493 | $questionObj = Question::read( |
||
| 494 | $question_id, |
||
| 495 | $courseId |
||
| 496 | ); |
||
| 497 | |||
| 498 | switch ($detectQuestionType) { |
||
| 499 | case GLOBAL_MULTIPLE_ANSWER: |
||
| 500 | $questionObj->updateWeighting($globalScore); |
||
| 501 | break; |
||
| 502 | case UNIQUE_ANSWER: |
||
| 503 | case MULTIPLE_ANSWER: |
||
| 504 | default: |
||
| 505 | $questionObj->updateWeighting($total); |
||
| 506 | break; |
||
| 507 | } |
||
| 508 | |||
| 509 | $questionObj->save(); |
||
| 510 | } |
||
| 511 | break; |
||
| 512 | case FREE_ANSWER: |
||
| 513 | $questionObj = Question::read($question_id, $courseId); |
||
| 514 | $globalScore = $score_list[$i][3]; |
||
| 515 | $questionObj->updateWeighting($globalScore); |
||
| 516 | $questionObj->save(); |
||
| 517 | break; |
||
| 518 | case FILL_IN_BLANKS: |
||
| 519 | |||
| 520 | $scoreList = array(); |
||
| 521 | $size = array(); |
||
| 522 | |||
| 523 | $globalScore = 0; |
||
| 524 | foreach($answerList as $data) { |
||
| 525 | $score = isset($data[3]) ? $data[3] : 0; |
||
| 526 | $globalScore += $score; |
||
| 527 | $scoreList[] = $score; |
||
| 528 | $size[] = 200; |
||
| 529 | } |
||
| 530 | |||
| 531 | $scoreToString = implode(',', $scoreList); |
||
| 532 | $sizeToString = implode(',', $size); |
||
| 533 | |||
| 534 | //<p>Texte long avec les [mots] à [remplir] mis entre [crochets]</p>::10,10,10:200.36363999999998,200,200:0@' |
||
| 535 | $answerValue = $description.'::'.$scoreToString.':'.$sizeToString.':0@'; |
||
| 536 | $objAnswer = new Answer($question_id, $courseId); |
||
| 537 | $objAnswer->createAnswer( |
||
| 538 | $answerValue, |
||
| 539 | '', //$correct, |
||
| 540 | '', //$comment, |
||
| 541 | $globalScore, |
||
| 542 | 1 |
||
| 543 | ); |
||
| 544 | |||
| 545 | $objAnswer->save(); |
||
| 546 | |||
| 547 | $questionObj = Question::read($question_id, $courseId); |
||
| 548 | $questionObj->updateWeighting($globalScore); |
||
| 549 | $questionObj->save(); |
||
| 550 | break; |
||
| 551 | case MATCHING: |
||
| 552 | $globalScore = $score_list[$i][3]; |
||
| 553 | $position = 1; |
||
| 554 | $objAnswer = new Answer($question_id, $courseId); |
||
| 555 | foreach ($answerList as $data) { |
||
| 556 | $option = isset($data[3]) ? $data[3] : ''; |
||
| 557 | $objAnswer->createAnswer($option, 0, '', 0, $position); |
||
| 558 | $position++; |
||
| 559 | } |
||
| 560 | |||
| 561 | $counter = 1; |
||
| 562 | foreach ($answerList as $data) { |
||
| 563 | $value = isset($data[2]) ? $data[2] : ''; |
||
| 564 | $position++; |
||
| 565 | |||
| 566 | $objAnswer->createAnswer( |
||
| 567 | $value, |
||
| 568 | $counter, |
||
| 569 | ' ', |
||
| 570 | $globalScore, |
||
| 571 | $position |
||
| 572 | ); |
||
| 573 | $counter++; |
||
| 574 | } |
||
| 575 | |||
| 576 | $objAnswer->save(); |
||
| 577 | |||
| 578 | $questionObj = Question::read($question_id, $courseId); |
||
| 579 | $questionObj->updateWeighting($globalScore); |
||
| 580 | $questionObj->save(); |
||
| 581 | |||
| 582 | break; |
||
| 583 | } |
||
| 584 | } |
||
| 585 | } |
||
| 586 | $lpFromSession = Session::read('lpobject'); |
||
| 587 | |||
| 588 | if (isset($lpFromSession)) { |
||
| 589 | if ($debug > 0) { |
||
| 590 | error_log('New LP - SESSION[lpobject] is defined', 0); |
||
| 591 | } |
||
| 592 | $oLP = unserialize($lpFromSession); |
||
| 593 | if (is_object($oLP)) { |
||
| 594 | if ($debug > 0) { |
||
| 595 | error_log('New LP - oLP is object', 0); |
||
| 596 | } |
||
| 597 | if ((empty($oLP->cc)) OR $oLP->cc != api_get_course_id()) { |
||
| 598 | if ($debug > 0) { |
||
| 599 | error_log('New LP - Course has changed, discard lp object', 0); |
||
| 600 | } |
||
| 601 | $oLP = null; |
||
| 602 | Session::erase('oLP'); |
||
| 603 | Session::erase('lpobject'); |
||
| 604 | } else { |
||
| 605 | Session::write('oLP', $oLP); |
||
| 606 | } |
||
| 607 | } |
||
| 608 | } |
||
| 609 | /** @var learnpath $lpFromSession */ |
||
| 610 | $lpFromSession = Session::read('oLP'); |
||
| 611 | if (isset($lpFromSession) && isset($_GET['lp_id'])) { |
||
| 612 | $previous = $lpFromSession->select_previous_item_id(); |
||
| 613 | $parent = 0; |
||
| 614 | // Add a Quiz as Lp Item |
||
| 615 | $lpFromSession->add_item( |
||
| 616 | $parent, |
||
| 617 | $previous, |
||
| 618 | TOOL_QUIZ, |
||
| 619 | $quiz_id, |
||
| 620 | $quiz_title, |
||
| 621 | '' |
||
| 622 | ); |
||
| 623 | // Redirect to home page for add more content |
||
| 624 | header( |
||
| 625 | 'location: ../newscorm/lp_controller.php?'.api_get_cidreq( |
||
| 626 | ).'&action=add_item&type=step&lp_id='.intval($_GET['lp_id']) |
||
| 627 | ); |
||
| 628 | exit; |
||
| 629 | View Code Duplication | } else { |
|
| 630 | echo '<script>window.location.href = "'.api_get_path(WEB_CODE_PATH).'exercice/admin.php?'.api_get_cidReq().'&exerciseId='.$quiz_id.'&session_id='.api_get_session_id().'"</script>'; |
||
| 631 | } |
||
| 632 | } |
||
| 633 | } |
||
| 634 | |||
| 679 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.