UniqueAnswerImage.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 $typePicture = 'uaimg.png';
  12. public $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. switch ($objExercise->getFeedbackType()) {
  41. case EXERCISE_FEEDBACK_TYPE_DIRECT:
  42. // Scenario
  43. $commentTitle = '<th width="20%">'.get_lang('Comment').'</th>';
  44. $feedbackTitle = '<th width="20%">'.get_lang('Scenario').'</th>';
  45. break;
  46. case EXERCISE_FEEDBACK_TYPE_POPUP:
  47. $commentTitle = '<th width="20%">'.get_lang('Comment').'</th>';
  48. break;
  49. default:
  50. $commentTitle = '<th width="40%">'.get_lang('Comment').'</th>';
  51. break;
  52. }
  53. $html = '<div class="alert alert-success" role="alert">'.
  54. get_lang('UniqueAnswerImagePreferredSize200x150').'</div>';
  55. $html .= '<table class="table table-striped table-hover">
  56. <thead>
  57. <tr style="text-align: center;">
  58. <th width="10">'.get_lang('Number').'</th>
  59. <th>'.get_lang('True').'</th>
  60. <th>'.get_lang('Answer').'</th>
  61. '.$commentTitle.'
  62. '.$feedbackTitle.'
  63. <th width="15">'.get_lang('Weighting').'</th>
  64. </tr>
  65. </thead>
  66. <tbody>';
  67. $form->addHeader(get_lang('Answers'));
  68. $form->addHtml($html);
  69. $defaults = [];
  70. $correct = 0;
  71. if (!empty($this->id)) {
  72. $answer = new Answer($this->id);
  73. $answer->read();
  74. if ($answer->nbrAnswers > 0 && !$form->isSubmitted()) {
  75. $numberAnswers = $answer->nbrAnswers;
  76. }
  77. }
  78. $form->addElement('hidden', 'nb_answers');
  79. //Feedback SELECT
  80. $questionList = $objExercise->selectQuestionList();
  81. $selectQuestion = [];
  82. $selectQuestion[0] = get_lang('SelectTargetQuestion');
  83. if (is_array($questionList)) {
  84. foreach ($questionList as $key => $questionid) {
  85. //To avoid warning messages
  86. if (!is_numeric($questionid)) {
  87. continue;
  88. }
  89. $question = Question::read($questionid);
  90. $questionTitle = strip_tags($question->selectTitle());
  91. $selectQuestion[$questionid] = "Q$key: $questionTitle";
  92. }
  93. }
  94. $selectQuestion[-1] = get_lang('ExitTest');
  95. $list = new LearnpathList(api_get_user_id());
  96. $flatList = $list->get_flat_list();
  97. $selectLpId = [];
  98. $selectLpId[0] = get_lang('SelectTargetLP');
  99. foreach ($flatList as $id => $details) {
  100. $selectLpId[$id] = cut($details['lp_name'], 20);
  101. }
  102. $tempScenario = [];
  103. if ($numberAnswers < 1) {
  104. $numberAnswers = 1;
  105. echo Display::return_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  106. }
  107. for ($i = 1; $i <= $numberAnswers; $i++) {
  108. $form->addHtml('<tr>');
  109. if (isset($answer) && is_object($answer)) {
  110. if ($answer->correct[$i]) {
  111. $correct = $i;
  112. }
  113. $defaults['answer['.$i.']'] = $answer->answer[$i];
  114. $defaults['comment['.$i.']'] = $answer->comment[$i];
  115. $defaults['weighting['.$i.']'] = float_format(
  116. $answer->weighting[$i],
  117. 1
  118. );
  119. $itemList = explode('@@', $answer->destination[$i]);
  120. $try = $itemList[0];
  121. $lp = $itemList[1];
  122. $listDestination = $itemList[2];
  123. $url = $itemList[3];
  124. $tryResult = 0;
  125. if ($try != 0) {
  126. $tryResult = 1;
  127. }
  128. $urlResult = '';
  129. if ($url != 0) {
  130. $urlResult = $url;
  131. }
  132. $tempScenario['url'.$i] = $urlResult;
  133. $tempScenario['try'.$i] = $tryResult;
  134. $tempScenario['lp'.$i] = $lp;
  135. $tempScenario['destination'.$i] = $listDestination;
  136. } else {
  137. $defaults['answer[1]'] = get_lang('DefaultUniqueAnswer1');
  138. $defaults['weighting[1]'] = 10;
  139. $defaults['answer[2]'] = get_lang('DefaultUniqueAnswer2');
  140. $defaults['weighting[2]'] = 0;
  141. $tempScenario['destination'.$i] = ['0'];
  142. $tempScenario['lp'.$i] = ['0'];
  143. }
  144. $defaults['scenario'] = $tempScenario;
  145. $renderer = $form->defaultRenderer();
  146. $renderer->setElementTemplate(
  147. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  148. 'correct'
  149. );
  150. $renderer->setElementTemplate(
  151. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  152. 'counter['.$i.']'
  153. );
  154. $renderer->setElementTemplate(
  155. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  156. 'answer['.$i.']'
  157. );
  158. $renderer->setElementTemplate(
  159. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  160. 'comment['.$i.']'
  161. );
  162. $renderer->setElementTemplate(
  163. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  164. 'weighting['.$i.']'
  165. );
  166. $answerNumber = $form->addElement('text', 'counter['.$i.']', null, ' value = "'.$i.'"');
  167. $answerNumber->freeze();
  168. $form->addElement('radio', 'correct', null, null, $i, 'class="checkbox"');
  169. $form->addHtmlEditor('answer['.$i.']', null, null, false, $editorConfig);
  170. $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
  171. switch ($objExercise->getFeedbackType()) {
  172. case EXERCISE_FEEDBACK_TYPE_DIRECT:
  173. $this->setDirectOptions($i, $form, $renderer, $selectLpId, $selectQuestion);
  174. break;
  175. case EXERCISE_FEEDBACK_TYPE_POPUP:
  176. default:
  177. $form->addHtmlEditor('comment['.$i.']', null, null, false, $editorConfig);
  178. break;
  179. }
  180. $form->addText('weighting['.$i.']', null, null, ['class' => 'col-md-1', 'value' => '0']);
  181. $form->addHtml('</tr>');
  182. }
  183. $form->addHtml('</tbody>');
  184. $form->addHtml('</table>');
  185. global $text;
  186. $buttonGroup = [];
  187. if ($objExercise->edit_exercise_in_lp == true ||
  188. (empty($this->exerciseList) && empty($objExercise->id))
  189. ) {
  190. //setting the save button here and not in the question class.php
  191. $buttonGroup[] = $form->addButtonDelete(get_lang('LessAnswer'), 'lessAnswers', true);
  192. $buttonGroup[] = $form->addButtonCreate(get_lang('PlusAnswer'), 'moreAnswers', true);
  193. $buttonGroup[] = $form->addButtonSave($text, 'submitQuestion', true);
  194. $form->addGroup($buttonGroup);
  195. }
  196. // We check the first radio button to be sure a radio button will be check
  197. if ($correct == 0) {
  198. $correct = 1;
  199. }
  200. $defaults['correct'] = $correct;
  201. if (!empty($this->id)) {
  202. $form->setDefaults($defaults);
  203. } else {
  204. if ($this->isContent == 1) {
  205. // Default sample content.
  206. $form->setDefaults($defaults);
  207. } else {
  208. $form->setDefaults(['correct' => 1]);
  209. }
  210. }
  211. $form->setConstants(['nb_answers' => $numberAnswers]);
  212. }
  213. /**
  214. * {@inheritdoc}
  215. */
  216. public function processAnswersCreation($form, $exercise)
  217. {
  218. $questionWeighting = $nbrGoodAnswers = 0;
  219. $correct = $form->getSubmitValue('correct');
  220. $objAnswer = new Answer($this->id);
  221. $numberAnswers = $form->getSubmitValue('nb_answers');
  222. for ($i = 1; $i <= $numberAnswers; $i++) {
  223. $answer = trim(str_replace(['<p>', '</p>'], '', $form->getSubmitValue('answer['.$i.']')));
  224. $comment = trim(str_replace(['<p>', '</p>'], '', $form->getSubmitValue('comment['.$i.']')));
  225. $weighting = trim($form->getSubmitValue('weighting['.$i.']'));
  226. $scenario = $form->getSubmitValue('scenario');
  227. //$listDestination = $form -> getSubmitValue('destination'.$i);
  228. //$destinationStr = $form -> getSubmitValue('destination'.$i);
  229. $try = $scenario['try'.$i];
  230. $lp = $scenario['lp'.$i];
  231. $destination = $scenario['destination'.$i];
  232. $url = trim($scenario['url'.$i]);
  233. /*
  234. How we are going to parse the destination value
  235. here we parse the destination value which is a string
  236. 1@@3@@2;4;4;@@http://www.chamilo.org
  237. where: try_again@@lp_id@@selected_questions@@url
  238. try_again = is 1 || 0
  239. lp_id = id of a learning path (0 if dont select)
  240. selected_questions= ids of questions
  241. url= an url
  242. $destinationStr='';
  243. foreach ($listDestination as $destination_id)
  244. {
  245. $destinationStr.=$destination_id.';';
  246. } */
  247. $goodAnswer = $correct == $i ? true : false;
  248. if ($goodAnswer) {
  249. $nbrGoodAnswers++;
  250. $weighting = abs($weighting);
  251. if ($weighting > 0) {
  252. $questionWeighting += $weighting;
  253. }
  254. }
  255. if (empty($try)) {
  256. $try = 0;
  257. }
  258. if (empty($lp)) {
  259. $lp = 0;
  260. }
  261. if (empty($destination)) {
  262. $destination = 0;
  263. }
  264. if ($url == '') {
  265. $url = 0;
  266. }
  267. //1@@1;2;@@2;4;4;@@http://www.chamilo.org
  268. $dest = $try.'@@'.$lp.'@@'.$destination.'@@'.$url;
  269. $objAnswer->createAnswer(
  270. $answer,
  271. $goodAnswer,
  272. $comment,
  273. $weighting,
  274. $i,
  275. null,
  276. null,
  277. $dest
  278. );
  279. }
  280. // saves the answers into the data base
  281. $objAnswer->save();
  282. // sets the total weighting of the question
  283. $this->updateWeighting($questionWeighting);
  284. $this->save($exercise);
  285. }
  286. /**
  287. * {@inheritdoc}
  288. */
  289. public function return_header(Exercise $exercise, $counter = null, $score = [])
  290. {
  291. if ($exercise->showExpectedChoice()) {
  292. $header = '<table class="'.$this->question_table_class.'">
  293. <tr>
  294. <th>'.get_lang('Choice').'</th>';
  295. if ($exercise->showExpectedChoiceColumn()) {
  296. $header .= '<th>'.get_lang('ExpectedChoice').'</th>';
  297. }
  298. $header .= '<th>'.get_lang('Answer').'</th>';
  299. $header .= '<th>'.get_lang('Status').'</th>';
  300. $header .= '<th>'.get_lang('Comment').'</th>';
  301. $header .= '</tr>';
  302. } else {
  303. $header = parent::return_header($exercise, $counter, $score);
  304. }
  305. return $header;
  306. }
  307. }