Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
damarinov1 committed May 19, 2017
0 parents commit f6a1b10
Show file tree
Hide file tree
Showing 30 changed files with 958 additions and 0 deletions.
68 changes: 68 additions & 0 deletions app/AnswerCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

class AnswerCheck
{

public static function answerCheckTf($correctAnsIndex, $answer, $answer_input)
{
$isCorrect = false;

if ($correctAnsIndex == $answer) {
if ($answer_input == 'yes') {
Session::incrementCorrectQuestions();
$isCorrect = true;
}
}
if ($correctAnsIndex != $answer) {
if ($answer_input == 'no') {
Session::incrementCorrectQuestions();
$isCorrect = true;
}
}

Session::incrementAnsweredQuestions();
return $isCorrect;
}

public static function answerCheckMc($correctAnsIndex, $answer)
{
$isCorrect = false;

if ($correctAnsIndex == $answer) {
Session::incrementCorrectQuestions();
$isCorrect = true;
}

Session::incrementAnsweredQuestions();
}

public static function getPreviousCorrectAnswer()
{
$excluded = Session::getExcluded();

if (!empty($excluded) && count($excluded) > 1) {
end($excluded);
$previousQuestionId = prev($excluded);
$questionObj = QuestionRepository::getInstance()->find($previousQuestionId);
$previousAnswer = AnswerRepository::getInstance()->find($questionObj->getAnswerId());
}

return $previousAnswer;
}

public static function printPreviousAnswer($isTrue, $previousAnswer)
{
$color = "red";

if ($isTrue) {
$color = "green";
}

return "<strong><span style='color: {$color}'>" . $previousAnswer . "</span></strong>";
}

public static function isAnsweredQuestion()
{
return $_SESSION[Session::answeredQuestionsCount_key] > 0 ? 1 : 0;
}
}
31 changes: 31 additions & 0 deletions app/Results.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

class Results
{

private function getTotalQuestions()
{
return Session::getTotalQuestions();
}

private function getCorrectAnswers()
{
return Session::getCorrectAnswers();
}

private function getWrongAnswers()
{
return self::getTotalQuestions() - self::getCorrectAnswers();
}

public static function getPrintResults()
{
$results = [
'total' => self::getTotalQuestions(),
'correct' => self::getCorrectAnswers(),
'wrong' => self::getWrongAnswers()
];

return $results;
}
}
23 changes: 23 additions & 0 deletions app/Router.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

class Router
{

private static $routes = [];

public static function register($path, $action)
{
self::$routes[$path] = $action;
}

public static function dispatch()
{
$path = isset($_GET['path']) ? $_GET['path'] : "single";

if (array_key_exists($path, self::$routes)) {
echo call_user_func(self::$routes[$path]);
} else {
echo "<h1>404 - Not Found</h1>";
}
}
}
72 changes: 72 additions & 0 deletions app/ServiceContainer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
ini_set("display_errors", 1);
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
* Description of AbstractServiceContainer
*
* @author denis
*/
class ServiceContainer
{

/**
*
* @var object
*/
private $instances = [];
private $services = [];

public function register($name, $method)
{
if (isset($this->services[$name])) {
throw new Exception("Service '$name' already exists");
}

$this->services[$name] = $method;
}

/**
*
* @param string $name
* @return object
*/
public function get($name)
{
if (!isset($this->services[$name])) {
throw new Exception("Unknown service '$name'");
}

if (!isset($this->instances[$name])) {
$this->instances[$name] = call_user_func_array($this->services[$name], [$this]);
}

return $this->instances[$name];
}
}


class A {
public function __construct()
{
echo __CLASS__ . " it works! <br>";
}
}

class B {
/**
*
* @param A $a
*/
public function __construct(A $a)
{
echo __CLASS__ . " <br>";
}
}



72 changes: 72 additions & 0 deletions app/Session.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

class Session
{

const totalQuestionsCount_key = 'totalQuestionsCount';
const answeredQuestionsCount_key = 'answeredQuestionsCount';
const correctAnswersCount_key = 'correctAnswersCount';
const isStarted_key = 'isStarted';
const excluded_id = 'excludedId';
const mode = 'mode';

public static function init()
{
$_SESSION[self::totalQuestionsCount_key] = 10;
$_SESSION[self::answeredQuestionsCount_key] = 0;
$_SESSION[self::correctAnswersCount_key] = 0;
$_SESSION[self::isStarted_key] = true;
$_SESSION[self::mode] = 'tf';
$_SESSION[self::excluded_id] = [];
}

public static function getTotalQuestions()
{
return $_SESSION[self::totalQuestionsCount_key];
}

public static function getCorrectAnswers()
{
return $_SESSION[self::correctAnswersCount_key];
}

public static function incrementAnsweredQuestions()
{
$_SESSION[self::answeredQuestionsCount_key] ++;
}

public static function incrementCorrectQuestions()
{
$_SESSION[self::correctAnswersCount_key] ++;
}

public static function addToExluded($id)
{
$_SESSION[self::excluded_id][] = $id;
}

public static function getExcluded()
{
return $_SESSION[self::excluded_id];
}

public static function resetExcluded()
{
$_SESSION[self::excluded_id] = [];
}

public static function resetSession()
{
$_SESSION[self::answeredQuestionsCount_key] = 0;
$_SESSION[self::correctAnswersCount_key] = 0;
}

public static function isFinished()
{
if ($_SESSION[self::answeredQuestionsCount_key] == $_SESSION[self::totalQuestionsCount_key]) {
return true;
} else {
return false;
}
}
}
15 changes: 15 additions & 0 deletions app/View.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

class View
{

public static function render($template, Array $data = [])
{
ob_start();
include "views/" . $template . ".tpl.php";
$output = ob_get_contents();
ob_end_clean();

return $output;
}
}
43 changes: 43 additions & 0 deletions app/controllers/IndexController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

class IndexController
{

public function index()
{
return View::render('index', [
"id" => 23,
"title" => "It's working",
]);
}

public function changeMode()
{
$mode = $_POST['mode'];

$_SESSION[Session::mode] = $mode;

if ($_SESSION[Session::answeredQuestionsCount_key] > 0) {
Session::resetExcluded();
Session::resetSession();
}

$map = [
'tf' => 'single',
'mc' => 'multiple',
];

header("Location: ?path=" . $map[$mode]);
}

public function destroySession()
{
session_destroy();
header("Location: ?path=single");
}

public function getResults()
{
return View::render('results');
}
}
51 changes: 51 additions & 0 deletions app/controllers/MultipleController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

class MultipleController
{

public function index()
{
$excluded = Session::getExcluded();
$randQuestion = QuestionRepository::getInstance()->findRandom($excluded);

if ($randQuestion instanceof Question) {
Session::addToExluded($randQuestion->getId());
} else {
Session::resetExcluded();
}

$correctAnswerId = $randQuestion->getAnswerId();
$correctAnswer = AnswerRepository::getInstance()->find($correctAnswerId);
$randomAnswer1 = AnswerRepository::getInstance()->findRandom();

if ($randomAnswer1->getId() == $correctAnswerId) {
$randomAnswer1 = AnswerRepository::getInstance()->findRandom();
}
do {
$randomAnswer2 = AnswerRepository::getInstance()->findRandom();
} while ($randomAnswer1->getId() == $randomAnswer2->getId() ||
$randomAnswer2->getId() == $correctAnswerId);

$answerList = [$randomAnswer1, $randomAnswer2, $correctAnswer];
shuffle($answerList);

return View::render("multiple", [
"question" => $randQuestion,
"answers" => $answerList
]);
}

public function checkAnswer()
{
$correntAnswerId = QuestionRepository::getInstance()->find($_POST['question'])->getAnswerId();
$answer = $_POST['answer'];

AnswerCheck::answerCheckMc($correntAnswerId, $answer);

if (Session::isFinished()) {
header("Location: ?path=results");
} else {
header("Location: ?path=multiple");
}
}
}
Loading

0 comments on commit f6a1b10

Please sign in to comment.