account.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. $page_title = __("Account");
  3. $customer = null;
  4. $unpaid_count = 0;
  5. $test_count = 0;
  6. $pass_count = 0;
  7. $pass_fail_test_count = 0;
  8. $completed_tests = null;
  9. $year_cycle_start_text = null;
  10. $stage_chart = null;
  11. $pass_rate = null;
  12. $logo_path = null;
  13. $to_do = [];
  14. $user = Core::$user;
  15. if($user->isCustomer()) {
  16. $customers = $user->getCustomers();
  17. if($customers) {
  18. $customer = $customers[0];
  19. $logo_path = $customer->getLogoPath();
  20. $page_title = __("%s Dashboard", $customer->name);
  21. [$start, $end] = $customer->getYearCycleDates();
  22. $dt = new DateTime($start);
  23. $year_cycle_start_text = $dt->format("jS M");
  24. $projects = $customer->getFinishedProjects();
  25. $project_ids = array_column($projects, "id");
  26. $tests = Test::query([
  27. "project_id" => $project_ids,
  28. "status" => Test::STATUS_COMPLETED,
  29. ]);
  30. $test_result_set = new TestResultSet($tests);
  31. $tests = $test_result_set->filterByDateRange($start, $end);
  32. $test_count = count($tests);
  33. if($test_count > 0) {
  34. $total_data = (object) [
  35. "stage1Count" => 0,
  36. "stage2Count" => 0,
  37. "stage3Count" => 0,
  38. "stage4Count" => 0,
  39. "stage5Count" => 0,
  40. ];
  41. foreach($tests as $test) {
  42. if($test->challenge_stage <= 0) continue;
  43. $key = sprintf("stage%dCount", $test->challenge_stage);
  44. ++$total_data->$key;
  45. }
  46. $chart_generator = new ChartGenerator($total_data, []);
  47. $chart_generator->setDateRangeLabel(__("YTD"));
  48. $stage_chart = $chart_generator->getTestsByStageChart(["1 - On Entry", "2 - While Browsing", "3 - During Play", "4 - After Play/Exit", "5 - No Challenge"]);
  49. }
  50. $pass_fail_tests = array_filter($tests, static function(Test $test) {
  51. $project = $test->getProject();
  52. $template = $project->getTemplate();
  53. return $template->hasPassFail();
  54. });
  55. $pass_fail_test_count = count($pass_fail_tests);
  56. if($pass_fail_test_count) {
  57. $passed_tests = array_filter($pass_fail_tests, function(Test $test) {
  58. return $test->pass;
  59. });
  60. $pass_count = count($passed_tests);
  61. $pass_rate = $pass_count / $pass_fail_test_count;
  62. }
  63. }
  64. }
  65. Core::addBreadcrumb("/account", $page_title);
  66. $users_pending_verification = 0;
  67. $tests_pending_verification = 0;
  68. $rejected_test_count = 0;
  69. $tests_remaining = 0;
  70. $icons = [];
  71. if($user->isTesterManagerOrHigher()) {
  72. $icons[] = new AccountIcon(__("Stats"), "/account/overview", "tasks-alt");
  73. }
  74. if($user->canAccessCustomers()) {
  75. $icons[] = new AccountIcon(__("Customers"), "/account/customers", "warehouse");
  76. }
  77. if($user->canAccessMapOverview()) {
  78. $icons[] = new AccountIcon(__("Map Overview"), "/account/map-overview", "map-marked");
  79. }
  80. if($user->canAccessUsers()) {
  81. $users = User::query(["level" => User::LEVEL_UNVERIFIED]);
  82. $tests = Test::query(["status" => Test::STATUS_PENDING_VERIFICATION]);
  83. $users_pending_verification = count($users);
  84. $tests_pending_verification = count($tests);
  85. $rejected_test_count = Test::count(["status" => Test::STATUS_UNASSIGNED, "dts_rejected" => Constraint::isNotNull()]);
  86. $icons[] = new AccountIcon(($user->isSuperAdmin() ? __("Users") : __("Testers")), "/account/users", "users");
  87. }
  88. if($user->canAccessProjects()) {
  89. $icons[] = new AccountIcon(__("Projects"), "/account/projects", "list");
  90. }
  91. if($user->canAccessTemplates()) {
  92. $icons[] = new AccountIcon(__("Templates"), "/account/templates", "file-alt");
  93. }
  94. if($user->canAccessVenueTypes()) {
  95. $icons[] = new AccountIcon(__("Venue Types"), "/account/venue-types", "list-alt");
  96. }
  97. if($user->canAccessLog()) {
  98. $icons[] = new AccountIcon(__("Log"), "/account/log", "database");
  99. }
  100. if($user->canAccessInvoices()) {
  101. $pending_tester_invoice_tests = Test::query([
  102. "status" => (new Constraint("NOT_EQUALS"))->setValues(Test::STATUS_CANCELLED),
  103. "status_tester_invoice" => Test::STATUS_TESTER_INVOICE_PENDING
  104. ]);
  105. $pending_tester_invoice_test_count = count($pending_tester_invoice_tests);
  106. $invoice_icon = new AccountIcon(__("Invoices"), "/account/tests?status_tester_invoice=100", "file-invoice");
  107. $invoice_icon->setCounter($pending_tester_invoice_test_count);
  108. $icons[] = $invoice_icon;
  109. }
  110. if($user->canAccessLocations()) {
  111. $icons[] = new AccountIcon(__("Locations"), "/account/locations", "map-marker");
  112. }
  113. if($user->canAccessReports()) {
  114. $icons[] = new AccountIcon(__("Reports"), "/account/reports", "chart-pie");
  115. }
  116. if($user->canAccessTestResults()) {
  117. $icons[] = new AccountIcon(__("Test Results"), "/account/test-results", "clipboard-list-check");
  118. if($user->isCustomer()) {
  119. $icons[] = new AccountIcon(__("Tests"), "/account/tests", "clipboard-check");
  120. }
  121. }
  122. if($user->canAccessTests()) {
  123. $tests = $user->getPendingTests();
  124. $tests_remaining = count($tests);
  125. $tests_url = $user->isTesterManagerOrHigher() ? "/account/tests?custom=overdue_test" : "/account/tests";
  126. $tests_icon = new AccountIcon(__("Tests"), $tests_url, "clipboard-check");
  127. $tests_icon->setCounter($tests_remaining);
  128. $icons[] = $tests_icon;
  129. $icons[] = new AccountIcon(__("Retests"), "/account/tests?retest=1", "clipboard-check");
  130. if($user->isAdmin()) {
  131. $unpaid_count = Test::count([
  132. "status" => Constraint::moreThan(Test::STATUS_CANCELLED),
  133. "payment_required" => "1",
  134. "dts_paid" => null
  135. ]);
  136. $billing_icon = new AccountIcon(__("Billing"), "/account/tests?custom=payment_required_unpaid", "file-invoice");
  137. $billing_icon->setCounter($unpaid_count);
  138. $to_do[] = $billing_icon;
  139. }
  140. }
  141. if($user->canAccessPractice()) {
  142. $icons[] = new AccountIcon(__("Practice"), "/account/practice", "chalkboard-teacher");
  143. }
  144. if($user->canAccessAddresses()) {
  145. $icons[] = new AccountIcon(__("Addresses"), "/account/addresses", "address-book");
  146. }
  147. if($user->canAccessCompletedTests()) {
  148. $icons[] = new AccountIcon(__("Tests (Completed)"), "/account/tests/completed", "check");
  149. }
  150. if($user->canSubmitId()) {
  151. $submit_id_icon = new AccountIcon(__("Submit ID"), "/account/submit-id", "id-card");
  152. $submit_id_icon->setButtonClass("btn-warning");
  153. $icons[] = $submit_id_icon;
  154. }
  155. if(!$user->terms_accepted) {
  156. $icons = [];
  157. }
  158. $icons[] = new AccountIcon(__("Settings"), "/account/settings", "cog");
  159. usort($icons, static function($a, $b) {
  160. return $a->name <=> $b->name;
  161. });
  162. // Actions
  163. $actions = [];
  164. if($user->canReactivate()) {
  165. $actions[] = new AccountIcon(__("Reactivate Account"), "/account/reactivate", "user-check");
  166. }
  167. if($user->canDeactivate()) {
  168. $actions[] = new AccountIcon(__("Deactivate Account..."), "/account/deactivate", "user-times");
  169. }
  170. $actions[] = new AccountIcon(__("Delete Account..."), "/account/delete", "user-slash");
  171. // To-do
  172. if($users_pending_verification) {
  173. $icon = new AccountIcon(__("Verify Users"), "/account/users?level=" . User::LEVEL_UNVERIFIED, "user-check");
  174. $icon->setCounter($users_pending_verification);
  175. $to_do[] = $icon;
  176. }
  177. if($tests_pending_verification) {
  178. $icon = new AccountIcon(__("Verify Tests"), "/account/tests?status=" . Test::STATUS_PENDING_VERIFICATION . "&sort=end", "clipboard-check");
  179. $icon->setCounter($tests_pending_verification);
  180. $to_do[] = $icon;
  181. }
  182. if($rejected_test_count) {
  183. $icon = new AccountIcon(__("Declined Tests"), "/account/tests?status=" . Test::STATUS_UNASSIGNED . "&custom=declined&sort=end", "times");
  184. $icon->setCounter($rejected_test_count);
  185. $to_do[] = $icon;
  186. }
  187. if($user->canAccessMapOverview()) {
  188. $to_do[] = new AccountIcon(__("Failed"), "/account/map-overview?filter=failed-only", "times-circle");
  189. $to_do[] = new AccountIcon(__("Untested"), "/account/map-overview?filter=untested-only", "question-circle");
  190. }
  191. $referrals = $user->getReferrals();
  192. View::includeOnce("header", compact("page_title"));
  193. View::include("account", compact("page_title", "icons", "actions", "to_do", "referrals", "customer", "logo_path", "year_cycle_start_text", "pass_rate", "pass_count", "pass_fail_test_count", "test_count", "stage_chart"));
  194. View::includeOnce("footer");