检查值是否为x并存在,如果全部存在,则标记为完成

时间:2018-10-08 20:24:51

标签: php laravel for-loop if-statement eloquent

我认为我正在考虑这个过程,以至于我迷失了自己。

我有员工参加的课程。员工完成课程后,每个课程都标记为已完成。

我正在尝试检查员工是否已完成所有课程,如果所有课程都已完成,则将课程标记为已完成。

到目前为止我所拥有的。

  1. 查询课程。

  2. 查询员工完成的课程。

我坚持检查每门课程是否完成了该课程并返回true或false。

public function lessoncomplete(Request $request)
{
    //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_lessons = Lesson::where('course_id', $course_id)->get();

//获取用户已完成的课程

    $user_lessons = LessonLog::where('user_id', $user_id)->where('course_id', $course_id)->get();

//检查该课程的每节课对用户是否完整。

foreach($ course_lesson as $ lesson){     $ completed = false;

foreach ($user_lessons as $user_lesson) {
    if ($user_lesson->lesson_id == $lesson->id) {
        $completed = true;
        break;
    }
}

//If $completed is still false, then the user has not completed the course
dd($completed);

}

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

2 个答案:

答案 0 :(得分:0)

我假设if (process.client) { require('bootstrap') const $ = require('jquery') ..some jquery and bootstrap code specific to a theme. } 将包含所有已完成的课程?如果是这样,您可以循环浏览与该课程相关的课程,并在课程日志中查看哪些ID匹配。如果它们都匹配,则说明课程已完成。

示例:

LessonLog

答案 1 :(得分:0)

    //check if each of the lessons for the course are complete for the user.

  foreach ($course_lessons as $lesson) {
    $completed = false;

    if($user_lessons->isEmpty()){
    $completed= false;
    }
    else{

    foreach ($user_lessons as $user_lesson) {
        if ($user_lesson->lesson_id == $lesson->id) {
            $completed = true;
            break;
        }else{
            $completed= false;
            break;
        }
    }
}
    //If $completed is still false, then the user has not completed the course
    $complete = new CourseCompletions;

    $complete->user_id = $user_id;
    $complete->course_id = $course_id;
    $complete->added_by = '99999';

    $complete->save();
}

我看到我遗漏了检查是否没有价值的地方。我尝试了几种方法来完成它。我想我在说错话。