template.php 8.8 KB

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