survey.php 16 KB

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