Skip to content

Commit

Permalink
Fix #494 the incorrect timezone adjustment
Browse files Browse the repository at this point in the history
Use PHP DateTime for due_in calculation.
  • Loading branch information
xcompass committed Dec 14, 2015
1 parent 126e18f commit 7f68367
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 265 deletions.
16 changes: 3 additions & 13 deletions app/controllers/v1_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -765,24 +765,14 @@ public function events()
{
$course_id = $this->params['course_id'];
$results = array();
$fields = array('title', 'course_id', 'event_template_type_id', 'due_date');
$fields = array('id', 'title', 'course_id', 'event_template_type_id', 'due_date');

if ($this->RequestHandler->isGet()) {
if (!isset($this->params['event_id']) || empty($this->params['event_id'])) {
$list = $this->Event->getEventFieldsByCourseId($course_id, $fields);

if (!empty($list)) {
foreach ($list as $data) {
$results[] = $data['Event'];
}
}
$results = $this->Event->getEventFieldsByCourseId($course_id, $fields);
$statusCode = 'HTTP/1.1 200 OK';
} else {
$list = $this->Event->getEventFieldsByEventId($this->params['event_id'], $fields);

if (!empty($list)) {
$results = $list['Event'];
}
$results = $this->Event->getEventFieldsByEventId($this->params['event_id'], $fields);
$statusCode = 'HTTP/1.1 200 OK';
}
$this->set('statusCode', $statusCode);
Expand Down
86 changes: 86 additions & 0 deletions app/libs/toolkit.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,4 +408,90 @@ static function formatRubricEvaluationResultsMatrix($evalResult)

return $summary + $group;
}

function getUpcomingTableArray($html, $events) {
$ret = array();
foreach ($events as $event) {
$tmp = array();
if (isset($event['Group']['group_name'])) {
$tmp[] = $html->link($event['Event']['title'],
'/evaluations/makeEvaluation/'.$event['Event']['id'].'/'.
$event['Group']['id']);
$tmp[] = $event['Group']['group_name'];
}
else {
$tmp[] = $html->link($event['Event']['title'],
'/evaluations/makeEvaluation/'.$event['Event']['id']);
}
$tmp[] = $event['Course']['course'];
$tmp[] = Toolkit::formatDate($event['Event']['due_date']);

$due = $event['Event']['due_in'];
if ($event['late']) {
$penalty = isset($event['percent_penalty']) ?
', ' . $event['percent_penalty'] . '% penalty' : '';
$tmp[] = "<span class='red'>$due</span>$penalty";
}
else {
$tmp[] = $due;
}

$ret[] = $tmp;
}
return $ret;
}

function getNonUpcomingTableArray($html, $events) {
$ret = array();
foreach ($events as $event) {
$tmp = array();
if (isset($event['Event']['is_result_released']) &&
$event['Event']['is_result_released']
) { // we're in the result release period, so link to the results
$tmp[] = $html->link($event['Event']['title'],
'/evaluations/studentViewEvaluationResult/' .
$event['Event']['id'] . '/' . $event['Group']['id']);
$tmp[] = $event['Event']['result_release_date_end'];
}
else if ($event['Event']['event_template_type_id'] == 3) {
// this is a survey, no release period, so link to the results
$tmp[] = $html->link($event['Event']['title'],
'/evaluations/studentViewEvaluationResult/' .
$event['Event']['id']);
}
else {
// we're not in the result release period, notify user when they can
// view the results
if ($event['Event']['is_released']) {
// can let students edit their submissions
if (isset($event['Group']['group_name'])) {
$tmp[] = $html->link($event['Event']['title'],
'/evaluations/makeEvaluation/'.$event['Event']['id'].
'/'. $event['Group']['id']);
}
else {
$tmp[] = $html->link($event['Event']['title'],
'/evaluations/makeEvaluation/'.$event['Event']['id']);
}
}
else {
$tmp[] = $event['Event']['title'];
}
$tmp[] = "<span class='orangered'>" .
$event['Event']['result_release_date_begin'] . "</span>";
}
if (isset($event['Group']['group_name'])) {
// NOTE: surveys don't have group names
$tmp[] = $event['Group']['group_name'];
}
$tmp[] = $event['Course']['course'];
$tmp[] = Toolkit::formatDate($event['Event']['due_date']);
if (!empty($event['EvaluationSubmission'])) {
// expired events have no submissions
$tmp[] = $event['EvaluationSubmission']['date_submitted'];
}
$ret[] = $tmp;
}
return $ret;
}
}
78 changes: 60 additions & 18 deletions app/models/event.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ class Event extends AppModel
),
);

private $timezone = null;

/**
* __construct
*
Expand Down Expand Up @@ -178,6 +180,7 @@ function __construct($id = false, $table = null, $ds = null)
ini_get('date.timezone') : 'UTC';
}
date_default_timezone_set($timezone);
$this->timezone = $timezone;
parent::__construct($id, $table, $ds);
$this->virtualFields['response_count'] = sprintf('SELECT count(*) as count FROM evaluation_submissions as sub WHERE sub.event_id = %s.id', $this->alias);
$this->virtualFields['to_review_count'] = sprintf('SELECT count(*) as count FROM group_events as ge WHERE ge.event_id = %s.id AND marked LIKE "to review"', $this->alias);
Expand All @@ -190,14 +193,14 @@ function __construct($id = false, $table = null, $ds = null)
* SQL does not recognize daylight savings, therefore when not in a daylight savings period, the timezone is 1hr (3600s) behind the actual time.
* NOTE: UNIX_TIMESTAMP() appears to work, but online sources say otherwise.
*/
$dueIn = "";
if(date('I') == 1){
$dueIn = date('Y-m-d H:i:s', time());
}
else{
$dueIn = date('Y-m-d H:i:s', time() + 3600);
}
$this->virtualFields['due_in'] = sprintf('TIMESTAMPDIFF(SECOND,"%s",due_date)', $dueIn);
// $dueIn = "";
// if(date('I') == 1){
// $dueIn = date('Y-m-d H:i:s', time());
// }
// else{
// $dueIn = date('Y-m-d H:i:s', time() + 3600);
// }
// $this->virtualFields['due_in'] = sprintf('TIMESTAMPDIFF(SECOND,"%s",due_date)', $dueIn);
//$this->virtualFields['due_in'] = sprintf('UNIX_TIMESTAMP(due_date) - UNIX_TIMESTAMP("%s")', date('Y-m-d H:i:s'));
//$this->virtualFields['due_in'] = sprintf('TIMESTAMPDIFF(SECOND,"%s",due_date)', date('Y-m-d H:i:s'));
}
Expand All @@ -209,11 +212,11 @@ function __construct($id = false, $table = null, $ds = null)
* @param mixed $primary
*
* @access public
* @return void
* @return mixed
*/
function afterFind(array $results, $primary)
{
$currentDate = strtotime('NOW');
$currentDate = time();
foreach ($results as $key => $event) {
if (isset($event['Event']['release_date_begin'])) {
$results[$key]['Event']['is_released'] =
Expand All @@ -229,6 +232,10 @@ function afterFind(array $results, $primary)
$results[$key]['Event']['is_ended'] =
($currentDate > strtotime($event['Event']['release_date_end']));
}
if (isset($event['Event']['due_date'])) {
$due = new DateTime($event['Event']['due_date'], new DateTimeZone($this->timezone));
$results[$key]['Event']['due_in'] = $due->getTimestamp() - $currentDate;
}
}

return $results;
Expand Down Expand Up @@ -645,9 +652,12 @@ function getEventTitleById($id)
*/
function getEventFieldsByCourseId($courseId, $fields)
{
return $this->find('all', array(
$events = $this->find('all', array(
'conditions' => array('course_id' => $courseId),
'fields' => $fields));
'fields' => $fields,
'contain' => array()));

return $this->filterFields($events, $fields);
}

/**
Expand All @@ -660,11 +670,38 @@ function getEventFieldsByCourseId($courseId, $fields)
*/
function getEventFieldsByEventId($eventId, $fields)
{
return $this->find('first', array(
$events = $this->find('first', array(
'conditions' => array('Event.id' => $eventId),
'fields' => $fields));
'fields' => $fields,
'contain' => array()));

$result = $this->filterFields($events, $fields);
if (!empty($result)) {
$result = $result[0];
}
return $result;
}

function filterFields($events, $fields) {
$result = array();
if (!$events) {
return array();
}

if (array_key_exists('Event', $events)) {
$events = array($events);
}
foreach ($events as $e) {
foreach ($e['Event'] as $key => $value) {
if (!in_array($key, $fields)) {
unset($e['Event'][$key]);
}
}
$result[] = $e['Event'];
}

return $result;
}
/**
* Get evaluations and surveys assigned to the given user. Also gets the
* evaluation submission entries made by this specific user.
Expand Down Expand Up @@ -715,7 +752,7 @@ function getEventsByUserId($userId, $fields = null, $extraId = null)
$evaluationEvents = $this->find('all', array(
'fields' => $evaluationFields,
'conditions' => array('GroupEvent.id' => $groupEventIds),
'order' => array('due_in ASC'),
'order' => array('due_date ASC'),
'contain' => array(
'Course',
'Group',
Expand Down Expand Up @@ -764,7 +801,7 @@ function getEventsByUserId($userId, $fields = null, $extraId = null)
$surveyEvents = $this->find('all', array(
'fields' => $surveyFields,
'conditions' => array('event_template_type_id' => '3', 'course_id' => $courseIds),
'order' => array('due_in ASC'),
'order' => array('due_date ASC'),
'contain' => array(
'Course',
'EvaluationSubmission' => array(
Expand Down Expand Up @@ -795,14 +832,19 @@ function getEventsByUserId($userId, $fields = null, $extraId = null)
* @param mixed $fields the fields to retreive
*
* @access public
* @return void
* @return array
*/
public function getPendingEventsByUserId($userId, $options, $fields = null)
public function getPendingEventsByUserId($userId, $options, $fields = array())
{
$pendingEvents = array();
$events = $this->getEventsByUserId($userId, $fields);
$events = array_merge($events['Evaluations'], $events['Surveys']);
foreach ($events as $event) {
# temporary hack for removing due_in field as it is not a virtualfield anymore
if (!array_key_exists('due_in', $fields)) {
unset($event['Event']['due_in']);
}

if ($options['submission'] && $event['Event']['is_released'] && empty($event['EvaluationSubmission'])) {
// filter for released evaluations that are available for submission
$pendingEvents[] = $event['Event'];
Expand Down
Loading

0 comments on commit 7f68367

Please sign in to comment.