, Ghent University: cleanup, * refactoring and rewriting large parts (if not all) of the code * @author Julio Montoya Armas , Chamilo: Personality * Test modification and rewriting large parts of the code * * @version $Id: create_new_survey.php 22297 2009-07-22 22:08:30Z cfasanando $ * * @todo only the available platform languages should be used => need an * api get_languages and and api_get_available_languages (or a parameter) */ require_once __DIR__.'/../inc/global.inc.php'; $_course = api_get_course_info(); $this_section = SECTION_COURSES; $allowSurveyAvailabilityDatetime = api_get_configuration_value('allow_survey_availability_datetime'); $table_gradebook_link = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK); /** @todo this has to be moved to a more appropriate place (after the display_header of the code) */ // If user is not teacher or if he's a coach trying to access an element out of his session if (!api_is_allowed_to_edit()) { if (!api_is_session_general_coach() || (!empty($_GET['survey_id']) && !api_is_element_in_the_session(TOOL_SURVEY, $_GET['survey_id'])) ) { api_not_allowed(true); } } // Getting the survey information $survey_id = isset($_GET['survey_id']) ? (int) $_GET['survey_id'] : null; $action = isset($_GET['action']) ? Security::remove_XSS($_GET['action']) : ''; $survey_data = SurveyManager::get_survey($survey_id); // Additional information $course_id = api_get_course_id(); $session_id = api_get_session_id(); $gradebook_link_type = 8; $urlname = isset($survey_data['title']) ? strip_tags($survey_data['title']) : null; // Breadcrumbs if ($action == 'add') { $interbreadcrumb[] = [ 'url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php?'.api_get_cidreq(), 'name' => get_lang('Survey list'), ]; $tool_name = get_lang('Create survey'); } if ($action == 'edit' && is_numeric($survey_id)) { $interbreadcrumb[] = [ 'url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php?'.api_get_cidreq(), 'name' => get_lang('Survey list'), ]; $interbreadcrumb[] = [ 'url' => api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$survey_id.'&'.api_get_cidreq(), 'name' => Security::remove_XSS($urlname), ]; $tool_name = get_lang('Edit survey'); } $gradebook_link_id = null; // Getting the default values if ($action == 'edit' && isset($survey_id) && is_numeric($survey_id)) { $defaults = $survey_data; $defaults['survey_id'] = $survey_id; $defaults['anonymous'] = $survey_data['anonymous']; if ($allowSurveyAvailabilityDatetime) { $defaults['avail_from'] = api_get_local_time($defaults['avail_from'], null, 'UTC'); $defaults['avail_till'] = api_get_local_time($defaults['avail_till'], null, 'UTC'); $defaults['start_date'] = $defaults['avail_from']; $defaults['end_date'] = $defaults['avail_till']; } $link_info = GradebookUtils::isResourceInCourseGradebook( $course_id, $gradebook_link_type, $survey_id, $session_id ); $gradebook_link_id = $link_info['id']; if ($link_info) { $defaults['category_id'] = $link_info['category_id']; $gradebook_link_id = (int) $gradebook_link_id; $sql = "SELECT weight FROM $table_gradebook_link WHERE id = $gradebook_link_id"; $result = Database::query($sql); $gradeBookData = Database::fetch_array($result); if ($gradeBookData) { $defaults['survey_qualify_gradebook'] = $gradebook_link_id; $defaults['survey_weight'] = number_format($gradeBookData['weight'], 2, '.', ''); } } } else { $defaults['survey_language'] = $_course['language']; $defaults['start_date'] = date( $allowSurveyAvailabilityDatetime ? 'Y-m-d 00:00:00' : 'Y-m-d', api_strtotime(api_get_local_time()) ); $startdateandxdays = time() + 864000; // today + 10 days $defaults['end_date'] = date( $allowSurveyAvailabilityDatetime ? 'Y-m-d 23:59:59' : 'Y-m-d', $startdateandxdays ); $defaults['anonymous'] = 0; } // Initialize the object $form = new FormValidator( 'survey', 'post', api_get_self().'?action='.$action.'&survey_id='.$survey_id.'&'.api_get_cidreq() ); $form->addElement('header', $tool_name); // Setting the form elements if ($action == 'edit' && isset($survey_id) && is_numeric($survey_id)) { $form->addElement('hidden', 'survey_id'); } $survey_code = $form->addElement( 'text', 'survey_code', get_lang('Code'), ['size' => '20', 'maxlength' => '20', 'autofocus' => 'autofocus'] ); if ($action == 'edit') { $survey_code->freeze(); $form->applyFilter('survey_code', 'api_strtoupper'); } $form->addElement( 'html_editor', 'survey_title', get_lang('Survey title'), null, ['ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '200'] ); $form->addElement( 'html_editor', 'survey_subtitle', get_lang('Survey subtitle'), null, [ 'ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '100', 'ToolbarStartExpanded' => false, ] ); // Pass the language of the survey in the form $form->addElement('hidden', 'survey_language'); $allowSurveyAvailabilityDatetime = api_get_configuration_value('allow_survey_availability_datetime'); if ($allowSurveyAvailabilityDatetime) { $startDateElement = $form->addDateTimePicker('start_date', get_lang('Start Date')); $endDateElement = $form->addDateTimePicker('end_date', get_lang('End Date')); $form->addRule('start_date', get_lang('Invalid date'), 'datetime'); $form->addRule('end_date', get_lang('Invalid date'), 'datetime'); } else { $startDateElement = $form->addElement('date_picker', 'start_date', get_lang('Start Date')); $endDateElement = $form->addElement('date_picker', 'end_date', get_lang('End Date')); $form->addRule('start_date', get_lang('Invalid date'), 'date'); $form->addRule('end_date', get_lang('Invalid date'), 'date'); } $form->setRequired($startDateElement); $form->setRequired($endDateElement); $form->addElement('checkbox', 'anonymous', null, get_lang('Anonymous')); $visibleResults = [ SURVEY_VISIBLE_TUTOR => get_lang('Coach'), SURVEY_VISIBLE_TUTOR_STUDENT => get_lang('Coach and student'), SURVEY_VISIBLE_PUBLIC => get_lang('Everyone'), ]; if (api_get_configuration_value('hide_survey_reporting_button')) { $form->addLabel(get_lang('Results visibility'), get_lang('Feature disabled by administrator')); } else { $form->addElement('select', 'visible_results', get_lang('Results visibility'), $visibleResults); } //$defaults['visible_results'] = 0; $form->addElement( 'html_editor', 'survey_introduction', get_lang('Survey introduction'), null, ['ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '130', 'ToolbarStartExpanded' => false] ); $form->addElement( 'html_editor', 'survey_thanks', get_lang('Final thanks'), null, ['ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '130', 'ToolbarStartExpanded' => false] ); $extraField = new ExtraField('survey'); $extraField->addElements($form, $survey_id); // Additional Parameters $form->addButtonAdvancedSettings('advanced_params'); $form->addElement('html', '