survey.php 16 KB

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