survey.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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_question = Database::get_course_table(TABLE_SURVEY_QUESTION);
  34. $table_survey_question_option = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  35. $table_survey_question_group = Database::get_course_table(TABLE_SURVEY_QUESTION_GROUP);
  36. $survey_id = (int) $_GET['survey_id'];
  37. $course_id = api_get_course_int_id();
  38. $action = isset($_GET['action']) ? $_GET['action'] : null;
  39. // Breadcrumbs
  40. $interbreadcrumb[] = [
  41. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php',
  42. 'name' => get_lang('Survey list'),
  43. ];
  44. // Getting the survey information
  45. if (!empty($_GET['survey_id'])) {
  46. $course_code = api_get_course_id();
  47. if ($course_code != -1) {
  48. $survey_data = SurveyManager::get_survey($survey_id);
  49. } else {
  50. api_not_allowed(true);
  51. }
  52. } else {
  53. api_not_allowed(true);
  54. }
  55. $tool_name = strip_tags($survey_data['title']);
  56. $is_survey_type_1 = $survey_data['survey_type'] == 1;
  57. if (api_strlen(strip_tags($survey_data['title'])) > 40) {
  58. $tool_name .= '...';
  59. }
  60. if ($is_survey_type_1 && ($action == 'addgroup' || $action == 'deletegroup')) {
  61. $_POST['name'] = trim($_POST['name']);
  62. if ($action == 'addgroup') {
  63. if (!empty($_POST['group_id'])) {
  64. Database::query('UPDATE '.$table_survey_question_group.' SET description = \''.Database::escape_string($_POST['description']).'\'
  65. WHERE c_id = '.$course_id.' AND id = \''.Database::escape_string($_POST['group_id']).'\'');
  66. $sendmsg = 'GroupUpdatedSuccessfully';
  67. } elseif (!empty($_POST['name'])) {
  68. 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']).'\',\''.$survey_id.'\') ');
  69. $sendmsg = 'GroupCreatedSuccessfully';
  70. } else {
  71. $sendmsg = 'GroupNeedName';
  72. }
  73. }
  74. if ($action == 'deletegroup') {
  75. $sql = 'DELETE FROM '.$table_survey_question_group.'
  76. WHERE c_id = '.$course_id.' AND id = '.intval($_GET['gid']).' AND survey_id = '.$survey_id;
  77. Database::query($sql);
  78. $sendmsg = 'GroupDeletedSuccessfully';
  79. }
  80. header('Location: '.api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$survey_id.'&sendmsg='.$sendmsg);
  81. exit;
  82. }
  83. // Displaying the header
  84. Display::display_header($tool_name, 'Survey');
  85. // Action handling
  86. $my_action_survey = Security::remove_XSS($action);
  87. $my_question_id_survey = isset($_GET['question_id']) ? Security::remove_XSS($_GET['question_id']) : null;
  88. $my_survey_id_survey = Security::remove_XSS($_GET['survey_id']);
  89. if (isset($action)) {
  90. if (($action == 'moveup' || $action == 'movedown') && isset($_GET['question_id'])) {
  91. SurveyManager::move_survey_question(
  92. $my_action_survey,
  93. $my_question_id_survey,
  94. $my_survey_id_survey
  95. );
  96. echo Display::return_message(get_lang('The question has been moved'), 'confirmation');
  97. }
  98. if ($action == 'delete' && is_numeric($_GET['question_id'])) {
  99. SurveyManager::delete_survey_question(
  100. $my_survey_id_survey,
  101. $my_question_id_survey,
  102. $survey_data['is_shared']
  103. );
  104. }
  105. }
  106. if (!empty($survey_data['survey_version'])) {
  107. echo '<b>'.get_lang('Version').': '.$survey_data['survey_version'].'</b>';
  108. }
  109. // We exit here is the first or last question is a pagebreak (which causes errors)
  110. SurveyUtil::check_first_last_question($_GET['survey_id']);
  111. // Action links
  112. $survey_actions = '';
  113. if ($survey_data['survey_type'] != 3) {
  114. $survey_actions = '<a href="'.api_get_path(WEB_CODE_PATH).'survey/create_new_survey.php?'.api_get_cidreq(
  115. ).'&action=edit&survey_id='.$survey_id.'">'.
  116. Display::return_icon('edit.png', get_lang('Edit survey'), '', ICON_SIZE_MEDIUM).'</a>';
  117. }
  118. $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('Delete survey').'?', ENT_QUOTES)).'\')) return false;">'.
  119. Display::return_icon('delete.png', get_lang('Delete survey'), '', ICON_SIZE_MEDIUM).'</a>';
  120. if ($survey_data['survey_type'] != 3) {
  121. $survey_actions .= '<a href="'.api_get_path(WEB_CODE_PATH).'survey/preview.php?'.api_get_cidreq().'&survey_id='.$survey_id.'">'.
  122. Display::return_icon('preview_view.png', get_lang('Preview'), '', ICON_SIZE_MEDIUM).'</a>';
  123. }
  124. $survey_actions .= '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey_invite.php?'.api_get_cidreq().'&survey_id='.$survey_id.'">'.
  125. Display::return_icon('mail_send.png', get_lang('Publish'), '', ICON_SIZE_MEDIUM).'</a>';
  126. if ($survey_data['survey_type'] != 3) {
  127. if (!api_get_configuration_value('hide_survey_reporting_button')) {
  128. $survey_actions .= Display::url(
  129. Display::return_icon('statistics.png', get_lang('Reporting'), [], ICON_SIZE_MEDIUM),
  130. api_get_path(WEB_CODE_PATH).'survey/reporting.php?'.api_get_cidreq().'&survey_id='.$survey_id
  131. );
  132. }
  133. }
  134. $survey_actions .= SurveyUtil::getAdditionalTeacherActions($survey_id, ICON_SIZE_MEDIUM);
  135. echo '<div class="actions">'.$survey_actions.'</div>';
  136. $urlQuestion = api_get_path(WEB_CODE_PATH).'survey/question.php?'.api_get_cidreq().'&action=add';
  137. if ($survey_data['survey_type'] == 0) {
  138. echo '<div class="well">';
  139. echo Display::url(
  140. Display::return_icon('yesno.png', get_lang('Yes / No'), null, ICON_SIZE_BIG),
  141. $urlQuestion.'&type=yesno&survey_id='.$survey_id
  142. );
  143. echo Display::url(
  144. Display::return_icon('mcua.png', get_lang('Multiple choice'), null, ICON_SIZE_BIG),
  145. $urlQuestion.'&type=multiplechoice&survey_id='.$survey_id
  146. );
  147. echo Display::url(
  148. Display::return_icon('mcma.png', get_lang('Multiple answers'), null, ICON_SIZE_BIG),
  149. $urlQuestion.'&type=multipleresponse&survey_id='.$survey_id
  150. );
  151. echo Display::url(
  152. Display::return_icon('open_answer.png', get_lang('Open'), null, ICON_SIZE_BIG),
  153. $urlQuestion.'&type=open&survey_id='.$survey_id
  154. );
  155. echo Display::url(
  156. Display::return_icon('dropdown.png', get_lang('Dropdown'), null, ICON_SIZE_BIG),
  157. $urlQuestion.'&type=dropdown&survey_id='.$survey_id
  158. );
  159. echo Display::url(
  160. Display::return_icon('percentagequestion.png', get_lang('Percentage'), null, ICON_SIZE_BIG),
  161. $urlQuestion.'&type=percentage&survey_id='.$survey_id
  162. );
  163. echo Display::url(
  164. Display::return_icon('scorequestion.png', get_lang('Score'), null, ICON_SIZE_BIG),
  165. $urlQuestion.'&type=score&survey_id='.$survey_id
  166. );
  167. echo Display::url(
  168. Display::return_icon('commentquestion.png', get_lang('Comment'), null, ICON_SIZE_BIG),
  169. $urlQuestion.'&type=comment&survey_id='.$survey_id
  170. );
  171. if ($survey_data['one_question_per_page'] == 0) {
  172. echo Display::url(
  173. Display::return_icon('page_end.png', get_lang('Page end (distinct questions)'), null, ICON_SIZE_BIG),
  174. $urlQuestion.'&type=pagebreak&survey_id='.$survey_id
  175. );
  176. }
  177. echo '</div>';
  178. } else {
  179. if ($survey_data['survey_type'] != 3) {
  180. echo '<div class="well">';
  181. echo Display::url(
  182. Display::return_icon('yesno.png', get_lang('Yes / No'), null, ICON_SIZE_BIG),
  183. $urlQuestion.'&type=personality&survey_id='.$survey_id
  184. );
  185. echo '</a></div>';
  186. }
  187. }
  188. // Displaying the table header with all the questions
  189. echo '<table class="table table-bordered">';
  190. echo '<thead>';
  191. echo '<tr>';
  192. echo ' <th width="5%">'.get_lang('N°').'</th>';
  193. echo ' <th width="50%">'.get_lang('Title').'</th>';
  194. echo ' <th width="15%">'.get_lang('Type').'</th>';
  195. echo ' <th width="15%" >'.get_lang('Options').'</th>';
  196. echo ' <th width="15%">'.get_lang('Edit').'</th>';
  197. if ($is_survey_type_1) {
  198. echo '<th width="100">'.get_lang('Condition').'</th>';
  199. echo '<th width="40">'.get_lang('Group').'</th>';
  200. }
  201. echo ' </tr>';
  202. echo '</thead>';
  203. // Displaying the table contents with all the questions
  204. $question_counter = 1;
  205. $sql = "SELECT * FROM $table_survey_question_group
  206. WHERE c_id = $course_id AND survey_id = $survey_id
  207. ORDER BY id";
  208. $result = Database::query($sql);
  209. $groups = [];
  210. while ($row = Database::fetch_array($result)) {
  211. $groups[$row['id']] = $row['name'];
  212. }
  213. $sql = "SELECT survey_question.*, count(survey_question_option.question_option_id) as number_of_options
  214. FROM $table_survey_question survey_question
  215. LEFT JOIN $table_survey_question_option survey_question_option
  216. ON
  217. survey_question.question_id = survey_question_option.question_id AND
  218. survey_question_option.c_id = survey_question.c_id
  219. WHERE
  220. survey_question.survey_id = $survey_id AND
  221. survey_question.c_id = $course_id
  222. GROUP BY survey_question.question_id
  223. ORDER BY survey_question.sort ASC";
  224. $result = Database::query($sql);
  225. $question_counter_max = Database::num_rows($result);
  226. while ($row = Database::fetch_array($result, 'ASSOC')) {
  227. echo '<tr>';
  228. echo ' <td>'.$question_counter.'</td>';
  229. echo ' <td>';
  230. if ($survey_data['survey_type'] != 3) {
  231. if (api_strlen($row['survey_question']) > 100) {
  232. echo api_substr(strip_tags($row['survey_question']), 0, 100).' ... ';
  233. } else {
  234. echo $row['survey_question'];
  235. }
  236. } else {
  237. $parts = explode('@@', $row['survey_question']);
  238. echo api_get_local_time($parts[0]).' - '.api_get_local_time($parts[1]);
  239. }
  240. if ($row['type'] == 'yesno') {
  241. $tool_name = get_lang('Yes / No');
  242. } elseif ($row['type'] == 'multiplechoice') {
  243. $tool_name = get_lang('Multiple choice');
  244. } else {
  245. $tool_name = get_lang(api_ucfirst(Security::remove_XSS($row['type'])));
  246. }
  247. echo '</td>';
  248. echo '<td>'.$tool_name.'</td>';
  249. echo '<td>'.$row['number_of_options'].'</td>';
  250. echo '<td>';
  251. if ($survey_data['survey_type'] != 3) {
  252. 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'].'">'.
  253. Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL).'</a>';
  254. }
  255. 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("Delete surveyQuestion").'?', ENT_QUOTES, $charset)).'\')) return false;">'.
  256. Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).'</a>';
  257. if ($survey_data['survey_type'] != 3) {
  258. if ($question_counter > 1) {
  259. echo '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey.php?'.api_get_cidreq(
  260. ).'&action=moveup&survey_id='.$survey_id.'&question_id='.$row['question_id'].'">'.
  261. Display::return_icon('up.png', get_lang('Move up'), '', ICON_SIZE_SMALL).'</a>';
  262. } else {
  263. Display::display_icon('up_na.png', '&nbsp;', '', ICON_SIZE_SMALL);
  264. }
  265. if ($question_counter < $question_counter_max) {
  266. echo '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey.php?'.api_get_cidreq(
  267. ).'&action=movedown&survey_id='.$survey_id.'&question_id='.$row['question_id'].'">'.
  268. Display::return_icon('down.png', get_lang('Move down'), '', ICON_SIZE_SMALL).'</a>';
  269. } else {
  270. Display::display_icon('down_na.png', '&nbsp;', '', ICON_SIZE_SMALL);
  271. }
  272. }
  273. echo ' </td>';
  274. $question_counter++;
  275. if ($is_survey_type_1) {
  276. echo '<td>'.(($row['survey_group_pri'] == 0) ? get_lang('Secondary') : get_lang('Primary')).'</td>';
  277. echo '<td>'.(($row['survey_group_pri'] == 0) ? $groups[$row['survey_group_sec1']].'-'.$groups[$row['survey_group_sec2']] : $groups[$row['survey_group_pri']]).'</td>';
  278. }
  279. echo '</tr>';
  280. }
  281. echo '</table>';
  282. if ($is_survey_type_1) {
  283. echo '<br /><br /><b>'.get_lang('Manage groups').'</b><br /><br />';
  284. if (in_array(
  285. $_GET['sendmsg'],
  286. ['GroupUpdatedSuccessfully', 'GroupDeletedSuccessfully', 'GroupCreatedSuccessfully']
  287. )
  288. ) {
  289. echo Display::return_message(
  290. get_lang($_GET['sendmsg']),
  291. 'confirmation',
  292. false
  293. );
  294. }
  295. if (in_array($_GET['sendmsg'], ['GroupNeedName'])) {
  296. echo Display::return_message(
  297. get_lang($_GET['sendmsg']),
  298. 'warning',
  299. false
  300. );
  301. }
  302. echo '<table border="0"><tr><td width="100">'.get_lang('Name').'</td><td>'.get_lang('Description').'</td></tr></table>';
  303. echo '<form action="'.api_get_path(WEB_CODE_PATH).'survey/survey.php?action=addgroup&survey_id='.$survey_id.'" method="post">';
  304. if ($_GET['action'] == 'editgroup') {
  305. $sql = 'SELECT name,description FROM '.$table_survey_question_group.'
  306. WHERE id = '.intval($_GET['gid']).' AND survey_id = '.$survey_id.' limit 1';
  307. $rs = Database::query($sql);
  308. $editedrow = Database::fetch_array($rs, 'ASSOC');
  309. echo '<input type="text" maxlength="20" name="name" value="'.$editedrow['name'].'" size="10" disabled>';
  310. echo '<input type="text" maxlength="150" name="description" value="'.$editedrow['description'].'" size="40">';
  311. echo '<input type="hidden" name="group_id" value="'.Security::remove_XSS($_GET['gid']).'">';
  312. 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).'\';" />';
  313. } else {
  314. echo '<input type="text" maxlength="20" name="name" value="" size="10">';
  315. echo '<input type="text" maxlength="250" name="description" value="" size="80">';
  316. echo '<input type="submit" value="'.get_lang('Create').'"';
  317. }
  318. echo '</form><br />';
  319. echo '<table class="data_table">';
  320. echo ' <tr class="row_odd">';
  321. echo ' <th width="200">'.get_lang('Name').'</th>';
  322. echo ' <th>'.get_lang('Description').'</th>';
  323. echo ' <th width="100">'.get_lang('Edit').'</th>';
  324. echo ' </tr>';
  325. $sql = 'SELECT id,name,description
  326. FROM '.$table_survey_question_group.'
  327. WHERE
  328. c_id = '.$course_id.' AND
  329. survey_id = '.intval($survey_id).'
  330. ORDER BY name';
  331. $rs = Database::query($sql);
  332. while ($row = Database::fetch_array($rs, 'ASSOC')) {
  333. $grouplist .= '<tr><td>'.$row['name'].'</td><td>'.$row['description'].'</td><td>'.
  334. '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$survey_id.'&gid='.$row['id'].'&action=editgroup">'.
  335. Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL).'</a> '.
  336. '<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('Delete surveyGroup'), $row['name']).'?', ENT_QUOTES)).'\')) return false;">'.
  337. Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).'</a>'.
  338. '</td></tr>';
  339. }
  340. echo $grouplist.'</table>';
  341. }
  342. Session::erase('answer_count');
  343. Display::display_footer();