Viewing File: /home/assersoft/public_html/audiogramnew/app/Controllers/AudiogramTestsController.php

<?php

namespace App\Controllers;

use App\Models\PatientsModel;
use App\Models\PureToneAudiometryModel;
use App\Models\SpeechTestsModel;
use App\Validation\AudiogramRules;
use App\Validation\ClinicRules;
use App\Validation\PatientRules;
use App\Validation\PureToneAudiometryRules;
use App\Validation\SpeechTestRules;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\RESTful\ResourceController;

class AudiogramTestsController extends ResourceController
{

    protected $modelName               = 'App\Models\AudiogramTestsModel';
    protected $format                  = 'json';
    protected $pureToneAudiometryModel;
    protected $speechTestModel;
    protected $rules;
    protected $pureToneAudiometryRules;
    protected $speechTestRules;
    protected $patientRules;
    protected $patientsModel;
    protected $clinicRules;

    public function __construct()
    {
        $this->pureToneAudiometryModel = new PureToneAudiometryModel();
        $this->speechTestModel         = new SpeechTestsModel();
        $this->rules                   = new AudiogramRules();
        $this->pureToneAudiometryRules = new PureToneAudiometryRules();
        $this->speechTestRules         = new SpeechTestRules();
        $this->patientRules            = new PatientRules();
        $this->patientsModel           = new PatientsModel();
        $this->clinicRules             = new ClinicRules();
    }

    /**
     * Return an array of resource objects, themselves in array format.
     *
     * @return ResponseInterface
     */
    public function index()
    {
        if(!$this->validate($this->patientRules->patientId)) {
            return $this->failValidationErrors($this->validator->getErrors());
        }

        $patientId = $this->request->getVar('patient_id');
        $patient = $this->patientsModel->find($patientId);
        if($patient === null) {
            return $this->failNotFound('Patient not found');
        }

        $audiogramTest = $this->model->where('patient_id', $patientId)->findAll();
        return $this->respond($audiogramTest);

    }

    /**
     * Return the properties of a resource object.
     *
     * @param int|string|null $id
     *
     * @return ResponseInterface
     */
    public function show($id = null)
    {
        if($id === null) {
            return $this->fail('Audiogram Test ID is required');
        }

        $audiogramTest = $this->model->find($id);
        if($audiogramTest === null) {
            return $this->failNotFound('Audiogram Test not found');
        }

        $pureTone = $this->pureToneAudiometryModel->where('audiogramtest_id', $id)->findAll();
        if(!$pureTone) {
            return $this->failNotFound('Pure Tone Data not found');
        }
        $speechTest = $this->speechTestModel->where('audiogramtest_id', $id)->findAll();

        $audiogramTest['pureTone'] = $pureTone;
        $audiogramTest['speech'] = $speechTest;

        return $this->respond($audiogramTest);
    }

    /**
     * Create a new resource object, from "posted" parameters.
     *
     * @return ResponseInterface
     */
    public function create()
    {
        if(!$this->validate($this->rules->createAudiogram)) {
            return $this->failValidationErrors($this->validator->getErrors());
        }

        $audiogramData = (array) $this->request->getVar();

        $this->model->insert($audiogramData);
        $audiogramId = $this->model->getInsertID();
        if(!$audiogramId) {
            return $this->failServerError('Failed to create audiogram test');
        }
        $audiogramData['audiogramtest_id'] = $audiogramId;

        $pureToneData = (array) $audiogramData['puretone_audiometry'];

        $includeSpeechData = false;
        $speechData = [];
        if(array_key_exists(
            'include_speech_audiometry',
            $audiogramData
        )) {
            $includeSpeechData = $audiogramData['include_speech_audiometry'];
            $speechData = (array) $audiogramData['speech_audiometry'];
        }
 
        $pureToneTests = [];
        foreach($pureToneData as &$pureToneTest) {
            $pureToneTests[] = [
                'audiogramtest_id' => $audiogramId,
                'test_no' => $pureToneTest->test_no,
                'ear' => $pureToneTest->ear,
                'frequency_125' => $pureToneTest->frequency_125,
                'frequency_250' => $pureToneTest->frequency_250,
                'frequency_500' => $pureToneTest->frequency_500,
                'frequency_1000' => $pureToneTest->frequency_1000,
                'frequency_2000' => $pureToneTest->frequency_2000,
                'frequency_4000' => $pureToneTest->frequency_4000,
                'frequency_8000' => $pureToneTest->frequency_8000,
            ];
        }
        $this->pureToneAudiometryModel->insertBatch($pureToneTests);

        if($includeSpeechData) {
            $speechTest = [];
            // $audiogramData['audiogram_id'] = $audiogramId;
            $speechTest['audiogramtest_id'] = $audiogramId;
            $speechTest['frequency_10'] = $speechData[0];
            $speechTest['frequency_20'] = $speechData[1];
            $speechTest['frequency_30'] = $speechData[2];
            $speechTest['frequency_40'] = $speechData[3];
            $speechTest['frequency_50'] = $speechData[4];
            $speechTest['frequency_60'] = $speechData[5];
            $speechTest['frequency_70'] = $speechData[6];
            $speechTest['frequency_80'] = $speechData[7];
            $speechTest['frequency_90'] = $speechData[8];
            $speechTest['frequency_100'] = $speechData[9];
            $speechTest['frequency_110'] = $speechData[10];
            $speechTest['frequency_120'] = $speechData[11];

            $this->speechTestModel->insert($speechTest);
        }

        return $this->respondCreated($audiogramData);
    }

    /**
     * Add or update a model resource, from "posted" properties.
     *
     * @param int|string|null $id
     *
     * @return ResponseInterface
     */
    public function update($id = null)
    {
        if($id === null) {
            return $this->fail('Audiogram Test ID is required');
        }

        if(!$this->validate($this->rules->updateAudiogram)) {
            return $this->failValidationErrors($this->validator->getErrors());
        }

        $audiogram = $this->model->find($id);
        if($audiogram === null) {
            return $this->failNotFound('Audiogram Test not found');
        }

        $audiogramData = (array) $this->request->getVar();

        $audiogramId = $this->model->update($id, $audiogramData);
        if($audiogramId === false) {
            return $this->fail('Failed to update audiogram test');
        }

        $pureToneData = (array) $audiogramData['puretone_audiometry'];
        $speechData = (array) $audiogramData['speech_audiometry'];
        $includeSpeechData = $audiogramData['include_speech_audiometry'];

        $pureToneTests = [];
        foreach ($pureToneData as &$pureToneTest) {
            $pureToneTests[] = [
                'puretone_id' => $pureToneTest->puretone_id,
                'test_no' => $pureToneTest->test_no,
                'ear' => $pureToneTest->ear,
                'frequency_125' => $pureToneTest->frequency_125,
                'frequency_250' => $pureToneTest->frequency_250,
                'frequency_500' => $pureToneTest->frequency_500,
                'frequency_1000' => $pureToneTest->frequency_1000,
                'frequency_2000' => $pureToneTest->frequency_2000,
                'frequency_4000' => $pureToneTest->frequency_4000,
                'frequency_8000' => $pureToneTest->frequency_8000,
            ];
        }
        
        $this->pureToneAudiometryModel->updateBatch($pureToneTests, 'puretone_id');

        if ($includeSpeechData) {
            $speechTest = [];
            
            $speechTest['frequency_10'] = $speechData[0] ?? null;
            $speechTest['frequency_20'] = $speechData[1] ?? null;
            $speechTest['frequency_30'] = $speechData[2] ?? null;
            $speechTest['frequency_40'] = $speechData[3] ?? null;
            $speechTest['frequency_50'] = $speechData[4] ?? null;
            $speechTest['frequency_60'] = $speechData[5] ?? null;
            $speechTest['frequency_70'] = $speechData[6] ?? null;
            $speechTest['frequency_80'] = $speechData[7] ?? null;
            $speechTest['frequency_90'] = $speechData[8] ?? null;
            $speechTest['frequency_100'] = $speechData[9] ?? null;
            $speechTest['frequency_110'] = $speechData[10] ?? null;
            $speechTest['frequency_120'] = $speechData[11] ?? null;

            try {
            if($this->speechTestModel->where('audiogramtest_id', $id)->countAllResults() === 0) {
                $speechTest['audiogramtest_id'] = $id;
                $this->speechTestModel->insert($speechTest);
            } else {
                $this->speechTestModel->where('audiogramtest_id', $id)->set($speechTest)->update();
            }
            } catch (\Exception $e) {
                return $this->failServerError('Failed to update speech test');
            }
        } else {
            $this->speechTestModel->where('audiogramtest_id', $id)->delete();
        }

        return $this->respondUpdated($audiogramData);
    }

    /**
     * Delete the designated resource object from the model.
     *
     * @param int|string|null $id
     *
     * @return ResponseInterface
     */
    public function delete($id = null)
    {
        if($id === null) {
            return $this->fail('Audiogram Test ID is required');
        }

        $audiogram = $this->model->find($id);
        if($audiogram === null) {
            return $this->failNotFound('Audiogram Test not found');
        }

        $this->pureToneAudiometryModel->where('audiogramtest_id', $id)->delete();
        $this->speechTestModel->where('audiogramtest_id', $id)->delete();

        $this->model->delete($id);

        return $this->respondDeleted(['id' => $id]);
    }

    public function getAudiogramTestsCount() {
        $clinicId = $this->request->getVar('clinic_id');
        if(!$clinicId) {
            return $this->fail('Clinic ID is required');
        }
        $audiogramTestsCount = $this->model->where('clinic_id', $clinicId)->countAllResults();
        return $this->respond(['count' => $audiogramTestsCount]);
    }
}
Back to Directory File Manager