diff --git a/main/exercice/upload_exercise.php b/main/exercice/upload_exercise.php index 49038af972..5841a7b3d0 100755 --- a/main/exercice/upload_exercise.php +++ b/main/exercice/upload_exercise.php @@ -37,9 +37,7 @@ lp_upload_quiz_action_handling(); $interbreadcrumb[]= array ("url"=>"exercise.php", "name"=> get_lang('Exercises')); -// Display the header - -Display :: display_header(get_lang('ImportExcelQuiz'), 'Exercises'); +Display::display_header(get_lang('ImportExcelQuiz'), 'Exercises'); if (isset($_GET['message'])) { if (in_array($_GET['message'], array('ExerciseEdited'))) { @@ -61,18 +59,17 @@ function lp_upload_quiz_actions() { return $return; } -function lp_upload_quiz_secondary_actions() { - $lp_id = Security::remove_XSS($_GET['lp_id']); - $return = ''; - $return .= '' . - Display :: return_icon('reporting32.png', get_lang('Tracking')) . get_lang('Tracking') . ''; +function lp_upload_quiz_secondary_actions() +{ + $return = '' . + Display :: return_icon('reporting32.png', get_lang('Tracking')) . get_lang('Tracking') . ''; return $return; } function lp_upload_quiz_main() { // variable initialisation - $lp_id = isset($_GET['lp_id']) ? Security::remove_XSS($_GET['lp_id']) : null; + $lp_id = isset($_GET['lp_id']) ? intval($_GET['lp_id']) : null; $form = new FormValidator( 'upload', @@ -85,7 +82,7 @@ function lp_upload_quiz_main() { $form->addElement('file', 'user_upload_quiz', get_lang('FileUpload')); $link = ''. - Display::return_icon('export_excel.png', get_lang('DownloadExcelTemplate')).get_lang('DownloadExcelTemplate').''; + Display::return_icon('export_excel.png', get_lang('DownloadExcelTemplate')).get_lang('DownloadExcelTemplate').''; $form->addElement('label', '', $link); $form->addElement('checkbox', 'user_custom_score', null, get_lang('UseCustomScoreForAllQuestions'), array('id'=> 'user_custom_score')); $form->addElement('html', '
".$question_description[$i][2]."
"; + $question_description_text = "".$description."
"; } // Unique answers are the only question types available for now // through xls-format import + $answerList = isset($new_answer[$i]) ? $new_answer[$i] : ''; $question_id = null; - - $detectQuestionType = detectQuestionType( - $new_answer[$i], - $score_list - ); + if (isset($questionTypeList[$i])) { + $detectQuestionType = intval($questionTypeList[$i]); + } else { + $detectQuestionType = detectQuestionType( + $answerList, + $score_list + ); + } /** @var Question $answer */ switch ($detectQuestionType) { @@ -310,6 +338,13 @@ function lp_upload_quiz_action_handling() { case MULTIPLE_ANSWER: $answer = new MultipleAnswer(); break; + case FILL_IN_BLANKS: + $answer = new FillBlanks(); + $question_description_text = ''; + break; + case MATCHING: + $answer = new Matching(); + break; case UNIQUE_ANSWER: default: $answer = new UniqueAnswer(); @@ -326,93 +361,175 @@ function lp_upload_quiz_action_handling() { ); } - $total = 0; - if (is_array($new_answer[$i]) && !empty($question_id)) { - $id = 1; - $answers_data = $new_answer[$i]; - $globalScore = null; - $objAnswer = new Answer($question_id, $courseId); - $globalScore = $score_list[$i][3]; - - // Calculate the number of correct answers to divide the - // score between them when importing from CSV - $numberRightAnswers = 0; - foreach ($answers_data as $answer_data) { - if (strtolower($answer_data[3]) == 'x') { - $numberRightAnswers++; - } - } - foreach ($answers_data as $answer_data) { - $answerValue = $answer_data[2]; - $correct = 0; - $score = 0; - if (strtolower($answer_data[3]) == 'x') { - $correct = 1; - $score = $score_list[$i][3]; - $comment = $feedback_true_list[$i][2]; - } else { - $comment = $feedback_false_list[$i][2]; - $floatVal = (float)$answer_data[3]; - if (is_numeric($floatVal)) { - $score = $answer_data[3]; + switch ($detectQuestionType) { + case GLOBAL_MULTIPLE_ANSWER: + case MULTIPLE_ANSWER: + case UNIQUE_ANSWER: + $total = 0; + if (is_array($answerList) && !empty($question_id)) { + $id = 1; + $globalScore = null; + $objAnswer = new Answer($question_id, $courseId); + $globalScore = $score_list[$i][3]; + + // Calculate the number of correct answers to divide the + // score between them when importing from CSV + $numberRightAnswers = 0; + foreach ($answerList as $answer_data) { + if (strtolower($answer_data[3]) == 'x') { + $numberRightAnswers++; + } } - } - if ($useCustomScore) { - if ($correct) { - $score = $correctScore; - } else { - $score = $incorrectScore; + foreach ($answerList as $answer_data) { + $answerValue = $answer_data[2]; + $correct = 0; + $score = 0; + if (strtolower($answer_data[3]) == 'x') { + $correct = 1; + $score = $score_list[$i][3]; + $comment = $feedback_true_list[$i][2]; + } else { + $comment = $feedback_false_list[$i][2]; + $floatVal = (float)$answer_data[3]; + if (is_numeric($floatVal)) { + $score = $answer_data[3]; + } + } + + if ($useCustomScore) { + if ($correct) { + $score = $correctScore; + } else { + $score = $incorrectScore; + } + } + + // Fixing scores: + switch ($detectQuestionType) { + case GLOBAL_MULTIPLE_ANSWER: + if (isset($noNegativeScoreList[$i][3])) { + if (!(strtolower($noNegativeScoreList[$i][3]) == 'x') && + !$correct + ) { + $score = $score_list[$i][3] * -1; + } + } + $score /= $numberRightAnswers; + break; + case UNIQUE_ANSWER: + break; + case MULTIPLE_ANSWER: + if (!$correct) { + //$total = $total - $score; + } + break; + } + + $objAnswer->createAnswer( + $answerValue, + $correct, + $comment, + $score, + $id + ); + + $total += $score; + $id++; } + + $objAnswer->save(); + + $questionObj = Question::read( + $question_id, + $courseId + ); + + switch ($detectQuestionType) { + case GLOBAL_MULTIPLE_ANSWER: + $questionObj->updateWeighting($globalScore); + break; + case UNIQUE_ANSWER: + case MULTIPLE_ANSWER: + default: + $questionObj->updateWeighting($total); + break; + } + + $questionObj->save(); } + break; + case FREE_ANSWER: + $questionObj = Question::read($question_id, $courseId); + $globalScore = $score_list[$i][3]; + $questionObj->updateWeighting($globalScore); + $questionObj->save(); + break; + case FILL_IN_BLANKS: - // Fixing scores: - switch ($detectQuestionType) { - case GLOBAL_MULTIPLE_ANSWER: - $score /= $numberRightAnswers; - break; - case UNIQUE_ANSWER: - break; - case MULTIPLE_ANSWER: - if (!$correct) { - //$total = $total - $score; - } - break; + $scoreList = array(); + $size = array(); + + $globalScore = 0; + foreach($answerList as $data) { + $score = isset($data[3]) ? $data[3] : 0; + $globalScore += $score; + $scoreList[] = $score; + $size[] = 200; } + $scoreToString = implode(',', $scoreList); + $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, - $score, - $id + '', //$correct, + '', //$comment, + $globalScore, + 1 ); - $total += $score; - $id++; - } + $objAnswer->save(); - $objAnswer->save(); + $questionObj = Question::read($question_id, $courseId); + $questionObj->updateWeighting($globalScore); + $questionObj->save(); + break; + case MATCHING: + $globalScore = $score_list[$i][3]; + $position = 1; + $objAnswer = new Answer($question_id, $courseId); + foreach ($answerList as $data) { + $option = isset($data[3]) ? $data[3] : ''; + $objAnswer->createAnswer($option, 0, '', 0, $position); + $position++; + } - $questionObj = Question::read($question_id, $courseId); + $counter = 1; + foreach ($answerList as $data) { + $value = isset($data[2]) ? $data[2] : ''; + $position++; + + $objAnswer->createAnswer( + $value, + $counter, + ' ', + $globalScore, + $position + ); + $counter++; + } - switch ($detectQuestionType) { - case GLOBAL_MULTIPLE_ANSWER: - $questionObj->updateWeighting($globalScore); - break; - case UNIQUE_ANSWER: - case MULTIPLE_ANSWER: - default: - $questionObj->updateWeighting($total); - break; - } + $objAnswer->save(); - $questionObj->save(); - } else if ($detectQuestionType === FREE_ANSWER) { - $questionObj = Question::read($question_id, $courseId); - $globalScore = $score_list[$i][3]; - $questionObj->updateWeighting($globalScore); - $questionObj->save(); + $questionObj = Question::read($question_id, $courseId); + $questionObj->updateWeighting($globalScore); + $questionObj->save(); + + break; } } } @@ -458,19 +575,19 @@ function lp_upload_quiz_action_handling() { * @param array $answers_data * @return int */ -function detectQuestionType($answers_data) -{ +function detectQuestionType($answers_data) { $correct = 0; $isNumeric = false; - if (!empty($answers_data)) { - foreach ($answers_data as $answer_data) { - if (strtolower($answer_data[3]) == 'x') { - $correct++; - } else { - if (is_numeric($answer_data[3])) { - $isNumeric = true; - } + if (empty($answers_data)) { + return FREE_ANSWER; + } + foreach ($answers_data as $answer_data) { + if (strtolower($answer_data[3]) == 'x') { + $correct++; + } else { + if (is_numeric($answer_data[3])) { + $isNumeric = true; } } }