Beispiel #1
0
 public function limitReached($quiz_id)
 {
     $today = Test::where('quiz_id', $quiz_id)->where('user_id', Auth::id())->where('created_at', '>=', new \DateTime('today'))->get();
     if ($this->isExaminee() && count($today) >= 3) {
         return true;
     }
     return false;
 }
Beispiel #2
0
 public function show($id, TestTransformer $testTransformer)
 {
     $rawTest = Test::where('id', $id)->with('questions.options')->firstOrFail();
     $test = $testTransformer->transform($rawTest);
     return $test;
 }
 public function getactivetests()
 {
     $tests = \App\Test::where('status', 1)->get();
     return $tests;
 }
 public function question($id, QuestionRequest $request)
 {
     if ($request->correct1 + $request->correct2 + $request->correct3 + $request->correct4 + $request->correct5 == null) {
         return redirect()->back()->withErrors(['There must be at least one correct answer.'])->withInput()->with('id', $id);
     }
     $course = Course::findorFail($id);
     $test = Test::where('course_id', '=', $course->id)->firstOrFail();
     $question = $test->questions()->create($request->all());
     $question->save();
     $this->evaluate($question, $question->test()->first());
     if ($course->done == 1) {
         Session::flash('qu_add', 'alt');
         return redirect('/exam/' . $course->id . '/edit');
     }
     Session::flash('question_or_finish', 'alt');
     return redirect('getQuestion/' . $id);
 }
Beispiel #5
0
 function store(Request $request)
 {
     $input = $request->all();
     $testArr = array();
     $files = $request->file('test_files');
     $getParam = Parameter::where('name', '=', $input['parameter'])->first();
     if (!empty($getParam)) {
         $testArr['parameter_id'] = $getParam->id;
     } else {
         $parameter = new Parameter();
         $parameter->create(array('name' => $input['parameter']));
         $testArr['parameter_id'] = DB::getPdo()->lastInsertId();
     }
     $getState = Parameter::where('name', '=', $input['state'])->first();
     if (!empty($getState)) {
         $testArr['state_id'] = $getState->id;
     } else {
         $state = new State();
         $state->create(array('name' => $input['state']));
         $testArr['state_id'] = DB::getPdo()->lastInsertId();
     }
     $getMethod = Method::where('name', '=', $input['method'])->first();
     if (!empty($getMethod)) {
         $testArr['method_id'] = $getMethod->id;
     } else {
         $method = new Method();
         $method->create(array('name' => $input['method']));
         $testArr['method_id'] = DB::getPdo()->lastInsertId();
     }
     $getTestMethod = TestMethod::where('name', '=', $input['test_method'])->first();
     if (!empty($getTestMethod)) {
         $testArr['test_method_id'] = $getTestMethod->id;
     } else {
         $tmethod = new TestMethod();
         $tmethod->create(array('name' => $input['test_method']));
         $testArr['test_method_id'] = DB::getPdo()->lastInsertId();
     }
     $checkTest = Test::where('parameter_id', '=', $testArr['parameter_id'])->where('parameter_id', '=', $testArr['state_id'])->where('parameter_id', '=', $testArr['method_id'])->where('parameter_id', '=', $testArr['test_method_id'])->first();
     if ($checkTest) {
         $test_id = $checkTest->id;
         Session::flash('flash_message', 'Test already exist.');
     } else {
         $testArr['price'] = $input['price'];
         $test = new Test();
         $test::create($testArr);
         $test_id = DB::getPdo()->lastInsertId();
         Session::flash('flash_message', 'Test created successfully.');
     }
     if ($test_id) {
         if (!empty($files[0])) {
             foreach ($files as $file) {
                 $fileName = $file->getClientOriginalName();
                 $file->move(public_path() . '/TestFiles/' . $test_id, $fileName);
                 $file = new TestFile();
                 $file::create(array('file_name' => $fileName));
             }
         }
         DB::table('test_processes')->where('test_id', '=', $test_id)->delete();
         if ($request->has('test_item')) {
             $items = $request->get('test_item');
             foreach ($items as $key => $value) {
                 $process = new Process();
                 $process::create(array('test_id' => $test_id, 'item_id' => $value, 'status' => 1));
             }
         }
     }
     Session::flash('flash_type', 'alert-success');
     return redirect('admin/tests');
 }
Beispiel #6
0
 protected function _saveTest($data)
 {
     $isNewEntry = !array_key_exists('id', $data);
     if ($isNewEntry) {
         $test = new Test();
         $latest_test_on_course = Test::where(['course_id' => $data['course']['id']])->orderBy('order', 'DESC')->first();
         $next_course_order = ($latest_test_on_course ? $latest_test_on_course->order : 0) + 1;
         $test->order = $next_course_order;
     } else {
         $test = Test::find($data['id']);
         $submitted_questions = [];
         foreach ($data['questions'] as $qkey => $qdata) {
             if (array_key_exists('id', $qdata)) {
                 $submitted_questions[] = $qdata['id'];
             }
         }
         foreach ($test->questions as $question) {
             if (!in_array($question->id, $submitted_questions)) {
                 $question->delete();
             }
         }
         if ($test->course->id != $data['course']['id']) {
             $latest_test_on_course = Test::where(['course_id' => $data['course']['id']])->orderBy('order', 'DESC')->first();
             $next_course_order = ($latest_test_on_course ? $latest_test_on_course->order : 0) + 1;
             $test->order = $next_course_order;
         }
     }
     $test->course_id = $data['course']['id'];
     $test->title = $data['title'];
     $test->description = $data['description'];
     $test->autodiscard = $data['autodiscard'];
     $test->save();
     ///////////////////////
     $isNewPage = !array_key_exists('id', $data['page']);
     $pageBody = trim($data['page']['body']);
     $page = false;
     if ($isNewPage) {
         $page = new Page();
     } else {
         $page = Page::find($data['page']['id']);
     }
     if ($page) {
         $page->test_id = $test->id;
         $page->body = $pageBody;
         $page->hidden = false;
         if (strlen(trim(strip_tags($pageBody, '<img>'))) == 0) {
             $page->hidden = true;
         }
         $page->save();
     }
     /////////////////////////
     foreach ($data['questions'] as $qkey => $question_data) {
         if (!array_key_exists('id', $question_data)) {
             $question = new Question();
         } else {
             $question = Question::find($question_data['id']);
             $submitted_answers = [];
             foreach ($question_data['answers'] as $adata) {
                 if (array_key_exists('id', $adata)) {
                     $submitted_answers[] = $adata['id'];
                 }
             }
             foreach ($question->answers as $akey => &$answer) {
                 if (!in_array($answer->id, $submitted_answers) || $question_data['type'] == "TEXT" && $akey > 0 || $question_data['type'] == "TEXTAREA") {
                     $answer->delete();
                 }
             }
         }
         $question->test_id = $test->id;
         $question->type = $question_data['type'];
         $question->title = $question_data['title'];
         $question->subtitle = $question_data['subtitle'];
         $question->order = $qkey + 1;
         $question->data = array_key_exists('data', $question_data) ? $question_data['data'] : '{}';
         switch ($question->type) {
             case 'MULTITEXT':
                 if (!property_exists($question->data, 'multitext_required') || is_null($question->data->multitext_required) || $question->data->multitext_required > count($question_data['answers'])) {
                     $question->data->multitext_required = count($question_data['answers']);
                 }
                 break;
         }
         $question->save();
         ///////////////////////////
         foreach ($question_data['answers'] as $akey => $answer_data) {
             if (!array_key_exists('id', $answer_data)) {
                 $answer = new Answer();
             } else {
                 $answer = Answer::find($answer_data['id']);
             }
             if (!@$answer) {
                 $answer = new Answer();
             }
             $answer->question_id = $question->id;
             $answer->text = $answer_data['text'];
             if ($question->type == "CHOICE") {
                 $answer->is_correct = intval($question_data['correct_answer']) == $akey;
             } else {
                 $answer->is_correct = @$answer_data['is_correct'] ? true : false;
             }
             switch ($question->type) {
                 case 'TEXT':
                 case 'MULTITEXT':
                     $answer->is_correct = true;
                     break;
             }
             if (array_key_exists('error_margin', $answer_data)) {
                 $answer->error_margin = $answer_data['error_margin'];
             } else {
                 $answer->error_margin = 10;
             }
             $answer->save();
         }
     }
     return $test->id;
 }