diff --git a/main/exercise/upload_exercise.php b/main/exercise/upload_exercise.php index 0bac450025..6264383a06 100755 --- a/main/exercise/upload_exercise.php +++ b/main/exercise/upload_exercise.php @@ -1,4 +1,5 @@ get_lang('Exercises'), ]; -// Display the header -Display :: display_header(get_lang('ImportExcelQuiz'), 'Exercises'); +Display::display_header(get_lang('ImportExcelQuiz'), 'Exercises'); -// display the actions echo '
'; echo lp_upload_quiz_actions(); echo '
'; -// the main content lp_upload_quiz_main(); function lp_upload_quiz_actions() @@ -142,7 +138,6 @@ function lp_upload_quiz_action_handling() } $courseId = $_course['real_id']; - // Get the extension of the document. $path_info = pathinfo($_FILES['user_upload_quiz']['name']); @@ -162,12 +157,11 @@ function lp_upload_quiz_action_handling() $questionTypeList = []; $answerList = []; $quizTitle = ''; - $objPHPExcel = PHPExcel_IOFactory::load($_FILES['user_upload_quiz']['tmp_name']); $objPHPExcel->setActiveSheetIndex(0); $worksheet = $objPHPExcel->getActiveSheet(); $highestRow = $worksheet->getHighestRow(); // e.g. 10 - $highestColumn = $worksheet->getHighestColumn(); // e.g 'F' + //$highestColumn = $worksheet->getHighestColumn(); // e.g 'F' $correctScore = isset($_POST['correct_score']) ? $_POST['correct_score'] : null; $incorrectScore = isset($_POST['incorrect_score']) ? $_POST['incorrect_score'] : null; @@ -218,11 +212,11 @@ function lp_upload_quiz_action_handling() $questionTypeTitle = $worksheet->getCellByColumnAndRow(0, $answerRow); $questionTypeExtra = $worksheet->getCellByColumnAndRow(2, $answerRow); $title = $questionTypeTitle->getValue(); - if ($title == 'QuestionType') { + if ($title === 'QuestionType') { $questionTypeList[$numberQuestions] = $questionTypeExtra->getValue(); $continue = false; } - if ($title == 'Question') { + if ($title === 'Question') { $continue = false; } // To avoid loops @@ -263,315 +257,322 @@ function lp_upload_quiz_action_handling() } } - if ($quizTitle != '') { - // Variables - $type = 2; - $random = $active = $results = $max_attempt = $expired_time = 0; - // Make sure feedback is enabled (3 to disable), otherwise the fields - // added to the XLS are not shown, which is confusing - $feedback = 0; - - // Quiz object - $exercise = new Exercise(); - $exercise->updateTitle($quizTitle); - $exercise->updateExpiredTime($expired_time); - $exercise->updateType($type); - $exercise->setRandom($random); - $exercise->active = $active; - $exercise->updateResultsDisabled($results); - $exercise->updateAttempts($max_attempt); - $exercise->updateFeedbackType($feedback); - $exercise->updatePropagateNegative($propagateNegative); - $quiz_id = $exercise->save(); - - if ($quiz_id) { - // Import questions. - for ($i = 0; $i < $numberQuestions; $i++) { - // Question name - $questionTitle = $question[$i]; - $myAnswerList = isset($answerList[$i]) ? $answerList[$i] : []; - $description = isset($questionDescriptionList[$i]) ? $questionDescriptionList[$i] : ''; - $categoryId = null; - if (isset($categoryList[$i]) && !empty($categoryList[$i])) { - $categoryName = $categoryList[$i]; - $categoryId = TestCategory::get_category_id_for_title($categoryName, $courseId); - if (empty($categoryId)) { - $category = new TestCategory(); - $category->name = $categoryName; - $categoryId = $category->save(); - } - } + $url = api_get_path(WEB_CODE_PATH).'exercise/upload_exercise.php?'.api_get_cidreq(); - $question_description_text = '

'; - if (!empty($description)) { - // Question description. - $question_description_text = "

$description

"; - } + if (empty($quizTitle)) { + Display::addFlash(Display::return_message('ErrorImportingFile'), 'warning'); + api_location($url); + } - // 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); + // Variables + $type = 2; + $random = $active = $results = $max_attempt = $expired_time = 0; + // Make sure feedback is enabled (3 to disable), otherwise the fields + // added to the XLS are not shown, which is confusing + $feedback = 0; + + // Quiz object + $exercise = new Exercise(); + $exercise->updateTitle($quizTitle); + $exercise->updateExpiredTime($expired_time); + $exercise->updateType($type); + $exercise->setRandom($random); + $exercise->active = $active; + $exercise->updateResultsDisabled($results); + $exercise->updateAttempts($max_attempt); + $exercise->updateFeedbackType($feedback); + $exercise->updatePropagateNegative($propagateNegative); + $quiz_id = $exercise->save(); + + if ($quiz_id) { + // Import questions. + for ($i = 0; $i < $numberQuestions; $i++) { + // Question name + $questionTitle = $question[$i]; + $myAnswerList = isset($answerList[$i]) ? $answerList[$i] : []; + $description = isset($questionDescriptionList[$i]) ? $questionDescriptionList[$i] : ''; + $categoryId = null; + if (isset($categoryList[$i]) && !empty($categoryList[$i])) { + $categoryName = $categoryList[$i]; + $categoryId = TestCategory::get_category_id_for_title($categoryName, $courseId); + if (empty($categoryId)) { + $category = new TestCategory(); + $category->name = $categoryName; + $categoryId = $category->save(); } + } - /** @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 FILL_IN_BLANKS: - $answer = new FillBlanks(); - $question_description_text = ''; - break; - case MATCHING: - $answer = new Matching(); - break; - case UNIQUE_ANSWER: - default: - $answer = new UniqueAnswer(); - break; - } + $question_description_text = '

'; + if (!empty($description)) { + // Question description. + $question_description_text = "

$description

"; + } - if ($questionTitle != '') { - $question_id = $answer->create_question( - $quiz_id, - $questionTitle, - $question_description_text, - 0, // max score - $answer->type - ); + // 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); + } - if (!empty($categoryId)) { - TestCategory::addCategoryToQuestion( - $categoryId, - $question_id, - $courseId - ); - } + /** @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 FILL_IN_BLANKS: + $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: - 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++; + } + switch ($detectQuestionType) { + case GLOBAL_MULTIPLE_ANSWER: + 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']; } } - 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] : ''; + if ($useCustomScore) { + if ($correct) { + $score = $correctScore; } else { - $comment = isset($feedbackFalseList[$i]) ? $feedbackFalseList[$i] : ''; - $floatVal = (float) $answer_data['extra']; - if (is_numeric($floatVal)) { - $score = $answer_data['extra']; - } + $score = $incorrectScore; } + } - if ($useCustomScore) { + // Fixing scores: + switch ($detectQuestionType) { + case GLOBAL_MULTIPLE_ANSWER: if ($correct) { - $score = $correctScore; + $score = abs($scoreList[$i]); } else { - $score = $incorrectScore; - } - } - - // Fixing scores: - switch ($detectQuestionType) { - case GLOBAL_MULTIPLE_ANSWER: - if ($correct) { - $score = abs($scoreList[$i]); + if (isset($noNegativeScoreList[$i]) && $noNegativeScoreList[$i] == 'x') { + $score = 0; } else { - if (isset($noNegativeScoreList[$i]) && $noNegativeScoreList[$i] == 'x') { - $score = 0; - } else { - $score = -abs($scoreList[$i]); - } + $score = -abs($scoreList[$i]); } - $score /= $numberRightAnswers; - break; - case UNIQUE_ANSWER: - break; - case MULTIPLE_ANSWER: - if (!$correct) { - //$total = $total - $score; - } - break; - } - - $objAnswer->createAnswer( - $answerValue, - $correct, - $comment, - $score, - $id - ); - if ($correct) { - //only add the item marked as correct ( x ) - $total += (float) $score; - } - $id++; + } + $score /= $numberRightAnswers; + break; + case UNIQUE_ANSWER: + break; + case MULTIPLE_ANSWER: + if (!$correct) { + //$total = $total - $score; + } + break; } - $objAnswer->save(); - - $questionObj = Question::read( - $question_id, - $_course + $objAnswer->createAnswer( + $answerValue, + $correct, + $comment, + $score, + $id ); - - if ($questionObj) { - switch ($detectQuestionType) { - case GLOBAL_MULTIPLE_ANSWER: - $questionObj->updateWeighting($globalScore); - break; - case UNIQUE_ANSWER: - case MULTIPLE_ANSWER: - default: - $questionObj->updateWeighting($total); - break; - } - $questionObj->save($exercise); + if ($correct) { + //only add the item marked as correct ( x ) + $total += (float) $score; } - } - 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: - $fillInScoreList = []; - $size = []; - $globalScore = 0; - foreach ($myAnswerList as $data) { - $score = isset($data['extra']) ? $data['extra'] : 0; - $globalScore += $score; - $fillInScoreList[] = $score; - $size[] = 200; + $id++; } - $scoreToString = implode(',', $fillInScoreList); - $sizeToString = implode(',', $size); + $objAnswer->save(); - //

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 + $questionObj = Question::read( + $question_id, + $_course ); - $objAnswer->save(); - - $questionObj = Question::read($question_id, $_course); if ($questionObj) { - $questionObj->updateWeighting($globalScore); + switch ($detectQuestionType) { + case GLOBAL_MULTIPLE_ANSWER: + $questionObj->updateWeighting($globalScore); + break; + case UNIQUE_ANSWER: + case MULTIPLE_ANSWER: + default: + $questionObj->updateWeighting($total); + break; + } $questionObj->save($exercise); } - break; - case MATCHING: - $globalScore = isset($scoreList[$i]) ? $scoreList[$i] : null; - $position = 1; + } + 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: + $fillInScoreList = []; + $size = []; + $globalScore = 0; + foreach ($myAnswerList as $data) { + $score = isset($data['extra']) ? $data['extra'] : 0; + $globalScore += $score; + $fillInScoreList[] = $score; + $size[] = 200; + } - $objAnswer = new Answer($question_id, $courseId); - foreach ($myAnswerList as $data) { - $option = isset($data['extra']) ? $data['extra'] : ''; - $objAnswer->createAnswer($option, 0, '', 0, $position); - $position++; - } + $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 + ); - $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; - } - } - } + $objAnswer->save(); - $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); - } + $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; } } + } - 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 { - // header('location: exercise.php?' . api_get_cidreq()); - echo ''; + $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); + } } /** diff --git a/main/inc/lib/exercise.lib.php b/main/inc/lib/exercise.lib.php index 01cd3e10d6..8bb8cde21f 100644 --- a/main/inc/lib/exercise.lib.php +++ b/main/inc/lib/exercise.lib.php @@ -6141,7 +6141,7 @@ EOT; if (empty($emailList)) { continue; } - $attempts = $attemptData['attempts']; + $attempts = isset($attemptData['attempts']) ? $attemptData['attempts'] : []; foreach ($attempts as $attempt) { $sendMessage = false; if (isset($attempt['attempt']) && $attemptCountToSend !== (int) $attempt['attempt']) {