diff --git a/CHANGELOG.md b/CHANGELOG.md index 99bcc2916..3f4b16363 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [0.17.14] - 14.06.2018 +### Added +* Logout action for RUNs + ## [0.17.13] - 12.06.2018 ### Fixed * Show-ifs: Use previous show-if value for paginated survey diff --git a/application/Controller/RunController.php b/application/Controller/RunController.php index c786688b7..ab9ec2955 100644 --- a/application/Controller/RunController.php +++ b/application/Controller/RunController.php @@ -55,7 +55,7 @@ protected function settingsAction() { } // People who have no session in the run need not set anything - $session = new RunSession($this->fdb, $run->id, 'cron', $this->user->user_code); + $session = new RunSession($this->fdb, $run->id, 'cron', $this->user->user_code, $run); if (!$session->id) { formr_error(401, 'Unauthorized', 'You cannot create settings in a study you have not participated in.'); } @@ -93,6 +93,14 @@ protected function settingsAction() { )); } + protected function logoutAction() { + $run = $this->getRun(); + Session::destroy(); + $hint = 'Session Ended'; + $text = 'Your session was successfully closed! You can restart a new session by clicking the link below.'; + formr_error(200, 'OK', $text, $hint, run_url($run->name), 'Start New Session'); + } + protected function monkeyBarAction($action = '') { $action = str_replace('ajax_', '', $action); $allowed_actions = array('send_to_position', 'remind', 'next_in_run', 'delete_user', 'snip_unit_session'); diff --git a/application/Library/Functions.php b/application/Library/Functions.php index ffe1ece5f..862196e79 100644 --- a/application/Library/Functions.php +++ b/application/Library/Functions.php @@ -135,13 +135,17 @@ function bad_request_header() { header('HTTP/1.0 500 Bad Request'); } -function formr_error($code = 500, $title = 'Bad Request', $text = 'Request could not be processed', $hint = null, $link = null) { +function formr_error($code = 500, $title = 'Bad Request', $text = 'Request could not be processed', $hint = null, $link = null, $link_text = null) { $code = $code ? $code : 500; header("HTTP/1.0 {$code} {$title}"); if ($link === null) { $link = site_url(); } + if ($link_text === null) { + $link_text = 'Go to Site'; + } + if (php_sapi_name() == 'cli') { echo date('r') . " Error {$code}: {$text} \n"; exit; @@ -152,6 +156,7 @@ function formr_error($code = 500, $title = 'Bad Request', $text = 'Request could 'title' => $hint ? $hint : $title, 'text' => $text, 'link' => $link, + 'link_text' => $link_text, )); exit; } @@ -1615,3 +1620,35 @@ function fwrite_json($handle, $data) { } } } + +function do_run_shortcodes($text, $run_name, $sess_code) { + $link_tpl = '%{text}'; + if ($run_name) { + $login_url = run_url($run_name, null, array('code' => $sess_code)); + $logout_url = run_url($run_name, 'logout', array('code' => $sess_code)); + $settings_url = run_url($run_name, 'settings', array('code' => $sess_code)); + } else { + $login_url = $settings_url = site_url(); + $logout_url = site_url('logout'); + //alert("Generated a login link, but no run was specified", 'alert-danger'); + } + + + $settings_link = Template::replace($link_tpl, array('url' => $settings_url, 'text' => 'Settings Link')); + $login_link = Template::replace($link_tpl, array('url' => $login_url, 'text' => 'Login Link')); + $logout_link = Template::replace($link_tpl, array('url' => $logout_url, 'text' => 'Logout Link')); + + $text = str_replace("{{login_link}}", $login_link, $text); + $text = str_replace("{{login_url}}", $login_url, $text); + $text = str_replace("{{login_code}}", urlencode($sess_code), $text); + $text = str_replace("{{settings_link}}", $settings_link, $text); + $text = str_replace("{{settings_url}}", $settings_url, $text); + $text = str_replace("{{logout_link}}", $logout_link, $text); + $text = str_replace("{{logout_url}}", $logout_url, $text); + $text = str_replace(urlencode("{{login_url}}"), $login_url, $text); + $text = str_replace(urlencode("{{login_code}}"), urlencode($sess_code), $text); + $text = str_replace(urlencode("{{settings_url}}"), $settings_url, $text); + $text = str_replace(urlencode("{{logout_url}}"), $logout_url, $text); + + return $text; +} diff --git a/application/Library/Session.php b/application/Library/Session.php index b39b15719..6cf1109b8 100644 --- a/application/Library/Session.php +++ b/application/Library/Session.php @@ -40,6 +40,7 @@ public static function start() { } public static function destroy() { + setcookie(session_name(), '', time() - 3600); session_unset(); session_destroy(); } diff --git a/application/Model/Email.php b/application/Model/Email.php index 7d15da897..db83cd1b2 100644 --- a/application/Model/Email.php +++ b/application/Model/Email.php @@ -111,27 +111,13 @@ public function getSubject() { protected function substituteLinks($body) { $sess = null; + $run_name = null; if (isset($this->run_name)) { + $run_name = $this->run_name; $sess = isset($this->session) ? $this->session : "TESTCODE"; - $login_link = run_url($this->run_name, null, array('code' => $sess)); - } else { - $login_link = site_url(); - alert("Generated a login link, but no run was specified", 'alert-danger'); } - $settings_link = run_url($this->run_name, 'settings', array('code' => $sess)); - $settings_link_html = 'Settings link'; - - $login_link_html = 'Login link'; - - $body = str_replace("{{login_link}}", $login_link_html, $body); - $body = str_replace("{{login_url}}", $login_link, $body); - $body = str_replace("{{login_code}}", urlencode($sess), $body); - $body = str_replace("{{settings_link}}", $settings_link_html, $body); - $body = str_replace("{{settings_url}}", $settings_link, $body); - $body = str_replace(urlencode("{{login_url}}"), $login_link, $body); - $body = str_replace(urlencode("{{login_code}}"), urlencode($sess), $body); - $body = str_replace(urlencode("{{settings_url}}"), $settings_link, $body); + $body = do_run_shortcodes($body, $run_name, $sess); return $body; } diff --git a/application/Model/Page.php b/application/Model/Page.php index 47e9e5033..a5c0fdf7e 100644 --- a/application/Model/Page.php +++ b/application/Model/Page.php @@ -102,7 +102,10 @@ public function exec() { return true; // never show to the cronjob endif; + $run_name = $sess_code = null; if ($this->run_session) { + $run_name = $this->run_session->run_name; + $sess_code = $this->run_session->session; $this->run_session->end(); } @@ -111,9 +114,10 @@ public function exec() { return true; // wait for openCPU to be fixed! } + $body = do_run_shortcodes($this->body_parsed, $run_name, $sess_code); return array( // 'title' => $this->title, - 'body' => $this->body_parsed + 'body' => $body, ); } diff --git a/application/View/public/error.php b/application/View/public/error.php index 941509bca..99bafe09a 100644 --- a/application/View/public/error.php +++ b/application/View/public/error.php @@ -21,7 +21,7 @@

-

Go to Site

+

diff --git a/application/View/public/run/index.php b/application/View/public/run/index.php index 3d7f65e29..645a03151 100644 --- a/application/View/public/run/index.php +++ b/application/View/public/run/index.php @@ -4,7 +4,7 @@ - +
diff --git a/setup.php b/setup.php index 0b7ef147a..d743ab90e 100644 --- a/setup.php +++ b/setup.php @@ -35,7 +35,7 @@ } // Set current formr version (bumped on release) -$settings['version'] = 'v0.17.13'; +$settings['version'] = 'v0.17.14'; // Load application autoloader $autoloader = require_once APPLICATION_PATH . 'Library/Autoloader.php';