survey.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * @package chamilo.survey
  6. *
  7. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University:
  8. * cleanup, refactoring and rewriting large parts of the code
  9. * @author Julio Montoya
  10. */
  11. require_once __DIR__.'/../inc/global.inc.php';
  12. $this_section = SECTION_COURSES;
  13. $current_course_tool = TOOL_SURVEY;
  14. api_protect_course_script(true);
  15. /** @todo this has to be moved to a more appropriate place (after the display_header of the code)*/
  16. // Coach can't view this page
  17. $extend_rights_for_coachs = api_get_setting('extend_rights_for_coach_on_survey');
  18. $isDrhOfCourse = CourseManager::isUserSubscribedInCourseAsDrh(
  19. api_get_user_id(),
  20. api_get_course_info()
  21. );
  22. if ($isDrhOfCourse) {
  23. header('Location: '.api_get_path(WEB_CODE_PATH).'survey/survey_list.php?'.api_get_cidreq());
  24. exit;
  25. }
  26. if (!api_is_allowed_to_edit(false, true) ||
  27. (api_is_session_general_coach() && $extend_rights_for_coachs == 'false')
  28. ) {
  29. api_not_allowed(true);
  30. exit;
  31. }
  32. // Database table definitions
  33. $table_survey = Database::get_course_table(TABLE_SURVEY);
  34. $table_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION);
  35. $table_survey_question_option = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  36. $table_survey_question_group = Database::get_course_table(TABLE_SURVEY_QUESTION_GROUP);
  37. $table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  38. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  39. $survey_id = (int) $_GET['survey_id'];
  40. $course_id = api_get_course_int_id();
  41. $action = isset($_GET['action']) ? $_GET['action'] : null;
  42. // Breadcrumbs
  43. $interbreadcrumb[] = [
  44. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php',
  45. 'name' => get_lang('SurveyList'),
  46. ];
  47. // Getting the survey information
  48. if (!empty($_GET['survey_id'])) {
  49. $course_code = api_get_course_id();
  50. if ($course_code != -1) {
  51. $survey_data = SurveyManager::get_survey($survey_id);
  52. } else {
  53. api_not_allowed(true);
  54. }
  55. } else {
  56. api_not_allowed(true);
  57. }
  58. $tool_name = strip_tags($survey_data['title']);
  59. $is_survey_type_1 = $survey_data['survey_type'] == 1;
  60. if (api_strlen(strip_tags($survey_data['title'])) > 40) {
  61. $tool_name .= '...';
  62. }
  63. if ($is_survey_type_1 && ($action == 'addgroup' || $action == 'deletegroup')) {
  64. $_POST['name'] = trim($_POST['name']);
  65. if ($action == 'addgroup') {
  66. if (!empty($_POST['group_id'])) {
  67. Database::query('UPDATE '.$table_survey_question_group.' SET description = \''.Database::escape_string($_POST['description']).'\'
  68. WHERE c_id = '.$course_id.' AND id = \''.Database::escape_string($_POST['group_id']).'\'');
  69. $sendmsg = 'GroupUpdatedSuccessfully';
  70. } elseif (!empty($_POST['name'])) {
  71. Database::query('INSERT INTO '.$table_survey_question_group.' (c_id, name,description,survey_id) values ('.$course_id.', \''.Database::escape_string($_POST['name']).'\',\''.Database::escape_string($_POST['description']).'\',\''.Database::escape_string($survey_id).'\') ');
  72. $sendmsg = 'GroupCreatedSuccessfully';
  73. } else {
  74. $sendmsg = 'GroupNeedName';
  75. }
  76. }
  77. if ($action == 'deletegroup') {
  78. $sql = 'DELETE FROM '.$table_survey_question_group.'
  79. WHERE c_id = '.$course_id.' AND id = '.intval($_GET['gid']).' AND survey_id = '.intval($survey_id);
  80. Database::query($sql);
  81. $sendmsg = 'GroupDeletedSuccessfully';
  82. }
  83. header('Location: '.api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$survey_id.'&sendmsg='.$sendmsg);
  84. exit;
  85. }
  86. // Displaying the header
  87. Display::display_header($tool_name, 'Survey');
  88. // Action handling
  89. $my_action_survey = Security::remove_XSS($action);
  90. $my_question_id_survey = isset($_GET['question_id']) ? Security::remove_XSS($_GET['question_id']) : null;
  91. $my_survey_id_survey = Security::remove_XSS($_GET['survey_id']);
  92. $message_information = isset($_GET['message']) ? Security::remove_XSS($_GET['message']) : null;
  93. if (isset($action)) {
  94. if (($action == 'moveup' || $action == 'movedown') && isset($_GET['question_id'])) {
  95. SurveyManager::move_survey_question(
  96. $my_action_survey,
  97. $my_question_id_survey,
  98. $my_survey_id_survey
  99. );
  100. echo Display::return_message(get_lang('SurveyQuestionMoved'), 'confirmation');
  101. }
  102. if ($action == 'delete' && is_numeric($_GET['question_id'])) {
  103. SurveyManager::delete_survey_question(
  104. $my_survey_id_survey,
  105. $my_question_id_survey,
  106. $survey_data['is_shared']
  107. );
  108. }
  109. }
  110. if (!empty($survey_data['survey_version'])) {
  111. echo '<b>'.get_lang('Version').': '.$survey_data['survey_version'].'</b>';
  112. }
  113. // We exit here is the first or last question is a pagebreak (which causes errors)
  114. SurveyUtil::check_first_last_question($_GET['survey_id']);
  115. // Action links
  116. $survey_actions = '';
  117. if ($survey_data['survey_type'] != 3) {
  118. $survey_actions = '<a href="'.api_get_path(WEB_CODE_PATH).'survey/create_new_survey.php?'.api_get_cidreq(
  119. ).'&action=edit&survey_id='.$survey_id.'">'.
  120. Display::return_icon('edit.png', get_lang('EditSurvey'), '', ICON_SIZE_MEDIUM).'</a>';
  121. }
  122. $survey_actions .= '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey_list.php?'.api_get_cidreq().'&action=delete&survey_id='.$survey_id.'" onclick="javascript:if(!confirm(\''.addslashes(api_htmlentities(get_lang('DeleteSurvey').'?', ENT_QUOTES)).'\')) return false;">'.
  123. Display::return_icon('delete.png', get_lang('DeleteSurvey'), '', ICON_SIZE_MEDIUM).'</a>';
  124. if ($survey_data['survey_type'] != 3) {
  125. $survey_actions .= '<a href="'.api_get_path(WEB_CODE_PATH).'survey/preview.php?'.api_get_cidreq().'&survey_id='.$survey_id.'">'.
  126. Display::return_icon('preview_view.png', get_lang('Preview'), '', ICON_SIZE_MEDIUM).'</a>';
  127. }
  128. $survey_actions .= '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey_invite.php?'.api_get_cidreq().'&survey_id='.$survey_id.'">'.
  129. Display::return_icon('mail_send.png', get_lang('Publish'), '', ICON_SIZE_MEDIUM).'</a>';
  130. if ($survey_data['survey_type'] != 3) {
  131. if (!api_get_configuration_value('hide_survey_reporting_button')) {
  132. $survey_actions .= Display::url(
  133. Display::return_icon('statistics.png', get_lang('Reporting'), [], ICON_SIZE_MEDIUM),
  134. api_get_path(WEB_CODE_PATH).'survey/reporting.php?'.api_get_cidreq().'&survey_id='.$survey_id
  135. );
  136. }
  137. }
  138. $survey_actions .= SurveyUtil::getAdditionalTeacherActions($survey_id, ICON_SIZE_MEDIUM);
  139. echo '<div class="actions">'.$survey_actions.'</div>';
  140. $urlQuestion = api_get_path(WEB_CODE_PATH).'survey/question.php?'.api_get_cidreq().'&action=add';
  141. if ($survey_data['survey_type'] == 0) {
  142. echo '<div class="well">';
  143. echo Display::url(
  144. Display::return_icon('yesno.png', get_lang('YesNo'), null, ICON_SIZE_BIG),
  145. $urlQuestion.'&type=yesno&survey_id='.$survey_id
  146. );
  147. echo Display::url(
  148. Display::return_icon('mcua.png', get_lang('UniqueSelect'), null, ICON_SIZE_BIG),
  149. $urlQuestion.'&type=multiplechoice&survey_id='.$survey_id
  150. );
  151. echo Display::url(
  152. Display::return_icon('mcma.png', get_lang('MultipleResponse'), null, ICON_SIZE_BIG),
  153. $urlQuestion.'&type=multipleresponse&survey_id='.$survey_id
  154. );
  155. echo Display::url(
  156. Display::return_icon('open_answer.png', get_lang('Open'), null, ICON_SIZE_BIG),
  157. $urlQuestion.'&type=open&survey_id='.$survey_id
  158. );
  159. echo Display::url(
  160. Display::return_icon('dropdown.png', get_lang('Dropdown'), null, ICON_SIZE_BIG),
  161. $urlQuestion.'&type=dropdown&survey_id='.$survey_id
  162. );
  163. echo Display::url(
  164. Display::return_icon('percentagequestion.png', get_lang('Percentage'), null, ICON_SIZE_BIG),
  165. $urlQuestion.'&type=percentage&survey_id='.$survey_id
  166. );
  167. echo Display::url(
  168. Display::return_icon('scorequestion.png', get_lang('Score'), null, ICON_SIZE_BIG),
  169. $urlQuestion.'&type=score&survey_id='.$survey_id
  170. );
  171. echo Display::url(
  172. Display::return_icon('commentquestion.png', get_lang('Comment'), null, ICON_SIZE_BIG),
  173. $urlQuestion.'&type=comment&survey_id='.$survey_id
  174. );
  175. if ($survey_data['one_question_per_page'] == 0) {
  176. echo Display::url(
  177. Display::return_icon('page_end.png', get_lang('Pagebreak'), null, ICON_SIZE_BIG),
  178. $urlQuestion.'&type=pagebreak&survey_id='.$survey_id
  179. );
  180. }
  181. echo '</div>';
  182. } else {
  183. if ($survey_data['survey_type'] != 3) {
  184. echo '<div class="well">';
  185. echo Display::url(
  186. Display::return_icon('yesno.png', get_lang('YesNo'), null, ICON_SIZE_BIG),
  187. $urlQuestion.'&type=personality&survey_id='.$survey_id
  188. );
  189. echo '</a></div>';
  190. }
  191. }
  192. // Displaying the table header with all the questions
  193. echo '<table class="table table-bordered">';
  194. echo '<thead>';
  195. echo '<tr>';
  196. echo ' <th width="5%">'.get_lang('NumberAbbreviation').'</th>';
  197. echo ' <th width="50%">'.get_lang('Title').'</th>';
  198. echo ' <th width="15%">'.get_lang('Type').'</th>';
  199. echo ' <th width="15%" >'.get_lang('NumberOfOptions').'</th>';
  200. echo ' <th width="15%">'.get_lang('Modify').'</th>';
  201. if ($is_survey_type_1) {
  202. echo '<th width="100">'.get_lang('Condition').'</th>';
  203. echo '<th width="40">'.get_lang('Group').'</th>';
  204. }
  205. echo ' </tr>';
  206. echo '</thead>';
  207. // Displaying the table contents with all the questions
  208. $question_counter = 1;
  209. $sql = "SELECT * FROM $table_survey_question_group
  210. WHERE c_id = $course_id AND survey_id = $survey_id
  211. ORDER BY id";
  212. $result = Database::query($sql);
  213. $groups = [];
  214. while ($row = Database::fetch_array($result)) {
  215. $groups[$row['id']] = $row['name'];
  216. }
  217. $sql = "SELECT survey_question.*, count(survey_question_option.question_option_id) as number_of_options
  218. FROM $table_survey_question survey_question
  219. LEFT JOIN $table_survey_question_option survey_question_option
  220. ON
  221. survey_question.question_id = survey_question_option.question_id AND
  222. survey_question_option.c_id = $course_id
  223. WHERE
  224. survey_question.survey_id = ".intval($survey_id)." AND
  225. survey_question.c_id = $course_id
  226. GROUP BY survey_question.question_id
  227. ORDER BY survey_question.sort ASC";
  228. $result = Database::query($sql);
  229. $question_counter_max = Database::num_rows($result);
  230. while ($row = Database::fetch_array($result, 'ASSOC')) {
  231. echo '<tr>';
  232. echo ' <td>'.$question_counter.'</td>';
  233. echo ' <td>';
  234. if ($survey_data['survey_type'] != 3) {
  235. if (api_strlen($row['survey_question']) > 100) {
  236. echo api_substr(strip_tags($row['survey_question']), 0, 100).' ... ';
  237. } else {
  238. echo $row['survey_question'];
  239. }
  240. } else {
  241. $parts = explode('@@', $row['survey_question']);
  242. echo api_get_local_time($parts[0]).' - '.api_get_local_time($parts[1]);
  243. }
  244. if ($row['type'] == 'yesno') {
  245. $tool_name = get_lang('YesNo');
  246. } elseif ($row['type'] == 'multiplechoice') {
  247. $tool_name = get_lang('UniqueSelect');
  248. } else {
  249. $tool_name = get_lang(api_ucfirst(Security::remove_XSS($row['type'])));
  250. }
  251. echo '</td>';
  252. echo '<td>'.$tool_name.'</td>';
  253. echo '<td>'.$row['number_of_options'].'</td>';
  254. echo '<td>';
  255. if ($survey_data['survey_type'] != 3) {
  256. echo '<a href="'.api_get_path(WEB_CODE_PATH).'survey/question.php?'.api_get_cidreq().'&action=edit&type='.$row['type'].'&survey_id='.$survey_id.'&question_id='.$row['question_id'].'">'.
  257. Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL).'</a>';
  258. }
  259. echo '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey.php?'.api_get_cidreq().'&action=delete&survey_id='.$survey_id.'&question_id='.$row['question_id'].'" onclick="javascript:if(!confirm(\''.addslashes(api_htmlentities(get_lang("DeleteSurveyQuestion").'?', ENT_QUOTES, $charset)).'\')) return false;">'.
  260. Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).'</a>';
  261. if ($survey_data['survey_type'] != 3) {
  262. if ($question_counter > 1) {
  263. echo '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey.php?'.api_get_cidreq(
  264. ).'&action=moveup&survey_id='.$survey_id.'&question_id='.$row['question_id'].'">'.
  265. Display::return_icon('up.png', get_lang('MoveUp'), '', ICON_SIZE_SMALL).'</a>';
  266. } else {
  267. Display::display_icon('up_na.png', '&nbsp;', '', ICON_SIZE_SMALL);
  268. }
  269. if ($question_counter < $question_counter_max) {
  270. echo '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey.php?'.api_get_cidreq(
  271. ).'&action=movedown&survey_id='.$survey_id.'&question_id='.$row['question_id'].'">'.
  272. Display::return_icon('down.png', get_lang('MoveDown'), '', ICON_SIZE_SMALL).'</a>';
  273. } else {
  274. Display::display_icon('down_na.png', '&nbsp;', '', ICON_SIZE_SMALL);
  275. }
  276. }
  277. echo ' </td>';
  278. $question_counter++;
  279. if ($is_survey_type_1) {
  280. echo '<td>'.(($row['survey_group_pri'] == 0) ? get_lang('Secondary') : get_lang('Primary')).'</td>';
  281. echo '<td>'.(($row['survey_group_pri'] == 0) ? $groups[$row['survey_group_sec1']].'-'.$groups[$row['survey_group_sec2']] : $groups[$row['survey_group_pri']]).'</td>';
  282. }
  283. echo '</tr>';
  284. }
  285. echo '</table>';
  286. if ($is_survey_type_1) {
  287. echo '<br /><br /><b>'.get_lang('ManageGroups').'</b><br /><br />';
  288. if (in_array(
  289. $_GET['sendmsg'],
  290. ['GroupUpdatedSuccessfully', 'GroupDeletedSuccessfully', 'GroupCreatedSuccessfully']
  291. )
  292. ) {
  293. echo Display::return_message(
  294. get_lang($_GET['sendmsg']),
  295. 'confirmation',
  296. false
  297. );
  298. }
  299. if (in_array($_GET['sendmsg'], ['GroupNeedName'])) {
  300. echo Display::return_message(
  301. get_lang($_GET['sendmsg']),
  302. 'warning',
  303. false
  304. );
  305. }
  306. echo '<table border="0"><tr><td width="100">'.get_lang('Name').'</td><td>'.get_lang('Description').'</td></tr></table>';
  307. echo '<form action="'.api_get_path(WEB_CODE_PATH).'survey/survey.php?action=addgroup&survey_id='.$survey_id.'" method="post">';
  308. if ($_GET['action'] == 'editgroup') {
  309. $sql = 'SELECT name,description FROM '.$table_survey_question_group.'
  310. WHERE id = '.intval($_GET['gid']).' AND survey_id = '.intval($survey_id).' limit 1';
  311. $rs = Database::query($sql);
  312. $editedrow = Database::fetch_array($rs, 'ASSOC');
  313. echo '<input type="text" maxlength="20" name="name" value="'.$editedrow['name'].'" size="10" disabled>';
  314. echo '<input type="text" maxlength="150" name="description" value="'.$editedrow['description'].'" size="40">';
  315. echo '<input type="hidden" name="group_id" value="'.Security::remove_XSS($_GET['gid']).'">';
  316. echo '<input type="submit" value="'.get_lang('Save').'"'.'<input type="button" value="'.get_lang('Cancel').'" onclick="window.location.href = \'survey.php?survey_id='.Security::remove_XSS($survey_id).'\';" />';
  317. } else {
  318. echo '<input type="text" maxlength="20" name="name" value="" size="10">';
  319. echo '<input type="text" maxlength="250" name="description" value="" size="80">';
  320. echo '<input type="submit" value="'.get_lang('Create').'"';
  321. }
  322. echo '</form><br />';
  323. echo '<table class="data_table">';
  324. echo ' <tr class="row_odd">';
  325. echo ' <th width="200">'.get_lang('Name').'</th>';
  326. echo ' <th>'.get_lang('Description').'</th>';
  327. echo ' <th width="100">'.get_lang('Modify').'</th>';
  328. echo ' </tr>';
  329. $sql = 'SELECT id,name,description
  330. FROM '.$table_survey_question_group.'
  331. WHERE
  332. c_id = '.$course_id.' AND
  333. survey_id = '.intval($survey_id).'
  334. ORDER BY name';
  335. $rs = Database::query($sql);
  336. while ($row = Database::fetch_array($rs, 'ASSOC')) {
  337. $grouplist .= '<tr><td>'.$row['name'].'</td><td>'.$row['description'].'</td><td>'.
  338. '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$survey_id.'&gid='.$row['id'].'&action=editgroup">'.
  339. Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL).'</a> '.
  340. '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$survey_id.'&gid='.$row['id'].'&action=deletegroup" onclick="javascript:if(!confirm(\''.addslashes(api_htmlentities(sprintf(get_lang('DeleteSurveyGroup'), $row['name']).'?', ENT_QUOTES)).'\')) return false;">'.
  341. Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).'</a>'.
  342. '</td></tr>';
  343. }
  344. echo $grouplist.'</table>';
  345. }
  346. Session::erase('answer_count');
  347. Display::display_footer();