template.php 8.8 KB

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