laravel在单一考试中获得阅读理解多项选择题和单一mcq

时间:2017-04-18 17:46:47

标签: php laravel

我有两种要求。

1)测试有直接问题和多项选择答案(单一答案)

2)测试与阅读理解及其一系列问题和一些各种问题然后再次通过。 (没有特别顺序出现段落和随机问题)

我已经通过构建以下表格来实现任务1。

Table( questions)
        $table->integer('quiz_id')->index();
        $table->integer('question_no')->index();
        $table->text('question');
        $table->text('quiz_ans_1');
        $table->text('quiz_ans_2');
        $table->text('quiz_ans_3');
        $table->text('quiz_ans_4');

Table (answers)
        $table->increments('id');
        $table->integer('question_id');
        $table->integer('quiz_id')->index();
        $table->integer('question_no')->index();
        $table->string('answer');

对于正常的直接测验,它在刀片模板中效果很好我可以提取问题标题和答案并运行" foreach"环。

对于任务2,我很困惑将阅读理解段落放在数据库中并将其拉入刀片,如测试格式所述,从考试到考试不等。

几个问题: -

1)如果模型"段落"与hasMany(' App \ Questions')一起创建。然后,当我打电话给测验并拉动刀片时,它将仅显示与其相关的段落和问题并忽略。但是需要提出许多单一的问题。

2)如果模型"问题" belongsTo Passage被创建,它定义每个问题都属于某个段落,而不是这种情况。

我想在在线测试的实例中实现以下内容: -

1)第1节 - > " N"独立的问题数量

2)第2节 - >三个阅读理解段落及其自己的一组问题(数字各不相同)

3)第3节 - > " M"独立的问题

4)第4节 - >两个阅读理解段落及其自己的一组问题(数字不尽相同)

总问题(n + m + 1)= 100(或200固定)

如果有些人可以让我朝着正确的方向前进,那将是很有帮助的。

1 个答案:

答案 0 :(得分:1)

你需要让你的问题具有多态性。意思是他们可以属于测试(独立问题),通过(通过问题)或测验(测验问题)。阅读这里的多态关系https://laravel.com/docs/5.4/eloquent-relationships#polymorphic-relations

问题表

id - integer
questionable_id - integer
questionable_type - string

由于您的问题现在是多语言的,因此您不能再拥有quiz_ans_1quiz_ans_2等。所以你为他们制作了一张桌子

选项表

id - integer
question_id - integer
content - text

然后你的模型测试(或考试),段落,Quizz,问题,选项,答案。在这些模型中,添加与问题模型的关系

public function questions() {
    return $this->morphMany(Question::class, 'questionable');
}
问题模型中的

添加

public function questionable() {
    return $this->morphTo();
}

顺便说一句,在表格答案中,删除quiz_idquestion_id足以知道它属于什么。此外,您可以回答任何问题,而不仅仅是测验问题。

现在,在你的考试/测试中你可以像这样得到它们

//get the exam first
$exam = Exam::with('questions')->find($examid);

//$exam hasMany [quizz, passages, questions] (independant questions)

//quiz questions
$quiz = Quiz::with('questions')->where('exam_id', $exam->id)->first();

//passage questions
$passages = Passage::with('questions')->where('exam_id', $exam->id)->get();

//independant questions
$questions = $exam->questions; 

在您看来,您可以这样显示

//1. Section 1, 10 independent questions
@php $firstTenQuestions = $questions->take(10); @endphp
@foreach($firstTenQuestions->all() as $question)
    {{ $question->content }}
@endforeach

//2. Reading passages (3)
@php $firstThreeReadings = $readings->take(3); @endphp
@foreach($firstThreeReadings->all() as $reading)
    {{ $reading->content }}

    @foreach($reading->questions as $question)
        {{ $question->content }}
    @endforeach
@endforeach

//3. Second set of independent questions 
// get questions from index 10 [11 - n]
@php $secondSet = $questions->splice(10); @endphp 
@foreach($secondSet->all() as $question)
    {{ $question->content }}
@endforeach

//4. Reading Passages (2)
// take from index 3, limit it to 2
@php $readings2 = $readings->splice(3, 2); @endphp
@foreach($readings2->all() as $reading)
    {{ $reading->content }}

    @foreach($reading->questions as $question)
        {{ $question->content }}
    @endforeach
@endforeach

这不是解决方案。但我相信你可以从中建立起来,到达你想要的地方。在此处了解集合操作https://laravel.com/docs/5.4/collections