2007-10-30 22:31:13 +00:00
|
|
|
<?php
|
|
|
|
|
2007-11-29 02:56:10 +00:00
|
|
|
// Bolt scheduler. GET args:
|
2007-10-30 22:31:13 +00:00
|
|
|
// course_id: course ID
|
|
|
|
// action:
|
2007-11-29 02:56:10 +00:00
|
|
|
// 'start' or none: show current (or first) item,
|
|
|
|
// and prompt for user info if any missing
|
2007-10-30 22:31:13 +00:00
|
|
|
// 'next': go to next lesson
|
|
|
|
// answers:
|
|
|
|
// JSON represenation of exercise answers
|
|
|
|
|
|
|
|
require_once("../inc/bolt.inc");
|
|
|
|
require_once("../inc/bolt_db.inc");
|
2007-11-29 23:26:49 +00:00
|
|
|
require_once("../inc/bolt_ex.inc");
|
2007-12-07 23:23:25 +00:00
|
|
|
require_once("../inc/bolt_util.inc");
|
2007-10-30 22:31:13 +00:00
|
|
|
require_once("../inc/util.inc");
|
|
|
|
|
|
|
|
$user = get_logged_in_user();
|
2007-11-29 02:56:10 +00:00
|
|
|
BoltUser::lookup($user);
|
2007-10-30 22:31:13 +00:00
|
|
|
$course_id = get_int('course_id');
|
|
|
|
$view_id = get_int('view_id', true);
|
|
|
|
$action = get_str('action', true);
|
|
|
|
|
|
|
|
$course = BoltCourse::lookup_id($course_id);
|
|
|
|
if (!$course) {
|
|
|
|
error_page("no such course");
|
|
|
|
}
|
|
|
|
|
2007-11-29 23:26:49 +00:00
|
|
|
function update_info() {
|
2007-11-29 02:56:10 +00:00
|
|
|
$sex = get_int('sex');
|
|
|
|
$birth_year = get_int('birth_year');
|
|
|
|
$user->bolt->update("sex=$sex, birth_year=$birth_year");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-30 22:31:13 +00:00
|
|
|
$course_doc = require_once($course->doc_file);
|
|
|
|
|
2007-12-07 23:23:25 +00:00
|
|
|
function finalize_view($user, $view_id, $action) {
|
2007-11-29 23:26:49 +00:00
|
|
|
if (!$view_id) return null;
|
2007-10-30 22:31:13 +00:00
|
|
|
$view = BoltView::lookup_id($view_id);
|
|
|
|
if ($view && $view->user_id == $user->id && !$view->end_time) {
|
|
|
|
$now = time();
|
2007-12-07 23:23:25 +00:00
|
|
|
$view->update("end_time=$now, action=$action");
|
|
|
|
return $view;
|
2007-10-30 22:31:13 +00:00
|
|
|
}
|
2007-12-07 23:23:25 +00:00
|
|
|
return null;
|
2007-10-30 22:31:13 +00:00
|
|
|
}
|
|
|
|
|
2007-12-07 23:23:25 +00:00
|
|
|
function default_mode($item) {
|
|
|
|
return $item->is_exercise()?BOLT_MODE_SHOW:BOLT_MODE_LESSON;
|
|
|
|
}
|
2007-10-30 22:31:13 +00:00
|
|
|
|
2007-12-07 23:23:25 +00:00
|
|
|
function create_view($user, $course, $item, $mode) {
|
2007-11-29 02:56:10 +00:00
|
|
|
$now = time();
|
2007-12-07 23:23:25 +00:00
|
|
|
return BoltView::insert("(user_id, course_id, item_name, start_time, mode) values ($user->id, $course->id, '$item->name', $now, $mode)");
|
2007-10-30 22:31:13 +00:00
|
|
|
}
|
|
|
|
|
2007-11-29 23:26:49 +00:00
|
|
|
function get_current_item($e, $course_doc) {
|
|
|
|
$frac_done = 0;
|
|
|
|
$iter = new BoltIter($course_doc);
|
2007-12-04 22:16:37 +00:00
|
|
|
$iter->decode_state($e->state);
|
|
|
|
$iter->at();
|
|
|
|
return $iter;
|
2007-11-29 23:26:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_next_item($e, $course_doc) {
|
|
|
|
$iter = new BoltIter($course_doc);
|
2007-12-04 22:16:37 +00:00
|
|
|
$iter->decode_state($e->state);
|
|
|
|
$iter->next();
|
|
|
|
$state = $iter->encode_state();
|
2007-11-29 23:26:49 +00:00
|
|
|
$e->update("state='$state'");
|
2007-12-04 22:16:37 +00:00
|
|
|
return $iter;
|
2007-10-30 22:31:13 +00:00
|
|
|
}
|
|
|
|
|
2007-12-07 23:23:25 +00:00
|
|
|
function show_item(
|
|
|
|
$item, $frac_done, $user, $course, $e, $view_id, $mode
|
|
|
|
) {
|
|
|
|
global $bolt_ex_mode;
|
|
|
|
global $bolt_ex_index;
|
|
|
|
|
2007-11-29 23:26:49 +00:00
|
|
|
$now = time();
|
|
|
|
$e->update("last_view=$now, fraction_done=$frac_done");
|
|
|
|
|
|
|
|
if ($item->is_exercise()) {
|
2007-12-07 23:23:25 +00:00
|
|
|
$bolt_ex_mode = $mode;
|
2007-11-29 23:26:49 +00:00
|
|
|
$bolt_ex_index = 0;
|
2007-12-07 23:23:25 +00:00
|
|
|
switch ($mode) {
|
|
|
|
case BOLT_MODE_SHOW:
|
|
|
|
echo "
|
|
|
|
<form action=bolt_sched.php>
|
|
|
|
<input type=hidden name=view_id value=$view_id>
|
|
|
|
<input type=hidden name=course_id value=$course->id>
|
|
|
|
<input type=hidden name=action value=answer>
|
|
|
|
";
|
|
|
|
srand($view_id);
|
|
|
|
require($item->filename);
|
|
|
|
echo "<p><input type=submit value=OK>";
|
|
|
|
break;
|
|
|
|
case BOLT_MODE_ANSWER:
|
|
|
|
require($item->filename);
|
|
|
|
echo "<p><a href=bolt_sched.php?course_id=$course->id&action=next&view_id=$view_id>Next</a>";
|
|
|
|
break;
|
|
|
|
}
|
2007-11-29 23:26:49 +00:00
|
|
|
} else {
|
|
|
|
require_once($item->filename);
|
|
|
|
echo "<p><a href=bolt_sched.php?course_id=$course->id&action=next&view_id=$view_id>Next</a>";
|
|
|
|
}
|
2007-10-30 22:31:13 +00:00
|
|
|
|
2007-12-07 23:23:25 +00:00
|
|
|
echo "<p>Fraction done: $frac_done
|
|
|
|
<a href=bolt_course.php?course_id=$course->id>Course history</a>
|
|
|
|
";
|
|
|
|
}
|
|
|
|
|
|
|
|
function start_course($user, $course, $course_doc) {
|
|
|
|
BoltEnrollment::delete($user->id, $course->id);
|
|
|
|
$iter = new BoltIter($course_doc);
|
|
|
|
$iter->at();
|
|
|
|
|
|
|
|
$now = time();
|
|
|
|
print_r($iter->state);
|
|
|
|
$state = $iter->encode_state();
|
|
|
|
BoltEnrollment::insert("(create_time, user_id, course_id, state) values ($now, $user->id, $course->id, '$state')");
|
|
|
|
$e = BoltEnrollment::lookup($user->id, $course->id);
|
|
|
|
$mode = default_mode($iter->item);
|
|
|
|
$view_id = create_view($user, $course, $iter->item, $mode);
|
|
|
|
show_item($iter->item, 0, $user, $course, $e, $view_id, $mode);
|
2007-11-29 23:26:49 +00:00
|
|
|
}
|
|
|
|
|
2007-12-04 22:16:37 +00:00
|
|
|
$e = BoltEnrollment::lookup($user->id, $course_id);
|
2007-11-29 23:26:49 +00:00
|
|
|
switch ($action) {
|
|
|
|
case 'start':
|
|
|
|
if (info_incomplete($user)) {
|
|
|
|
request_info($user, $course);
|
|
|
|
exit();
|
|
|
|
}
|
2007-12-04 22:16:37 +00:00
|
|
|
if ($e) {
|
|
|
|
page_head("Confirm start");
|
|
|
|
echo "You are already enrolled in $course->name.
|
|
|
|
Are you sure you want to start from the beginning?
|
|
|
|
";
|
|
|
|
show_button(
|
|
|
|
"bolt_sched.php?action=start_confirm&course_id=$course->id",
|
|
|
|
"Yes",
|
|
|
|
"Start this course from the beginning"
|
|
|
|
);
|
|
|
|
page_tail();
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
case 'start_confirm':
|
2007-11-29 23:26:49 +00:00
|
|
|
start_course($user, $course, $course_doc);
|
|
|
|
break;
|
|
|
|
case 'update_info':
|
|
|
|
update_info();
|
|
|
|
start_course($user, $course, $course_doc);
|
|
|
|
case 'next': // "next" button in lesson or exercise answer page
|
2007-12-07 23:23:25 +00:00
|
|
|
$view = finalize_view($user, $view_id, BOLT_ACTION_NEXT);
|
2007-12-04 22:16:37 +00:00
|
|
|
$iter = get_next_item($e, $course_doc);
|
|
|
|
if (!$iter->item) {
|
2007-11-29 23:26:49 +00:00
|
|
|
page_head("Done with course");
|
|
|
|
echo "All done!";
|
|
|
|
page_tail();
|
|
|
|
exit();
|
|
|
|
}
|
2007-12-07 23:23:25 +00:00
|
|
|
$mode = default_mode($iter->item);
|
|
|
|
$view_id = create_view($user, $course, $iter->item, $mode);
|
|
|
|
show_item($iter->item, $iter->frac_done, $user, $course, $e, $view_id, $mode);
|
2007-11-29 23:26:49 +00:00
|
|
|
break;
|
|
|
|
case 'answer': // submit answer in exercise
|
2007-12-07 23:23:25 +00:00
|
|
|
$view = finalize_view($user, $view_id, BOLT_ACTION_SUBMIT);
|
2007-12-04 22:16:37 +00:00
|
|
|
$iter = get_current_item($e, $course_doc);
|
|
|
|
$item = $iter->item;
|
2007-11-29 23:26:49 +00:00
|
|
|
if (!$item->is_exercise()) {
|
|
|
|
error_page("expected an exercise");
|
|
|
|
}
|
|
|
|
if ($view->item_name != $item->name) {
|
|
|
|
error_page("unexpected name");
|
|
|
|
}
|
|
|
|
$bolt_ex_mode = BOLT_MODE_SCORE;
|
|
|
|
$bolt_ex_index = 0;
|
|
|
|
$bolt_ex_score = 0;
|
2007-12-07 23:23:25 +00:00
|
|
|
$bolt_ex_response = "";
|
|
|
|
srand($view_id);
|
2007-11-30 00:38:38 +00:00
|
|
|
ob_start(); // turn on output buffering
|
2007-11-29 23:26:49 +00:00
|
|
|
require($item->filename);
|
2007-11-30 00:38:38 +00:00
|
|
|
ob_end_clean();
|
2007-12-07 23:23:25 +00:00
|
|
|
|
|
|
|
$result_id = BoltResult::insert(
|
|
|
|
"(view_id, score, response)
|
|
|
|
values ($view->id, $bolt_ex_score, '$bolt_ex_response')"
|
|
|
|
);
|
|
|
|
$view->update("result_id=$result_id");
|
|
|
|
srand($view_id);
|
|
|
|
$view_id = create_view($user, $course, $item, BOLT_MODE_ANSWER);
|
|
|
|
show_item(
|
|
|
|
$iter->item, $iter->frac_done, $user, $course, $e,
|
|
|
|
$view_id, BOLT_MODE_ANSWER
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$iter = get_current_item($e, $course_doc);
|
|
|
|
$mode = default_mode($iter->item);
|
|
|
|
$view_id = create_view($user, $course, $iter->item, $mode);
|
|
|
|
show_item($iter->item, $iter->frac_done, $user, $course, $e, $view_id, $mode);
|
2007-11-29 23:26:49 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-11-29 02:56:10 +00:00
|
|
|
|
2007-10-30 22:31:13 +00:00
|
|
|
?>
|