unique_answer_image.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. *
  5. * This class allows to instantiate an object of type UNIQUE_ANSWER_IMAGE
  6. * extending the class question
  7. *
  8. * @author Julio Montoya
  9. * @package chamilo.exercise
  10. **/
  11. class UniqueAnswerImage extends UniqueAnswer
  12. {
  13. static $typePicture = 'mcua.gif';
  14. static $explanationLangVar = 'UniqueSelect';
  15. /**
  16. * Constructor
  17. */
  18. public function UniqueAnswerImage()
  19. {
  20. //this is highly important
  21. parent::question();
  22. $this->type = UNIQUE_ANSWER_IMAGE;
  23. $this->isContent = $this->getIsContent();
  24. }
  25. /**
  26. * function which redifines Question::createAnswersForm
  27. * @param FormValidator instance
  28. */
  29. public function createAnswersForm($form)
  30. {
  31. // Getting the exercise list
  32. $obj_ex = $this->exercise;
  33. $editor_config = array('ToolbarSet' => 'UniqueAnswerImage', 'Width' => '100%', 'Height' => '125');
  34. //this line define how many question by default appear when creating a choice question
  35. $nb_answers = isset($_POST['nb_answers']) ? (int)$_POST['nb_answers'] : 4; // The previous default value was 2. See task #1759.
  36. $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
  37. $feedback_title = '';
  38. $comment_title = '';
  39. if ($obj_ex->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_END) {
  40. $comment_title = '<th>'.get_lang('Comment').'</th>';
  41. } elseif ($obj_ex->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  42. //Scenario
  43. $editor_config['Width'] = '250';
  44. $editor_config['Height'] = '110';
  45. $comment_title = '<th width="500px" >'.get_lang('Comment').'</th>';
  46. $feedback_title = '<th width="350px" >'.get_lang('Scenario').'</th>';
  47. }
  48. $html = '<table class="data_table">
  49. <tr style="text-align: center;">
  50. <th width="10px">
  51. '.get_lang('Number').'
  52. </th>
  53. <th width="10px" >
  54. '.get_lang('True').'
  55. </th>
  56. <th width="50%">
  57. '.get_lang('Answer').'
  58. </th>
  59. '.$comment_title.'
  60. '.$feedback_title.'
  61. <th width="50px">
  62. '.get_lang('Weighting').'
  63. </th>
  64. </tr>';
  65. $form->addElement('label', get_lang('Answers').'<br />'.Display::return_icon('fill_field.png'), $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. $list = new LearnpathList(api_get_user_id());
  77. $flat_list = $list->get_flat_list();
  78. $select_lp_id = array();
  79. $select_lp_id[0] = get_lang('SelectTargetLP');
  80. foreach ($flat_list as $id => $details) {
  81. $select_lp_id[$id] = Text::cut($details['lp_name'], 20);
  82. }
  83. $temp_scenario = array();
  84. if ($nb_answers < 1) {
  85. $nb_answers = 1;
  86. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  87. }
  88. for ($i = 1; $i <= $nb_answers; ++$i) {
  89. $form->addElement('html', '<tr>');
  90. if (isset($answer) && is_object($answer)) {
  91. if ($answer->correct[$i]) {
  92. $correct = $i;
  93. }
  94. $defaults['answer['.$i.']'] = $answer->answer[$i];
  95. $defaults['comment['.$i.']'] = $answer->comment[$i];
  96. $defaults['weighting['.$i.']'] = Text::float_format($answer->weighting[$i], 1);
  97. $item_list = explode('@@', $answer->destination[$i]);
  98. $try = $item_list[0];
  99. $lp = $item_list[1];
  100. $list_dest = $item_list[2];
  101. $url = $item_list[3];
  102. if ($try == 0) {
  103. $try_result = 0;
  104. } else {
  105. $try_result = 1;
  106. }
  107. if ($url == 0) {
  108. $url_result = '';
  109. } else {
  110. $url_result = $url;
  111. }
  112. $temp_scenario['url'.$i] = $url_result;
  113. $temp_scenario['try'.$i] = $try_result;
  114. $temp_scenario['lp'.$i] = $lp;
  115. $temp_scenario['destination'.$i] = $list_dest;
  116. } else {
  117. $defaults['answer[1]'] = get_lang('DefaultUniqueAnswer1');
  118. $defaults['weighting[1]'] = 10;
  119. $defaults['answer[2]'] = get_lang('DefaultUniqueAnswer2');
  120. $defaults['weighting[2]'] = 0;
  121. $temp_scenario['destination'.$i] = array('0');
  122. $temp_scenario['lp'.$i] = array('0');
  123. }
  124. $defaults['scenario'] = $temp_scenario;
  125. $renderer = $form->defaultRenderer();
  126. $renderer->setElementTemplate(
  127. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  128. 'correct'
  129. );
  130. $renderer->setElementTemplate(
  131. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  132. 'counter['.$i.']'
  133. );
  134. $renderer->setElementTemplate(
  135. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  136. 'answer['.$i.']'
  137. );
  138. $renderer->setElementTemplate(
  139. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  140. 'comment['.$i.']'
  141. );
  142. $renderer->setElementTemplate(
  143. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  144. 'weighting['.$i.']'
  145. );
  146. $answer_number = $form->addElement('text', 'counter['.$i.']', null, ' value = "'.$i.'"');
  147. $answer_number->freeze();
  148. $form->addElement('radio', 'correct', null, null, $i, 'class="checkbox" style="margin-left: 0em;"');
  149. $form->addElement('html_editor', 'answer['.$i.']', null, 'style="vertical-align:middle"', $editor_config);
  150. /*
  151. if ($obj_ex->fastEdition) {
  152. $form->addElement('textarea', 'answer['.$i.']', null, $this->textareaSettings);
  153. } else {
  154. $form->addElement('html_editor', 'answer['.$i.']', null, 'style="vertical-align:middle"', $editor_config);
  155. }*/
  156. $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
  157. if ($obj_ex->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_END) {
  158. if ($obj_ex->fastEdition) {
  159. // feedback
  160. $form->addElement(
  161. 'textarea',
  162. 'comment['.$i.']',
  163. null,
  164. $this->textareaSettings
  165. );
  166. } else {
  167. // feedback
  168. $form->addElement(
  169. 'html_editor',
  170. 'comment['.$i.']',
  171. null,
  172. 'style="vertical-align:middle"',
  173. $editor_config
  174. );
  175. }
  176. } elseif ($obj_ex->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  177. $form->addElement(
  178. 'html_editor',
  179. 'comment['.$i.']',
  180. null,
  181. 'style="vertical-align:middle"',
  182. $editor_config
  183. );
  184. // Direct feedback
  185. //Adding extra feedback fields
  186. $group = array();
  187. $group['try'.$i] = $form->createElement('checkbox', 'try'.$i, null, get_lang('TryAgain'));
  188. $group['lp'.$i] = $form->createElement(
  189. 'select',
  190. 'lp'.$i,
  191. get_lang('SeeTheory').': ',
  192. $select_lp_id
  193. );
  194. $group['destination'.$i] = $form->createElement(
  195. 'select',
  196. 'destination'.$i,
  197. get_lang('GoToQuestion').': ',
  198. $select_question
  199. );
  200. $group['url'.$i] = $form->createElement(
  201. 'text',
  202. 'url'.$i,
  203. get_lang('Other').': ',
  204. array('class' => 'span2', 'placeholder' => get_lang('Other'))
  205. );
  206. $form->addGroup($group, 'scenario');
  207. $renderer->setElementTemplate(
  208. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}',
  209. 'scenario'
  210. );
  211. }
  212. $form->addElement('text', 'weighting['.$i.']', null, array('class' => "span1", 'value' => '0'));
  213. $form->addElement('html', '</tr>');
  214. }
  215. $form->addElement('html', '</table>');
  216. $form->addElement('html', '<br />');
  217. $navigator_info = api_get_navigator();
  218. //ie6 fix
  219. if ($obj_ex->edit_exercise_in_lp == true) {
  220. if ($navigator_info['name'] == 'Internet Explorer' && $navigator_info['version'] == '6') {
  221. $form->addElement('submit', 'lessAnswers', get_lang('LessAnswer'), 'class="btn minus"');
  222. $form->addElement('submit', 'moreAnswers', get_lang('PlusAnswer'), 'class="btn plus"');
  223. $form->addElement('submit', 'submitQuestion', $this->submitText, 'class="'.$this->submitClass.'"');
  224. } else {
  225. //setting the save button here and not in the question class.php
  226. $form->addElement('style_submit_button', 'lessAnswers', get_lang('LessAnswer'), 'class="btn minus"');
  227. $form->addElement('style_submit_button', 'moreAnswers', get_lang('PlusAnswer'), 'class="btn plus"');
  228. $form->addElement('style_submit_button', 'submitQuestion', $this->submitText, 'class="'.$this->submitClass.'"');
  229. }
  230. }
  231. $renderer->setElementTemplate('{element}&nbsp;', 'submitQuestion');
  232. $renderer->setElementTemplate('{element}&nbsp;', 'lessAnswers');
  233. $renderer->setElementTemplate('{element}&nbsp;', 'moreAnswers');
  234. $form->addElement('html', '</div></div>');
  235. //We check the first radio button to be sure a radio button will be check
  236. if ($correct == 0) {
  237. $correct = 1;
  238. }
  239. $defaults['correct'] = $correct;
  240. if (!empty($this->id)) {
  241. $form->setDefaults($defaults);
  242. } else {
  243. if ($this->isContent == 1) {
  244. $form->setDefaults($defaults);
  245. }
  246. }
  247. $form->setConstants(array('nb_answers' => $nb_answers));
  248. }
  249. /**
  250. * abstract function which creates the form to create / edit the answers of the question
  251. * @param FormValidator instance
  252. */
  253. public function processAnswersCreation($form)
  254. {
  255. $questionWeighting = $nbrGoodAnswers = 0;
  256. $correct = $form->getSubmitValue('correct');
  257. $objAnswer = new Answer($this->id);
  258. $nb_answers = $form->getSubmitValue('nb_answers');
  259. for ($i = 1; $i <= $nb_answers; $i++) {
  260. $answer = trim($form->getSubmitValue('answer['.$i.']'));
  261. $comment = trim($form->getSubmitValue('comment['.$i.']'));
  262. $weighting = trim($form->getSubmitValue('weighting['.$i.']'));
  263. $scenario = $form->getSubmitValue('scenario');
  264. //$list_destination = $form -> getSubmitValue('destination'.$i);
  265. //$destination_str = $form -> getSubmitValue('destination'.$i);
  266. $try = $scenario['try'.$i];
  267. $lp = $scenario['lp'.$i];
  268. $destination = $scenario['destination'.$i];
  269. $url = trim($scenario['url'.$i]);
  270. $goodAnswer = ($correct == $i) ? true : false;
  271. if ($goodAnswer) {
  272. $nbrGoodAnswers++;
  273. $weighting = abs($weighting);
  274. if ($weighting > 0) {
  275. $questionWeighting += $weighting;
  276. }
  277. }
  278. if (empty($try)) {
  279. $try = 0;
  280. }
  281. if (empty($lp)) {
  282. $lp = 0;
  283. }
  284. if (empty($destination)) {
  285. $destination = 0;
  286. }
  287. if ($url == '') {
  288. $url = 0;
  289. }
  290. //1@@1;2;@@2;4;4;@@http://www.chamilo.org
  291. $dest = $try.'@@'.$lp.'@@'.$destination.'@@'.$url;
  292. $objAnswer->createAnswer($answer, $goodAnswer, $comment, $weighting, $i, null, null, $dest);
  293. }
  294. // saves the answers into the data base
  295. $objAnswer->save();
  296. // sets the total weighting of the question
  297. $this->updateWeighting($questionWeighting);
  298. $this->save();
  299. }
  300. /**
  301. * {@inheritdoc}
  302. */
  303. public function return_header($feedback_type = null, $counter = null, $score = null, $show_media = false, $hideTitle = 0)
  304. {
  305. return parent::return_header($feedback_type, $counter, $score, $show_media, $hideTitle);
  306. }
  307. }