IMMREX7

aku nok ndi : /home/spdtg/www/schoolmis/app/Http/Controllers/School/Exam/
File Up :
aku nok ndi : /home/spdtg/www/schoolmis/app/Http/Controllers/School/Exam/ExamMcqReportController.php

<?php

namespace App\Http\Controllers\School\Exam;

use Illuminate\Http\Request;
use App\Http\Controllers\School\SchoolController;
use DB;
use Auth;
use Carbon\Carbon;
use App\Http\SendSmsApi;
use Illuminate\Support\Facades\Redirect;
use Session;

class ExamMcqReportController extends SchoolController {

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request) {
        
        $results = \App\McqResult::join('students','students.idStudent','exam_mcq_results.idStudent')->join('exam_mcq','exam_mcq_results.idExam','exam_mcq.idMcq')->select('exam_mcq_results.*','students.ecNo','exam_mcq.totalMarks','exam_mcq.examDate','exam_mcq.examName','students.firstName')->where('exam_mcq_results.idSchool', '=', Auth::guard('school')->user()->idSchool);
            
        if ($request->has('idClass')) {
            $results = $results
                    ->where('exam_mcq_results.idClass',$request->idClass)
                    ->get();
        } elseif ($request->has('idSection')) {
            if($request->idSection != "all"){
                $results = $results
                        ->where('exam_mcq_results.idSection',$request->idSection)
                        ->get();
            }
        } elseif ($request->has('idSubject')) {
            $results = $results
                   ->where('exam_mcq_results.idSubject',$request->idSubject)
                    ->get();
        } elseif ($request->has('idExam')) {
            $results = $results
                   ->where('exam_mcq_results.idExam',$request->idExam)
                    ->get();
        } else {
            $results = [];
        }

        
        $subject = ['' => '-- Select Subject --'];
        $exam = ['' => '-- Select Exam --'];
        $section = ['' => '-- Select Section --'];
        $classes = ['' => '-- Select Class --'] + \App\ClassM::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->orderBy('idClass')->get()->pluck('className', 'idClass')->toArray();
                        
        return view('schools.exams.view_mcq_report', compact('subject','exam', 'section','classes','results'));
    }
    
    public function getExamName($id){
        $exam = ['' => '-- Select Exam --']+ \App\ExamMcq::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->where('idSubject',$id)->pluck('examName', 'idMcq')->toArray();
        return $exam;
    }

    public function getExamReviews(Request $request){
        $exam = ['' => '-- Select Exam --']+ \App\ExamMcq::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->pluck('examName', 'idMcq')->toArray();
        $students = array();
        $isMcq = 0;
        if ($request->has('idExam')) {


            if($request->publish != "Show"){
                $students = DB::table('exam_mcq_response')
                    ->select('exam_mcq_response.idStudent','exam_mcq_response.idExam','exam_mcq_response.isPublished')
                    ->where('idExam',$request->idExam)
                    ->where('isPublished','N')
                    ->groupBy('idStudent')->get();

                if (count($students) == 0) {
                    flash('Exam has already published.');
                     $isMcq =  DB::table('exam_mcq_response')->where('idExam',$request->idExam)->where('isPublished','N')->count();
               $students = DB::table('exam_mcq_response')
                    ->join('students','students.idStudent','exam_mcq_response.idStudent')
                    ->join('classes','classes.idClass','students.idClass')
                    ->join('sections','sections.idSection','students.idSection')
                    ->select('exam_mcq_response.idStudent','exam_mcq_response.idExam','exam_mcq_response.isPublished','students.ecNo','students.firstName','classes.className','sections.sectionName')
                    ->where('idExam',$request->idExam)
                    ->groupBy('idStudent')->get(); 
                    
                }else{
                    $exam =  \App\ExamMcq::where('idMcq', '=', $request->idExam)->first(); 
                    foreach ($students as $student) {
                        $marksObtained = 0;  
                        $response = DB::table('exam_mcq_response')
                        ->where('idExam',$request->idExam)
                        ->where('idStudent',$student->idStudent)
                        ->get();

                        foreach ($response as $sheet) {
                            $ques = \App\ExamQuestions::where('idQuestions', $sheet->idQuestion)->first();
                            if($ques->answerCorrect == $sheet->response){
                                $marksObtained = $marksObtained + $ques->marks;
                            }

                            $result = DB::table('exam_mcq_response')
                      ->where('idStudent', $student->idStudent)
                      ->where('idQuestion', $sheet->idQuestion)
                      ->update(['marks' => $ques->marks,'isPublished'=>'Y']);
                        
                        }

                        $student = \App\AdmEntry::where('idStudent', '=', $student->idStudent)->first();
                        $mcq = \App\McqResult::where('idStudent',$student->student)->where('idExam',$request->idMcq)->first();
                        if(isset($mcq->idSchool)){
                            $mcq->marks = $marksObtained;
                            $mcq->update();  
                            //dd($mcq);
                        }
                        else{
                            $mcq = new \App\McqResult();
                            $mcq->idStudent = $student->idStudent;
                            $mcq->idFinancialYear = $student->idFinancialYear;
                            $mcq->idSchool = $student->idSchool;
                            $mcq->idClass = $student->idClass;
                            $mcq->idSection = $student->idSection;
                            $mcq->idSchool = $student->idSchool;
                            $mcq->idExam = $exam->idMcq;
                            $mcq->idSubject = $exam->idSubject;
                            $mcq->marks = $marksObtained;
                            $mcq->save();
                            //dd($mcq);
                        }
                    }

                    flash('Exam published successfully');
                    $isMcq =  DB::table('exam_mcq_response')->where('idExam',$request->idExam)->where('isPublished','N')->count();
               $students = DB::table('exam_mcq_response')
                    ->join('students','students.idStudent','exam_mcq_response.idStudent')
                    ->join('classes','classes.idClass','students.idClass')
                    ->join('sections','sections.idSection','students.idSection')
                    ->select('exam_mcq_response.idStudent','exam_mcq_response.idExam','exam_mcq_response.isPublished','students.ecNo','students.firstName','classes.className','sections.sectionName')
                    ->where('idExam',$request->idExam)
                    ->groupBy('idStudent')->get(); 
                } 
            }else{
               $isMcq =  DB::table('exam_mcq_response')->where('idExam',$request->idExam)->where('isPublished','N')->count();
               $students = DB::table('exam_mcq_response')
                    ->join('students','students.idStudent','exam_mcq_response.idStudent')
                    ->join('classes','classes.idClass','students.idClass')
                    ->join('sections','sections.idSection','students.idSection')
                    ->select('exam_mcq_response.idStudent','exam_mcq_response.idExam','exam_mcq_response.isPublished','students.ecNo','students.firstName','classes.className','sections.sectionName')
                    ->where('idExam',$request->idExam)
                    ->groupBy('idStudent')->get(); 
            }
                   
        }
        return view('schools.exams.select_mcq_review', compact('exam','students','isMcq'));
    }


    public function showExamReviews($id,$idStudent){
       $template = \App\ExamMcq::join('subjects','exam_mcq.idSubject','subjects.idSubject')->select('exam_mcq.*','subjects.subjectName')->findOrfail($id);
       $paper = DB::table('exam_ques_types')->where('idExam',$id)->get();
       $questions =  ['' => '-- Select Questions --','mcq' => 'MCQ Questions','images_questions' => 'Image to Questions'
        ,'question_image' => 'Questions to Images','image_image' => 'Image to Image','math_type' => 'Math Type','short' => 'Short Question'
        ,'long' => 'Long Question','image_long' => 'Image for Long Question'] ;
       $options =  ['' => '-- Select Correct Option --','A' => 'Option A','B' => 'Option B','C' => 'Option C','D' => 'Option D'] ; 

       $students = \App\AdmEntry::select('idStudent','ecNo','firstName','idClass','idSection')->where('idStudent', '=', $idStudent)->first();  

       return view('schools.exams.exam_questions_result', compact('template','paper','questions','options','students'));
    }


    public function storeExamReviews(Request $request){
        if (count($request->mcq) == 0) {
            if ($request->ajax()) {
                return response()->json(['errors' => "No more question to add "], 422, ['app-status' => 'failed']);
            }
        }
        DB::beginTransaction();
        try {
        $marksObtained = 0;   
        $student = \App\AdmEntry::where('idStudent', '=', $request->student)->first(); 
        foreach ($request->mcq as $key => $value) {
                $marksObtained = $marksObtained + $value['marks'];
                $ques = \App\ExamQuestions::where('idQuestions', $key)->first();
                if($ques->marks < $value['marks'] ){
                     if ($request->ajax()) {
                         return response()->json(['errors' => "You cannot give more marks than assigned mark."], 422, ['app-status' => 'failed']);
                    }
                }

                $result = DB::table('exam_mcq_response')
                      ->where('idStudent', $request->student)
                      ->where('idQuestion', $key)
                      ->update(['marks' => $value['marks'],'isPublished'=>'Y']);
            }
            
            $exam = \App\ExamMcq::where('idMcq', '=', $request->idMcq)->first(); 
            $mcq = \App\McqResult::where('idStudent',$request->student)->where('idExam',$request->idMcq)->first();
            if(isset($mcq->idSchool)){
                $mcq->marks = $marksObtained;
                $mcq->update();  
            }
            else{
                $mcq = new \App\McqResult();
                $mcq->idStudent = $student->idStudent;
                $mcq->idFinancialYear = $student->idFinancialYear;
                $mcq->idSchool = $student->idSchool;
                $mcq->idClass = $student->idClass;
                $mcq->idSection = $student->idSection;
                $mcq->idSchool = $student->idSchool;
                $mcq->idExam = $exam->idMcq;
                $mcq->idSubject = $exam->idSubject;
                $mcq->marks = $marksObtained;
                $mcq->save();
            }

            DB::commit();
            if ($request->ajax()) {
                return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
            }
        } catch (\Exception $e) {
            \DB::rollback();
            if ($request->ajax()) {
            return response()->json(['errors' => $e->getMessage()], 422, ['app-status' => 'success']);
            }
        }    
    }


}

Copyright © 2021 - 2025 IMMREX7