template.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php /** @noinspection PhpIncludeInspection */
  2. use Carbon\CarbonImmutable;
  3. /**
  4. * @property int $id
  5. * @property int $customer_id
  6. * @property string $name
  7. * @property string $json
  8. * @property string $json_file
  9. * @property bool $practice
  10. * @property string $folder
  11. * @property int $pass_stage
  12. */
  13. class Template extends Model {
  14. protected static $table = "template";
  15. protected static $id_field = "id";
  16. protected static $fillable_fields = ["customer_id", "name", "json", "json_file", "practice", "folder", "pass_stage"];
  17. private $overrides = [];
  18. /** @var string[] Render fields with these keys otherwise skip */
  19. private $keys = [];
  20. // Object methods
  21. public function setKeys($keys): void {
  22. $this->keys = $keys;
  23. }
  24. public function getKeys(): array {
  25. return $this->keys;
  26. }
  27. /**
  28. * Returns possible template keys this template has
  29. * @return string[]
  30. */
  31. public function getTemplateKeys(): array {
  32. $fields = $this->getFields();
  33. return array_unique(array_column($fields, "requiredKey"));
  34. }
  35. /**
  36. * @param string $processing_file
  37. * @param Test[] $tests
  38. * @throws Exception
  39. */
  40. public function exportTestsCSV(string $processing_file, array $tests = []): void {
  41. // Get customer
  42. $customer = $this->getCustomer();
  43. $year_cycle_constraint = $customer->getYearCycleConstraint(TIMEZONE);
  44. // Preload
  45. Project::load(array_column($tests, "project_id"));
  46. Location::load(array_column($tests, "location_id"));
  47. User::load(array_column($tests, "tester_id"));
  48. // Get processing file
  49. $processing_file_path = $this->getProcessingFilePath($processing_file);
  50. // Get fields
  51. $config = json_decode($this->getJSON());
  52. $fields = array_merge($config->beforeTest, $config->duringTest);
  53. $fields = array_filter($fields, static function($field) {
  54. if(!isset($field->name)) return false;
  55. if($field->type === "file") return false;
  56. return true;
  57. });
  58. // Output
  59. HTTP::setDownloadHeaders("export.csv");
  60. $handle = fopen("php://output", "wb");
  61. require $processing_file_path;
  62. fclose($handle);
  63. }
  64. /**
  65. * @return string
  66. */
  67. public function getJSON(): string {
  68. $json_path = ROOT_DIR . "/json/" . $this->json_file;
  69. if(!empty($this->json_file) && file_exists($json_path)) {
  70. return file_get_contents($json_path);
  71. }
  72. return $this->json;
  73. }
  74. /**
  75. * @return Customer
  76. * @throws Exception
  77. */
  78. public function getCustomer(): Customer {
  79. return Customer::get($this->customer_id);
  80. }
  81. /**
  82. * @param $file
  83. * @return string
  84. * @throws Exception
  85. */
  86. public function getProcessingFilePath($file): string {
  87. if(!$this->folder) throw new Exception("Template processing folder not set.");
  88. $folder_path = ROOT_DIR . "/template_processing/" . $this->folder;
  89. if(!file_exists($folder_path)) throw new Exception("Template processing folder does not exist.");
  90. $file_path = $folder_path . "/" . $file;
  91. if(!file_exists($file_path)) {
  92. $file_path = ROOT_DIR . "/template_processing/_common/{$file}";
  93. if(!file_exists($file_path)) throw new Exception("Template processing file \"$file\" does not exist.");
  94. }
  95. return $file_path;
  96. }
  97. /**
  98. * @return string
  99. */
  100. public function getURL(): string {
  101. return "/account/templates/" . $this->id;
  102. }
  103. /**
  104. * @return array
  105. */
  106. public function getFields(): array {
  107. $fields = [];
  108. $json = json_decode($this->getJSON());
  109. $before_fields = $json->beforeTest;
  110. $during_fields = $json->duringTest;
  111. $all_fields = array_merge($before_fields, $during_fields);
  112. foreach($all_fields as $field) {
  113. if(isset($field->name)) {
  114. $fields[$field->name] = $field;
  115. }
  116. }
  117. return $fields;
  118. }
  119. /**
  120. * @return array
  121. * @throws Exception
  122. */
  123. public function getTesterInvoiceFields(): array {
  124. $json = json_decode($this->getJSON());
  125. if(!$json) throw new Exception("Failed to decode template JSON.");
  126. return $json->testerInvoiceFields ?? [];
  127. }
  128. /**
  129. * @return array
  130. * @throws Exception
  131. */
  132. public function getCustomerInvoiceFields(): array {
  133. $json = json_decode($this->getJSON());
  134. if(!$json) throw new Exception("Failed to decode template JSON.");
  135. return $json->customerInvoiceFields ?? [];
  136. }
  137. /**
  138. * @param array $values Values as associative array
  139. * @return float
  140. * @throws Exception
  141. */
  142. public function getTesterInvoiceCosts(array $values = []): float {
  143. $costs = 0;
  144. $invoice_fields = $this->getTesterInvoiceFields();
  145. foreach($invoice_fields as $field) {
  146. $costs += (float) ($values[$field->name] ?? 0);
  147. }
  148. return $costs;
  149. }
  150. /**
  151. * @param array $overrides
  152. */
  153. public function setOverrides(array $overrides): void {
  154. $this->overrides = $overrides;
  155. }
  156. /**
  157. * @param object $field
  158. * @return object
  159. */
  160. public function applyOverridesToField(object $field): object {
  161. foreach($this->overrides as $override) {
  162. if(empty($field->name)) continue;
  163. if($override->field === $field->name) {
  164. $element = $override->element;
  165. if($element === "label") {
  166. $field->label = $override->value;
  167. }
  168. }
  169. }
  170. return $field;
  171. }
  172. /**
  173. * @param Project|null $project
  174. * @throws Exception
  175. */
  176. public function renderBeforeTestForm(?Project $project = null): void {
  177. $json = json_decode($this->getJSON());
  178. $fields = $json->beforeTest;
  179. ?>
  180. <form method="post" enctype="multipart/form-data" id="before-test-form">
  181. <?php
  182. if($project) {
  183. $project->renderBeforeTestSection();
  184. }
  185. ?>
  186. <div class="alert alert-info">
  187. <i class="fal fa-fw fa-info-circle"></i>
  188. Answer as clearly and accurately as you can otherwise you may be contacted after the test for clarification and/or verification purposes.
  189. </div>
  190. <?php
  191. foreach($fields as $field) {
  192. $field = $this->applyOverridesToField($field);
  193. $field->template = $this;
  194. Form::renderField($field);
  195. }
  196. ?>
  197. <input type="hidden" name="submit" value="<?=$_SESSION["csrf_token"]?>" />
  198. <span class="text-danger">Please note that it may take a while for photos to upload. Please do not refresh the page when it is uploading.</span><br />
  199. <button class="btn btn-primary mt-3" type="submit">
  200. Submit Info &amp; Photos
  201. </button>
  202. </form>
  203. <?php
  204. }
  205. /**
  206. * @throws Exception
  207. */
  208. public function renderDuringTestForm(): void {
  209. $json = json_decode($this->getJSON());
  210. $fields = $json->duringTest;
  211. $dt_now = CarbonImmutable::now(TIMEZONE);
  212. $format = "Y-m-d\TH:i:00";
  213. $dt_start = $dt_now->format($format);
  214. $dt_end = $dt_now->add(1, "minute")->format($format);
  215. ?>
  216. <form method="post" enctype="multipart/form-data" id="test-form">
  217. <div id="test-main-form-container">
  218. <div class="alert alert-info">
  219. <i class="fal fa-fw fa-info-circle"></i>
  220. Answer as clearly and accurately as you can otherwise you may be contacted after the test for clarification and/or verification purposes.
  221. </div>
  222. <?php
  223. foreach($fields as $field) {
  224. $field = $this->applyOverridesToField($field);
  225. $field->template = $this;
  226. Form::renderField($field);
  227. }
  228. ?>
  229. <hr />
  230. <h3>Entry/Exit Times</h3>
  231. <p>Please confirm your entry and exit times below. Please try to be as accurate as possible.</p>
  232. <div class="form-row">
  233. <div class="col-sm-6">
  234. <div class="form-group">
  235. <label for="start-override">Entry Time <span class="badge badge-danger">Required</span></label>
  236. <input class="form-control" type="datetime-local" name="start_override" id="start-override" value="<?=htmlspecialchars($dt_start)?>" />
  237. </div>
  238. </div>
  239. <div class="col-sm-6">
  240. <div class="form-group">
  241. <label for="end-override">Exit Time <span class="badge badge-danger">Required</span></label>
  242. <input class="form-control" type="datetime-local" name="end_override" id="end-override" value="<?=htmlspecialchars($dt_end)?>" />
  243. </div>
  244. </div>
  245. </div>
  246. <input type="hidden" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>" />
  247. <input type="hidden" name="void" id="void" value="0" />
  248. <div class="form-group">
  249. <button class="btn btn-block btn-success" type="submit" id="test-submit-button">
  250. <?=htmlspecialchars(__("Submit Test Results"))?>
  251. </button>
  252. </div>
  253. <div class="text-center">
  254. <small class="form-text text-muted d-none" id="test-no-internet-text">
  255. <span class="text-danger"><?=htmlspecialchars(__("No Internet Connectivity"))?></span><br />
  256. <?=htmlspecialchars(__("Please check your Internet connectivity and try again."))?>
  257. </small>
  258. <small id="test-submit-status">
  259. </small>
  260. </div>
  261. </div>
  262. </form>
  263. <?php
  264. }
  265. /**
  266. * @return int
  267. */
  268. public function getPassStage(): int {
  269. return (int) $this->pass_stage;
  270. }
  271. /**
  272. * @return bool
  273. */
  274. public function hasPassFail(): bool {
  275. return !is_null($this->pass_stage);
  276. }
  277. public function getChallengeStageField(): ?object {
  278. return $this->getFields()["challenge_stage"] ?? null;
  279. }
  280. }