lundi 8 octobre 2018

Check if value is x and exists and then mark complete if all exist

I believe I am over thinking this process to the point I have lost myself.

I have courses which employees take. Each lesson is marked completed when the employee finishes the lesson.

I am trying to check if the employee has completed all course lessons and then mark the course as completed if all lessons have completed.

What I have so far.

  1. Query for course lessons.

  2. Query for completed lesson from employee.

I'm stuck on checking for each course lesson has the completed the lesson and returning a true or false value.

    <?php
//LessonLog::create(request()->all());

$user_id=$request->user;
$lesson_id=$request->lesson;
$course_id = $request->course;
$lessonLog = new LessonLog;
$lessonLog->user_id = $user_id;
$lessonLog->course_id = $course_id;
$lessonLog->lesson_id = $lesson_id;
$lessonLog->save();

//get course lessons
$course_lesson = Lesson::find($course_id);

//get the lessons the user has completed

$user_lessons = LessonLog::where('user_id', $user_id);

//check if each of the lessons for the course are complete for the user.
foreach($course_lesson as $lesson){
    //here is where I am stuck determining which process to use to check the results.
    //There is the possibility that the user has not lessons for the course in the log
    //due to it is only logged when the user completes the lesson. 

    if($user_lessons) {

        //Check if user has a corresponding completion for each course lesson available.

    } else {
        $completed = False;
    }


}

//if all is good mark course as completed.

if($completed = True){
    $course_complete = new CourseCompletion;

    $course_complete->course_id = $course_id;
    $course_complete->user_id = $user_id;

    $course_complete->save();
}

return response()->json(['success'=>1]);

?>

Aucun commentaire:

Enregistrer un commentaire