- 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 David Oct 23 2008
- Add callback mechanism for exercises and exercise sets - Add callback mechanism for exercises and exercise sets
- Default count for exercise sets is N, not 1 - 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 exercise API
$bolt_ex_mode = 0; // The following is a global var accessed by exercise functions.
$bolt_ex_index = 0; //
$bolt_ex_state = 0; $bolt_ex = null;
$bolt_ex_score = 0; $bolt_ex->mode = 0; // input: SHOW/SCORE/ANSWER
$bolt_ex_query_string = ""; $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) { class BoltWeight {
global $bolt_ex_mode; // input public $weight;
global $bolt_ex_index; // input function __construct($weight) {
global $bolt_ex_score; // incremental output if SCORE $this->weight = $weight;
global $bolt_ex_query_string; }
}
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: case BOLT_MODE_SHOW:
shuffle($choices); shuffle($choices);
$i = 0; $i = 0;
start_table(); start_table();
foreach ($choices as $choice) { 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++; $i++;
} }
end_table(); end_table();
@ -46,18 +74,19 @@ function exclusive_choice($choices) {
case BOLT_MODE_SCORE: case BOLT_MODE_SCORE:
$right_ans = $choices[0]; $right_ans = $choices[0];
shuffle($choices); shuffle($choices);
$key = "q_$bolt_ex_index"; $key = "q_$bolt_ex->index";
if (isset($$key)) { if (isset($$key)) {
$response = $$key; $response = $$key;
if ($choices[$response] == $right_ans) { if ($choices[$response] == $right_ans) {
$bolt_ex_score += 1; $bolt_ex->score += 1;
} }
} }
$bolt_ex->weight += $weight;
break; break;
case BOLT_MODE_ANSWER: case BOLT_MODE_ANSWER:
$right_ans = $choices[0]; $right_ans = $choices[0];
shuffle($choices); shuffle($choices);
$key = "q_$bolt_ex_index"; $key = "q_$bolt_ex->index";
if (isset($$key)) { if (isset($$key)) {
$response = $$key; $response = $$key;
} else { } else {
@ -84,25 +113,39 @@ function exclusive_choice($choices) {
end_table(); end_table();
break; break;
} }
$bolt_ex_index++; $bolt_ex->index++;
} }
function inclusive_choice($choices) { function inclusive_choice() {
global $bolt_ex_mode; // input global $bolt_ex;
global $bolt_ex_index; // input $weight = 1;
global $bolt_ex_score; // incremental output if SCORE
global $bolt_ex_query_string;
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: case BOLT_MODE_SHOW:
shuffle($choices); shuffle($choices);
$i = 0; $i = 0;
start_table(); start_table();
foreach ($choices as $choice) { foreach ($choices as $choice) {
$c = $choice[0]; $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++; $i++;
} }
end_table(); end_table();
@ -113,14 +156,15 @@ function inclusive_choice($choices) {
$score = 0; $score = 0;
shuffle($choices); shuffle($choices);
foreach ($choices as $choice) { foreach ($choices as $choice) {
$key = "q_".$bolt_ex_index."_$i"; $key = "q_".$bolt_ex->index."_$i";
$response = isset($$key); $response = isset($$key);
$r = $choice[1]; $r = $choice[1];
$correct = ($r && $response) || (!$r && !$response); $correct = ($r && $response) || (!$r && !$response);
if ($correct) $score += 1./$n; if ($correct) $score += 1./$n;
$i++; $i++;
} }
$bolt_ex_score += $score; $bolt_ex->score += $score;
$bolt_ex->weight += $weight;
break; break;
case BOLT_MODE_ANSWER: case BOLT_MODE_ANSWER:
$i = 0; $i = 0;
@ -130,7 +174,7 @@ function inclusive_choice($choices) {
table_header("Choice", "Correct?", "Your answer"); table_header("Choice", "Correct?", "Your answer");
foreach ($choices as $choice) { foreach ($choices as $choice) {
$c = $choice[0]; $c = $choice[0];
$key = "q_".$bolt_ex_index."_$i"; $key = "q_".$bolt_ex->index."_$i";
$response = isset($$key); $response = isset($$key);
$r = $choice[1]; $r = $choice[1];
$correct = ($r && $response) || (!$r && !$response); $correct = ($r && $response) || (!$r && !$response);
@ -143,18 +187,15 @@ function inclusive_choice($choices) {
end_table(); end_table();
break; break;
} }
$bolt_ex_index++; $bolt_ex->index++;
} }
function image_rect($img, $rect) { function image_rect($img, $rect) {
global $bolt_ex_mode; // input global $bolt_ex;
global $bolt_ex_index; // input
global $bolt_ex_state; // output if SHOW, else input
global $bolt_ex_score; // incremental output if SCORE
parse_str($bolt_ex_query_string); parse_str($bolt_ex->query_string);
switch ($bolt_ex_mode) { switch ($bolt_ex->mode) {
case BOLT_MODE_SHOW: case BOLT_MODE_SHOW:
echo "<img src=$img>"; echo "<img src=$img>";
break; 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) { function show_item($iter, $view_id, $prev_view_id, $mode, $repeat=null) {
global $user; global $user;
global $course; global $course;
global $bolt_ex_mode; global $bolt_ex;
global $bolt_ex_index;
global $bolt_ex_score;
global $bolt_query_string;
global $refresh; global $refresh;
global $url_args; 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>"; $next = "<a href=bolt_sched.php?$url_args&action=next&view_id=$view_id><img src=img/next.gif></a>";
if ($item->is_exercise()) { if ($item->is_exercise()) {
$bolt_ex_mode = $mode; $bolt_ex->mode = $mode;
$bolt_ex_index = 0; $bolt_ex->index = 0;
switch ($mode) { switch ($mode) {
case BOLT_MODE_SHOW: case BOLT_MODE_SHOW:
echo " echo "
@ -220,7 +217,7 @@ function show_item($iter, $view_id, $prev_view_id, $mode, $repeat=null) {
case BOLT_MODE_ANSWER: case BOLT_MODE_ANSWER:
require($item->filename); require($item->filename);
if (function_exists('bolt_divide')) bolt_divide(); 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%"; echo "Score: $score_pct%";
break; 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 // Show the student the results of an old exercise; no navigation items
// //
function show_answer_page($iter, $score) { function show_answer_page($iter, $score) {
global $bolt_ex_mode; global $bolt_ex;
global $bolt_ex_index;
global $bolt_query_string;
$bolt_ex_mode = BOLT_MODE_ANSWER; $bolt_ex->mode = BOLT_MODE_ANSWER;
$bolt_ex_index = 0; $bolt_ex->index = 0;
$item = $iter->item; $item = $iter->item;
page_header(); page_header();
@ -397,8 +392,8 @@ case 'prev':
$v2 = BoltView::lookup_id($view->prev_view_id); $v2 = BoltView::lookup_id($view->prev_view_id);
$result = BoltResult::lookup_id($v2->result_id); $result = BoltResult::lookup_id($v2->result_id);
srand($v2->id); srand($v2->id);
$bolt_ex_score = $result->score; $bolt_ex->score = $result->score;
$bolt_ex_query_string = $result->response; $bolt_ex->query_string = $result->response;
} }
$view_id = create_view($iter, $mode, $view->prev_view_id); $view_id = create_view($iter, $mode, $view->prev_view_id);
show_item($iter, $view_id, $view->prev_view_id, $mode); show_item($iter, $view_id, $view->prev_view_id, $mode);
@ -461,22 +456,22 @@ case 'answer': // submit answer in exercise
// compute the score // compute the score
$bolt_ex_query_string = $_SERVER['QUERY_STRING']; $bolt_ex->query_string = $_SERVER['QUERY_STRING'];
$bolt_ex_mode = BOLT_MODE_SCORE; $bolt_ex->mode = BOLT_MODE_SCORE;
$bolt_ex_index = 0; $bolt_ex->index = 0;
$bolt_ex_score = 0; $bolt_ex->score = 0;
$bolt_query_string = $item->query_string; $bolt_query_string = $item->query_string;
srand($view_id); srand($view_id);
ob_start(); // buffer output to avoid showing exercise text ob_start(); // buffer output to avoid showing exercise text
require($item->filename); require($item->filename);
ob_end_clean(); ob_end_clean();
$bolt_ex_score /= $bolt_ex_index; $bolt_ex->score /= $bolt_ex->index;
if ($item->callback) { if ($item->callback) {
$user->bolt->attrs = unserialize($user->bolt->attrs); $user->bolt->attrs = unserialize($user->bolt->attrs);
call_user_func( 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); $user->bolt->attrs = serialize($user->bolt->attrs);
$attrs = $user->bolt->attrs; $attrs = $user->bolt->attrs;
@ -489,7 +484,7 @@ case 'answer': // submit answer in exercise
$now = time(); $now = time();
$result_id = BoltResult::insert( $result_id = BoltResult::insert(
"(create_time, user_id, course_id, view_id, item_name, score, response) "(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"); $view->update("result_id=$result_id");
@ -499,7 +494,7 @@ case 'answer': // submit answer in exercise
$xset = $iter->xset; $xset = $iter->xset;
if ($xset) { if ($xset) {
$is_last = $xset->xset_record_score( $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 ($repeat) $repeat->avg_score = $avg_score;
if ($is_last) { if ($is_last) {
@ -553,7 +548,7 @@ case 'answer_page':
} }
$result = BoltResult::lookup_id($view->result_id); $result = BoltResult::lookup_id($view->result_id);
srand($view_id); srand($view_id);
$bolt_ex_query_string = $result->response; $bolt_ex->query_string = $result->response;
show_answer_page($iter, $result->score); show_answer_page($iter, $result->score);
break; break;
case 'course_home': case 'course_home':
@ -636,9 +631,9 @@ case 'resume':
$view_orig = BoltView::lookup_id($view->prev_view_id); $view_orig = BoltView::lookup_id($view->prev_view_id);
$result = BoltResult::lookup_id($view_orig->result_id); $result = BoltResult::lookup_id($view_orig->result_id);
srand($view_orig->id); srand($view_orig->id);
$bolt_ex_query_string = $result->response; $bolt_ex->query_string = $result->response;
$bolt_ex_score = $result->score; $bolt_ex->score = $result->score;
$bolt_ex_index = 0; $bolt_ex->index = 0;
$view_id = create_view($iter, $mode, $view_orig->id); $view_id = create_view($iter, $mode, $view_orig->id);
show_item($iter, $view_id, $view_orig->id, $mode); show_item($iter, $view_id, $view_orig->id, $mode);
} else { } else {