Skip to content

Commit

Permalink
Merge branch 'release/v0.20.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
cyriltata committed Oct 21, 2022
2 parents fb539a1 + 836dff4 commit ad40d8f
Show file tree
Hide file tree
Showing 18 changed files with 194 additions and 70 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## [v0.20.5] - 20.10.2022
### Added
* User search by email in admin
* User deletion

### Fixed
* Various bug fixes

## [v0.20.4] - 13.09.2022
### Fixed
* Restart database transactions in case of lock wait timeout or deadlock.
Expand Down
67 changes: 43 additions & 24 deletions application/Controller/AdminAdvancedController.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
<?php

class AdminAdvancedController extends Controller {

public function __construct(Site &$site) {
parent::__construct($site);
if (!$this->user->isSuperAdmin()) {
formr_error(403, 'Unauthorized', 'You do not have access to this area');
}
if (!Request::isAjaxRequest()) {
$default_assets = get_default_assets('admin');
$this->registerAssets($default_assets);
$this->registerAssets('ace');
}
}
class AdminAdvancedController extends AdminController {

public function indexAction() {
$this->request->redirect('/');
}

public function infoAction() {
$this->setView('admin/advanced/info');
$this->setView('advanced/info');
return $this->sendResponse();
}

Expand All @@ -30,20 +18,20 @@ public function timingAction() {

$ocpu_time = $ocpu->post('/base/R/Sys.time/json')->getRawResult();

$this->setView('admin/advanced/timing', array(
$this->setView('advanced/timing', array(
"php" => mysql_datetime(),
"db" => $db_time,
"ocpu" => $ocpu_time));
return $this->sendResponse();
}

public function testOpencpuAction() {
$this->setView('admin/advanced/test_opencpu');
$this->setView('advanced/test_opencpu');
return $this->sendResponse();
}

public function testOpencpuSpeedAction() {
$this->setView('admin/advanced/test_opencpu_speed');
$this->setView('advanced/test_opencpu_speed');
return $this->sendResponse();
}

Expand All @@ -65,6 +53,37 @@ public function ajaxAdminAction() {
$this->response->setJsonContent($content);
return $this->sendResponse();
}

if ($request->user_delete) {
$user = (new User())->refresh(['id' => (int)$request->user_id, 'email' => $request->user_email]);
if ($user->isSuperAdmin()) {
alert('A super administrator cannot be deleted. Set admin level to 1 or below to delete', 'alert-danger');
$this->response->setContentType('application/json');
$this->response->setJsonContent(['success' => true]);
return $this->sendResponse();
}

$runs = $user->getRuns();
foreach ($runs as $row) {
$run = new Run(null, $row['id']);
$run->emptySelf();
$run->deleteUnits();
$run->delete();
}

$studies = $user->getStudies('id DESC', null, 'id');
foreach ($studies as $row) {
$study = new SurveyStudy($row['id']);
$study->delete();
}

$user->delete();

alert('User and associated data have been deleted.', 'alert-success');
$this->response->setContentType('application/json');
$this->response->setJsonContent(['success' => true]);
return $this->sendResponse();
}
}

private function setAdminLevel($user_id, $level) {
Expand Down Expand Up @@ -139,7 +158,7 @@ public function cronLogParsed() {
$parse = $file;
}

$this->setView('admin/advanced/cron_log_parsed', array(
$this->setView('advanced/cron_log_parsed', array(
'files' => $files,
'parse' => $parse,
'parser' => $parser,
Expand All @@ -155,14 +174,14 @@ public function cronLogAction() {

public function userManagementAction() {
$table = UserHelper::getUserManagementTablePdoStatement($this->request->getParams());
$this->setView('admin/advanced/user_management', $table);
$this->setView('advanced/user_management', $table);

return $this->sendResponse();
}

public function activeUsersAction() {
$table = UserHelper::getActiveUsersTablePdoStatement();
$this->setView('admin/advanced/active_users', array(
$this->setView('advanced/active_users', array(
'pdoStatement' => $table['pdoStatement'],
'pagination' => $table['pagination'],
'status_icons' => array(0 => 'fa-eject', 1 => 'fa-volume-off', 2 => 'fa-volume-down', 3 => 'fa-volume-up')
Expand Down Expand Up @@ -190,13 +209,13 @@ public function runsManagementAction() {
if (!$run->valid) {
formr_error(404, 'Not Found', 'Run Not Found');
}
$this->setView('admin/advanced/runs_management_queue', array(
$this->setView('advanced/runs_management_queue', array(
'stmt' => UnitSessionQueue::getRunItems($run),
'run' => $run,
));
return $this->sendResponse();
} else {
$this->setView('admin/advanced/runs_management', RunHelper::getRunsManagementTablePdoStatement());
$this->setView('advanced/runs_management', RunHelper::getRunsManagementTablePdoStatement());
return $this->sendResponse();
}
}
Expand Down Expand Up @@ -230,7 +249,7 @@ public function contentSettingsAction() {
$this->sendResponse($this->site->renderAlerts());
}

$this->setView('admin/advanced/settings', array('settings' => Site::getSettings()));
$this->setView('advanced/settings', array('settings' => Site::getSettings()));
return $this->sendResponse();
}
public function userDetailsAction() {
Expand Down Expand Up @@ -283,7 +302,7 @@ public function userDetailsAction() {
$users[$i] = $userx;
}

$this->setView('admin/advanced/user_detail', array(
$this->setView('advanced/user_detail', array(
'users' => $users,
'pagination' => $table['pagination'],
'position_lt' => $queryparams['position_operator'],
Expand Down
4 changes: 2 additions & 2 deletions application/Controller/AdminAjaxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,15 +462,15 @@ private function ajaxUserBulkActions() {
$run = $this->controller->run;
$count = 0;
foreach ($sessions as $sess) {
$emailSession = $run->getReminderSession($this->request->int('reminder'), $sess, $runSession->id);
$emailSession = $run->getReminderSession($this->request->int('reminder'), $sess, null);
if ($emailSession->execute() !== false) {
$count++;
}
//$email->end();
}

if ($count) {
alert("{$count} session(s) have been sent the reminder '{$email->getSubject()}'", 'alert-success');
alert("{$count} session(s) have been sent the reminder '{$emailSession->runUnit->getSubject($emailSession)}'", 'alert-success');
$res['success'] = true;
} else {
$res['error'] = $this->site->renderAlerts();
Expand Down
2 changes: 1 addition & 1 deletion application/Controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ protected function header() {
}

if ($this->site->inSuperAdminArea() && !$this->user->isSuperAdmin()) {
formr_error(403, 'Forbidden', 'Sorry! Only super admins have access to this section.');
formr_error(403, 'Unauthorized', 'You are not authorized to access this section.');
}
}

Expand Down
4 changes: 4 additions & 0 deletions application/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1755,3 +1755,7 @@ function formr_check_maintenance() {
function formr_in_console() {
return php_sapi_name() === 'cli';
}

function formr_search_highlight($search, $subject) {
return str_replace($search, '<span class="search-highlight">'.$search.'</span>', $subject);
}
1 change: 1 addition & 0 deletions application/Helper/UserHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static function getUserManagementTablePdoStatement($params = array()) {
$stmt->execute();

return array(
'search_email' => $params['email'] ?? '',
'pdoStatement' => $stmt,
'pagination' => $pagination,
);
Expand Down
2 changes: 1 addition & 1 deletion application/Model/Item/Random.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class Random_Item extends Number_Item {

public $type = 'random';
public $input_attributes = array('type' => 'hidden', 'step' => 1);
public $input_attributes = array('type' => 'hidden', 'step' => 1, 'min' => 0, 'max' => 10000000);
public $mysql_field = 'INT UNSIGNED DEFAULT NULL';
public $no_user_input_required = true;

Expand Down
2 changes: 1 addition & 1 deletion application/Model/Item/RangeTicks.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class RangeTicks_Item extends Number_Item {

public $type = 'range_ticks';
public $input_attributes = array('type' => 'range', 'step' => 1);
public $input_attributes = array('type' => 'range', 'step' => 1, 'min' => 0, 'max' => 100);
protected $labels = array();
protected $left_label = '';
protected $right_labeel = '';
Expand Down
18 changes: 18 additions & 0 deletions application/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public function update($data) {
$this->assignProperties($data);
$this->save();
}

public function delete() {
$this->db->delete($this->table, ['id' => $this->id]);
}

protected function toArray() {
return [];
Expand All @@ -82,4 +86,18 @@ public function getDbConnection() {
public function isCron() {
return $this->cron;
}

public function refresh($options) {
if (!$this->table) {
return null;
}

$row = $this->db->findRow($this->table, $options);
if ($row) {
$this->assignProperties($row);
return $this;
}

return null;
}
}
9 changes: 7 additions & 2 deletions application/Model/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,17 @@ public function delete() {
alert("<strong>Success.</strong> Successfully deleted run '{$this->name}'.", 'alert-success');
return true;
} catch (Exception $e) {
formr_log_exception($e, __CLASS__);
formr_log_exception($e, __METHOD__);
alert(__('Could not delete run %s. This is probably because there are still run units present. For safety\'s sake you\'ll first need to delete each unit individually.', $this->name), 'alert-danger');
return false;
}
}

public function deleteUnits() {
$this->db->delete('survey_run_special_units', array('run_id' => $this->id));
$this->db->delete('survey_run_units', array('run_id' => $this->id));
}

public function togglePublic($public) {
if (!in_array($public, range(0, 3))) {
return false;
Expand Down Expand Up @@ -472,7 +477,7 @@ public function getReminderSession($reminder_id, $session, $run_session_id) {
$runSession = new RunSession($session, $this, ['id' => $run_session_id]);
$runSession->createUnitSession($runUnit, false);

return $runSession;
return $runSession->currentUnitSession;
}

public function getCustomCSS() {
Expand Down
6 changes: 3 additions & 3 deletions application/Model/RunUnit/Survey.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function create($options = []) {

parent::create($options);

if (!empty($options['study_id'])) {
if (!empty($options['study_id']) && $this->db->entry_exists('survey_studies', ['id' => (int)$options['study_id']])) {
$this->unit_id = (int) $options['study_id'];
$this->surveyStudy = $this->getStudy(true);
}
Expand Down Expand Up @@ -180,8 +180,6 @@ public function getUnitSessionOutput(UnitSession $unitSession) {
return $this->processStudy($request, $study, $unitSession);
}
} catch (Exception $e) {
$this->db->logLastStatement($e);

if ($this->db->retryTransaction($e) && $this->retryOutput) {
$this->retryOutput = false;
sleep(rand(1, 4));
Expand All @@ -194,6 +192,8 @@ public function getUnitSessionOutput(UnitSession $unitSession) {
];

formr_log_exception($e, __CLASS__ . '-' . $e->getCode());
$this->db->logLastStatement($e);

return $data;
}
}
Expand Down
2 changes: 2 additions & 0 deletions application/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class User extends Model {
public $admin = false;
public $referrer_code = null;
// todo: time zone, etc.

protected $table = "survey_users";

public function __construct($id = null, $user_code = null, $options = []) {
parent::__construct();
Expand Down
2 changes: 1 addition & 1 deletion setup.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

define('FORMR_VERSION', 'v0.20.4');
define('FORMR_VERSION', 'v0.20.5');

define('APPLICATION_ROOT', __DIR__ . '/');
define('INCLUDE_ROOT', APPLICATION_ROOT);
Expand Down
Loading

0 comments on commit ad40d8f

Please sign in to comment.