multiple_answer.class.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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('MultipleAnswer')):
  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 MultipleAnswer extends Question {
  20. static $typePicture = 'mcma.gif';
  21. static $explanationLangVar = 'MultipleSelect';
  22. /**
  23. * Constructor
  24. */
  25. function MultipleAnswer(){
  26. parent::question();
  27. $this -> type = MULTIPLE_ANSWER;
  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. $html='
  39. <div class="row">
  40. <div class="label">
  41. '.get_lang('Answers').'<br /><img src="../img/fill_field.png">
  42. </div>
  43. <div class="formw">
  44. <table class="data_table">
  45. <tr style="text-align: center;">
  46. <th>
  47. '.get_lang('Number').'
  48. </th>
  49. <th>
  50. '.get_lang('True').'
  51. </th>
  52. <th>
  53. '.get_lang('Answer').'
  54. </th>';
  55. // show column comment when feedback is enable
  56. if ($obj_ex->selectFeedbackType() != EXERCISE_FEEDBACK_TYPE_EXAM ) {
  57. $html .='<th>
  58. '.get_lang('Comment').'
  59. </th>';
  60. }
  61. $html .= '<th>
  62. '.get_lang('Weighting').'
  63. </th>
  64. </tr>';
  65. $form -> addElement ('html', $html);
  66. $defaults = array();
  67. $correct = 0;
  68. if(!empty($this -> id)) {
  69. $answer = new Answer($this -> id);
  70. $answer -> read();
  71. if(count($answer->nbrAnswers)>0 && !$form->isSubmitted()) {
  72. $nb_answers = $answer->nbrAnswers;
  73. }
  74. }
  75. $form -> addElement('hidden', 'nb_answers');
  76. $boxes_names = array();
  77. if ($nb_answers < 1) {
  78. $nb_answers = 1;
  79. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  80. }
  81. for($i = 1 ; $i <= $nb_answers ; ++$i) {
  82. if(is_object($answer)) {
  83. $defaults['answer['.$i.']'] = $answer -> answer[$i];
  84. $defaults['comment['.$i.']'] = $answer -> comment[$i];
  85. $defaults['weighting['.$i.']'] = float_format($answer -> weighting[$i], 1);
  86. $defaults['correct['.$i.']'] = $answer -> correct[$i];
  87. } else {
  88. $defaults['answer[1]'] = get_lang('langDefaultMultipleAnswer2');
  89. $defaults['comment[1]'] = get_lang('langDefaultMultipleComment2');
  90. $defaults['correct[1]'] = true;
  91. $defaults['weighting[1]'] = 10;
  92. $defaults['answer[2]'] = get_lang('langDefaultMultipleAnswer1');
  93. $defaults['comment[2]'] = get_lang('langDefaultMultipleComment1');
  94. $defaults['correct[2]'] = false;
  95. $defaults['weighting[2]'] = -5;
  96. }
  97. $renderer = & $form->defaultRenderer();
  98. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>');
  99. $answer_number=$form->addElement('text', null,null,'value="'.$i.'"');
  100. $answer_number->freeze();
  101. $form->addElement('checkbox', 'correct['.$i.']', null, null, 'class="checkbox" style="margin-left: 0em;"');
  102. $boxes_names[] = 'correct['.$i.']';
  103. $form->addElement('html_editor', 'answer['.$i.']',null, 'style="vertical-align:middle"', array('ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '100'));
  104. $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
  105. // show comment when feedback is enable
  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. $form->addElement('text', 'weighting['.$i.']',null, 'style="vertical-align:middle;margin-left: 0em;" size="5" value="10"');
  110. $form -> addElement ('html', '</tr>');
  111. }
  112. $form -> addElement ('html', '</table>');
  113. $form -> addElement ('html', '<br />');
  114. $form -> add_multiple_required_rule ($boxes_names , get_lang('ChooseAtLeastOneCheckbox') , 'multiple_required');
  115. $navigator_info = api_get_navigator();
  116. global $text, $class;
  117. //ie6 fix
  118. if ($navigator_info['name']=='Internet Explorer' && $navigator_info['version']=='6') {
  119. $form->addElement('submit', 'lessAnswers', get_lang('LessAnswer'),'class="minus"');
  120. $form->addElement('submit', 'moreAnswers', get_lang('PlusAnswer'),'class="plus"');
  121. $form->addElement('submit','submitQuestion',$text, 'class="'.$class.'"');
  122. } else {
  123. $form->addElement('style_submit_button', 'lessAnswers', get_lang('LessAnswer'),'class="minus"');
  124. $form->addElement('style_submit_button', 'moreAnswers', get_lang('PlusAnswer'),'class="plus"');
  125. // setting the save button here and not in the question class.php
  126. $form->addElement('style_submit_button','submitQuestion',$text, 'class="'.$class.'"');
  127. }
  128. $renderer->setElementTemplate('{element}&nbsp;','lessAnswers');
  129. $renderer->setElementTemplate('{element}&nbsp;','submitQuestion');
  130. $renderer->setElementTemplate('{element}','moreAnswers');
  131. $form -> addElement ('html', '</div></div>');
  132. $defaults['correct'] = $correct;
  133. $form -> setDefaults($defaults);
  134. $form->setConstants(array('nb_answers' => $nb_answers));
  135. }
  136. /**
  137. * abstract function which creates the form to create / edit the answers of the question
  138. * @param the formvalidator instance
  139. * @param the answers number to display
  140. */
  141. function processAnswersCreation($form) {
  142. $questionWeighting = $nbrGoodAnswers = 0;
  143. $objAnswer = new Answer($this->id);
  144. $nb_answers = $form -> getSubmitValue('nb_answers');
  145. for($i=1 ; $i <= $nb_answers ; $i++)
  146. {
  147. $answer = trim($form -> getSubmitValue('answer['.$i.']'));
  148. $comment = trim($form -> getSubmitValue('comment['.$i.']'));
  149. $weighting = trim($form -> getSubmitValue('weighting['.$i.']'));
  150. $goodAnswer = trim($form -> getSubmitValue('correct['.$i.']'));
  151. if($goodAnswer){
  152. $weighting = abs($weighting);
  153. } else {
  154. $weighting = abs($weighting);
  155. $weighting = -$weighting;
  156. }
  157. if($weighting > 0)
  158. {
  159. $questionWeighting += $weighting;
  160. }
  161. $objAnswer -> createAnswer($answer,$goodAnswer,$comment,$weighting,$i);
  162. }
  163. // saves the answers into the data base
  164. $objAnswer -> save();
  165. // sets the total weighting of the question
  166. $this -> updateWeighting($questionWeighting);
  167. $this -> save();
  168. }
  169. }
  170. endif;
  171. ?>