multiple_answer_combination.class.php 7.0 KB

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