multiple_answer_combination.class.php 7.8 KB

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