From e19fd43bd5982d15b6dd65261035f961e4560656 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 19 Dec 2017 12:19:20 +0100 Subject: [locationinfo] Reload panels when opening times are changed --- modules-available/locationinfo/api.inc.php | 26 ++++--- modules-available/locationinfo/page.inc.php | 79 +++++++++++++++++----- .../locationinfo/templates/page-locations.html | 6 ++ 3 files changed, 82 insertions(+), 29 deletions(-) (limited to 'modules-available/locationinfo') diff --git a/modules-available/locationinfo/api.inc.php b/modules-available/locationinfo/api.inc.php index ceaf04c0..ad71de8b 100644 --- a/modules-available/locationinfo/api.inc.php +++ b/modules-available/locationinfo/api.inc.php @@ -49,15 +49,11 @@ function HandleParameters() /** * Get last config modification timestamp for given panel. - * This was planned to be smart and check the involved locations, - * even going up the location tree if the opening time schedule - * is inherited, but this would still be incomplete by design, as - * it wouldn't react to the linked room plan being considered - * for changes, or added/removed PCs etc. So rather than giving - * an incomplete "clever" design for detecting changes, we only - * consider direct editing of the panel now. So the advice would - * simply be "if you want the panel to reload automatically, hit - * the edit button and click save". Might even add a shortcut + * This is incomplete however, as it wouldn't react to the + * linked room plan being edited, or added/removed PCs + * etc. So the advice would simply be "if you want the + * panel to reload automatically, hit the edit button + * and click save". Might even add a shortcut * reload-button to the list of panels at some point. * * @param string $paneluuid panels uuid @@ -65,13 +61,21 @@ function HandleParameters() */ function getLastChangeTs($paneluuid) { - $panel = Database::queryFirst('SELECT lastchange FROM locationinfo_panel WHERE paneluuid = :paneluuid', + $panel = Database::queryFirst('SELECT lastchange, locationids FROM locationinfo_panel WHERE paneluuid = :paneluuid', compact('paneluuid')); if ($panel === false) { http_response_code(404); die('Panel not found'); } - return (int)$panel['lastchange']; + $lastChange = array((int)$panel['lastchange']); + if (!empty($panel['locationids'])) { + $res = Database::simpleQuery('SELECT lastchange FROM locationinfo_locationconfig + WHERE locationid IN (:locs)', array('locs' => explode(',', $panel['locationids']))); + while (($lc = $res->fetchColumn()) !== false) { + $lastChange[] = (int)$lc; + } + } + return max($lastChange); } /** diff --git a/modules-available/locationinfo/page.inc.php b/modules-available/locationinfo/page.inc.php index d4a2b35e..22a21951 100644 --- a/modules-available/locationinfo/page.inc.php +++ b/modules-available/locationinfo/page.inc.php @@ -129,8 +129,8 @@ class Page_LocationInfo extends Page } $serverlocationid = Request::post('serverlocationid', '', 'string'); - $recursive = (Request::post('recursive', '', 'string') !== ''); - if (empty($serverlocationid) && !$recursive) { + $changeServerRecursive = (Request::post('recursive', '', 'string') !== ''); + if (empty($serverlocationid) && !$changeServerRecursive) { $insertServerId = null; $ignoreServer = 1; } else { @@ -170,12 +170,16 @@ class Page_LocationInfo extends Page $mangled[] = $entry; } if (empty($mangled)) { - $openingtimes = ''; + $openingtimes = null; } else { $openingtimes = json_encode($mangled); } } } + $NOW = time(); + // Check if openingtimes changed + $res = Database::queryFirst('SELECT openingtime FROM locationinfo_locationconfig WHERE locationid = :locationid', compact('locationid')); + $otChanged = $res === false || $res['openingtime'] !== $openingtimes; Database::exec("INSERT INTO `locationinfo_locationconfig` (locationid, serverid, serverlocationid, openingtime, lastcalendarupdate, lastchange) VALUES (:id, :insertserverid, :serverlocationid, :openingtimes, 0, :now) @@ -187,27 +191,66 @@ class Page_LocationInfo extends Page 'openingtimes' => $openingtimes, 'serverlocationid' => $serverlocationid, 'ignore_server' => $ignoreServer, - 'now' => time(), + 'now' => $NOW, )); - if (!$recursive) - return true; - - // Recursive overwriting of serverid - $children = Location::getRecursiveFlat($locationid); - $array = array(); - foreach ($children as $loc) { - $array[] = $loc['locationid']; + if ($otChanged) { + $tree = Location::getLocationsAssoc(); + $todo = array(); + $done = array(); + foreach ($tree as $l) { + if ($l['parentlocationid'] == $locationid) { + $todo[] = $l['locationid']; + } + } + while (!empty($todo)) { + $loc = array_pop($todo); + if (in_array($loc, $done)) + continue; + $done[] = $loc; + // See if this one inherits + $res = Database::queryFirst('SELECT openingtime FROM locationinfo_locationconfig WHERE locationid = :loc', compact('loc')); + if ($res === false) { + $res = Database::exec('INSERT INTO locationinfo_locationconfig (locationid, lastchange) + VALUES (:locationid, :now) ON DUPLICATE KEY UPDATE lastchange = :now', + array('locationid' => $loc, 'now' => $NOW)); + } elseif (strlen($res['openingtime']) < 5) { + $res = Database::exec('UPDATE locationinfo_locationconfig SET lastchange = :now, openingtime = NULL + WHERE locationid = :locationid', + array('locationid' => $loc, 'now' => $NOW)); + } else { + $res = 0; + } + if ($res > 0) { + // Row was updated, which means the openingtime column was empty, which means the openingtime is inherited, descend further + $todo = array_merge($todo, $tree[$loc]['children']); + foreach ($tree as $l) { + if ($l['parentlocationid'] == $loc) { + $todo[] = $l['locationid']; + } + } + } + } } - if (!empty($array)) { - Database::exec("UPDATE locationinfo_locationconfig + + if ($changeServerRecursive) { + // Recursive overwriting of serverid + $children = Location::getRecursiveFlat($locationid); + $array = array(); + foreach ($children as $loc) { + $array[] = $loc['locationid']; + } + if (!empty($array)) { + Database::exec("UPDATE locationinfo_locationconfig SET serverid = :serverid, lastcalendarupdate = IF(serverid <> :serverid, 0, lastcalendarupdate), lastchange = :now WHERE locationid IN (:locations)", array( 'serverid' => $serverid, 'locations' => $array, - 'now' => time(), - )); + 'now' => $NOW, + )); + } } + return true; } @@ -465,7 +508,7 @@ class Page_LocationInfo extends Page $locations[$locid] += array( 'openingGlyph' => $glyph, 'backend' => $backend, - 'lastCalendarUpdate' => $row['lastcalendarupdate'], // TODO + 'lastCalendarUpdate' => Util::prettyTime($row['lastcalendarupdate']), // TODO 'backendMissing' => !CourseBackend::exists($row['servertype']), ); } @@ -478,8 +521,8 @@ class Page_LocationInfo extends Page $depth--; } while ($location['depth'] > $depth) { + array_push($stack, empty($location['openingGlyph']) && ($depth === -1 || empty($stack[$depth])) ? '' : 'arrow-up'); $depth++; - array_push($stack, empty($location['openingGlyph']) ? '' : 'arrow-up'); } if ($depth > 0 && empty($location['openingGlyph'])) { $location['openingGlyph'] = $stack[$depth - 1]; diff --git a/modules-available/locationinfo/templates/page-locations.html b/modules-available/locationinfo/templates/page-locations.html index de8dab7e..df760940 100644 --- a/modules-available/locationinfo/templates/page-locations.html +++ b/modules-available/locationinfo/templates/page-locations.html @@ -7,6 +7,7 @@ {{lang_locationName}} {{lang_backend}} + {{lang_lastUpdate}} {{lang_openingtimes}} @@ -24,6 +25,11 @@ {{backend}} + + {{#backend}} + {{lastCalendarUpdate}} + {{/backend}} + -- cgit v1.2.3-55-g7522