| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
- namespace Models {
- require_once 'Models/User.class.php';
- require_once 'Tools/Random.class.php';
-
- class HomeInterface {
- //
- protected $DataInterface;
- /**
- *
- */
- public function __construct($DataInterface) {
- $this->DataInterface = $DataInterface;
- }
- /**
- *
- */
- public function homeGet($User) {
- // user type
- $statement = $this->DataInterface->DatabaseConnection->prepare(
- "SELECT type FROM user WHERE ID = :fk_user"
- );
- $statement->bindParam(':fk_user', $User->ID);
- // Error check
- if(!$statement->execute()) {
- return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
- }
- $userType = $statement->fetchAll(\PDO::FETCH_ASSOC)[0]['type'];
- $targetID = $User->ID;
- if($userType=='reader') {
- // customer CRO
- $statement = $this->DataInterface->DatabaseConnection->prepare(
- "SELECT * FROM user WHERE type = 'cro' ORDER BY ID LIMIT 0,1"
- );
- // Error check
- if(!$statement->execute()) {
- return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
- }
- $customer = $statement->fetchAll(\PDO::FETCH_ASSOC);
- $targetID = $customer[0]['ID'];
- }
- // total purchased credits
- $statement = $this->DataInterface->DatabaseConnection->prepare(
- "SELECT SUM(count) AS purchased FROM credit WHERE ID_user = :fk_user"
- );
- $statement->bindParam(':fk_user', $targetID);
- // Error check
- if(!$statement->execute()) {
- return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
- }
- $purchased = intval($statement->fetchAll(\PDO::FETCH_ASSOC)[0]['purchased']);
- // total used credits
- $statement = $this->DataInterface->DatabaseConnection->prepare(
- "SELECT COUNT(ID) AS used FROM credit_usage WHERE fk_user = :fk_user"
- );
- $statement->bindParam(':fk_user', $targetID);
- // Error check
- if(!$statement->execute()) {
- return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
- }
- $used = intval($statement->fetchAll(\PDO::FETCH_ASSOC)[0]['used']);
-
- // settings
- $statement = $this->DataInterface->DatabaseConnection->prepare(
- "SELECT data FROM settings"
- );
- if(!$statement->execute()) {
- return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
- }
- $settings = $statement->fetchAll(\PDO::FETCH_ASSOC)[0]['data'];
- // CT
- $statement = $this->DataInterface->DatabaseConnection->prepare(
- "SELECT * FROM clinical_trial"
- );
- if(!$statement->execute()) {
- return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
- }
- $clinical_trial = $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
- // OK
- return [
- 'result' => 'OK',
- 'ID' => $User->ID,
- 'type' => $userType,
- 'firstname' => $User->firstname,
- 'lastname' => $User->lastname,
- 'email' => $User->email,
- 'settings' => json_decode($settings),
- 'clinical_trial' => $clinical_trial,
- 'credit_left' => $userType=='investigator'?1:($purchased - $used)
- ];
- }
- }
- }
|