$(function () { $('#user_custom_score').on('change', function () { if ($('#user_custom_score[type=\"checkbox\"]').prop('checked')) { $('#options').removeClass('hidden') } else { $('#options').addClass('hidden') } }); }); "; // Action handling lp_upload_quiz_action_handling(); $interbreadcrumb[] = [ 'url' => 'exercise.php?'.api_get_cidreq(), 'name' => get_lang('Exercises'), ]; Display::display_header(get_lang('ImportExcelQuiz'), 'Exercises'); echo '
$description
"; } // Unique answers are the only question types available for now // through xls-format import $question_id = null; if (isset($questionTypeList[$i]) && $questionTypeList[$i] != '') { $detectQuestionType = (int) $questionTypeList[$i]; } else { $detectQuestionType = detectQuestionType($myAnswerList); } /** @var Question $answer */ switch ($detectQuestionType) { case FREE_ANSWER: $answer = new FreeAnswer(); break; case GLOBAL_MULTIPLE_ANSWER: $answer = new GlobalMultipleAnswer(); break; case MULTIPLE_ANSWER: $answer = new MultipleAnswer(); break; case MULTIPLE_ANSWER_DROPDOWN: $answer = new MultipleAnswerDropdown(); break; case MULTIPLE_ANSWER_DROPDOWN_COMBINATION: $answer = new MultipleAnswerDropdownCombination(); break; case FILL_IN_BLANKS: case FILL_IN_BLANKS_COMBINATION: $answer = new FillBlanks(); $question_description_text = ''; break; case MATCHING: $answer = new Matching(); break; case UNIQUE_ANSWER: default: $answer = new UniqueAnswer(); break; } if ($questionTitle != '') { $question_id = $answer->create_question( $quiz_id, $questionTitle, $question_description_text, 0, // max score $answer->type ); if (!empty($categoryId)) { TestCategory::addCategoryToQuestion( $categoryId, $question_id, $courseId ); } } switch ($detectQuestionType) { case GLOBAL_MULTIPLE_ANSWER: case MULTIPLE_ANSWER_DROPDOWN: case MULTIPLE_ANSWER_DROPDOWN_COMBINATION: case MULTIPLE_ANSWER: case UNIQUE_ANSWER: $total = 0; if (is_array($myAnswerList) && !empty($myAnswerList) && !empty($question_id)) { $id = 1; $objAnswer = new Answer($question_id, $courseId); $globalScore = isset($scoreList[$i]) ? $scoreList[$i] : null; // Calculate the number of correct answers to divide the // score between them when importing from CSV $numberRightAnswers = 0; foreach ($myAnswerList as $answer_data) { if (strtolower($answer_data['extra']) == 'x') { $numberRightAnswers++; } } foreach ($myAnswerList as $answer_data) { $answerValue = $answer_data['data']; $correct = 0; $score = 0; if (strtolower($answer_data['extra']) == 'x') { $correct = 1; $score = isset($scoreList[$i]) ? $scoreList[$i] : 0; $comment = isset($feedbackTrueList[$i]) ? $feedbackTrueList[$i] : ''; } else { $comment = isset($feedbackFalseList[$i]) ? $feedbackFalseList[$i] : ''; $floatVal = (float) $answer_data['extra']; if (is_numeric($floatVal)) { $score = $answer_data['extra']; } } if ($useCustomScore) { if ($correct) { $score = $correctScore; } else { $score = $incorrectScore; } } // Fixing scores: switch ($detectQuestionType) { case GLOBAL_MULTIPLE_ANSWER: if ($correct) { $score = abs($scoreList[$i]); } else { if (isset($noNegativeScoreList[$i]) && $noNegativeScoreList[$i] == 'x') { $score = 0; } else { $score = -abs($scoreList[$i]); } } $score /= $numberRightAnswers; break; case UNIQUE_ANSWER: break; case MULTIPLE_ANSWER: if (!$correct) { //$total = $total - $score; } break; case MULTIPLE_ANSWER_DROPDOWN_COMBINATION: $score = 0; break; } $objAnswer->createAnswer( $answerValue, $correct, $comment, $score, $id ); if ($correct) { //only add the item marked as correct ( x ) $total += (float) $score; } $id++; } $objAnswer->save(); $questionObj = Question::read( $question_id, $_course ); if ($questionObj) { switch ($detectQuestionType) { case GLOBAL_MULTIPLE_ANSWER: case MULTIPLE_ANSWER_DROPDOWN_COMBINATION: $questionObj->updateWeighting($globalScore); break; case UNIQUE_ANSWER: case MULTIPLE_ANSWER: default: $questionObj->updateWeighting($total); break; } $questionObj->save($exercise); } } break; case FREE_ANSWER: $globalScore = isset($scoreList[$i]) ? $scoreList[$i] : null; $questionObj = Question::read($question_id, $_course); if ($questionObj) { $questionObj->updateWeighting($globalScore); $questionObj->save($exercise); } break; case FILL_IN_BLANKS: case FILL_IN_BLANKS_COMBINATION: $fillInScoreList = []; $size = []; $globalScore = 0; foreach ($myAnswerList as $data) { $score = isset($data['extra']) ? $data['extra'] : 0; $globalScore += $score; $fillInScoreList[] = $score; $size[] = 200; } $scoreToString = implode(',', $fillInScoreList); $sizeToString = implode(',', $size); //Texte long avec les [mots] à [remplir] mis entre [crochets]
::10,10,10:200.36363999999998,200,200:0@' $answerValue = $description.'::'.$scoreToString.':'.$sizeToString.':0@'; $objAnswer = new Answer($question_id, $courseId); $objAnswer->createAnswer( $answerValue, '', //$correct, '', //$comment, $globalScore, 1 ); $objAnswer->save(); $questionObj = Question::read($question_id, $_course); if ($questionObj) { $questionObj->updateWeighting($globalScore); $questionObj->save($exercise); } break; case MATCHING: $globalScore = isset($scoreList[$i]) ? $scoreList[$i] : null; $position = 1; $objAnswer = new Answer($question_id, $courseId); foreach ($myAnswerList as $data) { $option = isset($data['extra']) ? $data['extra'] : ''; $objAnswer->createAnswer($option, 0, '', 0, $position); $position++; } $counter = 1; foreach ($myAnswerList as $data) { $value = isset($data['data']) ? $data['data'] : ''; $position++; $objAnswer->createAnswer( $value, $counter, ' ', $globalScore, $position ); $counter++; } $objAnswer->save(); $questionObj = Question::read($question_id, $_course); if ($questionObj) { $questionObj->updateWeighting($globalScore); $questionObj->save($exercise); } break; } } } $lpObject = Session::read('lpobject'); if (!empty($lpObject)) { /** @var learnpath $oLP */ $oLP = UnserializeApi::unserialize('lp', $lpObject); if (is_object($oLP)) { if ((empty($oLP->cc)) || $oLP->cc != api_get_course_id()) { $oLP = null; Session::erase('oLP'); Session::erase('lpobject'); } else { Session::write('oLP', $oLP); } } } Display::addFlash(Display::return_message(get_lang('FileImported'))); if (isset($_SESSION['oLP']) && isset($_GET['lp_id'])) { $previous = $_SESSION['oLP']->select_previous_item_id(); $parent = 0; // Add a Quiz as Lp Item $_SESSION['oLP']->add_item( $parent, $previous, TOOL_QUIZ, $quiz_id, $quizTitle, '' ); // Redirect to home page for add more content header('Location: ../lp/lp_controller.php?'.api_get_cidreq().'&action=add_item&type=step&lp_id='.intval($_GET['lp_id'])); exit; } else { $exerciseUrl = api_get_path(WEB_CODE_PATH). 'exercise/admin.php?'.api_get_cidreq().'&exerciseId='.$quiz_id.'&session_id='.api_get_session_id(); api_location($exerciseUrl); } } /** * @param array $answers_data * * @return int */ function detectQuestionType($answers_data) { $correct = 0; $isNumeric = false; if (empty($answers_data)) { return FREE_ANSWER; } foreach ($answers_data as $answer_data) { if (strtolower($answer_data['extra']) == 'x') { $correct++; } else { if (is_numeric($answer_data['extra'])) { $isNumeric = true; } } } if ($correct == 1) { $type = UNIQUE_ANSWER; } else { if ($correct > 1) { $type = MULTIPLE_ANSWER; } else { $type = FREE_ANSWER; } } if ($type == MULTIPLE_ANSWER) { if ($isNumeric) { $type = MULTIPLE_ANSWER; } else { $type = GLOBAL_MULTIPLE_ANSWER; } } return $type; } if ($origin != 'learnpath') { //so we are not in learnpath tool Display::display_footer(); }