create_new_survey.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.survey
  5. *
  6. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University: cleanup,
  7. * refactoring and rewriting large parts (if not all) of the code
  8. * @author Julio Montoya Armas <gugli100@gmail.com>, Chamilo: Personality
  9. * Test modification and rewriting large parts of the code
  10. *
  11. * @version $Id: create_new_survey.php 22297 2009-07-22 22:08:30Z cfasanando $
  12. *
  13. * @todo only the available platform languages should be used => need an
  14. * api get_languages and and api_get_available_languages (or a parameter)
  15. */
  16. require_once __DIR__.'/../inc/global.inc.php';
  17. $_course = api_get_course_info();
  18. $this_section = SECTION_COURSES;
  19. $allowSurveyAvailabilityDatetime = api_get_configuration_value('allow_survey_availability_datetime');
  20. // Database table definitions
  21. $table_survey = Database::get_course_table(TABLE_SURVEY);
  22. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  23. $table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  24. $table_gradebook_link = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  25. /** @todo this has to be moved to a more appropriate place (after the display_header of the code) */
  26. // If user is not teacher or if he's a coach trying to access an element out of his session
  27. if (!api_is_allowed_to_edit()) {
  28. if (!api_is_session_general_coach() ||
  29. (!empty($_GET['survey_id']) &&
  30. !api_is_element_in_the_session(TOOL_SURVEY, $_GET['survey_id']))
  31. ) {
  32. api_not_allowed(true);
  33. }
  34. }
  35. // Getting the survey information
  36. $survey_id = isset($_GET['survey_id']) ? (int) $_GET['survey_id'] : null;
  37. $action = isset($_GET['action']) ? Security::remove_XSS($_GET['action']) : '';
  38. $survey_data = SurveyManager::get_survey($survey_id);
  39. // Additional information
  40. $course_id = api_get_course_id();
  41. $session_id = api_get_session_id();
  42. $gradebook_link_type = 8;
  43. $urlname = isset($survey_data['title']) ? strip_tags($survey_data['title']) : null;
  44. // Breadcrumbs
  45. if ($action == 'add') {
  46. $interbreadcrumb[] = [
  47. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php?'.api_get_cidreq(),
  48. 'name' => get_lang('SurveyList'),
  49. ];
  50. $tool_name = get_lang('CreateNewSurvey');
  51. }
  52. if ($action == 'edit' && is_numeric($survey_id)) {
  53. $interbreadcrumb[] = [
  54. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php?'.api_get_cidreq(),
  55. 'name' => get_lang('SurveyList'),
  56. ];
  57. $interbreadcrumb[] = [
  58. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$survey_id.'&'.api_get_cidreq(),
  59. 'name' => Security::remove_XSS($urlname),
  60. ];
  61. $tool_name = get_lang('EditSurvey');
  62. }
  63. $gradebook_link_id = null;
  64. // Getting the default values
  65. if ($action == 'edit' && isset($survey_id) && is_numeric($survey_id)) {
  66. $defaults = $survey_data;
  67. $defaults['survey_id'] = $survey_id;
  68. $defaults['anonymous'] = $survey_data['anonymous'];
  69. if ($allowSurveyAvailabilityDatetime) {
  70. $defaults['avail_from'] = api_get_local_time($defaults['avail_from'], null, 'UTC');
  71. $defaults['avail_till'] = api_get_local_time($defaults['avail_till'], null, 'UTC');
  72. $defaults['start_date'] = $defaults['avail_from'];
  73. $defaults['end_date'] = $defaults['avail_till'];
  74. }
  75. $link_info = GradebookUtils::isResourceInCourseGradebook(
  76. $course_id,
  77. $gradebook_link_type,
  78. $survey_id,
  79. $session_id
  80. );
  81. $gradebook_link_id = $link_info['id'];
  82. if ($link_info) {
  83. $defaults['category_id'] = $link_info['category_id'];
  84. $gradebook_link_id = (int) $gradebook_link_id;
  85. $sql = "SELECT weight FROM $table_gradebook_link WHERE id = $gradebook_link_id";
  86. $result = Database::query($sql);
  87. $gradeBookData = Database::fetch_array($result);
  88. if ($gradeBookData) {
  89. $defaults['survey_qualify_gradebook'] = $gradebook_link_id;
  90. $defaults['survey_weight'] = number_format($gradeBookData['weight'], 2, '.', '');
  91. }
  92. }
  93. } else {
  94. $defaults['survey_language'] = $_course['language'];
  95. $defaults['start_date'] = date(
  96. $allowSurveyAvailabilityDatetime ? 'Y-m-d 00:00:00' : 'Y-m-d',
  97. api_strtotime(api_get_local_time())
  98. );
  99. $startdateandxdays = time() + 864000; // today + 10 days
  100. $defaults['end_date'] = date(
  101. $allowSurveyAvailabilityDatetime ? 'Y-m-d 23:59:59' : 'Y-m-d',
  102. $startdateandxdays
  103. );
  104. $defaults['anonymous'] = 0;
  105. }
  106. // Initialize the object
  107. $form = new FormValidator(
  108. 'survey',
  109. 'post',
  110. api_get_self().'?action='.$action.'&survey_id='.$survey_id.'&'.api_get_cidreq()
  111. );
  112. $form->addElement('header', $tool_name);
  113. // Setting the form elements
  114. if ($action == 'edit' && isset($survey_id) && is_numeric($survey_id)) {
  115. $form->addElement('hidden', 'survey_id');
  116. }
  117. $survey_code = $form->addElement(
  118. 'text',
  119. 'survey_code',
  120. get_lang('SurveyCode'),
  121. ['size' => '20', 'maxlength' => '20', 'autofocus' => 'autofocus']
  122. );
  123. if ($action == 'edit') {
  124. $survey_code->freeze();
  125. $form->applyFilter('survey_code', 'api_strtoupper');
  126. }
  127. $form->addElement(
  128. 'html_editor',
  129. 'survey_title',
  130. get_lang('SurveyTitle'),
  131. null,
  132. ['ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '200']
  133. );
  134. $form->addElement(
  135. 'html_editor',
  136. 'survey_subtitle',
  137. get_lang('SurveySubTitle'),
  138. null,
  139. [
  140. 'ToolbarSet' => 'Survey',
  141. 'Width' => '100%',
  142. 'Height' => '100',
  143. 'ToolbarStartExpanded' => false,
  144. ]
  145. );
  146. // Pass the language of the survey in the form
  147. $form->addElement('hidden', 'survey_language');
  148. $allowSurveyAvailabilityDatetime = api_get_configuration_value('allow_survey_availability_datetime');
  149. if ($allowSurveyAvailabilityDatetime) {
  150. $startDateElement = $form->addDateTimePicker('start_date', get_lang('StartDate'));
  151. $endDateElement = $form->addDateTimePicker('end_date', get_lang('EndDate'));
  152. $form->addRule('start_date', get_lang('InvalidDate'), 'datetime');
  153. $form->addRule('end_date', get_lang('InvalidDate'), 'datetime');
  154. } else {
  155. $startDateElement = $form->addElement('date_picker', 'start_date', get_lang('StartDate'));
  156. $endDateElement = $form->addElement('date_picker', 'end_date', get_lang('EndDate'));
  157. $form->addRule('start_date', get_lang('InvalidDate'), 'date');
  158. $form->addRule('end_date', get_lang('InvalidDate'), 'date');
  159. }
  160. $form->setRequired($startDateElement);
  161. $form->setRequired($endDateElement);
  162. $form->addElement('checkbox', 'anonymous', null, get_lang('Anonymous'));
  163. $visibleResults = [
  164. SURVEY_VISIBLE_TUTOR => get_lang('Coach'),
  165. SURVEY_VISIBLE_TUTOR_STUDENT => get_lang('CoachAndStudent'),
  166. SURVEY_VISIBLE_PUBLIC => get_lang('Everyone'),
  167. ];
  168. if (api_get_configuration_value('hide_survey_reporting_button')) {
  169. $form->addLabel(get_lang('ResultsVisibility'), get_lang('FeatureDisabledByAdministrator'));
  170. } else {
  171. $form->addElement('select', 'visible_results', get_lang('ResultsVisibility'), $visibleResults);
  172. }
  173. //$defaults['visible_results'] = 0;
  174. $form->addElement(
  175. 'html_editor',
  176. 'survey_introduction',
  177. get_lang('SurveyIntroduction'),
  178. null,
  179. ['ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '130', 'ToolbarStartExpanded' => false]
  180. );
  181. $form->addElement(
  182. 'html_editor',
  183. 'survey_thanks',
  184. get_lang('SurveyThanks'),
  185. null,
  186. ['ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '130', 'ToolbarStartExpanded' => false]
  187. );
  188. $extraField = new ExtraField('survey');
  189. $extraField->addElements($form, $survey_id);
  190. // Additional Parameters
  191. $form->addButtonAdvancedSettings('advanced_params');
  192. $form->addElement('html', '<div id="advanced_params_options" style="display:none">');
  193. if (Gradebook::is_active()) {
  194. // An option: Qualify the fact that survey has been answered in the gradebook
  195. $form->addElement(
  196. 'checkbox',
  197. 'survey_qualify_gradebook',
  198. null,
  199. get_lang('QualifyInGradebook'),
  200. 'onclick="javascript: if (this.checked) { document.getElementById(\'gradebook_options\').style.display = \'block\'; } else { document.getElementById(\'gradebook_options\').style.display = \'none\'; }"'
  201. );
  202. $form->addElement(
  203. 'html',
  204. '<div id="gradebook_options"'.($gradebook_link_id ? '' : ' style="display:none"').'>'
  205. );
  206. $form->addElement(
  207. 'text',
  208. 'survey_weight',
  209. get_lang('QualifyWeight'),
  210. 'value="0.00" style="width: 40px;" onfocus="javascript: this.select();"'
  211. );
  212. $form->applyFilter('survey_weight', 'html_filter');
  213. // Loading Gradebook select
  214. GradebookUtils::load_gradebook_select_in_tool($form);
  215. if ($action == 'edit') {
  216. $element = $form->getElement('category_id');
  217. $element->freeze();
  218. }
  219. $form->addElement('html', '</div>');
  220. }
  221. // Personality/Conditional Test Options
  222. $surveytypes[0] = get_lang('Normal');
  223. $surveytypes[1] = get_lang('Conditional');
  224. if ($action == 'add') {
  225. $form->addElement('hidden', 'survey_type', 0);
  226. $survey_tree = new SurveyTree();
  227. $list_surveys = $survey_tree->createList($survey_tree->surveylist);
  228. $list_surveys[0] = '';
  229. $form->addElement('select', 'parent_id', get_lang('ParentSurvey'), $list_surveys);
  230. $defaults['parent_id'] = 0;
  231. }
  232. $form->addElement('checkbox', 'one_question_per_page', null, get_lang('OneQuestionPerPage'));
  233. $form->addElement('checkbox', 'shuffle', null, get_lang('ActivateShuffle'));
  234. $input_name_list = null;
  235. if ($action == 'edit' && !empty($survey_id)) {
  236. if ($survey_data['anonymous'] == 0) {
  237. $form->addElement(
  238. 'checkbox',
  239. 'show_form_profile',
  240. null,
  241. get_lang('ShowFormProfile'),
  242. 'onclick="javascript: if(this.checked){document.getElementById(\'options_field\').style.display = \'block\';}else{document.getElementById(\'options_field\').style.display = \'none\';}"'
  243. );
  244. if ($survey_data['show_form_profile'] == 1) {
  245. $form->addElement('html', '<div id="options_field" style="display:block">');
  246. } else {
  247. $form->addElement('html', '<div id="options_field" style="display:none">');
  248. }
  249. $field_list = SurveyUtil::make_field_list();
  250. if (is_array($field_list)) {
  251. // TODO hide and show the list in a fancy DIV
  252. foreach ($field_list as $key => &$field) {
  253. if ($field['visibility'] == 1) {
  254. $form->addElement('checkbox', 'profile_'.$key, ' ', '&nbsp;&nbsp;'.$field['name']);
  255. $input_name_list .= 'profile_'.$key.',';
  256. }
  257. }
  258. // Necessary to know the fields
  259. $form->addElement('hidden', 'input_name_list', $input_name_list);
  260. // Set defaults form fields
  261. if ($survey_data['form_fields']) {
  262. $form_fields = explode('@', $survey_data['form_fields']);
  263. foreach ($form_fields as &$field) {
  264. $field_value = explode(':', $field);
  265. if ($field_value[0] != '' && $field_value[1] != '') {
  266. $defaults[$field_value[0]] = $field_value[1];
  267. }
  268. }
  269. }
  270. }
  271. $form->addElement('html', '</div>');
  272. }
  273. }
  274. $skillList = Skill::addSkillsToForm($form, ITEM_TYPE_SURVEY, $survey_id);
  275. $form->addElement('html', '</div><br />');
  276. if (isset($_GET['survey_id']) && $action == 'edit') {
  277. $form->addButtonUpdate(get_lang('ModifySurvey'), 'submit_survey');
  278. } else {
  279. $form->addButtonCreate(get_lang('CreateSurvey'), 'submit_survey');
  280. }
  281. // Setting the rules
  282. if ($action == 'add') {
  283. $form->addRule('survey_code', get_lang('ThisFieldIsRequired'), 'required');
  284. $form->addRule('survey_code', '', 'maxlength', 20);
  285. }
  286. $form->addRule('survey_title', get_lang('ThisFieldIsRequired'), 'required');
  287. $form->addRule('start_date', get_lang('InvalidDate'), $allowSurveyAvailabilityDatetime ? 'datetime' : 'date');
  288. $form->addRule('end_date', get_lang('InvalidDate'), $allowSurveyAvailabilityDatetime ? 'datetime' : 'date');
  289. $form->addRule(
  290. ['start_date', 'end_date'],
  291. get_lang('StartDateShouldBeBeforeEndDate'),
  292. 'date_compare',
  293. 'lte'
  294. );
  295. $defaults['skills'] = array_keys($skillList);
  296. // Setting the default values
  297. $form->setDefaults($defaults);
  298. // The validation or display
  299. if ($form->validate()) {
  300. // Exporting the values
  301. $values = $form->getSubmitValues();
  302. // Storing the survey
  303. $return = SurveyManager::store_survey($values);
  304. Skill::saveSkills($form, ITEM_TYPE_SURVEY, $return['id']);
  305. $values['item_id'] = $return['id'];
  306. $extraFieldValue = new ExtraFieldValue('survey');
  307. $extraFieldValue->saveFieldValues($values);
  308. // Redirecting to the survey page (whilst showing the return message)
  309. header('Location: '.api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$return['id'].'&'.api_get_cidreq());
  310. exit;
  311. } else {
  312. // Displaying the header
  313. Display::display_header($tool_name);
  314. $form->display();
  315. }
  316. Display::display_footer();