* @version $Id: exercise.lib.php 22247 2009-07-20 15:57:25Z ivantcholakov $ */ // The initialization class for the online editor is needed here. require_once '../inc/lib/fckeditor/fckeditor.php'; /** * @param int question id * @param boolean only answers * @param boolean origin i.e = learnpath * @param int current item from the list of questions * @param int number of total questions * */ function showQuestion($questionId, $onlyAnswers=false, $origin=false,$current_item, $total_item) { //change false to true in the following line to enable answer hinting $debug_mark_answer = api_is_allowed_to_edit() && false; if (!ereg("MSIE", $_SERVER["HTTP_USER_AGENT"])) { //echo ''; //echo ''; } // reads question informations if (!$objQuestionTmp = Question::read($questionId)) { // question not found return false; } $answerType=$objQuestionTmp->selectType(); $pictureName=$objQuestionTmp->selectPicture(); if ($answerType != HOT_SPOT) { // Question is not of type hotspot if (!$onlyAnswers) { $questionName=$objQuestionTmp->selectTitle(); $questionDescription=$objQuestionTmp->selectDescription(); $questionName=api_parse_tex($questionName); $s="
".get_lang('Question').' '; $s.=$current_item; //@todo I need the get the feedback type //if($answerType != 1) //$s.=' / '.$total_item; echo $s; echo ' : '; echo $questionName.'
'; $s=''; $s.="
"; $questionDescription=api_parse_tex($questionDescription); $s.=$questionDescription; $s.="
"; if (!empty($pictureName)) { $s.=" "; } } $s.= ''; if (!ereg("MSIE",$_SERVER["HTTP_USER_AGENT"])) { $s .= '
'; } else { $option_ie="margin-left:10px"; } $s .= ''; // construction of the Answer object (also gets all answers details) $objAnswerTmp=new Answer($questionId); $nbrAnswers=$objAnswerTmp->selectNbrAnswers(); // For "matching" type here, we need something a little bit special // because the match between the suggestions and the answers cannot be // done easily (suggestions and answers are in the same table), so we // have to go through answers first (elems with "correct" value to 0). $select_items = array(); //This will contain the number of answers on the left side. We call them // suggestions here, for the sake of comprehensions, while the ones // on the right side are called answers $num_suggestions = 0; if ($answerType == MATCHING) { $x = 1; //iterate through answers $letter = 'A'; //mark letters for each answer $answer_matching = $cpt1 = array(); $answer_suggestions = $nbrAnswers; for ($answerId=1;$answerId <= $nbrAnswers;$answerId++) { $answerCorrect = $objAnswerTmp->isCorrect($answerId); $numAnswer = $objAnswerTmp->selectAutoId($answerId); $answer=$objAnswerTmp->selectAnswer($answerId); if ($answerCorrect==0) { // options (A, B, C, ...) that will be put into the list-box // have the "correct" field set to 0 because they are answer $cpt1[$x] = $letter; $answer_matching[$x]=$objAnswerTmp->selectAnswerByAutoId($numAnswer); $x++; $letter++; } } $i = 1; foreach ($answer_matching as $id => $value) { $select_items[$i]['id'] = $value['id']; $select_items[$i]['letter'] = $cpt1[$id]; $select_items[$i]['answer'] = $value['answer']; $i ++; } $num_suggestions = ($nbrAnswers - $x) + 1; } elseif ($answerType == FREE_ANSWER) { #$comment = $objAnswerTmp->selectComment(1); // $oFCKeditor = new FCKeditor("choice[".$questionId."]") ; $oFCKeditor->ToolbarSet = 'TestFreeAnswer'; $oFCKeditor->Width = '100%'; $oFCKeditor->Height = '200'; $oFCKeditor->Value = '' ; $s .= '"; //$s.=""; } // Now navigate through the possible answers, using the max number of // answers for the question as a limiter $lines_count=1; // a counter for matching-type answers for ($answerId=1;$answerId <= $nbrAnswers;$answerId++) { $answer = $objAnswerTmp->selectAnswer($answerId); $answerCorrect = $objAnswerTmp->isCorrect($answerId); $numAnswer = $objAnswerTmp->selectAutoId($answerId); if ($answerType == FILL_IN_BLANKS) { // splits text and weightings that are joined with the character '::' list($answer) = explode('::',$answer); // because [] is parsed here we follow this procedure: // 1. find everything between the [tex] and [/tex] tags $startlocations = api_strpos($answer,'[tex]'); $endlocations = api_strpos($answer,'[/tex]'); if ($startlocations !== false && $endlocations !== false) { $texstring = api_substr($answer,$startlocations,$endlocations-$startlocations+6); // 2. replace this by {texcode} $answer = str_replace($texstring,'{texcode}',$answer); } // 3. do the normal matching parsing // replaces [blank] by an input field //getting the matches $answer = api_ereg_replace('\[[^]]+\]','',($answer)); // Change input size /* preg_match_all('/\[[^]]+]/',$answer,$matches); $answer=ereg_replace('\[[^]]+\]','',($answer)); // 4. resize the input foreach($matches[0] as $match) { $answer_len = strlen($match)-2; //we will only replace 1 item // echo implode("replace term", explode("search term", "input", $limit)); if ($answer_len <= 5) { $answer = (implode("5", explode("@@", $answer, 2))); } elseif($answer_len <= 10) { $answer = (implode("10", explode("@@", $answer, 2))); } elseif($answer_len <= 20) { $answer = (implode("20", explode("@@", $answer, 2))); } elseif($answer_len <= 30) { $answer = (implode("30", explode("@@", $answer, 2))); } elseif($answer_len <= 40) { $answer = (implode("45", explode("@@", $answer, 2))); } elseif($answer_len <= 50) { $answer = (implode("60", explode("@@", $answer, 2))); } elseif($answer_len <= 60) { $answer = (implode("70", explode("@@", $answer, 2))); } elseif($answer_len <= 70) { $answer = (implode("80", explode("@@", $answer, 2))); } elseif($answer_len <= 80) { $answer = (implode("90", explode("@@", $answer, 2))); } elseif($answer_len <= 90) { $answer = (implode("100", explode("@@", $answer, 2))); } elseif($answer_len <= 100) { $answer = (implode("110", explode("@@", $answer, 2))); } elseif($answer_len > 100 ) { $answer = (implode("120", explode("@@", $answer, 2))); } } */ // 5. replace the {texcode by the api_pare_tex parsed code} $texstring = api_parse_tex($texstring); $answer=str_replace("{texcode}",$texstring,$answer); } // unique answer if ($answerType == UNIQUE_ANSWER) { // set $debug_mark_answer to true at function start to // show the correct answer with a suffix '-x' $help = $selected = ''; if ($debug_mark_answer==true) { if ($answerCorrect) { $help = 'x-'; $selected = 'checked="checked"'; } } $s .= ''; } elseif ($answerType == MULTIPLE_ANSWER) { // multiple answers // set $debug_mark_answer to true at function start to // show the correct answer with a suffix '-x' $help = $selected = ''; if ($debug_mark_answer==true) { if ($answerCorrect) { $help = 'x-'; $selected = 'checked="checked"'; } } $s .= ''; } elseif ($answerType == MULTIPLE_ANSWER_COMBINATION) { // multiple answers // set $debug_mark_answer to true at function start to // show the correct answer with a suffix '-x' $help = $selected = ''; if ($debug_mark_answer==true) { if ($answerCorrect) { $help = 'x-'; $selected = 'checked="checked"'; } } $s .= ''; } elseif ($answerType == FILL_IN_BLANKS) { // fill in blanks $s .= ''; } else { // matching type, showing suggestions and answers // TODO: replace $answerId by $numAnswer if ($answerCorrect != 0) { // only show elements to be answered (not the contents of // the select boxes, who are corrrect = 0) $s .= ''; //middle part (matches selects) $s .= ''; //print_r($select_items); //right part (answers) $s.=''; $s .= ''; $lines_count++; //if the left side of the "matching" has been completely // shown but the right side still has values to show... if (($lines_count -1) == $num_suggestions) { // if it remains answers to shown at the right side while (isset($select_items[$lines_count])) { $s .= '"; $lines_count++; } // end while() } // end if() } } } // end for() if (!ereg("MSIE", $_SERVER["HTTP_USER_AGENT"])) { $s .= '
'.$oFCKeditor->CreateHtml()."

'; $answer = api_parse_tex($answer); $s .= Security::remove_XSS($answer, STUDENT); $s .= '

'; $answer = api_parse_tex($answer); $s .= Security::remove_XSS($answer, STUDENT); $s .= '

'; $answer = api_parse_tex($answer); $s .= Security::remove_XSS($answer, STUDENT); $s .= '
'.$answer.'
'; $parsed_answer = api_parse_tex($answer); //left part questions $s .= ''.$lines_count.' '.$parsed_answer.'     '; if (isset($select_items[$lines_count])) { $s.=''.$select_items[$lines_count]['letter'].'.'. ''.$select_items[$lines_count]['answer'].''; } else { $s.=' '; } $s .= '
  '; $s.=''.$select_items[$lines_count]['letter'].'. '.$select_items[$lines_count]['answer']; $s.="
'; } $s .= '

'; // destruction of the Answer object unset($objAnswerTmp); // destruction of the Question object unset($objQuestionTmp); if ($origin != 'export') { echo $s; } else { return($s); } } elseif ($answerType == HOT_SPOT) { // Question is of type HOT_SPOT $questionName=$objQuestionTmp->selectTitle(); $questionDescription=$objQuestionTmp->selectDescription(); // Get the answers, make a list $objAnswerTmp=new Answer($questionId); $nbrAnswers=$objAnswerTmp->selectNbrAnswers(); // get answers of hotpost $answers_hotspot = array(); for ($answerId=1;$answerId <= $nbrAnswers;$answerId++) { $answers = $objAnswerTmp->selectAnswerByAutoId($objAnswerTmp->selectAutoId($answerId)); $answers_hotspot[$answers['id']] = $objAnswerTmp->selectAnswer($answerId); } // display answers of hotpost order by id $answer_list = '
'.get_lang('HotspotZones').'
'; if (!empty($answers_hotspot)) { ksort($answers_hotspot); foreach ($answers_hotspot as $key => $value) { $answer_list .= '
'.$key.'.- '.$value.'

'; } } $answer_list .= '
'; if (!$onlyAnswers) { echo '
'.get_lang('Question').' '.$current_item.' : '.$questionName.'
'; //@todo I need to the get the feedback type //if($answerType == 2) // $s.=' / '.$total_item; echo ''; echo ''; } $canClick = isset($_GET['editQuestion']) ? '0' : (isset($_GET['modifyAnswers']) ? '0' : '1'); //$tes = isset($_GET['modifyAnswers']) ? '0' : '1'; //echo $tes; $s .= ''; $s .= ''; echo $s; } echo '
'; echo $questionDescription=api_parse_tex($questionDescription); echo '
'.$answer_list.'

'; return $nbrAnswers; }