fcosrno/exam-php

用于创建、显示和评分考试的PHP库。

v1.0.0 2014-09-10 02:25 UTC

This package is not auto-updated.

Last update: 2024-09-24 08:08:18 UTC


README

用于创建、显示和评分考试的PHP库。

附加功能:您可以使用它来生成调查(只需忽略评分功能即可)。

Build Status Latest Stable Version

设置

composer require fcosrno/exam-php

快速使用

// Create instance of exam
$exam = new Exam();

// Add your questions
$exam->ask('Is the sky blue?')->setChoices('Always','Never','Sometimes')->setAnswer('Sometimes');
$exam->ask('Select things you may see at nighttime')->setSelections('Stars','Sun','Moon')->setAnswer('Stars','Moon');
$exam->ask('Is this a question?')->truefalse('true');

// Generate the HTML form (optional)
$view = $exam->generateHtml();

// Grade the exam
$myAnswers = array_values($_POST);
$percent = $exam->grade($myAnswers); // returns percent

添加问题

您可以通过提问来创建问题。

$exam->ask('Is the sky blue?'); 

然而,仅仅提问是不够的。考试需要知道问题的选项或选择。您可以通过对问题使用方法链 setChoices()setSelections() 来完成此操作。

在本例中,我们想要一个多项选择题,因此我们将使用 setChoices()

$exam->ask('Is the sky blue?')->setChoices('Always','Never','Sometimes');

如果您要评分这次考试,那么您还需要定义一个答案。

$exam->ask('Is the sky blue?')->setChoices('Always','Never','Sometimes')->setAnswer('Sometimes');

现在我们的多项选择题已经定义好了。让我们将其渲染为表单输入,以便我们可以将其发送到浏览器并向用户提问。

echo $exam->generateHtml();

我们的多项选择题将使用单选按钮渲染为HTML表单输入。

<p>Is the sky blue?</p>
<div class="radio">
	<label>
		<input type="radio" name="answer1" value="Always">Always
	</label>
</div>
<div class="radio">
	<label>
	<input type="radio" name="answer1" value="Never">Never
	</label>
</div>
<div class="radio">
	<label>
	<input type="radio" name="answer1" value="Sometimes">Sometimes
	</label>
</div>

问题类型

此库允许两种类型的问题:多项选择全选适用。还有一个第三种类型,对错,实际上只是对具有两个选项(正确或错误)的多项选择的便捷快捷方式。其他类型的问题正在考虑中,但尚未构建,除非社区提出请求。

根据您提问时使用的方法,将为您定义问题类型。例如,如果您使用 setChoices(),问题类型将是“多项选择”,但如果您使用 setSelections(),问题类型将是“全选适用”。您始终可以通过使用 setType() 属性来覆盖此设置(仅用于调试)。

多项选择(单选按钮)

多项选择(单选按钮)使用 setChoices(),类型为 multiple。这可能是最常见的问题类型。

// Quickest way (recommended)
$exam->ask('Is the sky blue?')->setChoices('Always','Never','Sometimes')->setAnswer('Sometimes');

// Fallback with type defined (redundant, yet consistent)
$exam->ask('Is the sky blue?')->setType('multiple')->setChoices('Always','Never','Sometimes')->setAnswer('Sometimes');

全选适用(复选框)

全选适用(复选框)使用 setSelections(),类型为 select

// Quickest way (recommended)
$exam->ask('Select things you may see at nighttime')->setSelections('Stars','Sun','Moon')->setAnswer('Stars','Moon');

// Fallback with type defined (redundant, yet consistent)
$exam->ask('Select things you may see at nighttime')->setChoices('Stars','Sun','Moon')->setType('select')->setAnswer('Stars','Moon');

请注意,答案的顺序不必与选项的顺序相同。

对错(单选按钮)

这是一个对只有正确或错误作为选项的多项选择的便捷包装器。

// You can do this (recommended)
$exam->ask('Is this a question?')->truefalse('true');

// Which is the same as this (a little more typing)
$exam->ask('Is this a question?')->truefalse()->setAnswer('True');

// Which is also the same as this (long way, but works)
$exam->ask('Is this a question?')->setChoices('True','False')->setAnswer('True');

其他

正在考虑其他类型的问题,但尚未构建,除非社区提出请求。以下是一些其他类型问题的想法

  • 配对将是类型 match
  • 文本(输入)必须与答案完全匹配,并考虑大小写选项。

评分考试

要评分考试,请将用户的答案传递给 grade() 方法。您必须传递与问题相同数量的答案,否则Exam将拒绝。如果用户留空答案,请确保您的POST过程将其定义为空字符串。

$exam->grade($answers);

这将返回以百分比字符串形式表示的评分,例如“75%”。但是,您也可以获取小数、分数或百分比的评分。

// this will return a decimal, ie 0.66666666666666663 (numeric)
$exam->grade($answers)->asDecimal(); 

// this will return a fraction, ie 2 / 3 (string)
$exam->grade($answers)->asFraction(); 

// this will return a percentage, ie 75 (integer)
$exam->grade($answers)->asPercentage(); 

关于百分比与百分比的快速说明

单词“百分比”(或符号%)与特定数字一起使用,而更通用的单词“百分比”则不与数字一起使用。默认情况下,评分函数返回百分比字符串,但您可以请求百分比。

// this will return percent, ie "75%" (string)
$exam->grade($answers);	

// this will return percentage, ie 75 (integer)
$exam->grade($answers)->asPercentage();

工作示例

./doc/example.php 中有一个工作示例,允许您参加考试并在提交答案后显示结果。

测试

测试是用PHPUnit构建的。

请确保使用开发需求进行安装。

composer install

前往项目根目录,然后在终端中输入以下命令运行所有测试

phpunit --bootstrap vendor/autoload.php tests/

带有覆盖率报告

phpunit --coverage-html ./tests/report --bootstrap vendor/autoload.php tests/

上次使用PHPUnit 4.1.4运行测试。