- Bolt stuff

svn path=/trunk/boinc/; revision=16313
This commit is contained in:
David Anderson 2008-10-24 20:09:16 +00:00
parent e859261535
commit b5a83796b4
3 changed files with 109 additions and 61 deletions

View File

@ -234,3 +234,15 @@ David Oct 20 2008
David Oct 23 2008
- Add callback mechanism for exercises and exercise sets
- Default count for exercise sets is N, not 1
- Switch from JSON to PHP serialization
(NOTE: this requires cleaning out your DB)
David Oct 23 2008
- Change the info passed to/from exercise functions from
bunch of globals to a single global struct
- Change the arguments to exercise functions
to allow for a weight() arg
html/
inc/
bolt_ex.inc

View File

@ -18,27 +18,55 @@
// Bolt exercise API
$bolt_ex_mode = 0;
$bolt_ex_index = 0;
$bolt_ex_state = 0;
$bolt_ex_score = 0;
$bolt_ex_query_string = "";
// The following is a global var accessed by exercise functions.
//
$bolt_ex = null;
$bolt_ex->mode = 0; // input: SHOW/SCORE/ANSWER
$bolt_ex->index = 0; // input: sequence of this exercise in file
$bolt_ex->score = 0; // input/output: cumulative score (if mode = SCORE)
$bolt_ex->weight = 0; // input/output: cumulative weight
$bolt_ex->query_string = ""; // user's response (if SCORE or ANSWER)
function exclusive_choice($choices) {
global $bolt_ex_mode; // input
global $bolt_ex_index; // input
global $bolt_ex_score; // incremental output if SCORE
global $bolt_ex_query_string;
class BoltWeight {
public $weight;
function __construct($weight) {
$this->weight = $weight;
}
}
parse_str($bolt_ex_query_string);
function weight($w) {
return new BoltWeight($w);
}
switch ($bolt_ex_mode) {
function exclusive_choice() {
global $bolt_ex;
$weight = 1;
$choices = array();
$args = func_get_args();
foreach ($args as $arg) {
if (is_string($arg)) {
$choices[] = $arg;
} else if (is_object($arg)) {
if (get_class($arg) == 'BoltWeight') {
$weight = $arg->weight;
} else {
"bad arg to exclusive_choice()";
}
} else {
"bad arg to exclusive_choice()";
}
}
parse_str($bolt_ex->query_string);
switch ($bolt_ex->mode) {
case BOLT_MODE_SHOW:
shuffle($choices);
$i = 0;
start_table();
foreach ($choices as $choice) {
row2($choice, "<input name=q_$bolt_ex_index type=radio value=$i>");
row2($choice, "<input name=q_$bolt_ex->index type=radio value=$i>");
$i++;
}
end_table();
@ -46,18 +74,19 @@ function exclusive_choice($choices) {
case BOLT_MODE_SCORE:
$right_ans = $choices[0];
shuffle($choices);
$key = "q_$bolt_ex_index";
$key = "q_$bolt_ex->index";
if (isset($$key)) {
$response = $$key;
if ($choices[$response] == $right_ans) {
$bolt_ex_score += 1;
$bolt_ex->score += 1;
}
}
$bolt_ex->weight += $weight;
break;
case BOLT_MODE_ANSWER:
$right_ans = $choices[0];
shuffle($choices);
$key = "q_$bolt_ex_index";
$key = "q_$bolt_ex->index";
if (isset($$key)) {
$response = $$key;
} else {
@ -84,25 +113,39 @@ function exclusive_choice($choices) {
end_table();
break;
}
$bolt_ex_index++;
$bolt_ex->index++;
}
function inclusive_choice($choices) {
global $bolt_ex_mode; // input
global $bolt_ex_index; // input
global $bolt_ex_score; // incremental output if SCORE
global $bolt_ex_query_string;
function inclusive_choice() {
global $bolt_ex;
$weight = 1;
parse_str($bolt_ex_query_string);
$choices = array();
$args = func_get_args();
foreach ($args as $arg) {
if (is_array($arg)) {
$choices[] = $arg;
} else if (is_object($arg)) {
if (get_class($arg) == 'BoltWeight') {
$weight = $arg->weight;
} else {
"bad arg to inclusive_choice()";
}
} else {
"bad arg to inclusive_choice()";
}
}
switch ($bolt_ex_mode) {
parse_str($bolt_ex->query_string);
switch ($bolt_ex->mode) {
case BOLT_MODE_SHOW:
shuffle($choices);
$i = 0;
start_table();
foreach ($choices as $choice) {
$c = $choice[0];
row2("<input name=q_".$bolt_ex_index."_$i type=checkbox>", $c);
row2("<input name=q_".$bolt_ex->index."_$i type=checkbox>", $c);
$i++;
}
end_table();
@ -113,14 +156,15 @@ function inclusive_choice($choices) {
$score = 0;
shuffle($choices);
foreach ($choices as $choice) {
$key = "q_".$bolt_ex_index."_$i";
$key = "q_".$bolt_ex->index."_$i";
$response = isset($$key);
$r = $choice[1];
$correct = ($r && $response) || (!$r && !$response);
if ($correct) $score += 1./$n;
$i++;
}
$bolt_ex_score += $score;
$bolt_ex->score += $score;
$bolt_ex->weight += $weight;
break;
case BOLT_MODE_ANSWER:
$i = 0;
@ -130,7 +174,7 @@ function inclusive_choice($choices) {
table_header("Choice", "Correct?", "Your answer");
foreach ($choices as $choice) {
$c = $choice[0];
$key = "q_".$bolt_ex_index."_$i";
$key = "q_".$bolt_ex->index."_$i";
$response = isset($$key);
$r = $choice[1];
$correct = ($r && $response) || (!$r && !$response);
@ -143,18 +187,15 @@ function inclusive_choice($choices) {
end_table();
break;
}
$bolt_ex_index++;
$bolt_ex->index++;
}
function image_rect($img, $rect) {
global $bolt_ex_mode; // input
global $bolt_ex_index; // input
global $bolt_ex_state; // output if SHOW, else input
global $bolt_ex_score; // incremental output if SCORE
global $bolt_ex;
parse_str($bolt_ex_query_string);
parse_str($bolt_ex->query_string);
switch ($bolt_ex_mode) {
switch ($bolt_ex->mode) {
case BOLT_MODE_SHOW:
echo "<img src=$img>";
break;

View File

@ -178,10 +178,7 @@ function show_nav($links, $up_link, $view_id) {
function show_item($iter, $view_id, $prev_view_id, $mode, $repeat=null) {
global $user;
global $course;
global $bolt_ex_mode;
global $bolt_ex_index;
global $bolt_ex_score;
global $bolt_query_string;
global $bolt_ex;
global $refresh;
global $url_args;
@ -197,8 +194,8 @@ function show_item($iter, $view_id, $prev_view_id, $mode, $repeat=null) {
$next = "<a href=bolt_sched.php?$url_args&action=next&view_id=$view_id><img src=img/next.gif></a>";
if ($item->is_exercise()) {
$bolt_ex_mode = $mode;
$bolt_ex_index = 0;
$bolt_ex->mode = $mode;
$bolt_ex->index = 0;
switch ($mode) {
case BOLT_MODE_SHOW:
echo "
@ -220,7 +217,7 @@ function show_item($iter, $view_id, $prev_view_id, $mode, $repeat=null) {
case BOLT_MODE_ANSWER:
require($item->filename);
if (function_exists('bolt_divide')) bolt_divide();
$score_pct = number_format($bolt_ex_score*100);
$score_pct = number_format($bolt_ex->score*100);
echo "Score: $score_pct%";
break;
}
@ -269,12 +266,10 @@ function show_item($iter, $view_id, $prev_view_id, $mode, $repeat=null) {
// Show the student the results of an old exercise; no navigation items
//
function show_answer_page($iter, $score) {
global $bolt_ex_mode;
global $bolt_ex_index;
global $bolt_query_string;
global $bolt_ex;
$bolt_ex_mode = BOLT_MODE_ANSWER;
$bolt_ex_index = 0;
$bolt_ex->mode = BOLT_MODE_ANSWER;
$bolt_ex->index = 0;
$item = $iter->item;
page_header();
@ -397,8 +392,8 @@ case 'prev':
$v2 = BoltView::lookup_id($view->prev_view_id);
$result = BoltResult::lookup_id($v2->result_id);
srand($v2->id);
$bolt_ex_score = $result->score;
$bolt_ex_query_string = $result->response;
$bolt_ex->score = $result->score;
$bolt_ex->query_string = $result->response;
}
$view_id = create_view($iter, $mode, $view->prev_view_id);
show_item($iter, $view_id, $view->prev_view_id, $mode);
@ -461,22 +456,22 @@ case 'answer': // submit answer in exercise
// compute the score
$bolt_ex_query_string = $_SERVER['QUERY_STRING'];
$bolt_ex_mode = BOLT_MODE_SCORE;
$bolt_ex_index = 0;
$bolt_ex_score = 0;
$bolt_ex->query_string = $_SERVER['QUERY_STRING'];
$bolt_ex->mode = BOLT_MODE_SCORE;
$bolt_ex->index = 0;
$bolt_ex->score = 0;
$bolt_query_string = $item->query_string;
srand($view_id);
ob_start(); // buffer output to avoid showing exercise text
require($item->filename);
ob_end_clean();
$bolt_ex_score /= $bolt_ex_index;
$bolt_ex->score /= $bolt_ex->index;
if ($item->callback) {
$user->bolt->attrs = unserialize($user->bolt->attrs);
call_user_func(
$item->callback, $user, $bolt_ex_score, $bolt_ex_query_string
$item->callback, $user, $bolt_ex->score, $bolt_ex->query_string
);
$user->bolt->attrs = serialize($user->bolt->attrs);
$attrs = $user->bolt->attrs;
@ -489,7 +484,7 @@ case 'answer': // submit answer in exercise
$now = time();
$result_id = BoltResult::insert(
"(create_time, user_id, course_id, view_id, item_name, score, response)
values ($now, $user->id, $course->id, $view->id, '$view->item_name', $bolt_ex_score, '$qs')"
values ($now, $user->id, $course->id, $view->id, '$view->item_name', $bolt_ex->score, '$qs')"
);
$view->update("result_id=$result_id");
@ -499,7 +494,7 @@ case 'answer': // submit answer in exercise
$xset = $iter->xset;
if ($xset) {
$is_last = $xset->xset_record_score(
$iter, $bolt_ex_score, $view->id, $avg_score, $repeat
$iter, $bolt_ex->score, $view->id, $avg_score, $repeat
);
if ($repeat) $repeat->avg_score = $avg_score;
if ($is_last) {
@ -553,7 +548,7 @@ case 'answer_page':
}
$result = BoltResult::lookup_id($view->result_id);
srand($view_id);
$bolt_ex_query_string = $result->response;
$bolt_ex->query_string = $result->response;
show_answer_page($iter, $result->score);
break;
case 'course_home':
@ -636,9 +631,9 @@ case 'resume':
$view_orig = BoltView::lookup_id($view->prev_view_id);
$result = BoltResult::lookup_id($view_orig->result_id);
srand($view_orig->id);
$bolt_ex_query_string = $result->response;
$bolt_ex_score = $result->score;
$bolt_ex_index = 0;
$bolt_ex->query_string = $result->response;
$bolt_ex->score = $result->score;
$bolt_ex->index = 0;
$view_id = create_view($iter, $mode, $view_orig->id);
show_item($iter, $view_id, $view_orig->id, $mode);
} else {