samedi 19 mars 2016

how to calculate score in laravel 5.1

i am very new to Laravel.

I have three tables named as applicants, questions,answers:

Applicants

id jobid name 
1   151  ABC
2   151  DEF

Answers

id qid jobid aaplicantid answer
1   21  151     1         1
2   22  151     1         0
3   21  151     2         1
4   22  151     2         1

questions

id jobid answer
21 151   1
22 151   1

Here i want calculate the % of score of the applicants based on questions ans answers table just assume for one job there two questions if the applicant answers match with question table answer then he will get 100% score if any one is not matched that much will be substracted from the score.

I want the score should be divided like e.g : one job has 5 questions

score=>100/count(questions)=20 for each matched answer

for jobid 151 the applicants list should be as follows

id name score joid
1  ABC  50%   151
2  DEF  100%  151

And i am using the following query but its not giving expected results.

$apps=applicants::where('jobid','=',$jobId)->get();

    foreach($apps as $apo){
        $appid[]=$apo->id;
    }


    $score=answers::whereIn('appid',$appid)->where('jobid','=',$jobId)->get();

    $qid=array();
    foreach($score as $s){
        $qid[]=$s->qid;
    }
    $queid=questions::whereIn('id',$qid)->where('jobid','=',$jobId)->get();

    $calscore=100/(count($queid));


     $key = 0 ;   $finalscore = array();$sss = 0;
    for($n = 0; $n< count($queid); $n++) {
        foreach($queid as $que){
            $quei[]=$que->id;
            $weight[]=$que->weight;
            foreach ($score as $s){
                $qid[]=$s->qid;
                $sc[]=$s->answer;
                if($que->id==$s->qid){

                    if($weight==$sc){
                    $finalscore=$sss+$calscore;
                    $sss++;

                    }


                }
            }

            $key++;

        }
    }

    dd($finalscore);

and even i tried with join query its not giving the proper results.will this can be possible with eloquent model .

Can somebody please help me out to solve this.

thank you .



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire