Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 154
V3ToV4DataMigrationService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 6
992.00
0.00% covered (danger)
0.00%
0 / 154
 __construct
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 3
 __clone
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 2
 getInstance
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 5
 migrateFromV3ToV4
0.00% covered (danger)
0.00%
0 / 1
380.00
0.00% covered (danger)
0.00%
0 / 114
 custom_array_diff
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 5
 updateBatchMember
0.00% covered (danger)
0.00%
0 / 1
56.00
0.00% covered (danger)
0.00%
0 / 25
<?php
namespace com\linways\core\ams\professional\service\v3Tov4Migration;
use com\linways\core\ams\professional\exception\ProfessionalException;
use stdClass;
use com\linways\base\util\SecurityUtils;
use com\linways\core\ams\professional\service\SemesterService;
use com\linways\core\ams\professional\service\BaseService;
use com\linways\core\ams\professional\service\v3Tov4Migration\SemesterServiceTest;
use com\linways\core\ams\professional\service\v3Tov4Migration\V3MigrationService;
class V3ToV4DataMigrationService extends BaseService
{
    // /Condition 1 - Presence of a static member variable
    private static $_instance = null;
    private $mapper = [];
    // /Condition 2 - Locked down the constructor
    private function __construct()
    {
        $this->mapper = null;
    }
    // Prevent any oustide instantiation of this class
    // /Condition 3 - Prevent any object or instance of that class to be cloned
    private function __clone()
    {
    }
    // Prevent any copy of this object
    // /Condition 4 - Have a single globally accessible static method
    public static function getInstance()
    {
        if (!is_object(self::$_instance)) // or if( is_null(self::$_instance) ) or if( self::$_instance == null )
            self::$_instance = new self();
        return self::$_instance;
    }
    public function migrateFromV3ToV4($request)
    {
        try {
            ini_set('memory_limit', '-1');
            ini_set('max_execution_time', 0); // Set max_execution_time to unlimited (or a very high value)
            // $currentTimestamp = date('Y-m-d H:i:s');
            $currentTimestamp = $request->dateTime;
            // if(empty($dateTime)){
            //     $dateTime = date('Y-m-d H:i:s');
            // }
            // $currentTimestamp = "2024-08-01 12:12:05";
            
            /***********************************BEGINING OF BATCH RELATION**************************************************/
            echo("\n*********************Creating Batch Relation*********************************");
            $batchCondition = "";
            $batches = V3MigrationService::getInstance()->getAllDetailsFromRequestTable('','batches', $batchCondition);
            if($batches){
                V4MigrationService::getInstance()->saveV4InsertScript($batches, 'batches');
            }
            echo("\n*********************End Batch Relation*********************************");
            /***********************************END OF BATCH RELATION**************************************************/
            /***********************************BEGINING OF STAFF RELATION**************************************************/
            echo("\n*********************Creating Staff Relation*********************************");
            $subjectCondition = "";
            $hr_designations = V3MigrationService::getInstance()->getAllDetailsFromRequestTable('','hr_designations', $subjectCondition);
            if($hr_designations){
                V4MigrationService::getInstance()->saveV4InsertScript($hr_designations, 'hr_designations');
            }
            
            $studentWhereCondition = "";
            // $studentWhereCondition = " WHERE (staffCreateDate >= '$currentTimestamp' OR updated_date >= '$currentTimestamp')";
            $V3Students = V3MigrationService::getInstance()->getAllDetailsFromRequestTable('','staffaccounts', $studentWhereCondition);
            if($V3Students){
                V4MigrationService::getInstance()->saveV4InsertScript($V3Students, 'staffaccounts');
            }
            
            echo("\n*********************End Staff Relation*********************************");
            /***********************************END OF STAFF RELATION*********************************************************/
                
            /***********************************BEGINING OF STUDENT RELATION**************************************************/
            echo("\n*********************Creating Student Relation*********************************");
            //v3 batch change check
            $studentWhereCondition = " WHERE studentLastupdate >= '$currentTimestamp'";
            $V3UpdatedStudents = V3MigrationService::getInstance()->getAllDetailsFromRequestTable("studentID, CONCAT('B-',batchID,'-S-',studentID) as batchStudentId",'studentaccount', $studentWhereCondition);
            if( $V3UpdatedStudents ){
                $updatedStudentIds = array_column($V3UpdatedStudents, 'studentID');
                $studentWhereCondition = "  WHERE studentID IN (".implode(',', $updatedStudentIds).")";
                $V4UpdatedStudents = V4MigrationService::getInstance()->getAllDetailsFromRequestTable("studentID, CONCAT('B-',batchID,'-S-',studentID) as batchStudentId",'studentaccount', $studentWhereCondition);
                $v3UpdatedBatchStudentList = $this->custom_array_diff($V3UpdatedStudents, $V4UpdatedStudents,'batchStudentId');
                if($v3UpdatedBatchStudentList){
                    $this->updateBatchMember($v3UpdatedBatchStudentList, 0) ;
                }
            }
            //add v3 student details to v4
            $studentWhereCondition = " WHERE (studentCreateTime >= '$currentTimestamp' OR studentLastupdate >= '$currentTimestamp')";
            $V3Students = V3MigrationService::getInstance()->getAllDetailsFromRequestTable('','studentaccount', $studentWhereCondition);
            if($V3Students){
                V4MigrationService::getInstance()->saveV4InsertScript($V3Students, 'studentaccount');
            }
            //identifying deleted students from v4
            $condtion = '';
            $V3StudentIds = V3MigrationService::getInstance()->getAllDetailsFromRequestTable('studentID', 'studentaccount',$condtion);
            $V4StudentIds = V4MigrationService::getInstance()->getAllDetailsFromRequestTable('studentID', 'studentaccount',$condtion);
            $v3DeletedStudentList = $this->custom_array_diff($V3StudentIds, $V4StudentIds,'studentID');
            if($v3DeletedStudentList){
                $this->updateBatchMember($v3DeletedStudentList, 1) ;
            }
            //adding student program account
            V4MigrationService::getInstance()->insertStudentProgramAccount($currentTimestamp);
            //adding student batch members
            V4MigrationService::getInstance()->insertBatchGroupMembers($currentTimestamp);
            echo("\n*********************End Student Relation*********************************");
            /***********************************END OF STUDENT RELATION**************************************************/
            
            /***********************************BEGINING OF SUBJECT RELATION**************************************************/
            echo("\n*********************Create Subject Relation*********************************");
            $subjectCondition = " WHERE (created_date >= '$currentTimestamp' OR updated_date >= '$currentTimestamp')";
            $v3Subjects = V3MigrationService::getInstance()->getAllDetailsFromRequestTable('','subjects', $subjectCondition);
            if($v3Subjects){
                V4MigrationService::getInstance()->saveV4InsertScript($v3Subjects, 'subjects');
            }
            $subjectCondition = " WHERE (subjectCreateDate >= '$currentTimestamp' OR updated_date >= '$currentTimestamp')";
            $v3pseudosubjects = V3MigrationService::getInstance()->getAllDetailsFromRequestTable('','pseudosubjects', $subjectCondition);
            if($v3pseudosubjects){
                V4MigrationService::getInstance()->saveV4InsertScript($v3pseudosubjects, 'pseudosubjects');
            }
            $subjectCondition = " WHERE (createdDate >= '$currentTimestamp' OR updatedDate >= '$currentTimestamp')";
            $v3sbs_relation = V3MigrationService::getInstance()->getAllDetailsFromRequestTable('','sbs_relation', $subjectCondition);
            if($v3sbs_relation){
                V4MigrationService::getInstance()->saveV4InsertScript($v3sbs_relation, 'sbs_relation');
            }
            $subjectCondition = " WHERE (created_date >= '$currentTimestamp' OR updated_date >= '$currentTimestamp')";
            $v3pseudosubjectsSbs = V3MigrationService::getInstance()->getAllDetailsFromRequestTable('','pseudosubjects_sbs', $subjectCondition);
            if($v3pseudosubjectsSbs){
                V4MigrationService::getInstance()->saveV4InsertScript($v3pseudosubjectsSbs, 'pseudosubjects_sbs');
            }
          
            $subjectCondition = " WHERE (created_date >= '$currentTimestamp' OR updated_date >= '$currentTimestamp')";
            $v3pseudosubjectsStudent = V3MigrationService::getInstance()->getAllDetailsFromRequestTable('','pseudosubjects_students', $subjectCondition);
            if($v3pseudosubjectsStudent){
                V4MigrationService::getInstance()->saveV4InsertScript($v3pseudosubjectsStudent, 'pseudosubjects_students');
            }
            $subjectCondition = " WHERE (failedDate >= '$currentTimestamp' OR updated_date >= '$currentTimestamp')";
            $v3FailedStudents = V3MigrationService::getInstance()->getAllDetailsFromRequestTable('','failed_students', $subjectCondition);
            if($v3FailedStudents){
                V4MigrationService::getInstance()->saveV4InsertScript($v3FailedStudents, 'failed_students');
            }
            $v3SubBatches = V3MigrationService::getInstance()->getAllDetailsFromRequestTable('subbatchID','subbatches', $batchCondition);
            $v4SubBatches = V4MigrationService::getInstance()->getAllDetailsFromRequestTable('subbatchID','subbatches', $batchCondition);
            $v3SubBatchList = $this->custom_array_diff($v4SubBatches, $v3SubBatches,'subbatchID');
            $batchIds = array_column($v3SubBatchList, 'subbatchID');
            if( $batchIds ){
                $batchCondition = " WHERE subbatchID IN (".implode(',', $batchIds).")";
                $v3SubBatches = V3MigrationService::getInstance()->getAllDetailsFromRequestTable('','subbatches', $batchCondition);
                if($v3SubBatches){
                    V4MigrationService::getInstance()->saveV4InsertScript($v3SubBatches, 'subbatches');
                }
            }
           
            $subjectCondition = " WHERE (created_date >= '$currentTimestamp' OR updated_date >= '$currentTimestamp')";
            $v3SubBatchSbs = V3MigrationService::getInstance()->getAllDetailsFromRequestTable('','subbatch_sbs', $subjectCondition);
            if($v3SubBatchSbs){
                V4MigrationService::getInstance()->saveV4InsertScript($v3SubBatchSbs, 'subbatch_sbs');
            }
            $subjectCondition = " WHERE (created_date >= '$currentTimestamp' OR updated_date >= '$currentTimestamp')";
            $v3SubBatchStudent = V3MigrationService::getInstance()->getAllDetailsFromRequestTable('','subbatch_student', $subjectCondition);
            if($v3SubBatchStudent){
                V4MigrationService::getInstance()->saveV4InsertScript($v3SubBatchStudent, 'subbatch_student');
            }
            V4MigrationService::getInstance()->insertCurriculumDetails($currentTimestamp);
            V4MigrationService::getInstance()->insertStaffRelation($currentTimestamp);
            V4MigrationService::getInstance()->insertV4Subjects($currentTimestamp);
            V4MigrationService::getInstance()->insertClusterRelation($currentTimestamp);
            V4MigrationService::getInstance()->insertStudentProgramRelation($currentTimestamp);
            echo("\n*********************End Subject Relation*********************************");
            echo"\n\ncompleted all migrations";
            /***********************************END OF SUBJECT RELATION**************************************************/
        } catch (ProfessionalException $e) {
             throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    public function custom_array_diff($arrayV3, $arrayV4, $column) {
        $studentIDs = array_column($arrayV3, $column);
        $diff = array_filter($arrayV4, function ($value) use ($studentIDs, $column) {
            return !in_array($value->$column, $studentIDs);
        });
    
        return $diff;
    }
    public function updateBatchMember($studentList, $isdelete) {
        $deletedStudentIds = array_column($studentList, 'studentID');
        if($deletedStudentIds){
            $studentWhereCondition = " WHERE studentID IN (".implode(',', $deletedStudentIds).")";
            $V4DeletedStudentDetails = V4MigrationService::getInstance()->getAllDetailsFromRequestTable('studentID, batchID', 'studentaccount',$studentWhereCondition);
            $gmUpdateId = [];
            foreach($V4DeletedStudentDetails as $deletedStudentDetails){
                $gmInnerJoin = " group_members gm INNER JOIN studentaccount s ON s.studentID = gm.student_id INNER JOIN batches b ON b.batchID = s.batchID ";
                $gmWhereCond = " WHERE b.batchID = $deletedStudentDetails->batchID AND s.studentID = $deletedStudentDetails->studentID";
                $gmExists = V4MigrationService::getInstance()->getAllDetailsFromRequestTable('gm.id', $gmInnerJoin, $gmWhereCond);
                if ($gmExists){
                    $gmUpdateId[] = reset($gmExists)->id;
                }
            }
            if($gmUpdateId){
                $gmSetvalue = " SET properties = JSON_SET(properties,'$.academicStatus','REMOVED') ";
                $gmCondStr = is_array($gmUpdateId) ? "'" . implode("','",$gmUpdateId) . "'" : "'".$gmUpdateId."'";
                $gmWhereCond = " WHERE id IN ($gmCondStr";
                $gmExists = V4MigrationService::getInstance()->updateDetailsByRequestTable($gmSetvalue, 'group_members', $gmWhereCond);
            }
            if( $isdelete ){
                V4MigrationService::getInstance()->deleteRowByRequestTable('studentaccount', $studentWhereCondition);
            }
        }
        return true;
    }
    
    
}