survey_question.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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('ToolbarSet' => 'SurveyQuestion', 'Width' => '100%', 'Height' => '120');
  56. $form->addHtmlEditor('question', get_lang('Question'), true, false, $config);
  57. // When survey type = 1??
  58. if ($surveyData['survey_type'] == 1) {
  59. $table_survey_question_group = Database::get_course_table(TABLE_SURVEY_QUESTION_GROUP);
  60. $sql = 'SELECT id,name FROM '.$table_survey_question_group.'
  61. WHERE survey_id = '.(int)$_GET['survey_id'].'
  62. ORDER BY name';
  63. $rs = Database::query($sql);
  64. $glist = null;
  65. while ($row = Database::fetch_array($rs, 'NUM')) {
  66. $glist .= '<option value="'.$row[0].'" >'.$row[1].'</option>';
  67. }
  68. $grouplist = $grouplist1 = $grouplist2 = $glist;
  69. if (!empty($formData['assigned'])) {
  70. $grouplist = str_replace('<option value="'.$formData['assigned'].'"','<option value="'.$formData['assigned'].'" selected',$glist);
  71. }
  72. if (!empty($formData['assigned1'])) {
  73. $grouplist1 = str_replace('<option value="'.$formData['assigned1'].'"','<option value="'.$formData['assigned1'].'" selected',$glist);
  74. }
  75. if (!empty($formData['assigned2'])) {
  76. $grouplist2 = str_replace('<option value="'.$formData['assigned2'].'"','<option value="'.$formData['assigned2'].'" selected',$glist);
  77. }
  78. $this->html .= ' <tr><td colspan="">
  79. <fieldset style="border:1px solid black"><legend>'.get_lang('Condition').'</legend>
  80. <b>'.get_lang('Primary').'</b><br />
  81. '.'<input type="radio" name="choose" value="1" '.(($formData['choose'] == 1) ? 'checked' : '').
  82. '><select name="assigned">'.$grouplist.'</select><br />';
  83. $this->html .= '
  84. <b>'.get_lang('Secondary').'</b><br />
  85. '.'<input type="radio" name="choose" value="2" '.(($formData['choose']==2)?'checked':'').
  86. '><select name="assigned1">'.$grouplist1.'</select> '.
  87. '<select name="assigned2">'.$grouplist2.'</select>'
  88. .'</fieldset><br />';
  89. //$form->addRadio('choose', get_lang('Primary'));
  90. //$form->addRadio('choose', get_lang('Secondary'));
  91. }
  92. $this->setForm($form);
  93. return $form;
  94. }
  95. /**
  96. * Adds submit button
  97. *
  98. */
  99. public function renderForm()
  100. {
  101. if (isset($_GET['question_id']) and !empty($_GET['question_id'])) {
  102. $this->buttonList[] = $this->getForm()->addButtonUpdate(get_lang('ModifyQuestionSurvey'), 'save', true);
  103. } else {
  104. $this->buttonList[] = $this->getForm()->addButtonSave(get_lang('CreateQuestionSurvey'), 'save', true);
  105. }
  106. $this->getForm()->addGroup($this->buttonList, 'buttons');
  107. }
  108. /**
  109. * @return FormValidator
  110. */
  111. public function getForm()
  112. {
  113. return $this->form;
  114. }
  115. /**
  116. * @param FormValidator $form
  117. */
  118. public function setForm($form)
  119. {
  120. $this->form = $form;
  121. }
  122. /**
  123. * @param array $formData
  124. *
  125. * @return mixed
  126. */
  127. public function preSave($formData)
  128. {
  129. $counter = Session::read('answer_count');
  130. $answerList = Session::read('answer_list');
  131. if (empty($answerList)) {
  132. $answerList = isset($formData['answers']) ? $formData['answers'] : array();
  133. Session::write('answer_list', $answerList);
  134. }
  135. if (isset($_POST['answers'])) {
  136. $formData['answers'] = $_POST['answers'];
  137. }
  138. if (empty($counter)) {
  139. $counter = count($answerList) - 1;
  140. Session::write('answer_count', $counter);
  141. }
  142. // Moving an answer up
  143. if (isset($_POST['move_up']) && $_POST['move_up']) {
  144. foreach ($_POST['move_up'] as $key => & $value) {
  145. $id1 = $key;
  146. $content1 = $formData['answers'][$id1];
  147. $id2 = $key - 1;
  148. $content2 = $formData['answers'][$id2];
  149. $formData['answers'][$id1] = $content2;
  150. $formData['answers'][$id2] = $content1;
  151. }
  152. }
  153. // Moving an answer down
  154. if (isset($_POST['move_down']) && $_POST['move_down']) {
  155. foreach ($_POST['move_down'] as $key => & $value) {
  156. $id1 = $key;
  157. $content1 = $formData['answers'][$id1];
  158. $id2 = $key + 1;
  159. $content2 = $formData['answers'][$id2];
  160. $formData['answers'][$id1] = $content2;
  161. $formData['answers'][$id2] = $content1;
  162. }
  163. }
  164. /**
  165. * This solution is a little bit strange but I could not find a different solution.
  166. */
  167. if (isset($_POST['delete_answer'])) {
  168. $deleted = false;
  169. foreach ($_POST['delete_answer'] as $key => & $value) {
  170. $deleted = $key;
  171. $counter--;
  172. Session::write('answer_count', $counter);
  173. }
  174. foreach ($formData['answers'] as $key => & $value) {
  175. if ($key > $deleted) {
  176. $formData['answers'][$key - 1] = $formData['answers'][$key];
  177. unset($formData['answers'][$key]);
  178. }
  179. }
  180. }
  181. // Adding an answer
  182. if (isset($_POST['buttons']) && isset($_POST['buttons']['add_answer'])) {
  183. $counter++;
  184. Session::write('answer_count', $counter);
  185. }
  186. // Removing an answer
  187. if (isset($_POST['buttons']) && isset($_POST['buttons']['remove_answer'])) {
  188. $counter--;
  189. Session::write('answer_count', $counter);
  190. foreach ($formData['answers'] as $index => &$data) {
  191. if ($index > $counter) {
  192. unset($formData['answers'][$index]);
  193. }
  194. }
  195. }
  196. if (!isset($_POST['delete_answer'])) {
  197. if (isset($formData['answers'])) {
  198. foreach ($formData['answers'] as $index => $data) {
  199. if ($index > $counter) {
  200. unset($formData['answers'][$index]);
  201. }
  202. }
  203. for ($i = 0; $i <= $counter; $i++) {
  204. if (!isset($formData['answers'][$i])) {
  205. $formData['answers'][$i] = '';
  206. }
  207. }
  208. }
  209. }
  210. $formData['answers'] = isset($formData['answers']) ? $formData['answers'] : [];
  211. Session::write('answer_list', $formData['answers']);
  212. return $formData;
  213. }
  214. /**
  215. * @param array $surveyData
  216. * @param array $formData
  217. *
  218. * @return mixed
  219. */
  220. public function save($surveyData, $formData)
  221. {
  222. // Saving a question
  223. if (isset($_POST['buttons']) && isset($_POST['buttons']['save'])) {
  224. Session::erase('answer_count');
  225. Session::erase('answer_list');
  226. $message = SurveyManager::save_question(
  227. $surveyData,
  228. $formData
  229. );
  230. if ($message == 'QuestionAdded' || $message == 'QuestionUpdated') {
  231. header('Location: '.api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.intval($_GET['survey_id']).'&message='.$message.'&'.api_get_cidreq());
  232. exit;
  233. }
  234. }
  235. return $formData;
  236. }
  237. /**
  238. * Adds two buttons. One to add an option, one to remove an option
  239. *
  240. * @param array $data
  241. *
  242. */
  243. public function addRemoveButtons($data)
  244. {
  245. $this->buttonList['remove_answer'] = $this->getForm()->createElement(
  246. 'button',
  247. 'remove_answer',
  248. get_lang('RemoveAnswer'),
  249. 'minus'
  250. );
  251. if (count($data['answers']) <= 2) {
  252. $this->buttonList['remove_answer']->updateAttributes(
  253. array('disabled' => 'disabled')
  254. );
  255. }
  256. $this->buttonList['add_answer'] = $this->getForm()->createElement(
  257. 'button',
  258. 'add_answer',
  259. get_lang('AddAnswer'),
  260. 'plus'
  261. );
  262. }
  263. /**
  264. * @param FormValidator $form
  265. * @param array $questionData
  266. * @param array $answers
  267. */
  268. public function render(FormValidator $form, $questionData = array(), $answers = array())
  269. {
  270. return null;
  271. }
  272. }