unique_answer_no_option.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Class UniqueAnswerNoOption
  6. * Allows to instantiate an object of type UNIQUE_ANSWER (MULTIPLE CHOICE, UNIQUE ANSWER),
  7. * extending the class question.
  8. *
  9. * @author Eric Marguin
  10. * @author Julio Montoya
  11. */
  12. class UniqueAnswerNoOption extends Question
  13. {
  14. public $typePicture = 'mcuao.png';
  15. public $explanationLangVar = 'UniqueAnswerNoOption';
  16. /**
  17. * Constructor.
  18. */
  19. public function __construct()
  20. {
  21. parent::__construct();
  22. $this->type = UNIQUE_ANSWER_NO_OPTION;
  23. $this->isContent = $this->getIsContent();
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function createAnswersForm($form)
  29. {
  30. // getting the exercise list
  31. $obj_ex = Session::read('objExercise');
  32. $editor_config = [
  33. 'ToolbarSet' => 'TestProposedAnswer',
  34. 'Width' => '100%',
  35. 'Height' => '125',
  36. ];
  37. // This line define how many question by default appear when creating a choice question
  38. // The previous default value was 2. See task #1759.
  39. $nb_answers = isset($_POST['nb_answers']) ? (int) $_POST['nb_answers'] : 3;
  40. $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
  41. /*
  42. Types of Feedback
  43. $feedback_option[0]=get_lang('Feedback');
  44. $feedback_option[1]=get_lang('Adaptative test with immediate feedback');
  45. $feedback_option[2]=get_lang('Exam (no feedback)');
  46. */
  47. $feedback_title = '';
  48. if ($obj_ex->getFeedbackType() == 1) {
  49. $editor_config['Width'] = '250';
  50. $editor_config['Height'] = '110';
  51. $comment_title = '<th width="50%" >'.get_lang('Comment').'</th>';
  52. $feedback_title = '<th width="50%" >'.get_lang('Scenario').'</th>';
  53. } else {
  54. $comment_title = '<th width="50%">'.get_lang('Comment').'</th>';
  55. }
  56. $html = '<table class="table table-striped table-hover">';
  57. $html .= '<thead>';
  58. $html .= '<tr>';
  59. $html .= '<th>'.get_lang('N°').'</th>';
  60. $html .= '<th>'.get_lang('True').'</th>';
  61. $html .= '<th width="50%">'.get_lang('Answer').'</th>';
  62. $html .= $comment_title.$feedback_title;
  63. $html .= '<th>'.get_lang('Score').'</th>';
  64. $html .= '</tr>';
  65. $html .= '</thead>';
  66. $html .= '<tbody>';
  67. $form->addHeader(get_lang('Answers'));
  68. $form->addHtml($html);
  69. $defaults = [];
  70. $correct = 0;
  71. $answer = false;
  72. if (!empty($this->id)) {
  73. $answer = new Answer($this->id);
  74. $answer->read();
  75. if ($answer->nbrAnswers > 0 && !$form->isSubmitted()) {
  76. $nb_answers = $answer->nbrAnswers;
  77. }
  78. }
  79. $temp_scenario = [];
  80. if ($nb_answers < 1) {
  81. $nb_answers = 1;
  82. echo Display::return_message(get_lang('You have to create at least one answer'));
  83. }
  84. $editQuestion = isset($_GET['editQuestion']) ? $_GET['editQuestion'] : false;
  85. if ($editQuestion) {
  86. //fixing $nb_answers
  87. $new_list = [];
  88. $count = 1;
  89. if (isset($_POST['lessAnswers'])) {
  90. if (!isset($_SESSION['less_answer'])) {
  91. $_SESSION['less_answer'] = $this->id;
  92. $nb_answers--;
  93. }
  94. }
  95. for ($k = 1; $k <= $nb_answers; $k++) {
  96. if ($answer->position[$k] != '666') {
  97. $new_list[$count] = $count;
  98. $count++;
  99. }
  100. }
  101. } else {
  102. for ($k = 1; $k <= $nb_answers; $k++) {
  103. $new_list[$k] = $k;
  104. }
  105. }
  106. foreach ($new_list as $key) {
  107. $i = $key;
  108. $form->addElement('html', '<tr>');
  109. if (is_object($answer)) {
  110. if ($answer->position[$i] == 666) {
  111. //we set nothing
  112. } else {
  113. if ($answer->correct[$i]) {
  114. $correct = $i;
  115. }
  116. $answer_result = $answer->answer[$i];
  117. $weight_result = float_format($answer->weighting[$i], 1);
  118. if ($nb_answers == $i) {
  119. $weight_result = '0';
  120. }
  121. $defaults['answer['.$i.']'] = $answer_result;
  122. $defaults['comment['.$i.']'] = $answer->comment[$i];
  123. $defaults['weighting['.$i.']'] = $weight_result;
  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. }
  144. }
  145. $defaults['scenario'] = $temp_scenario;
  146. $renderer = &$form->defaultRenderer();
  147. $renderer->setElementTemplate(
  148. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  149. 'correct'
  150. );
  151. $renderer->setElementTemplate(
  152. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  153. 'counter['.$i.']'
  154. );
  155. $renderer->setElementTemplate(
  156. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  157. 'answer['.$i.']'
  158. );
  159. $renderer->setElementTemplate(
  160. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  161. 'comment['.$i.']'
  162. );
  163. $renderer->setElementTemplate(
  164. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  165. 'weighting['.$i.']'
  166. );
  167. $answer_number = $form->addElement('text', 'counter['.$i.']', null, 'value="'.$i.'"');
  168. $answer_number->freeze();
  169. $form->addElement('radio', 'correct', null, null, $i, 'class="checkbox" style="margin-left: 0em;"');
  170. $form->addElement('html_editor', 'answer['.$i.']', null, [], $editor_config);
  171. $form->addElement('html_editor', 'comment['.$i.']', null, [], $editor_config);
  172. $form->addElement('text', 'weighting['.$i.']', null, ['style' => 'width: 60px;', 'value' => '0']);
  173. $form->addElement('html', '</tr>');
  174. $i++;
  175. }
  176. if (empty($this->id)) {
  177. $form->addElement('hidden', 'new_question', 1);
  178. }
  179. //Adding the "I don't know" question answer
  180. //if (empty($this -> id)) {
  181. $i = 666;
  182. $form->addHtml('<tr>');
  183. $defaults["counter[$i]"] = '-';
  184. $defaults['answer['.$i.']'] = get_lang('Don\'t know');
  185. $defaults['weighting['.$i.']'] = '0';
  186. $defaults['scenario'] = $temp_scenario;
  187. $renderer = &$form->defaultRenderer();
  188. $renderer->setElementTemplate(
  189. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  190. 'correct'
  191. );
  192. $renderer->setElementTemplate(
  193. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  194. 'counter['.$i.']'
  195. );
  196. $renderer->setElementTemplate(
  197. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  198. 'answer['.$i.']'
  199. );
  200. $renderer->setElementTemplate(
  201. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  202. 'comment['.$i.']'
  203. );
  204. $renderer->setElementTemplate(
  205. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  206. 'weighting['.$i.']'
  207. );
  208. $form
  209. ->addElement('text', 'counter['.$i.']', null)
  210. ->freeze();
  211. $form->addElement('hidden', 'position['.$i.']', '666');
  212. $form->addElement('radio', 'correct', null, null, $i, ['class' => 'checkbox', 'disabled' => true]);
  213. $form->addElement('html_editor', 'answer['.$i.']', null, [], $editor_config);
  214. $form->addRule('answer['.$i.']', get_lang('Required field'), 'required');
  215. $form->addElement('html_editor', 'comment['.$i.']', null, [], $editor_config);
  216. $form->addElement('text', "weighting[$i]", null)->freeze();
  217. $form->addHTml('</tr>');
  218. $form->addHtml('</tbody></table>');
  219. $buttonGroup = [];
  220. global $text;
  221. //ie6 fix
  222. if ($obj_ex->edit_exercise_in_lp == true ||
  223. (empty($this->exerciseList) && empty($obj_ex->id))
  224. ) {
  225. //setting the save button here and not in the question class.php
  226. $buttonGroup[] = $form->addButtonDelete(get_lang('Remove answer option'), 'lessAnswers', true);
  227. $buttonGroup[] = $form->addButtonCreate(get_lang('Add answer option'), 'moreAnswers', true);
  228. $buttonGroup[] = $form->addButtonSave($text, 'submitQuestion', true);
  229. $form->addGroup($buttonGroup);
  230. }
  231. //We check the first radio button to be sure a radio button will be check
  232. if ($correct == 0) {
  233. $correct = 1;
  234. }
  235. $defaults['correct'] = $correct;
  236. if (!empty($this->id)) {
  237. $form->setDefaults($defaults);
  238. } else {
  239. $form->setDefaults($defaults);
  240. }
  241. $form->addElement('hidden', 'nb_answers');
  242. $form->setConstants(['nb_answers' => $nb_answers]);
  243. }
  244. /**
  245. * {@inheritdoc}
  246. */
  247. public function processAnswersCreation($form, $exercise)
  248. {
  249. $questionWeighting = $nbrGoodAnswers = 0;
  250. $correct = $form->getSubmitValue('correct');
  251. $objAnswer = new Answer($this->id);
  252. $nb_answers = $form->getSubmitValue('nb_answers');
  253. $minus = 1;
  254. if ($form->getSubmitValue('new_question')) {
  255. $minus = 0;
  256. }
  257. for ($i = 1; $i <= $nb_answers - $minus; $i++) {
  258. $position = trim($form->getSubmitValue('position['.$i.']'));
  259. $answer = trim($form->getSubmitValue('answer['.$i.']'));
  260. $comment = trim($form->getSubmitValue('comment['.$i.']'));
  261. $weighting = trim($form->getSubmitValue('weighting['.$i.']'));
  262. $scenario = $form->getSubmitValue('scenario');
  263. //$list_destination = $form -> getSubmitValue('destination'.$i);
  264. //$destination_str = $form -> getSubmitValue('destination'.$i);
  265. $try = $scenario['try'.$i];
  266. $lp = $scenario['lp'.$i];
  267. $destination = $scenario['destination'.$i];
  268. $url = trim($scenario['url'.$i]);
  269. /*
  270. How we are going to parse the destination value
  271. here we parse the destination value which is a string
  272. 1@@3@@2;4;4;@@http://www.chamilo.org
  273. where: try_again@@lp_id@@selected_questions@@url
  274. try_again = is 1 || 0
  275. lp_id = id of a learning path (0 if dont select)
  276. selected_questions= ids of questions
  277. url= an url
  278. */
  279. /*
  280. $destination_str='';
  281. foreach ($list_destination as $destination_id)
  282. {
  283. $destination_str.=$destination_id.';';
  284. }*/
  285. $goodAnswer = ($correct == $i) ? true : false;
  286. if ($goodAnswer) {
  287. $nbrGoodAnswers++;
  288. $weighting = abs($weighting);
  289. if ($weighting > 0) {
  290. $questionWeighting += $weighting;
  291. }
  292. }
  293. if (empty($try)) {
  294. $try = 0;
  295. }
  296. if (empty($lp)) {
  297. $lp = 0;
  298. }
  299. if (empty($destination)) {
  300. $destination = 0;
  301. }
  302. if ($url == '') {
  303. $url = 0;
  304. }
  305. //1@@1;2;@@2;4;4;@@http://www.chamilo.org
  306. $dest = $try.'@@'.$lp.'@@'.$destination.'@@'.$url;
  307. $objAnswer->createAnswer(
  308. $answer,
  309. $goodAnswer,
  310. $comment,
  311. $weighting,
  312. $i,
  313. null,
  314. null,
  315. $dest
  316. );
  317. }
  318. //Create 666 answer
  319. $i = 666;
  320. $answer = trim($form->getSubmitValue('answer['.$i.']'));
  321. $comment = trim($form->getSubmitValue('comment['.$i.']'));
  322. $weighting = trim($form->getSubmitValue('weighting['.$i.']'));
  323. $goodAnswer = $correct == $i ? true : false;
  324. $dest = '';
  325. $objAnswer->createAnswer(
  326. $answer,
  327. $goodAnswer,
  328. $comment,
  329. $weighting,
  330. $i,
  331. null,
  332. null,
  333. $dest
  334. );
  335. // saves the answers into the data base
  336. $objAnswer->save();
  337. // sets the total weighting of the question
  338. $this->updateWeighting($questionWeighting);
  339. $this->save($exercise);
  340. }
  341. /**
  342. * {@inheritdoc}
  343. */
  344. public function return_header(Exercise $exercise, $counter = null, $score = [])
  345. {
  346. $header = parent::return_header($exercise, $counter, $score);
  347. $header .= '<table class="'.$this->question_table_class.'"><tr>';
  348. if (!in_array($exercise->results_disabled, [
  349. RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
  350. ])
  351. ) {
  352. $header .= '<th>'.get_lang('Your choice').'</th>';
  353. if ($exercise->showExpectedChoiceColumn()) {
  354. $header .= '<th>'.get_lang('ExpectedYour choice').'</th>';
  355. }
  356. }
  357. $header .= '<th>'.get_lang('Answer').'</th>';
  358. if ($exercise->showExpectedChoice()) {
  359. $header .= '<th>'.get_lang('Status').'</th>';
  360. }
  361. $header .= '<th>'.get_lang('Comment').'</th>';
  362. $header .= '</tr>';
  363. return $header;
  364. }
  365. }