survey.php 16 KB

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