tp5.1 學生掃碼選座做爲單獨功能實現

前言

本功能目的是將學生掃碼選座功能做爲一個單獨的功能實現,教師不用登錄就能夠實現查看學生選定座位狀況,教師又能夠登錄綁定課程,統計學生本課程簽到次數。老師不須要繁瑣的註冊登錄就能夠實現部分功能,也可使用本產品創建課程進行綁定,從而利用產品統計學生簽到次數。這將大大增長老師對本產品的體驗,有效增長用戶總數。
本文章具體講學生掃碼功能實現,其餘再也不具體講述。session

前期準備

1.首先將每一個教室的每一小節創建一個表,這裏稱做classroom_time,這些數據應該在增長教室字段時自動生成,以天天11個小節爲例,每一個教室生成11個classroom_time字段,如圖。
image.pngthis

2.每一個座位應該也要存入一個字段用於保存它的行列數,學生id和所對應的classroom_time_id用於保存它是哪一個教室的哪一個小節的座位。咱們在這裏稱之爲seattable,初始爲0條數據。
image.png
3.再創建一個網頁用於顯示一個classroom_time的座位表
image.png
4.每一個座位應該對應一個二維碼,url傳值這個教室id,行列數,同時查看座位表應該有一個單獨的二維碼,不用登陸直接顯示學生選座狀況。
image.png
咱們學生掃碼功能主要是對seattable表數據進行操做。url

學生掃碼功能實現

1.經過url獲取這個座位的基本信息
經過掃碼所傳入的url,獲取這個座位的行列號,classroom_id,也要經過靜態方法獲取student_id和第幾小節數,小節數這裏稱爲time。同時經過第幾小節和教室id查詢惟一一個classroom_time.spa

public function entercourse()
    {   
        $id = $this->request->param('id');
        
        $classroom_id = substr($id,0,4)*1;
        $row = substr($id,4,2)*1;
        $column = substr($id,6,2)*1;
        $time = Term::littleClass();
        if ($time<=0 || $time>11) {
            return $this->error('上課時間已結束', url('/index/student/page'));
        }
        $student_id = session('studentId');
        $classroom_time = Classroom_time::where('classroom_id',$classroom_id)->where('littleclass',$time)->find();
        $seattable = Seattable::where('student_id',$student_id)->where('classroom_time_id',$classroom_time->id)->find();
        return ;
    }
這裏獲取第幾小節的同時判斷一下,若是超出十一小節,說明上課時間已結束,返回到學生主頁。

2.經過classroom_time的id和學生id在seattable表裏找有沒有這個字段,在這裏定義爲$seattable,咱們要經過有無$seattable進行if語句。ssr

$seattable = Seattable::where('student_id',$student_id)->where('classroom_time_id',$classroom_time->id)->find();

        // 若是這個學生原來簽過到
        if($seattable) {
        } else { // 若是這個學生原來沒選過座位
        }
        return $this->success('選座成功', url('/index/student/page'));~~~~
這裏舉個例子,學生進入教室就會有一條數據,他選擇座位就會將行列數填入,別人搶了他的位置,將他的行列數清空,至關於他沒作座位,可是還在教室裏,學生id數據存在,這樣有利於老師綁定課程時簽到數加一。
我原來寫的思路是新建數據定死行列數清空學生id,這樣會致使別人搶了他的位置他再次掃碼時沒法判斷這是二次掃碼仍是第一第掃碼,從而沒法正確統計學生簽到總數。
確立定死student_id改變行列值的思路是實現這個功能的關鍵。

3.若是這個學生簽過到
兩種狀況,這個座位原來有人,這個座位原來沒人
有人的話先看這我的是否是他本身,是的話直接提示並返回學生主頁,不是的話獲得這個座位原來學生的一條數據,通知原來的人有人佔了座位了,將原來的人的行列數據清除,並將這個學生行列數填上。
沒人直接將行列數填上。code

$primaryStudent = Seattable::where('row',$row)->where('column',$column)->where('classroom_time_id',$classroom_time->id)->find();

    // 若是這個座位原來有學生
    if ($primaryStudent) {
        // 若是這個學生是他本身
        if ($primaryStudent->student_id == $student_id) {
            return $this->error('您已成功掃碼選擇此座位,請不要重複掃碼', url('/index/student/page'));    
        }

        // 通知他


        // 他行列信息清空
        $primaryStudent->row = 100;
        $primaryStudent->column = 100;
        if (!$primaryStudent->save()) {
            return $this->error('信息保存異常,請從新掃碼', url('/index/student/page'));
        }
    }


    // 將新的行列數保存到學生那條數據裏
    $seattable->row = $row;
    $seattable->column = $column;
    if (!$seattable->save()) {

        return $this->error('信息保存異常,請從新掃碼', url('/index/student/page'));
    }

舉例:本身(我叫張三)原來掃過碼而且掃碼的座位上有人。
掃碼前
image.png
image.png
掃碼後
image.png
image.pngblog

由於後續會用到對行列排序,爲了讓清空的行列數不顯示名字,咱們這裏將行列重置爲100,100(行列最大值)。

4.若是這個學生沒簽過到,也是先判斷這個座位原來是否有人,有人的話先通知他並清空行列數。沒簽過到seattable就不會有對應的student_id和classroom_time_id的數據,這時直接建立一條新的$seattable並將student_id,行列數填上,若是$seattable所對應的classroom_time->status爲1(status爲1表示已經跟課程綁定,status爲0表示沒有跟課程綁定),再進行簽到總數+1.排序

// 若是這個學生原來沒選過座位
            $primaryStudent = Seattable::where('row',$row)->where('column',$column)->where('classroom_time_id',$classroom_time->id)->find();
            // 若是這個座位原來有學生
            if ($primaryStudent) {
                // 通知他


                // 他行列信息清空
                $primaryStudent->row = 100;
                $primaryStudent->column = 100;
                if (!$primaryStudent->save()) {
                    return $this->error('信息保存異常,請從新掃碼', url('/index/student/page'));
                }
            }
             
            // 建立一條新數據
            $seattable = new Seattable;
            
            $seattable->classroom_time_id = $classroom_time->id;
            $seattable->row = $row;
            $seattable->column = $column;
            $seattable->student_id = $student_id;
            $seattable->role = 0;
            if (!$seattable->save()) {
                return $this->error('信息保存異常,請從新掃碼', url('/index/student/page'));
            } 
            
            // 若是這個classroom_time的狀態爲1,簽到次數加一
            if ($classroom_time->status) {
                $score = Score::where('student_id',$student_id)->where('course_id',$classroom_time->courseinfo->course_id)->find();

                if ($score) {
                    // 若是本學生有本課程的一條數據,簽到次數+1
                    $score->arrivals++;
                } else {
                    // 若是沒有,新建之
                    $score = new Score;
                    $score->student_id = $student_id;
                    $score->course_id = $classroom_time->courseinfo->course_id;
                    $score->usual_score = 0;
                    $score->exam_score = 0;
                    $score->total_score = 0;
                    $score->arrivals = 0;
                    $score->respond = 0;
                    $score->arrivals++;
                }
                if (!$score->save()) {
                     return $this->error('信息保存異常,請從新掃碼', url('/index/student/page'));
                }
            }

你們看看思路就好,完整代碼僅供參考產品

// 學生掃碼選座位(新中新)
    public function entercourse()
    {   
        $id = $this->request->param('id');
        
        $classroom_id = substr($id,0,4)*1;
        $row = substr($id,4,2)*1;
        $column = substr($id,6,2)*1;
        $time = Term::littleClass();
        if ($time<=0 || $time>11) {
            return $this->error('上課時間已結束', url('/index/student/page'));
        }
        $student_id = session('studentId');
        $classroom_time = Classroom_time::where('classroom_id',$classroom_id)->where('littleclass',$time)->find();

        
        $seattable = Seattable::where('student_id',$student_id)->where('classroom_time_id',$classroom_time->id)->find();

        // 若是這個學生原來簽過到
        if($seattable) {
            $primaryStudent = Seattable::where('row',$row)->where('column',$column)->where('classroom_time_id',$classroom_time->id)->find();

            // 若是這個座位原來有學生
            if ($primaryStudent) {
                // 若是這個學生是他本身
                if ($primaryStudent->student_id == $student_id) {
                    return $this->error('您已成功掃碼選擇此座位,請不要重複掃碼', url('/index/student/page'));    
                }

                // 通知他


                // 他行列信息清空
                $primaryStudent->row = 100;
                $primaryStudent->column = 100;
                if (!$primaryStudent->save()) {
                    return $this->error('信息保存異常,請從新掃碼', url('/index/student/page'));
                }
            }
            

            // 將新的行列數保存到學生那條數據裏
            $seattable->row = $row;
            $seattable->column = $column;
            if (!$seattable->save()) {

                return $this->error('信息保存異常,請從新掃碼', url('/index/student/page'));
            }

        } else {  // 若是這個學生原來沒選過座位
            $primaryStudent = Seattable::where('row',$row)->where('column',$column)->where('classroom_time_id',$classroom_time->id)->find();
            // 若是這個座位原來有學生
            if ($primaryStudent) {
                // 通知他


                // 他行列信息清空
                $primaryStudent->row = 100;
                $primaryStudent->column = 100;
                if (!$primaryStudent->save()) {
                    return $this->error('信息保存異常,請從新掃碼', url('/index/student/page'));
                }
            }
             
            // 建立一條新數據
            $seattable = new Seattable;
            
            $seattable->classroom_time_id = $classroom_time->id;
            $seattable->row = $row;
            $seattable->column = $column;
            $seattable->student_id = $student_id;
            $seattable->role = 0;
            if (!$seattable->save()) {
                return $this->error('信息保存異常,請從新掃碼', url('/index/student/page'));
            } 
            
            // 若是這個classroom_time的狀態爲1,簽到次數加一
            if ($classroom_time->status) {
                $score = Score::where('student_id',$student_id)->where('course_id',$classroom_time->courseinfo->course_id)->find();

                if ($score) {
                    // 若是本學生有本課程的一條數據,簽到次數+1
                    $score->arrivals++;
                } else {
                    // 若是沒有,新建之
                    $score = new Score;
                    $score->student_id = $student_id;
                    $score->course_id = $classroom_time->courseinfo->course_id;
                    $score->usual_score = 0;
                    $score->exam_score = 0;
                    $score->total_score = 0;
                    $score->arrivals = 0;
                    $score->respond = 0;
                    $score->arrivals++;
                }
                if (!$score->save()) {
                     return $this->error('信息保存異常,請從新掃碼', url('/index/student/page'));
                }
            }
            
        }

        return $this->success('選座成功', url('/index/student/page'));
    }

這個功能還須要天天定時清除數據,包括所有清除seattable表裏的數據和classroom_time表裏全部status歸0,courseinfo變爲null。it

總結

寫功能前肯定好思路很重要,否則可能會測出漏洞從新寫。

相關文章
相關標籤/搜索