unique_answer.class.php 17 KB

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