UniqueAnswerImage.php 13 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 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. * @param FormValidator $form
  24. * @throws Exception
  25. */
  26. public function createAnswersForm($form)
  27. {
  28. $objExercise = Session::read('objExercise');
  29. $editorConfig = array(
  30. 'ToolbarSet' => 'TestFreeAnswer',
  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(), 20
  84. );
  85. }
  86. }
  87. $selectQuestion[-1] = get_lang('ExitTest');
  88. $list = new LearnpathList(api_get_user_id());
  89. $flatList = $list->get_flat_list();
  90. $selectLpId = array();
  91. $selectLpId[0] = get_lang('SelectTargetLP');
  92. foreach ($flatList as $id => $details) {
  93. $selectLpId[$id] = cut($details['lp_name'], 20);
  94. }
  95. $tempScenario = array();
  96. if ($numberAnswers < 1) {
  97. $numberAnswers = 1;
  98. Display::display_normal_message(
  99. get_lang('YouHaveToCreateAtLeastOneAnswer')
  100. );
  101. }
  102. for ($i = 1; $i <= $numberAnswers; ++$i) {
  103. $form->addHtml('<tr>');
  104. if (isset($answer) && is_object($answer)) {
  105. if ($answer->correct[$i]) {
  106. $correct = $i;
  107. }
  108. $defaults['answer[' . $i . ']'] = $answer->answer[$i];
  109. $defaults['comment[' . $i . ']'] = $answer->comment[$i];
  110. $defaults['weighting[' . $i . ']'] = float_format(
  111. $answer->weighting[$i], 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. $try = 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('comment[' . $i . ']', null, null, false, $editorConfig);
  167. // Direct feedback
  168. //Adding extra feedback fields
  169. $group = array();
  170. $group['try' . $i] = $form->createElement('checkbox', 'try' . $i, null, get_lang('TryAgain'));
  171. $group['lp' . $i] = $form->createElement(
  172. 'select',
  173. 'lp' . $i,
  174. get_lang('SeeTheory') . ': ',
  175. $selectLpId
  176. );
  177. $group['destination' . $i] = $form->createElement(
  178. 'select',
  179. 'destination' . $i,
  180. get_lang('GoToQuestion') . ': ',
  181. $selectQuestion
  182. );
  183. $group['url' . $i] = $form->createElement(
  184. 'text', 'url' . $i,
  185. get_lang('Other') . ': ',
  186. array(
  187. 'class' => 'col-md-2',
  188. 'placeholder' => get_lang('Other')
  189. )
  190. );
  191. $form->addGroup($group, 'scenario');
  192. $renderer->setElementTemplate(
  193. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}',
  194. 'scenario'
  195. );
  196. } else {
  197. $form->addHtmlEditor('comment[' . $i . ']', null, null, false, $editorConfig);
  198. }
  199. $form->addText('weighting[' . $i . ']', null, null, array('class' => "col-md-1", 'value' => '0'));
  200. $form->addHtml('</tr>');
  201. }
  202. $form->addHtml('</tbody>');
  203. $form->addHtml('</table>');
  204. global $text, $class;
  205. $buttonGroup = [];
  206. if ($objExercise->edit_exercise_in_lp == true) {
  207. //setting the save button here and not in the question class.php
  208. $buttonGroup[] = $form->addButtonDelete(get_lang('LessAnswer'), 'lessAnswers', true);
  209. $buttonGroup[] = $form->addButtonCreate(get_lang('PlusAnswer'), 'moreAnswers', true);
  210. $buttonGroup[] = $form->addButtonSave($text, 'submitQuestion', true);
  211. $form->addGroup($buttonGroup);
  212. }
  213. // We check the first radio button to be sure a radio button will be check
  214. if ($correct == 0) {
  215. $correct = 1;
  216. }
  217. $defaults['correct'] = $correct;
  218. if (!empty($this->id)) {
  219. $form->setDefaults($defaults);
  220. } else {
  221. if ($this->isContent == 1) {
  222. // Default sample content.
  223. $form->setDefaults($defaults);
  224. } else {
  225. $form->setDefaults(array('correct' => 1));
  226. }
  227. }
  228. $form->setConstants(array('nb_answers' => $numberAnswers));
  229. }
  230. public function processAnswersCreation($form)
  231. {
  232. $questionWeighting = $nbrGoodAnswers = 0;
  233. $correct = $form->getSubmitValue('correct');
  234. $objAnswer = new Answer($this->id);
  235. $numberAnswers = $form->getSubmitValue('nb_answers');
  236. for ($i = 1; $i <= $numberAnswers; $i++) {
  237. $answer = trim(str_replace(['<p>', '</p>'], '', $form->getSubmitValue('answer[' . $i . ']')));
  238. $comment = trim(str_replace(['<p>', '</p>'], '', $form->getSubmitValue('comment[' . $i . ']')));
  239. $weighting = trim($form->getSubmitValue('weighting[' . $i . ']'));
  240. $scenario = $form->getSubmitValue('scenario');
  241. //$listDestination = $form -> getSubmitValue('destination'.$i);
  242. //$destinationStr = $form -> getSubmitValue('destination'.$i);
  243. $try = $scenario['try' . $i];
  244. $lp = $scenario['lp' . $i];
  245. $destination = $scenario['destination' . $i];
  246. $url = trim($scenario['url' . $i]);
  247. /*
  248. How we are going to parse the destination value
  249. here we parse the destination value which is a string
  250. 1@@3@@2;4;4;@@http://www.chamilo.org
  251. where: try_again@@lp_id@@selected_questions@@url
  252. try_again = is 1 || 0
  253. lp_id = id of a learning path (0 if dont select)
  254. selected_questions= ids of questions
  255. url= an url
  256. $destinationStr='';
  257. foreach ($listDestination as $destination_id)
  258. {
  259. $destinationStr.=$destination_id.';';
  260. } */
  261. $goodAnswer = ($correct == $i) ? true : false;
  262. if ($goodAnswer) {
  263. $nbrGoodAnswers++;
  264. $weighting = abs($weighting);
  265. if ($weighting > 0) {
  266. $questionWeighting += $weighting;
  267. }
  268. }
  269. if (empty($try)) {
  270. $try = 0;
  271. }
  272. if (empty($lp)) {
  273. $lp = 0;
  274. }
  275. if (empty($destination)) {
  276. $destination = 0;
  277. }
  278. if ($url == '') {
  279. $url = 0;
  280. }
  281. //1@@1;2;@@2;4;4;@@http://www.chamilo.org
  282. $dest = $try . '@@' . $lp . '@@' . $destination . '@@' . $url;
  283. $objAnswer->createAnswer($answer, $goodAnswer, $comment, $weighting, $i, null, null, $dest);
  284. }
  285. // saves the answers into the data base
  286. $objAnswer->save();
  287. // sets the total weighting of the question
  288. $this->updateWeighting($questionWeighting);
  289. $this->save();
  290. }
  291. public function return_header($feedback_type = null, $counter = null, $score = null)
  292. {
  293. return parent::return_header($feedback_type, $counter, $score);
  294. }
  295. }