HomeInterface.class.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace Models {
  3. require_once 'Models/User.class.php';
  4. require_once 'Tools/Random.class.php';
  5. class HomeInterface {
  6. //
  7. protected $DataInterface;
  8. /**
  9. *
  10. */
  11. public function __construct($DataInterface) {
  12. $this->DataInterface = $DataInterface;
  13. }
  14. /**
  15. *
  16. */
  17. public function homeGet($User) {
  18. // user type
  19. $statement = $this->DataInterface->DatabaseConnection->prepare(
  20. "SELECT type FROM user WHERE ID = :fk_user"
  21. );
  22. $statement->bindParam(':fk_user', $User->ID);
  23. // Error check
  24. if(!$statement->execute()) {
  25. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  26. }
  27. $userType = $statement->fetchAll(\PDO::FETCH_ASSOC)[0]['type'];
  28. $targetID = $User->ID;
  29. if($userType=='reader') {
  30. // customer CRO
  31. $statement = $this->DataInterface->DatabaseConnection->prepare(
  32. "SELECT * FROM user WHERE type = 'cro' ORDER BY ID LIMIT 0,1"
  33. );
  34. // Error check
  35. if(!$statement->execute()) {
  36. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  37. }
  38. $customer = $statement->fetchAll(\PDO::FETCH_ASSOC);
  39. $targetID = $customer[0]['ID'];
  40. }
  41. // total purchased credits
  42. $statement = $this->DataInterface->DatabaseConnection->prepare(
  43. "SELECT SUM(count) AS purchased FROM credit WHERE ID_user = :fk_user"
  44. );
  45. $statement->bindParam(':fk_user', $targetID);
  46. // Error check
  47. if(!$statement->execute()) {
  48. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  49. }
  50. $purchased = intval($statement->fetchAll(\PDO::FETCH_ASSOC)[0]['purchased']);
  51. // total used credits
  52. $statement = $this->DataInterface->DatabaseConnection->prepare(
  53. "SELECT COUNT(ID) AS used FROM credit_usage WHERE fk_user = :fk_user"
  54. );
  55. $statement->bindParam(':fk_user', $targetID);
  56. // Error check
  57. if(!$statement->execute()) {
  58. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  59. }
  60. $used = intval($statement->fetchAll(\PDO::FETCH_ASSOC)[0]['used']);
  61. // settings
  62. $statement = $this->DataInterface->DatabaseConnection->prepare(
  63. "SELECT data FROM settings"
  64. );
  65. if(!$statement->execute()) {
  66. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  67. }
  68. $settings = $statement->fetchAll(\PDO::FETCH_ASSOC)[0]['data'];
  69. // CT
  70. $statement = $this->DataInterface->DatabaseConnection->prepare(
  71. "SELECT * FROM clinical_trial"
  72. );
  73. if(!$statement->execute()) {
  74. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  75. }
  76. $clinical_trial = $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
  77. // OK
  78. return [
  79. 'result' => 'OK',
  80. 'ID' => $User->ID,
  81. 'type' => $userType,
  82. 'firstname' => $User->firstname,
  83. 'lastname' => $User->lastname,
  84. 'email' => $User->email,
  85. 'settings' => json_decode($settings),
  86. 'clinical_trial' => $clinical_trial,
  87. 'credit_left' => $userType=='investigator'?1:($purchased - $used)
  88. ];
  89. }
  90. }
  91. }