template.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <?php /** @noinspection PhpIncludeInspection */
  2. /**
  3. * @property int $id
  4. * @property int $customer_id
  5. * @property string $name
  6. * @property string $json
  7. * @property string $json_file
  8. * @property bool $practice
  9. * @property string $folder
  10. * @property int $pass_stage
  11. */
  12. class Template extends Model {
  13. protected static $table = "template";
  14. protected static $id_field = "id";
  15. protected static $fillable_fields = ["customer_id", "name", "json", "json_file", "practice", "folder", "pass_stage"];
  16. private $overrides = [];
  17. // Object methods
  18. /**
  19. * @param string $processing_file
  20. * @param Test[] $tests
  21. * @throws Exception
  22. */
  23. public function exportTestsCSV(string $processing_file, array $tests = []) {
  24. // Preload
  25. Project::load(array_column($tests, "project_id"));
  26. Location::load(array_column($tests, "location_id"));
  27. User::load(array_column($tests, "tester_id"));
  28. // Get processing file
  29. $processing_file_path = $this->getProcessingFilePath($processing_file);
  30. // Get fields
  31. $config = json_decode($this->getJSON());
  32. $fields = array_merge($config->beforeTest, $config->duringTest);
  33. $fields = array_filter($fields, function($field) {
  34. if(!isset($field->name)) return false;
  35. if($field->type == "file") return false;
  36. return true;
  37. });
  38. // Output
  39. HTTP::setDownloadHeaders("export.csv");
  40. $handle = fopen("php://output", "w");
  41. require $processing_file_path;
  42. fclose($handle);
  43. }
  44. /**
  45. * @return string
  46. */
  47. public function getJSON(): string {
  48. $json_path = ROOT_DIR . "/json/" . $this->json_file;
  49. if(!empty($this->json_file) && file_exists($json_path)) {
  50. return file_get_contents($json_path);
  51. }
  52. return $this->json;
  53. }
  54. /**
  55. * @return Customer|null
  56. * @throws Exception
  57. */
  58. public function getCustomer(): ?Customer {
  59. return Customer::get($this->customer_id);
  60. }
  61. /**
  62. * @param $file
  63. * @return string
  64. * @throws Exception
  65. */
  66. public function getProcessingFilePath($file): string {
  67. if(!$this->folder) throw new Exception("Template processing folder not set.");
  68. $folder_path = ROOT_DIR . "/template_processing/" . $this->folder;
  69. if(!file_exists($folder_path)) throw new Exception("Template processing folder does not exist.");
  70. $file_path = $folder_path . "/" . $file;
  71. if(!file_exists($file_path)) {
  72. $file_path = ROOT_DIR . "/template_processing/_common/{$file}";
  73. if(!file_exists($file_path)) throw new Exception("Template processing file \"$file\" does not exist.");
  74. }
  75. return $file_path;
  76. }
  77. /**
  78. * @return string
  79. */
  80. public function getURL(): string {
  81. return "/account/templates/" . $this->id;
  82. }
  83. /**
  84. * @return array
  85. */
  86. public function getFields(): array {
  87. $fields = [];
  88. $json = json_decode($this->getJSON());
  89. $before_fields = $json->beforeTest;
  90. $during_fields = $json->duringTest;
  91. $all_fields = array_merge($before_fields, $during_fields);
  92. foreach($all_fields as $field) {
  93. if(empty($field->name)) continue;
  94. $fields[$field->name] = $field;
  95. }
  96. return $fields;
  97. }
  98. /**
  99. * @return array
  100. * @throws Exception
  101. */
  102. public function getTesterInvoiceFields(): array {
  103. $json = json_decode($this->getJSON());
  104. if(!$json) throw new Exception("Failed to decode template JSON.");
  105. return $json->testerInvoiceFields ?? [];
  106. }
  107. /**
  108. * @return array
  109. * @throws Exception
  110. */
  111. public function getCustomerInvoiceFields(): array {
  112. $json = json_decode($this->getJSON());
  113. if(!$json) throw new Exception("Failed to decode template JSON.");
  114. return $json->customerInvoiceFields ?? [];
  115. }
  116. /**
  117. * @param array $values
  118. * @return float
  119. * @throws Exception
  120. */
  121. public function getTesterInvoiceCosts(array $values = []): float {
  122. $costs = 0;
  123. $invoice_fields = $this->getTesterInvoiceFields();
  124. foreach($invoice_fields as $field) {
  125. $costs += (float) $values[$field->name] ?? 0;
  126. }
  127. return $costs;
  128. }
  129. /**
  130. * @param array $overrides
  131. */
  132. public function setOverrides(array $overrides) {
  133. $this->overrides = $overrides;
  134. }
  135. /**
  136. * @param object $field
  137. * @return object
  138. */
  139. public function applyOverridesToField(object $field): object {
  140. foreach($this->overrides as $override) {
  141. if(empty($field->name)) continue;
  142. if($override->field == $field->name) {
  143. switch($override->element) {
  144. case "label":
  145. $field->label = $override->value;
  146. break;
  147. }
  148. }
  149. }
  150. return $field;
  151. }
  152. /**
  153. * @throws Exception
  154. */
  155. public function renderBeforeTestForm() {
  156. $json = json_decode($this->getJSON());
  157. $fields = $json->beforeTest;
  158. ?>
  159. <form method="post" enctype="multipart/form-data" id="before-test-form">
  160. <div class="alert alert-info">
  161. <i class="fal fa-fw fa-info-circle"></i>
  162. Answer as clearly and accurately as you can otherwise you may be contacted after the test for clarification and/or verification purposes.
  163. </div>
  164. <?php
  165. foreach($fields as $field) {
  166. $field = $this->applyOverridesToField($field);
  167. Form::renderField($field);
  168. }
  169. ?>
  170. <input type="hidden" name="submit" value="<?=$_SESSION["csrf_token"]?>" />
  171. <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 />
  172. <button class="btn btn-primary mt-3" type="submit">
  173. Submit Info &amp; Photos
  174. </button>
  175. </form>
  176. <?php
  177. }
  178. /**
  179. * @throws Exception
  180. */
  181. public function renderDuringTestForm() {
  182. $json = json_decode($this->getJSON());
  183. $fields = $json->duringTest;
  184. ?>
  185. <form method="post" id="test-form">
  186. <div id="test-main-form-container">
  187. <div class="alert alert-info">
  188. <i class="fal fa-fw fa-info-circle"></i>
  189. Answer as clearly and accurately as you can otherwise you may be contacted after the test for clarification and/or verification purposes.
  190. </div>
  191. <?php
  192. foreach($fields as $field) {
  193. $field = $this->applyOverridesToField($field);
  194. Form::renderField($field);
  195. }
  196. ?>
  197. <h3>Entry/Exit Times</h3>
  198. <p>Please confirm your entry and exit times below. Please try to be as accurate as possible.</p>
  199. <div class="row mb-1">
  200. <label class="col-sm-4 col-form-label" for="start-override-hour">Entry Time <span class="badge badge-danger">Required</span></label>
  201. <div class="col-sm">
  202. <div class="row">
  203. <div class="col-lg">
  204. <select class="form-control w-auto d-inline-block" name="start_override_hour" id="start-override-hour">
  205. <option value="">HH</option>
  206. <?php
  207. Form::renderHourOptions();
  208. ?>
  209. </select>
  210. <select class="form-control w-auto d-inline-block" name="start_override_minute" id="start-override-minute">
  211. <option value="">MM</option>
  212. <?php
  213. Form::renderMinuteOptions();
  214. ?>
  215. </select>
  216. <select class="form-control w-auto d-inline-block" name="start_override_second" id="start-override-second">
  217. <option value="">SS</option>
  218. <?php
  219. Form::renderSecondOptions(0);
  220. ?>
  221. </select>
  222. </div>
  223. <div class="col-lg">
  224. <select class="form-control w-auto d-inline-block" name="start_override_day" id="start-override-day">
  225. <option value="">Day</option>
  226. <?php
  227. Form::renderDayOptions(date("j"));
  228. ?>
  229. </select>
  230. <select class="form-control w-auto d-inline-block" name="start_override_month" id="start-override-month">
  231. <option value="">Month</option>
  232. <?php
  233. Form::renderMonthOptions(date("n"));
  234. ?>
  235. </select>
  236. <select class="form-control w-auto d-inline-block" name="start_override_year" id="start-override-year">
  237. <option value="">Year</option>
  238. <?php
  239. Form::renderYearOptionsDescending(2017, date("Y"), date("Y"));
  240. ?>
  241. </select>
  242. </div>
  243. </div>
  244. </div>
  245. </div>
  246. <div class="row mb-1">
  247. <label class="col-sm-4 col-form-label" for="end-override-hour">Exit Time <span class="badge badge-danger">Required</span></label>
  248. <div class="col-sm">
  249. <div class="row">
  250. <div class="col-lg">
  251. <select class="form-control w-auto d-inline-block" name="end_override_hour" id="end-override-hour">
  252. <option value="">HH</option>
  253. <?php
  254. Form::renderHourOptions();
  255. ?>
  256. </select>
  257. <select class="form-control w-auto d-inline-block" name="end_override_minute" id="end-override-minute">
  258. <option value="">MM</option>
  259. <?php
  260. Form::renderMinuteOptions();
  261. ?>
  262. </select>
  263. <select class="form-control w-auto d-inline-block" name="end_override_second" id="end-override-second">
  264. <option value="">SS</option>
  265. <?php
  266. Form::renderSecondOptions(0);
  267. ?>
  268. </select>
  269. </div>
  270. <div class="col-lg">
  271. <select class="form-control w-auto d-inline-block" name="end_override_day" id="end-override-day">
  272. <option value="">Day</option>
  273. <?php
  274. Form::renderDayOptions(date("j"));
  275. ?>
  276. </select>
  277. <select class="form-control w-auto d-inline-block" name="end_override_month" id="end-override-month">
  278. <option value="">Month</option>
  279. <?php
  280. Form::renderMonthOptions(date("n"));
  281. ?>
  282. </select>
  283. <select class="form-control w-auto d-inline-block" name="end_override_year" id="end-override-year">
  284. <option value="">Year</option>
  285. <?php
  286. Form::renderYearOptionsDescending(2017, date("Y"), date("Y"));
  287. ?>
  288. </select>
  289. </div>
  290. </div>
  291. </div>
  292. </div>
  293. <div class="bg-light text-center p-3 mt-3">
  294. <input type="hidden" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>" />
  295. <button class="btn btn-block btn-primary" type="submit" id="test-submit-button">Submit Test Results</button>
  296. <small class="form-text text-muted d-none" id="test-no-internet-text">
  297. <span class="text-danger">No Internet Connectivity</span><br />
  298. Please check your Internet connectivity and try again.
  299. </small>
  300. <small id="test-submit-status">
  301. </small>
  302. </div>
  303. </div>
  304. </form>
  305. <?php
  306. }
  307. /**
  308. * @return int
  309. */
  310. public function getPassStage(): int {
  311. return (int) $this->pass_stage;
  312. }
  313. /**
  314. * @return bool
  315. */
  316. public function hasPassFail(): bool {
  317. return !is_null($this->pass_stage);
  318. }
  319. }