From 35e7a2b2adad42b077de60d1566c22da62edeb77 Mon Sep 17 00:00:00 2001 From: MaZderMind Date: Sun, 18 Dec 2016 13:31:34 +0100 Subject: move upcoming & current logic from javascript/ajax into php --- assets/js/lustiges-script.js | 74 -------------------------------------------- index.php | 5 --- lib/helper.php | 31 +++++++++++++++++++ template/overview.phtml | 7 +++-- view/overview.php | 18 +++++++++++ view/schedule-json.php | 13 -------- 6 files changed, 53 insertions(+), 95 deletions(-) delete mode 100644 view/schedule-json.php diff --git a/assets/js/lustiges-script.js b/assets/js/lustiges-script.js index 78b8758..65fc6ef 100644 --- a/assets/js/lustiges-script.js +++ b/assets/js/lustiges-script.js @@ -176,80 +176,6 @@ $(function() { }); }); -// startpage schedule teaser -$(function() { - var - updateTimer = 5*1000, /* update display every 5 seconds */ - refetchTimer = 10*60*1000, /* re-request current / upcoming schedule every 10 minutes */ - scheduleData = {}, - $lecture = $('.room.has-schedule'); - - if($lecture.length == 0) - return; - - function fetchProgram() { - $.ajax({ - url: $('div[data-schedule-url]').data('schedule-url'), - dataType: 'json', - success: function(data) { - scheduleData = data; - updateProgtamTeaser(); - }, - - // success & error - complete: function() { - setTimeout(fetchProgram, refetchTimer); - } - }); - } - - function updateProgtamTeaser() { - var - // corrected "now" timestamp in unix-counting (seconds, not microseconds) - now = (Date.now() / 1000); - - $.each(scheduleData, function(room, talks) { - var currentTalk, nextTalk; - - $.each(talks, function(room, talk) { - - if(!talk.room_known) - return; - - if(talk.start < now && talk.end > now) - currentTalk = talk; - - if(!nextTalk && !talk.special && talk.start > now) - nextTalk = talk; - - }); - - var s = nextTalk ? new Date(nextTalk.start*1000) : new Date(); - if(currentTalk) - $lecture.filter('.room-'+room) - .find('.current-talk') - .removeClass('hidden') - .find('.t') - .text(currentTalk.special ? 'none' : currentTalk.title) - - if(nextTalk) - $lecture.filter('.room-'+room) - .find('.next-talk') - .toggleClass('hidden', !nextTalk || nextTalk.special || (nextTalk.start - now > 60*60)) - .find('strong') - .text(s.getHours()+':'+(s.getMinutes() < 10 ? '0' : '')+s.getMinutes()) - .end() - .find('.t') - .text(nextTalk ? nextTalk.title : '') - .end() - }); - - setTimeout(updateProgtamTeaser, updateTimer); - } - - fetchProgram(); -}); - // feedback form $(function() { $('.feedback-form').on('submit', function(e) { diff --git a/index.php b/index.php index ca0a6d4..409d75e 100644 --- a/index.php +++ b/index.php @@ -196,11 +196,6 @@ try { require('view/feedback-read.php'); } - else if($route == 'schedule.json') - { - require('view/schedule-json.php'); - } - else if($route == 'gen/main.css') { if(Conferences::hasCustomStyles($mandator)) diff --git a/lib/helper.php b/lib/helper.php index df7f329..1925382 100644 --- a/lib/helper.php +++ b/lib/helper.php @@ -114,3 +114,34 @@ function url_params() return ''; } + +/** + * returns the fielst element matching $predicate or null, if none matched. + * $predicate is a callable that receives one array value at a time and can + * return a bool'ish value + */ +function array_filter_first($array, $predicate) +{ + foreach ($array as $value) { + if( $predicate($value) ) { + return $value; + } + } + + return null; +} +/** + * returns the fielst element matching $predicate or null, if none matched. + * $predicate is a callable that receives one array value at a time and can + * return a bool'ish value + */ +function array_filter_last($array, $predicate) +{ + foreach (array_reverse($array) as $value) { + if( $predicate($value) ) { + return $value; + } + } + + return null; +} diff --git a/template/overview.phtml b/template/overview.phtml index 02f0b5f..39cca02 100644 --- a/template/overview.phtml +++ b/template/overview.phtml @@ -1,6 +1,6 @@ -
+

@@ -78,15 +78,16 @@ hasSchedule()): ?> + getScheduleName() ] ?: [] ?>
Now: - none +
Next Talk: - none +
diff --git a/view/overview.php b/view/overview.php index 93cf007..adc8d5f 100644 --- a/view/overview.php +++ b/view/overview.php @@ -1,8 +1,26 @@ getSchedule(); + +$talksPerRoom = $schedule->getSchedule(); +$now = time() + $schedule->getSimulationOffset(); + +$upcomingTalksPerRoom = array_map(function($talks) use($now) { + return [ + 'current' => array_filter_last($talks, function($talk) use ($now) { + return $talk['start'] < $now && $talk['end'] > $now; + }), + 'next' => array_filter_first($talks, function($talk) use ($now) { + return !isset($talk['special']) && $talk['start'] > $now; + }), + ]; +}, $talksPerRoom); + echo $tpl->render(array( 'page' => 'overview', 'title' => 'Live-Streams', 'overview' => $conference->getOverview(), + + 'upcomingTalksPerRoom' => $upcomingTalksPerRoom, )); diff --git a/view/schedule-json.php b/view/schedule-json.php deleted file mode 100644 index 19346fd..0000000 --- a/view/schedule-json.php +++ /dev/null @@ -1,13 +0,0 @@ -getSchedule(); -if(!$schedule->isEnabled()) - throw new NotFoundException('Schedule is disabled'); - -header('Content-Type: application/json'); - -if($conference->isClosed()) - echo '{}'; - -else - echo json_encode($schedule->getSchedule(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); -- cgit v1.2.3