survey_question.php 12 KB

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