survey.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php // $Id: survey.php 19829 2009-04-17 13:49:47Z pcool $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2009 Dokeos SPRL
  6. Copyright (c) 2003 Ghent University (UGent)
  7. For a full list of contributors, see "credits.txt".
  8. The full license can be read in "license.txt".
  9. This program is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU General Public License
  11. as published by the Free Software Foundation; either version 2
  12. of the License, or (at your option) any later version.
  13. See the GNU General Public License for more details.
  14. Contact: Dokeos, rue Notre Dame, 152, B-1140 Evere, Belgium, 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 19829 2009-04-17 13:49:47Z pcool $
  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. // coach can't view this page
  35. $extend_rights_for_coachs = api_get_setting('extend_rights_for_coach_on_survey');
  36. if (!api_is_allowed_to_edit(false,true) || (api_is_course_coach() && $extend_rights_for_coachs=='false'))
  37. {
  38. Display :: display_header(get_lang('Survey'));
  39. Display :: display_error_message(get_lang('NotAllowed'), false);
  40. Display :: display_footer();
  41. exit;
  42. }
  43. // Database table definitions
  44. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  45. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  46. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  47. $table_survey_question_group = Database :: get_course_table(TABLE_SURVEY_QUESTION_GROUP);
  48. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  49. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  50. $user_info = Database :: get_main_table(TABLE_MAIN_SURVEY_REMINDER);
  51. // breadcrumbs
  52. $interbreadcrumb[] = array ("url" => "survey_list.php", "name" => get_lang('SurveyList'));
  53. // getting the survey information
  54. $survey_data = survey_manager::get_survey($_GET['survey_id']);
  55. if (substr($survey_data['title'],0,3)!='<p>'){
  56. $tool_name = strip_tags(substr(html_entity_decode($survey_data['title'],ENT_QUOTES,$charset), 0, 40));
  57. }else{
  58. $tool_name = strip_tags(substr(html_entity_decode(substr($survey_data['title'],3,-4),ENT_QUOTES,$charset), 0, 40));
  59. }
  60. $is_survey_type_1 = ($survey_data['survey_type']==1)?true:false;
  61. if (strlen(strip_tags($survey_data['title'])) > 40)
  62. {
  63. $tool_name .= '...';
  64. }
  65. if($is_survey_type_1 && ($_GET['action']=='addgroup')||($_GET['action']=='deletegroup'))
  66. {
  67. $_POST['name'] = trim($_POST['name']);
  68. if(($_GET['action']=='addgroup'))
  69. {
  70. if(!empty($_POST['group_id']))
  71. {
  72. api_sql_query('UPDATE '.$table_survey_question_group.' SET description = \''.Database::escape_string($_POST['description']).'\' WHERE id = \''.Database::escape_string($_POST['group_id']).'\'');
  73. $sendmsg = 'GroupUpdatedSuccessfully';
  74. }
  75. elseif(!empty($_POST['name']))
  76. {
  77. api_sql_query('INSERT INTO '.$table_survey_question_group.' (name,description,survey_id) values (\''.Database::escape_string($_POST['name']).'\',\''.Database::escape_string($_POST['description']).'\',\''.Database::escape_string($_GET['survey_id']).'\') ');
  78. $sendmsg = 'GroupCreatedSuccessfully';
  79. } else {
  80. $sendmsg = 'GroupNeedName';
  81. }
  82. }
  83. if($_GET['action']=='deletegroup'){
  84. api_sql_query('DELETE FROM '.$table_survey_question_group.' WHERE id = '.Database::escape_string($_GET['gid']).' and survey_id = '.Database::escape_string($_GET['survey_id']));
  85. $sendmsg = 'GroupDeletedSuccessfully';
  86. }
  87. header('Location:survey.php?survey_id='.(int)$_GET['survey_id'].'&sendmsg='.$sendmsg);
  88. exit;
  89. }
  90. // Displaying the header
  91. Display::display_header($tool_name,'Survey');
  92. // Action handling
  93. $my_action_survey = Security::remove_XSS($_GET['action']);
  94. $my_question_id_survey = Security::remove_XSS($_GET['question_id']);
  95. $my_survey_id_survey = Security::remove_XSS($_GET['survey_id']);
  96. $message_information = Security::remove_XSS($_GET['message']);
  97. if (isset($_GET['action'])) {
  98. if (($_GET['action'] == 'moveup' OR $_GET['action'] == 'movedown') AND isset($_GET['question_id'])) {
  99. survey_manager::move_survey_question($my_action_survey,$my_question_id_survey,$my_survey_id_survey);
  100. Display::display_confirmation_message(get_lang('SurveyQuestionMoved'));
  101. }
  102. if ($_GET['action'] == 'delete' AND is_numeric($_GET['question_id'])) {
  103. survey_manager::delete_survey_question($my_survey_id_survey, $my_question_id_survey, $survey_data['is_shared']);
  104. }
  105. }
  106. if (isset($_GET['message'])) {
  107. // we have created the survey or updated the survey
  108. if (in_array($_GET['message'], array('SurveyUpdatedSuccesfully','SurveyCreatedSuccesfully'))) {
  109. Display::display_confirmation_message(get_lang($message_information).','.PHP_EOL.strtolower(get_lang('YouCanNowAddQuestionToYourSurvey')));
  110. }
  111. // we have added a question
  112. if (in_array($_GET['message'], array('QuestionAdded','QuestionUpdated'))) {
  113. Display::display_confirmation_message(get_lang($message_information));
  114. }
  115. if (in_array($_GET['message'], array('YouNeedToCreateGroups'))) {
  116. Display::display_warning_message(get_lang($message_information), false);
  117. }
  118. }
  119. if(!empty($survey_data['survey_version'])) echo '<b>'.get_lang('Version').': '.$survey_data['survey_version'].'</b>';
  120. // We exit here is the first or last question is a pagebreak (which causes errors)
  121. SurveyUtil::check_first_last_question($_GET['survey_id']);
  122. // Action links
  123. $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')).' '.get_lang('EditSurvey').'</a>';
  124. $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')).' '.get_lang('DeleteSurvey').'</a>';
  125. //$survey_actions .= '<a href="create_survey_in_another_language.php?id_survey='.$_GET['survey_id'].'">'.Display::return_icon('copy.gif', get_lang('Copy')).'</a>';
  126. $survey_actions .= '<a href="preview.php?'.api_get_cidreq().'&amp;survey_id='.$_GET['survey_id'].'">'.Display::return_icon('preview.gif', get_lang('Preview')).' '.get_lang('Preview').'</a>';
  127. $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')).' '.get_lang('Publish').'</a>';
  128. $survey_actions .= '<a href="reporting.php?'.api_get_cidreq().'&amp;survey_id='.$_GET['survey_id'].'">'.Display::return_icon('statistics.gif', get_lang('Reporting')).' '.get_lang('Reporting').'</a>';
  129. echo '<div class="actions">'.$survey_actions.'</div>';
  130. if ($survey_data['survey_type']==0) {
  131. echo '<div class="actionsbig">';
  132. echo '<a style="padding-left:0px;" 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>';
  133. echo '<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>';
  134. echo '<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>';
  135. echo '<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>';
  136. echo '<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>';
  137. echo '<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>';
  138. echo '<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>';
  139. echo '<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>';
  140. echo '<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>';
  141. echo '</div>';
  142. } else {
  143. echo '<div class="actionsbig">';
  144. //echo '<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>';
  145. echo '<a style="padding-left:0px;" href="question.php?'.api_get_cidreq().'&amp;action=add&type=personality&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/yesno.gif" />'.get_lang('PersonalityQuestion').'</a></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>'.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 WHERE survey_id = '".Database::escape_string($_GET['survey_id'])."' ORDER BY id";
  164. $result = api_sql_query($sql, __FILE__, __LINE__);
  165. $groups = array();
  166. while($row = Database::fetch_array($result)) {
  167. $groups[$row['id']] = $row['name'];
  168. }
  169. $sql = "SELECT survey_question.*, count(survey_question_option.question_option_id) as number_of_options
  170. FROM $table_survey_question survey_question
  171. LEFT JOIN $table_survey_question_option survey_question_option
  172. ON survey_question.question_id = survey_question_option.question_id
  173. WHERE survey_question.survey_id = '".Database::escape_string($_GET['survey_id'])."'
  174. GROUP BY survey_question.question_id
  175. ORDER BY survey_question.sort ASC";
  176. $result = api_sql_query($sql, __FILE__, __LINE__);
  177. $question_counter_max = Database::num_rows($result);
  178. while ($row = Database::fetch_array($result,'ASSOC')) {
  179. echo '<tr>';
  180. echo ' <td>'.$question_counter.'</td>';
  181. echo ' <td>';
  182. if (strlen($row['survey_question']) > 100) {
  183. echo substr(strip_tags($row['survey_question']),0, 100).' ... ';
  184. } else {
  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)
  208. {
  209. echo '<td>'.(($row['survey_group_pri']==0)?get_lang('Secondary'):get_lang('Primary')).'</td>';
  210. echo '<td>'.(($row['survey_group_pri']==0)?$groups[$row['survey_group_sec1']].'-'.$groups[$row['survey_group_sec2']]:$groups[$row['survey_group_pri']]).'</td>';
  211. }
  212. echo '</tr>';
  213. }
  214. echo '</table>';
  215. if($is_survey_type_1)
  216. {
  217. echo '<br /><br /><b>'.get_lang('ManageGroups').'</b><br /><br />';
  218. if (in_array($_GET['sendmsg'], array('GroupUpdatedSuccessfully','GroupDeletedSuccessfully','GroupCreatedSuccessfully'))){
  219. echo Display::display_confirmation_message(get_lang($_GET['sendmsg']), false);
  220. }
  221. if (in_array($_GET['sendmsg'], array('GroupNeedName'))){
  222. echo Display::display_warning_message(get_lang($_GET['sendmsg']), false);
  223. }
  224. echo '<table border="0"><tr><td width="100">'.get_lang('Name').'</td><td>'.get_lang('Description').'</td></tr></table>';
  225. echo '<form action="survey.php?action=addgroup&survey_id='.(int)$_GET['survey_id'].'" method="post">';
  226. if($_GET['action']=='editgroup') {
  227. $sql = 'SELECT name,description FROM '.$table_survey_question_group.' WHERE id = '.Database::escape_string($_GET['gid']).' AND survey_id = '.Database::escape_string($_GET['survey_id']).' limit 1';
  228. $rs = api_sql_query($sql,__FILE__,__LINE__);
  229. $editedrow = Database::fetch_array($rs,'ASSOC');
  230. echo '<input type="text" maxlength="20" name="name" value="'.$editedrow['name'].'" size="10" disabled>';
  231. echo '<input type="text" maxlength="150" name="description" value="'.$editedrow['description'].'" size="40">';
  232. echo '<input type="hidden" name="group_id" value="'.Security::remove_XSS($_GET['gid']).'">';
  233. 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($_GET['survey_id']).'\';" />';
  234. } else {
  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_survey_question_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. ?>