Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 17260 | Rev 17262 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 17260 Rev 17261
Línea 647... Línea 647...
647
    public function uploadAction()
647
    public function uploadAction()
648
    {
648
    {
649
        $request = $this->getRequest();
649
        $request = $this->getRequest();
Línea 650... Línea 650...
650
        
650
        
-
 
651
        $currentUserPlugin = $this->plugin('currentUserPlugin');
-
 
652
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
651
        $currentUserPlugin = $this->plugin('currentUserPlugin');
653
 
652
        $currentUser    = $currentUserPlugin->getUser();
654
        $currentUser    = $currentUserPlugin->getUser();
653
        $currentCompany = $currentUserPlugin->getCompany();
-
 
654
        
-
 
655
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
655
        $currentCompany = $currentUserPlugin->getCompany();
Línea 656... Línea -...
656
        $currentNetwork = $currentNetworkPlugin->getNetwork();
-
 
657
        
656
        $currentNetwork = $currentNetworkPlugin->getNetwork();
658
        $request    = $this->getRequest();
657
        
-
 
658
        $topic_uuid     = $this->params()->fromRoute('topic_uuid');
-
 
659
 
-
 
660
        if(!$currentUser) {
-
 
661
            return new JsonModel([
-
 
662
                'success'   => false,
-
 
663
                'data'   => 'ERROR_USER_NOT_FOUND'
-
 
664
            ]);
-
 
665
        }
-
 
666
 
-
 
667
        if(!$currentCompany) {
-
 
668
            return new JsonModel([
-
 
669
                'success'   => false,
-
 
670
                'data'   => 'ERROR_COMPANY_NOT_FOUND'
-
 
671
            ]);
-
 
672
        }
-
 
673
 
-
 
674
        if(!$currentNetwork) {
-
 
675
            return new JsonModel([
-
 
676
                'success'   => false,
-
 
677
                'data'   => 'ERROR_NETWORK_NOT_FOUND'
-
 
678
            ]);
-
 
679
        }
-
 
680
 
-
 
681
        if(!$topic_uuid) {
-
 
682
            return new JsonModel([
-
 
683
                'success'   => false,
-
 
684
                'data'   => 'ERROR_TOPIC_NOT_FOUND'
-
 
685
            ]);
-
 
686
        }
-
 
687
 
-
 
688
        if(!$request->isPost()) {
-
 
689
            return new JsonModel([
-
 
690
                'success'   => false,
-
 
691
                'data'   => 'ERROR_METHOD_NOT_ALLOWED'
Línea 659... Línea 692...
659
        $topic_uuid     = $this->params()->fromRoute('topic_uuid');
692
            ]);
660
        
693
        }
-
 
694
        
661
        
695
        $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
662
        $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
696
        $topic = $topicMapper->fetchOneByUuid($topic_uuid);
663
        $topic = $topicMapper->fetchOneByUuid($topic_uuid);
697
        
664
        if(!$topic) {
698
        if(!$topic) {
665
            return new JsonModel([
699
            return new JsonModel([
Línea 673... Línea 707...
673
                'success'   => false,
707
                'success'   => false,
674
                'data'   => 'ERROR_UNAUTHORIZED'
708
                'data'   => 'ERROR_UNAUTHORIZED'
675
            ]);
709
            ]);
676
        }
710
        }
Línea 677... Línea -...
677
        
-
 
678
        if($request->isPost()) {
-
 
679
            
711
        
680
            $step = Functions::sanitizeFilterString($this->params()->fromPost('step')); 
-
 
681
            if($step == 'validation') { 
-
 
682
                $userMapper = UserMapper::getInstance($this->adapter);
-
 
683
                $topicUserMapper = MicrolearningTopicUserMapper::getInstance($this->adapter);
-
 
684
 
-
 
685
                $form = new  TopicCustomerUploadForm();
-
 
686
                $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
-
 
687
                
-
 
688
                $form->setData($dataPost);
-
 
689
                
-
 
690
                if($form->isValid()) {
-
 
Línea -... Línea 712...
-
 
712
        $step = Functions::sanitizeFilterString($this->params()->fromPost('step')); 
691
                    $storage = Storage::getInstance($this->config, $this->adapter);
713
 
Línea 692... Línea 714...
692
 
714
        $storage = Storage::getInstance($this->config, $this->adapter);
693
                    $storage->setFiles($dataPost);
715
        $storage->setFiles($request->getFiles()->toArray());
694
 
716
 
695
                    if(!$storage->setCurrentFilename('file')) {
717
        if(!$storage->setCurrentFilename('file')) {
696
                        return new JsonModel([
718
            return new JsonModel([
697
                            'success' => false,
719
                'success' => false,
-
 
720
                'data' => 'ERROR_UPLOAD_FILE'
-
 
721
            ]);
-
 
722
        }
-
 
723
            
698
                            'data' => 'ERROR_UPLOAD_FILE'
724
        if($step == 'validation') { 
699
                        ]);
725
            $userMapper = UserMapper::getInstance($this->adapter);
700
                    }
726
            $topicUserMapper = MicrolearningTopicUserMapper::getInstance($this->adapter);
701
 
727
 
702
                    $tmp_filename = $storage->getTmpFilename();
728
            $tmp_filename = $storage->getTmpFilename();
703
                    $filename =   $storage->getFilename();
729
            $filename =   $storage->getFilename();
704
                    $target_filename = $storage->composePathToFilename(
730
            $target_filename = $storage->composePathToFilename(
705
                        Storage::TYPE_MICROLEARNING_ACCESS_FOR_STUDENTS,
731
                Storage::TYPE_MICROLEARNING_ACCESS_FOR_STUDENTS,
706
                        $topic->uuid,
732
                $topic->uuid,
707
                        $filename
733
                $filename
708
                    );
734
            );
709
                    
735
            
710
                    if(!$storage->putFile($tmp_filename, $target_filename)) {
736
            if(!$storage->putFile($tmp_filename, $target_filename)) {
711
                        return new JsonModel([
737
                return new JsonModel([
712
                            'success' => false,
738
                    'success' => false,
713
                            'data' => 'ERROR_UPLOAD_FILE'
739
                    'data' => 'ERROR_UPLOAD_FILE'
714
                        ]);
740
                ]);
715
                    }
741
            }
716
                    
742
            
717
                   
743
            
718
                    $count_users = 0;
744
            $count_users = 0;
719
                    $users = [];
745
            $users = [];
720
                    $count_errors = 0;
746
            $count_errors = 0;
721
                    $errors = [];
747
            $errors = [];
722
                    
748
            
723
                    try {
749
            try {
724
                        $spreadsheet = IOFactory::load($target_filename);
750
                $spreadsheet = IOFactory::load($target_filename);
725
                        $records = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
751
                $records = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
726
                    } catch (\Exception $e) {
752
            } catch (\Exception $e) {
-
 
753
                return new JsonModel([
-
 
754
                    'success' => false,
-
 
755
                    'data' => 'ERROR_UPLOAD_FILE' . $e->getMessage()
-
 
756
                ]);
-
 
757
            }
-
 
758
            
-
 
759
            $emails = [];
-
 
760
            $row = 0;
-
 
761
            
-
 
762
            foreach($records as $record)
-
 
763
            {
-
 
764
                /*
-
 
765
                A = Nombre	
727
                        return new JsonModel([
766
                B = Apellido	
-
 
767
                C = Email	
-
 
768
                D = Contraseña	
-
 
769
                E = Empresa	
-
 
770
                F = Función	
-
 
771
                G = Grupo	
-
 
772
                H = Institución	
-
 
773
                I = Programa	
-
 
774
                J = Socio	
-
 
775
                K = Sector	
728
                            'success' => false,
776
                L = Tipo de Estudiante	
-
 
777
                M = País	
-
 
778
                N = Es adulto
-
 
779
                */
-
 
780
                $row++;
-
 
781
                
-
 
782
                $first_name = Functions::sanitizeFilterString($record['A']);
-
 
783
                $last_name = Functions::sanitizeFilterString($record['B']);
-
 
784
                $email = trim(filter_var($record['C'], FILTER_SANITIZE_EMAIL));
-
 
785
                $password = Functions::sanitizeFilterString($record['D']);
-
 
786
                
-
 
787
                $company =  isset($record['E']) ? Functions::sanitizeFilterString($record['E']) : '';
-
 
788
                $function = isset($record['F']) ? Functions::sanitizeFilterString($record['F']) : '';
-
 
789
                $group = isset($record['G']) ? Functions::sanitizeFilterString($record['G']) : '';
-
 
790
                $institution = isset($record['H']) ? Functions::sanitizeFilterString($record['H']) : '';
-
 
791
                $program = isset($record['I']) ? Functions::sanitizeFilterString($record['I']) : '';
-
 
792
                $partner = isset($record['J']) ? Functions::sanitizeFilterString($record['J']) : '';
-
 
793
                $sector = isset($record['K']) ? Functions::sanitizeFilterString($record['K']) : '';
729
                            'data' => 'ERROR_UPLOAD_FILE' . $e->getMessage()
794
                $studentType = isset($record['L']) ? Functions::sanitizeFilterString($record['L']) : '';
-
 
795
                $country =  isset($record['M']) ? Functions::sanitizeFilterString($record['M']) : '';
730
                        ]);
796
                $isAdult = isset($record['N']) ? Functions::sanitizeFilterString($record['N']) : '';
-
 
797
                
-
 
798
                if($row == 1) {
-
 
799
                    continue;
-
 
800
                }
731
                    }
801
                
-
 
802
                //||  empty($password)
-
 
803
                if(empty($first_name) || empty($last_name) || !filter_var($email, FILTER_VALIDATE_EMAIL) ) {
-
 
804
                    continue;                            
-
 
805
                }
-
 
806
                
-
 
807
                if(!in_array($email, $emails)) {
Línea 732... Línea -...
732
                    
-
 
733
                    $emails = [];
-
 
734
                    $row = 0;
-
 
735
                    
-
 
736
                    foreach($records as $record)
-
 
737
                    {
-
 
738
                        /*
-
 
739
                        A = Nombre	
-
 
740
                        B = Apellido	
-
 
741
                        C = Email	
-
 
742
                        D = Contraseña	
-
 
743
                        E = Empresa	
-
 
744
                        F = Función	
-
 
745
                        G = Grupo	
-
 
746
                        H = Institución	
-
 
747
                        I = Programa	
-
 
748
                        J = Socio	
-
 
749
                        K = Sector	
-
 
750
                        L = Tipo de Estudiante	
-
 
751
                        M = País	
-
 
752
                        N = Es adulto
-
 
753
                        */
-
 
754
                        $row++;
-
 
755
                        
-
 
756
                        
-
 
757
                        $first_name = Functions::sanitizeFilterString($record['A']);
-
 
758
                        $last_name = Functions::sanitizeFilterString($record['B']);
-
 
759
                        $email = trim(filter_var($record['C'], FILTER_SANITIZE_EMAIL));
-
 
760
                        $password = Functions::sanitizeFilterString($record['D']);
-
 
761
                        
-
 
762
                        $company =  isset($record['E']) ? Functions::sanitizeFilterString($record['E']) : '';
-
 
763
                        $function = isset($record['F']) ? Functions::sanitizeFilterString($record['F']) : '';
-
 
764
                        $group = isset($record['G']) ? Functions::sanitizeFilterString($record['G']) : '';
-
 
765
                        $institution = isset($record['H']) ? Functions::sanitizeFilterString($record['H']) : '';
-
 
766
                        $program = isset($record['I']) ? Functions::sanitizeFilterString($record['I']) : '';
-
 
767
                        $partner = isset($record['J']) ? Functions::sanitizeFilterString($record['J']) : '';
-
 
768
                        $sector = isset($record['K']) ? Functions::sanitizeFilterString($record['K']) : '';
-
 
769
                        $studentType = isset($record['L']) ? Functions::sanitizeFilterString($record['L']) : '';
-
 
770
                        $country =  isset($record['M']) ? Functions::sanitizeFilterString($record['M']) : '';
-
 
771
                        $isAdult = isset($record['N']) ? Functions::sanitizeFilterString($record['N']) : '';
-
 
772
                        
-
 
773
                        if($row == 1) {
-
 
774
                            continue;
-
 
775
                        }
-
 
776
                        
-
 
777
                        //||  empty($password)
-
 
778
                        if(empty($first_name) || empty($last_name) || !filter_var($email, FILTER_VALIDATE_EMAIL) ) {
-
 
779
                            continue;                            
-
 
780
                        }
-
 
781
                        
-
 
782
                        if(!in_array($email, $emails)) {
-
 
783
                            
-
 
784
                            $user = $userMapper->fetchOneByEmail($email);
-
 
785
                            if($user) {
-
 
786
                                $assigned_topics =  $topicUserMapper->fetchCountByCompanyIdAndTopicIdAndUserId($topic->company_id, $topic->id, $user->id);
-
 
787
                            } else {
-
 
788
                                $assigned_topics = 0;
-
 
789
                            }
808
                    $user = $userMapper->fetchOneByEmail($email);
790
                            
809
                    $assigned_topics = $user ? $topicUserMapper->fetchCountByCompanyIdAndTopicIdAndUserId($topic->company_id, $topic->id, $user->id) : 0;
791
                            $count_users++;
810
                    $count_users++;
792
                            
811
                    
793
                            array_push($emails, $email);
812
                    array_push($emails, $email);
794
                            array_push($users, [
813
                    array_push($users, [
795
                                'id' => $count_users,
814
                        'id' => $count_users,
796
                                'first_name' => $first_name,
815
                        'first_name' => $first_name,
797
                                'last_name' => $last_name,
816
                        'last_name' => $last_name,
798
                                'password'  => $password,
817
                        'password'  => $password,
799
                                'email' => $email,
818
                        'email' => $email,
800
                                'assigned_topics' => $assigned_topics, 
819
                        'assigned_topics' => $assigned_topics, 
801
                                'company' => $company, 
820
                        'company' => $company, 
802
                                'function' => $function, 
821
                        'function' => $function, 
803
                                'group' => $group, 
822
                        'group' => $group, 
804
                                'institution' => $institution, 
823
                        'institution' => $institution, 
805
                                'program' => $program,
824
                        'program' => $program,
806
                                'partner' => $partner,
825
                        'partner' => $partner,
807
                                'sector' => $sector, 
-
 
808
                                'studentType' => $studentType,
-
 
809
                                'country' => $country,
-
 
810
                                'isAdult' => $isAdult,
-
 
811
                            ]);
-
 
812
                        } else {
-
 
813
                            $count_errors++;
-
 
814
                            array_push($errors,[
-
 
815
                                'id' => $count_errors,
-
 
816
                                'first_name' => $first_name,
-
 
817
                                'last_name' => $last_name,
-
 
818
                                'password'  => $password,
-
 
819
                                'email' => $email,
-
 
820
                                'status' => 'DUPLICATE IN EXCEL'
-
 
821
                            ]);
-
 
822
                        }
-
 
823
                    }
-
 
824
 
-
 
825
                    // cache the users
-
 
826
                    try {
-
 
827
                        $key = md5($currentUser->id . '-' . $topic->uuid . '-' . $topic->uuid);
-
 
828
                        $this->cache->setItem($key, serialize($users));
-
 
829
                    } catch (\Exception $e) {
-
 
830
                        return new JsonModel([
-
 
831
                            'success' => false,
-
 
832
                            'data' => 'ERROR_CACHE_SET_ITEM' . $e->getMessage()
-
 
833
                        ]);
-
 
834
                    }
-
 
835
 
-
 
836
                    // return the key
-
 
837
                    return new JsonModel([
-
 
838
                        'success' => true,
-
 
839
                        'data' => [
-
 
840
                            'key' => $key,
-
 
841
                            'topic' => $topic->name,
-
 
842
                            'items' => [
-
 
843
                                'ok' => $users,
826
                        'sector' => $sector, 
844
                                'error' => $errors,
827
                        'studentType' => $studentType,
845
                            ],     
828
                        'country' => $country,
846
                        ]
-
 
847
                    ]);
-
 
848
                } else {
-
 
849
                    $messages = [];
-
 
850
                    $form_messages = (array) $form->getMessages();
-
 
851
                    foreach($form_messages  as $fieldname => $field_messages)
-
 
852
                    {
-
 
853
                        
829
                        'isAdult' => $isAdult,
854
                        $messages[$fieldname] = array_values($field_messages);
830
                    ]);
855
                    }
831
                } else {
856
                    
-
 
857
                    return new JsonModel([
-
 
858
                        'success'   => false,
-
 
859
                        'data'   => $messages
-
 
860
                    ]);
-
 
861
                }
-
 
862
            } else if($step == 'process') {
-
 
863
                // get the key from the post
832
                    $count_errors++;
864
                $key = Functions::sanitizeFilterString($this->params()->fromPost('key')); 
-
 
865
                if(!$key) {
-
 
866
                    return new JsonModel([
-
 
867
                        'success' => false,
-
 
868
                        'data' => 'ERROR_CACHE_KEY_EMPTY'
-
 
869
                    ]); 
-
 
870
                }
-
 
871
                // get the value from the cache
-
 
872
                $value = $this->cache->getItem($key);
-
 
873
 
833
                    array_push($errors,[
874
                // if the value is not found, return an error
-
 
875
                if(!$value) {
-
 
876
                    return new JsonModel([
-
 
877
                        'success' => false,
-
 
878
                        'data' => 'ERROR_CACHE_NOT_FOUND'
-
 
879
                    ]); 
-
 
880
                }
-
 
881
                
-
 
882
                // unserialize the value
-
 
883
                $records = unserialize($value);
-
 
884
 
834
                        'id' => $count_errors,
885
                // if the value is not found, return an error
835
                        'first_name' => $first_name,
886
                if(!$records) {
836
                        'last_name' => $last_name,
887
                    return new JsonModel([
837
                        'password'  => $password,
-
 
838
                        'email' => $email,
Línea 888... Línea -...
888
                        'success' => false,
-
 
889
                        'data' => 'ERROR_CACHE_INVALID'
-
 
890
                    ]);
-
 
891
                }
-
 
892
 
-
 
893
                // get the network, company, company follower
-
 
894
                $networkMapper = NetworkMapper::getInstance($this->adapter);
-
 
895
                $companyMapper = CompanyMapper::getInstance($this->adapter);
-
 
896
                $companyFollowerMapper = CompanyFollowerMapper::getInstance($this->adapter);
-
 
897
             
-
 
898
                // get the connection, user, user password, topic user
-
 
899
                $connectionMapper = ConnectionMapper::getInstance($this->adapter);
-
 
900
                $userMapper = UserMapper::getInstance($this->adapter);
-
 
901
                $userPasswordMapper = UserPasswordMapper::getInstance($this->adapter);
-
 
902
                $topicUserMapper = MicrolearningTopicUserMapper::getInstance($this->adapter);
-
 
903
                
-
 
904
                // get the microlearning extend user, microlearning extend user company, microlearning extend user function, microlearning extend user group, microlearning extend user institution, microlearning extend user program, microlearning extend user partner, microlearning extend user sector, microlearning extend user student type, microlearning extend user country
-
 
905
                $microlearningExtendUserMapper = MicrolearningExtendUserMapper::getInstance($this->adapter);
-
 
906
                $microlearningExtendUserCompanyMapper = MicrolearningExtendUserCompanyMapper::getInstance($this->adapter);
-
 
907
                $microlearningExtendUserFunctionMapper = MicrolearningExtendUserFunctionMapper::getInstance($this->adapter);
-
 
908
                $microlearningExtendUserGroupMapper = MicrolearningExtendUserGroupMapper::getInstance($this->adapter);
-
 
909
                $microlearningExtendUserInstitutionMapper = MicrolearningExtendUserInstitutionMapper::getInstance($this->adapter);
-
 
910
                $microlearningExtendUserProgramMapper = MicrolearningExtendUserProgramMapper::getInstance($this->adapter);
-
 
911
                $microlearningExtendUserPartnerMapper = MicrolearningExtendUserPartnerMapper::getInstance($this->adapter);
-
 
912
                $microlearningExtendUserSectorMapper = MicrolearningExtendUserSectorMapper::getInstance($this->adapter);
-
 
913
                $microlearningExtendUserStudentTypeMapper = MicrolearningExtendUserStudentTypeMapper::getInstance($this->adapter);
-
 
914
                $microlearningExtendUserCountryMapper = MicrolearningExtendUserCountryMapper::getInstance($this->adapter);
-
 
915
 
-
 
916
                // get the network default, user default for connection, company for follower
-
 
917
                $networkDefault = $networkMapper->fetchOneByDefault();
-
 
918
                $userDefaultForConnection = $userMapper->fetchOneDefaultForConnection();
-
 
919
                
-
 
920
                $companyForFollower = $companyMapper->fetchOneDefaultForFollowers();
-
 
921
                
-
 
922
                // create the csv
-
 
923
                $csv = "FIRST NAME|LAST NAME|EMAIL|STATUS\r\n";
-
 
924
                
-
 
925
                // get the users processed, users assigned, user ids
839
                        'status' => 'DUPLICATE IN EXCEL'
926
                $users_processed = 0;
-
 
927
                $users_assigned = 0;
-
 
928
                $user_ids = [];
-
 
929
                foreach($records as $record)
-
 
930
                {
-
 
931
                    $first_name = $record['first_name'];
-
 
932
                    $last_name = $record['last_name'];
-
 
933
                    $password = $record['password'];
-
 
934
                    $email = $record['email'];
-
 
935
                    $company = $record['company'];
-
 
936
                    $function = $record['function'];
-
 
937
                    $group = $record['group'];
-
 
938
                    $institution = $record['institution'];
-
 
939
                    $program = $record['program'];
-
 
940
                    $partner = $record['partner'];
-
 
941
                    $sector = $record['sector'];
-
 
942
                    $studentType = $record['studentType'];
-
 
943
                    $country = $record['country'];
-
 
944
                    $isAdult = strtolower(trim( $record['isAdult'])) == User::IS_ADULT_YES;
-
 
945
    
-
 
946
                    $user = $userMapper->fetchOneByEmail($email);
-
 
947
 
-
 
948
                    // if the user is not found, create the user
-
 
949
                    if(!$user) {    
-
 
950
                        // if the password is not empty, create the user
-
 
951
                        if($password) {
-
 
952
                            $password_hash = password_hash($password, PASSWORD_DEFAULT);
-
 
953
                            
-
 
954
                            $user = new User();
-
 
955
                            
-
 
956
                            $user->network_id = $currentNetwork->id;
-
 
957
                            $user->blocked = User::BLOCKED_NO;
-
 
958
                            $user->email_verified = User::EMAIL_VERIFIED_YES;
-
 
959
                            $user->email = $email;
-
 
960
                            $user->first_name = $first_name;
-
 
961
                            $user->last_name = $last_name;
-
 
962
                            $user->password = $password_hash;
-
 
963
                            $user->login_attempt = 0;
-
 
964
                            $user->usertype_id = UserType::USER;
-
 
965
                            $user->status = User::STATUS_ACTIVE;
-
 
966
                            $user->is_adult = $isAdult ? User::IS_ADULT_YES : User::IS_ADULT_NO;
-
 
967
                            
-
 
968
                            $result = $userMapper->insert($user);
-
 
969
                            if($result) {
-
 
970
                                $userPassword = new UserPassword();
-
 
971
                                $userPassword->user_id = $user->id;
-
 
972
                                $userPassword->password = $password_hash;
-
 
973
                                $userPasswordMapper->insert($userPassword);
-
 
974
                                
-
 
975
                                
-
 
976
                                $csv .= "$first_name|$last_name|$email|CREATE USER \r\n";
-
 
977
                            } else {
-
 
978
                                $csv .= "$first_name|$last_name|$email|FAIL CREATE USER \r\n";
-
 
979
                                
-
 
980
                                continue;
-
 
981
                            } 
-
 
982
                            
-
 
983
                        }
-
 
984
                    } else {
-
 
985
                        $user->is_adult = $isAdult ? User::IS_ADULT_YES : User::IS_ADULT_NO;
-
 
986
                        if($user->email_verified == User::EMAIL_VERIFIED_NO || $user->status != User::STATUS_ACTIVE) {
-
 
987
                            $user->email_verified = User::EMAIL_VERIFIED_YES;
-
 
988
                            $user->status != User::STATUS_ACTIVE;
-
 
989
                            
-
 
990
                            
-
 
991
                        }
-
 
992
                        if(!$userMapper->update($user)) {
-
 
993
                            $csv .= "$first_name|$last_name|$email|FAIL UPDATE USER \r\n";
-
 
994
                            continue;
-
 
995
                        }
-
 
996
                            
-
 
997
                        
-
 
998
                    }
-
 
999
                    
-
 
1000
      
-
 
1001
                    $user_id_in_default_network = 0;
-
 
1002
                    if($user->is_adult == User::IS_ADULT_YES) {
-
 
1003
                        
-
 
1004
                        if($currentNetwork->default == Network::DEFAULT_YES) {
-
 
1005
                            
-
 
1006
                            $user_id_in_default_network = $user->id;
-
 
1007
                            
-
 
1008
                            
-
 
1009
                            
-
 
1010
                        } else {
-
 
1011
                            
-
 
1012
                            $userInDefaultNetwork = $userMapper->fetchOneByEmailAndNetworkId($user->email, $networkDefault->id);
-
 
1013
                            if($userInDefaultNetwork) {
-
 
1014
                                $user_id_in_default_network = $userInDefaultNetwork->id;
-
 
1015
                         
-
 
1016
                                if($userInDefaultNetwork->email_verified == User::EMAIL_VERIFIED_NO || $userInDefaultNetwork->status != User::STATUS_ACTIVE) {
-
 
1017
                                    $userInDefaultNetwork->email_verified = User::EMAIL_VERIFIED_YES;
-
 
1018
                                    $userInDefaultNetwork->status != User::STATUS_ACTIVE;
-
 
1019
                                    
-
 
1020
                                    if(!$userMapper->update($userInDefaultNetwork)) {
-
 
1021
                                        $csv .= "$first_name|$last_name|$email|FAIL UPDATE USER IN DEFAULT NETWORK \r\n";
-
 
1022
                                        continue;
-
 
1023
                                    }
-
 
1024
                                }
-
 
1025
                                
-
 
1026
                                
-
 
1027
                            } else {
-
 
1028
                                $userInDefaultNetwork = new User();
-
 
1029
                                $userInDefaultNetwork->network_id = $networkDefault->id;
-
 
1030
                                $userInDefaultNetwork->blocked = User::BLOCKED_NO;
-
 
1031
                                $userInDefaultNetwork->email_verified = User::EMAIL_VERIFIED_YES;
-
 
1032
                                $userInDefaultNetwork->email = $email;
-
 
1033
                                $userInDefaultNetwork->first_name = $first_name;
-
 
1034
                                $userInDefaultNetwork->last_name = $last_name;
-
 
1035
                                $userInDefaultNetwork->password = $password_hash;
-
 
1036
                                $userInDefaultNetwork->login_attempt = 0;
-
 
1037
                                $userInDefaultNetwork->usertype_id = UserType::USER;
-
 
1038
                                $userInDefaultNetwork->status = User::STATUS_ACTIVE;
-
 
1039
                                $userInDefaultNetwork->is_adult = $isAdult == User::IS_ADULT_YES;
-
 
1040
                                $result = $userMapper->insert($userInDefaultNetwork);
-
 
1041
                                if($result) {
-
 
1042
                                    $user_id_in_default_network = $userInDefaultNetwork->id;
-
 
1043
                                    
-
 
1044
                                    
-
 
1045
                                    $userPassword = new UserPassword();
-
 
1046
                                    $userPassword->user_id = $userInDefaultNetwork->id;
-
 
1047
                                    $userPassword->password = $password_hash;
-
 
1048
                                    $userPasswordMapper->insert($userPassword);
-
 
1049
                                    
-
 
1050
                                    $csv .= "$first_name|$last_name|$email|CREATE USER IN DEFAULT NETWORK \r\n";
-
 
1051
                                } else {
-
 
1052
                                    $csv .= "$first_name|$last_name|$email|FAIL CREATE USER IN DEFAULT NETWORK \r\n";
-
 
1053
                                }
-
 
1054
                                
-
 
1055
                            }
-
 
1056
                            
-
 
1057
                            
-
 
1058
                        }
-
 
1059
                    }
-
 
1060
     
-
 
1061
                    if($user_id_in_default_network) {
-
 
1062
                        
-
 
1063
                        if($userDefaultForConnection) {
-
 
1064
                        
-
 
1065
                            $connection = $connectionMapper->fetchOneByUserId1AndUserId2($userDefaultForConnection->id, $user_id_in_default_network);
-
 
1066
                            if($connection) {
-
 
1067
                                if($connection->status != Connection::STATUS_ACCEPTED) {
-
 
1068
                                    $connection->status = Connection::STATUS_ACCEPTED;
-
 
1069
                                    $connectionMapper->update($connection);
-
 
1070
                                    
-
 
1071
                                }
-
 
1072
                                
-
 
1073
                                
-
 
1074
                            } else {
-
 
1075
                                $connection = new Connection();
-
 
1076
                                $connection->request_to = $user_id_in_default_network;
-
 
1077
                                $connection->request_from = $userDefaultForConnection->id;
-
 
1078
                                $connection->status = Connection::STATUS_ACCEPTED;
-
 
1079
                                $connectionMapper->insert($connection);
-
 
1080
                            }
-
 
1081
                        }
-
 
1082
                        
-
 
1083
                        if($companyForFollower) {
-
 
1084
                            $companyFollower = $companyFollowerMapper->fetchOneByCompanyIdAndUserId($companyForFollower->id, $user_id_in_default_network);
-
 
1085
                            if(!$companyFollower) {
-
 
1086
                                $companyFollower = new CompanyFollower();
-
 
1087
                                $companyFollower->company_id = $companyForFollower->id;
-
 
1088
                                $companyFollower->follower_id = $user_id_in_default_network;
-
 
1089
                                
-
 
1090
                                $companyFollowerMapper->insert($companyFollower);
-
 
1091
                                
-
 
1092
                                
-
 
1093
                            }
-
 
1094
                        }
-
 
1095
                        
-
 
1096
                        
-
 
1097
                    }
-
 
1098
                        
-
 
1099
                    
-
 
1100
                    if(!in_array($user->id, $user_ids)) {
-
 
1101
                        array_push($user_ids, $user->id);
-
 
1102
                    }
-
 
1103
                    
-
 
1104
                    /*
-
 
1105
                    echo '$filterCompany = ' . $filterCompany . PHP_EOL;
-
 
1106
                    echo '$filterFunctio = ' . $filterFunction . PHP_EOL;
-
 
1107
                    echo '$filterGroup = ' . $filterGroup . PHP_EOL;
-
 
1108
                    echo '$filterInstitution = ' . $filterInstitution . PHP_EOL;
-
 
1109
                    echo '$filterPartner = ' . $filterPartner . PHP_EOL;
-
 
1110
                    echo '$filterProgram = ' . $filterProgram . PHP_EOL;
-
 
1111
                    echo '$filterSector = ' . $filterSector . PHP_EOL;
-
 
1112
                    echo '$filterStudentType = ' . $filterStudentType . PHP_EOL;
-
 
1113
                  */
-
 
1114
                   
-
 
1115
                    
-
 
1116
                    $extendUser = $microlearningExtendUserMapper->fetchOneByCompanyIdAndUserId($currentCompany->id, $user->id);
-
 
1117
                    if(!$extendUser) {
-
 
1118
                        $extendUser = new MicrolearningExtendUser();
-
 
1119
                        $extendUser->company_id = $currentCompany->id;
-
 
1120
                        $extendUser->user_id = $user->id;
-
 
1121
                    }
-
 
1122
                    
-
 
1123
                    
-
 
1124
                    if($company) {
-
 
1125
                        $record = $microlearningExtendUserCompanyMapper->fetchOneByCompanyIdAndName($currentCompany->id, $company);
-
 
1126
      
-
 
1127
                        if(!$record) {
-
 
1128
                            $record = new MicrolearningExtendUserCompany();
-
 
1129
                            $record->company_id = $currentCompany->id;
-
 
1130
                            $record->name = $company;
-
 
1131
                                    
-
 
1132
                            $microlearningExtendUserCompanyMapper->insert($record);
-
 
1133
                        }
-
 
1134
                    
-
 
1135
 
-
 
1136
                        
-
 
1137
                                
-
 
1138
                        if($record->id) {
-
 
1139
                            $extendUser->extend_company_id = $record->id;
-
 
1140
                        }
-
 
1141
                    }
-
 
1142
                            
-
 
1143
                    if($function) {
-
 
1144
                        $record = $microlearningExtendUserFunctionMapper->fetchOneByCompanyIdAndName($currentCompany->id, $function);
-
 
1145
                        if(!$record) {
-
 
1146
                            $record = new MicrolearningExtendUserFunction();
-
 
1147
                            $record->company_id = $currentCompany->id;
-
 
1148
                            $record->name = $function;
-
 
1149
                                    
-
 
1150
                            $microlearningExtendUserFunctionMapper->insert($record);
-
 
1151
                        }
-
 
1152
                                
-
 
1153
                        if($record->id) {
-
 
1154
                            $extendUser->extend_function_id = $record->id;
-
 
1155
                        }
-
 
1156
                    }
-
 
1157
                            
-
 
1158
                    if($group) {
-
 
1159
                        $record = $microlearningExtendUserGroupMapper->fetchOneByCompanyIdAndName($currentCompany->id, $group);
-
 
1160
                        if(!$record) {
-
 
1161
                            $record = new MicrolearningExtendUserGroup();
-
 
1162
                            $record->company_id = $currentCompany->id;
-
 
1163
                            $record->name = $group;
-
 
1164
                                    
-
 
1165
                            $microlearningExtendUserGroupMapper->insert($record);
-
 
1166
                        }
-
 
1167
                                
-
 
1168
                        if($record->id) {
-
 
1169
                            $extendUser->extend_group_id = $record->id;
-
 
1170
                        }
-
 
1171
                    }
-
 
1172
                            
-
 
1173
                    if($institution) {
-
 
1174
                        $record = $microlearningExtendUserInstitutionMapper->fetchOneByCompanyIdAndName($currentCompany->id, $institution);
-
 
1175
                        if(!$record) {
-
 
1176
                            $record = new MicrolearningExtendUserInstitution();
-
 
1177
                            $record->company_id = $currentCompany->id;
-
 
1178
                            $record->name = $institution;
-
 
1179
                                    
840
                    ]);
1180
                            $microlearningExtendUserInstitutionMapper->insert($record);
-
 
1181
                        }
-
 
1182
                                
-
 
1183
                        if($record->id) {
-
 
1184
                            $extendUser->extend_institution_id = $record->id;
-
 
1185
                        }
-
 
1186
                    }
-
 
1187
                            
-
 
1188
                    if($program) {
-
 
1189
                        $record = $microlearningExtendUserProgramMapper->fetchOneByCompanyIdAndName($currentCompany->id, $program);
-
 
1190
                        if(!$record) {
-
 
1191
                            $record = new MicrolearningExtendUserProgram();
-
 
1192
                            $record->company_id = $currentCompany->id;
-
 
1193
                            $record->name = $program;
-
 
1194
                                    
-
 
1195
                            $microlearningExtendUserProgramMapper->insert($record);
-
 
1196
                        }
-
 
1197
                                
-
 
1198
                        if($record->id) {
-
 
1199
                            $extendUser->extend_program_id = $record->id;
-
 
1200
                        }
-
 
1201
                    }
-
 
1202
                            
-
 
1203
                    if($partner) {
-
 
1204
                        
-
 
1205
                   
-
 
1206
                        $record = $microlearningExtendUserPartnerMapper->fetchOneByCompanyIdAndName($currentCompany->id, $partner);
-
 
1207
                        if(!$record) {
-
 
1208
                            $record = new MicrolearningExtendUserPartner();
-
 
1209
                            $record->company_id = $currentCompany->id;
-
 
1210
                            $record->name = $partner;
-
 
1211
                                    
-
 
1212
                            $microlearningExtendUserPartnerMapper->insert($record);
-
 
1213
                        }
-
 
1214
                                
-
 
1215
                        if($record->id) {
-
 
1216
                            $extendUser->extend_partner_id = $record->id;
-
 
1217
                        }
-
 
1218
                    }
-
 
1219
                            
-
 
1220
                    if($sector) {
-
 
1221
                        $record = $microlearningExtendUserSectorMapper->fetchOneByCompanyIdAndName($currentCompany->id, $sector);
-
 
1222
                        if(!$record) {
-
 
1223
                            $record = new MicrolearningExtendUserSector();
-
 
1224
                            $record->company_id = $currentCompany->id;
-
 
1225
                            $record->name = $sector;
-
 
1226
                                    
-
 
1227
                            $microlearningExtendUserSectorMapper->insert($record);
-
 
1228
                        }
-
 
1229
                                
-
 
1230
                        if($record->id) {
-
 
1231
                            $extendUser->extend_sector_id = $record->id;
-
 
1232
                        }
-
 
1233
                    }
-
 
1234
                            
-
 
1235
                    if($studentType) {
-
 
1236
                        $record = $microlearningExtendUserStudentTypeMapper->fetchOneByCompanyIdAndName($currentCompany->id, $studentType);
-
 
1237
                        if(!$record) {
-
 
1238
                            $record = new MicrolearningExtendUserStudentType();
-
 
1239
                            $record->company_id = $currentCompany->id;
-
 
1240
                            $record->name = $studentType;
-
 
1241
                                    
-
 
1242
                            $microlearningExtendUserStudentTypeMapper->insert($record);
-
 
1243
                        }
-
 
1244
                        if($record->id) {
-
 
1245
                            $extendUser->extend_student_type_id = $record->id;
-
 
1246
                        }
-
 
1247
                    }
-
 
1248
                    
-
 
1249
                    if($country) {
-
 
1250
                        $record = $microlearningExtendUserCountryMapper->fetchOneByCompanyIdAndName($currentCompany->id, $country);
-
 
1251
                        if(!$record) {
-
 
1252
                            $record = new MicrolearningExtendUserCountry();
-
 
1253
                            $record->company_id = $currentCompany->id;
-
 
1254
                            $record->name = $country;
-
 
1255
                            
-
 
1256
                            $microlearningExtendUserCountryMapper->insert($record);
-
 
1257
                        }
-
 
1258
                        if($record->id) {
-
 
1259
                            $extendUser->extend_country_id = $record->id;
-
 
1260
                        }
-
 
1261
                    }
-
 
1262
                    
-
 
1263
 
-
 
1264
                    
-
 
1265
 
-
 
1266
                            
-
 
1267
                    if($extendUser->id) {
-
 
1268
                       $result =   $microlearningExtendUserMapper->update($extendUser);
-
 
1269
                    } else {
-
 
1270
                       $result = $microlearningExtendUserMapper->insert($extendUser);
-
 
1271
                    }
-
 
1272
                
-
 
1273
                
-
 
1274
                   
-
 
1275
                }
-
 
1276
                
-
 
1277
                $notificationMapper = NotificationMapper::getInstance($this->adapter);
-
 
1278
                
-
 
1279
                
841
                }
1280
                $users_processed = 0;
-
 
1281
                $users_previous = 0;
-
 
1282
                foreach($user_ids as $user_id)
-
 
1283
                {
-
 
1284
                    $user = $userMapper->fetchOne($user_id);
-
 
1285
                    $first_name = $user->first_name;
-
 
1286
                    $last_name = $user->last_name;
-
 
1287
                    $email = $user->email;
842
            }
1288
                    
-
 
1289
                    
-
 
1290
                    $users_processed++;
-
 
1291
                    $topicUser = $topicUserMapper->fetchOneByUserIdAndTopicId($user_id, $topic->id);
-
 
1292
                    if($topicUser) {
-
 
1293
                        $users_previous++;
-
 
1294
                        $csv .= "$first_name|$last_name|$email|PREVIOUS FOUND \r\n";
-
 
1295
                    } else {
-
 
1296
                        $topicUser = new MicrolearningTopicUser();
-
 
1297
                        $topicUser->company_id = $topic->company_id;
-
 
1298
                        $topicUser->topic_id = $topic->id;
-
 
1299
                        $topicUser->topic_id = $topic->id;
-
 
1300
                        $topicUser->user_id = $user_id;
-
 
1301
                        $topicUser->access = MicrolearningTopicUser::ACCESS_UNLIMITED;
-
 
1302
                        
-
 
1303
                        if($topicUserMapper->insert($topicUser)) {
-
 
1304
                            
-
 
1305
                            $notification = new Notification();
-
 
1306
                            $notification->company_id = $topic->company_id;
-
 
1307
                            $notification->user_id = $user_id;
-
 
1308
                            $notification->topic_id = $topic->id;
-
 
1309
                            $notification->topic_id = $topic->id;
-
 
1310
                            $notification->type = Notification::TYPE_NEW_MICROLEARNING_CAPSULE;
-
 
1311
                            $notification->message  = 'LABEL_NOTIFICATION_NEW_MICROLEARNING_CAPSULE' . ' : ' . $topic->name;
-
 
1312
                            $notification->url      = '/microlearning/topics/' . $topic->uuid .  '/detail';
-
 
1313
                            
-
 
1314
                            $notificationMapper->insert($notification);
-
 
1315
                            
-
 
1316
                            
-
 
1317
                            $csv .= "$first_name|$last_name|$email|CAPSULE USER ASSIGNED\r\n";
-
 
1318
                            $users_assigned++;  
-
 
1319
                            
-
 
1320
                            
-
 
1321
                            $topicUser = $topicUserMapper->fetchOne($topicUser->id);
-
 
1322
                            if($topicUser) {
-
 
1323
                                $microlearningUserMapper = MicrolearningUserMapper::getInstance($this->adapter);
-
 
1324
                                $microlearningUser = $microlearningUserMapper->fetchOneByUserIdAndCompanyId($topicUser->user_id, $topicUser->company_id);
-
 
1325
                                if($microlearningUser) {
-
 
1326
                                    $microlearningUser->updated_on = $topicUser->updated_on;
-
 
1327
                                    
-
 
1328
                                    $microlearningUserMapper->update($microlearningUser);
-
 
1329
                                    
-
 
1330
                                } else {
-
 
1331
                                    $microlearningUser = new MicrolearningUser();
-
 
1332
                                    $microlearningUser->company_id = $topicUser->company_id;
-
 
1333
                                    $microlearningUser->user_id = $topicUser->user_id;
-
 
1334
                                    $microlearningUser->added_on = $topicUser->added_on;
-
 
1335
                                    $microlearningUser->updated_on = $topicUser->updated_on;
-
 
1336
                                    
-
 
1337
                                    $microlearningUserMapper->insert($microlearningUser);
-
 
1338
                                } 
-
 
1339
                            }
-
 
1340
                        } else {
-
 
1341
                            $csv .= "$first_name|$last_name|$email|CAPSULE USER PREVIOUS \r\n";
-
 
1342
                        }
-
 
1343
                        
-
 
1344
                    }
-
 
1345
                    
-
 
1346
                 
-
 
1347
                    
-
 
1348
                }
-
 
1349
                
-
 
1350
                $users_in_the_topic = $topicUserMapper->fetchCountByCompanyIdAndTopicId($topic->company_id, $topic->id);
-
 
1351
                
-
 
1352
                return new JsonModel([
-
 
1353
                    'success' => true, 
-
 
1354
                    'data' => [
-
 
1355
                        'users_assigned' => $users_assigned, 
-
 
1356
                        'users_processed' => $users_processed,
-
 
1357
                        'users_in_the_topic' => $users_in_the_topic,
-
 
1358
                        'users_previous' => $users_previous,
-
 
1359
                        'csv_base64_content' => base64_encode($csv),
-
 
1360
                        'csv_filename' => 'topic-users-' .date('Y-m-d-h-i-s').  '.csv'
-
 
1361
                    ]
-
 
1362
                ]); 
-
 
1363
                
843
 
1364
                
844
            try {
1365
                
845
                $key = md5($currentUser->id . '-' . $topic->uuid . '-' . $topic->uuid);
1366
                
846
                $this->cache->setItem($key, serialize($users));
1367
            } else {
847
            } catch (\Exception $e) {
1368
                return new JsonModel([
-
 
1369
                    'success' => false,
-
 
Línea -... Línea 848...
-
 
848
                return new JsonModel([
-
 
849
                    'success' => false,
-
 
850
                    'data' => 'ERROR_CACHE_SET_ITEM' . $e->getMessage()
-
 
851
                ]);
-
 
852
            }
-
 
853
 
-
 
854
            return new JsonModel([
-
 
855
                'success' => true,
-
 
856
                'data' => [
-
 
857
                    'key' => $key,
1370
                    'data' => 'ERROR_PARAMETERS_ARE_INVALID'
858
                    'topic' => $topic->name,
1371
                ]); 
859
                    'items' => [
Línea 1372... Línea 860...
1372
            } 
860
                        'ok' => $users,
1373
            
861
                        'error' => $errors,
1374
            
862
                    ],     
1375
 
863
                ]
1376
            
864
            ]);
Línea 1377... Línea 865...
1377
        }
865
        }
1378
        
866