keys = $keys; } public function getKeys(): array { return $this->keys; } /** * Returns possible template keys this template has * @return string[] */ public function getTemplateKeys(): array { $fields = $this->getFields(); return array_unique(array_column($fields, "requiredKey")); } /** * @param string $processing_file * @param Test[] $tests * @throws Exception */ public function exportTestsCSV(string $processing_file, array $tests = []): void { // Get customer $customer = $this->getCustomer(); $year_cycle_constraint = $customer->getYearCycleConstraint(TIMEZONE); // Preload Project::load(array_column($tests, "project_id")); Location::load(array_column($tests, "location_id")); User::load(array_column($tests, "tester_id")); // Get processing file $processing_file_path = $this->getProcessingFilePath($processing_file); // Get fields $config = json_decode($this->getJSON()); $fields = array_merge($config->beforeTest, $config->duringTest); $fields = array_filter($fields, static function($field) { if(!isset($field->name)) return false; if($field->type === "file") return false; return true; }); // Output HTTP::setDownloadHeaders("export.csv"); $handle = fopen("php://output", "wb"); require $processing_file_path; fclose($handle); } /** * @return string */ public function getJSON(): string { $json_path = ROOT_DIR . "/json/" . $this->json_file; if(!empty($this->json_file) && file_exists($json_path)) { return file_get_contents($json_path); } return $this->json; } /** * @return Customer * @throws Exception */ public function getCustomer(): Customer { return Customer::get($this->customer_id); } public function getProcessingDirectoryPath(): string { return ROOT_DIR . "/template_processing/" . $this->folder; } /** * @param $file * @return string * @throws Exception */ public function getProcessingFilePath($file): string { if(!$this->folder) { throw new RuntimeException("Template processing folder not set."); } $folder_path = $this->getProcessingDirectoryPath(); if(!file_exists($folder_path)) { throw new RuntimeException("Template processing folder does not exist."); } $file_path = $folder_path . "/" . $file; if(!file_exists($file_path)) { $file_path = ROOT_DIR . "/template_processing/_common/{$file}"; if(!file_exists($file_path)) { throw new RuntimeException("Template processing file \"$file\" does not exist."); } } return $file_path; } /** * @return string */ public function getURL(): string { return "/account/templates/" . $this->id; } /** * @return array */ public function getFields(): array { $fields = []; $json = json_decode($this->getJSON()); $before_fields = $json->beforeTest; $during_fields = $json->duringTest; $all_fields = array_merge($before_fields, $during_fields); foreach($all_fields as $field) { if(isset($field->name)) { $fields[$field->name] = $field; } } return $fields; } /** * @return array * @throws Exception */ public function getTesterInvoiceFields(): array { $json = json_decode($this->getJSON()); if(!$json) throw new Exception("Failed to decode template JSON."); return $json->testerInvoiceFields ?? []; } /** * @return array * @throws Exception */ public function getCustomerInvoiceFields(): array { $json = json_decode($this->getJSON()); if(!$json) throw new Exception("Failed to decode template JSON."); return $json->customerInvoiceFields ?? []; } /** * @param array $values Values as associative array * @return float * @throws Exception */ public function getTesterInvoiceCosts(array $values = []): float { $costs = 0; $invoice_fields = $this->getTesterInvoiceFields(); foreach($invoice_fields as $field) { $costs += (float) ($values[$field->name] ?? 0); } return $costs; } /** * @param array $overrides */ public function setOverrides(array $overrides): void { $this->overrides = $overrides; } /** * @param object $field * @return object */ public function applyOverridesToField(object $field): object { foreach($this->overrides as $override) { if(empty($field->name)) continue; if($override->field === $field->name) { $element = $override->element; if($element === "label") { $field->label = $override->value; } } } return $field; } /** * @param Project|null $project * @throws Exception */ public function renderBeforeTestForm(?Project $project = null): void { $json = json_decode($this->getJSON()); $fields = $json->beforeTest; ?>
getJSON()); $fields = $json->duringTest; $dt_now = CarbonImmutable::now(TIMEZONE); $format = "Y-m-d\TH:i:00"; $dt_start = $dt_now->format($format); $dt_end = $dt_now->add(1, "minute")->format($format); ?> pass_stage; } /** * @return bool */ public function hasPassFail(): bool { return !is_null($this->pass_stage); } public function getChallengeStageField(): ?object { return $this->getFields()["challenge_stage"] ?? null; } }