survey.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php // $Id: survey.php 16739 2008-11-13 15:36:40Z pcool $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2008 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 16739 2008-11-13 15:36:40Z 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. $tool_name = substr(html_entity_decode($survey_data['title'],ENT_QUOTES,$charset), 0, 40);
  56. $is_survey_type_1 = ($survey_data['survey_type']==1)?true:false;
  57. if (strlen(strip_tags($survey_data['title'])) > 40)
  58. {
  59. $tool_name .= '...';
  60. }
  61. if($is_survey_type_1 && ($_GET['action']=='addgroup')||($_GET['action']=='deletegroup'))
  62. {
  63. $_POST['name'] = trim($_POST['name']);
  64. if(($_GET['action']=='addgroup'))
  65. {
  66. if(!empty($_POST['group_id']))
  67. {
  68. api_sql_query('UPDATE '.$table_survey_question_group.' SET description = \''.Database::escape_string($_POST['description']).'\' WHERE id = \''.Database::escape_string($_POST['group_id']).'\'');
  69. $sendmsg = 'GroupUpdatedSuccessfully';
  70. }
  71. elseif(!empty($_POST['name']))
  72. {
  73. 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']).'\') ');
  74. $sendmsg = 'GroupCreatedSuccessfully';
  75. } else {
  76. $sendmsg = 'GroupNeedName';
  77. }
  78. }
  79. if($_GET['action']=='deletegroup'){
  80. 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']));
  81. $sendmsg = 'GroupDeletedSuccessfully';
  82. }
  83. header('Location:survey.php?survey_id='.(int)$_GET['survey_id'].'&sendmsg='.$sendmsg);
  84. exit;
  85. }
  86. // Displaying the header
  87. Display::display_header($tool_name,'Survey');
  88. // Action handling
  89. if (isset($_GET['action']))
  90. {
  91. if (($_GET['action'] == 'moveup' OR $_GET['action'] == 'movedown') AND isset($_GET['question_id']))
  92. {
  93. survey_manager::move_survey_question($_GET['action'], $_GET['question_id'], $_GET['survey_id']);
  94. Display::display_confirmation_message('SurveyQuestionMoved', false);
  95. }
  96. if ($_GET['action'] == 'delete' AND is_numeric($_GET['question_id']))
  97. {
  98. survey_manager::delete_survey_question($_GET['survey_id'], $_GET['question_id'], $survey_data['is_shared']);
  99. }
  100. }
  101. if (isset($_GET['message']))
  102. {
  103. // we have created the survey or updated the survey
  104. if (in_array($_GET['message'], array('SurveyUpdatedSuccesfully','SurveyCreatedSuccesfully')))
  105. {
  106. Display::display_confirmation_message(get_lang($_GET['message']).'<br />'.get_lang('YouCanNowAddQuestionToYourSurvey'), false);
  107. }
  108. // we have added a question
  109. if (in_array($_GET['message'], array('QuestionAdded','QuestionUpdated')))
  110. {
  111. Display::display_confirmation_message(get_lang($_GET['message']), false);
  112. }
  113. if (in_array($_GET['message'], array('YouNeedToCreateGroups')))
  114. {
  115. Display::display_warning_message(get_lang($_GET['message']), false);
  116. }
  117. }
  118. if(!empty($survey_data['survey_version'])) echo '<b>'.get_lang('Version').': '.$survey_data['survey_version'].'</b>';
  119. // We exit here is the first or last question is a pagebreak (which causes errors)
  120. SurveyUtil::check_first_last_question($_GET['survey_id']);
  121. // Action links
  122. $survey_actions = get_lang('Survey').': ';
  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. //print_r($survey_data);
  131. if ($survey_data['survey_type']==0)
  132. {
  133. echo '<div class="actionsbig">';
  134. echo '<a href="question.php?'.api_get_cidreq().'&amp;action=add&type=yesno&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/yesno.gif" />'.get_lang('YesNo').'</a>';
  135. echo '<a href="question.php?'.api_get_cidreq().'&amp;action=add&type=multiplechoice&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/mcua.gif" />'.get_lang('MultipleChoice').'</a>';
  136. echo '<a href="question.php?'.api_get_cidreq().'&amp;action=add&type=multipleresponse&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/mcma.gif" />'.get_lang('MultipleResponse').'</a>';
  137. 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" />'.get_lang('Open').'</a>';
  138. echo '<a href="question.php?'.api_get_cidreq().'&amp;action=add&type=dropdown&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/dropdown.gif" />'.get_lang('Dropdown').'</a>';
  139. echo '<a href="question.php?'.api_get_cidreq().'&amp;action=add&type=percentage&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/percentagequestion.gif" />'.get_lang('Percentage').'</a>';
  140. echo '<a href="question.php?'.api_get_cidreq().'&amp;action=add&type=score&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/scorequestion.gif" />'.get_lang('Score').'</a>';
  141. echo '<a href="question.php?'.api_get_cidreq().'&amp;action=add&type=comment&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/commentquestion.gif" />'.get_lang('Comment').'</a>';
  142. 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" />'.get_lang('Pagebreak').'</a>';
  143. echo '</div>';
  144. }
  145. else
  146. {
  147. echo '<div>';
  148. //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>';
  149. echo '<a 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>';
  150. echo '</div>';
  151. }
  152. // Displaying the table header with all the questions
  153. echo '<table class="data_table">';
  154. echo ' <tr class="row_odd">';
  155. echo ' <th width="15">'.get_lang('QuestionNumber').'</th>';
  156. echo ' <th>'.get_lang('Title').'</th>';
  157. echo ' <th>'.get_lang('Type').'</th>';
  158. echo ' <th>'.get_lang('NumberOfOptions').'</th>';
  159. echo ' <th width="100">'.get_lang('Modify').'</th>';
  160. if($is_survey_type_1)
  161. {
  162. echo '<th width="100">'.get_lang('Condition').'</th>';
  163. echo '<th width="40">'.get_lang('Group').'</th>';
  164. }
  165. echo ' </tr>';
  166. // Displaying the table contents with all the questions
  167. $question_counter = 1;
  168. $sql = "SELECT * FROM $table_survey_question_group WHERE survey_id = '".(int)$_GET['survey_id']."' ORDER BY id";
  169. $result = api_sql_query($sql, __FILE__, __LINE__);
  170. $groups = array();
  171. while($row = Database::fetch_array($result))
  172. {
  173. $groups[$row['id']] = $row['name'];
  174. }
  175. $sql = "SELECT survey_question.*, count(survey_question_option.question_option_id) as number_of_options
  176. FROM $table_survey_question survey_question
  177. LEFT JOIN $table_survey_question_option survey_question_option
  178. ON survey_question.question_id = survey_question_option.question_id
  179. WHERE survey_question.survey_id = '".Database::escape_string($_GET['survey_id'])."'
  180. GROUP BY survey_question.question_id
  181. ORDER BY survey_question.sort ASC";
  182. $result = api_sql_query($sql, __FILE__, __LINE__);
  183. $question_counter_max = Database::num_rows($result);
  184. while ($row = Database::fetch_array($result,'ASSOC'))
  185. {
  186. echo '<tr>';
  187. echo ' <td>'.$question_counter.'</td>';
  188. echo ' <td>';
  189. if (strlen($row['survey_question']) > 100)
  190. {
  191. echo substr(strip_tags($row['survey_question']),0, 100).' ... ';
  192. }
  193. else
  194. {
  195. echo $row['survey_question'];
  196. }
  197. echo '</td>';
  198. echo ' <td>'.get_lang(ucfirst($row['type'])).'</td>';
  199. echo ' <td>'.$row['number_of_options'].'</td>';
  200. echo ' <td>';
  201. 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>';
  202. 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>';
  203. if ($question_counter > 1)
  204. {
  205. 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>';
  206. } else {
  207. echo ' <img src="../img/up_na.gif"> ';
  208. }
  209. if ($question_counter < $question_counter_max)
  210. {
  211. 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>';
  212. } else {
  213. echo ' <img src="../img/down_na.gif"> ';
  214. }
  215. echo ' </td>';
  216. $question_counter++;
  217. if($is_survey_type_1)
  218. {
  219. echo '<td>'.(($row['survey_group_pri']==0)?get_lang('Secondary'):get_lang('Primary')).'</td>';
  220. echo '<td>'.(($row['survey_group_pri']==0)?$groups[$row['survey_group_sec1']].'-'.$groups[$row['survey_group_sec2']]:$groups[$row['survey_group_pri']]).'</td>';
  221. }
  222. echo '</tr>';
  223. }
  224. echo '</table>';
  225. if($is_survey_type_1)
  226. {
  227. echo '<br /><br /><b>'.get_lang('ManageGroups').'</b><br /><br />';
  228. if (in_array($_GET['sendmsg'], array('GroupUpdatedSuccessfully','GroupDeletedSuccessfully','GroupCreatedSuccessfully'))){
  229. echo Display::display_confirmation_message(get_lang($_GET['sendmsg']), false);
  230. }
  231. if (in_array($_GET['sendmsg'], array('GroupNeedName'))){
  232. echo Display::display_warning_message(get_lang($_GET['sendmsg']), false);
  233. }
  234. echo '<table border="0"><tr><td width="100">'.get_lang('Name').'</td><td>'.get_lang('Description').'</td></tr></table>';
  235. echo '<form action="survey.php?action=addgroup&survey_id='.(int)$_GET['survey_id'].'" method="post">';
  236. if($_GET['action']=='editgroup')
  237. {
  238. $sql = 'SELECT name,description FROM '.$table_survey_question_group.' WHERE id = '.(int)$_GET['gid'].' AND survey_id = '.Database::escape_string($_GET['survey_id']).' limit 1';
  239. $rs = api_sql_query($sql,__FILE__,__LINE__);
  240. $editedrow = Database::fetch_array($rs,'ASSOC');
  241. echo '<input type="text" maxlength="20" name="name" value="'.$editedrow['name'].'" size="10" disabled>';
  242. echo '<input type="text" maxlength="150" name="description" value="'.$editedrow['description'].'" size="40">';
  243. echo '<input type="hidden" name="group_id" value="'.(int)$_GET['gid'].'">';
  244. 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'].'\';" />';
  245. }
  246. else
  247. {
  248. echo '<input type="text" maxlength="20" name="name" value="" size="10">';
  249. echo '<input type="text" maxlength="250" name="description" value="" size="80">';
  250. echo '<input type="submit" value="'.get_lang('Create').'"';
  251. }
  252. echo '</form><br />';
  253. echo '<table class="data_table">';
  254. echo ' <tr class="row_odd">';
  255. echo ' <th width="200">'.get_lang('Name').'</th>';
  256. echo ' <th>'.get_lang('Description').'</th>';
  257. echo ' <th width="100">'.get_lang('Modify').'</th>';
  258. echo ' </tr>';
  259. $sql = 'SELECT id,name,description FROM '.$table_survey_question_group.' WHERE survey_id = '.Database::escape_string($_GET['survey_id']).' ORDER BY name';
  260. $rs = api_sql_query($sql,__FILE__,__LINE__);
  261. while($row = Database::fetch_array($rs,ASSOC)){
  262. $grouplist .= '<tr><td>'.$row['name'].'</td><td>'.$row['description'].'</td><td>'.
  263. '<a href="survey.php?survey_id='.(int)$_GET['survey_id'].'&gid='.$row['id'].'&action=editgroup">'.
  264. Display::return_icon('edit.gif', get_lang('Edit')).'</a> '.
  265. '<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;">'.
  266. Display::return_icon('delete.gif', get_lang('Delete')).'</a>'.
  267. '</td></tr>';
  268. }
  269. echo $grouplist.'</table>';
  270. }
  271. // Footer
  272. Display :: display_footer();
  273. ?>