template.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. public function getProcessingDirectoryPath(): string {
  82. return ROOT_DIR . "/template_processing/" . $this->folder;
  83. }
  84. /**
  85. * @param $file
  86. * @return string
  87. * @throws Exception
  88. */
  89. public function getProcessingFilePath($file): string {
  90. if(!$this->folder) {
  91. throw new RuntimeException("Template processing folder not set.");
  92. }
  93. $folder_path = $this->getProcessingDirectoryPath();
  94. if(!file_exists($folder_path)) {
  95. throw new RuntimeException("Template processing folder does not exist.");
  96. }
  97. $file_path = $folder_path . "/" . $file;
  98. if(!file_exists($file_path)) {
  99. $file_path = ROOT_DIR . "/template_processing/_common/{$file}";
  100. if(!file_exists($file_path)) {
  101. throw new RuntimeException("Template processing file \"$file\" does not exist.");
  102. }
  103. }
  104. return $file_path;
  105. }
  106. /**
  107. * @return string
  108. */
  109. public function getURL(): string {
  110. return "/account/templates/" . $this->id;
  111. }
  112. /**
  113. * @return array
  114. */
  115. public function getFields(): array {
  116. $fields = [];
  117. $json = json_decode($this->getJSON());
  118. $before_fields = $json->beforeTest;
  119. $during_fields = $json->duringTest;
  120. $all_fields = array_merge($before_fields, $during_fields);
  121. foreach($all_fields as $field) {
  122. if(isset($field->name)) {
  123. $fields[$field->name] = $field;
  124. }
  125. }
  126. return $fields;
  127. }
  128. /**
  129. * @return array
  130. * @throws Exception
  131. */
  132. public function getTesterInvoiceFields(): array {
  133. $json = json_decode($this->getJSON());
  134. if(!$json) throw new Exception("Failed to decode template JSON.");
  135. return $json->testerInvoiceFields ?? [];
  136. }
  137. /**
  138. * @return array
  139. * @throws Exception
  140. */
  141. public function getCustomerInvoiceFields(): array {
  142. $json = json_decode($this->getJSON());
  143. if(!$json) throw new Exception("Failed to decode template JSON.");
  144. return $json->customerInvoiceFields ?? [];
  145. }
  146. /**
  147. * @param array $values Values as associative array
  148. * @return float
  149. * @throws Exception
  150. */
  151. public function getTesterInvoiceCosts(array $values = []): float {
  152. $costs = 0;
  153. $invoice_fields = $this->getTesterInvoiceFields();
  154. foreach($invoice_fields as $field) {
  155. $costs += (float) ($values[$field->name] ?? 0);
  156. }
  157. return $costs;
  158. }
  159. /**
  160. * @param array $overrides
  161. */
  162. public function setOverrides(array $overrides): void {
  163. $this->overrides = $overrides;
  164. }
  165. /**
  166. * @param object $field
  167. * @return object
  168. */
  169. public function applyOverridesToField(object $field): object {
  170. foreach($this->overrides as $override) {
  171. if(empty($field->name)) continue;
  172. if($override->field === $field->name) {
  173. $element = $override->element;
  174. if($element === "label") {
  175. $field->label = $override->value;
  176. }
  177. }
  178. }
  179. return $field;
  180. }
  181. /**
  182. * @param Project|null $project
  183. * @throws Exception
  184. */
  185. public function renderBeforeTestForm(?Project $project = null): void {
  186. $json = json_decode($this->getJSON());
  187. $fields = $json->beforeTest;
  188. ?>
  189. <form method="post" enctype="multipart/form-data" id="before-test-form">
  190. <?php
  191. if($project) {
  192. $project->renderBeforeTestSection();
  193. }
  194. ?>
  195. <div class="alert alert-info">
  196. <i class="fal fa-fw fa-info-circle"></i>
  197. Answer as clearly and accurately as you can otherwise you may be contacted after the test for clarification and/or verification purposes.
  198. </div>
  199. <?php
  200. foreach($fields as $field) {
  201. $field = $this->applyOverridesToField($field);
  202. $field->template = $this;
  203. Form::renderField($field);
  204. }
  205. ?>
  206. <input type="hidden" name="submit" value="<?=$_SESSION["csrf_token"]?>" />
  207. <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 />
  208. <button class="btn btn-primary mt-3" type="submit">
  209. Submit Info &amp; Photos
  210. </button>
  211. </form>
  212. <?php
  213. }
  214. /**
  215. * @throws Exception
  216. */
  217. public function renderDuringTestForm(): void {
  218. $json = json_decode($this->getJSON());
  219. $fields = $json->duringTest;
  220. $dt_now = CarbonImmutable::now(TIMEZONE);
  221. $format = "Y-m-d\TH:i:00";
  222. $dt_start = $dt_now->format($format);
  223. $dt_end = $dt_now->add(1, "minute")->format($format);
  224. ?>
  225. <form method="post" enctype="multipart/form-data" id="test-form">
  226. <div id="test-main-form-container">
  227. <div class="alert alert-info">
  228. <i class="fal fa-fw fa-info-circle"></i>
  229. Answer as clearly and accurately as you can otherwise you may be contacted after the test for clarification and/or verification purposes.
  230. </div>
  231. <?php
  232. foreach($fields as $field) {
  233. $field = $this->applyOverridesToField($field);
  234. $field->template = $this;
  235. Form::renderField($field);
  236. }
  237. ?>
  238. <hr />
  239. <h3>Entry/Exit Times</h3>
  240. <p>Please confirm your entry and exit times below. Please try to be as accurate as possible.</p>
  241. <div class="form-row">
  242. <div class="col-sm-6">
  243. <div class="form-group">
  244. <label for="start-override">Entry Time <span class="badge badge-danger">Required</span></label>
  245. <input class="form-control" type="datetime-local" name="start_override" id="start-override" value="<?=htmlspecialchars($dt_start)?>" />
  246. </div>
  247. </div>
  248. <div class="col-sm-6">
  249. <div class="form-group">
  250. <label for="end-override">Exit Time <span class="badge badge-danger">Required</span></label>
  251. <input class="form-control" type="datetime-local" name="end_override" id="end-override" value="<?=htmlspecialchars($dt_end)?>" />
  252. </div>
  253. </div>
  254. </div>
  255. <input type="hidden" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>" />
  256. <input type="hidden" name="void" id="void" value="0" />
  257. <div class="form-group">
  258. <button class="btn btn-block btn-success" type="submit" id="test-submit-button">
  259. <?=htmlspecialchars(__("Submit Test Results"))?>
  260. </button>
  261. </div>
  262. <div id="test-submit-alert" class="alert" hidden>
  263. <div class="mb-1">
  264. <i class="fa fa-fw fa-info-circle"></i> <strong></strong>
  265. </div>
  266. <div id="test-submit-status">
  267. </div>
  268. </div>
  269. </div>
  270. </form>
  271. <?php
  272. }
  273. /**
  274. * @return int
  275. */
  276. public function getPassStage(): int {
  277. return (int) $this->pass_stage;
  278. }
  279. /**
  280. * @return bool
  281. */
  282. public function hasPassFail(): bool {
  283. return !is_null($this->pass_stage);
  284. }
  285. public function getChallengeStageField(): ?object {
  286. return $this->getFields()["challenge_stage"] ?? null;
  287. }
  288. }