multiple_answer_combination.class.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php // $Id: document.php 16494 2008-10-10 22:07:36Z yannoo $
  2. /* For licensing terms, see /chamilo_license.txt */
  3. /**
  4. * File containing the MultipleAnswer class.
  5. * @package dokeos.exercise
  6. * @author Eric Marguin
  7. * @version $Id: admin.php 10680 2007-01-11 21:26:23Z pcool $
  8. */
  9. if(!class_exists('MultipleAnswerCombination')):
  10. /**
  11. CLASS MultipleAnswer
  12. *
  13. * This class allows to instantiate an object of type MULTIPLE_ANSWER (MULTIPLE CHOICE, MULTIPLE ANSWER),
  14. * extending the class question
  15. *
  16. * @author Eric Marguin
  17. * @package dokeos.exercise
  18. **/
  19. class MultipleAnswerCombination extends Question {
  20. static $typePicture = 'mcmac.gif';
  21. static $explanationLangVar = 'MultipleSelectCombination';
  22. /**
  23. * Constructor
  24. */
  25. function MultipleAnswerCombination(){
  26. parent::question();
  27. $this -> type = MULTIPLE_ANSWER_COMBINATION;
  28. }
  29. /**
  30. * function which redifines Question::createAnswersForm
  31. * @param the formvalidator instance
  32. * @param the answers number to display
  33. */
  34. function createAnswersForm ($form) {
  35. $nb_answers = isset($_POST['nb_answers']) ? $_POST['nb_answers'] : 2;
  36. $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
  37. $obj_ex = $_SESSION['objExercise'];
  38. /*
  39. *
  40. * <th>
  41. '.get_lang('Weighting').'
  42. </th>
  43. */
  44. $html='
  45. <div class="row">
  46. <div class="label">
  47. '.get_lang('Answers').'<br /><img src="../img/fill_field.png">
  48. </div>
  49. <div class="formw">
  50. <table class="data_table">
  51. <tr style="text-align: center;">
  52. <th>
  53. '.get_lang('Number').'
  54. </th>
  55. <th>
  56. '.get_lang('True').'
  57. </th>
  58. <th>
  59. '.get_lang('Answer').'
  60. </th>';
  61. // show column comment when feedback is enable
  62. if ($obj_ex->selectFeedbackType() != EXERCISE_FEEDBACK_TYPE_EXAM ) {
  63. $html .='<th>'.get_lang('Comment').'</th>';
  64. }
  65. $html .= '</tr>';
  66. $form -> addElement ('html', $html);
  67. $defaults = array();
  68. $correct = 0;
  69. if(!empty($this -> id)) {
  70. $answer = new Answer($this -> id);
  71. $answer -> read();
  72. if(count($answer->nbrAnswers)>0 && !$form->isSubmitted()) {
  73. $nb_answers = $answer->nbrAnswers;
  74. }
  75. }
  76. $form -> addElement('hidden', 'nb_answers');
  77. $boxes_names = array();
  78. if ($nb_answers < 1) {
  79. $nb_answers = 1;
  80. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  81. }
  82. for($i = 1 ; $i <= $nb_answers ; ++$i) {
  83. if(is_object($answer)) {
  84. $defaults['answer['.$i.']'] = $answer -> answer[$i];
  85. $defaults['comment['.$i.']'] = $answer -> comment[$i];
  86. $defaults['weighting['.$i.']'] = float_format($answer -> weighting[$i], 1);
  87. $defaults['correct['.$i.']'] = $answer -> correct[$i];
  88. } else {
  89. $defaults['answer[1]'] = get_lang('langDefaultMultipleAnswer2');
  90. $defaults['comment[1]'] = get_lang('langDefaultMultipleComment2');
  91. $defaults['correct[1]'] = true;
  92. $defaults['weighting[1]'] = 10;
  93. $defaults['answer[2]'] = get_lang('langDefaultMultipleAnswer1');
  94. $defaults['comment[2]'] = get_lang('langDefaultMultipleComment1');
  95. $defaults['correct[2]'] = false;
  96. //$defaults['weighting[2]'] = -5;
  97. }
  98. $renderer = & $form->defaultRenderer();
  99. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>');
  100. $answer_number=$form->addElement('text', null,null,'value="'.$i.'"');
  101. $answer_number->freeze();
  102. $form->addElement('checkbox', 'correct['.$i.']', null, null, 'class="checkbox" style="margin-left: 0em;"');
  103. $boxes_names[] = 'correct['.$i.']';
  104. $form->addElement('html_editor', 'answer['.$i.']',null, 'style="vertical-align:middle"', array('ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '100'));
  105. $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
  106. if ($obj_ex->selectFeedbackType() != EXERCISE_FEEDBACK_TYPE_EXAM) {
  107. $form->addElement('html_editor', 'comment['.$i.']',null, 'style="vertical-align:middle"', array('ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '100'));
  108. }
  109. //only 1 answer the all deal ...
  110. //$form->addElement('text', 'weighting['.$i.']',null, 'style="vertical-align:middle;margin-left: 0em;" size="5" value="10"');
  111. $form -> addElement ('html', '</tr>');
  112. }
  113. $form -> addElement ('html', '</table>');
  114. $form -> addElement ('html', '<br />');
  115. $form -> add_multiple_required_rule ($boxes_names , get_lang('ChooseAtLeastOneCheckbox') , 'multiple_required');
  116. $html_total_score ='<div style="margin-bottom:2px;">'.get_lang('Score').'</div>';
  117. $form -> addElement ('html', $html_total_score);
  118. //only 1 answer the all deal ...
  119. $form->addElement('text', 'weighting[1]',null, 'style="vertical-align:middle;margin-left: 0em;" size="5" value="10"');
  120. $form -> addElement ('html', '<br /><br />');
  121. $navigator_info = api_get_navigator();
  122. global $text, $class, $show_quiz_edition;
  123. //ie6 fix
  124. if ($show_quiz_edition) {
  125. if ($navigator_info['name']=='Internet Explorer' && $navigator_info['version']=='6') {
  126. $form->addElement('submit', 'lessAnswers', get_lang('LessAnswer'),'class="minus"');
  127. $form->addElement('submit', 'moreAnswers', get_lang('PlusAnswer'),'class="plus"');
  128. $form->addElement('submit','submitQuestion',$text, 'class="'.$class.'"');
  129. } else {
  130. $form->addElement('style_submit_button', 'lessAnswers', get_lang('LessAnswer'),'class="minus"');
  131. $form->addElement('style_submit_button', 'moreAnswers', get_lang('PlusAnswer'),'class="plus"');
  132. // setting the save button here and not in the question class.php
  133. $form->addElement('style_submit_button','submitQuestion',$text, 'class="'.$class.'"');
  134. }
  135. }
  136. $renderer->setElementTemplate('{element}&nbsp;','lessAnswers');
  137. $renderer->setElementTemplate('{element}&nbsp;','submitQuestion');
  138. $renderer->setElementTemplate('{element}','moreAnswers');
  139. $form -> addElement ('html', '</div></div>');
  140. $defaults['correct'] = $correct;
  141. $form -> setDefaults($defaults);
  142. $form->setConstants(array('nb_answers' => $nb_answers));
  143. }
  144. /**
  145. * abstract function which creates the form to create / edit the answers of the question
  146. * @param the formvalidator instance
  147. * @param the answers number to display
  148. */
  149. function processAnswersCreation($form) {
  150. $questionWeighting = $nbrGoodAnswers = 0;
  151. $objAnswer = new Answer($this->id);
  152. $nb_answers = $form -> getSubmitValue('nb_answers');
  153. for($i=1 ; $i <= $nb_answers ; $i++)
  154. {
  155. $answer = trim($form -> getSubmitValue('answer['.$i.']'));
  156. $comment = trim($form -> getSubmitValue('comment['.$i.']'));
  157. if ($i == 1)
  158. $weighting = trim($form -> getSubmitValue('weighting['.$i.']'));
  159. else {
  160. $weighting = 0;
  161. }
  162. $goodAnswer = trim($form -> getSubmitValue('correct['.$i.']'));
  163. if($goodAnswer){
  164. $weighting = abs($weighting);
  165. } else {
  166. $weighting = abs($weighting);
  167. // $weighting = -$weighting;
  168. }
  169. if($weighting > 0)
  170. {
  171. $questionWeighting += $weighting;
  172. }
  173. $objAnswer -> createAnswer($answer,$goodAnswer,$comment,$weighting,$i);
  174. }
  175. // saves the answers into the data base
  176. $objAnswer -> save();
  177. // sets the total weighting of the question
  178. $this -> updateWeighting($questionWeighting);
  179. $this -> save();
  180. }
  181. }
  182. endif;
  183. ?>