survey.php 16 KB

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