问题偏好¶
Question preferences allow a single question to be customized for different assessments without duplicating the question.问题在其 info.json 中定义首选项架构,并且每个评估在包含问题时都可以覆盖默认值。
server.py(通过 data["preferences"])和 question.html(通过 {{preferences.key}})中均提供首选项。
定义偏好¶
可以通过问题设置页面或直接编辑 info.json 来管理首选项。
使用问题设置页面¶
在问题设置页面上,首选项部分显示一个表格,您可以在其中添加、编辑、重新排序和删除首选项。每个偏好都有:
- 名称:用于在代码中引用首选项的唯一标识符(例如,
show_hints)。 - 类型:
string、number或boolean。 - 默认:评估不提供覆盖时使用的值。
- 允许值:一组可选的允许值。设置后,默认值和任何评估覆盖都必须是这些值之一。如果留空(显示为“Any”),则接受与该类型匹配的任何值。
可以通过拖动每行左侧的手柄来重新排序首选项。
使用 JSON¶
将 preferences 对象添加到您问题的 info.json 中。每个键都定义一个首选项字段,其中包含 type、default 值和可选的允许值 enum。
{
"uuid": "302d2d6c-a8a4-4413-97dc-79344d75a5f0",
"title": "Force on a falling object",
"topic": "Forces",
"type": "v3",
"preferences": {
"gravitational_constant": {
"type": "number",
"default": 9.8
},
"fired_object": {
"type": "string",
"enum": ["cannon ball", "bowling ball"],
"default": "cannon ball"
}
}
}
首选项字段属性¶
| 物业 | 类型 | 描述 |
|---|---|---|
type |
"string"、"number" 或 "boolean" |
偏好值的数据类型。 (Required) |
default |
字符串、数字或布尔值 | 当评估不提供覆盖时使用的默认值。必须与声明的 type 匹配。 (Required) |
enum |
字符串或数字数组 | 允许值的可选列表。如果提供,则默认值和任何评估覆盖都必须在此列表中。 |
评估中最重要的偏好¶
可以通过评估问题编辑器或直接编辑 infoAssessment.json 来设置偏好覆盖。
使用评估问题编辑器¶
在评估问题页面中,单击“编辑”进入编辑模式,然后展开定义首选项的问题。每个首选项均显示其当前覆盖值。从下拉列表中选择一个值或输入 1 以覆盖问题默认值。要恢复问题默认值,请清除该字段或选择“默认”选项。
使用 JSON¶
在 infoAssessment.json 中,将 preferences 对象添加到问题条目中。仅覆盖您指定的首选项;其余的使用默认值。
{
"uuid": "c113ec72-acb7-41fb-8e76-62f2e79cd7f0",
"type": "Homework",
"set": "Homework",
"number": "5",
"title": "Forces on Earth",
"zones": [
{
"questions": [
{
"id": "forces/fallingObject",
"autoPoints": 1,
"preferences": {
"fired_object": "bowling ball",
"gravitational_constant": 9.8
}
}
]
}
]
}
在同步期间根据问题的首选项架构验证覆盖。如果您提供的值与声明的 type 不匹配或不在 enum 列表中,则会报告同步错误。
Warning
对带有共享问题的偏好的支持并不完美;仅在同步时根据共享问题的首选项架构验证首选项。
还可以对问题备选库中的各个备选方案设置首选项:
{
"numberChoose": 1,
"alternatives": [
{
"id": "forces/fallingObject",
"preferences": { "gravitational_constant": 9.8 }
},
{
"id": "kinematics/projectileRange",
"preferences": { "unit_system": "imperial" }
}
]
}
使用 question.html 中的首选项¶
首选项值可在 {{preferences.key}} 命名空间下的 Mustache 模板中使用,就像 {{params.key}} 一样:
<pl-question-panel>
<p>
A {{preferences.fired_object}} with mass $m = {{params.m}} \rm\ kg$
is dropped from a cliff.
</p>
<p>
Assume the acceleration due to gravity is
$g = {{preferences.gravitational_constant}} \rm\ m/s^2$.
</p>
<p>What is the magnitude of the net force acting on the {{preferences.fired_object}}?</p>
</pl-question-panel>
<pl-number-input answers-name="force" label="$F = $"></pl-number-input>
使用 server.py 中的首选项¶
偏好值可在 data["preferences"] 字典中找到。该字典是只读的,可用于所有 server.py 函数(generate、prepare、render、parse、grade、file、test)。
import random
def generate(data):
m = random.choice([2, 5, 10, 15, 20, 25, 50])
g = float(data["preferences"]["gravitational_constant"])
data["params"]["m"] = m
data["correct_answers"]["force"] = m * g
示例¶
配置单位(SI 与英制)¶
一个问题可以通过定义偏好来支持不同的单位系统:
{
"uuid": "...",
"title": "Projectile range",
"topic": "Kinematics",
"type": "v3",
"preferences": {
"unit_system": {
"type": "string",
"enum": ["SI", "imperial"],
"default": "SI"
}
}
}
import random
import math
def generate(data):
if data["preferences"]["unit_system"] == "SI":
g = 9.8
unit = "m"
else:
g = 32.2
unit = "ft"
v0 = random.choice([10, 15, 20, 25])
angle = random.choice([30, 45, 60])
data["params"]["v0"] = v0
data["params"]["angle"] = angle
data["params"]["unit"] = unit
data["correct_answers"]["range"] = round(v0**2 * math.sin(2 * math.radians(angle)) / g, 2)
然后,一项评估可以使用 "preferences": { "unit_system": "SI" },另一项评估可以使用 "preferences": { "unit_system": "imperial" },重复使用同一问题。
调整难度¶
布尔首选项可以控制是否显示提示:
{
"preferences": {
"show_hints": {
"type": "boolean",
"default": true
}
}
}
{{#preferences.show_hints}}
<pl-hidden-hints>
<pl-hint>Remember that $F = ma$.</pl-hint>
</pl-hidden-hints>
{{/preferences.show_hints}}
练习作业可以保留默认的 (true),而考试评估会覆盖它:
{ "id": "forces/fallingObject", "autoPoints": 5, "preferences": { "show_hints": false } }
在具有不同背景的课程实例中重复使用问题¶
有关统计的问题可以更改数据集上下文:
{
"preferences": {
"dataset_context": {
"type": "string",
"enum": ["weather", "stock prices", "exam scores"],
"default": "exam scores"
}
}
}
<pl-question-panel>
<p>Given the following {{preferences.dataset_context}} data, compute the sample mean.</p>
</pl-question-panel>