unique_answer.class.php 16 KB

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