unique_answer_image.class.php 14 KB

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