From 63f47b7b9b958638275d54d556ce337612600e33 Mon Sep 17 00:00:00 2001 From: Eric Marguin Date: Fri, 5 Jan 2007 14:54:24 +0100 Subject: [PATCH] [svn r10594] major commit : - question.class.php is now an abstract class. hotspot.class.php, uniquer_answer.class.php are extending Question. - when we create / edit a question, the question form and the answers form are now on the same page (except for hotspot) I'm writing a wiki page on the new structure... See http://www.dokeos.com/wiki/index.php/Test_tool_redesign in a few moment. --- main/exercice/admin.php | 61 +-- main/exercice/answer.class.php | 6 +- main/exercice/answer_admin.inc.php | 13 +- main/exercice/exercice_submit.php | 10 +- main/exercice/exercise.lib.php | 7 +- main/exercice/exercise_result.php | 5 +- main/exercice/exercise_show.php | 3 +- main/exercice/feedback.php | 3 +- main/exercice/fill_blanks.class.php | 148 +++++++ main/exercice/freeanswer.class.php | 90 ++++ main/exercice/hotspot.class.php | 92 ++++ main/exercice/hotspot_actionscript.as.php | 3 +- .../hotspot_actionscript_admin.as.php | 3 +- main/exercice/hotspot_admin.inc.php | 393 ++++++++++++++++++ main/exercice/hotspot_answers.as.php | 3 +- main/exercice/mark_free_answer.php | 3 +- main/exercice/matching.class.php | 272 ++++++++++++ main/exercice/multiple_answer.class.php | 192 +++++++++ main/exercice/question.class.php | 146 ++++++- main/exercice/question_admin.inc.php | 327 ++++----------- main/exercice/question_list_admin.inc.php | 9 +- main/exercice/question_pool.php | 11 +- main/exercice/statement_admin.inc.php | 328 --------------- main/exercice/unique_answer.class.php | 198 +++++++++ 24 files changed, 1653 insertions(+), 673 deletions(-) create mode 100644 main/exercice/fill_blanks.class.php create mode 100644 main/exercice/freeanswer.class.php create mode 100644 main/exercice/hotspot.class.php create mode 100644 main/exercice/hotspot_admin.inc.php create mode 100644 main/exercice/matching.class.php create mode 100644 main/exercice/multiple_answer.class.php delete mode 100644 main/exercice/statement_admin.inc.php create mode 100644 main/exercice/unique_answer.class.php diff --git a/main/exercice/admin.php b/main/exercice/admin.php index 747f34cfdf..934b39a6b3 100644 --- a/main/exercice/admin.php +++ b/main/exercice/admin.php @@ -1,4 +1,4 @@ -read($editQuestion)) + if(!$objQuestion = Question::read($editQuestion)) { die(get_lang('QuestionNotFound')); } + // saves the object into the session + api_session_register('objQuestion'); } } @@ -237,11 +226,6 @@ if($editQuestion || $newQuestion || $modifyQuestion || $modifyAnswers) // gets the question ID $questionId=$objQuestion->selectId(); } - // question not found - else - { - die(get_lang('QuestionNotFound')); - } } // if cancelling an exercise @@ -432,7 +416,7 @@ function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision) } // --> "; - +$interbreadcrumb[] = array ("url"=>"exercice.php", "name"=> get_lang('Exercices')); Display::display_header($nameTools,"Exercise"); ?> @@ -441,39 +425,20 @@ Display::display_header($nameTools,"Exercise"); + ?> hotspot_coordinates[$i]=$object->hotspot_coordinates; "`position` = '$position' " . "WHERE `id` =$position " . "AND `question_id` =$questionId"; + api_sql_query($sql,__FILE__,__LINE__); } @@ -418,7 +419,7 @@ $this->hotspot_coordinates[$i]=$object->hotspot_coordinates; $sql="INSERT INTO `$TBL_REPONSES`" . "(id,question_id,answer,correct,comment," . "ponderation,position,hotspot_coordinates,hotspot_type) VALUES"; - + for($i=1;$i <= $this->new_nbrAnswers;$i++) { $answer=addslashes($this->new_answer[$i]); @@ -434,6 +435,7 @@ $this->hotspot_coordinates[$i]=$object->hotspot_coordinates; } $sql = substr($sql,0,-1); + api_sql_query($sql,__FILE__,__LINE__); // moves $new_* arrays diff --git a/main/exercice/answer_admin.inc.php b/main/exercice/answer_admin.inc.php index 94b46fb128..136220662a 100644 --- a/main/exercice/answer_admin.inc.php +++ b/main/exercice/answer_admin.inc.php @@ -1,4 +1,4 @@ -selectTitle(); $answerType=$objQuestion->selectType(); $pictureName=$objQuestion->selectPicture(); + $debug = 0; // debug variable to get where we are $okPicture=empty($pictureName)?false:true; @@ -65,9 +70,7 @@ if($modifyIn) $objExercise->addToList($questionId); // construction of the duplicated Question - $objQuestion=new Question(); - - $objQuestion->read($questionId); + $objQuestion = Question::read($questionId); // adds the exercise ID into the exercise list of the Question object $objQuestion->addToList($exerciseId); @@ -609,6 +612,7 @@ if($modifyAnswers) // construction of the Answer object $objAnswer=new Answer($questionId); + api_session_register('objAnswer'); if($answerType == UNIQUE_ANSWER || $answerType == MULTIPLE_ANSWER) @@ -811,6 +815,7 @@ if($modifyAnswers) if(!$nbrAnswers) { + $nbrAnswers=$objAnswer->selectNbrAnswers(); $reponse=Array(); diff --git a/main/exercice/exercice_submit.php b/main/exercice/exercice_submit.php index 256e4867ce..19587c6dd2 100644 --- a/main/exercice/exercice_submit.php +++ b/main/exercice/exercice_submit.php @@ -1,4 +1,4 @@ -read($questionId); + $objQuestionTmp = Question :: read($questionId); // for sequential exercises if($exerciseType == 2) @@ -519,10 +518,7 @@ foreach($questionList as $questionId) if(isset($exerciseResult[$questionId])) { // construction of the Question object - $objQuestionTmp=new Question(); - - // reads question informations - $objQuestionTmp->read($questionId); + $objQuestionTmp = Question::read($questionId); $questionName=$objQuestionTmp->selectTitle(); diff --git a/main/exercice/exercise.lib.php b/main/exercice/exercise.lib.php index 9ddc561580..dc9c091742 100644 --- a/main/exercice/exercise.lib.php +++ b/main/exercice/exercise.lib.php @@ -1,4 +1,4 @@ -read($questionId)) + if(!$objQuestionTmp = Question::read($questionId)) { // question not found return false; diff --git a/main/exercice/exercise_result.php b/main/exercice/exercise_result.php index ae9bea2448..21b86a92a9 100644 --- a/main/exercice/exercise_result.php +++ b/main/exercice/exercise_result.php @@ -1,4 +1,4 @@ -read($questionId); + $objQuestionTmp = Question :: read($questionId); $questionName=$objQuestionTmp->selectTitle(); $questionWeighting=$objQuestionTmp->selectWeighting(); diff --git a/main/exercice/exercise_show.php b/main/exercice/exercise_show.php index f9a387faff..102314bbef 100644 --- a/main/exercice/exercise_show.php +++ b/main/exercice/exercise_show.php @@ -285,8 +285,7 @@ $result =api_sql_query($query, __FILE__, __LINE__); $k++; $choice=$exerciseResult[$questionId]; // creates a temporary Question object - $objQuestionTmp=new Question(); - $objQuestionTmp->read($questionId); + $objQuestionTmp = Question::read($questionId); $questionName=$objQuestionTmp->selectTitle(); $questionWeighting=$objQuestionTmp->selectWeighting(); $answerType=$objQuestionTmp->selectType(); diff --git a/main/exercice/feedback.php b/main/exercice/feedback.php index 1cd6c3602f..180d869652 100644 --- a/main/exercice/feedback.php +++ b/main/exercice/feedback.php @@ -24,8 +24,7 @@ Display::display_header($nameTools,"Exercise"); read($id); + $objQuestionTmp = Question::read($id); echo "".get_lang('Question')." : "; echo $objQuestionTmp->selectTitle(); echo ""; diff --git a/main/exercice/fill_blanks.class.php b/main/exercice/fill_blanks.class.php new file mode 100644 index 0000000000..c505e12244 --- /dev/null +++ b/main/exercice/fill_blanks.class.php @@ -0,0 +1,148 @@ + type = FILL_IN_BLANKS; + } + + /** + * function which redifines Question::createAnswersForm + * @param the formvalidator instance + */ + function createAnswersForm ($form) { + + // javascript + echo ' + + '; + + + // answer + $form -> addElement ('html', '

'.get_lang('TypeTextBelow').', '.get_lang('And').' '.get_lang('UseTagForBlank').'
'); + $form -> addElement ('textarea', 'answer',get_lang('Answer'),'id="answer" cols="65" rows="6" onkeyup="updateBlanks(this)"'); + $form -> addRule ('answer',get_lang('GiveText'),'required'); + $form -> addRule ('answer',get_lang('DefineBlanks'),'regex','/\[.*\]/'); + + $defaults = array(); + + if(!empty($this->id)) + { + $objAnswer = new answer($this->id); + $defaults['answer'] = $objAnswer->selectAnswer(1); + } + else + { + $defaults['answer'] = get_lang('DefaultTextInBlanks'); + } + + $form -> addElement('html','
'); + + $form -> setDefaults($defaults); + + } + + + /** + * abstract function which creates the form to create / edit the answers of the question + * @param the formvalidator instance + */ + function processAnswersCreation($form) { + + $answer = $form -> getSubmitValue('answer'); + + //remove the :: eventually written by the user + $answer = str_replace('::','',$answer); + + // get the blanks weightings + $nb = preg_match_all('/\[[^\]]*\]/', $answer, $blanks); + if($nb>0) + { + $answer .= '::'; + for($i=0 ; $i<$nb ; ++$i) + { + $answer .= $form -> getSubmitValue('weighting['.$i.']').','; + $this -> weighting += $form -> getSubmitValue('weighting['.$i.']'); + } + $answer = substr($answer,0,-1); + } + + $this -> save(); + + $objAnswer = new answer($this->id); + $objAnswer->createAnswer($answer,0,'',0,''); + $objAnswer->save(); + + echo ''; + + } + +} + +endif; +?> \ No newline at end of file diff --git a/main/exercice/freeanswer.class.php b/main/exercice/freeanswer.class.php new file mode 100644 index 0000000000..303aecfc00 --- /dev/null +++ b/main/exercice/freeanswer.class.php @@ -0,0 +1,90 @@ + type = FREE_ANSWER; + } + + /** + * function which redifines Question::createAnswersForm + * @param the formvalidator instance + */ + function createAnswersForm ($form) { + + $form -> addElement('text','weighting',get_lang('Weighting'),'size="5"'); + if(!empty($this->id)) + { + $form -> setDefaults(array('weighting' => $this->weighting)); + } + else { + $form -> setDefaults(array('weighting' => '1')); + } + + } + + + /** + * abstract function which creates the form to create / edit the answers of the question + * @param the formvalidator instance + */ + function processAnswersCreation($form) { + + $this -> weighting = $form -> getSubmitValue('weighting'); + + $this->save(); + + echo ''; + + } + +} + +endif; +?> \ No newline at end of file diff --git a/main/exercice/hotspot.class.php b/main/exercice/hotspot.class.php new file mode 100644 index 0000000000..b9aba64f16 --- /dev/null +++ b/main/exercice/hotspot.class.php @@ -0,0 +1,92 @@ + type = HOT_SPOT; + } + + function display(){ + + } + + function createForm ($form) { + parent::createForm ($form); + if(!isset($_GET['editQuestion'])) + { + $form->addElement('file','imageUpload'); + $form->addRule('imageUpload', get_lang('OnlyJPG'), 'mimetype','image/jpeg'); + $form->addRule('imageUpload', get_lang('NoImage'), 'uploadedfile'); + } + } + + function processCreation ($form, $objExercise) { + $file_info = $form -> getSubmitValue('imageUpload'); + parent::processCreation ($form, $objExercise); + if(!empty($file_info['tmp_name'])) + { + $this->uploadPicture($file_info['tmp_name'], $file_info['name']); + $this->save(); + } + echo ''; + } + + function createAnswersForm ($form) { + + // nothing + + } + + function processAnswersCreation ($form) { + + // nothing + + } + +} + +endif; +?> \ No newline at end of file diff --git a/main/exercice/hotspot_actionscript.as.php b/main/exercice/hotspot_actionscript.as.php index 9aa3e19d38..7ba8ff74ce 100644 --- a/main/exercice/hotspot_actionscript.as.php +++ b/main/exercice/hotspot_actionscript.as.php @@ -38,8 +38,7 @@ // set vars $questionId = $_GET['modifyAnswers']; - $objQuestion = new Question(); - $objQuestion->read($questionId); + $objQuestion = Question::read($questionId); $TBL_ANSWERS = $_course['dbNameGlu'].'quiz_answer'; diff --git a/main/exercice/hotspot_actionscript_admin.as.php b/main/exercice/hotspot_actionscript_admin.as.php index 0479fe4583..4ed2052af9 100644 --- a/main/exercice/hotspot_actionscript_admin.as.php +++ b/main/exercice/hotspot_actionscript_admin.as.php @@ -38,8 +38,7 @@ // set vars $questionId = $_GET['modifyAnswers']; - $objQuestion = new Question(); - $objQuestion->read($questionId); + $objQuestion = Question::read($questionId); $TBL_ANSWERS = $_course['dbNameGlu'].'quiz_answer'; diff --git a/main/exercice/hotspot_admin.inc.php b/main/exercice/hotspot_admin.inc.php new file mode 100644 index 0000000000..e2c625e67a --- /dev/null +++ b/main/exercice/hotspot_admin.inc.php @@ -0,0 +1,393 @@ +>>>>>>>>>>>>>>>>>>> ANSWER ADMINISTRATION <<<<<<<<<<<<<<<<<<<<*/ +/** +============================================================================== + * This script allows to manage answers + * + * It is included from the script admin.php + * @author Olivier Brouckaert + * @package dokeos.exercise +============================================================================== + */ + +// ALLOWED_TO_INCLUDE is defined in admin.php +if(!defined('ALLOWED_TO_INCLUDE')) +{ + exit(); +} + +$modifyAnswers = $_GET['hotspotadmin']; + +if(!is_object($objQuestion)) +{ + $objQuestion = Question :: read($modifyAnswers); +} + +$questionName=$objQuestion->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); + } + + + $color=unserialize($color); + $reponse=unserialize($reponse); + $comment=unserialize($comment); + $weighting=unserialize($weighting); + $hotspot_coordinates=unserialize($hotspot_coordinates); + $hotspot_type=unserialize($hotspot_type); + + + unset($buttonBack); +} + +// the answer form has been submitted +if($submitAnswers || $buttonBack) +{ + if($debug>0){echo '$submitAnswers or $buttonBack was set'."
\n";} + + $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)) + { + + 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]=addslashes(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); + + echo ''; + + } + 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($objQuestion -> id); + + api_session_register('objAnswer'); + + if($debug>0){echo str_repeat(' ',2).'$answerType is HOT_SPOT'."
\n";} + + $TBL_ANSWERS = $_course['dbNameGlu'].'quiz_answer'; + + 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($debug>0){echo str_repeat(' ',2).'$usedInSeveralExercises is untrue'."
\n";} + + + if($debug>0){echo str_repeat(' ',4).'$answerType is HOT_SPOT'."
\n";} + $hotspot_colors = array("", // $i starts from 1 on next loop (ugly fix) + "#4271B5", + "#FE8E16", + "#3B3B3B", + "#BCD631", + "#D63173", + "#D7D7D7", + "#90AFDD", + "#AF8640", + "#4F9242", + "#F4EB24", + "#ED2024", + "#45C7F0", + "#F7BDE2"); +?> + +

+ +

+ + + + + + + + + +
+ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
 **
+ +
+ + +
+ + +
+
+ + + + + +
*
+
+ + + + +0){echo str_repeat(' ',0).'$modifyAnswers was set - end'."
\n";} +} +?> diff --git a/main/exercice/hotspot_answers.as.php b/main/exercice/hotspot_answers.as.php index 9dd0ecd0d3..14b35a7c31 100644 --- a/main/exercice/hotspot_answers.as.php +++ b/main/exercice/hotspot_answers.as.php @@ -42,8 +42,7 @@ $userId = $_user['user_id']; $questionId = $_GET['modifyAnswers']; - $objQuestion = new Question(); - $objQuestion->read($questionId); + $objQuestion = Question :: read($questionId); $TBL_ANSWERS = $_course['dbNameGlu'].'quiz_answer'; diff --git a/main/exercice/mark_free_answer.php b/main/exercice/mark_free_answer.php index e40a262f15..d64e49d564 100644 --- a/main/exercice/mark_free_answer.php +++ b/main/exercice/mark_free_answer.php @@ -100,8 +100,7 @@ if(!$is_courseTutor) api_not_allowed(); } -$obj_question=new Question(); -$obj_question->read($my_qst); +$obj_question = Question :: read($my_qst); $nameTools=get_lang('Exercice'); diff --git a/main/exercice/matching.class.php b/main/exercice/matching.class.php new file mode 100644 index 0000000000..e33f812cf8 --- /dev/null +++ b/main/exercice/matching.class.php @@ -0,0 +1,272 @@ + type = MATCHING; + } + + /** + * function which redifines Question::createAnswersForm + * @param the formvalidator instance + */ + function createAnswersForm ($form) { + + $defaults = array(); + + $nb_matches = $nb_options = 2; + if($form -> isSubmitted()) + { + $nb_matches = $form -> getSubmitValue('nb_matches'); + $nb_options = $form -> getSubmitValue('nb_options'); + if(isset($_POST['lessMatches'])) + $nb_matches--; + if(isset($_POST['moreMatches'])) + $nb_matches++; + if(isset($_POST['lessOptions'])) + $nb_options--; + if(isset($_POST['moreOptions'])) + $nb_options++; + + } + else if(!empty($this -> id)) + { + $answer = new Answer($this -> id); + $answer -> read(); + if(count($answer->nbrAnswers)>0) + { + $a_matches = $a_options = array(); + $nb_matches = $nb_options = 0; + for($i=1 ; $i<=$answer->nbrAnswers ; $i++){ + if($answer -> isCorrect($i)) + { + $nb_matches++; + $defaults['answer['.$nb_matches.']'] = $answer -> selectAnswer($i); + $defaults['weighting['.$nb_matches.']'] = $answer -> selectWeighting($i); + $defaults['matches['.$nb_matches.']'] = $answer -> correct[$i]; + } + else + { + $nb_options++; + $defaults['option['.$nb_options.']'] = $answer -> selectAnswer($i); + } + } + + } + } + else { + $defaults['answer[1]'] = get_lang('DefaultMakeCorrespond1'); + $defaults['answer[2]'] = get_lang('DefaultMakeCorrespond2'); + $defaults['matches[2]'] = '2'; + $defaults['option[1]'] = get_lang('DefaultMatchingOptA'); + $defaults['option[2]'] = get_lang('DefaultMatchingOptB'); + } + $a_matches = array(); + for($i=1 ; $i<=$nb_options ; ++$i) + { + $a_matches[$i] = chr(64+$i); // fill the array with A, B, C..... + } + + + + + + $form -> addElement('hidden', 'nb_matches', $nb_matches); + $form -> addElement('hidden', 'nb_options', $nb_options); + + + //////////////////////// + // DISPLAY MATCHES //// + ////////////////////// + + $html=' +
+
+ '.get_lang('Answers').' +
+
+ '.get_lang('MakeCorrespond').' + + + + + + + + '; + $form -> addElement ('html', $html); + + + for($i = 1 ; $i <= $nb_matches ; ++$i) + { + + $form -> addElement ('html', ''); + + } + + $form -> addElement ('html', '
+ N° + + '.get_lang('Answer').' + + '.get_lang('MatchesTo').' + + Weighting +
'); + + $group = array(); + $puce = FormValidator :: createElement ('text', null,null,'value="'.$i.'"'); + $puce->freeze(); + $group[] = $puce; + $group[] = FormValidator :: createElement ('text', 'answer['.$i.']',null, 'size="30"'); + $group[] = FormValidator :: createElement ('select', 'matches['.$i.']',null,$a_matches); + $group[] = FormValidator :: createElement ('text', 'weighting['.$i.']',null, 'style="vertical-align:middle" size="2" value="1"'); + $form -> addGroup($group, null, null, ''); + + $form -> addElement ('html', '
'); + $group = array(); + $group[] = FormValidator :: createElement ('submit', 'lessMatches', '-elem'); + $group[] = FormValidator :: createElement ('submit', 'moreMatches', '+elem'); + $form -> addGroup($group); + + + + //////////////////////// + // DISPLAY OPTIONS //// + ////////////////////// + $html=' +
+
+
+


+ + + + + + '; + $form -> addElement ('html', $html); + + + + for($i = 1 ; $i <= $nb_options ; ++$i) + { + $form -> addElement ('html', ''); + + } + + $form -> addElement ('html', '
+ N° + + '.get_lang('Answer').' +
'); + + $group = array(); + $puce = FormValidator :: createElement ('text', null,null,'value="'.chr(64+$i).'"'); + $puce->freeze(); + $group[] = $puce; + $group[] = FormValidator :: createElement ('text', 'option['.$i.']',null, 'size="30"'); + $form -> addGroup($group, null, null, ''); + + $form -> addElement ('html', '
'); + $group = array(); + $group[] = FormValidator :: createElement ('submit', 'lessOptions', '-elem'); + $group[] = FormValidator :: createElement ('submit', 'moreOptions', '+elem'); + $form -> addGroup($group); + + $form -> setDefaults($defaults); + + $form->setConstants(array('nb_matches' => $nb_matches,'nb_options' => $nb_options)); + + } + + + /** + * abstract function which creates the form to create / edit the answers of the question + * @param the formvalidator instance + */ + function processAnswersCreation($form) { + + $nb_matches = $form -> getSubmitValue('nb_matches'); + $nb_options = $form -> getSubmitValue('nb_options'); + $this -> weighting = 0; + $objAnswer = new Answer($this->id); + + $position = 0; + + // insert the options + for($i=1 ; $i<=$nb_options ; ++$i) + { + $position++; + $option = $form -> getSubmitValue('option['.$i.']'); + $objAnswer->createAnswer($option, 0, '', 0, $position); + } + + // insert the answers + for($i=1 ; $i<=$nb_matches ; ++$i) + { + $position++; + $answer = $form -> getSubmitValue('answer['.$i.']'); + $matches = $form -> getSubmitValue('matches['.$i.']'); + $weighting = $form -> getSubmitValue('weighting['.$i.']'); + $this -> weighting += $weighting; + $objAnswer->createAnswer($answer,$matches,'',$weighting,$position); + } + + $objAnswer->save(); + $this->save(); + + echo ''; + + } + +} + +endif; +?> \ No newline at end of file diff --git a/main/exercice/multiple_answer.class.php b/main/exercice/multiple_answer.class.php new file mode 100644 index 0000000000..b19d4ae091 --- /dev/null +++ b/main/exercice/multiple_answer.class.php @@ -0,0 +1,192 @@ + type = MULTIPLE_ANSWER; + } + + /** + * function which redifines Question::createAnswersForm + * @param the formvalidator instance + * @param the answers number to display + */ + function createAnswersForm ($form) { + + $nb_answers = isset($_POST['nb_answers']) ? $_POST['nb_answers'] : 2; + $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0)); + + $html=' +
+
+ '.get_lang('Answers').' +
+
+ + + + + + + + + '; + $form -> addElement ('html', $html); + + $defaults = array(); + if(!empty($this -> id)) + { + $answer = new Answer($this -> id); + $answer -> read(); + if(count($answer->nbrAnswers)>0 && !$form->isSubmitted()) + { + $nb_answers = $answer->nbrAnswers; + } + } + + $form -> addElement('hidden', 'nb_answers'); + + for($i = 1 ; $i <= $nb_answers ; ++$i) + { + + if(is_object($answer)) + { + $defaults['answer['.$i.']'] = $answer -> answer[$i]; + $defaults['comment['.$i.']'] = $answer -> comment[$i]; + $defaults['weighting['.$i.']'] = $answer -> weighting[$i]; + $defaults['correct['.$i.']'] = $answer -> correct[$i]; + } + + $form -> addElement ('html', ''); + + } + + $form -> addElement ('html', '
+ N° + + True + + Answer + + Comment + + Weighting +
'); + + $group = array(); + $puce = FormValidator :: createElement ('text', null,null,'value="1"'); + $puce->freeze(); + $group[] = $puce; + $group[] = FormValidator :: createElement ('checkbox', 'correct['.$i.']', null, null, $i); + $group[] = FormValidator :: createElement ('textarea', 'answer['.$i.']',null, 'style="vertical-align:middle" cols="30"'); + $group[] = FormValidator :: createElement ('textarea', 'comment['.$i.']',null, 'style="vertical-align:middle" cols="30"'); + $group[] = FormValidator :: createElement ('text', 'weighting['.$i.']',null, 'style="vertical-align:middle" size="5" value="0"'); + $form -> addGroup($group, null, null, ''); + + $form -> addElement ('html', '
'); + $group = array(); + $group[] = FormValidator :: createElement ('submit', 'lessAnswers', '-answ'); + $group[] = FormValidator :: createElement ('submit', 'moreAnswers', '+answ'); + $form -> addGroup($group); + + $form -> setDefaults($defaults); + $form->setConstants(array('nb_answers' => $nb_answers)); + + + } + + + /** + * abstract function which creates the form to create / edit the answers of the question + * @param the formvalidator instance + * @param the answers number to display + */ + function processAnswersCreation($form) { + + $questionWeighting = $nbrGoodAnswers = 0; + + $objAnswer = new Answer($this->id); + + $nb_answers = $form -> getSubmitValue('nb_answers'); + + for($i=1 ; $i <= $nb_answers ; $i++) + { + $answer = trim($form -> getSubmitValue('answer['.$i.']')); + $comment = trim($form -> getSubmitValue('comment['.$i.']')); + $weighting = trim($form -> getSubmitValue('weighting['.$i.']')); + $goodAnswer = trim($form -> getSubmitValue('correct['.$i.']')); + + if($goodAnswer) + { + $nbrGoodAnswers++; + $weighting = abs($weighting); + if($weighting > 0) + { + $questionWeighting += $weighting; + } + } + + $objAnswer -> createAnswer($answer,$goodAnswer,$comment,$weighting,$i); + + } + + // saves the answers into the data base + $objAnswer -> save(); + + // sets the total weighting of the question + $this -> updateWeighting($questionWeighting); + $this -> save(); + + echo ''; + } + +} + +endif; +?> \ No newline at end of file diff --git a/main/exercice/question.class.php b/main/exercice/question.class.php index 408ec6e3d1..70d6138ccc 100644 --- a/main/exercice/question.class.php +++ b/main/exercice/question.class.php @@ -1,4 +1,4 @@ - array('unique_answer.class.php' , 'UniqueAnswer'), + MULTIPLE_ANSWER => array('multiple_answer.class.php' , 'MultipleAnswer'), + FILL_IN_BLANKS => array('fill_blanks.class.php' , 'FillBlanks'), + MATCHING => array('matching.class.php' , 'Matching'), + FREE_ANSWER => array('freeanswer.class.php' , 'FreeAnswer'), + HOT_SPOT => array('hotspot.class.php' , 'HotSpot') + ); /** * constructor of the class @@ -78,7 +99,7 @@ class Question * @param - integer $id - question ID * @return - boolean - true if question exists, otherwise false */ - function read($id) + static function read($id) { global $_course; @@ -92,24 +113,25 @@ class Question // if the question has been found if($object=mysql_fetch_object($result)) { - $this->id=$id; - $this->question=$object->question; - $this->description=$object->description; - $this->weighting=$object->ponderation; - $this->position=$object->position; - $this->type=$object->type; - $this->picture=$object->picture; - - $sql="SELECT exercice_id FROM `$TBL_EXERCICE_QUESTION` WHERE question_id='$id'"; + $objQuestion = Question::getInstance($object->type); + $objQuestion->id=$id; + $objQuestion->question=$object->question; + $objQuestion->description=$object->description; + $objQuestion->weighting=$object->ponderation; + $objQuestion->position=$object->position; + $objQuestion->type=$object->type; + $objQuestion->picture=$object->picture; + + $sql="SELECT exercice_id FROM `$TBL_EXERCICE_QUESTION` WHERE question_id='".intval($id)."'"; $result=api_sql_query($sql,__FILE__,__LINE__); // fills the array with the exercises which this question is in while($object=mysql_fetch_object($result)) { - $this->exerciseList[]=$object->exercice_id; + $objQuestion->exerciseList[]=$object->exercice_id; } - return true; + return $objQuestion; } // question not found @@ -306,7 +328,6 @@ class Question $Extension=$PictureName[sizeof($PictureName)-1]; $this->picture='quiz-'.$this->id.'.'.$Extension; - return move_uploaded_file($Picture,$picturePath.'/'.$this->picture)?true:false; } @@ -574,6 +595,7 @@ class Question $this->exerciseList[]=$exerciseId; $sql="INSERT INTO `$TBL_EXERCICE_QUESTION`(question_id,exercice_id) VALUES('$id','$exerciseId')"; + api_sql_query($sql,__FILE__,__LINE__); } } @@ -675,7 +697,101 @@ class Question return $id; } + + /** + * Returns an instance of the class corresponding to the type + * @param integer $type the type of the question + * @return an instance of a Question subclass (or of Questionc class by default) + */ + static function getInstance ($type) { + + list($file_name,$class_name) = self::$questionTypes[$type]; + + include_once ($file_name); + + + if(class_exists($class_name)) + { + return new $class_name(); + } + else + { + echo 'Can\'t instanciate class '.$class_name.' of type '.$type; + return null; + } + + } + + /** + * Creates the form to create / edit a question + * A subclass can redifine this function to add fields... + * @param FormValidator $form the formvalidator instance (by reference) + */ + function createForm (&$form) { + + // question name + $form->addElement('text','questionName',get_lang('Question'),'size="60"'); + $form->addRule('questionName', get_lang('GiveQuestion'), 'required'); + + // question type + $answerType= intval($_REQUEST['answerType']); + $form->addElement('hidden','answerType',$_REQUEST['answerType']); + + + // html editor + global $fck_attribute; + $fck_attribute = array(); + $fck_attribute['Width'] = '100%'; + $fck_attribute['Height'] = '150'; + $fck_attribute['ToolbarSet'] = 'Small'; + $fck_attribute['Config']['IMUploadPath'] = 'upload/test/'; + $fck_attribute['Config']['FlashUploadPath'] = 'upload/test/'; + if(!api_is_allowed_to_edit()) $fck_attribute['Config']['UserStatus'] = 'student'; + + $form->add_html_editor('questionDescription', get_lang('QuestionDescription'), false); + + // hidden values + $form->addElement('hidden','myid',$_REQUEST['myid']); + + + // default values + $defaults = array(); + $defaults['questionName'] = $this -> question; + $defaults['questionDescription'] = $this -> description; + $form -> setDefaults($defaults); + } + + /** + * function which process the creation of answers + * @param FormValidator $form the formvalidator instance + * @param Exercise $objExercise the objExercise number to display + */ + function processCreation ($form, $objExercise) { + + $this -> updateTitle($form->getSubmitValue('questionName')); + $this -> updateDescription($form->getSubmitValue('questionDescription')); + $this -> save($objExercise -> id); + + // modify the exercise + $objExercise->addToList($this -> id); + $objExercise->save(); + + } + + /** + * abstract function which creates the form to create / edit the answers of the question + * @param the formvalidator instance + */ + abstract function createAnswersForm ($form); + + /** + * abstract function which process the creation of answers + * @param the formvalidator instance + */ + abstract function processAnswersCreation ($form); } endif; + + ?> diff --git a/main/exercice/question_admin.inc.php b/main/exercice/question_admin.inc.php index 4f40d79d47..a7e9e24748 100644 --- a/main/exercice/question_admin.inc.php +++ b/main/exercice/question_admin.inc.php @@ -1,4 +1,4 @@ -selectPicture(); -if ( empty ($answerType) ) { - $answerType = $objQuestion->selectType(); -} -//added -if($_REQUEST['exerciseId']) -$myid=1; -else -$myid=0; - -// if the question we are modifying is used in several exercises -if($usedInSeveralExercises) +/********************* + * INIT QUESTION + *********************/ +if(isset($_GET['editQuestion'])) { -?> - -

- -

- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ".get_lang('ModifyInAllExercises')."
- ".get_lang('ModifyInThisExercise')."
- - "; - Display::display_normal_message($msgBox); //main API -?> -
- -id; } else { - // selects question informations - $questionName=$objQuestion->selectTitle(); - $questionDescription=$objQuestion->selectDescription(); - - // is picture set ? - $okPicture=empty($pictureName)?false:true; -?> - -

- - - -<?php echo get_lang('Modify'); ?> - -

- - - - -
- - - -
- -
- - - -<?php echo get_lang('Modify'); ?> - - - -
- - - - - - - -<< - - - - - - -<?php echo get_lang('Modify'); ?> - - - - + $objQuestion = Question :: getInstance($_REQUEST['answerType']); + $action = $_SERVER['PHP_SELF']."?modifyQuestion=".$modifyQuestion."&newQuestion=".$newQuestion; +} - - - +if(is_object($objQuestion)) +{ - + div.row div.label{ + width: 10%; } -?> - - -
- -
- - - - - -<< - + + '; + echo $styles; + + + /********************* + * INIT FORM + *********************/ + $form = new FormValidator('question_admin_form','post',$action); + - createForm ($form); + + $objQuestion -> createAnswersForm ($form); + + $form->addElement('submit','submitQuestion',get_lang('Ok')); + + + /********************** + * FORM VALIDATION + **********************/ + if(isset($_POST['submitQuestion']) && $form->validate()) + { + // question + $objQuestion -> processCreation($form,$objExercise); + + // answers + $objQuestion -> processAnswersCreation($form,$nb_answers); + } + else + { + + /****************** + * FORM DISPLAY + ******************/ + echo '

'.$questionName.'

'; + + + if(!empty($pictureName)){ + echo ''; + } + + if(!empty($msgErr)) + { + Display::display_normal_message($msgErr); //main API + } + + + // display the form + $form->display(); + } } + ?> \ No newline at end of file diff --git a/main/exercice/question_list_admin.inc.php b/main/exercice/question_list_admin.inc.php index 04d21b332a..92ba795733 100644 --- a/main/exercice/question_list_admin.inc.php +++ b/main/exercice/question_list_admin.inc.php @@ -1,4 +1,4 @@ -read($deleteQuestion)) + if($objQuestionTmp = Question::read($deleteQuestion)) { $objQuestionTmp->delete($exerciseId); @@ -104,9 +102,8 @@ if($nbrQuestions) $i=1; foreach($questionList as $id) { - $objQuestionTmp=new Question(); - $objQuestionTmp->read($id); + $objQuestionTmp = Question :: read($id); //showQuestion($id); ?> diff --git a/main/exercice/question_pool.php b/main/exercice/question_pool.php index 879bd8de28..2ce16df371 100644 --- a/main/exercice/question_pool.php +++ b/main/exercice/question_pool.php @@ -1,4 +1,4 @@ -read($delete)) + if($objQuestionTmp = Question::read($delete)) { // deletes the question from all exercises $objQuestionTmp->delete(); @@ -92,11 +90,8 @@ if($is_allowedToEdit) // gets an existing question and copies it into a new exercise elseif($recup && $fromExercise) { - // construction of the Question object - $objQuestionTmp=new Question(); - // if the question exists - if($objQuestionTmp->read($recup)) + if($objQuestionTmp = Question :: read($recup)) { // adds the exercise ID represented by $fromExercise into the list of exercises for the current question $objQuestionTmp->addToList($fromExercise); diff --git a/main/exercice/statement_admin.inc.php b/main/exercice/statement_admin.inc.php deleted file mode 100644 index d1e9371fca..0000000000 --- a/main/exercice/statement_admin.inc.php +++ /dev/null @@ -1,328 +0,0 @@ -0){echo str_repeat(' ',2).'$submitQuestion is true'."
\n";} - - $questionName=trim(stripslashes($_POST['questionName'])); - $questionDescription=trim(stripslashes($_POST['questionDescription'])); - $_FILES['imageUpload']['name']=strtolower($_FILES['imageUpload']['name']); - - $hotspotErr = false; - - // no name given - if(!$modifyQuestion && (empty($questionName) || ($answerType == HOT_SPOT && ($_FILES['imageUpload']['type'] != 'image/jpeg' && $_FILES['imageUpload']['type'] != 'image/pjpeg' && $_FILES['imageUpload']['type'] != 'image/jpg')) || ($answerType == HOT_SPOT && empty($_FILES['imageUpload']['name'])))) - { - if(($_FILES['imageUpload']['type'] != 'image/jpeg' && $_FILES['imageUpload']['type'] != 'image/pjpeg' && $_FILES['imageUpload']['type'] != 'image/jpg') && !$modifyQuestion) - { - $msgErr = get_lang('langOnlyJPG'); - $hotspotErr = true; - } - if(empty($_FILES['imageUpload']['name']) && !$modifyQuestion) - { - $msgErr=get_lang('NoImage'); - $hotspotErr = true; - } - if(empty($questionName)) - { - $msgErr=get_lang('GiveQuestion'); - } - } - // checks if the question is used in several exercises - elseif($exerciseId && !$modifyIn && $objQuestion->selectNbrExercises() > 1) - { - if($debug>0){echo str_repeat(' ',4).'$exerciseId is set and $modifyIn is unset and this question is in more than one exercise'."
\n";} - $usedInSeveralExercises=1; - - // if a picture has been set - if($_FILES['imageUpload']['size']) - { - // saves the picture into a temporary file - $objQuestion->setTmpPicture($_FILES['imageUpload']['tmp_name'],$_FILES['imageUpload']['name']); - } - } - else - { - if($debug>0){echo str_repeat(' ',4).'You have chosen to modify/add a question locally'."
\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($modifyQuestion); - - $nbrQuestions--; - - // construction of the duplicated Question - $objQuestion=new Question(); - - $objQuestion->read($questionId); - - // adds the exercise ID into the exercise list of the Question object - $objQuestion->addToList($exerciseId); - - // construction of the Answer object - $objAnswerTmp=new Answer($modifyQuestion); - - // copies answers from $modifyQuestion to $questionId - $objAnswerTmp->duplicate($questionId); - - // destruction of the Answer object - unset($objAnswerTmp); - } - - $objQuestion->updateTitle($questionName); - $objQuestion->updateDescription($questionDescription); - - $objQuestion->updateType($_REQUEST['answerType']); - $objQuestion->save($exerciseId); - - // if a picture has been set or checkbox "delete" has been checked - if($_FILES['imageUpload']['size'] || $deletePicture) - { - // we remove the picture - $objQuestion->removePicture(); - - // if we add a new picture - if($_FILES['imageUpload']['size']) - { - // image is already saved in a temporary file - if($modifyIn) - { - $objQuestion->getTmpPicture(); - } - // saves the picture coming from POST FILE - else - { - $objQuestion->uploadPicture($_FILES['imageUpload']['tmp_name'],$_FILES['imageUpload']['name']); - - if(!$objQuestion->resizePicture("any", 350)) - { - $msgErr = get_lang('langHotspotBadMetadata'); - $hotspotErr = true; - - $objQuestion->removePicture(); - } - } - } - - if($hotspotErr === false) - { - $objQuestion->save($exerciseId); - } - else - { - if($newQuestion) - $objQuestion->removeFromList($exerciseId); - } - } - - if($hotspotErr === false) - { - $questionId=$objQuestion->selectId(); - - if($exerciseId) - { - // adds the question ID into the question list of the Exercise object - if($objExercise->addToList($questionId)) - { - $objExercise->save(); - - $nbrQuestions++; - } - } - - if($newQuestion) - { - // goes to answer administration - // -> answer_admin.inc.php - $modifyAnswers=$questionId; - } - else - { - // goes to exercise viewing - $editQuestion=$questionId; - } - - // avoids displaying the following form in case we're editing the answer - unset($newQuestion,$modifyQuestion); - } - } - if($debug>0){echo str_repeat(' ',2).'$submitQuestion is true - end'."
\n";} - -} -else -{ - if($debug>0){echo str_repeat(' ',2).'$submitQuestion was unset'."
\n";} - - // if we don't come here after having cancelled the warning message "used in serveral exercises" - if(!$buttonBack) - { - if($debug>0){echo str_repeat(' ',4).'$buttonBack was unset'."
\n";} - $questionName=$objQuestion->selectTitle(); - $questionDescription=$objQuestion->selectDescription(); - $answerType= isset($_REQUEST['answerType']) ? $_REQUEST['answerType'] : $objQuestion->selectType(); - $pictureName=$objQuestion->selectPicture(); - } - - $okPicture=empty($pictureName)?false:true; - if($debug>0){echo str_repeat(' ',2).'$submitQuestion was unset - end'."
\n";} -} -if(($newQuestion || $modifyQuestion) && !$usedInSeveralExercises) -{ - if($debug>0){echo str_repeat(' ',2).'$newQuestion or modifyQuestion was set but the question only exists in this exercise'."
\n";} - -?> - -

- -

- - - - -$questionDescription, - "questionName"=>$questionName, - "answerType"=>$defaultType); - - -$form = new FormValidator('introduction_text','post',$_SERVER['PHP_SELF']."?modifyQuestion=".$modifyQuestion."&newQuestion=".$newQuestion); -//$renderer =&$form->defaultRenderer(); -//$renderer->setElementTemplate('
{element}
'); - //$attrs = array("align"=>"right"); - -//$buttons[] = &$form->createElement('static','label1',get_lang('Question')); -//$buttons[] = &$form->createElement('text','questionName'); -//$form->addGroup($buttons, null, null, ' '); - -//$form->addelement('static','label1',get_lang('Question')); -$form->addelement('text','questionName',get_lang('Question')); -$form->addelement('hidden','myid',$_REQUEST['myid']); -$form->add_html_editor('questionDescription', get_lang('questionDescription')); -//$form->addElement('html_editor','questionDescription',get_lang('QuestionDescription'),false); -if($okPicture) - { - $form->addelement('checkbox','deletePicture',get_lang('DeletePicture')); - } - -if($modifyQuestion) { - $obj_group_type[] = &HTML_QuickForm::createElement('radio', NULL, NULL, get_lang('UniqueSelect'),1); - $obj_group_type[] = &HTML_QuickForm::createElement('radio', NULL, NULL, get_lang('MultipleSelect'),2); - $obj_group_type[] = &HTML_QuickForm::createElement('radio', NULL, NULL, get_lang('Matching'),4); - $obj_group_type[] = &HTML_QuickForm::createElement('radio', NULL, NULL, get_lang('FillBlanks'),3); - $obj_group_type[] = &HTML_QuickForm::createElement('radio', NULL, NULL, get_lang('freeAnswer'),5); - $obj_group_type[] = &HTML_QuickForm::createElement('radio', NULL, NULL, get_lang('Hotspot'),6); - $form->addGroup($obj_group_type, 'answerType', get_lang('AnswerType').':','
'); -} -else { - $form->addElement('hidden','answerType',$_REQUEST['answerType']); -} - -if($answerType == HOT_SPOT) - $form->addElement('file','imageUpload'); -$form->addElement('submit','submitQuestion',get_lang('Ok')); - - $form->setDefaults($user); - -$form->display(); - -?> - - - - \ No newline at end of file diff --git a/main/exercice/unique_answer.class.php b/main/exercice/unique_answer.class.php new file mode 100644 index 0000000000..c5ccf93b0c --- /dev/null +++ b/main/exercice/unique_answer.class.php @@ -0,0 +1,198 @@ + type = UNIQUE_ANSWER; + } + + /** + * function which redifines Question::createAnswersForm + * @param the formvalidator instance + * @param the answers number to display + */ + function createAnswersForm ($form) { + + + $nb_answers = isset($_POST['nb_answers']) ? $_POST['nb_answers'] : 2; + $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0)); + + $html=' +
+
+ '.get_lang('Answers').' +
+
+ + + + + + + + + '; + $form -> addElement ('html', $html); + + $defaults = array(); + $correct = 0; + if(!empty($this -> id)) + { + $answer = new Answer($this -> id); + $answer -> read(); + if(count($answer->nbrAnswers)>0 && !$form->isSubmitted()) + { + $nb_answers = $answer->nbrAnswers; + } + } + + $form -> addElement('hidden', 'nb_answers'); + + for($i = 1 ; $i <= $nb_answers ; ++$i) + { + + if(is_object($answer)) + { + if($answer -> correct[$i]) + { + $correct = $i; + } + $defaults['answer['.$i.']'] = $answer -> answer[$i]; + $defaults['comment['.$i.']'] = $answer -> comment[$i]; + $defaults['weighting['.$i.']'] = $answer -> weighting[$i]; + } + + $form -> addElement ('html', ''); + + } + + $form -> addElement ('html', '
+ N° + + True + + Answer + + Comment + + Weighting +
'); + + $group = array(); + $puce = FormValidator :: createElement ('text', null,null,'value="'.$i.'"'); + $puce->freeze(); + $group[] = $puce; + $group[] = FormValidator :: createElement ('radio', 'correct', null, null, $i); + $group[] = FormValidator :: createElement ('textarea', 'answer['.$i.']',null, 'style="vertical-align:middle" cols="30"'); + $group[] = FormValidator :: createElement ('textarea', 'comment['.$i.']',null, 'style="vertical-align:middle" cols="30"'); + $group[] = FormValidator :: createElement ('text', 'weighting['.$i.']',null, 'style="vertical-align:middle" size="5" value="0"'); + $form -> addGroup($group, null, null, ''); + + $form -> addElement ('html', '
'); + $group = array(); + $group[] = FormValidator :: createElement ('submit', 'lessAnswers', '-answ'); + $group[] = FormValidator :: createElement ('submit', 'moreAnswers', '+answ'); + $form -> addGroup($group); + + $defaults['correct'] = $correct; + $form -> setDefaults($defaults); + + $form->setConstants(array('nb_answers' => $nb_answers)); + + } + + + /** + * abstract function which creates the form to create / edit the answers of the question + * @param the formvalidator instance + * @param the answers number to display + */ + function processAnswersCreation($form) { + + $questionWeighting = $nbrGoodAnswers = 0; + + $correct = $form -> getSubmitValue('correct'); + $objAnswer = new Answer($this->id); + $nb_answers = $form -> getSubmitValue('nb_answers'); + + for($i=1 ; $i <= $nb_answers ; $i++) + { + $answer = trim($form -> getSubmitValue('answer['.$i.']')); + $comment = trim($form -> getSubmitValue('comment['.$i.']')); + $weighting = trim($form -> getSubmitValue('weighting['.$i.']')); + + $goodAnswer= ($correct == $i) ? true : false; + + if($goodAnswer) + { + $nbrGoodAnswers++; + $weighting = abs($weighting); + if($weighting > 0) + { + $questionWeighting += $weighting; + } + } + + $objAnswer -> createAnswer($answer,$goodAnswer,$comment,$weighting,$i); + + } + + // saves the answers into the data base + $objAnswer -> save(); + + // sets the total weighting of the question + $this -> updateWeighting($questionWeighting); + $this -> save(); + echo ''; + } + +} + +endif; +?> \ No newline at end of file