A PHP library that implements functionality that's necessary to create multiple choice quizzes.
The license for this software is located in the project root in the file named LICENSE
- PHP 5.5 or newer
You should use composer to install this library. Here is an example composer.json file:
{
"require": {
"willskates/quizzes": "v1.0.0"
}
}
All information pertaining to the project charter, norms and development cycle can be found here.
This library provides you with a selection of classes. The main three are (including properties):
- Quiz
- name - The name of the quiz.
- description - A description for the quiz.
- alias - An alternative name for the quiz.
- uuid - A universally unique identifier for this quiz.
- url - A url where this quiz can be viewed.
- questions - A list of questions that belong to this quiz.
- Question
- name - The name of the question.
- description - A description for the question.
- alias - An alternative name for the question.
- uuid - A universally unique identifier for this question.
- url - A url where this question can be viewed.
- quiz - The quiz to which this question belongs.
- answers - A list of answers to this question.
- Answer
- name - The name of the answer.
- description - A description for the answer.
- alias - An alternative name for the answer.
- uuid - A universally unique identifier for this answer.
- url - A url where this item can be viewed.
- question - The question to which this answer belongs.
Most of these properties can be accessed using getters and setters e.g. 'name' can be accessed by using $obj->getName().
<?php
use Quizzes\Quiz;
use Quizzes\Question;
use Quizzes\Answer;
$quiz = new Quiz('A name', 'A description', 'An alias');
//Lets create a question.
$question = new Question('What is the meaning of life?', '42', 'The ultimate question');
$quiz->getQuestions()->add($question);
$question->setQuiz($quiz);
//Lets create some answers
$answers = [
new Answer('42', 'A number that we aren\'t really sure about.', ''),
new Answer('43', 'Another number that we aren\'t really sure about.', ''),
new Answer('44', 'Yet another number that we aren\'t really sure about.', '')
];
foreach($answers as $answer) {
$answer->setQuestion($question);
$question->getAnswers()->add($answer);
}
?>
We also have a selection of classes that can store this information.
At the moment this library supports the following databases out of the box:
- Mysql
<?php
use Quizzes\Storage\Mysql;
$connection = new Mysql(
'localhost',
'username',
'password',
'databasename'
);
?>
Now that you have a connection available you have access to storage repositories for Quizzes, Questions and Answers.
<?php
$connection->getQuizzes();
$connection->getQuestions();
$connection->getAnswers();
?>
All of these are instances of http://www.doctrine-project.org/api/orm/2.2/class-Doctrine.ORM.EntityRepository.html#_find and have access to it's methods. Here's a few examples of what you can do with it. (These can be used for questions, Quizzes and Answers).
<?php
$all = $connection->getQuizzes()->findAll();
$list = $connection->getQuizzes()->findBy(["name" => "a name to search for"]);
$single = $connection->getQuizzes()->findOneBy(["name" => "a name to search for"]);
?>
Every connection has a persist() method that you can use to save objects. Note that identifiers and primary keys are stored in objects automatically.
<?php
use Quizzes\Quiz;
use Quizzes\Question;
use Quizzes\Answer;
use Quizzes\Storage\Mysql;
$connection = new Mysql(
'localhost',
'username',
'password',
'databasename'
);
$quiz = new Quiz('A name', 'A description', 'An alias');
//Lets create a question.
$question = new Question('What is the meaning of life?', '42', 'The ultimate question');
$quiz->getQuestions()->add($question);
$question->setQuiz($quiz);
//Lets create some answers
$answers = [
new Answer('42', 'A number that we aren\'t really sure about.', ''),
new Answer('43', 'Another number that we aren\'t really sure about.', ''),
new Answer('44', 'Yet another number that we aren\'t really sure about.', '')
];
foreach($answers as $answer) {
$answer->setQuestion($question);
$question->getAnswers()->add($answer);
}
$connection->persist($quiz);
?>