Skip to content

Commit c2efa24

Browse files
committed
Add allow_survey_availability_datetime conf - refs BT#14468
Allows to set the date and time of availability for surveys. Requires DB changes: ALTER TABLE c_survey CHANGE avail_from avail_from DATETIME DEFAULT NULL, CHANGE avail_till avail_till DATETIME DEFAULT NULL; Requires change the Doctrine type from date to datime in CSurvey::$availFrom and CSurvey::$availTill
1 parent bb94d6c commit c2efa24

File tree

5 files changed

+60
-13
lines changed

5 files changed

+60
-13
lines changed

main/install/configuration.dist.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,10 @@
494494
// Hide survey edition tools for all or some surveys.
495495
//Set an asterisk to hide for all, otherwise set an array with the survey codes in which the options will be blocked
496496
//$_configuration['hide_survey_edition'] = ['codes' => []];
497+
// Allows to set the date and time of availability for surveys. Requires DB changes:
498+
// ALTER TABLE c_survey CHANGE avail_from avail_from DATETIME DEFAULT NULL, CHANGE avail_till avail_till DATETIME DEFAULT NULL;
499+
// Requires change the Doctrine type from date to datime in CSurvey::$availFrom and CSurvey::$availTill
500+
//$_configuration['allow_survey_availability_datetime'] = false;
497501
// ------
498502

499503
// Allow career diagram, requires a DB change:

main/survey/create_new_survey.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
$this_section = SECTION_COURSES;
2020

21+
$allowSurveyAvailabilityDatetime = api_get_configuration_value('allow_survey_availability_datetime');
22+
2123
// Database table definitions
2224
$table_survey = Database::get_course_table(TABLE_SURVEY);
2325
$table_user = Database::get_main_table(TABLE_MAIN_USER);
@@ -93,9 +95,15 @@
9395
}
9496
} else {
9597
$defaults['survey_language'] = $_course['language'];
96-
$defaults['start_date'] = date('Y-m-d', api_strtotime(api_get_local_time()));
98+
$defaults['start_date'] = date(
99+
$allowSurveyAvailabilityDatetime ? 'Y-m-d 00:00:00' : 'Y-m-d',
100+
api_strtotime(api_get_local_time())
101+
);
97102
$startdateandxdays = time() + 864000; // today + 10 days
98-
$defaults['end_date'] = date('Y-m-d', $startdateandxdays);
103+
$defaults['end_date'] = date(
104+
$allowSurveyAvailabilityDatetime ? 'Y-m-d 23:59:59' : 'Y-m-d',
105+
$startdateandxdays
106+
);
99107
//$defaults['survey_share']['survey_share'] = 0;
100108
//$form_share_value = 1;
101109
$defaults['anonymous'] = 0;
@@ -149,8 +157,14 @@
149157

150158
// Pass the language of the survey in the form
151159
$form->addElement('hidden', 'survey_language');
152-
$form->addElement('date_picker', 'start_date', get_lang('StartDate'));
153-
$form->addElement('date_picker', 'end_date', get_lang('EndDate'));
160+
161+
if ($allowSurveyAvailabilityDatetime) {
162+
$form->addDateTimePicker('start_date', get_lang('StartDate'));
163+
$form->addDateTimePicker('end_date', get_lang('EndDate'));
164+
} else {
165+
$form->addElement('date_picker', 'start_date', get_lang('StartDate'));
166+
$form->addElement('date_picker', 'end_date', get_lang('EndDate'));
167+
}
154168

155169
$form->addElement('checkbox', 'anonymous', null, get_lang('Anonymous'));
156170
$visibleResults = [
@@ -298,8 +312,8 @@
298312
$form->addRule('survey_code', '', 'maxlength', 20);
299313
}
300314
$form->addRule('survey_title', get_lang('ThisFieldIsRequired'), 'required');
301-
$form->addRule('start_date', get_lang('InvalidDate'), 'date');
302-
$form->addRule('end_date', get_lang('InvalidDate'), 'date');
315+
$form->addRule('start_date', get_lang('InvalidDate'), $allowSurveyAvailabilityDatetime ? 'datetime': 'date');
316+
$form->addRule('end_date', get_lang('InvalidDate'), $allowSurveyAvailabilityDatetime ? 'datetime': 'date');
303317
$form->addRule(
304318
['start_date', 'end_date'],
305319
get_lang('StartDateShouldBeBeforeEndDate'),

main/survey/fillsurvey.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1364,11 +1364,14 @@
13641364
*/
13651365
function check_time_availability($surveyData)
13661366
{
1367+
$allowSurveyAvailabilityDatetime = api_get_configuration_value('allow_survey_availability_datetime');
13671368
$utcZone = new DateTimeZone('UTC');
13681369
$startDate = new DateTime($surveyData['start_date'], $utcZone);
13691370
$endDate = new DateTime($surveyData['end_date'], $utcZone);
13701371
$currentDate = new DateTime('now', $utcZone);
1371-
$currentDate->modify('today');
1372+
if (!$allowSurveyAvailabilityDatetime) {
1373+
$currentDate->modify('today');
1374+
}
13721375
if ($currentDate < $startDate) {
13731376
api_not_allowed(
13741377
true,

main/survey/survey.lib.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,15 +331,21 @@ public static function store_survey($values)
331331
}
332332
}
333333

334+
$allowSurveyAvailabilityDatetime = api_get_configuration_value('allow_survey_availability_datetime');
335+
334336
$params = [
335337
'c_id' => $course_id,
336338
'code' => self::generateSurveyCode($values['survey_code']),
337339
'title' => $values['survey_title'],
338340
'subtitle' => $values['survey_subtitle'],
339341
'author' => $_user['user_id'],
340342
'lang' => $values['survey_language'],
341-
'avail_from' => $values['start_date'],
342-
'avail_till' => $values['end_date'],
343+
'avail_from' => $allowSurveyAvailabilityDatetime
344+
? api_get_utc_datetime($values['start_date'])
345+
: $values['start_date'],
346+
'avail_till' => $allowSurveyAvailabilityDatetime
347+
? api_get_utc_datetime($values['end_date'])
348+
: $values['end_date'],
343349
'is_shared' => $shared_survey_id,
344350
'template' => 'template',
345351
'intro' => $values['survey_introduction'],

main/survey/surveyUtil.class.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3075,6 +3075,8 @@ public static function get_survey_data(
30753075
$mandatoryAllowed = api_get_configuration_value('allow_mandatory_survey');
30763076
$_user = api_get_user_info();
30773077

3078+
$allowSurveyAvailabilityDatetime = api_get_configuration_value('allow_survey_availability_datetime');
3079+
30783080
// Searching
30793081
$search_restriction = self::survey_search_restriction();
30803082
if ($search_restriction) {
@@ -3145,8 +3147,14 @@ public static function get_survey_data(
31453147
$array[2] = $survey[2].$session_img;
31463148
$array[3] = $survey[3];
31473149
$array[4] = $survey[4];
3148-
$array[5] = $survey[5];
3149-
$array[6] = $survey[6];
3150+
$array[5] = api_convert_and_format_date(
3151+
$survey[5],
3152+
$allowSurveyAvailabilityDatetime ? DATE_TIME_FORMAT_LONG : DATE_FORMAT_LONG
3153+
);
3154+
$array[6] = api_convert_and_format_date(
3155+
$survey[6],
3156+
$allowSurveyAvailabilityDatetime ? DATE_TIME_FORMAT_LONG : DATE_FORMAT_LONG
3157+
);
31503158
$array[7] =
31513159
Display::url(
31523160
$survey['answered'],
@@ -3196,6 +3204,7 @@ public static function get_survey_data(
31963204
public static function get_survey_data_for_coach($from, $number_of_items, $column, $direction)
31973205
{
31983206
$mandatoryAllowed = api_get_configuration_value('allow_mandatory_survey');
3207+
$allowSurveyAvailabilityDatetime = api_get_configuration_value('allow_survey_availability_datetime');
31993208
$survey_tree = new SurveyTree();
32003209
//$last_version_surveys = $survey_tree->get_last_children_from_branch($survey_tree->surveylist);
32013210
$last_version_surveys = $survey_tree->surveylist;
@@ -3251,6 +3260,15 @@ public static function get_survey_data_for_coach($from, $number_of_items, $colum
32513260
$res = Database::query($sql);
32523261
$surveys = [];
32533262
while ($survey = Database::fetch_array($res)) {
3263+
$survey['col5'] = api_convert_and_format_date(
3264+
$survey['col5'],
3265+
$allowSurveyAvailabilityDatetime ? DATE_TIME_FORMAT_LONG : DATE_FORMAT_LONG
3266+
);
3267+
$survey['col6'] = api_convert_and_format_date(
3268+
$survey['col6'],
3269+
$allowSurveyAvailabilityDatetime ? DATE_TIME_FORMAT_LONG : DATE_FORMAT_LONG
3270+
);
3271+
32543272
if ($mandatoryAllowed) {
32553273
$survey['col10'] = $survey['col9'];
32563274
$efvMandatory = $efv->get_values_by_handler_and_field_variable(
@@ -3281,6 +3299,7 @@ public static function getSurveyList($user_id)
32813299
$user_id = intval($user_id);
32823300
$sessionId = api_get_session_id();
32833301
$mandatoryAllowed = api_get_configuration_value('allow_mandatory_survey');
3302+
$allowSurveyAvailabilityDatetime = api_get_configuration_value('allow_survey_availability_datetime');
32843303

32853304
// Database table definitions
32863305
$table_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION);
@@ -3311,6 +3330,7 @@ public static function getSurveyList($user_id)
33113330

33123331
/** @var \DateTime $now */
33133332
$now = api_get_utc_datetime(null, false, true);
3333+
$filterDate = $allowSurveyAvailabilityDatetime ? $now->format('Y-m-d H:i') : $now->format('Y-m-d');
33143334

33153335
$sql = "SELECT *
33163336
FROM $table_survey survey
@@ -3323,8 +3343,8 @@ public static function getSurveyList($user_id)
33233343
)
33243344
WHERE
33253345
survey_invitation.user = $user_id AND
3326-
survey.avail_from <= '".$now->format('Y-m-d')."' AND
3327-
survey.avail_till >= '".$now->format('Y-m-d')."' AND
3346+
survey.avail_from <= '$filterDate' AND
3347+
survey.avail_till >= '$filterDate' AND
33283348
survey.c_id = $course_id AND
33293349
survey.session_id = $sessionId AND
33303350
survey_invitation.c_id = $course_id

0 commit comments

Comments
 (0)