From 326766e9ce7caeaa653cb2bac20962d1147a7e6a Mon Sep 17 00:00:00 2001 From: Jannik Schönartz Date: Thu, 9 Mar 2017 00:23:56 +0100 Subject: Locationinfo: Errors from the CourseBackend are now safes in the db and shown in the Admin-Panel. Refresh-button added for the servers. Install.php needs to be executed! --- modules-available/locationinfo/api.inc.php | 12 ++++--- .../inc/coursebackend/coursebackend_dummy.inc.php | 24 ++++++++++--- modules-available/locationinfo/install.inc.php | 11 +++++- .../locationinfo/lang/de/messages.json | 2 +- .../locationinfo/lang/en/messages.json | 2 +- modules-available/locationinfo/page.inc.php | 41 +++++++++++++++++----- .../locationinfo/templates/credentials.html | 2 +- .../locationinfo/templates/location-info.html | 15 ++++---- 8 files changed, 82 insertions(+), 27 deletions(-) (limited to 'modules-available') diff --git a/modules-available/locationinfo/api.inc.php b/modules-available/locationinfo/api.inc.php index 4fd7eee5..96685cab 100644 --- a/modules-available/locationinfo/api.inc.php +++ b/modules-available/locationinfo/api.inc.php @@ -66,13 +66,17 @@ function getCalendar($idList) { $resultarray = array(); foreach ($serverList as $serverid => $server) { $serverInstance = CourseBackend::getInstance($server['type']); - $serverInstance->setCredentials($server['credentials'], $server['url'], $serverid); - echo $serverInstance->getError(); + $setCred = $serverInstance->setCredentials($server['credentials'], $server['url'], $serverid); + $calendarFromBackend = $serverInstance->fetchSchedule($server['idlist']); $formattedArray = array(); - if ($calendarFromBackend === false) { - // TODO: write error in db. + if ($calendarFromBackend === false || $setCred === false) { + $error['timestamp'] = time(); + $error['error'] = $serverInstance->getError(); + Database::exec("UPDATE `setting_location_info` Set error=:error WHERE serverid=:id", array('id' => $serverid, 'error' => json_encode($error, true))); + } else { + Database::exec("UPDATE `setting_location_info` Set error=NULL WHERE serverid=:id", array('id' => $serverid)); } foreach ($calendarFromBackend as $key => $value) { diff --git a/modules-available/locationinfo/inc/coursebackend/coursebackend_dummy.inc.php b/modules-available/locationinfo/inc/coursebackend/coursebackend_dummy.inc.php index 02883df5..3dd0f7aa 100644 --- a/modules-available/locationinfo/inc/coursebackend/coursebackend_dummy.inc.php +++ b/modules-available/locationinfo/inc/coursebackend/coursebackend_dummy.inc.php @@ -1,14 +1,30 @@ pw = $x['password']; + + if ($this->pw == "mfg") { + $this->error = false; + return true; + } else { + $this->errormsg = "USE mfg as password!"; + $this->error = true; + return false; + } } public function checkConnection(){ - return "Your Error could be shown here!"; + if ($this->pw == "mfg") { + $this->error = false; + return true; + } else { + $this->errormsg = "USE mfg as password!"; + $this->error = true; + return false; + } } public function getCredentials(){ diff --git a/modules-available/locationinfo/install.inc.php b/modules-available/locationinfo/install.inc.php index c6e8f1f3..a371ad02 100644 --- a/modules-available/locationinfo/install.inc.php +++ b/modules-available/locationinfo/install.inc.php @@ -19,11 +19,20 @@ $res[] = tableCreate('setting_location_info', ' `servername` VARCHAR(2000) NOT NULL, `serverurl` VARCHAR(2000) NOT NULL, `servertype` VARCHAR(100) NOT NULL, - `auth` VARCHAR(100) NOT NULL, + `credentials` VARCHAR(2000), + `error` VARCHAR(2000), PRIMARY KEY (`serverid`) '); // Create response for browser +if (!tableHasColumn('setting_location_info', 'error')) { + $ret = Database::exec("ALTER TABLE `setting_location_info` ADD `error` VARCHAR(2000) AFTER `credentials`"); + if ($ret === false) { + finalResponse(UPDATE_FAILED, 'Adding column error failed: ' . Database::lastError()); + } + $res[] = UPDATE_DONE; +} + if (!tableHasColumn('setting_location_info', 'credentials')) { $ret = Database::exec("ALTER TABLE `setting_location_info` ADD `credentials` VARCHAR(2000) AFTER `servertype`"); if ($ret === false) { diff --git a/modules-available/locationinfo/lang/de/messages.json b/modules-available/locationinfo/lang/de/messages.json index 426374f6..d2ea6b05 100644 --- a/modules-available/locationinfo/lang/de/messages.json +++ b/modules-available/locationinfo/lang/de/messages.json @@ -4,5 +4,5 @@ "deleted-x-entries": "Eintr\u00e4ge gelöscht: {{0}}", "openingtime-updated": "Öffnungszeiten aktualisierts.", "config-saved": "Einstellungen erfolgreich gespeichert.", - "auth-failed": "Authentifizierung fehlgeschlagen: Locationid {{0}} Error: {{1}}" + "auth-failed": "[{{0}}] {{1}} Error: {{2}}" } diff --git a/modules-available/locationinfo/lang/en/messages.json b/modules-available/locationinfo/lang/en/messages.json index 9537fba1..b85b56cd 100644 --- a/modules-available/locationinfo/lang/en/messages.json +++ b/modules-available/locationinfo/lang/en/messages.json @@ -4,5 +4,5 @@ "deleted-x-entries": "Entries deleted: {{0}}", "openingtime-updated": "Openingtime updated.", "config-saved": "Config successfully saved.", - "auth-failed": "Authentification failed: Locationid {{0}} Error: {{1}}" + "auth-failed": "[{{0}}] {{1}} Error: {{2}}" } diff --git a/modules-available/locationinfo/page.inc.php b/modules-available/locationinfo/page.inc.php index 578a773c..ede1ac37 100644 --- a/modules-available/locationinfo/page.inc.php +++ b/modules-available/locationinfo/page.inc.php @@ -29,6 +29,8 @@ class Page_LocationInfo extends Page $this->deleteServer(); } elseif ($this->action === 'updateCredentials') { $this->updateCredentials(); + } elseif ($this->action === 'checkConnection') { + $this->checkConnection(); } } @@ -70,13 +72,13 @@ class Page_LocationInfo extends Page ON DUPLICATE KEY UPDATE servername=:name, serverurl=:url, servertype=:type", array('id' => $id, 'name' => Request::post('name', '', 'string'), 'url' => Request::post('url', '', 'string'), 'type' => Request::post('type', '', 'string'))); } - Util::redirect('?do=locationinfo&action=infoscreen'); + + $this->checkConnection(); } private function deleteServer() { $id = Request::post('id', 0, 'int'); Database::exec("DELETE FROM `setting_location_info` WHERE serverid=:id", array('id' => $id)); - Util::redirect('?do=locationinfo&action=infoscreen'); } private function updateConfig() @@ -118,6 +120,7 @@ class Page_LocationInfo extends Page $credentialsJson[$key] = Request::post($key); } Database::exec('UPDATE `setting_location_info` SET credentials = :credentials WHERE serverid = :id', array('id' => $serverid, 'credentials' => json_encode($credentialsJson, true))); + $this->checkConnection(); } private function updateOpeningTimeExpert() @@ -232,6 +235,27 @@ class Page_LocationInfo extends Page Util::redirect('?do=locationinfo'); } + private function checkConnection() { + $serverid = Request::post('id', 0, 'int'); + + if ($serverid != 0) { + $dbresult = Database::queryFirst("SELECT * FROM `setting_location_info` WHERE serverid = :serverid", array('serverid' => $serverid)); + + $serverInstance = CourseBackend::getInstance($dbresult['servertype']); + $serverInstance->setCredentials(json_decode($dbresult['credentials'], true), $dbresult['serverurl'], $serverid); + //$setCred = $serverInstance->fetchSchedule(array(42)); + $setCred = $serverInstance->checkConnection(); + + if (!$setCred) { + $error['timestamp'] = time(); + $error['error'] = $serverInstance->getError(); + Database::exec("UPDATE `setting_location_info` Set error=:error WHERE serverid=:id", array('id' => $serverid, 'error' => json_encode($error, true))); + } else { + Database::exec("UPDATE `setting_location_info` Set error=NULL WHERE serverid=:id", array('id' => $serverid)); + } + } + } + protected function toggleHidden($id, $val) { Database::exec("INSERT INTO `location_info` (locationid, hidden) VALUES (:id, :hidden) ON DUPLICATE KEY UPDATE hidden=:hidden", array('id' => $id, 'hidden' => $val)); @@ -345,16 +369,15 @@ class Page_LocationInfo extends Page $server['id'] = $db['serverid']; $server['name'] = $db['servername']; - // Instance the backend and set the credentials to check if the Authentification is accepted. - $backendType = CourseBackend::getInstance($db['servertype']); - $backendType->setCredentials(json_decode($db['credentials'], true), $db['serverurl'], $db['serverid']); - - $connection = $backendType->checkConnection(); - if ($connection === true) { + if ($db['error'] == NULL) { $server['auth'] = true; } else { $server['auth'] = false; - Message::addError('auth-failed', $server['id'], $connection); + $error = json_decode($db['error'], true); + + $time = date('Y/m/d H:i:s', $error['timestamp']); + + Message::addError('auth-failed', $server['id'], $time, $error['error']); } $serverty = array(); diff --git a/modules-available/locationinfo/templates/credentials.html b/modules-available/locationinfo/templates/credentials.html index 6933db75..70e96982 100644 --- a/modules-available/locationinfo/templates/credentials.html +++ b/modules-available/locationinfo/templates/credentials.html @@ -16,7 +16,7 @@ } else if("{{type}}" == "int") { $("#div").before('


'); } else if("{{type}}" == "bool") { - $("#div").before('


'); + $("#div").before('


'); } else if("{{type}}" == "array") { $("#div").before('
\