简体   繁体   中英

Get data from array and assign them in Yii Php

First of all im new to yii framework.. Currently i want to write a function to automatically assign short listed submissions user to evaluators so they can mark their submissions profiles.

For example

Total submission users: 20
Total evaluators: 11
Evaluator per submission: 3

I want to get the array of total submission users, and also total evaluators.. then i want to assign submissions automatically to them, Below is something that i want.

submission 1
    Assigned to evaluator 1, evaluator 2, evaluator 3.

submission 2
    Assigned to evaluator 4, evaluator 5, evaluator 6.

submission 3
    Assigned to evaluator 7, evaluator 8, evaluator 9.

submission 4
    Assigned to evaluator 10, evaluator 11, evaluator 1.

submission 5
    Assigned to evaluator 2, evaluator 3, evaluator 4.
.
.

.
submission 20
    Assigned to evaluator 3, evaluator 4, evaluator 5.

and so on until all submissions are evaluated are automatically assigned.. Currently im doing everything manually by going to first evaluator profile, then assign sumission to him, then go to second evaluator and then assign submission to him, it will take lot of time if i have more than 50 evaluators and 200+ submissions that i want automated.

Im new to yii so not sure how to do all that.. here is my code for new function..

 public function actionAutoAssign(){
    $evaluator_list = ApplicantsController::model()->findAllByAttributes(array('user_type'=>'evaluator'));
    $applicants_list = ApplicantsController::model()->findAllByAttributes(array('appl_status'=>'Short listed'));
    $award_id = Awards::model()->actionCurrentAward();
    $status = '';

    $model = new Evaluation();
    $model->setScenario('evalassign');
    foreach($applicants_list as $key=>$val){
        $model->app_id = $val;
        $model->eval_id = $evaluator_list;
        $model->assign_date = date('Y-m-d H:i:s');
        $model->award_id = $award_id;
        if($model->validate()){
            $model2 = null;

            $model2 = HmcApplicants::model()->find('id=:applId',array('applId'=>$val));
            $model2->appl_status = 'Under Evaluation Now';
            $model2->update($val);

            if($model->save(FALSE)){
                $status = 'ok';
            }
        }

    }
}

Im not really sure how to do this in yii, im trying but confused with how to use arrays that im doing above, and how to automatically assign each submission to 3 evaluators.

The resul of a findAllByAttributes is a collection of related models the you should refer to these models by iteration for $applicant_list you do it right then you need somethings equivalent for $evaluater_list (use foreach again or index )

assuming the $evaluator_list containt the eval_id in a field named id you can do random eg: with array_rand this way

    public function actionAutoAssign(){
        $evaluator_list = ApplicantsController::model()->findAllByAttributes(array('user_type'=>'evaluator'));
        $applicants_list = ApplicantsController::model()->findAllByAttributes(array('appl_status'=>'Short listed'));
        $award_id = Awards::model()->actionCurrentAward();
        $status = '';

        $model = new Evaluation();
        $model->setScenario('evalassign');
        foreach($applicants_list as $key=>$val){
            $model->app_id = $val;
            for ($cnt = 0; $cnt<2; $cnt++){     
                $model->eval_id = array_rand($evaluator_list)->id ;
            }
            $model->assign_date = date('Y-m-d H:i:s');
            $model->award_id = $award_id;
            if($model->validate()){
                $model2 = null;

                $model2 = HmcApplicants::model()->find('id=:applId',array('applId'=>$val));
                $model2->appl_status = 'Under Evaluation Now';
                $model2->update($val);

                if($model->save(FALSE)){
                    $status = 'ok';
                }
            }

        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM