create_new_survey.php 13 KB

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