PHP code example of harishdurga / laravel-quiz
1. Go to this page and download the library: Download harishdurga/laravel-quiz library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
harishdurga / laravel-quiz example snippets
$computer_science = Topic::create([
'name' => 'Computer Science',
'slug' => 'computer-science',
]);
$algorithms = Topic::create([
'name' => 'Algorithms',
'slug' => 'algorithms'
]);
$computer_science->children()->save($algorithms);
QuestionType::create(['name'=>'select_all']);
'get_score_for_question_type' => [
1 => '\Harishdurga\LaravelQuiz\Models\QuizAttempt::get_score_for_type_1_question',
2 => '\Harishdurga\LaravelQuiz\Models\QuizAttempt::get_score_for_type_2_question',
3 => '\Harishdurga\LaravelQuiz\Models\QuizAttempt::get_score_for_type_3_question',
4 => 'Your custom method'
]
/**
* @param QuizAttemptAnswer[] $quizQuestionAnswers All the answers of the quiz question
*/
public static function get_score_for_type_3_question(QuizAttempt $quizAttempt, QuizQuestion $quizQuestion, array $quizQuestionAnswers, $data = null): float
{
// Your logic here
}
$question_one = Question::create([
'name' => 'What is an algorithm?',
'question_type_id' => 1,
'is_active' => true,
'media_url' => 'url',
'media_type' => 'image'
]);
$question_type->questions
Question::hasOptions()->get()
$question->topics()->attach([$computer_science->id, $algorithms->id]);
$question_two_option_one = QuestionOption::create([
'question_id' => $question_two->id,
'name' => 'array',
'is_correct' => true,
'media_type'=>'image',
'media_url'=>'media url'
]);
$question->options
$quiz = Quiz::create([
'name' => 'Computer Science Quiz',
'description' => 'Test your knowledge of computer science',
'slug' => 'computer-science-quiz',
'time_between_attempts' => 0, //Time in seconds between each attempt
'total_marks' => 10,
'pass_marks' => 6,
'max_attempts' => 1,
'is_published' => 1,
'valid_from' => now(),
'valid_upto' => now()->addDay(5),
'media_url'=>'',
'media_type'=>'',
'negative_marking_settings'=>[
'enable_negative_marks' => true,
'negative_marking_type' => 'fixed',
'negative_mark_value' => 0,
]
]);
$quiz->topics()->attach([$topic_one->id, $topic_two->id]);
$admin = Author::create(
['name' => "John Doe"]
);
$quiz = Quiz::factory()->make()->create([
'name' => 'Sample Quiz',
'slug' => 'sample-quiz'
]);
QuizAuthor::create([
'quiz_id' => $quiz->id,
'author_id' => $admin->id,
'author_type' => get_class($admin),
'author_role' => 'admin',
]);
$quiz->quizAuthors->first()->author; //Original User
$quiz_question = QuizQuestion::create([
'quiz_id' => $quiz->id,
'question_id' => $question->id,
'marks' => 3,
'order' => 1,
'negative_marks'=>1,
'is_optional'=>false
]);
$quiz->questions
$quiz_attempt = QuizAttempt::create([
'quiz_id' => $quiz->id,
'participant_id' => $participant->id,
'participant_type' => get_class($participant)
]);
$quiz_attempt->participant
QuizAttemptAnswer::create(
[
'quiz_attempt_id' => $quiz_attempt->id,
'quiz_question_id' => $quiz_question->id,
'question_option_id' => $question_option->id,
]
);
$quiz_attempt->calculate_score()
$question->correct_options
public function correct_options(): Collection
{
return $this->options()->where('is_correct', 1)->get();
}
[
1 => [
'score' => 10,
'is_correct' => true,
'correct_answer' => ['One','Five','Seven'],
'user_answer' => ['Five','One','Seven']
],
2 => [
'score' => 0,
'is_correct' => false,
'correct_answer' => 'Hello There',
'user_answer' => 'Hello World'
]
]
'render_answers_responses' => [
1 => '\Harishdurga\LaravelQuiz\Models\QuizAttempt::renderQuestionType1Answers',
2 => '\Harishdurga\LaravelQuiz\Models\QuizAttempt::renderQuestionType2Answers',
3 => '\Harishdurga\LaravelQuiz\Models\QuizAttempt::renderQuestionType3Answers',
]
public static function renderQuestionType1Answers(QuizQuestion $quizQuestion,QuizAttempt $quizAttempt,mixed $data=null)
{
/**
* @var Question $actualQuestion
*/
$actualQuestion = $quizQuestion->question;
$answers = $quizQuestion->answers->where('quiz_attempt_id', $quizAttempt->id);
$questionOptions = $actualQuestion->options;
$correctAnswer = $actualQuestion->correct_options()->first()?->option;
$givenAnswer = $answers->first()?->question_option_id;
foreach ($questionOptions as $questionOption) {
if ($questionOption->id == $givenAnswer) {
$givenAnswer = $questionOption->option;
break;
}
}
return [$correctAnswer, $givenAnswer];
}
bash
php artisan vendor:publish --provider="Harishdurga\LaravelQuiz\LaravelQuizServiceProvider"
bash
php artisan db:seed --class=\\Harishdurga\\LaravelQuiz\\Database\\Seeders\\QuestionTypeSeeder