UniqueAnswerImage.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * UniqueAnswerImage.
  6. *
  7. * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
  8. */
  9. class UniqueAnswerImage extends UniqueAnswer
  10. {
  11. public static $typePicture = 'uaimg.png';
  12. public static $explanationLangVar = 'UniqueAnswerImage';
  13. /**
  14. * UniqueAnswerImage constructor.
  15. */
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. $this->type = UNIQUE_ANSWER_IMAGE;
  20. $this->isContent = $this->getIsContent();
  21. }
  22. /**
  23. * {@inheritdoc}
  24. *
  25. * @throws Exception
  26. */
  27. public function createAnswersForm($form)
  28. {
  29. $objExercise = Session::read('objExercise');
  30. $editorConfig = [
  31. 'ToolbarSet' => 'TestProposedAnswer',
  32. 'Width' => '100%',
  33. 'Height' => '125',
  34. ];
  35. //this line defines how many questions by default appear when creating a choice question
  36. // The previous default value was 2. See task #1759.
  37. $numberAnswers = isset($_POST['nb_answers']) ? (int) $_POST['nb_answers'] : 4;
  38. $numberAnswers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
  39. $feedbackTitle = '';
  40. if ($objExercise->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  41. //Scenario
  42. $commentTitle = '<th>'.get_lang('Comment').'</th>';
  43. $feedbackTitle = '<th>'.get_lang('Scenario').'</th>';
  44. } else {
  45. $commentTitle = '<th >'.get_lang('Comment').'</th>';
  46. }
  47. $html = '<div class="alert alert-success" role="alert">'.
  48. get_lang('UniqueAnswerImagePreferredSize200x150').'</div>';
  49. $html .= '<table class="table table-striped table-hover">
  50. <thead>
  51. <tr style="text-align: center;">
  52. <th width="10">'.get_lang('Number').'</th>
  53. <th>'.get_lang('True').'</th>
  54. <th>'.get_lang('Answer').'</th>
  55. '.$commentTitle.'
  56. '.$feedbackTitle.'
  57. <th width="15">'.get_lang('Weighting').'</th>
  58. </tr>
  59. </thead>
  60. <tbody>';
  61. $form->addHeader(get_lang('Answers'));
  62. $form->addHtml($html);
  63. $defaults = [];
  64. $correct = 0;
  65. if (!empty($this->id)) {
  66. $answer = new Answer($this->id);
  67. $answer->read();
  68. if ($answer->nbrAnswers > 0 && !$form->isSubmitted()) {
  69. $numberAnswers = $answer->nbrAnswers;
  70. }
  71. }
  72. $form->addElement('hidden', 'nb_answers');
  73. //Feedback SELECT
  74. $questionList = $objExercise->selectQuestionList();
  75. $selectQuestion = [];
  76. $selectQuestion[0] = get_lang('SelectTargetQuestion');
  77. if (is_array($questionList)) {
  78. foreach ($questionList as $key => $questionid) {
  79. //To avoid warning messages
  80. if (!is_numeric($questionid)) {
  81. continue;
  82. }
  83. $question = Question::read($questionid);
  84. $questionTitle = strip_tags($question->selectTitle());
  85. $selectQuestion[$questionid] = "Q$key: $questionTitle";
  86. }
  87. }
  88. $selectQuestion[-1] = get_lang('ExitTest');
  89. $list = new LearnpathList(api_get_user_id());
  90. $flatList = $list->get_flat_list();
  91. $selectLpId = [];
  92. $selectLpId[0] = get_lang('SelectTargetLP');
  93. foreach ($flatList as $id => $details) {
  94. $selectLpId[$id] = cut($details['lp_name'], 20);
  95. }
  96. $tempScenario = [];
  97. if ($numberAnswers < 1) {
  98. $numberAnswers = 1;
  99. echo Display::return_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  100. }
  101. for ($i = 1; $i <= $numberAnswers; $i++) {
  102. $form->addHtml('<tr>');
  103. if (isset($answer) && is_object($answer)) {
  104. if ($answer->correct[$i]) {
  105. $correct = $i;
  106. }
  107. $defaults['answer['.$i.']'] = $answer->answer[$i];
  108. $defaults['comment['.$i.']'] = $answer->comment[$i];
  109. $defaults['weighting['.$i.']'] = float_format(
  110. $answer->weighting[$i],
  111. 1
  112. );
  113. $itemList = explode('@@', $answer->destination[$i]);
  114. $try = $itemList[0];
  115. $lp = $itemList[1];
  116. $listDestination = $itemList[2];
  117. $url = $itemList[3];
  118. $tryResult = 0;
  119. if ($try != 0) {
  120. $tryResult = 1;
  121. }
  122. $urlResult = '';
  123. if ($url != 0) {
  124. $urlResult = $url;
  125. }
  126. $tempScenario['url'.$i] = $urlResult;
  127. $tempScenario['try'.$i] = $tryResult;
  128. $tempScenario['lp'.$i] = $lp;
  129. $tempScenario['destination'.$i] = $listDestination;
  130. } else {
  131. $defaults['answer[1]'] = get_lang('DefaultUniqueAnswer1');
  132. $defaults['weighting[1]'] = 10;
  133. $defaults['answer[2]'] = get_lang('DefaultUniqueAnswer2');
  134. $defaults['weighting[2]'] = 0;
  135. $tempScenario['destination'.$i] = ['0'];
  136. $tempScenario['lp'.$i] = ['0'];
  137. }
  138. $defaults['scenario'] = $tempScenario;
  139. $renderer = $form->defaultRenderer();
  140. $renderer->setElementTemplate(
  141. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  142. 'correct'
  143. );
  144. $renderer->setElementTemplate(
  145. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  146. 'counter['.$i.']'
  147. );
  148. $renderer->setElementTemplate(
  149. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  150. 'answer['.$i.']'
  151. );
  152. $renderer->setElementTemplate(
  153. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  154. 'comment['.$i.']'
  155. );
  156. $renderer->setElementTemplate(
  157. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  158. 'weighting['.$i.']'
  159. );
  160. $answerNumber = $form->addElement('text', 'counter['.$i.']', null, ' value = "'.$i.'"');
  161. $answerNumber->freeze();
  162. $form->addElement('radio', 'correct', null, null, $i, 'class="checkbox"');
  163. $form->addHtmlEditor('answer['.$i.']', null, null, false, $editorConfig);
  164. $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
  165. if ($objExercise->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  166. $form->addHtmlEditor(
  167. 'comment['.$i.']',
  168. null,
  169. null,
  170. false,
  171. $editorConfig
  172. );
  173. // Direct feedback
  174. // Adding extra feedback fields
  175. $group = [];
  176. $group['try'.$i] = $form->createElement('checkbox', 'try'.$i, null, get_lang('TryAgain'));
  177. $group['lp'.$i] = $form->createElement(
  178. 'select',
  179. 'lp'.$i,
  180. get_lang('SeeTheory').': ',
  181. $selectLpId
  182. );
  183. $group['destination'.$i] = $form->createElement(
  184. 'select',
  185. 'destination'.$i,
  186. get_lang('GoToQuestion').': ',
  187. $selectQuestion
  188. );
  189. $group['url'.$i] = $form->createElement(
  190. 'text',
  191. 'url'.$i,
  192. get_lang('Other').': ',
  193. [
  194. 'class' => 'col-md-2',
  195. 'placeholder' => get_lang('Other'),
  196. ]
  197. );
  198. $form->addGroup($group, 'scenario');
  199. $renderer->setElementTemplate(
  200. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}',
  201. 'scenario'
  202. );
  203. } else {
  204. $form->addHtmlEditor('comment['.$i.']', null, null, false, $editorConfig);
  205. }
  206. $form->addText('weighting['.$i.']', null, null, ['class' => "col-md-1", 'value' => '0']);
  207. $form->addHtml('</tr>');
  208. }
  209. $form->addHtml('</tbody>');
  210. $form->addHtml('</table>');
  211. global $text;
  212. $buttonGroup = [];
  213. if ($objExercise->edit_exercise_in_lp == true ||
  214. (empty($this->exerciseList) && empty($objExercise->id))
  215. ) {
  216. //setting the save button here and not in the question class.php
  217. $buttonGroup[] = $form->addButtonDelete(get_lang('LessAnswer'), 'lessAnswers', true);
  218. $buttonGroup[] = $form->addButtonCreate(get_lang('PlusAnswer'), 'moreAnswers', true);
  219. $buttonGroup[] = $form->addButtonSave($text, 'submitQuestion', true);
  220. $form->addGroup($buttonGroup);
  221. }
  222. // We check the first radio button to be sure a radio button will be check
  223. if ($correct == 0) {
  224. $correct = 1;
  225. }
  226. $defaults['correct'] = $correct;
  227. if (!empty($this->id)) {
  228. $form->setDefaults($defaults);
  229. } else {
  230. if ($this->isContent == 1) {
  231. // Default sample content.
  232. $form->setDefaults($defaults);
  233. } else {
  234. $form->setDefaults(['correct' => 1]);
  235. }
  236. }
  237. $form->setConstants(['nb_answers' => $numberAnswers]);
  238. }
  239. /**
  240. * {@inheritdoc}
  241. */
  242. public function processAnswersCreation($form, $exercise)
  243. {
  244. $questionWeighting = $nbrGoodAnswers = 0;
  245. $correct = $form->getSubmitValue('correct');
  246. $objAnswer = new Answer($this->id);
  247. $numberAnswers = $form->getSubmitValue('nb_answers');
  248. for ($i = 1; $i <= $numberAnswers; $i++) {
  249. $answer = trim(str_replace(['<p>', '</p>'], '', $form->getSubmitValue('answer['.$i.']')));
  250. $comment = trim(str_replace(['<p>', '</p>'], '', $form->getSubmitValue('comment['.$i.']')));
  251. $weighting = trim($form->getSubmitValue('weighting['.$i.']'));
  252. $scenario = $form->getSubmitValue('scenario');
  253. //$listDestination = $form -> getSubmitValue('destination'.$i);
  254. //$destinationStr = $form -> getSubmitValue('destination'.$i);
  255. $try = $scenario['try'.$i];
  256. $lp = $scenario['lp'.$i];
  257. $destination = $scenario['destination'.$i];
  258. $url = trim($scenario['url'.$i]);
  259. /*
  260. How we are going to parse the destination value
  261. here we parse the destination value which is a string
  262. 1@@3@@2;4;4;@@http://www.chamilo.org
  263. where: try_again@@lp_id@@selected_questions@@url
  264. try_again = is 1 || 0
  265. lp_id = id of a learning path (0 if dont select)
  266. selected_questions= ids of questions
  267. url= an url
  268. $destinationStr='';
  269. foreach ($listDestination as $destination_id)
  270. {
  271. $destinationStr.=$destination_id.';';
  272. } */
  273. $goodAnswer = $correct == $i ? true : false;
  274. if ($goodAnswer) {
  275. $nbrGoodAnswers++;
  276. $weighting = abs($weighting);
  277. if ($weighting > 0) {
  278. $questionWeighting += $weighting;
  279. }
  280. }
  281. if (empty($try)) {
  282. $try = 0;
  283. }
  284. if (empty($lp)) {
  285. $lp = 0;
  286. }
  287. if (empty($destination)) {
  288. $destination = 0;
  289. }
  290. if ($url == '') {
  291. $url = 0;
  292. }
  293. //1@@1;2;@@2;4;4;@@http://www.chamilo.org
  294. $dest = $try.'@@'.$lp.'@@'.$destination.'@@'.$url;
  295. $objAnswer->createAnswer(
  296. $answer,
  297. $goodAnswer,
  298. $comment,
  299. $weighting,
  300. $i,
  301. null,
  302. null,
  303. $dest
  304. );
  305. }
  306. // saves the answers into the data base
  307. $objAnswer->save();
  308. // sets the total weighting of the question
  309. $this->updateWeighting($questionWeighting);
  310. $this->save($exercise);
  311. }
  312. /**
  313. * @param Exercise $exercise
  314. * @param null $counter
  315. * @param null $score
  316. *
  317. * @return string
  318. */
  319. public function return_header($exercise, $counter = null, $score = null)
  320. {
  321. if ($exercise->showExpectedChoice()) {
  322. $header = '<table class="'.$this->question_table_class.'">
  323. <tr>
  324. <th>'.get_lang('Choice').'</th>
  325. <th>'.get_lang('ExpectedChoice').'</th>
  326. <th>'.get_lang('Answer').'</th>';
  327. $header .= '<th>'.get_lang('Status').'</th>';
  328. $header .= '<th>'.get_lang('Comment').'</th>';
  329. $header .= '</tr>';
  330. } else {
  331. $header = parent::return_header($exercise, $counter, $score);
  332. }
  333. return $header;
  334. }
  335. }