php-test/classes/quiz.class.php

67 lines
1.2 KiB
PHP

<?php
namespace TestProject;
/*
* Quiz class
*/
class Quiz extends Item {
private const tb_str = 'quiz';
private const resulTb_str = 'quiz_result';
private $name_str;
private $maxScore_int;
private $questionID_arr;
/**
* Load quiz
*
* @param int $id_int ID of quiz to load
*/
public function __construct( int $id_int ){
$db = DB::getDB();
$rows_arr = $db->select( self::tb_str, array(), array( 'id' => $id_int ) );
if ( empty( $rows_arr ) ){
throw new \RuntimeException( 'Invalid quiz ID' );
}
$this->id_int = $rows_arr[0]['id'];
$this->name_str = $rows_arr[0]['name'];
$this->maxScore_int = $rows_arr[0]['max_score'];
$this->questionID_arr = Question::getList( $this->id_int );
}
/**
* Get a list of quiz IDs and names
*
* @return string[] Array of quiz names keyed by id
*/
public static function getList(){
$db = DB::getDB();
$quiz_arr = $db->select( self::tb_str, array( 'id', 'name' ) );
return $quiz_arr;
}
/**
* Get name of Quiz
*
* @return string Name
*/
public function getName(){
return $this->name_str;
}
/**
* Get Maximum possible Score of quiz
*
* @return int Max score
*/
public function getMaxScore(){
return $this->maxScore_int;
}
}