Skip to content

Commit d7298ab

Browse files
committed
Fixing date range see #7162
1 parent 926e240 commit d7298ab

File tree

2 files changed

+91
-52
lines changed

2 files changed

+91
-52
lines changed

main/admin/system_announcements.php

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
$this_section = SECTION_PLATFORM_ADMIN;
2525
$_SESSION['this_section']=$this_section;
2626

27+
$action = isset($_GET['action']) ? $_GET['action'] : null;
28+
2729
// Access restrictions
2830
api_protect_admin_script(true);
2931

@@ -33,15 +35,15 @@
3335
$tool_name = null;
3436

3537
if (empty($_GET['lang'])) {
36-
$_GET['lang'] = $_SESSION['user_language_choice'];
38+
$_GET['lang'] = isset($_SESSION['user_language_choice']) ? $_SESSION['user_language_choice'] : null;
3739
}
3840

39-
if (isset($_GET['action'])) {
41+
if (!empty($action)) {
4042
$interbreadcrumb[] = array ("url" => "system_announcements.php", "name" => get_lang('SystemAnnouncements'));
41-
if ($_GET['action'] == 'add') {
43+
if ($action == 'add') {
4244
$interbreadcrumb[] = array ("url" => '#', "name" => get_lang('AddAnnouncement'));
4345
}
44-
if ($_GET['action'] == 'edit') {
46+
if ($action == 'edit') {
4547
$interbreadcrumb[] = array ("url" => '#', "name" => get_lang('Edit'));
4648
}
4749
} else {
@@ -51,7 +53,7 @@
5153
// Displaying the header.
5254
Display :: display_header($tool_name);
5355

54-
if ($_GET['action'] != 'add' && $_GET['action'] != 'edit') {
56+
if ($action != 'add' && $action != 'edit') {
5557
echo '<div class="actions">';
5658
echo '<a href="?action=add">'.Display::return_icon('add.png', get_lang('AddAnnouncement'), array(), 32).'</a>';
5759
echo '</div>';
@@ -96,6 +98,10 @@
9698
// Set default time window: NOW -> NEXT WEEK
9799
$values['start'] = date('Y-m-d H:i:s',api_strtotime(api_get_local_time()));
98100
$values['end'] = date('Y-m-d H:i:s',api_strtotime(api_get_local_time()) + (7 * 24 * 60 * 60));
101+
102+
$values['range'] =
103+
substr(api_get_local_time(api_get_local_time()), 0, 16).' / '.
104+
substr(api_get_local_time(api_get_local_time()) + (7 * 24 * 60 * 60), 0, 16);
99105
$action_todo = true;
100106
break;
101107
case 'edit':
@@ -106,6 +112,11 @@
106112
$values['content'] = $announcement->content;
107113
$values['start'] = api_get_local_time($announcement->date_start);
108114
$values['end'] = api_get_local_time($announcement->date_end);
115+
116+
$values['range'] =
117+
substr(api_get_local_time($announcement->date_start), 0, 16).' / '.
118+
substr(api_get_local_time($announcement->date_end), 0, 16);
119+
109120
$values['visible_teacher'] = $announcement->visible_teacher;
110121
$values['visible_student'] = $announcement->visible_student ;
111122
$values['visible_guest'] = $announcement->visible_guest ;
@@ -139,7 +150,8 @@
139150
} else {
140151
$form->add_html_editor('content', get_lang('Content'), true, false, array('ToolbarSet' => 'PortalNews', 'Width' => '100%', 'Height' => '300'));
141152
}
142-
$form->add_timewindow('start','end',get_lang('StartTimeWindow'),get_lang('EndTimeWindow'));
153+
$form->addDateRangePicker('range', get_lang('StartTimeWindow'), true, array('id' => 'date_range'));
154+
143155
$group = array();
144156

145157
$group[]= $form->createElement('checkbox', 'visible_teacher', null, get_lang('Teacher'));
@@ -151,8 +163,13 @@
151163
$form->addElement('hidden', 'id');
152164

153165
$group_list = GroupPortalManager::get_groups_list();
154-
$group_list[0] = get_lang('All');
155-
$form->addElement('select', 'group',get_lang('AnnouncementForGroup'),$group_list);
166+
$group_list[0] = get_lang('All');
167+
$form->addElement(
168+
'select',
169+
'group',
170+
get_lang('AnnouncementForGroup'),
171+
$group_list
172+
);
156173
$values['group'] = isset($values['group']) ? $values['group'] : '0';
157174

158175
$form->addElement('checkbox', 'send_mail', null, get_lang('SendMail'));
@@ -161,21 +178,22 @@
161178
$form->addElement('checkbox', 'add_to_calendar', null, get_lang('AddToCalendar'));
162179
$text=get_lang('AddNews');
163180
$class='add';
164-
$form->addElement('hidden', 'action','add');
181+
$form->addElement('hidden', 'action', 'add');
165182

166-
} elseif (isset($_REQUEST['action']) && $_REQUEST['action']=='edit') {
183+
} elseif (isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit') {
167184
$text=get_lang('EditNews');
168185
$class='save';
169-
$form->addElement('hidden', 'action','edit');
186+
$form->addElement('hidden', 'action', 'edit');
170187
}
171188

172189
$form->addElement('checkbox', 'send_email_test', null, get_lang('SendOnlyAnEmailToMySelfToTest'));
173190

174-
$form->addElement('style_submit_button', 'submit', $text,'class="'.$class.'"');
191+
$form->addElement('style_submit_button', 'submit', $text, 'class="'.$class.'"');
175192
if (api_get_setting('wcag_anysurfer_public_pages') == 'true') {
176193
$values['content'] = WCAG_Rendering::HTML_to_text($values['content']);
177194
}
178195
$form->setDefaults($values);
196+
179197
if ($form->validate()) {
180198
$values = $form->exportValues();
181199
if ( !isset($values['visible_teacher'])) {
@@ -193,18 +211,21 @@
193211
if (api_get_setting('wcag_anysurfer_public_pages') == 'true') {
194212
//$values['content'] = WCAG_Rendering::text_to_HTML($values['content']);
195213
}
214+
215+
$sendMail = isset($values['send_mail']) ? $values['send_mail'] : null;
216+
196217
switch ($values['action']) {
197218
case 'add':
198219
$announcement_id = SystemAnnouncementManager::add_announcement(
199220
$values['title'],
200221
$values['content'],
201-
$values['start'],
202-
$values['end'],
222+
$values['range_start'],
223+
$values['range_end'],
203224
$values['visible_teacher'],
204225
$values['visible_student'],
205226
$values['visible_guest'],
206227
$values['lang'],
207-
$values['send_mail'],
228+
$sendMail,
208229
$values['add_to_calendar'],
209230
$values['send_email_test']
210231
);
@@ -222,13 +243,13 @@
222243
$values['id'],
223244
$values['title'],
224245
$values['content'],
225-
$values['start'],
226-
$values['end'],
246+
$values['range_start'],
247+
$values['range_end'],
227248
$values['visible_teacher'],
228249
$values['visible_student'],
229250
$values['visible_guest'],
230251
$values['lang'],
231-
$values['send_mail'],
252+
$sendMail,
232253
$values['send_email_test']
233254
)
234255
) {

main/inc/lib/formvalidator/Element/DateRangePicker.php

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,41 @@
11
<?php
22
/* For licensing terms, see /license.txt */
3+
34
require_once 'HTML/QuickForm/date.php';
45

56
/**
67
* Form element to select a date and hour (with popup datepicker)
78
*/
89
class DateRangePicker extends HTML_QuickForm_text
910
{
10-
/**
11-
* Constructor
12-
*/
13-
public function DateRangePicker($elementName = null, $elementLabel = null, $attributes = null)
14-
{
11+
/**
12+
* Constructor
13+
*/
14+
public function DateRangePicker($elementName = null, $elementLabel = null, $attributes = null)
15+
{
1516
if (!isset($attributes['id'])) {
1617
$attributes['id'] = $elementName;
1718
}
1819
$attributes['class'] = 'span3';
19-
HTML_QuickForm_element::HTML_QuickForm_element($elementName, $elementLabel, $attributes);
20-
$this->_appendName = true;
21-
$this->_type = 'date_range_picker';
22-
}
23-
24-
/**
25-
* HTML code to display this datepicker
26-
*/
27-
public function toHtml()
28-
{
29-
$js = $this->getElementJS();
30-
return $js.parent::toHtml();
31-
}
32-
33-
function setValue($value)
20+
HTML_QuickForm_element::HTML_QuickForm_element($elementName, $elementLabel, $attributes);
21+
$this->_appendName = true;
22+
$this->_type = 'date_range_picker';
23+
}
24+
25+
/**
26+
* HTML code to display this datepicker
27+
*/
28+
public function toHtml()
29+
{
30+
$js = $this->getElementJS();
31+
32+
return $js.parent::toHtml();
33+
}
34+
35+
/**
36+
* @param string $value
37+
*/
38+
public function setValue($value)
3439
{
3540
$this->updateAttributes(
3641
array(
@@ -39,14 +44,25 @@ function setValue($value)
3944
);
4045
}
4146

42-
/**
43-
* Get the necessary javascript for this datepicker
44-
*/
45-
private function getElementJS()
46-
{
47+
/**
48+
* Get the necessary javascript for this datepicker
49+
* @return string
50+
*/
51+
private function getElementJS()
52+
{
4753
$js = null;
48-
4954
$id = $this->getAttribute('id');
55+
56+
$dateRange = $this->getAttribute('value');
57+
58+
$defaultDates = null;
59+
if (!empty($dateRange)) {
60+
$dates = $this->parseDateRange($dateRange);
61+
$defaultDates = "
62+
startDate: '".$dates['start']."',
63+
endDate: '".$dates['end']."', ";
64+
}
65+
5066
//timeFormat: 'hh:mm'
5167
$js .= "<script>
5268
$(function() {
@@ -55,6 +71,7 @@ private function getElementJS()
5571
timePicker: true,
5672
timePickerIncrement: 30,
5773
timePicker12Hour: false,
74+
$defaultDates
5875
ranges: {
5976
'".addslashes(get_lang('Today'))."': [moment(), moment()],
6077
'".addslashes(get_lang('ThisWeek'))."': [moment().weekday(1), moment().weekday(5)],
@@ -73,15 +90,15 @@ private function getElementJS()
7390
});
7491
</script>";
7592

76-
return $js;
77-
}
93+
return $js;
94+
}
7895

7996
/**
8097
* @param array $dateRange
81-
98+
*
8299
* @return array
83100
*/
84-
function parseDateRange($dateRange)
101+
public function parseDateRange($dateRange)
85102
{
86103
$dates = explode('/', $dateRange);
87104
$dates = array_map('trim', $dates);
@@ -93,10 +110,11 @@ function parseDateRange($dateRange)
93110
}
94111

95112
/**
96-
* @param array $dates result of parseDateRange()
97-
* @return bool
98-
*/
99-
function validateDates($dates)
113+
* @param array $dates result of parseDateRange()
114+
*
115+
* @return bool
116+
*/
117+
public function validateDates($dates)
100118
{
101119
if (empty($dates['start']) || empty($dates['end'])) {
102120
return false;

0 commit comments

Comments
 (0)