<commit_before><?php

namespace App\Http\Controllers\Teacher;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Redirect;
use App\Models\Student;
use App\Models\Teacher;
use App\Models\Sclass;
use App\Models\School;
use App\Models\Lesson;
use App\Models\Post;
use App\Models\PostRate;
use App\Models\Comment;
use App\Models\LessonLog;
use App\Models\Mark;
use App\Models\Term;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Validator;
use \Auth;
use \DB;
use \Storage;
use App\Libaries\pinyinfirstchar;


class HomeController extends Controller
{
    public function index()
    {
        // dd(auth()->guard('teacher')->user());

        $userId = auth()->guard('teacher')->id();
        $teacher = Teacher::find($userId);
        // dd($userId);
        $lessonLog = LessonLog::where(['teachers_id' => $userId, 'status' => 'open'])->first();

        if ($lessonLog) {
            // dd($lessonLog);die();
            //If the teacher has one lesson log, only need redirect to route takeclass and load view
            return redirect('teacher/takeclass');
        }


        $sclasses = Sclass::where(["is_graduated" => 0, "schools_id" => $teacher->schools_id])->get();
        $classData = [];
        array_push($classData, "请选择班级");
        foreach ($sclasses as $key => $sclass) {
            $term = Term::where(['enter_school_year' => $sclass['enter_school_year'], 'is_current' => 1])->first();
            $classData[$sclass['id']] = $term['grade_key'] . $sclass['class_title'] . "班";
        }
        $lessons = Lesson::orderBy("lessons.created_at", "DESC")->get();
        $lessonsData = [];
        array_push($lessonsData, "请选择课程");
        $order = 1;
        foreach ($lessons as $key => $lesson) {
            $lessonsData[$lesson['id']] = $order . ". " . $lesson['title'] . "(". $lesson['subtitle'] .")";
            $order++;
        }
        return view('teacher/home', compact('classData', 'lessonsData'));
    }

    public function takeClass(Request $request)
    {
        
        $schoolCode = $this->getSchoolCode();
        // dd($schoolCode);
        $userId = auth()->guard('teacher')->id();
        $lessonLog = LessonLog::select('lesson_logs.id', 'lesson_logs.rethink', 'lesson_logs.sclasses_id', 'lessons.title', 'lessons.subtitle', 'sclasses.enter_school_year', 'sclasses.class_title', 'terms.grade_key', 'terms.term_segment')
        ->leftJoin("lessons", 'lessons.id', '=', 'lesson_logs.lessons_id')
        ->leftJoin("sclasses", 'sclasses.id', '=', 'lesson_logs.sclasses_id')
        ->leftJoin("terms", 'terms.enter_school_year', '=', 'sclasses.enter_school_year')
        ->where(['lesson_logs.teachers_id' => $userId, 'lesson_logs.status' => 'open', 'terms.is_current' => 1])->first();
        // dd($lessonLog);die();
        if ("group" == $request->get("order")) {
            $students = DB::table('students')->select('students.id', 'students.username', 'posts.file_ext', 'posts.storage_name', 'comments.content', 'post_rates.rate', 'groups.order_num', 'posts.id as posts_id', DB::raw("COUNT(`marks`.`id`) as mark_num"))
            ->leftJoin('posts', 'posts.students_id', '=', 'students.id')
            ->leftJoin('post_rates', 'post_rates.posts_id', '=', 'posts.id')
            ->leftJoin('groups', 'groups.id', '=', 'students.groups_id')
            ->leftJoin('comments', 'comments.posts_id', '=', 'posts.id')
            ->leftJoin('marks', 'marks.posts_id', '=', 'posts.id')
            ->where(["students.sclasses_id" => $lessonLog['sclasses_id'], 'posts.lesson_logs_id' => $lessonLog['id']])
            ->where('students.is_lock', "!=", "1")
            ->groupBy('students.id', 'students.username', 'posts.storage_name', 'comments.content', 'post_rates.rate', 'posts.id')
            ->orderBy('groups.order_num', "ASC")->get();
            
        } else {
            $students = DB::table('students')->select('students.id', 'students.username', 'posts.file_ext', 'posts.storage_name', 'comments.content', 'post_rates.rate', 'posts.id as posts_id', DB::raw("COUNT(`marks`.`id`) as mark_num"))
            ->leftJoin('posts', 'posts.students_id', '=', 'students.id')
            ->leftJoin('post_rates', 'post_rates.posts_id', '=', 'posts.id')
            ->leftJoin('comments', 'comments.posts_id', '=', 'posts.id')
            ->leftJoin('marks', 'marks.posts_id', '=', 'posts.id')
            ->where(["students.sclasses_id" => $lessonLog['sclasses_id'], 'posts.lesson_logs_id' => $lessonLog['id']])
            ->where('students.is_lock', "!=", "1")
            ->groupBy('students.id', 'students.username', 'posts.storage_name', 'comments.content', 'post_rates.rate', 'posts.id')
            ->orderBy(DB::raw('convert(students.username using gbk)'), "ASC")->get();
        }

        
        
        // dd($lessonLog);
        $unPostStudentName = [];
        
        $postedStudents = [];
        $allStudentsList = DB::table('students')->select('students.username')
        ->where(['students.sclasses_id' => $lessonLog['sclasses_id']])->where('students.is_lock', "!=", "1")->get();
        foreach ($students as $key => $student) {
            array_push($postedStudents, $student->username);
        }
        foreach ($allStudentsList as $key => $studentsName) {
            if (!in_array($studentsName->username, $postedStudents)) {
                array_push($unPostStudentName, $studentsName->username);
            }
        }
        // dd($unPostStudentName);
        $allCount = count($allStudentsList);
        $py = new pinyinfirstchar();
        return view('teacher/takeclass', compact('students', 'lessonLog', 'py', 'allCount', 'unPostStudentName', 'schoolCode'));
    }

    public function updateRate(Request $request)
    {
        $this->validate($request, [
            'posts_id' => 'required',
            'rate' => 'required',
        ]);

        $id = Auth::guard('teacher')->id();
        $posts_id = $request->get('posts_id');
        $rate = $request->get('rate');
        $postRate = PostRate::where(['teachers_id' => $id, "posts_id" => $posts_id])->first();
        if (isset($postRate)) {
            $postRate->rate = $rate;
            if ($postRate->update()) {
                return "true";
            } else {
                return "false";
            }
        } else {
            $postRate = new PostRate();
            $postRate->teachers_id = $id;
            $postRate->posts_id = $posts_id;
            $postRate->rate = $rate;
            if ($postRate->save()) {
                return "true";
            } else {
                return "false";
            }
        }
    }

    public function getLessonLog(Request $request)
    {   
        $teachersId = Auth::guard('teacher')->id();
        $lessonLog = LessonLog::where(['lessons_id' => $request->input('lessonsId'), 
                                    'sclasses_id' => $request->input('sclassesId'), 
                                    'teachers_id' => $teachersId])->first();

        if (isset($lessonLog)) {
            $postNum = Post::where(['lesson_logs_id' => $lessonLog->id])->count();

            return $postNum;
        } else {
            return "false";
        }
    }

    public function getPostRate(Request $request)
    {
        $postRate = PostRate::where(['posts_id' => $request->input('posts_id')])->first();

        if (isset($postRate)) {
            return $postRate['rate'];
        } else {
            return "false";
        }
    }

    public function getPost(Request $request)
    {
        $middir = "/posts/" . $this->getSchoolCode() . "/";
        $post = Post::where(['id' => $request->input('posts_id')])->orderBy('id', 'desc')->first();
        $imgTypes = ['jpg', 'jpeg', 'bmp', 'gif', 'png'];
        $docTypes = ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx'];
        if (isset($post)) {
            if (in_array($post->file_ext, $imgTypes)) {
                return ["filetype"=>"img", "url" => getThumbnail($post['storage_name'], 800, 600, $this->getSchoolCode(), 'fit', $post['file_ext'])];
            } elseif (in_array($post->file_ext, $docTypes)) {
                return ["filetype"=>"doc", "url" => env('APP_URL'). $middir .$post->storage_name];
            } elseif ("sb2" == $post->file_ext) {
                return ["filetype"=>"sb2", "url" => env('APP_URL'). $middir .$post->storage_name];
            }
            // $file = Storage::disk('uploads')->get($post->storage_name)->getPath();
                // $post->storage_name = env('APP_URL')."/posts/".$post->storage_name;
            // return getThumbnail($post['storage_name'], 800, 600, 'fit');
        
        } else {<commit_msg>use background way to show jpg at teacher take class<commit_after><?php

namespace App\Http\Controllers\Teacher;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Redirect;
use App\Models\Student;
use App\Models\Teacher;
use App\Models\Sclass;
use App\Models\School;
use App\Models\Lesson;
use App\Models\Post;
use App\Models\PostRate;
use App\Models\Comment;
use App\Models\LessonLog;
use App\Models\Mark;
use App\Models\Term;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Validator;
use \Auth;
use \DB;
use \Storage;
use App\Libaries\pinyinfirstchar;


class HomeController extends Controller
{
    public function index()
    {
        // dd(auth()->guard('teacher')->user());

        $userId = auth()->guard('teacher')->id();
        $teacher = Teacher::find($userId);
        // dd($userId);
        $lessonLog = LessonLog::where(['teachers_id' => $userId, 'status' => 'open'])->first();

        if ($lessonLog) {
            // dd($lessonLog);die();
            //If the teacher has one lesson log, only need redirect to route takeclass and load view
            return redirect('teacher/takeclass');
        }


        $sclasses = Sclass::where(["is_graduated" => 0, "schools_id" => $teacher->schools_id])->get();
        $classData = [];
        array_push($classData, "请选择班级");
        foreach ($sclasses as $key => $sclass) {
            $term = Term::where(['enter_school_year' => $sclass['enter_school_year'], 'is_current' => 1])->first();
            $classData[$sclass['id']] = $term['grade_key'] . $sclass['class_title'] . "班";
        }
        $lessons = Lesson::orderBy("lessons.created_at", "DESC")->get();
        $lessonsData = [];
        array_push($lessonsData, "请选择课程");
        $order = 1;
        foreach ($lessons as $key => $lesson) {
            $lessonsData[$lesson['id']] = $order . ". " . $lesson['title'] . "(". $lesson['subtitle'] .")";
            $order++;
        }
        return view('teacher/home', compact('classData', 'lessonsData'));
    }

    public function takeClass(Request $request)
    {
        
        $schoolCode = $this->getSchoolCode();
        // dd($schoolCode);
        $userId = auth()->guard('teacher')->id();
        $lessonLog = LessonLog::select('lesson_logs.id', 'lesson_logs.rethink', 'lesson_logs.sclasses_id', 'lessons.title', 'lessons.subtitle', 'sclasses.enter_school_year', 'sclasses.class_title', 'terms.grade_key', 'terms.term_segment')
        ->leftJoin("lessons", 'lessons.id', '=', 'lesson_logs.lessons_id')
        ->leftJoin("sclasses", 'sclasses.id', '=', 'lesson_logs.sclasses_id')
        ->leftJoin("terms", 'terms.enter_school_year', '=', 'sclasses.enter_school_year')
        ->where(['lesson_logs.teachers_id' => $userId, 'lesson_logs.status' => 'open', 'terms.is_current' => 1])->first();
        // dd($lessonLog);die();
        if ("group" == $request->get("order")) {
            $students = DB::table('students')->select('students.id', 'students.username', 'posts.file_ext', 'posts.storage_name', 'comments.content', 'post_rates.rate', 'groups.order_num', 'posts.id as posts_id', DB::raw("COUNT(`marks`.`id`) as mark_num"))
            ->leftJoin('posts', 'posts.students_id', '=', 'students.id')
            ->leftJoin('post_rates', 'post_rates.posts_id', '=', 'posts.id')
            ->leftJoin('groups', 'groups.id', '=', 'students.groups_id')
            ->leftJoin('comments', 'comments.posts_id', '=', 'posts.id')
            ->leftJoin('marks', 'marks.posts_id', '=', 'posts.id')
            ->where(["students.sclasses_id" => $lessonLog['sclasses_id'], 'posts.lesson_logs_id' => $lessonLog['id']])
            ->where('students.is_lock', "!=", "1")
            ->groupBy('students.id', 'students.username', 'posts.storage_name', 'comments.content', 'post_rates.rate', 'posts.id')
            ->orderBy('groups.order_num', "ASC")->get();
            
        } else {
            $students = DB::table('students')->select('students.id', 'students.username', 'posts.file_ext', 'posts.storage_name', 'comments.content', 'post_rates.rate', 'posts.id as posts_id', DB::raw("COUNT(`marks`.`id`) as mark_num"))
            ->leftJoin('posts', 'posts.students_id', '=', 'students.id')
            ->leftJoin('post_rates', 'post_rates.posts_id', '=', 'posts.id')
            ->leftJoin('comments', 'comments.posts_id', '=', 'posts.id')
            ->leftJoin('marks', 'marks.posts_id', '=', 'posts.id')
            ->where(["students.sclasses_id" => $lessonLog['sclasses_id'], 'posts.lesson_logs_id' => $lessonLog['id']])
            ->where('students.is_lock', "!=", "1")
            ->groupBy('students.id', 'students.username', 'posts.storage_name', 'comments.content', 'post_rates.rate', 'posts.id')
            ->orderBy(DB::raw('convert(students.username using gbk)'), "ASC")->get();
        }

        
        
        // dd($lessonLog);
        $unPostStudentName = [];
        
        $postedStudents = [];
        $allStudentsList = DB::table('students')->select('students.username')
        ->where(['students.sclasses_id' => $lessonLog['sclasses_id']])->where('students.is_lock', "!=", "1")->get();
        foreach ($students as $key => $student) {
            array_push($postedStudents, $student->username);
        }
        foreach ($allStudentsList as $key => $studentsName) {
            if (!in_array($studentsName->username, $postedStudents)) {
                array_push($unPostStudentName, $studentsName->username);
            }
        }
        // dd($unPostStudentName);
        $allCount = count($allStudentsList);
        $py = new pinyinfirstchar();
        return view('teacher/takeclass', compact('students', 'lessonLog', 'py', 'allCount', 'unPostStudentName', 'schoolCode'));
    }

    public function updateRate(Request $request)
    {
        $this->validate($request, [
            'posts_id' => 'required',
            'rate' => 'required',
        ]);

        $id = Auth::guard('teacher')->id();
        $posts_id = $request->get('posts_id');
        $rate = $request->get('rate');
        $postRate = PostRate::where(['teachers_id' => $id, "posts_id" => $posts_id])->first();
        if (isset($postRate)) {
            $postRate->rate = $rate;
            if ($postRate->update()) {
                return "true";
            } else {
                return "false";
            }
        } else {
            $postRate = new PostRate();
            $postRate->teachers_id = $id;
            $postRate->posts_id = $posts_id;
            $postRate->rate = $rate;
            if ($postRate->save()) {
                return "true";
            } else {
                return "false";
            }
        }
    }

    public function getLessonLog(Request $request)
    {   
        $teachersId = Auth::guard('teacher')->id();
        $lessonLog = LessonLog::where(['lessons_id' => $request->input('lessonsId'), 
                                    'sclasses_id' => $request->input('sclassesId'), 
                                    'teachers_id' => $teachersId])->first();

        if (isset($lessonLog)) {
            $postNum = Post::where(['lesson_logs_id' => $lessonLog->id])->count();

            return $postNum;
        } else {
            return "false";
        }
    }

    public function getPostRate(Request $request)
    {
        $postRate = PostRate::where(['posts_id' => $request->input('posts_id')])->first();

        if (isset($postRate)) {
            return $postRate['rate'];
        } else {
            return "false";
        }
    }

    public function getPost(Request $request)
    {
        $middir = "/posts/" . $this->getSchoolCode() . "/";
        $post = Post::where(['id' => $request->input('posts_id')])->orderBy('id', 'desc')->first();
        $imgTypes = ['jpg', 'jpeg', 'bmp', 'gif', 'png'];
        $docTypes = ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx'];
        if (isset($post)) {
            if (in_array($post->file_ext, $imgTypes)) {
                return ["filetype"=>"img", "url" => getThumbnail($post['storage_name'], 801, 601, $this->getSchoolCode(), 'background', $post['file_ext'])];
            } elseif (in_array($post->file_ext, $docTypes)) {
                return ["filetype"=>"doc", "url" => env('APP_URL'). $middir .$post->storage_name];
            } elseif ("sb2" == $post->file_ext) {
                return ["filetype"=>"sb2", "url" => env('APP_URL'). $middir .$post->storage_name];
            }
            // $file = Storage::disk('uploads')->get($post->storage_name)->getPath();
                // $post->storage_name = env('APP_URL')."/posts/".$post->storage_name;
            // return getThumbnail($post['storage_name'], 800, 600, 'fit');
        
        } else {
{'fraction_non_alphanumeric': 0.1981370745170193, 'fraction_numerical': 0.0024149034038638455, 'mean_word_length': 2.616008316008316, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 231, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3825321', 'n_tokens_mistral': 5623, 'n_tokens_neox': 5504, 'n_words': 887}