Skip to content

Instantly share code, notes, and snippets.

@zloadmin
Created April 13, 2017 18:28
Show Gist options
  • Save zloadmin/c8ce0589c2ba54fb5abe068360132393 to your computer and use it in GitHub Desktop.
Save zloadmin/c8ce0589c2ba54fb5abe068360132393 to your computer and use it in GitHub Desktop.
<template>
<div>
<steps
v-for="(step, index) in steps"
:step="step"
:current="current"
:index="index"
:button_url="button_url"
:button_text="button_text"
:button_type="button_type"
:points="points"
v-on:next-step="nextStep(index)">
</steps>
</div>
</template>
<script>
import Steps from './Steps';
export default {
mounted() {
this.countPoints();
this.renderButton();
},
data() {
var steps = [];
steps.push({
type: 'first'
});
for(var i = 0; i < exam.questions.length; i++) {
steps.push({
type: 'question',
data: exam.questions[i],
index: i
});
}
steps.push({
type: 'last'
});
return {
steps: steps,
current: typeof(Cookies.get(exam._id + '_current')) !== 'undefined' ? Cookies.get(exam._id + '_current') : 0,
questions: null,
questions_count: exam.questions.length,
points: 0,
button_type: null,
button_url: null,
button_text: null
};
},
components: { Steps },
methods: {
nextStep(step) {
this.current = step + 1;
this.countPoints();
this.renderButton();
Cookies.set(exam._id + '_current', this.current);
},
renderButton() {
this.button_type = ( this.points > this.questions_count / 2 ) ? 1 : 0;
this.button_text = ( this.button_type === 1 ) ? exam.button_2_text : exam.button_1_text;
this.button_url = ( this.button_type === 1 ) ? exam.button_2_url : exam.button_1_url;
},
countPoints() {
var points = 0;
$(".answer_radio:checked").each(function(index, element){
points = Math.floor($(element).val()) + points;
});
this.points = points;
}
}
}
</script>
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Exam;
use App\ExamResult;
use Response;
use App\Acme\Transformers\ExamTransformer;
use App\Acme\Transformers\ExamResultTransformer;
use App\Http\Requests\ExamRequest;
use Cookie;
class ExamController extends ApiController
{
/*
* @var Acme\Transformers\ExamTransform;
*/
protected $examTransform;
/*
* @var Acme\Transformers\ExamResultTransformer;
*/
protected $examResultTransform;
static function removeCookie($name)
{
if (isset($_COOKIE[$name])) {
unset($_COOKIE[$name]);
setcookie($name, null, -1, '/');
}
}
/**
* ExamController constructor.
* @param $examTransform
*/
public function __construct(ExamTransformer $examTransform, ExamResultTransformer $examResultTransform)
{
$this->examTransform = $examTransform;;
$this->examResultTransform = $examResultTransform;;
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$exams = Exam::orderBy('name')->get();
return $this->respond([
'data' => $this->examTransform->transformCollection($exams->toArray())
]);
}
/**
* @param ExamRequest $request
* @return mixed
*/
public function store(ExamRequest $request)
{
Exam::create($request->all());
return $this->respondCreated('Тест успешно создан');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$exam = Exam::find($id);
if(!$exam) return $this->respondNotFound('Тест не найден');
return $this->respond(['data' => $this->examTransform->transform($exam)]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(ExamRequest $request, $id)
{
$exam = Exam::find($id);
if(!$exam) return $this->respondNotFound('Тест не найден');
$exam->update($request->all());
return $this->respondCreated('Тест успешно создан');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$exam = Exam::find($id);
if(!$exam) return $this->respondNotFound('Тест не найден');
$exam->delete();
return $this->respondDeleted('Тест успешно удален');
}
/**
* Show exam. Web side.
* @param $id
* * @return \Illuminate\Http\Response
*/
public function getExam($id)
{
$exam = Exam::find($id);
if(!$exam) abort(404);
return view('web.welcome', compact('exam'));
}
public function postExam(Request $request, $id)
{
ExamResult::create([
'email' => (string) $request->get('email'),
'questions' => (array) $request->get('questions'),
'questions_count' => (integer) count($request->get('questions')),
'button_url' => (string) $request->get('button_url'),
'button_text' => (string) $request->get('button_text'),
'type' => (boolean) $request->get('type'),
'points' => (integer) $request->get('points'),
'exam_id' => (string) $id,
]);
$redirect_url = str_replace("<%= email %>", $request->get('email'), $request->get('button_url'));
self::removeCookie($id . '_current');
self::removeCookie($id . '_email');
for($i=0;$i<=count(Exam::find($id)->questions);$i++) {
self::removeCookie($id . '_question_' . $i);
}
return \Redirect::to($redirect_url);
}
public function getResults($id)
{
$exam = Exam::find($id);
if(!$exam) return $this->respondNotFound('Тест не найден');
return $this->respond(['data' => [
'exam' => $this->examTransform->transform($exam),
'results' => $this->examResultTransform->transformCollection($exam->results->toArray())
]]);
}
public function deleteResult($id)
{
$result = ExamResult::find($id);
if(!$result) return $this->respondNotFound('Результат не найден');
$result->delete();
return $this->respondDeleted('Результат успешно удален');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment