survey_question.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Class survey_question.
  6. */
  7. class survey_question
  8. {
  9. public $buttonList = [];
  10. /** @var FormValidator */
  11. private $form;
  12. /**
  13. * Generic part of any survey question: the question field.
  14. *
  15. * @param array $surveyData
  16. * @param array $formData
  17. *
  18. * @return FormValidator
  19. */
  20. public function createForm($surveyData, $formData)
  21. {
  22. $action = isset($_GET['action']) ? Security::remove_XSS($_GET['action']) : null;
  23. $questionId = isset($_GET['question_id']) ? (int) $_GET['question_id'] : null;
  24. $surveyId = isset($_GET['survey_id']) ? (int) $_GET['survey_id'] : null;
  25. $type = isset($_GET['type']) ? Security::remove_XSS($_GET['type']) : null;
  26. $toolName = Display::return_icon(
  27. SurveyManager::icon_question($type),
  28. get_lang(ucfirst($type)),
  29. ['align' => 'middle', 'height' => '22px']
  30. ).' ';
  31. if ($action == 'add') {
  32. $toolName .= get_lang('AddQuestion').': ';
  33. } elseif ($action == 'edit') {
  34. $toolName .= get_lang('EditQuestion').': ';
  35. }
  36. switch ($_GET['type']) {
  37. case 'yesno':
  38. $toolName .= get_lang('YesNo');
  39. break;
  40. case 'multiplechoice':
  41. $toolName .= get_lang('UniqueSelect');
  42. break;
  43. case 'multipleresponse':
  44. $toolName .= get_lang('MultipleResponse');
  45. break;
  46. default:
  47. $toolName .= get_lang(api_ucfirst($type));
  48. }
  49. $sharedQuestionId = isset($formData['shared_question_id']) ? $formData['shared_question_id'] : null;
  50. $url = api_get_self().'?action='.$action.'&type='.$type.'&survey_id='.$surveyId.'&question_id='.$questionId.'&'.api_get_cidreq();
  51. $form = new FormValidator('question_form', 'post', $url);
  52. $form->addHeader($toolName);
  53. $form->addHidden('survey_id', $surveyId);
  54. $form->addHidden('question_id', $questionId);
  55. $form->addHidden('shared_question_id', Security::remove_XSS($sharedQuestionId));
  56. $form->addHidden('type', $type);
  57. $config = [
  58. 'ToolbarSet' => 'SurveyQuestion',
  59. 'Width' => '100%',
  60. 'Height' => '120',
  61. ];
  62. $form->addHtmlEditor(
  63. 'question',
  64. get_lang('Question'),
  65. true,
  66. false,
  67. $config
  68. );
  69. if (api_get_configuration_value('allow_required_survey_questions') &&
  70. in_array($_GET['type'], ['yesno', 'multiplechoice'])) {
  71. $form->addCheckBox('is_required', get_lang('IsMandatory'), get_lang('Yes'));
  72. }
  73. // When survey type = 1??
  74. if ($surveyData['survey_type'] == 1) {
  75. $table_survey_question_group = Database::get_course_table(TABLE_SURVEY_QUESTION_GROUP);
  76. $sql = 'SELECT id,name FROM '.$table_survey_question_group.'
  77. WHERE survey_id = '.(int) $_GET['survey_id'].'
  78. ORDER BY name';
  79. $rs = Database::query($sql);
  80. $glist = null;
  81. while ($row = Database::fetch_array($rs, 'NUM')) {
  82. $glist .= '<option value="'.$row[0].'" >'.$row[1].'</option>';
  83. }
  84. $grouplist = $grouplist1 = $grouplist2 = $glist;
  85. if (!empty($formData['assigned'])) {
  86. $grouplist = str_replace(
  87. '<option value="'.$formData['assigned'].'"',
  88. '<option value="'.$formData['assigned'].'" selected',
  89. $glist
  90. );
  91. }
  92. if (!empty($formData['assigned1'])) {
  93. $grouplist1 = str_replace(
  94. '<option value="'.$formData['assigned1'].'"',
  95. '<option value="'.$formData['assigned1'].'" selected',
  96. $glist
  97. );
  98. }
  99. if (!empty($formData['assigned2'])) {
  100. $grouplist2 = str_replace(
  101. '<option value="'.$formData['assigned2'].'"',
  102. '<option value="'.$formData['assigned2'].'" selected',
  103. $glist
  104. );
  105. }
  106. $this->html .= ' <tr><td colspan="">
  107. <fieldset style="border:1px solid black"><legend>'.get_lang('Condition').'</legend>
  108. <b>'.get_lang('Primary').'</b><br />
  109. '.'<input type="radio" name="choose" value="1" '.(($formData['choose'] == 1) ? 'checked' : '').
  110. '><select name="assigned">'.$grouplist.'</select><br />';
  111. $this->html .= '
  112. <b>'.get_lang('Secondary').'</b><br />
  113. '.'<input type="radio" name="choose" value="2" '.(($formData['choose'] == 2) ? 'checked' : '').
  114. '><select name="assigned1">'.$grouplist1.'</select> '.
  115. '<select name="assigned2">'.$grouplist2.'</select>'
  116. .'</fieldset><br />';
  117. }
  118. $this->setForm($form);
  119. return $form;
  120. }
  121. /**
  122. * Adds submit button.
  123. */
  124. public function renderForm()
  125. {
  126. if (isset($_GET['question_id']) && !empty($_GET['question_id'])) {
  127. /**
  128. * Check if survey has answers first before update it, this is because if you update it, the question
  129. * options will delete and re-insert in database loosing the iid and question_id to verify the correct answers.
  130. */
  131. $surveyId = isset($_GET['survey_id']) ? (int) $_GET['survey_id'] : 0;
  132. $answersChecker = SurveyUtil::checkIfSurveyHasAnswers($surveyId);
  133. if (!$answersChecker) {
  134. $this->buttonList[] = $this->getForm()->addButtonUpdate(get_lang('ModifyQuestionSurvey'), 'save', true);
  135. } else {
  136. $this->getForm()->addHtml('
  137. <div class="form-group">
  138. <label class="col-sm-2 control-label"></label>
  139. <div class="col-sm-8">
  140. <div class="alert alert-info">'.get_lang('YouCantNotEditThisQuestionBecauseAlreadyExistAnswers').'</div>
  141. </div>
  142. <div class="col-sm-2"></div>
  143. </div>
  144. ');
  145. }
  146. } else {
  147. $this->buttonList[] = $this->getForm()->addButtonSave(get_lang('CreateQuestionSurvey'), 'save', true);
  148. }
  149. $this->getForm()->addGroup($this->buttonList, 'buttons');
  150. }
  151. /**
  152. * @return FormValidator
  153. */
  154. public function getForm()
  155. {
  156. return $this->form;
  157. }
  158. /**
  159. * @param FormValidator $form
  160. */
  161. public function setForm($form)
  162. {
  163. $this->form = $form;
  164. }
  165. /**
  166. * @param array $formData
  167. *
  168. * @return mixed
  169. */
  170. public function preSave($formData)
  171. {
  172. $counter = Session::read('answer_count');
  173. $answerList = Session::read('answer_list');
  174. if (empty($answerList)) {
  175. $answerList = isset($formData['answers']) ? $formData['answers'] : [];
  176. Session::write('answer_list', $answerList);
  177. }
  178. if (isset($_POST['answers'])) {
  179. $formData['answers'] = $_POST['answers'];
  180. }
  181. if (empty($counter)) {
  182. $counter = count($answerList) - 1;
  183. Session::write('answer_count', $counter);
  184. }
  185. // Moving an answer up
  186. if (isset($_POST['move_up']) && $_POST['move_up']) {
  187. foreach ($_POST['move_up'] as $key => &$value) {
  188. $id1 = $key;
  189. $content1 = $formData['answers'][$id1];
  190. $id2 = $key - 1;
  191. $content2 = $formData['answers'][$id2];
  192. $formData['answers'][$id1] = $content2;
  193. $formData['answers'][$id2] = $content1;
  194. }
  195. }
  196. // Moving an answer down
  197. if (isset($_POST['move_down']) && $_POST['move_down']) {
  198. foreach ($_POST['move_down'] as $key => &$value) {
  199. $id1 = $key;
  200. $content1 = $formData['answers'][$id1];
  201. $id2 = $key + 1;
  202. $content2 = $formData['answers'][$id2];
  203. $formData['answers'][$id1] = $content2;
  204. $formData['answers'][$id2] = $content1;
  205. }
  206. }
  207. /**
  208. * This solution is a little bit strange but I could not find a different solution.
  209. */
  210. if (isset($_POST['delete_answer'])) {
  211. $deleted = false;
  212. foreach ($_POST['delete_answer'] as $key => &$value) {
  213. $deleted = $key;
  214. $counter--;
  215. Session::write('answer_count', $counter);
  216. }
  217. foreach ($formData['answers'] as $key => &$value) {
  218. if ($key > $deleted) {
  219. $formData['answers'][$key - 1] = $formData['answers'][$key];
  220. unset($formData['answers'][$key]);
  221. }
  222. }
  223. }
  224. // Adding an answer
  225. if (isset($_POST['buttons']) && isset($_POST['buttons']['add_answer'])) {
  226. $counter++;
  227. Session::write('answer_count', $counter);
  228. }
  229. // Removing an answer
  230. if (isset($_POST['buttons']) && isset($_POST['buttons']['remove_answer'])) {
  231. $counter--;
  232. Session::write('answer_count', $counter);
  233. foreach ($formData['answers'] as $index => &$data) {
  234. if ($index > $counter) {
  235. unset($formData['answers'][$index]);
  236. }
  237. }
  238. }
  239. if (!isset($_POST['delete_answer'])) {
  240. if (isset($formData['answers'])) {
  241. foreach ($formData['answers'] as $index => $data) {
  242. if ($index > $counter) {
  243. unset($formData['answers'][$index]);
  244. }
  245. }
  246. for ($i = 0; $i <= $counter; $i++) {
  247. if (!isset($formData['answers'][$i])) {
  248. $formData['answers'][$i] = '';
  249. }
  250. }
  251. }
  252. }
  253. $formData['answers'] = isset($formData['answers']) ? $formData['answers'] : [];
  254. Session::write('answer_list', $formData['answers']);
  255. if (!isset($formData['is_required']) && api_get_configuration_value('survey_mark_question_as_required')) {
  256. $formData['is_required'] = true;
  257. }
  258. return $formData;
  259. }
  260. /**
  261. * @param array $surveyData
  262. * @param array $formData
  263. *
  264. * @return mixed
  265. */
  266. public function save($surveyData, $formData)
  267. {
  268. // Saving a question
  269. if (isset($_POST['buttons']) && isset($_POST['buttons']['save'])) {
  270. Session::erase('answer_count');
  271. Session::erase('answer_list');
  272. $message = SurveyManager::save_question(
  273. $surveyData,
  274. $formData
  275. );
  276. if ($message == 'QuestionAdded' || $message == 'QuestionUpdated') {
  277. header('Location: '.api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.intval($_GET['survey_id']).'&message='.$message.'&'.api_get_cidreq());
  278. exit;
  279. }
  280. }
  281. return $formData;
  282. }
  283. /**
  284. * Adds two buttons. One to add an option, one to remove an option.
  285. *
  286. * @param array $data
  287. */
  288. public function addRemoveButtons($data)
  289. {
  290. $this->buttonList['remove_answer'] = $this->getForm()->createElement(
  291. 'button',
  292. 'remove_answer',
  293. get_lang('RemoveAnswer'),
  294. 'minus',
  295. 'default'
  296. );
  297. if (count($data['answers']) <= 2) {
  298. $this->buttonList['remove_answer']->updateAttributes(
  299. ['disabled' => 'disabled']
  300. );
  301. }
  302. $this->buttonList['add_answer'] = $this->getForm()->createElement(
  303. 'button',
  304. 'add_answer',
  305. get_lang('AddAnswer'),
  306. 'plus',
  307. 'default'
  308. );
  309. }
  310. /**
  311. * @param FormValidator $form
  312. * @param array $questionData
  313. * @param array $answers
  314. */
  315. public function render(FormValidator $form, $questionData = [], $answers = [])
  316. {
  317. return null;
  318. }
  319. }