Fix survey creation, set start/end date as required BT#15378

pull/2858/head
Julio Montoya 7 years ago
parent cacbae5444
commit 0469f700d9
  1. 38
      main/survey/create_new_survey.php
  2. 27
      main/survey/survey.lib.php

@ -36,12 +36,12 @@ if (!api_is_allowed_to_edit()) {
!api_is_element_in_the_session(TOOL_SURVEY, $_GET['survey_id']))
) {
api_not_allowed(true);
exit;
}
}
// 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
@ -51,14 +51,14 @@ $gradebook_link_type = 8;
$urlname = isset($survey_data['title']) ? strip_tags($survey_data['title']) : null;
// Breadcrumbs
if ($_GET['action'] == 'add') {
if ($action == 'add') {
$interbreadcrumb[] = [
'url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php?'.api_get_cidreq(),
'name' => get_lang('SurveyList'),
];
$tool_name = get_lang('CreateNewSurvey');
}
if ($_GET['action'] == 'edit' && is_numeric($survey_id)) {
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('SurveyList'),
@ -71,7 +71,7 @@ if ($_GET['action'] == 'edit' && is_numeric($survey_id)) {
}
$gradebook_link_id = null;
// Getting the default values
if ($_GET['action'] == 'edit' && isset($survey_id) && is_numeric($survey_id)) {
if ($action == 'edit' && isset($survey_id) && is_numeric($survey_id)) {
$defaults = $survey_data;
$defaults['survey_id'] = $survey_id;
$defaults['anonymous'] = $survey_data['anonymous'];
@ -113,8 +113,6 @@ if ($_GET['action'] == 'edit' && isset($survey_id) && is_numeric($survey_id)) {
$allowSurveyAvailabilityDatetime ? 'Y-m-d 23:59:59' : 'Y-m-d',
$startdateandxdays
);
//$defaults['survey_share']['survey_share'] = 0;
//$form_share_value = 1;
$defaults['anonymous'] = 0;
}
@ -122,13 +120,13 @@ if ($_GET['action'] == 'edit' && isset($survey_id) && is_numeric($survey_id)) {
$form = new FormValidator(
'survey',
'post',
api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&survey_id='.$survey_id.'&'.api_get_cidreq()
api_get_self().'?action='.$action.'&survey_id='.$survey_id.'&'.api_get_cidreq()
);
$form->addElement('header', $tool_name);
// Setting the form elements
if ($_GET['action'] == 'edit' && isset($survey_id) && is_numeric($survey_id)) {
if ($action == 'edit' && isset($survey_id) && is_numeric($survey_id)) {
$form->addElement('hidden', 'survey_id');
}
@ -139,7 +137,7 @@ $survey_code = $form->addElement(
['size' => '20', 'maxlength' => '20', 'autofocus' => 'autofocus']
);
if ($_GET['action'] == 'edit') {
if ($action == 'edit') {
$survey_code->freeze();
$form->applyFilter('survey_code', 'api_strtoupper');
}
@ -168,13 +166,16 @@ $form->addElement(
$form->addElement('hidden', 'survey_language');
if ($allowSurveyAvailabilityDatetime) {
$form->addDateTimePicker('start_date', get_lang('StartDate'));
$form->addDateTimePicker('end_date', get_lang('EndDate'));
$startDateElement = $form->addDateTimePicker('start_date', get_lang('StartDate'));
$endDateElement = $form->addDateTimePicker('end_date', get_lang('EndDate'));
} else {
$form->addElement('date_picker', 'start_date', get_lang('StartDate'));
$form->addElement('date_picker', 'end_date', get_lang('EndDate'));
$startDateElement = $form->addElement('date_picker', 'start_date', get_lang('StartDate'));
$endDateElement = $form->addElement('date_picker', 'end_date', get_lang('EndDate'));
}
$form->setRequired($startDateElement);
$form->setRequired($endDateElement);
$form->addElement('checkbox', 'anonymous', null, get_lang('Anonymous'));
$visibleResults = [
SURVEY_VISIBLE_TUTOR => get_lang('Coach'),
@ -234,7 +235,7 @@ if (Gradebook::is_active()) {
// Loading Gradebook select
GradebookUtils::load_gradebook_select_in_tool($form);
if ($_GET['action'] == 'edit') {
if ($action == 'edit') {
$element = $form->getElement('category_id');
$element->freeze();
}
@ -245,7 +246,7 @@ if (Gradebook::is_active()) {
$surveytypes[0] = get_lang('Normal');
$surveytypes[1] = get_lang('Conditional');
if ($_GET['action'] == 'add') {
if ($action == 'add') {
$form->addElement('hidden', 'survey_type', 0);
$survey_tree = new SurveyTree();
$list_surveys = $survey_tree->createList($survey_tree->surveylist);
@ -259,7 +260,7 @@ $form->addElement('checkbox', 'shuffle', null, get_lang('ActivateShuffle'));
$input_name_list = null;
if (isset($_GET['action']) && $_GET['action'] == 'edit' && !empty($survey_id)) {
if ($action == 'edit' && !empty($survey_id)) {
if ($survey_data['anonymous'] == 0) {
$form->addElement(
'checkbox',
@ -308,14 +309,14 @@ $skillList = Skill::addSkillsToForm($form, ITEM_TYPE_SURVEY, $survey_id);
$form->addElement('html', '</div><br />');
if (isset($_GET['survey_id']) && $_GET['action'] == 'edit') {
if (isset($_GET['survey_id']) && $action == 'edit') {
$form->addButtonUpdate(get_lang('ModifySurvey'), 'submit_survey');
} else {
$form->addButtonCreate(get_lang('CreateSurvey'), 'submit_survey');
}
// Setting the rules
if ($_GET['action'] == 'add') {
if ($action == 'add') {
$form->addRule('survey_code', get_lang('ThisFieldIsRequired'), 'required');
$form->addRule('survey_code', '', 'maxlength', 20);
}
@ -336,6 +337,7 @@ $form->setDefaults($defaults);
// The validation or display
if ($form->validate()) {
// Exporting the values
$values = $form->getSubmitValues();
// Storing the survey

@ -232,8 +232,8 @@ class SurveyManager
$sql = 'SELECT 1 FROM '.$table_survey.'
WHERE
c_id = '.$course_id.' AND
code="'.Database::escape_string($values['survey_code']).'" AND
lang="'.Database::escape_string($values['survey_language']).'"';
code = "'.Database::escape_string($values['survey_code']).'" AND
lang = "'.Database::escape_string($values['survey_language']).'"';
$rs = Database::query($sql);
if (Database::num_rows($rs) > 0) {
Display::addFlash(
@ -341,12 +341,6 @@ class SurveyManager
'subtitle' => $values['survey_subtitle'],
'author' => $_user['user_id'],
'lang' => $values['survey_language'],
'avail_from' => $allowSurveyAvailabilityDatetime
? api_get_utc_datetime($values['start_date'].':00')
: $values['start_date'],
'avail_till' => $allowSurveyAvailabilityDatetime
? api_get_utc_datetime($values['end_date'].':59')
: $values['end_date'],
'is_shared' => $shared_survey_id,
'template' => 'template',
'intro' => $values['survey_introduction'],
@ -357,11 +351,28 @@ class SurveyManager
'visible_results' => $values['visible_results'],
];
if (!empty($values['start_date'])) {
if ($allowSurveyAvailabilityDatetime) {
$params['avail_from'] = api_get_utc_datetime($values['start_date'].':00');
} else {
$params['avail_from'] = $values['start_date'];
}
}
if (!empty($values['end_date'])) {
if ($allowSurveyAvailabilityDatetime) {
$params['avail_till'] = api_get_utc_datetime($values['end_date'].':00');
} else {
$params['avail_till'] = $values['end_date'];
}
}
if (isset($values['survey_type']) && !empty($values['survey_type'])) {
$params['survey_type'] = $values['survey_type'];
}
$params = array_merge($params, $extraParams);
$survey_id = Database::insert($table_survey, $params);
if ($survey_id > 0) {
$sql = "UPDATE $table_survey SET survey_id = $survey_id

Loading…
Cancel
Save