template.php 9.3 KB

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