survey.php 16 KB

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