IMMREX7
<?php
namespace App\Http\Controllers\School\Exam;
use Illuminate\Http\Request;
use App\Http\Controllers\School\SchoolController;
use DB;
use Auth;
use Session;
class ExamQuizController extends SchoolController {
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index() {
$classes = ['' => '-- Select Class --'] + \App\ClassM::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->orderBy('idClass')->get()->pluck('className', 'idClass')->toArray();
$section = ['' => '-- Select Section --'] ;
$subject = ['' => '-- Select Subject --'] ;
$exams = \App\ExamMcq::join('subjects','exam_mcq.idSubject','subjects.idSubject')
->select('exam_mcq.*','subjects.subjectName')
->where('exam_mcq.idSchool',Auth::guard('school')->user()->idSchool)->where('exam_mcq.idFinancialYear',Session::get('idFinancialYear'))->get();
return view('schools.exams.exam_mcq', compact('classes','section','subject','exams'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create() {
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
**/
public function store(Request $request) {
$rules = [
'idClass' => 'required',
'idSection' => 'required',
'idSubject' => 'required',
'totalTime' => 'required',
'totalMarks' => 'required',
];
if ($request->has('examName')) {
$rules += ['examName' => 'required'];
}
$messages = [
'examDate.required' => 'Exam Date must be selected',
];
$this->validate($request, $rules, $messages);
$mcq = \App\ExamMcq::where('idClass', $request->idClass)
->where('idSection', $request->idSection)
->where('examDate', $request->examDate)
->first();
if(isset($mcq->idMcq)){
return response()->json(['errors' => "Two exam cannot be schedule on same time for same class and section"], 422, ['app-status' => 'failed']);
}
$mcq = new \App\ExamMcq();
$mcq->idSchool = Auth::guard('school')->user()->idSchool;
$mcq->idFinancialYear = getFinancialYear();
$mcq->idClass = $request->idClass;
if($request->idSection != "all"){
$mcq->idSection = $request->idSection;
}
$mcq->idSubject = $request->idSubject;
$mcq->examName = $request->examName;
$mcq->examDate = \Carbon\Carbon::parse($request->examDate)->format('Y-m-d H:i:s');
$mcq->examTime = $request->totalTime;
$mcq->totalMarks = $request->totalMarks;
DB::beginTransaction();
$mcq->save();
$marks = 0;
foreach ($request->mcq as $key => $value) {
$questions = new \App\ExamQuestions();
$questions->marks = $value['marks'];
$questions->questionType = "mcq";
$questions->idMcq = $mcq->idMcq;
$marks = $marks + $value['marks'];
$questions->question = $value['questions'];
$questions->answerA = $value['opa'];
if(isset($value['apa']))
$questions->answerCorrect = 'A';
$questions->answerB = $value['opb'];
if(isset($value['apb']))
$questions->answerCorrect = 'B';
$questions->answerC = $value['opc'];
if(isset($value['apc']))
$questions->answerCorrect = 'C';
$questions->answerD = $value['opd'];
if(isset($value['apd']))
$questions->answerCorrect = 'D';
$questions->save();
}
if( $request->totalMarks != $marks){
DB::rollback();
if ($request->ajax()) {
return response()->json(['errors' => "Marks total is incorrect Expected :".$request->totalMarks." and Total : ".$marks], 422, ['app-status' => 'failed']);
}
}
DB::commit();
flash('Exam has been saved successfully.');
if ($request->ajax()) {
return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id) {
$value = \App\ExamMcq::join('subjects','exam_mcq.idSubject','subjects.idSubject')
->select('exam_mcq.*','subjects.subjectName')->where('idMcq',$id)->first();
$questions = \App\ExamQuestions::where('idMcq',$value->idMcq)->get();
return view('schools.exams.exam_mcq_view', compact('value','questions'));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response**/
public function edit($id) {
$templates = DB::table('exam_template')->join('exam_types', 'exam_template.idExamType', '=', 'exam_types.idExamType')
->select('exam_template.*', 'exam_types.typeName')
->where('exam_template.idSchool', '=', Auth::guard('school')->user()->idSchool)
->orderBy('idTemplate', 'desc')->get();
$types = \App\ExamType::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->orderBy('idExamType', 'desc')->get()->pluck('typeName', 'idExamType')->toArray();
$classes = ['' => '--Select--'] + \App\ClassM::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->orderBy('idClass')->get()->pluck('className', 'idClass')->toArray();
$sizes = ['' => '--Select--','A2'=>'A2','A3'=>'A3','A4'=>'A4','A5'=>'A5','custom'=>'Custom'];
$template = \App\ExamTemplate::where('idTemplate','=',$id)->first();
return view('schools.exams.exam_template', compact('templates', 'classes', 'types','template','sizes'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response**/
public function update(Request $request, $id) {
$template = \App\ExamTemplate::findOrfail($id);
$template->fill($request->all());
$template->update();
return redirect('school/examtemplates');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id) {
$template = \App\ExamMcq::findOrfail($id);
$questions = \App\ExamQuestions::where('idMcq',$template->idMcq)->delete();
$template->delete();
return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
}
}
Copyright © 2021 -