create_new_survey.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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
  19. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University: cleanup, refactoring and rewriting large parts (if not all) of the code
  20. * @version $Id: create_new_survey.php 10680 2007-01-11 21:26:23Z pcool $
  21. *
  22. * @todo rename this file to survey.php
  23. * @todo try to understand the template stuff and implement it (if needed)
  24. * @todo check if the code is really unique (when adding or editing)
  25. * @todo only the available platform languages should be used => need an api get_languages and and api_get_available_languages (or a parameter)
  26. */
  27. // name of the language file that needs to be included
  28. $language_file = 'survey';
  29. // including the global dokeos file
  30. require_once ('../inc/global.inc.php');
  31. // including additional libraries
  32. /** @todo check if these are all needed */
  33. /** @todo check if the starting / is needed. api_get_path probably ends with an / */
  34. require_once (api_get_path(LIBRARY_PATH).'/fileManage.lib.php');
  35. require_once (api_get_path(CONFIGURATION_PATH) ."/add_course.conf.php");
  36. require_once (api_get_path(LIBRARY_PATH)."/add_course.lib.inc.php");
  37. require_once (api_get_path(LIBRARY_PATH)."/course.lib.php");
  38. require_once (api_get_path(LIBRARY_PATH)."/groupmanager.lib.php");
  39. require_once (api_get_path(LIBRARY_PATH)."/surveymanager.lib.php");
  40. require_once (api_get_path(LIBRARY_PATH)."/usermanager.lib.php");
  41. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  42. // Database table definitions
  43. /** @todo use database constants for the survey tables */
  44. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  45. $table_group = Database :: get_course_table(TABLE_SURVEY_GROUP);
  46. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  47. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  48. $table_course_survey_rel = Database :: get_main_table(TABLE_MAIN_COURSE_SURVEY);
  49. /** @todo replace this with the correct code */
  50. /*
  51. $status = surveymanager::get_status();
  52. api_protect_course_script();
  53. if($status==5)
  54. {
  55. api_protect_admin_script();
  56. }
  57. */
  58. /** @todo this has to be moved to a more appropriate place (after the display_header of the code)*/
  59. if (!api_is_allowed_to_edit())
  60. {
  61. Display :: display_header();
  62. Display :: display_error_message(get_lang('NotAllowedHere'));
  63. Display :: display_footer();
  64. exit;
  65. }
  66. // breadcrumbs
  67. if ($_GET['action'] == 'add')
  68. {
  69. $interbreadcrumb[] = array ("url" => "survey_list.php", "name" => get_lang('Survey'));
  70. $tool_name = get_lang('CreateNewSurvey');
  71. }
  72. if ($_GET['action'] == 'edit' AND is_numeric($_GET['survey_id']))
  73. {
  74. $interbreadcrumb[] = array ("url" => "survey_list.php", "name" => get_lang('Survey'));
  75. $tool_name = get_lang('EditSurvey');
  76. }
  77. // Displaying the header
  78. Display::display_header($tool_name);
  79. // Displaying the tool title
  80. //api_display_tool_title($tool_name);
  81. // initiate the object
  82. $form = new FormValidator('forumcategory', 'post', $_SERVER['PHP_SELF'].'?action='.$_GET['action'].'&survey_id='.$_GET['survey_id']);
  83. // settting the form elements
  84. if ($_GET['action'] == 'edit' AND isset($_GET['survey_id']) AND is_numeric($_GET['survey_id']))
  85. {
  86. $form->addElement('hidden', 'survey_id');
  87. }
  88. $form->addElement('text', 'survey_code', get_lang('SurveyCode'));
  89. $form->addElement('text', 'survey_title', get_lang('SurveyTitle'));
  90. $form->addElement('text', 'survey_subtitle', get_lang('SurveySubTitle'));
  91. $lang_array = api_get_languages();
  92. foreach ($lang_array['name'] as $key=>$value)
  93. {
  94. $languages[$lang_array['folder'][$key]] = $value;
  95. }
  96. $form->addElement('select', 'survey_language', get_lang('Language'), $languages);
  97. $form->addElement('datepicker', 'start_date', get_lang('StartDate'));
  98. $form->addElement('datepicker', 'end_date', get_lang('EndDate'));
  99. $group='';
  100. $group[] =& HTML_QuickForm::createElement('radio', 'survey_share',null, get_lang('Yes'),1);
  101. $group[] =& HTML_QuickForm::createElement('radio', 'survey_share',null, get_lang('No'),0);
  102. $form->addGroup($group, 'survey_share', get_lang('ShareSurvey'), '&nbsp;');
  103. $form->addElement('html_editor', 'survey_introduction', get_lang('SurveyIntroduction'));
  104. $form->addElement('html_editor', 'survey_thanks', get_lang('SurveyThanks'));
  105. $form->addElement('submit', 'submit_survey', get_lang('Ok'));
  106. // setting the rules
  107. $form->addRule('survey_code', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  108. $form->addRule('survey_title', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  109. $form->addRule('start_date', get_lang('InvalidDate'), 'date');
  110. $form->addRule('end_date', get_lang('InvalidDate'), 'date');
  111. /** @todo add a rule that checks if the end_date > start_date */
  112. // setting the default values
  113. if ($_GET['action'] == 'edit' AND isset($_GET['survey_id']) AND is_numeric($_GET['survey_id']))
  114. {
  115. $survey_data = get_survey($_GET['survey_id']);
  116. $defaults = $survey_data;
  117. $defaults['survey_id'] = $_GET['survey_id'];
  118. }
  119. else
  120. {
  121. $defaults['survey_language'] = $_course['language'];
  122. $defaults['start_date']['d'] = date('d');
  123. $defaults['start_date']['F'] = date('F');
  124. $defaults['start_date']['Y'] = date('Y');
  125. $defaults['start_date']['H'] = date('H');
  126. $defaults['start_date']['i'] = date('i');
  127. $defaults['end_date']['d'] = date('d');
  128. $defaults['end_date']['F'] = date('F');
  129. $defaults['end_date']['Y'] = date('Y');
  130. $defaults['end_date']['H'] = date('H');
  131. $defaults['end_date']['i'] = date('i');
  132. $defaults['survey_share']['survey_share'] = 0;
  133. }
  134. $form->setDefaults($defaults);
  135. // The validation or display
  136. if( $form->validate() )
  137. {
  138. $values = $form->exportValues();
  139. $return = store_survey($values);
  140. Display::display_confirmation_message($return['message']);
  141. }
  142. else
  143. {
  144. $form->display();
  145. }
  146. // Footer
  147. Display :: display_footer();
  148. /**
  149. * This function stores a survey in the database
  150. *
  151. * @param array $values
  152. * @return array $return the type of return message that has to be displayed and the message in it
  153. *
  154. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  155. * @version januari 2007
  156. *
  157. * @todo move this function to surveymanager.inc.lib.php
  158. */
  159. function store_survey($values)
  160. {
  161. global $_user;
  162. // table defnitions
  163. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  164. $table_survey_group = Database :: get_course_table(TABLE_SURVEY_GROUP);
  165. if (!$values['survey_id'] OR !is_numeric($values['survey_id']))
  166. {
  167. $sql = "INSERT INTO $table_survey (code, title, subtitle, author, lang, avail_from, avail_till, is_shared, template, intro, surveythanks, creation_date) VALUES (
  168. '".mysql_real_escape_string($values['survey_code'])."',
  169. '".mysql_real_escape_string($values['survey_title'])."',
  170. '".mysql_real_escape_string($values['survey_subtitle'])."',
  171. '".mysql_real_escape_string($_user['user_id'])."',
  172. '".mysql_real_escape_string($values['survey_language'])."',
  173. '".mysql_real_escape_string($values['start_date'])."',
  174. '".mysql_real_escape_string($values['end_date'])."',
  175. '".mysql_real_escape_string($values['survey_share']['survey_share'])."',
  176. '".mysql_real_escape_string('template')."',
  177. '".mysql_real_escape_string($values['survey_introduction'])."',
  178. '".mysql_real_escape_string($values['survey_thanks'])."',
  179. '".date()."')";
  180. $result = api_sql_query($sql, __FILE__, __LINE__);
  181. $survey_id = mysql_insert_id();
  182. $sql = "INSERT INTO $table_survey_group (group_id, survey_id, groupname, introduction) VALUES (
  183. '', '$survey_id', '".get_lang('NoGroup')."','".get_lang('ThisIsYourDefaultGroup')."')";
  184. $result = api_sql_query($sql, __FILE__, __LINE__);
  185. $return['message'] = get_lang('SurveyCreatedSuccesfully').'<br />'.get_lang('YouCanNowAddQuestionToYourSurvey').': ';
  186. $return['message'] .= '<a href="select_question_group.php?survey_id='.$survey_id.'">'.get_lang('ClickHere').'</a>';
  187. $return['type'] = 'confirmation';
  188. }
  189. else
  190. {
  191. $sql = "UPDATE $table_survey SET
  192. code = '".mysql_real_escape_string($values['survey_code'])."',
  193. title = '".mysql_real_escape_string($values['survey_title'])."',
  194. subtitle = '".mysql_real_escape_string($values['survey_subtitle'])."',
  195. author = '".mysql_real_escape_string($_user['user_id'])."',
  196. lang = '".mysql_real_escape_string($values['survey_language'])."',
  197. avail_from = '".mysql_real_escape_string($values['start_date'])."',
  198. avail_till = '".mysql_real_escape_string($values['end_date'])."',
  199. is_shared = '".mysql_real_escape_string($values['survey_share']['survey_share'])."',
  200. template = '".mysql_real_escape_string('template')."',
  201. intro = '".mysql_real_escape_string($values['survey_introduction'])."',
  202. surveythanks = '".mysql_real_escape_string($values['survey_thanks'])."'
  203. WHERE survey_id = '".mysql_real_escape_string($values['survey_id'])."'";
  204. $result = api_sql_query($sql, __FILE__, __LINE__);
  205. $return['message'] = get_lang('SurveyUpdatedSuccesfully').'<br />'.get_lang('YouCanNowAddQuestionToYourSurvey').': ';
  206. $return['message'] .= '<a href="select_question_group.php?survey_id='.$values['survey_id'].'">'.get_lang('Here').'</a>';
  207. $return['message'] .= get_lang('OrReturnToSurveyOverview').'<a href="survey_list.php">'.get_lang('Here').'</a>';
  208. $return['type'] = 'confirmation';
  209. }
  210. return $return;
  211. }
  212. /**
  213. * This function retrieves all the survey information
  214. *
  215. * @param integer $survey_id the id of the survey
  216. * @return array
  217. *
  218. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  219. * @version januari 2007
  220. *
  221. * @todo move this function to surveymanager.inc.lib.php
  222. */
  223. function get_survey($survey_id)
  224. {
  225. $tbl_survey = Database :: get_course_table(TABLE_SURVEY);
  226. $sql = "SELECT * FROM $tbl_survey WHERE survey_id='".mysql_real_escape_string($survey_id)."'";
  227. $result = api_sql_query($sql, __FILE__, __LINE__);
  228. $return = mysql_fetch_assoc($result);
  229. // we do this (temporarily) to have the array match the quickform elements immediately
  230. // idealiter the fields in the db match the quickform fields
  231. $return['survey_code'] = $return['code'];
  232. $return['survey_title'] = $return['title'];
  233. $return['survey_subtitle'] = $return['subtitle'];
  234. $return['survey_language'] = $return['lang'];
  235. $return['start_date'] = $return['avail_from'];
  236. $return['end_date'] = $return['avail_till'];
  237. $return['survey_share'] = $return['is_shared'];
  238. $return['survey_introduction'] = $return['intro'];
  239. $return['survey_thanks'] = $return['surveythanks'];
  240. return $return;
  241. }
  242. ?>