2016-09-01 11:37:49 +02:00
< ? php
namespace App ;
use Illuminate\Database\Eloquent\Model ;
class Question extends Model
{
protected $fillable = [ " title " ];
public function options ()
{
return $this -> hasMany ( Option :: class );
}
public function test ()
{
return $this -> belongsTo ( Test :: class );
}
2016-09-10 15:17:31 +02:00
public function isCorrect ( $user_answer )
{
if ( $user_answer == $this -> answer_id ) {
return true ;
}
2016-09-18 15:00:44 +02:00
return false ;
2016-09-10 15:17:31 +02:00
}
2016-09-11 19:28:27 +02:00
public function deleteQuestion ()
{
$options = $this -> options ;
foreach ( $options as $option ) {
$option -> deleteOption ();
}
$this -> delete ();
return true ;
}
2016-09-22 18:45:09 +02:00
public function addQuestion ( $test , $request )
{
$this -> title = $request -> title ;
$this -> question = $request -> question ;
if ( $request -> multiple_answers_question == null ) {
$this -> multiple_answers_question = 0 ;
$this -> question_type = " radio " ;
$this -> correct_answers = 1 ;
} else {
$this -> multiple_answers_question = $request -> multiple_answers_question ;
$this -> question_type = " checkbox " ;
$correct_answers = 0 ;
if ( $request -> correct_answer1 == 1 ) {
$correct_answers ++ ;
}
if ( $request -> correct_answer2 == 1 ) {
$correct_answers ++ ;
}
if ( $request -> correct_answer3 == 1 ) {
$correct_answers ++ ;
}
if ( $request -> correct_answer4 == 1 ) {
$correct_answers ++ ;
}
$this -> correct_answers = $correct_answers ;
}
$test -> questions () -> save ( $this );
}
2016-10-24 13:38:02 +02:00
public function updateQuestion ( $request )
2016-09-22 18:45:09 +02:00
{
$this -> title = $request -> title ;
$this -> question = $request -> question ;
if ( $request -> multiple_answers_question == null ) {
$this -> multiple_answers_question = 0 ;
$this -> question_type = " radio " ;
$this -> correct_answers = 1 ;
} else {
$this -> multiple_answers_question = $request -> multiple_answers_question ;
$this -> question_type = " checkbox " ;
$correct_answers = 0 ;
if ( $request -> correct_answer1 == 1 ) {
$correct_answers ++ ;
}
if ( $request -> correct_answer2 == 1 ) {
$correct_answers ++ ;
}
if ( $request -> correct_answer3 == 1 ) {
$correct_answers ++ ;
}
if ( $request -> correct_answer4 == 1 ) {
$correct_answers ++ ;
}
$this -> correct_answers = $correct_answers ;
}
$this -> update ();
}
2016-09-01 11:37:49 +02:00
}