we delete the objExercise from the session in order to get the latest changes in the exercise */ if (api_is_allowed_to_edit(null, true) && isset($_GET['preview']) && $_GET['preview'] == 1) { Session::erase('objExercise'); } // 1. Loading the $objExercise variable if (!isset($_SESSION['objExercise']) || $_SESSION['objExercise']->id != $_REQUEST['exerciseId']) { // Construction of Exercise $objExercise = new Exercise(); Session::write('firstTime', true); if ($debug) { error_log('1. Setting the $objExercise variable'); } unset($_SESSION['questionList']); // if the specified exercise doesn't exist or is disabled if (!$objExercise->read($exerciseId) || !$objExercise->selectStatus() && !$is_allowedToEdit && $origin != 'learnpath') { if ($debug) { error_log('1.1. Error while reading the exercise'); } unset($objExercise); $error = get_lang('ExerciseNotFound'); } else { // Saves the object into the session Session::write('objExercise', $objExercise); if ($debug) { error_log('1.1. $_SESSION[objExercise] was unset - set now - end'); } } } else { Session::write('firstTime', false); }
/** * This function exports the given Chamilo test * @param integer Test ID * @return string The test itself as an HTML string */ function export_exercise($item_id) { global $expdir, $_course, $_configuration, $_SESSION, $_SERVER, $language_interface, $langExerciseNotFound, $langQuestion, $langOk, $origin, $questionNum; $exerciseId = $item_id; require_once '../exercice/exercise.class.php'; require_once '../exercice/question.class.php'; require_once '../exercice/answer.class.php'; $TBL_EXERCISES = Database::get_course_table(TABLE_QUIZ_TEST); /* Clears the exercise session */ if (isset($_SESSION['objExercise'])) { Session::erase('objExercise'); } if (isset($_SESSION['objQuestion'])) { Session::erase('objQuestion'); } if (isset($_SESSION['objAnswer'])) { Session::erase('objAnswer'); } if (isset($_SESSION['questionList'])) { Session::erase('questionList'); } if (isset($_SESSION['exerciseResult'])) { Session::erase('exerciseResult'); } // If the object is not in the session: if (!isset($_SESSION['objExercise'])) { // Construction of Exercise. $objExercise = new Exercise(); $sql = "SELECT title,description,sound,type,random,active FROM {$TBL_EXERCISES} WHERE iid='{$exerciseId}'"; // If the specified exercise doesn't exist or is disabled: if (!$objExercise->read($exerciseId) || !$objExercise->selectStatus() && !api_is_allowed_to_edit() && $origin != 'learnpath') { die($langExerciseNotFound); } // Saves the object into the session. Session::write('objExercise', $objExercise); } $exerciseTitle = $objExercise->selectTitle(); $exerciseDescription = $objExercise->selectDescription(); $exerciseSound = $objExercise->selectSound(); $randomQuestions = $objExercise->isRandom(); $exerciseType = $objExercise->selectType(); if (!isset($_SESSION['questionList'])) { // Selects the list of question ID. $questionList = $randomQuestions ? $objExercise->selectRandomList() : $objExercise->selectQuestionList(); // Saves the question list into the session. Session::write('questionList', $questionList); } $nbrQuestions = sizeof($questionList); // If questionNum comes from POST and not from GET: if (!$questionNum || $_POST['questionNum']) { // Only used for sequential exercises (see $exerciseType). if (!$questionNum) { $questionNum = 1; } else { $questionNum++; } } $test .= "<h3>" . $exerciseTitle . "</h3>"; if (!empty($exerciseSound)) { $test .= "<a href=\"../document/download.php?doc_url=%2Faudio%2F" . $exerciseSound . "\"&SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9 target=\"_blank\"><img src=\"../img/sound.gif\" border=\"0\" align=\"absmiddle\" alt=" . get_lang("Sound") . "\" /></a>"; } // Writing the .js file with to check the correct answers begin. $scriptfilename = "Exercice" . $item_id . ".js"; $s = "<script type=\"text/javascript\" src='../js/" . $scriptfilename . "'></script>"; $test .= $s; $content = "function evaluate() {\n alert('Test evaluated.');\n }\n "; if (!($handle = fopen($expdir . '/js/' . $scriptfilename, 'w'))) { echo "Cannot open file ({$scriptfilename})"; } if (fwrite($handle, $content) === false) { echo "Cannot write to file ({$filename})"; exit; } fclose($handle); // Writing the .js file with to check the correct answers end. $s = "\n <p>{$exerciseDescription}</p>\n <table width='100%' border='0' cellpadding='1' cellspacing='0'>\n <form method='post' action=''><input type=\"hidden\" name=\"SQMSESSID\" value=\"36812c2dea7d8d6e708d5e6a2f09b0b9\" />\n <input type='hidden' name='formSent' value='1' />\n <input type='hidden' name='exerciseType' value='" . $exerciseType . "' />\n <input type='hidden' name='questionNum' value='" . $questionNum . "' />\n <input type='hidden' name='nbrQuestions' value='" . $nbrQuestions . "' />\n <tr>\n <td>\n <table width='100%' cellpadding='4' cellspacing='2' border='0'>"; $exerciseType = 1; // So to list all questions in one page. $test .= $s; $i = 0; foreach ($questionList as $questionId) { $i++; // For sequential exercises. if ($exerciseType == 2) { // If it is not the right question, goes to the next loop iteration. if ($questionNum != $i) { continue; } else { // if the user has already answered this question: if (isset($exerciseResult[$questionId])) { // Construction of the Question object. $objQuestionTmp = new Question(); // Reads question informations. $objQuestionTmp->read($questionId); $questionName = $objQuestionTmp->selectTitle(); // Destruction of the Question object. unset($objQuestionTmp); $test .= '<tr><td>' . get_lang('AlreadyAnswered') . ' "' . $questionName . '"</td></tr>'; break; } } } echo $s = "<tr bgcolor='#e6e6e6'><td valign='top' colspan='2'>" . get_lang('Question') . " "; // Call the showQuestion(). This basically displays the question in a table. $question_obj = Question::read($questionId); $test .= $objExercise->showQuestion($question_obj, false, 'export', $i); } // end foreach() $s = "</table></td></tr><tr><td><br/><input type='button' value='" . $langOk . "' onclick=\"javascript: evaluate(); alert('Evaluated.');\">"; $s .= "</td></tr></form></table>"; $s .= "<script type='text/javascript'> loadPage(); </script>"; $b = 2; $test .= $s; return $test; }
} //Checks if an exercise ID exists in the URL //and if so it gets the exercise object either by the session (if it exists there) //or by initializing it using the exercise ID if (isset($_REQUEST['exerciseId'])) { $exerciseId = intval($_REQUEST['exerciseId']); // Checks if exercise object exists in the Session if (isset($_SESSION['objExercise'][$exerciseId])) { $objExercise = $_SESSION['objExercise'][$exerciseId]; } else { // construction of Exercise $objExercise = new Exercise(); // if the specified exercise is disabled (this only applies to students) // or doesn't exist redirects and shows error if (!$objExercise->read($exerciseId) || (!$is_editor && $objExercise->selectStatus($exerciseId)==0)) { session::Messages($langExerciseNotFound); redirect_to_home_page('modules/exercise/index.php?course='.$course_code); } // saves the object into the session $_SESSION['objExercise'][$exerciseId] = $objExercise; } } else { redirect_to_home_page('modules/exercise/index.php?course='.$course_code); } //Initialize attempts timestamp if(isset($_POST['attempt_value']) && !isset($_GET['eurId'])){ $attempt_value = $_POST['attempt_value']; }elseif (isset($_GET['eurId'])) { //reinitialize paused attempt //If there is a paused attempt get it