UniqueAnswerImage.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. * @throws Exception
  25. */
  26. public function createAnswersForm($form)
  27. {
  28. $objExercise = Session::read('objExercise');
  29. $editorConfig = [
  30. 'ToolbarSet' => 'TestProposedAnswer',
  31. 'Width' => '100%',
  32. 'Height' => '125'
  33. ];
  34. //this line defines how many questions by default appear when creating a choice question
  35. // The previous default value was 2. See task #1759.
  36. $numberAnswers = isset($_POST['nb_answers']) ? (int) $_POST['nb_answers'] : 4;
  37. $numberAnswers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
  38. $feedbackTitle = '';
  39. if ($objExercise->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  40. //Scenario
  41. $commentTitle = '<th>'.get_lang('Comment').'</th>';
  42. $feedbackTitle = '<th>'.get_lang('Scenario').'</th>';
  43. } else {
  44. $commentTitle = '<th >'.get_lang('Comment').'</th>';
  45. }
  46. $html = '<div class="alert alert-success" role="alert">'.get_lang('UniqueAnswerImagePreferredSize200x150').'</div>';
  47. $html .= '<table class="table table-striped table-hover">
  48. <thead>
  49. <tr style="text-align: center;">
  50. <th width="10">' . get_lang('Number').'</th>
  51. <th>'.get_lang('True').'</th>
  52. <th>'.get_lang('Answer').'</th>
  53. '.$commentTitle.'
  54. '.$feedbackTitle.'
  55. <th width="15">' . get_lang('Weighting').'</th>
  56. </tr>
  57. </thead>
  58. <tbody>';
  59. $form->addHeader(get_lang('Answers'));
  60. $form->addHtml($html);
  61. $defaults = array();
  62. $correct = 0;
  63. if (!empty($this->id)) {
  64. $answer = new Answer($this->id);
  65. $answer->read();
  66. if (count($answer->nbrAnswers) > 0 && !$form->isSubmitted()) {
  67. $numberAnswers = $answer->nbrAnswers;
  68. }
  69. }
  70. $form->addElement('hidden', 'nb_answers');
  71. //Feedback SELECT
  72. $questionList = $objExercise->selectQuestionList();
  73. $selectQuestion = array();
  74. $selectQuestion[0] = get_lang('SelectTargetQuestion');
  75. if (is_array($questionList)) {
  76. foreach ($questionList as $key => $questionid) {
  77. //To avoid warning messages
  78. if (!is_numeric($questionid)) {
  79. continue;
  80. }
  81. $question = Question::read($questionid);
  82. $selectQuestion[$questionid] = 'Q'.$key.' :'.cut(
  83. $question->selectTitle(),
  84. 20
  85. );
  86. }
  87. }
  88. $selectQuestion[-1] = get_lang('ExitTest');
  89. $list = new LearnpathList(api_get_user_id());
  90. $flatList = $list->get_flat_list();
  91. $selectLpId = array();
  92. $selectLpId[0] = get_lang('SelectTargetLP');
  93. foreach ($flatList as $id => $details) {
  94. $selectLpId[$id] = cut($details['lp_name'], 20);
  95. }
  96. $tempScenario = array();
  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] = array('0');
  136. $tempScenario['lp'.$i] = array('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, true, $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 = array();
  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. array(
  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, array('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. //setting the save button here and not in the question class.php
  215. $buttonGroup[] = $form->addButtonDelete(get_lang('LessAnswer'), 'lessAnswers', true);
  216. $buttonGroup[] = $form->addButtonCreate(get_lang('PlusAnswer'), 'moreAnswers', true);
  217. $buttonGroup[] = $form->addButtonSave($text, 'submitQuestion', true);
  218. $form->addGroup($buttonGroup);
  219. }
  220. // We check the first radio button to be sure a radio button will be check
  221. if ($correct == 0) {
  222. $correct = 1;
  223. }
  224. $defaults['correct'] = $correct;
  225. if (!empty($this->id)) {
  226. $form->setDefaults($defaults);
  227. } else {
  228. if ($this->isContent == 1) {
  229. // Default sample content.
  230. $form->setDefaults($defaults);
  231. } else {
  232. $form->setDefaults(array('correct' => 1));
  233. }
  234. }
  235. $form->setConstants(array('nb_answers' => $numberAnswers));
  236. }
  237. /**
  238. * @inheritdoc
  239. */
  240. public function processAnswersCreation($form, $exercise)
  241. {
  242. $questionWeighting = $nbrGoodAnswers = 0;
  243. $correct = $form->getSubmitValue('correct');
  244. $objAnswer = new Answer($this->id);
  245. $numberAnswers = $form->getSubmitValue('nb_answers');
  246. for ($i = 1; $i <= $numberAnswers; $i++) {
  247. $answer = trim(str_replace(['<p>', '</p>'], '', $form->getSubmitValue('answer['.$i.']')));
  248. $comment = trim(str_replace(['<p>', '</p>'], '', $form->getSubmitValue('comment['.$i.']')));
  249. $weighting = trim($form->getSubmitValue('weighting['.$i.']'));
  250. $scenario = $form->getSubmitValue('scenario');
  251. //$listDestination = $form -> getSubmitValue('destination'.$i);
  252. //$destinationStr = $form -> getSubmitValue('destination'.$i);
  253. $try = $scenario['try'.$i];
  254. $lp = $scenario['lp'.$i];
  255. $destination = $scenario['destination'.$i];
  256. $url = trim($scenario['url'.$i]);
  257. /*
  258. How we are going to parse the destination value
  259. here we parse the destination value which is a string
  260. 1@@3@@2;4;4;@@http://www.chamilo.org
  261. where: try_again@@lp_id@@selected_questions@@url
  262. try_again = is 1 || 0
  263. lp_id = id of a learning path (0 if dont select)
  264. selected_questions= ids of questions
  265. url= an url
  266. $destinationStr='';
  267. foreach ($listDestination as $destination_id)
  268. {
  269. $destinationStr.=$destination_id.';';
  270. } */
  271. $goodAnswer = $correct == $i ? true : false;
  272. if ($goodAnswer) {
  273. $nbrGoodAnswers++;
  274. $weighting = abs($weighting);
  275. if ($weighting > 0) {
  276. $questionWeighting += $weighting;
  277. }
  278. }
  279. if (empty($try)) {
  280. $try = 0;
  281. }
  282. if (empty($lp)) {
  283. $lp = 0;
  284. }
  285. if (empty($destination)) {
  286. $destination = 0;
  287. }
  288. if ($url == '') {
  289. $url = 0;
  290. }
  291. //1@@1;2;@@2;4;4;@@http://www.chamilo.org
  292. $dest = $try.'@@'.$lp.'@@'.$destination.'@@'.$url;
  293. $objAnswer->createAnswer(
  294. $answer,
  295. $goodAnswer,
  296. $comment,
  297. $weighting,
  298. $i,
  299. null,
  300. null,
  301. $dest
  302. );
  303. }
  304. // saves the answers into the data base
  305. $objAnswer->save();
  306. // sets the total weighting of the question
  307. $this->updateWeighting($questionWeighting);
  308. $this->save($exercise);
  309. }
  310. }