Skip to content

Commit

Permalink
Merge branch 'release/v0.19.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenarslan committed Aug 17, 2021
2 parents 39f6d74 + 4f6f0b1 commit 2738cfd
Show file tree
Hide file tree
Showing 21 changed files with 378 additions and 87 deletions.
109 changes: 109 additions & 0 deletions application/Controller/AdminAdvancedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,5 +209,114 @@ public function settingsAction() {
$this->setView('admin/advanced/settings', array('settings' => Site::getSettings()));
return $this->sendResponse();
}
public function userDetailsAction() {
$querystring = array();
$queryparams = array( 'position_operator' => '=');

if ($this->request->position_lt && in_array($this->request->position_lt, array('=', '>', '<'))) {
$queryparams['position_operator'] = $this->request->position_lt;
$querystring['position_lt'] = $queryparams['position_operator'];
}

if ($this->request->run_name) {
$queryparams['run_name'] = $this->request->run_name;
$querystring['run_name'] = $queryparams['run_name'];
}

if ($this->request->session) {
$session = str_replace("", "", $this->request->session);
$queryparams['session'] = "%" . $session . "%";
$querystring['session'] = $session;
}

if ($this->request->position) {
$queryparams['position'] = $this->request->position;
$querystring['position'] = $queryparams['position'];
}

$table = $this->getUserDetailTable($queryparams);
$users = $table['data'];

foreach ($users as $i => $userx) {
if ($userx['expired']) {
$stay_seconds = strtotime($userx['expired']) - strtotime($userx['created']);
} else {
$stay_seconds = ($userx['ended'] ? strtotime($userx['ended']) : time() ) - strtotime($userx['created']);
}
$userx['stay_seconds'] = $stay_seconds;
if ($userx['expired']) {
$userx['ended'] = $userx['expired'] . ' (expired)';
}

if ($userx['unit_type'] != 'Survey') {
$userx['delete_msg'] = "Are you sure you want to delete this unit session?";
$userx['delete_title'] = "Delete this waypoint";
} else {
$userx['delete_msg'] = "You SHOULDN'T delete survey sessions, you might delete data! <br />Are you REALLY sure you want to continue?";
$userx['delete_title'] = "Survey unit sessions should not be deleted";
}

$users[$i] = $userx;
}

$this->setView('admin/advanced/user_detail', array(
'users' => $users,
'pagination' => $table['pagination'],
'position_lt' => $queryparams['position_operator'],
'querystring' => $querystring,
));

return $this->sendResponse();
}

private function getUserDetailTable($queryParams, $page = null) {
$query = array();
if (!empty($queryParams['run_name'])) {
$query[] = ' `survey_runs`.name LIKE :run_name ';
}

if (!empty($queryParams['session'])) {
$query[] = ' `survey_run_sessions`.session LIKE :session ';
}

if (!empty($queryParams['position'])) {
$query[] = " `survey_run_units`.position {$queryParams['position_operator']} :position ";
}
unset($queryParams['position_operator']);

if(count($query) > 0 ) {
$where = "WHERE " . implode(' AND ', $query);
} else {
$where = "";
}

$itemsQuery = "SELECT
`survey_run_sessions`.session,
`survey_unit_sessions`.id AS session_id,
`survey_runs`.name AS run_name,
`survey_run_units`.position,
`survey_run_units`.description,
`survey_units`.type AS unit_type,
`survey_unit_sessions`.created,
`survey_unit_sessions`.ended,
`survey_unit_sessions`.expired,
`survey_unit_sessions`.expires,
`survey_unit_sessions`.`queued`,
`survey_unit_sessions`.result,
`survey_unit_sessions`.result_log
FROM `survey_unit_sessions`
LEFT JOIN `survey_run_sessions` ON `survey_run_sessions`.id = `survey_unit_sessions`.run_session_id
LEFT JOIN `survey_units` ON `survey_unit_sessions`.unit_id = `survey_units`.id
LEFT JOIN `survey_run_units` ON `survey_unit_sessions`.unit_id = `survey_run_units`.unit_id
LEFT JOIN `survey_runs` ON `survey_runs`.id = `survey_run_units`.run_id
{$where}
ORDER BY `survey_run_sessions`.id DESC,`survey_unit_sessions`.id ASC LIMIT 1000
";

return array(
'data' => $this->fdb->execute($itemsQuery, $queryParams),
'pagination' => "",
);
}

}
8 changes: 6 additions & 2 deletions application/Controller/AdminRunController.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,12 @@ private function createNewTestCodeAction() {
$animal = substr($sess, 0, strpos($sess, "XXX"));
$sess_url = run_url($this->run->name, null, array('code' => $sess));

//alert("You've created a new guinea pig, ".h($animal).". Use this guinea pig to move through the run like a normal user with special powers (accessibly via the monkey bar at the bottom right). As a guinea pig, you can see more detailed error messages than real users, so it is easier to e.g. debug R problems. If you want someone else to be the guinea pig, just forward them this link: <br><textarea readonly cols='60' rows='3' class='copy_clipboard readonly-textarea'>" . h($sess_url) . "</textarea>", "alert-info");
$this->request->redirect($sess_url);
if (Config::get('use_study_subdomains')) {
$this->request->redirect($sess_url);
} else {
alert("You've created a <a target='_blank' href='".$sess_url."'>new guinea pig, ".h($animal)."</a>. Use this guinea pig to move through the run like a normal user with special powers (accessibly via the monkey bar at the bottom right). As a guinea pig, you can see more detailed error messages than real users, so it is easier to e.g. debug R problems. If you want someone else to be the guinea pig, just forward them this link: <br><textarea readonly cols='60' rows='3' class='copy_clipboard readonly-textarea'>" . h($sess_url) . "</textarea>", "alert-info");
$this->request->redirect(admin_run_url($this->run->name, 'user_overview'));
}
}

private function createNewNamedSessionAction() {
Expand Down
15 changes: 6 additions & 9 deletions application/Helper/RunHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public function getUserOverviewTable($queryParams, $page = null) {
`survey_run_sessions`.last_access,
`survey_run_sessions`.created,
`survey_run_sessions`.testing,
`survey_run_sessions`.current_unit_session_id,
`survey_runs`.name AS run_name,
`survey_units`.type AS unit_type,
`survey_run_sessions`.last_access,
Expand All @@ -173,10 +174,7 @@ public function getUserOverviewTable($queryParams, $page = null) {
LEFT JOIN `survey_runs` ON `survey_run_sessions`.run_id = `survey_runs`.id
LEFT JOIN `survey_run_units` ON `survey_run_sessions`.position = `survey_run_units`.position AND `survey_run_units`.run_id = `survey_run_sessions`.run_id
LEFT JOIN `survey_units` ON `survey_run_units`.unit_id = `survey_units`.id
LEFT JOIN
(SELECT * FROM `survey_unit_sessions`
WHERE `survey_unit_sessions`.ended IS NULL AND `survey_unit_sessions`.expired IS NULL
ORDER BY `survey_unit_sessions`.id DESC LIMIT 1) us ON `survey_run_sessions`.id = `us`.run_session_id
LEFT JOIN `survey_unit_sessions` us ON `survey_run_sessions`.current_unit_session_id = `us`.id
WHERE {$where}
ORDER BY `survey_run_sessions`.session != :admin_code, `survey_run_sessions`.last_access DESC
LIMIT $limits
Expand All @@ -195,6 +193,7 @@ public function getUserOverviewExportPdoStatement($queryParams) {
`survey_run_units`.description,
`survey_run_sessions`.session,
`survey_run_sessions`.created,
`survey_run_sessions`.current_unit_session_id,
`survey_run_sessions`.last_access,
`us`.result,
`us`.result_log,
Expand All @@ -203,10 +202,7 @@ public function getUserOverviewExportPdoStatement($queryParams) {
LEFT JOIN `survey_runs` ON `survey_run_sessions`.run_id = `survey_runs`.id
LEFT JOIN `survey_run_units` ON `survey_run_sessions`.position = `survey_run_units`.position AND `survey_run_units`.run_id = `survey_run_sessions`.run_id
LEFT JOIN `survey_units` ON `survey_run_units`.unit_id = `survey_units`.id
LEFT JOIN
(SELECT * FROM `survey_unit_sessions`
WHERE `survey_unit_sessions`.ended IS NULL AND `survey_unit_sessions`.expired IS NULL
ORDER BY `survey_unit_sessions`.id DESC LIMIT 1) us ON `survey_run_sessions`.id = `us`.run_session_id
LEFT JOIN `survey_unit_sessions` us ON `survey_run_sessions`.current_unit_session_id = `us`.id
WHERE `survey_run_sessions`.run_id = :run_id ORDER BY `survey_run_sessions`.session != :admin_code,`survey_run_sessions`.last_access DESC
";
$stmt = $this->db->prepare($query);
Expand Down Expand Up @@ -272,7 +268,8 @@ public function getUserDetailTable($queryParams, $page = null) {
public function getUserDetailExportPdoStatement($queryParams) {
$query = "
SELECT
`survey_run_units`.position,
`survey_unit_sessions`.id AS session_id,
`survey_run_units`.position,
`survey_units`.type AS unit_type,
`survey_run_units`.description,
`survey_run_sessions`.session,
Expand Down
32 changes: 17 additions & 15 deletions application/Library/EmailQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,22 @@ protected function closeSMTPConnection($account_id) {
}
}

protected function logResult($session_id, $status_code, $result, $result_log = null) {
protected function logResult($session_id, $email_id, $status_code, $result, $result_log = null) {
$this->db->exec('UPDATE `survey_email_log`
SET `status` = :status_code,
`sent` = NOW()
WHERE `session_id` = :session_id', array(
'session_id' => $session_id,
WHERE `id` = :id', array(
'id' => $email_id,
'status_code' => $status_code));
$this->db->exec('UPDATE `survey_unit_sessions`
SET `result` = :result,
`result_log` = :resultlog
WHERE `id` = :session_id', array(
'session_id' => $session_id,
'result' => $result,
'resultlog' => $result_log));
if($session_id) {
$this->db->exec('UPDATE `survey_unit_sessions`
SET `result` = :result,
`result_log` = :resultlog
WHERE `id` = :session_id', array(
'session_id' => $session_id,
'result' => $result,
'resultlog' => $result_log));
}
}

protected function deactivateAccount($account_id) {
Expand Down Expand Up @@ -188,11 +190,11 @@ protected function processQueue($account_id = null) {
$emailsStatement = $this->getEmailsStatement($account['account_id']);
while ($email = $emailsStatement->fetch(PDO::FETCH_ASSOC)) {
if (!filter_var($email['recipient'], FILTER_VALIDATE_EMAIL)) {
$this->logResult($email['session_id'], self::STATUS_INVALID_RECIPIENT, "error_email_invalid_recipient");
$this->logResult($email['session_id'], $email['id'], self::STATUS_INVALID_RECIPIENT, "error_email_invalid_recipient");
continue;
}
if (!$email['subject']) {
$this->logResult($email['session_id'], self::STATUS_INVALID_SUBJECT, "error_email_invalid_subject");
$this->logResult($email['session_id'], $email['id'], self::STATUS_INVALID_SUBJECT, "error_email_invalid_subject");
continue;
}

Expand Down Expand Up @@ -227,18 +229,18 @@ protected function processQueue($account_id = null) {
// Send mail
try {
if (($sent = $mailer->send())) {
$this->logResult($email['session_id'], self::STATUS_SENT, "email_sent");
$this->logResult($email['session_id'], $email['id'], self::STATUS_SENT, "email_sent");
$this->dbg("Send Success. \n {$debugInfo}");
} else {
$this->dbg($mailer->ErrorInfo);
$this->logResult($email['session_id'], self::STATUS_FAILED_TO_SEND, "error_email_not_sent", $mailer->ErrorInfo);
$this->logResult($email['session_id'], $email['id'], self::STATUS_FAILED_TO_SEND, "error_email_not_sent", $mailer->ErrorInfo);
throw new Exception($mailer->ErrorInfo);
}
} catch (Exception $e) {
//formr_log_exception($e, 'EmailQueue ' . $debugInfo);
$this->dbg("Send Failure: " . $mailer->ErrorInfo . ".\n {$debugInfo}");
$this->dbg($mailer->ErrorInfo);
$this->logResult($email['session_id'], self::STATUS_FAILED_TO_SEND, "error_email_not_sent", $mailer->ErrorInfo);
$this->logResult($email['session_id'], $email['id'], self::STATUS_FAILED_TO_SEND, "error_email_not_sent", $mailer->ErrorInfo);
// reset php mailer object for this account if smtp sending failed. Probably some limits have been hit
$this->closeSMTPConnection($account['account_id']);
$mailer = $this->getSMTPConnection($account);
Expand Down
2 changes: 1 addition & 1 deletion application/Library/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function run() {
return true;
}

protected function dbg($str) {
public function dbg($str) {
$args = func_get_args();
if (count($args) > 1) {
$str = vsprintf(array_shift($args), $args);
Expand Down
7 changes: 4 additions & 3 deletions application/Library/UnitSessionQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class UnitSessionQueue extends Queue {
const QUEUED_TO_EXECUTE = 1;
const QUEUED_TO_END = 2;
const QUEUED_NOT = 0;
const QUEUED_SUPERCEDED = -9;

public function __construct(DB $db, array $config) {
parent::__construct($db, $config);
Expand All @@ -35,7 +36,7 @@ public function run() {
while (!$this->out && $this->rested()) {
if ($this->processQueue() === false) {
// if there is nothing to process in the queue sleep for sometime
$this->dbg("Sleeping because nothing was found in queue");
// $this->dbg("Sleeping because nothing was found in queue");
sleep($this->sleep);
$sleeps++;
}
Expand Down Expand Up @@ -88,8 +89,8 @@ protected function getSessionsStatement() {
//LIMIT {$this->limit} OFFSET {$this->offset}";

if ($this->debug) {
$this->dbg($query . ' queued: ' . $queued);
$this->dbg($this->list_type);
// $this->dbg($query . ' queued: ' . $queued);
// $this->dbg($this->list_type);
}

return $this->db->rquery($query, array('queued' => $queued));
Expand Down
60 changes: 31 additions & 29 deletions application/Model/External.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,48 +135,50 @@ private function hasExpired() {
}

public function exec() {
// never redirect, if we're just in the cronjob. just text for expiry
// never redirect, if we're just in the cronjob. just test for expiry
$expired = $this->hasExpired();
if ($this->called_by_cron) {
if ($expired) {
$this->expire();
return false;
} else {
return true; // wait for expiry or user
}
}

// if it's the user, redirect them or do the call
if ($this->isR($this->address)) {
$goto = null;
$opencpu_vars = $this->getUserDataInRun($this->address);
$result = opencpu_evaluate($this->address, $opencpu_vars);

if ($result === null) {
return true; // don't go anywhere, wait for the error to be fixed!
} elseif ($result === false) {
$this->end();
return false; // go on, no redirect
} elseif ($this->isAddress($result)) {
$goto = $result;
} else {
if ($this->isR($this->address)) { ## if it's the user, redirect them
$goto = null;
$opencpu_vars = $this->getUserDataInRun($this->address);
$result = opencpu_evaluate($this->address, $opencpu_vars);

if ($result === null) {
$this->session_result = "error_opencpu";
return true; // don't go anywhere, wait for the error to be fixed!
} elseif ($result === false) {
$this->session_result = "external_r_call_no_redirect";
$this->end();
return false; // go on, no redirect
} elseif ($this->isAddress($result)) {
$this->session_result = "external_r_redirect";
$goto = $result;
}
} else { // the simplest case, just an address
$this->session_result = "external_redirect";
$goto = $this->address;
}
} else { // the simplest case, just an address
$goto = $this->address;
}
// replace the code placeholder, if any
$goto = $this->makeAddress($goto);
$this->logResult();

// replace the code placeholder, if any
$goto = $this->makeAddress($goto);

// never redirect if we're just in the cron job
if (!$this->called_by_cron) {
// sometimes we aren't able to control the other end
if (!$this->api_end) {
if ($this->api_end) {
$this->session_result = "external_wait_for_api";
$this->logResult();
} else {
$this->end();
$this->run_session->execute();
}

redirect_to($goto);
return false;
return !$this->api_end;
}
return true;
}

}
4 changes: 3 additions & 1 deletion application/Model/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ public function test() {
public function exec() {
if ($this->called_by_cron) {
$this->getParsedBody($this->body); // make report before showing it to the user, so they don't have to wait
$this->session_result = "ended_study_by_queue";
$this->logResult();
return true; // never show to the cronjob
}

Expand All @@ -110,7 +112,7 @@ public function exec() {

$body = do_run_shortcodes($this->body_parsed, $run_name, $sess_code);

$this->session_result = "ended";
$this->session_result = "ended_study";
$this->logResult();

return array(
Expand Down
2 changes: 1 addition & 1 deletion application/Model/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ public function getSpecialUnits($render = false, $xtype = null, $id = null) {
public function getReminder($reminder_id, $session, $run_session_id) {
// create a unit_session here and get a session_id and pass it when making the unit
$unitSession = new UnitSession($this->dbh, $run_session_id, $reminder_id);
$session_id = $unitSession->create();
$session_id = $unitSession->create(false);
$unit_factory = new RunUnitFactory();
$unit = $unit_factory->make($this->dbh, $session, array(
'type' => "Email",
Expand Down
Loading

0 comments on commit 2738cfd

Please sign in to comment.