selectTitle();
$answerType = $objQuestion->selectType();
$pictureName = $objQuestion->selectPicture();
$debug = 0; // debug variable to get where we are
$okPicture = empty($pictureName) ? false : true;
// if we come from the warning box "this question is used in serveral exercises"
if ($modifyIn) {
if ($debug > 0) {
echo '$modifyIn was set' . "
\n";
}
// if the user has chosed to modify the question only in the current exercise
if ($modifyIn == 'thisExercise') {
// duplicates the question
$questionId = $objQuestion->duplicate();
// deletes the old question
$objQuestion->delete($exerciseId);
// removes the old question ID from the question list of the Exercise object
$objExercise->removeFromList($modifyAnswers);
// adds the new question ID into the question list of the Exercise object
$objExercise->addToList($questionId);
// construction of the duplicated Question
$objQuestion = Question::read($questionId);
// adds the exercise ID into the exercise list of the Question object
$objQuestion->addToList($exerciseId);
// copies answers from $modifyAnswers to $questionId
$objAnswer->duplicate($questionId);
// construction of the duplicated Answers
$objAnswer = new Answer($questionId, null, $objExercise);
}
if ($answerType == UNIQUE_ANSWER || $answerType == MULTIPLE_ANSWER || $answerType == MULTIPLE_ANSWER_COMBINATION || $answerType == GLOBAL_MULTIPLE_ANSWER) {
$correct = unserialize($correct);
$reponse = unserialize($reponse);
$comment = unserialize($comment);
$weighting = unserialize($weighting);
} elseif ($answerType == MATCHING) {
$option = unserialize($option);
$match = unserialize($match);
$sel = unserialize($sel);
$weighting = unserialize($weighting);
} elseif ($answerType == FREE_ANSWER) {
$reponse = unserialize($reponse);
$comment = unserialize($comment);
$free_comment = $comment;
$weighting = unserialize($weighting);
} elseif ($answerType == ORAL_EXPRESSION) {
$reponse = unserialize($reponse);
$comment = unserialize($comment);
$free_comment = $comment;
$weighting = unserialize($weighting);
} elseif ($answerType == HOT_SPOT || $answerType == HOT_SPOT_ORDER || $answerType == HOT_SPOT_DELINEATION) {
$color = unserialize($color);
$reponse = unserialize($reponse);
$comment = unserialize($comment);
$weighting = unserialize($weighting);
$hotspot_coordinates = unserialize($hotspot_coordinates);
$hotspot_type = unserialize($hotspot_type);
} else {
//fill in blanks
$reponse = unserialize($reponse);
$comment = unserialize($comment);
$blanks = unserialize($blanks);
$weighting = unserialize($weighting);
}
unset($buttonBack);
}
// the answer form has been submitted
if ($submitAnswers || $buttonBack) {
if ($debug > 0) {
echo '$submitAnswers or $buttonBack was set' . "
\n";
}
if ($answerType == UNIQUE_ANSWER || $answerType == MULTIPLE_ANSWER || $answerType == MULTIPLE_ANSWER_COMBINATION || $answerType == GLOBAL_MULTIPLE_ANSWER) {
if ($debug > 0) {
echo ' $answerType is UNIQUE_ANSWER or MULTIPLE_ANSWER' . "
\n";
}
$questionWeighting = $nbrGoodAnswers = 0;
for ($i = 1; $i <= $nbrAnswers; $i++) {
$reponse[$i] = trim($reponse[$i]);
$comment[$i] = trim($comment[$i]);
$weighting[$i] = intval($weighting[$i]);
if ($answerType == UNIQUE_ANSWER) {
if ($debug > 0) {
echo str_repeat(' ', 4) . '$answerType is UNIQUE_ANSWER' . "
\n";
}
$goodAnswer = ($correct == $i) ? 1 : 0;
} else {
$goodAnswer = $correct[$i];
}
if ($goodAnswer) {
$nbrGoodAnswers++;
// a good answer can't have a negative weighting
$weighting[$i] = abs($weighting[$i]);
// calculates the sum of answer weighting only if it is different from 0 and the answer is good
if ($weighting[$i]) {
$questionWeighting+=$weighting[$i];
}
} elseif ($answerType == MULTIPLE_ANSWER || $answerType == GLOBAL_MULTIPLE_ANSWER) {
if ($debug > 0) {
echo str_repeat(' ', 4) . '$answerType is MULTIPLE_ANSWER' . "
\n";
}
// a bad answer can't have a positive weighting
$weighting[$i] = 0 - abs($weighting[$i]);
}
// checks if field is empty
if (empty($reponse[$i]) && $reponse[$i] != '0') {
$msgErr = get_lang('GiveAnswers');
// clears answers already recorded into the Answer object
$objAnswer->cancel();
break;
} else {
// adds the answer into the object
$objAnswer->createAnswer($reponse[$i], $goodAnswer, $comment[$i], $weighting[$i], $i);
$mainurl = $urlMainExercise."admin.php";
?>
cancel();
}
// checks if the question is used in several exercises
elseif ($exerciseId && !$modifyIn && $objQuestion->selectNbrExercises() > 1) {
$usedInSeveralExercises = 1;
} else {
// saves the answers into the data base
$objAnswer->save();
// sets the total weighting of the question
$objQuestion->updateWeighting($questionWeighting);
$objQuestion->save($exerciseId);
$editQuestion = $questionId;
unset($modifyAnswers);
}
}
} elseif ($answerType == FILL_IN_BLANKS) {
if ($debug > 0) {
echo str_repeat(' ', 2) . '$answerType is FILL_IN_BLANKS' . "
\n";
}
$reponse = trim($reponse);
if (!$buttonBack) {
if ($debug > 0) {
echo str_repeat(' ', 4) . '$buttonBack is not set' . "
\n";
}
if ($setWeighting) {
$blanks = unserialize($blanks);
// checks if the question is used in several exercises
if ($exerciseId && !$modifyIn && $objQuestion->selectNbrExercises() > 1) {
$usedInSeveralExercises = 1;
} else {
// separates text and weightings by '::'
$reponse.='::';
$questionWeighting = 0;
foreach ($weighting as $val) {
// a blank can't have a negative weighting
$val = abs($val);
$questionWeighting+=$val;
// adds blank weighting at the end of the text
$reponse.=$val . ',';
}
$reponse = api_substr($reponse, 0, -1);
$objAnswer->createAnswer($reponse, 0, '', 0, '');
$objAnswer->save();
//added
$mainurl = $urlMainExercise."admin.php";
?>
updateWeighting($questionWeighting);
$objQuestion->save($exerciseId);
$editQuestion = $questionId;
unset($modifyAnswers);
}
}
// if no text has been typed or the text contains no blank
elseif (empty($reponse)) {
$msgErr = get_lang('GiveText');
} elseif (!api_ereg('\[.+\]', $reponse)) {
$msgErr = get_lang('DefineBlanks');
} else {
// now we're going to give a weighting to each blank
$setWeighting = 1;
unset($submitAnswers);
// removes character '::' possibly inserted by the user in the text
$reponse = str_replace('::', '', $reponse);
// we save the answer because it will be modified
//$temp=$reponse;
$temp = $reponse;
/* // Deprecated code.
// 1. find everything between the [tex] and [/tex] tags
$startlocations=api_strpos($temp,'[tex]');
$endlocations=api_strpos($temp,'[/tex]');
if($startlocations !== false && $endlocations !== false)
{
$texstring=api_substr($temp,$startlocations,$endlocations-$startlocations+6);
// 2. replace this by {texcode}
$temp=str_replace($texstring,"{texcode}",$temp);
}
*/
// blanks will be put into an array
$blanks = array();
$i = 1;
// the loop will stop at the end of the text
while (1) {
// quits the loop if there are no more blanks
if (($pos = api_strpos($temp, '[')) === false) {
break;
}
// removes characters till '['
$temp = api_substr($temp, $pos + 1);
// quits the loop if there are no more blanks
if (($pos = api_strpos($temp, ']')) === false) {
break;
}
// stores the found blank into the array
$blanks[$i++] = api_substr($temp, 0, $pos);
// removes the character ']'
$temp = api_substr($temp, $pos + 1);
}
}
} else {
unset($setWeighting);
}
} elseif ($answerType == FREE_ANSWER) {
if ($debug > 0) {
echo str_repeat(' ', 2) . '$answerType is FREE_ANSWER' . "
\n";
}
if (empty($free_comment)) {
$free_comment = $_POST['comment'];
}
if (empty($weighting)) {
$weighting = $_POST['weighting'];
$weightingtemp = $_POST['weighting'];
}
if (!$buttonBack) {
if ($debug > 0) {
echo str_repeat(' ', 4) . '$buttonBack is not set' . "
\n";
}
if ($setWeighting) {
if ($debug > 0) {
echo str_repeat(' ', 6) . '$setWeighting is set' . "
\n";
}
// checks if the question is used in several exercises
if ($exerciseId && !$modifyIn && $objQuestion->selectNbrExercises() > 1) {
$usedInSeveralExercises = 1;
} else {
$objAnswer->createAnswer('', 0, $free_comment, $weighting, '');
$objAnswer->save();
// sets the total weighting of the question
$objQuestion->updateWeighting($weighting);
$objQuestion->save($exerciseId);
$editQuestion = $questionId;
unset($modifyAnswers); //added
$mainurl = $urlMainExercise."admin.php";
?>
0) {
echo str_repeat(' ', 6) . '$free_comment is empty' . "
\n";
}
$msgErr = get_lang('GiveText');
}
/* elseif(!ereg('\[.+\]',$reponse))
{
$msgErr=get_lang('DefineBlanks');
} */ else {
if ($debug > 0) {
echo str_repeat(' ', 6) . '$setWeighting is not set and $free_comment is not empty' . "
\n";
}
// now we're going to give a weighting to each blank
$setWeighting = 1;
unset($submitAnswers);
}
} else {
unset($setWeighting);
}
} elseif ($answerType == MATCHING) {
if ($debug > 0) {
echo str_repeat(' ', 2) . '$answerType is MATCHING' . "
\n";
}
for ($i = 1; $i <= $nbrOptions; $i++) {
$option[$i] = trim($option[$i]);
// checks if field is empty
if (empty($option[$i]) && $option[$i] != '0') {
$msgErr = get_lang('FillLists');
// clears options already recorded into the Answer object
$objAnswer->cancel();
break;
} else {
// adds the option into the object
$objAnswer->createAnswer($option[$i], 0, '', 0, $i);
}
}
$questionWeighting = 0;
if (empty($msgErr)) {
for ($j = 1; $j <= $nbrMatches; $i++, $j++) {
$match[$i] = trim($match[$i]);
$weighting[$i] = abs(intval($weighting[$i]));
$questionWeighting+=$weighting[$i];
// checks if field is empty
if (empty($match[$i]) && $match[$i] != '0') {
$msgErr = get_lang('FillLists');
// clears matches already recorded into the Answer object
$objAnswer->cancel();
break;
}
// check if correct number
else {
// adds the answer into the object
$objAnswer->createAnswer($match[$i], $sel[$i], '', $weighting[$i], $i);
$mainurl = $urlMainExercise."admin.php";
?>
selectNbrExercises() > 1) {
$usedInSeveralExercises = 1;
} else {
// all answers have been recorded, so we save them into the data base
$objAnswer->save();
// sets the total weighting of the question
$objQuestion->updateWeighting($questionWeighting);
$objQuestion->save($exerciseId);
$editQuestion = $questionId;
unset($modifyAnswers);
}
}
} elseif ($answerType == HOT_SPOT || $answerType == HOT_SPOT_ORDER) {
$questionWeighting = $nbrGoodAnswers = 0;
for ($i = 1; $i <= $nbrAnswers; $i++) {
if ($debug > 0) {
echo str_repeat(' ', 4) . '$answerType is HOT_SPOT' . "
\n";
}
$reponse[$i] = trim($reponse[$i]);
$comment[$i] = trim($comment[$i]);
$weighting[$i] = intval($weighting[$i]);
// checks if field is empty
if (empty($reponse[$i]) && $reponse[$i] != '0') {
$msgErr = get_lang('HotspotGiveAnswers');
// clears answers already recorded into the Answer object
$objAnswer->cancel();
break;
}
if ($weighting[$i] <= 0) {
$msgErr = get_lang('HotspotWeightingError');
// clears answers already recorded into the Answer object
$objAnswer->cancel();
break;
}
if ($hotspot_coordinates[$i] == '0;0|0|0' || empty($hotspot_coordinates[$i])) {
$msgErr = get_lang('HotspotNotDrawn');
// clears answers already recorded into the Answer object
$objAnswer->cancel();
break;
}
} // end for()
if (empty($msgErr)) {
if ($exerciseId && !$modifyIn && $objQuestion->selectNbrExercises() > 1) {
$usedInSeveralExercises = 1;
} else {
for ($i = 1; $i <= $nbrAnswers; $i++) {
if ($debug > 0) {
echo str_repeat(' ', 4) . '$answerType is HOT_SPOT' . "
\n";
}
$reponse[$i] = trim($reponse[$i]);
$comment[$i] = trim($comment[$i]);
$weighting[$i] = intval($weighting[$i]);
if ($weighting[$i]) {
$questionWeighting+=$weighting[$i];
}
// creates answer
$objAnswer->createAnswer($reponse[$i], '', $comment[$i], $weighting[$i], $i, $hotspot_coordinates[$i], $hotspot_type[$i]);
} // end for()
// saves the answers into the data base
$objAnswer->save();
// sets the total weighting of the question
$objQuestion->updateWeighting($questionWeighting);
$objQuestion->save($exerciseId);
$editQuestion = $questionId;
unset($modifyAnswers);
}
}
}
if ($debug > 0) {
echo '$modifyIn was set - end' . "
\n";
}
}
if ($modifyAnswers) {
if ($debug > 0) {
echo str_repeat(' ', 0) . '$modifyAnswers is set' . "
\n";
}
// construction of the Answer object
$objAnswer = new Answer($questionId, null, $objExercise);
Session::write('objAnswer', $objAnswer);
if ($answerType == UNIQUE_ANSWER || $answerType == MULTIPLE_ANSWER || $answerType == GLOBAL_MULTIPLE_ANSWER) {
if ($debug > 0) {
echo str_repeat(' ', 2) . '$answerType is UNIQUE_ANSWER or MULTIPLE_ANSWER' . "
\n";
}
if (!$nbrAnswers) {
$nbrAnswers = $objAnswer->selectNbrAnswers();
$reponse = array();
$comment = array();
$weighting = array();
// initializing + Modification de la ligne suivante
if ($answerType == MULTIPLE_ANSWER || $answerType == GLOBAL_MULTIPLE_ANSWER) {
$correct = array();
} else {
$correct = 0;
}
for ($i = 1; $i <= $nbrAnswers; $i++) {
$reponse[$i] = $objAnswer->selectAnswer($i);
$comment[$i] = $objAnswer->selectComment($i);
$weighting[$i] = $objAnswer->selectWeighting($i);
if ($answerType == MULTIPLE_ANSWER || $answerType == GLOBAL_MULTIPLE_ANSWER) {
$correct[$i] = $objAnswer->isCorrect($i);
} elseif ($objAnswer->isCorrect($i)) {
$correct = $i;
}
}
}
if ($lessAnswers) {
$nbrAnswers--;
}
if ($moreAnswers) {
$nbrAnswers++;
}
// minimum 2 answers
if ($nbrAnswers < 2) {
$nbrAnswers = 2;
}
} elseif ($answerType == FILL_IN_BLANKS) {
if ($debug > 0) {
echo str_repeat(' ', 2) . '$answerType is FILL_IN_BLANKS' . "
\n";
}
if (!$submitAnswers && !$buttonBack) {
if (!$setWeighting) {
$reponse = $objAnswer->selectAnswer(1);
list($reponse, $weighting) = explode('::', $reponse);
$weighting = explode(',', $weighting);
$temp = array();
// keys of the array go from 1 to N and not from 0 to N-1
for ($i = 0; $i < sizeof($weighting); $i++) {
$temp[$i + 1] = $weighting[$i];
}
$weighting = $temp;
} elseif (!$modifyIn) {
$weighting = unserialize($weighting);
}
}
} elseif ($answerType == FREE_ANSWER) {
if ($debug > 0) {
echo str_repeat(' ', 2) . '$answerType is FREE_ANSWER' . "
\n";
}
if (!$submitAnswers && !$buttonBack) {
if ($debug > 0) {
echo str_repeat(' ', 4) . '$submitAnswers && $buttonsBack are unset' . "
\n";
}
if (!$setWeighting) {
if ($debug > 0) {
echo str_repeat(' ', 6) . '$setWeighting is unset' . "
\n";
}
//YW: not quite sure about whether the comment has already been recovered,
// but as we have passed into the submitAnswers loop, this should be in the
// objAnswer object.
$free_comment = $objAnswer->selectComment(1);
$weighting = $objAnswer->selectWeighting(1); //added
} elseif (!$modifyIn) {
if ($debug > 0) {
echo str_repeat(' ', 6) . '$setWeighting is set and $modifyIn is unset' . "
\n";
}
$weighting = unserialize($weighting);
}
}
} elseif ($answerType == MATCHING) {
if ($debug > 0) {
echo str_repeat(' ', 2) . '$answerType is MATCHING' . "
\n";
}
if (!$nbrOptions || !$nbrMatches) {
$option = array();
$match = array();
$sel = array();
$nbrOptions = $nbrMatches = 0;
// fills arrays with data from de data base
for ($i = 1; $i <= $objAnswer->selectNbrAnswers(); $i++) {
// it is a match
if ($objAnswer->isCorrect($i)) {
$match[$i] = $objAnswer->selectAnswer($i);
$sel[$i] = $objAnswer->isCorrect($i);
$weighting[$i] = $objAnswer->selectWeighting($i);
$nbrMatches++;
}
// it is an option
else {
$option[$i] = $objAnswer->selectAnswer($i);
$nbrOptions++;
}
}
}
if ($lessOptions) {
// keeps the correct sequence of array keys when removing an option from the list
for ($i = $nbrOptions + 1, $j = 1; $nbrOptions > 2 && $j <= $nbrMatches; $i++, $j++) {
$match[$i - 1] = $match[$i];
$sel[$i - 1] = $sel[$i];
$weighting[$i - 1] = $weighting[$i];
}
unset($match[$i - 1]);
unset($sel[$i - 1]);
$nbrOptions--;
}
if ($moreOptions) {
// keeps the correct sequence of array keys when adding an option into the list
for ($i = $nbrMatches + $nbrOptions; $i > $nbrOptions; $i--) {
$match[$i + 1] = $match[$i];
$sel[$i + 1] = $sel[$i];
$weighting[$i + 1] = $weighting[$i];
}
unset($match[$i + 1]);
unset($sel[$i + 1]);
$nbrOptions++;
}
if ($lessMatches) {
$nbrMatches--;
}
if ($moreMatches) {
$nbrMatches++;
}
// minimum 2 options
if ($nbrOptions < 2) {
$nbrOptions = 2;
}
// minimum 2 matches
if ($nbrMatches < 2) {
$nbrMatches = 2;
}
} elseif ($answerType == HOT_SPOT || $answerType == HOT_SPOT_ORDER) {
if ($debug > 0) {
echo str_repeat(' ', 2) . '$answerType is HOT_SPOT' . "
\n";
}
if (!$nbrAnswers) {
$nbrAnswers = $objAnswer->selectNbrAnswers();
$reponse = array();
$comment = array();
$weighting = array();
$hotspot_coordinates = array();
$hotspot_type = array();
for ($i = 1; $i <= $nbrAnswers; $i++) {
$reponse[$i] = $objAnswer->selectAnswer($i);
$comment[$i] = $objAnswer->selectComment($i);
$weighting[$i] = $objAnswer->selectWeighting($i);
$hotspot_coordinates[$i] = $objAnswer->selectHotspotCoordinates($i);
$hotspot_type[$i] = $objAnswer->selectHotspotType($i);
}
}
$_SESSION['tmp_answers'] = array();
$_SESSION['tmp_answers']['answer'] = $reponse;
$_SESSION['tmp_answers']['comment'] = $comment;
$_SESSION['tmp_answers']['weighting'] = $weighting;
$_SESSION['tmp_answers']['hotspot_coordinates'] = $hotspot_coordinates;
$_SESSION['tmp_answers']['hotspot_type'] = $hotspot_type;
if ($lessAnswers) {
// At least 1 answer
if ($nbrAnswers > 1) {
$nbrAnswers--;
// Remove the last answer
$tmp = array_pop($_SESSION['tmp_answers']['answer']);
$tmp = array_pop($_SESSION['tmp_answers']['comment']);
$tmp = array_pop($_SESSION['tmp_answers']['weighting']);
$tmp = array_pop($_SESSION['tmp_answers']['hotspot_coordinates']);
$tmp = array_pop($_SESSION['tmp_answers']['hotspot_type']);
} else {
$msgErr = get_lang('MinHotspot');
}
}
if ($moreAnswers) {
if ($nbrAnswers < 12) {
$nbrAnswers++;
// Add a new answer
$_SESSION['tmp_answers']['answer'][] = '';
$_SESSION['tmp_answers']['comment'][] = '';
$_SESSION['tmp_answers']['weighting'][] = '1';
$_SESSION['tmp_answers']['hotspot_coordinates'][] = '0;0|0|0';
$_SESSION['tmp_answers']['hotspot_type'][] = 'square';
} else {
$msgErr = get_lang('MaxHotspot');
}
}
}
if (!$usedInSeveralExercises) {
if ($debug > 0) {
echo str_repeat(' ', 2) . '$usedInSeveralExercises is untrue' . "
\n";
}
if ($answerType == UNIQUE_ANSWER || $answerType == MULTIPLE_ANSWER || $answerType == MULTIPLE_ANSWER_COMBINATION || $answerType == GLOBAL_MULTIPLE_ANSWER) {
if ($debug > 0) {
echo str_repeat(' ', 4) . '$answerType is UNIQUE_ANSWER or MULTIPLE_ANSWER' . "
\n";
}
?>
0) {
echo str_repeat(' ', 4) . '$answerType is FILL_IN_BLANKS' . "
\n";
}
?>
0) {
echo str_repeat(' ', 4) . '$answerType is FREE_ANSWER' . "
\n";
}
?>
0) {
echo str_repeat(' ', 4) . '$answerType is HOT_SPOT' . "
\n";
}
$hotspot_colors = array("", // $i starts from 1 on next loop (ugly fix)
"#4271B5",
"#FE8E16",
"#45C7F0",
"#BCD631",
"#D63173",
"#D7D7D7",
"#90AFDD",
"#AF8640",
"#4F9242",
"#F4EB24",
"#ED2024",
"#3B3B3B",
"#F7BDE2");
?>
0) {
echo str_repeat(' ', 0) . '$modifyAnswers was set - end' . "
\n";
}
}