survey.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. /*
  3. DOKEOS - elearning and course management software
  4. For a full list of contributors, see documentation/credits.html
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. See "documentation/licence.html" more details.
  10. Contact:
  11. Dokeos
  12. Rue des Palais 44 Paleizenstraat
  13. B-1030 Brussels - Belgium
  14. Tel. +32 (2) 211 34 56
  15. */
  16. /**
  17. * @package dokeos.survey
  18. * @author unknown
  19. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University: cleanup, refactoring and rewriting large parts of the code
  20. * @version $Id: survey.php 15840 2008-07-23 22:59:44Z dperales $
  21. *
  22. * @todo use quickforms for the forms
  23. */
  24. // name of the language file that needs to be included
  25. $language_file = 'survey';
  26. // including the global dokeos file
  27. require ('../inc/global.inc.php');
  28. // including additional libraries
  29. //require_once (api_get_path(LIBRARY_PATH)."/survey.lib.php");
  30. require_once('survey.lib.php');
  31. require_once (api_get_path(LIBRARY_PATH)."/course.lib.php");
  32. /** @todo this has to be moved to a more appropriate place (after the display_header of the code)*/
  33. if (!api_is_allowed_to_edit())
  34. {
  35. Display :: display_header(get_lang('Survey'));
  36. Display :: display_error_message(get_lang('NotAllowed'), false);
  37. Display :: display_footer();
  38. exit;
  39. }
  40. // Database table definitions
  41. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  42. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  43. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  44. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  45. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  46. $user_info = Database :: get_main_table(TABLE_MAIN_SURVEY_REMINDER);
  47. $table_survey_question_group = Database :: get_main_table(TABLE_SURVEY_QUESTION_GROUP);
  48. // breadcrumbs
  49. $interbreadcrumb[] = array ("url" => "survey_list.php", "name" => get_lang('SurveyList'));
  50. // getting the survey information
  51. $survey_data = survey_manager::get_survey($_GET['survey_id']);
  52. $tool_name = substr(html_entity_decode($survey_data['title'],ENT_QUOTES,$charset), 0, 40);
  53. if (strlen(strip_tags($survey_data['title'])) > 40)
  54. {
  55. $tool_name .= '...';
  56. }
  57. if(($_GET['action']=='addgroup')||($_GET['action']=='deletegroup')){
  58. $table_survey_group = Database::get_course_table(TABLE_SURVEY_GROUP);
  59. $_POST['name'] = trim($_POST['name']);
  60. if(($_GET['action']=='addgroup')){
  61. if(!empty($_POST['group_id'])){
  62. api_sql_query('UPDATE '.$table_survey_group.' SET description = \''.Database::escape_string($_POST['description']).'\' WHERE id = \''.Database::escape_string($_POST['group_id']).'\'');
  63. $sendmsg = 'GroupUpdatedSuccessfully';
  64. } elseif(!empty($_POST['name'])){
  65. api_sql_query('INSERT INTO '.$table_survey_group.' (name,description,survey_id) values (\''.Database::escape_string($_POST['name']).'\',\''.Database::escape_string($_POST['description']).'\',\''.Database::escape_string($_GET['survey_id']).'\') ');
  66. $sendmsg = 'GroupCreatedSuccessfully';
  67. } else {
  68. $sendmsg = 'GroupNeedName';
  69. }
  70. }
  71. if($_GET['action']=='deletegroup'){
  72. api_sql_query('DELETE FROM '.$table_survey_group.' WHERE id = '.Database::escape_string($_GET['gid']).' and survey_id = '.Database::escape_string($_GET['survey_id']));
  73. $sendmsg = 'GroupDeletedSuccessfully';
  74. }
  75. header('Location:survey.php?survey_id='.(int)$_GET['survey_id'].'&sendmsg='.$sendmsg);
  76. exit;
  77. }
  78. // Displaying the header
  79. Display::display_header($tool_name,'Survey');
  80. // Action handling
  81. if (isset($_GET['action']))
  82. {
  83. if (($_GET['action'] == 'moveup' OR $_GET['action'] == 'movedown') AND isset($_GET['question_id']))
  84. {
  85. survey_manager::move_survey_question($_GET['action'], $_GET['question_id'], $_GET['survey_id']);
  86. Display::display_confirmation_message('SurveyQuestionMoved', false);
  87. }
  88. if ($_GET['action'] == 'delete' AND is_numeric($_GET['question_id']))
  89. {
  90. survey_manager::delete_survey_question($_GET['survey_id'], $_GET['question_id'], $survey_data['is_shared']);
  91. }
  92. }
  93. if (isset($_GET['message']))
  94. {
  95. // we have created the survey or updated the survey
  96. if (in_array($_GET['message'], array('SurveyUpdatedSuccesfully','SurveyCreatedSuccesfully')))
  97. {
  98. Display::display_confirmation_message(get_lang($_GET['message']).'<br />'.get_lang('YouCanNowAddQuestionToYourSurvey'), false);
  99. }
  100. // we have added a question
  101. if (in_array($_GET['message'], array('QuestionAdded','QuestionUpdated')))
  102. {
  103. Display::display_confirmation_message(get_lang($_GET['message']), false);
  104. }
  105. if (in_array($_GET['message'], array('YouNeedToCreateGroups')))
  106. {
  107. Display::display_warning_message(get_lang($_GET['message']), false);
  108. }
  109. }
  110. if(!empty($survey_data['version'])) echo '<b>'.get_lang('Version').': '.$survey_data['version'].'</b>';
  111. // We exit here is the first or last question is a pagebreak (which causes errors)
  112. SurveyUtil::check_first_last_question($_GET['survey_id']);
  113. // Action links
  114. $survey_actions = get_lang('Survey').': ';
  115. $survey_actions .= '<a href="create_new_survey.php?'.api_get_cidreq().'&amp;action=edit&amp;survey_id='.$_GET['survey_id'].'">'.Display::return_icon('edit.gif', get_lang('Edit')).'</a>';
  116. $survey_actions .= '<a href="survey_list.php?'.api_get_cidreq().'&amp;action=delete&amp;survey_id='.$_GET['survey_id'].'" onclick="javascript:if(!confirm(\''.addslashes(htmlentities(get_lang("DeleteSurvey").'?',ENT_QUOTES,$charset)).'\')) return false;">'.Display::return_icon('delete.gif', get_lang('Delete')).'</a>';
  117. //$survey_actions .= '<a href="create_survey_in_another_language.php?id_survey='.$_GET['survey_id'].'">'.Display::return_icon('copy.gif', get_lang('Copy')).'</a>';
  118. $survey_actions .= '<a href="preview.php?'.api_get_cidreq().'&amp;survey_id='.$_GET['survey_id'].'">'.Display::return_icon('preview.gif', get_lang('Preview')).'</a>';
  119. $survey_actions .= '<a href="survey_invite.php?'.api_get_cidreq().'&amp;survey_id='.$_GET['survey_id'].'">'.Display::return_icon('survey_publish.gif', get_lang('Publish')).'</a>';
  120. $survey_actions .= '<a href="reporting.php?'.api_get_cidreq().'&amp;survey_id='.$_GET['survey_id'].'">'.Display::return_icon('statistics.gif', get_lang('Reporting')).'</a>';
  121. echo '<div style="float:right;">'.$survey_actions.'</div>';
  122. echo '<div>';
  123. echo '<div style="float:left; text-align:center; margin:5px;"><a href="question.php?'.api_get_cidreq().'&amp;action=add&type=yesno&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/yesno.gif" /><br />'.get_lang('YesNo').'</a></div>';
  124. echo '<div style="float:left; text-align:center; margin:5px;"><a href="question.php?'.api_get_cidreq().'&amp;action=add&type=multiplechoice&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/mcua.gif" /><br />'.get_lang('MultipleChoice').'</a></div>';
  125. echo '<div style="float:left; text-align:center; margin:5px;"><a href="question.php?'.api_get_cidreq().'&amp;action=add&type=multipleresponse&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/mcma.gif" /><br />'.get_lang('MultipleResponse').'</a></div>';
  126. echo '<div style="float:left; text-align:center; margin:5px;"><a href="question.php?'.api_get_cidreq().'&amp;action=add&type=open&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/open_answer.gif" /><br />'.get_lang('Open').'</a></div>';
  127. echo '<div style="float:left; text-align:center; margin:5px;"><a href="question.php?'.api_get_cidreq().'&amp;action=add&type=dropdown&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/dropdown.gif" /><br />'.get_lang('Dropdown').'</a></div>';
  128. echo '<div style="float:left; text-align:center; margin:5px;"><a href="question.php?'.api_get_cidreq().'&amp;action=add&type=percentage&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/percentagequestion.gif" /><br />'.get_lang('Percentage').'</a></div>';
  129. echo '<div style="float:left; text-align:center; margin:5px;"><a href="question.php?'.api_get_cidreq().'&amp;action=add&type=score&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/scorequestion.gif" /><br />'.get_lang('Score').'</a></div>';
  130. echo '<div style="float:left; text-align:center; margin:5px;"><a href="question.php?'.api_get_cidreq().'&amp;action=add&type=comment&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/commentquestion.gif" /><br />'.get_lang('Comment').'</a></div>';
  131. echo '<div style="float:left; text-align:center; margin:5px;"><a href="question.php?'.api_get_cidreq().'&amp;action=add&type=pagebreak&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/page_end.gif" /><br />'.get_lang('Pagebreak').'</a></div>';
  132. echo '</div>';
  133. echo '<div style="clear:both;"></div>';
  134. // Displaying the table header with all the questions
  135. echo '<table class="data_table">';
  136. echo ' <tr class="row_odd">';
  137. echo ' <th width="15">'.get_lang('QuestionNumber').'</th>';
  138. echo ' <th>'.get_lang('Title').'</th>';
  139. echo ' <th>'.get_lang('Type').'</th>';
  140. echo ' <th>'.get_lang('NumberOfOptions').'</th>';
  141. echo ' <th width="100">'.get_lang('Modify').'</th>';
  142. if($survey_data['type']==1) echo '<th width="100">'.get_lang('Condition').'</th>';
  143. echo ' </tr>';
  144. // Displaying the table contents with all the questions
  145. $question_counter = 1;
  146. $sql = "SELECT survey_question.*, count(survey_question_option.question_option_id) as number_of_options
  147. FROM $table_survey_question survey_question
  148. LEFT JOIN $table_survey_question_option survey_question_option
  149. ON survey_question.question_id = survey_question_option.question_id
  150. WHERE survey_question.survey_id = '".Database::escape_string($_GET['survey_id'])."'
  151. GROUP BY survey_question.question_id
  152. ORDER BY survey_question.sort ASC";
  153. $result = api_sql_query($sql, __FILE__, __LINE__);
  154. $question_counter_max = mysql_num_rows($result);
  155. while ($row = mysql_fetch_assoc($result))
  156. {
  157. echo '<tr>';
  158. echo ' <td>'.$question_counter.'</td>';
  159. echo ' <td>';
  160. if (strlen($row['survey_question']) > 100)
  161. {
  162. echo substr(strip_tags($row['survey_question']),0, 100).' ... ';
  163. }
  164. else
  165. {
  166. echo $row['survey_question'];
  167. }
  168. echo '</td>';
  169. echo ' <td>'.get_lang(ucfirst($row['type'])).'</td>';
  170. echo ' <td>'.$row['number_of_options'].'</td>';
  171. echo ' <td>';
  172. echo ' <a href="question.php?'.api_get_cidreq().'&amp;action=edit&amp;type='.$row['type'].'&amp;survey_id='.$_GET['survey_id'].'&amp;question_id='.$row['question_id'].'">'.Display::return_icon('edit.gif', get_lang('Edit')).'</a>';
  173. echo ' <a href="survey.php?'.api_get_cidreq().'&amp;action=delete&amp;survey_id='.$_GET['survey_id'].'&amp;question_id='.$row['question_id'].'" onclick="javascript:if(!confirm(\''.addslashes(htmlentities(get_lang("DeleteSurveyQuestion").'?',ENT_QUOTES,$charset)).'\')) return false;">'.Display::return_icon('delete.gif', get_lang('Delete')).'</a>';
  174. if ($question_counter > 1)
  175. {
  176. echo ' <a href="survey.php?'.api_get_cidreq().'&amp;action=moveup&amp;survey_id='.$_GET['survey_id'].'&amp;question_id='.$row['question_id'].'">'.Display::return_icon('up.gif', get_lang('MoveUp')).'</a>';
  177. } else {
  178. echo ' <img src="../img/up_na.gif"> ';
  179. }
  180. if ($question_counter < $question_counter_max)
  181. {
  182. echo ' <a href="survey.php?'.api_get_cidreq().'&amp;action=movedown&amp;survey_id='.$_GET['survey_id'].'&amp;question_id='.$row['question_id'].'">'.Display::return_icon('down.gif', get_lang('MoveDown')).'</a>';
  183. } else {
  184. echo ' <img src="../img/down_na.gif"> ';
  185. }
  186. echo ' </td>';
  187. $question_counter++;
  188. if($survey_data['type']==1) echo '<td>'.(($survey_data['cond_group_basic']==0)?get_lang('Basic'):get_lang('Secondary')).'</td>';
  189. echo '</tr>';
  190. }
  191. echo '</table>';
  192. if($survey_data['type']==1)
  193. {
  194. echo '<br /><br /><b>'.get_lang('ManageGroups').'</b><br /><br />';
  195. if (in_array($_GET['sendmsg'], array('GroupUpdatedSuccessfully','GroupDeletedSuccessfully','GroupCreatedSuccessfully'))){
  196. echo Display::display_confirmation_message(get_lang($_GET['sendmsg']), false);
  197. }
  198. if (in_array($_GET['sendmsg'], array('GroupNeedName'))){
  199. echo Display::display_warning_message(get_lang($_GET['sendmsg']), false);
  200. }
  201. echo '<table border="0"><tr><td width="100">'.get_lang('Name').'</td><td>'.get_lang('Description').'</td></tr></table>';
  202. $table_surve_group = Database::get_course_table(TABLE_SURVEY_GROUP);
  203. echo '<form action="survey.php?action=addgroup&survey_id='.(int)$_GET['survey_id'].'" method="post">';
  204. if($_GET['action']=='editgroup')
  205. {
  206. $sql = 'SELECT name,description FROM '.$table_surve_group.' WHERE id = '.(int)$_GET['gid'].' AND survey_id = '.Database::escape_string($_GET['survey_id']).' limit 1';
  207. $rs = api_sql_query($sql,__FILE__,__LINE__);
  208. $editedrow = Database::fetch_array($rs,ASSOC);
  209. echo '<input type="text" maxlength="20" name="name" value="'.$editedrow['name'].'" size="10" disabled>';
  210. echo '<input type="text" maxlength="150" name="description" value="'.$editedrow['description'].'" size="40">';
  211. echo '<input type="hidden" name="group_id" value="'.(int)$_GET['gid'].'">';
  212. echo '<input type="submit" value="'.get_lang('Save').'"'.'<input type="button" value="'.get_lang('Cancel').'" onclick="window.location.href = \'survey.php?survey_id='.(int)$_GET['survey_id'].'\';" />';
  213. } else {
  214. echo '<input type="text" maxlength="20" name="name" value="" size="10">';
  215. echo '<input type="text" maxlength="150" name="description" value="" size="40">';
  216. echo '<input type="submit" value="'.get_lang('Create').'"';
  217. }
  218. echo '</form><br />';
  219. echo '<table class="data_table">';
  220. echo ' <tr class="row_odd">';
  221. echo ' <th width="200">'.get_lang('Name').'</th>';
  222. echo ' <th>'.get_lang('Description').'</th>';
  223. echo ' <th width="100">'.get_lang('Modify').'</th>';
  224. echo ' </tr>';
  225. $sql = 'SELECT id,name,description FROM '.$table_surve_group.' WHERE survey_id = '.Database::escape_string($_GET['survey_id']);
  226. $rs = api_sql_query($sql,__FILE__,__LINE__);
  227. while($row = Database::fetch_array($rs,NUM)){
  228. $grouplist .= '<tr><td>'.$row[1].'</td><td>'.$row[2].'</td><td>'.
  229. '<a href="survey.php?survey_id='.(int)$_GET['survey_id'].'&gid='.$row[0].'&action=editgroup">'.
  230. Display::return_icon('edit.gif', get_lang('Edit')).'</a> '.
  231. '<a href="survey.php?survey_id='.(int)$_GET['survey_id'].'&gid='.$row[0].'&action=deletegroup" onclick="javascript:if(!confirm(\''.addslashes(htmlentities(sprintf(get_lang('DeleteSurveyGroup'),$row[1]).'?',ENT_QUOTES,$charset)).'\')) return false;">'.
  232. Display::return_icon('delete.gif', get_lang('Delete')).'</a>'.
  233. '</td></tr>';
  234. }
  235. echo $grouplist.'</table>';
  236. }
  237. // Footer
  238. Display :: display_footer();
  239. ?>