survey_list.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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, the initial survey that did not make it in 1.8 because of bad code
  19. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University: cleanup, refactoring and rewriting large parts of the code
  20. * @version $Id: survey_list.php 13922 2007-12-04 23:20:19Z yannoo $
  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();
  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_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  44. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  45. // language variables
  46. if (isset ($_GET['search']) && $_GET['search'] == 'advanced')
  47. {
  48. $interbreadcrumb[] = array ('url' => 'survey_list.php', 'name' => get_lang('SurveyList'));
  49. $tool_name = get_lang('SearchASurvey');
  50. }
  51. else
  52. {
  53. $tool_name = get_lang('SurveyList');
  54. }
  55. // Header
  56. Display :: display_header($tool_name);
  57. //api_display_tool_title($tool_name);
  58. // Action handling: searching
  59. if (isset ($_GET['search']) AND $_GET['search'] == 'advanced')
  60. {
  61. display_survey_search_form();
  62. }
  63. // Action handling: deleting a survey
  64. if (isset($_GET['action']) AND $_GET['action'] == 'delete' AND isset($_GET['survey_id']) AND is_numeric($_GET['survey_id']))
  65. {
  66. // getting the information of the survey (used for when the survey is shared)
  67. $survey_data = survey_manager::get_survey($_GET['survey_id']);
  68. // if the survey is shared => also delete the shared content
  69. if (is_numeric($survey_data['survey_share']))
  70. {
  71. survey_manager::delete_survey($survey_data['survey_share'], true);
  72. }
  73. $return = survey_manager :: delete_survey($_GET['survey_id']);
  74. if ($return)
  75. {
  76. Display :: display_confirmation_message(get_lang('SurveyDeleted'), false);
  77. }
  78. else
  79. {
  80. Display :: display_error_message(get_lang('ErrorOccurred'), false);
  81. }
  82. }
  83. if(isset($_GET['action']) && $_GET['action'] == 'empty')
  84. {
  85. $return = survey_manager::empty_survey(intval($_GET['survey_id']));
  86. if ($return)
  87. {
  88. Display :: display_confirmation_message(get_lang('SurveyEmptied'), false);
  89. }
  90. else
  91. {
  92. Display :: display_error_message(get_lang('ErrorOccurred'), false);
  93. }
  94. }
  95. // Action handling: performing the same action on multiple surveys
  96. if ($_POST['action'])
  97. {
  98. if (is_array($_POST['id']))
  99. {
  100. foreach ($_POST['id'] as $key=>$value)
  101. {
  102. // getting the information of the survey (used for when the survey is shared)
  103. $survey_data = survey_manager::get_survey($value);
  104. // if the survey is shared => also delete the shared content
  105. if (is_numeric($survey_data['survey_share']))
  106. {
  107. survey_manager::delete_survey($survey_data['survey_share'], true);
  108. }
  109. // delete the actual survey
  110. survey_manager::delete_survey($value);
  111. }
  112. Display :: display_confirmation_message(get_lang('SurveysDeleted'), false);
  113. }
  114. else
  115. {
  116. Display :: display_error_message(get_lang('NoSurveysSelected'), false);
  117. }
  118. }
  119. // Action links
  120. echo '<a href="create_new_survey.php?'.api_get_cidreq().'&amp;action=add">'.get_lang('CreateNewSurvey').'</a> | ';
  121. //echo '<a href="survey_all_courses.php">'.get_lang('CreateExistingSurvey').'</a> | ';
  122. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;search=advanced">'.get_lang('Search').'</a>';
  123. // Main content
  124. display_survey_list();
  125. // Footer
  126. Display :: display_footer();
  127. /**
  128. * This function displays the form for searching a survey
  129. *
  130. * @return html code
  131. *
  132. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  133. * @version January 2007
  134. *
  135. * @todo use quickforms
  136. * @todo consider moving this to surveymanager.inc.lib.php
  137. */
  138. function display_survey_search_form()
  139. {
  140. echo '<form method="get" action="survey_list.php?search=advanced">';
  141. echo '<table>
  142. <tr>
  143. <td>'.get_lang('Title').'</td>
  144. <td><input type="text" name="keyword_title"/></td>
  145. </tr>
  146. <tr>
  147. <td>'.get_lang('Code').'</td>
  148. <td><input type="text" name="keyword_code"/></td>
  149. </tr>
  150. <tr>
  151. <td>'.get_lang('Language').'</td>
  152. <td>
  153. <select name="keyword_language"><option value="%">'.get_lang('All').'</option>';
  154. $languages = api_get_languages();
  155. foreach ($languages['name'] as $index => $name)
  156. {
  157. echo '<option value="'.$languages['folder'][$index].'">'.$name.'</option>';
  158. }
  159. echo ' </select>
  160. </td>
  161. </tr>
  162. <tr>
  163. <td>&nbsp;</td>
  164. <td><input type="submit" name="do_search" value="'.get_lang('Ok').'"/></td>
  165. </tr>
  166. </table>
  167. </form>';
  168. }
  169. /**
  170. * This function displays the sortable table with all the surveys
  171. *
  172. * @return html code
  173. *
  174. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  175. * @version January 2007
  176. */
  177. function display_survey_list()
  178. {
  179. $parameters = array();
  180. if ($_GET['do_search'])
  181. {
  182. $message = get_lang('DisplaySearchResults').'<br />';
  183. $message .= '<a href="'.api_get_self().'">'.get_lang('DisplayAll').'</a>';
  184. Display::display_normal_message($message, false);
  185. }
  186. // Create a sortable table with survey-data
  187. $table = new SortableTable('surveys', 'get_number_of_surveys', 'get_survey_data',2);
  188. $table->set_additional_parameters($parameters);
  189. $table->set_header(0, '', false);
  190. $table->set_header(1, get_lang('SurveyName'));
  191. $table->set_header(2, get_lang('SurveyCode'));
  192. $table->set_header(3, get_lang('NumberOfQuestions'));
  193. $table->set_header(4, get_lang('Author'));
  194. $table->set_header(5, get_lang('Language'));
  195. $table->set_header(6, get_lang('Shared'));
  196. $table->set_header(7, get_lang('AvailableFrom'));
  197. $table->set_header(8, get_lang('AvailableUntill'));
  198. $table->set_header(9, get_lang('Invite'));
  199. $table->set_header(10, get_lang('Anonymous'));
  200. $table->set_header(11, get_lang('Modify'), false);
  201. $table->set_column_filter(10, 'anonymous_filter');
  202. $table->set_column_filter(11, 'modify_filter');
  203. $table->set_form_actions(array ('delete' => get_lang('DeleteSurvey')));
  204. $table->display();
  205. }
  206. /**
  207. * This function calculates the total number of surveys
  208. *
  209. * @return integer
  210. *
  211. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  212. * @version January 2007
  213. */
  214. function get_number_of_surveys()
  215. {
  216. global $table_survey;
  217. $search_restriction = survey_search_restriction();
  218. if ($search_restriction)
  219. {
  220. $search_restriction = 'WHERE '.$search_restriction;
  221. }
  222. $sql = "SELECT count(survey_id) AS total_number_of_items FROM ".$table_survey.' '.$search_restriction;
  223. $res = api_sql_query($sql, __FILE__, __LINE__);
  224. $obj = mysql_fetch_object($res);
  225. return $obj->total_number_of_items;
  226. }
  227. /**
  228. * This function gets all the survey data that is to be displayed in the sortable table
  229. *
  230. * @param unknown_type $from
  231. * @param unknown_type $number_of_items
  232. * @param unknown_type $column
  233. * @param unknown_type $direction
  234. * @return unknown
  235. *
  236. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  237. * @version January 2007
  238. */
  239. function get_survey_data($from, $number_of_items, $column, $direction)
  240. {
  241. global $table_survey;
  242. global $table_user;
  243. global $table_survey_question;
  244. // searching
  245. $search_restriction = survey_search_restriction();
  246. if ($search_restriction)
  247. {
  248. $search_restriction = ' AND '.$search_restriction;
  249. }
  250. $sql = "SELECT
  251. survey.survey_id AS col0,
  252. CONCAT('<a href=\"survey.php?survey_id=',survey.survey_id,'\">',survey.title,'</a>') AS col1,
  253. survey.code AS col2,
  254. count(survey_question.question_id) AS col3,
  255. CONCAT(user.firstname, ' ', user.lastname) AS col4,
  256. survey.lang AS col5,
  257. IF(is_shared<>0,'V','-') AS col6,
  258. survey.avail_from AS col7,
  259. survey.avail_till AS col8,
  260. CONCAT('<a href=\"survey_invitation.php?view=answered&amp;survey_id=',survey.survey_id,'\">',survey.answered,'</a> / <a href=\"survey_invitation.php?view=invited&amp;survey_id=',survey.survey_id,'\">',survey.invited, '</a>') AS col9,
  261. survey.anonymous AS col10,
  262. survey.survey_id AS col11
  263. FROM $table_survey survey
  264. LEFT JOIN $table_survey_question survey_question ON survey.survey_id = survey_question.survey_id
  265. , $table_user user
  266. WHERE survey.author = user.user_id
  267. $search_restriction
  268. ";
  269. $sql .= " GROUP BY survey.survey_id";
  270. $sql .= " ORDER BY col$column $direction ";
  271. $sql .= " LIMIT $from,$number_of_items";
  272. $res = api_sql_query($sql, __FILE__, __LINE__);
  273. $surveys = array ();
  274. while ($survey = mysql_fetch_row($res))
  275. {
  276. $surveys[] = $survey;
  277. }
  278. return $surveys;
  279. }
  280. /**
  281. * This function changes the modify column of the sortable table
  282. *
  283. * @param integer $survey_id the id of the survey
  284. * @return html code that are the actions that can be performed on any survey
  285. *
  286. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  287. * @version January 2007
  288. */
  289. function modify_filter($survey_id)
  290. {
  291. global $charset;
  292. $return = '<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')).'</a>';
  293. $return .= '<a href="survey_list.php?'.api_get_cidreq().'&amp;action=delete&amp;survey_id='.$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>';
  294. //$return .= '<a href="create_survey_in_another_language.php?id_survey='.$survey_id.'">'.Display::return_icon('copy.gif', get_lang('Copy')).'</a>';
  295. //$return .= '<a href="survey.php?survey_id='.$survey_id.'">'.Display::return_icon('add.gif', get_lang('Add')).'</a>';
  296. $return .= '<a href="preview.php?'.api_get_cidreq().'&amp;survey_id='.$survey_id.'">'.Display::return_icon('preview.gif', get_lang('Preview')).'</a>';
  297. $return .= '<a href="survey_invite.php?'.api_get_cidreq().'&amp;survey_id='.$survey_id.'">'.Display::return_icon('survey_publish.gif', get_lang('Publish')).'</a>';
  298. $return .= '<a href="survey_list.php?'.api_get_cidreq().'&amp;action=empty&amp;survey_id='.$survey_id.'" onclick="javascript:if(!confirm(\''.addslashes(htmlentities(get_lang("EmptySurvey").'?')).'\')) return false;">'.Display::return_icon('empty.gif', get_lang('EmptySurvey')).'</a>';
  299. $return .= '<a href="reporting.php?'.api_get_cidreq().'&amp;survey_id='.$survey_id.'">'.Display::return_icon('statistics.gif', get_lang('Reporting')).'</a>';
  300. return $return;
  301. }
  302. function anonymous_filter($anonymous)
  303. {
  304. if ($anonymous == 1)
  305. {
  306. return get_lang('Yes');
  307. }
  308. else
  309. {
  310. return get_lang('No');
  311. }
  312. }
  313. /**
  314. * this function handles the search restriction for the SQL statements
  315. *
  316. * @return false or a part of a SQL statement
  317. *
  318. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  319. * @version January 2007
  320. */
  321. function survey_search_restriction()
  322. {
  323. if ($_GET['do_search'])
  324. {
  325. if ($_GET['keyword_title']<>'')
  326. {
  327. $search_term[] = 'title =\''.Database::escape_string($_GET['keyword_title']).'\'';
  328. }
  329. if ($_GET['keyword_code']<>'')
  330. {
  331. $search_term[] = 'code =\''.Database::escape_string($_GET['keyword_code']).'\'';
  332. }
  333. if ($_GET['keyword_language']<>'%')
  334. {
  335. $search_term[] = 'lang =\''.Database::escape_string($_GET['keyword_language']).'\'';
  336. }
  337. $search_restriction = implode(' AND ', $search_term);
  338. return $search_restriction;
  339. }
  340. else
  341. {
  342. return false;
  343. }
  344. }
  345. ?>