unique_answer.class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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 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(), 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 = isset($item_list[0]) ? $item_list[0] : '';
  128. $lp = isset($item_list[1]) ? $item_list[1] : '';
  129. $list_dest = isset($item_list[2]) ? $item_list[2] : '';
  130. $url = isset($item_list[3]) ? $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('value' => '0'));
  227. $form->addHtml('</tr>');
  228. }
  229. $form->addHtml('</tbody>');
  230. $form->addHtml('</table>');
  231. global $text;
  232. $buttonGroup = [];
  233. //ie6 fix
  234. if ($obj_ex->edit_exercise_in_lp == true) {
  235. //setting the save button here and not in the question class.php
  236. $buttonGroup[] = $form->addButtonDelete(get_lang('LessAnswer'), 'lessAnswers', true);
  237. $buttonGroup[] = $form->addButtonCreate(get_lang('PlusAnswer'), 'moreAnswers', true);
  238. $buttonGroup[] = $form->addButtonSave($text, 'submitQuestion', true);
  239. $form->addGroup($buttonGroup);
  240. }
  241. // We check the first radio button to be sure a radio button will be check
  242. if ($correct == 0) {
  243. $correct = 1;
  244. }
  245. $defaults['correct'] = $correct;
  246. if (!empty($this->id)) {
  247. $form->setDefaults($defaults);
  248. } else {
  249. if ($this->isContent == 1) {
  250. // Default sample content.
  251. $form->setDefaults($defaults);
  252. } else {
  253. $form->setDefaults(array('correct' => 1));
  254. }
  255. }
  256. $form->setConstants(array('nb_answers' => $nb_answers));
  257. }
  258. /**
  259. * Receives the unique answer question type creation form data and creates
  260. * or updates the answers from that question
  261. * @param FormValidator $form
  262. */
  263. public function processAnswersCreation($form)
  264. {
  265. $questionWeighting = $nbrGoodAnswers = 0;
  266. $correct = $form->getSubmitValue('correct');
  267. $objAnswer = new Answer($this->id);
  268. $nb_answers = $form->getSubmitValue('nb_answers');
  269. for ($i = 1; $i <= $nb_answers; $i++) {
  270. $answer = trim($form->getSubmitValue('answer[' . $i . ']'));
  271. $comment = trim($form->getSubmitValue('comment[' . $i . ']'));
  272. $weighting = trim($form->getSubmitValue('weighting[' . $i . ']'));
  273. $scenario = $form->getSubmitValue('scenario');
  274. //$list_destination = $form -> getSubmitValue('destination'.$i);
  275. //$destination_str = $form -> getSubmitValue('destination'.$i);
  276. $try = $scenario['try' . $i];
  277. $lp = $scenario['lp' . $i];
  278. $destination = $scenario['destination' . $i];
  279. $url = trim($scenario['url' . $i]);
  280. /*
  281. How we are going to parse the destination value
  282. here we parse the destination value which is a string
  283. 1@@3@@2;4;4;@@http://www.chamilo.org
  284. where: try_again@@lp_id@@selected_questions@@url
  285. try_again = is 1 || 0
  286. lp_id = id of a learning path (0 if dont select)
  287. selected_questions= ids of questions
  288. url= an url
  289. $destination_str='';
  290. foreach ($list_destination as $destination_id)
  291. {
  292. $destination_str.=$destination_id.';';
  293. }*/
  294. $goodAnswer = ($correct == $i) ? true : false;
  295. if ($goodAnswer) {
  296. $nbrGoodAnswers++;
  297. $weighting = abs($weighting);
  298. if ($weighting > 0) {
  299. $questionWeighting += $weighting;
  300. }
  301. }
  302. if (empty($try)) {
  303. $try = 0;
  304. }
  305. if (empty($lp)) {
  306. $lp = 0;
  307. }
  308. if (empty($destination)) {
  309. $destination = 0;
  310. }
  311. if ($url == '') {
  312. $url = 0;
  313. }
  314. //1@@1;2;@@2;4;4;@@http://www.chamilo.org
  315. $dest = $try . '@@' . $lp . '@@' . $destination . '@@' . $url;
  316. $objAnswer->createAnswer(
  317. $answer,
  318. $goodAnswer,
  319. $comment,
  320. $weighting,
  321. $i,
  322. null,
  323. null,
  324. $dest
  325. );
  326. }
  327. // saves the answers into the data base
  328. $objAnswer->save();
  329. // sets the total weighting of the question
  330. $this->updateWeighting($questionWeighting);
  331. $this->save();
  332. }
  333. /**
  334. * Helper function to print the column titles in the answers edition form
  335. * @param null $feedback_type The type of feedback influences what columns are shown to the editor
  336. * @param null $counter The number of answers to show, in case there should be pagination
  337. * @param null $score The maximum score for the question
  338. * @return string HTML string for a table header + a wrapper before it
  339. */
  340. public function return_header(
  341. $feedback_type = null,
  342. $counter = null,
  343. $score = null
  344. ) {
  345. $header = parent::return_header($feedback_type, $counter, $score);
  346. $header .= '<table class="' . $this->question_table_class . '">
  347. <tr>
  348. <th>' . get_lang("Choice") . '</th>
  349. <th>' . get_lang("ExpectedChoice") . '</th>
  350. <th>' . get_lang("Answer") . '</th>';
  351. $header .= '<th>' . get_lang("Comment") . '</th>';
  352. $header .= '</tr>';
  353. return $header;
  354. }
  355. /**
  356. * Saves one answer to the database
  357. * @param int $id The ID of the answer (has to be calculated for this course)
  358. * @param int $question_id The question ID (to which the answer is attached)
  359. * @param string $title The text of the answer
  360. * @param string $comment The feedback for the answer
  361. * @param double $score The score you get when picking this answer
  362. * @param integer $correct Whether this answer is considered *the* correct one (this is the unique answer type)
  363. */
  364. public function addAnswer(
  365. $id,
  366. $question_id,
  367. $title,
  368. $comment,
  369. $score = 0.0,
  370. $correct = 0
  371. ) {
  372. $tbl_quiz_answer = Database::get_course_table(TABLE_QUIZ_ANSWER);
  373. $tbl_quiz_question = Database::get_course_table(TABLE_QUIZ_QUESTION);
  374. $course_id = api_get_course_int_id();
  375. $question_id = intval($question_id);
  376. $score = floatval($score);
  377. $correct = intval($correct);
  378. $title = Database::escape_string($title);
  379. $comment = Database::escape_string($comment);
  380. // Get the max position.
  381. $sql = "SELECT max(position) as max_position
  382. FROM $tbl_quiz_answer
  383. WHERE
  384. c_id = $course_id AND
  385. question_id = $question_id";
  386. $rs_max = Database::query($sql);
  387. $row_max = Database::fetch_object($rs_max);
  388. $position = $row_max->max_position + 1;
  389. // Insert a new answer
  390. $params = [
  391. 'c_id' => $course_id,
  392. 'id' => $id,
  393. 'question_id' => $question_id,
  394. 'answer' => $title,
  395. 'correct' => $correct,
  396. 'comment' => $comment,
  397. 'ponderation' => $score,
  398. 'position' => $position,
  399. 'destination' => '0@@0@@0@@0',
  400. ];
  401. $id = Database::insert($tbl_quiz_answer, $params);
  402. if ($id) {
  403. $sql = "UPDATE $tbl_quiz_answer SET id = iid WHERE iid = $id";
  404. Database::query($sql);
  405. }
  406. if ($correct) {
  407. $sql = "UPDATE $tbl_quiz_question
  408. SET ponderation = (ponderation + $score)
  409. WHERE c_id = $course_id AND id = " . $question_id;
  410. Database::query($sql);
  411. }
  412. }
  413. }