multiple_answer_combination.class.php 10 KB

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