multiple_answer_combination.class.php 8.3 KB

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