/** * Resolves QuestionType object regardless of given identifier. * * @param $questionType * @return null * @throws QuestionTypeNotFoundException */ public static function resolveSelf($questionType) { if (is_null($questionType)) { return null; } if (!$questionType instanceof QuestionType) { if (is_numeric($questionType)) { try { $questionType = QuestionType::findOrFail($questionType); } catch (ModelNotFoundException $e) { throw new QuestionTypeNotFoundException('Question Type not found with the given ID.'); } } else { try { $questionType = QuestionType::whereSlug($questionType)->firstOrFail(); } catch (ModelNotFoundException $e) { throw new QuestionTypeNotFoundException('Question Type not found with the given slug.'); } } } return $questionType; }
public function setUp() { Eloquent::unguard(); $db = new DB(); $db->addConnection(['driver' => 'sqlite', 'database' => ':memory:']); $db->bootEloquent(); $db->setAsGlobal(); $this->schema()->create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->unsignedInteger('current_team_id')->nullable(); $table->timestamps(); }); $this->schema()->create('clients', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->unsignedInteger('current_team_id')->nullable(); $table->timestamps(); }); $this->schema()->create('question_types', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('slug')->unique(); $table->timestamps(); }); $question_types = ['Small Text', 'Large Text', 'Numeric', 'Date & Time', 'Multiple Choice', 'File Upload']; foreach ($question_types as $question_type) { QuestionType::create(['name' => $question_type, 'slug' => str_slug($question_type, '_')]); } $this->schema()->create('sections', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('slug')->unique(); $table->string('class_name')->nullable(); $table->text('options')->nullable(); $table->unsignedInteger('team_id')->nullable(); $table->softDeletes(); $table->timestamps(); }); $this->schema()->create('groups', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('slug')->unique(); $table->text('options')->nullable(); $table->unsignedInteger('section_id')->index(); $table->unsignedInteger('team_id')->nullable(); $table->softDeletes(); $table->timestamps(); $table->foreign('section_id')->references('id')->on('sections'); }); $this->schema()->create('questions', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('slug')->unique(); $table->unsignedInteger('question_type_id')->index(); $table->text('options')->nullable(); $table->text('choices')->nullable(); $table->unsignedInteger('group_id')->index(); $table->unsignedInteger('team_id')->nullable(); $table->softDeletes(); $table->timestamps(); $table->foreign('question_type_id')->references('id')->on('question_types'); $table->foreign('group_id')->references('id')->on('groups'); }); $this->schema()->create('answers', function (Blueprint $table) { $table->increments('id'); $table->unsignedInteger('question_id')->index(); $table->morphs('answerable'); $table->text('value'); $table->text('options')->nullable(); $table->unsignedInteger('team_id')->nullable(); $table->softDeletes(); $table->timestamps(); $table->foreign('question_id')->references('id')->on('questions'); }); }
/** * Return list of all Questions. * * @param null $question_type * @param null $group * @param null $team_id * @return \Illuminate\Database\Eloquent\Collection|static[] */ public function getQuestions($question_type = null, $group = null, $team_id = null) { return Question::where('team_id', $team_id)->when($group, function ($query) use($group) { return $query->where('group_id', Group::resolveSelf($group)->id); })->when($question_type, function ($query) use($question_type) { return $query->where('question_type_id', QuestionType::resolveSelf($question_type)->id); })->with('type')->get()->sortBy('order')->values(); }