unique_answer.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. use Chamilo\CourseBundle\Entity\CQuizAnswer;
  5. /**
  6. * Class UniqueAnswer
  7. *
  8. * This class allows to instantiate an object of type UNIQUE_ANSWER
  9. * (MULTIPLE CHOICE, UNIQUE ANSWER),
  10. * extending the class question
  11. *
  12. * @author Eric Marguin
  13. * @author Julio Montoya
  14. * @package chamilo.exercise
  15. **/
  16. class UniqueAnswer extends Question
  17. {
  18. public static $typePicture = 'mcua.png';
  19. public static $explanationLangVar = 'UniqueSelect';
  20. /**
  21. * Constructor
  22. */
  23. public function __construct()
  24. {
  25. parent::__construct();
  26. $this->type = UNIQUE_ANSWER;
  27. $this->isContent = $this->getIsContent();
  28. }
  29. /**
  30. * @inheritdoc
  31. */
  32. public function createAnswersForm($form)
  33. {
  34. // Getting the exercise list
  35. $obj_ex = Session::read('objExercise');
  36. $editor_config = array(
  37. 'ToolbarSet' => 'TestProposedAnswer',
  38. 'Width' => '100%',
  39. 'Height' => '125'
  40. );
  41. //this line defines how many questions by default appear when creating a choice question
  42. // The previous default value was 2. See task #1759.
  43. $nb_answers = isset($_POST['nb_answers']) ? (int) $_POST['nb_answers'] : 4;
  44. $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
  45. /*
  46. Types of Feedback
  47. $feedback_option[0]=get_lang('Feedback');
  48. $feedback_option[1]=get_lang('DirectFeedback');
  49. $feedback_option[2]=get_lang('NoFeedback');
  50. */
  51. $feedback_title = '';
  52. if ($obj_ex->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  53. //Scenario
  54. $comment_title = '<th width="20%">'.get_lang('Comment').'</th>';
  55. $feedback_title = '<th width="20%">'.get_lang('Scenario').'</th>';
  56. } else {
  57. $comment_title = '<th width="40%">'.get_lang('Comment').'</th>';
  58. }
  59. $html = '<table class="table table-striped table-hover">
  60. <thead>
  61. <tr style="text-align: center;">
  62. <th width="5%">' . get_lang('Number').'</th>
  63. <th width="5%"> ' . get_lang('True').'</th>
  64. <th width="40%">' . get_lang('Answer').'</th>
  65. ' . $comment_title.'
  66. ' . $feedback_title.'
  67. <th width="10%">' . get_lang('Weighting').'</th>
  68. </tr>
  69. </thead>
  70. <tbody>';
  71. $form->addHeader(get_lang('Answers'));
  72. $form->addHtml($html);
  73. $defaults = array();
  74. $correct = 0;
  75. if (!empty($this->id)) {
  76. $answer = new Answer($this->id);
  77. $answer->read();
  78. if (count($answer->nbrAnswers) > 0 && !$form->isSubmitted()) {
  79. $nb_answers = $answer->nbrAnswers;
  80. }
  81. }
  82. $form->addElement('hidden', 'nb_answers');
  83. //Feedback SELECT
  84. $question_list = $obj_ex->selectQuestionList();
  85. $select_question = array();
  86. $select_question[0] = get_lang('SelectTargetQuestion');
  87. if (is_array($question_list)) {
  88. foreach ($question_list as $key => $questionid) {
  89. //To avoid warning messages
  90. if (!is_numeric($questionid)) {
  91. continue;
  92. }
  93. $question = Question::read($questionid);
  94. $select_question[$questionid] = 'Q'.$key.' :'.cut(
  95. $question->selectTitle(),
  96. 20
  97. );
  98. }
  99. }
  100. $select_question[-1] = get_lang('ExitTest');
  101. $list = new LearnpathList(api_get_user_id());
  102. $flat_list = $list->get_flat_list();
  103. $select_lp_id = array();
  104. $select_lp_id[0] = get_lang('SelectTargetLP');
  105. foreach ($flat_list as $id => $details) {
  106. $select_lp_id[$id] = cut($details['lp_name'], 20);
  107. }
  108. $temp_scenario = array();
  109. if ($nb_answers < 1) {
  110. $nb_answers = 1;
  111. echo Display::return_message(
  112. get_lang('YouHaveToCreateAtLeastOneAnswer')
  113. );
  114. }
  115. for ($i = 1; $i <= $nb_answers; ++$i) {
  116. $form->addHtml('<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.']'] = float_format(
  124. $answer->weighting[$i],
  125. 1
  126. );
  127. $item_list = explode('@@', $answer->destination[$i]);
  128. $try = isset($item_list[0]) ? $item_list[0] : '';
  129. $lp = isset($item_list[1]) ? $item_list[1] : '';
  130. $list_dest = isset($item_list[2]) ? $item_list[2] : '';
  131. $url = isset($item_list[3]) ? $item_list[3] : '';
  132. if ($try == 0) {
  133. $try_result = 0;
  134. } else {
  135. $try_result = 1;
  136. }
  137. if ($url == 0) {
  138. $url_result = '';
  139. } else {
  140. $url_result = $url;
  141. }
  142. $temp_scenario['url'.$i] = $url_result;
  143. $temp_scenario['try'.$i] = $try_result;
  144. $temp_scenario['lp'.$i] = $lp;
  145. $temp_scenario['destination'.$i] = $list_dest;
  146. } else {
  147. $defaults['answer[1]'] = get_lang('DefaultUniqueAnswer1');
  148. $defaults['weighting[1]'] = 10;
  149. $defaults['answer[2]'] = get_lang('DefaultUniqueAnswer2');
  150. $defaults['weighting[2]'] = 0;
  151. $temp_scenario['destination'.$i] = array('0');
  152. $temp_scenario['lp'.$i] = array('0');
  153. }
  154. $defaults['scenario'] = $temp_scenario;
  155. $renderer = $form->defaultRenderer();
  156. $renderer->setElementTemplate(
  157. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  158. 'correct'
  159. );
  160. $renderer->setElementTemplate(
  161. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  162. 'counter['.$i.']'
  163. );
  164. $renderer->setElementTemplate(
  165. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  166. 'answer['.$i.']'
  167. );
  168. $renderer->setElementTemplate(
  169. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  170. 'comment['.$i.']'
  171. );
  172. $renderer->setElementTemplate(
  173. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  174. 'weighting['.$i.']'
  175. );
  176. $answer_number = $form->addElement(
  177. 'text',
  178. 'counter['.$i.']',
  179. null,
  180. ' value = "'.$i.'"'
  181. );
  182. $answer_number->freeze();
  183. $form->addElement(
  184. 'radio',
  185. 'correct',
  186. null,
  187. null,
  188. $i,
  189. 'class="checkbox"'
  190. );
  191. $form->addHtmlEditor('answer['.$i.']', null, null, false, $editor_config);
  192. $form->addRule(
  193. 'answer['.$i.']',
  194. get_lang('ThisFieldIsRequired'),
  195. 'required'
  196. );
  197. if ($obj_ex->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  198. $form->addHtmlEditor(
  199. 'comment['.$i.']',
  200. null,
  201. null,
  202. false,
  203. $editor_config
  204. );
  205. // Direct feedback
  206. //Adding extra feedback fields
  207. $group = array();
  208. $group['try'.$i] = $form->createElement(
  209. 'checkbox',
  210. 'try'.$i,
  211. null,
  212. get_lang('TryAgain')
  213. );
  214. $group['lp'.$i] = $form->createElement(
  215. 'select',
  216. 'lp'.$i,
  217. get_lang('SeeTheory').': ',
  218. $select_lp_id
  219. );
  220. $group['destination'.$i] = $form->createElement(
  221. 'select',
  222. 'destination'.$i,
  223. get_lang('GoToQuestion').': ',
  224. $select_question
  225. );
  226. $group['url'.$i] = $form->createElement(
  227. 'text',
  228. 'url'.$i,
  229. get_lang('Other').': ',
  230. array(
  231. 'class' => 'col-md-2',
  232. 'placeholder' => get_lang('Other')
  233. )
  234. );
  235. $form->addGroup($group, 'scenario');
  236. $renderer->setElementTemplate(
  237. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}',
  238. 'scenario'
  239. );
  240. } else {
  241. $form->addHtmlEditor('comment['.$i.']', null, null, false, $editor_config);
  242. }
  243. $form->addText('weighting['.$i.']', null, null, array('value' => '0'));
  244. $form->addHtml('</tr>');
  245. }
  246. $form->addHtml('</tbody>');
  247. $form->addHtml('</table>');
  248. global $text;
  249. $buttonGroup = [];
  250. //ie6 fix
  251. if ($obj_ex->edit_exercise_in_lp == true) {
  252. //setting the save button here and not in the question class.php
  253. $buttonGroup[] = $form->addButtonDelete(get_lang('LessAnswer'), 'lessAnswers', true);
  254. $buttonGroup[] = $form->addButtonCreate(get_lang('PlusAnswer'), 'moreAnswers', true);
  255. $buttonGroup[] = $form->addButton(
  256. 'submitQuestion',
  257. $text,
  258. 'check',
  259. 'primary',
  260. 'default',
  261. null,
  262. ['id' => 'submit-question'],
  263. true
  264. );
  265. $form->addGroup($buttonGroup);
  266. }
  267. // We check the first radio button to be sure a radio button will be check
  268. if ($correct == 0) {
  269. $correct = 1;
  270. }
  271. $defaults['correct'] = $correct;
  272. if (!empty($this->id)) {
  273. $form->setDefaults($defaults);
  274. } else {
  275. if ($this->isContent == 1) {
  276. // Default sample content.
  277. $form->setDefaults($defaults);
  278. } else {
  279. $form->setDefaults(array('correct' => 1));
  280. }
  281. }
  282. $form->setConstants(array('nb_answers' => $nb_answers));
  283. }
  284. /**
  285. * @inheritdoc
  286. */
  287. public function processAnswersCreation($form, $exercise)
  288. {
  289. $questionWeighting = $nbrGoodAnswers = 0;
  290. $correct = $form->getSubmitValue('correct');
  291. $objAnswer = new Answer($this->id);
  292. $nb_answers = $form->getSubmitValue('nb_answers');
  293. for ($i = 1; $i <= $nb_answers; $i++) {
  294. $answer = trim($form->getSubmitValue('answer['.$i.']'));
  295. $comment = trim($form->getSubmitValue('comment['.$i.']'));
  296. $weighting = trim($form->getSubmitValue('weighting['.$i.']'));
  297. $scenario = $form->getSubmitValue('scenario');
  298. //$list_destination = $form -> getSubmitValue('destination'.$i);
  299. //$destination_str = $form -> getSubmitValue('destination'.$i);
  300. $try = $scenario['try'.$i];
  301. $lp = $scenario['lp'.$i];
  302. $destination = $scenario['destination'.$i];
  303. $url = trim($scenario['url'.$i]);
  304. /*
  305. How we are going to parse the destination value
  306. here we parse the destination value which is a string
  307. 1@@3@@2;4;4;@@http://www.chamilo.org
  308. where: try_again@@lp_id@@selected_questions@@url
  309. try_again = is 1 || 0
  310. lp_id = id of a learning path (0 if dont select)
  311. selected_questions= ids of questions
  312. url= an url
  313. $destination_str='';
  314. foreach ($list_destination as $destination_id)
  315. {
  316. $destination_str.=$destination_id.';';
  317. }*/
  318. $goodAnswer = $correct == $i ? true : false;
  319. if ($goodAnswer) {
  320. $nbrGoodAnswers++;
  321. $weighting = abs($weighting);
  322. if ($weighting > 0) {
  323. $questionWeighting += $weighting;
  324. }
  325. }
  326. if (empty($try)) {
  327. $try = 0;
  328. }
  329. if (empty($lp)) {
  330. $lp = 0;
  331. }
  332. if (empty($destination)) {
  333. $destination = 0;
  334. }
  335. if ($url == '') {
  336. $url = 0;
  337. }
  338. //1@@1;2;@@2;4;4;@@http://www.chamilo.org
  339. $dest = $try.'@@'.$lp.'@@'.$destination.'@@'.$url;
  340. $objAnswer->createAnswer(
  341. $answer,
  342. $goodAnswer,
  343. $comment,
  344. $weighting,
  345. $i,
  346. null,
  347. null,
  348. $dest
  349. );
  350. }
  351. // saves the answers into the data base
  352. $objAnswer->save();
  353. // sets the total weighting of the question
  354. $this->updateWeighting($questionWeighting);
  355. $this->save($exercise);
  356. }
  357. /**
  358. * @inheritdoc
  359. */
  360. public function return_header(
  361. $exercise,
  362. $counter = null,
  363. $score = null
  364. ) {
  365. $header = parent::return_header($exercise, $counter, $score);
  366. $header .= '<table class="'.$this->question_table_class.'">
  367. <tr>
  368. <th>'.get_lang("Choice").'</th>
  369. <th>'.get_lang("ExpectedChoice").'</th>
  370. <th>'.get_lang("Answer").'</th>';
  371. $header .= '<th>'.get_lang("Comment").'</th>';
  372. $header .= '</tr>';
  373. return $header;
  374. }
  375. /**
  376. * Saves one answer to the database
  377. * @param int $id The ID of the answer (has to be calculated for this course)
  378. * @param int $question_id The question ID (to which the answer is attached)
  379. * @param string $title The text of the answer
  380. * @param string $comment The feedback for the answer
  381. * @param double $score The score you get when picking this answer
  382. * @param integer $correct Whether this answer is considered *the* correct one (this is the unique answer type)
  383. */
  384. public function addAnswer(
  385. $id,
  386. $question_id,
  387. $title,
  388. $comment,
  389. $score = 0.0,
  390. $correct = 0
  391. ) {
  392. $em = Database::getManager();
  393. $tbl_quiz_answer = Database::get_course_table(TABLE_QUIZ_ANSWER);
  394. $tbl_quiz_question = Database::get_course_table(TABLE_QUIZ_QUESTION);
  395. $course_id = api_get_course_int_id();
  396. $question_id = intval($question_id);
  397. $score = floatval($score);
  398. $correct = intval($correct);
  399. $title = Database::escape_string($title);
  400. $comment = Database::escape_string($comment);
  401. // Get the max position.
  402. $sql = "SELECT max(position) as max_position
  403. FROM $tbl_quiz_answer
  404. WHERE
  405. c_id = $course_id AND
  406. question_id = $question_id";
  407. $rs_max = Database::query($sql);
  408. $row_max = Database::fetch_object($rs_max);
  409. $position = $row_max->max_position + 1;
  410. // Insert a new answer
  411. $quizAnswer = new CQuizAnswer();
  412. $quizAnswer
  413. ->setCId($course_id)
  414. ->setId($id)
  415. ->setQuestionId($question_id)
  416. ->setAnswer($title)
  417. ->setCorrect($correct)
  418. ->setComment($comment)
  419. ->setPonderation($score)
  420. ->setPosition($position)
  421. ->setDestination('0@@0@@0@@0');
  422. $em->persist($quizAnswer);
  423. $em->flush();
  424. $id = $quizAnswer->getIid();
  425. if ($id) {
  426. $quizAnswer
  427. ->setId($id);
  428. $em->merge($quizAnswer);
  429. $em->flush();
  430. }
  431. if ($correct) {
  432. $sql = "UPDATE $tbl_quiz_question
  433. SET ponderation = (ponderation + $score)
  434. WHERE c_id = $course_id AND id = ".$question_id;
  435. Database::query($sql);
  436. }
  437. }
  438. }