<commit_before><?php

namespace App\Http\Controllers;

use App\Club;
use App\Qrcode;
use App\Record;
use App\User;
use Carbon\Carbon;
use Setting;

class QrcodeScanController extends Controller
{
    /**
     * 掃描QRCode
     *
     * @param $code
     * @return \Illuminate\Http\Response
     */
    public function scan($code)
    {
        /** @var User $user */
        $user = auth()->user();

        view()->share(compact('code'));
        //檢查冷卻（每分鐘60次）
        $throttle = \ExtendThrottle::get('qrcode scan ' . $user->id, 60, 1);
        if (!$throttle->attempt()) {
            return view('qrcode-scan.scan')->with('level', 'danger')->with('message', '掃描過於頻繁，請稍候重試');
        }

        //找出 QR Code
        /** @var Qrcode $qrcode */
        $qrcode = Qrcode::where('code', $code)->with('student.records.club.clubType')->first();
        if (!$qrcode) {
            return view('qrcode-scan.scan')->with('level', 'danger')->with('message', '無效的 QR Code');
        }
        view()->share(compact('qrcode'));

        //檢查QR Code是否已經被學生綁定
        if (!$qrcode->student) {
            return view('qrcode-scan.scan')->with('level', 'danger')->with('message', 'QR Code 尚未綁定，請聯絡課外活動組');
        }

        //檢查是否屬於活動時間
        $startAt = new Carbon(Setting::get('start_at'));
        if ($startAt->gte(Carbon::now())) {
            $startAtText = $startAt . '（' . $startAt->diffForHumans() . '）';

            return view('qrcode-scan.scan')->with('level', 'info')
                ->with('message', '集點活動尚未開始，預計在 ' . $startAtText . ' 開始');
        }
        $endAt = new Carbon(Setting::get('end_at'));
        if ($endAt->lte(Carbon::now())) {
            $endAtText = $endAt . '（' . $endAt->diffForHumans() . '）';

            return view('qrcode-scan.scan')->with('level', 'info')->with('message', '集點活動已在 ' . $endAtText . ' 結束');
        }

        //檢查掃描使用者是否為攤位負責人
        /** @var Club $club */
        $club = $user->club;
        if (!$club) {
            //非攤位負責人，不顯示訊息
            return view('qrcode-scan.scan');
        }

        //檢查QR Code為最後一組QR Code
        if (!$qrcode->is_last_one) {
            $lastBindingTime = $qrcode->student->qrcode->bind_at;
            return view('qrcode-scan.scan')->with('level', 'danger')
                ->with('message', "非最後一組 QR Code，請使用於 {$lastBindingTime}（{$lastBindingTime->diffForHumans()}）綁定之 QR Code");
        }

        //檢查是否在該攤位重複打卡
        /** @var Record $existRecord */
        $existRecord = Record::where('student_id', $qrcode->student->id)
            ->where('club_id', $club->id)
            ->first();
        if ($existRecord) {
            $createdAtForHumans = (new Carbon($existRecord->created_at))->diffForHumans();

            return view('qrcode-scan.scan')->with('level', 'warning')<commit_msg>Change not last qrcode message<commit_after><?php

namespace App\Http\Controllers;

use App\Club;
use App\Qrcode;
use App\Record;
use App\User;
use Carbon\Carbon;
use Setting;

class QrcodeScanController extends Controller
{
    /**
     * 掃描QRCode
     *
     * @param $code
     * @return \Illuminate\Http\Response
     */
    public function scan($code)
    {
        /** @var User $user */
        $user = auth()->user();

        view()->share(compact('code'));
        //檢查冷卻（每分鐘60次）
        $throttle = \ExtendThrottle::get('qrcode scan ' . $user->id, 60, 1);
        if (!$throttle->attempt()) {
            return view('qrcode-scan.scan')->with('level', 'danger')->with('message', '掃描過於頻繁，請稍候重試');
        }

        //找出 QR Code
        /** @var Qrcode $qrcode */
        $qrcode = Qrcode::where('code', $code)->with('student.records.club.clubType')->first();
        if (!$qrcode) {
            return view('qrcode-scan.scan')->with('level', 'danger')->with('message', '無效的 QR Code');
        }
        view()->share(compact('qrcode'));

        //檢查QR Code是否已經被學生綁定
        if (!$qrcode->student) {
            return view('qrcode-scan.scan')->with('level', 'danger')->with('message', 'QR Code 尚未綁定，請聯絡課外活動組');
        }

        //檢查是否屬於活動時間
        $startAt = new Carbon(Setting::get('start_at'));
        if ($startAt->gte(Carbon::now())) {
            $startAtText = $startAt . '（' . $startAt->diffForHumans() . '）';

            return view('qrcode-scan.scan')->with('level', 'info')
                ->with('message', '集點活動尚未開始，預計在 ' . $startAtText . ' 開始');
        }
        $endAt = new Carbon(Setting::get('end_at'));
        if ($endAt->lte(Carbon::now())) {
            $endAtText = $endAt . '（' . $endAt->diffForHumans() . '）';

            return view('qrcode-scan.scan')->with('level', 'info')->with('message', '集點活動已在 ' . $endAtText . ' 結束');
        }

        //檢查掃描使用者是否為攤位負責人
        /** @var Club $club */
        $club = $user->club;
        if (!$club) {
            //非攤位負責人，不顯示訊息
            return view('qrcode-scan.scan');
        }

        //檢查QR Code為最後一組QR Code
        if (!$qrcode->is_last_one) {
            $lastBindingTime = $qrcode->student->qrcode->bind_at;
            $lastBindingTimeText = $lastBindingTime . '（' . $lastBindingTime->diffForHumans() . '）';

            return view('qrcode-scan.scan')->with('level', 'danger')
                ->with('message', "非最後一組 QR Code，請使用於 {$lastBindingTimeText} 綁定之 QR Code");
        }

        //檢查是否在該攤位重複打卡
        /** @var Record $existRecord */
        $existRecord = Record::where('student_id', $qrcode->student->id)
            ->where('club_id', $club->id)
            ->first();
        if ($existRecord) {
            $createdAtForHumans = (new Carbon($existRecord->created_at))->diffForHumans();

            return view('qrcode-scan.scan')->with('level', 'warning')
{'fraction_non_alphanumeric': 0.19257294429708222, 'fraction_numerical': 0.0017683465959328027, 'mean_word_length': 2.2375500858614767, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2549346', 'n_tokens_mistral': 2149, 'n_tokens_neox': 2102, 'n_words': 307}