course)) { error("Course is misconfigured"); } if (! $lesson = get_record("lesson", "id", $cm->instance)) { error("Course module is incorrect"); } require_login($course->id); // set up some general variables $usehtmleditor = can_use_html_editor(); $navigation = ""; if ($course->category) { $navigation = "id\">$course->shortname ->"; } $strlessons = get_string("modulenameplural", "lesson"); $strlesson = get_string("modulename", "lesson"); $strlessonname = $lesson->name; // ... print the header and... print_header("$course->shortname: $lesson->name", "$course->fullname", "$navigation id>$strlessons -> id\">$lesson->name -> $action", "", "", true); //...get the action require_variable($action); /************** add page ************************************/ if ($action == 'addpage' ) { if (!isteacher($course->id)) { error("Only teachers can look at this page"); } // first get the preceeding page $pageid = $_GET['pageid']; // set of jump array $jump[0] = get_string("thispage", "lesson"); $jump[LESSON_NEXTPAGE] = get_string("nextpage", "lesson"); $jump[LESSON_EOL] = get_string("endoflesson", "lesson"); if (!$apageid = get_field("lesson_pages", "id", "lessonid", $lesson->id, "prevpageid", 0)) { error("Add page: first page not found"); } while (true) { if ($apageid) { $title = get_field("lesson_pages", "title", "id", $apageid); $jump[$apageid] = $title; $apageid = get_field("lesson_pages", "nextpageid", "id", $apageid); } else { // last page reached break; } } // give teacher a blank proforma print_heading_with_help(get_string("addanewpage", "lesson"), "overview", "lesson"); ?>
id)) { error("Only teachers can look at this page"); } if (empty($_GET['pageid'])) { error("Confirm delete: pageid missing"); } $pageid = $_GET['pageid']; if (!$thispage = get_record("lesson_pages", "id", $pageid)) { error("Confirm delete: the page record not found"); } print_heading(get_string("deletingpage", "lesson", $thispage->title)); // print the jumps to this page if ($answers = get_records_select("lesson_answers", "lessonid = $lesson->id AND jumpto = $pageid + 1")) { print_heading(get_string("thefollowingpagesjumptothispage", "lesson")); echo "\n";
foreach ($answers as $answer) {
if (!$title = get_field("lesson_pages", "title", "id", $answer->pageid)) {
error("Confirm delete: page title not found");
}
echo $title."
\n";
}
}
notice_yesno(get_string("confirmdeletionofthispage","lesson"),
"lesson.php?action=delete&id=$cm->id&pageid=$pageid]",
"view.php?id=$cm->id");
}
/****************** continue ************************************/
elseif ($action == 'continue' ) {
// record answer (if necessary) and show response (if none say if answer is correct or not)
if (empty($_POST['pageid'])) {
error("Continue: pageid missing");
}
$pageid = $_POST['pageid'];
if (!$page = get_record("lesson_pages", "id", $pageid)) {
error("Continue: Page record not found");
}
// set up some defaults
$answerid = 0;
$noanswer = false;
$correctanswer = false;
$newpageid = 0; // stay on the page
switch ($page->qtype) {
case LESSON_SHORTANSWER :
if (!$useranswer = $_POST['answer']) {
$noanswer = true;
break;
}
if (!$answers = get_records("lesson_answers", "pageid", $pageid, "id")) {
error("Continue: No answers found");
}
foreach ($answers as $answer) {
if (lesson_iscorrect($pageid, $answer->jumpto)) {
if ($page->qoption) {
// case sensitive
if ($answer->answer == $useranswer) {
$correctanswer = true;
$newpageid = $answer->jumpto;
if (trim(strip_tags($answer->response))) {
$response = $answer->response;
}
}
} else {
// case insensitive
if (strcasecmp($answer->answer, $useranswer) == 0) {
$correctanswer = true;
$newpageid = $answer->jumpto;
if (trim(strip_tags($answer->response))) {
$response = $answer->response;
}
}
}
} else {
// see if user typed in any of the wrong answers
// don't worry about case
if (strcasecmp($answer->answer, $useranswer) == 0) {
$newpageid = $answer->jumpto;
if (trim(strip_tags($answer->response))) {
$response = $answer->response;
}
}
}
}
if (!isset($response)) {
if ($correctanswer) {
$response = get_string("thatsthecorrectanswer", "lesson");
} else {
$response = get_string("thatsthewronganswer", "lesson");
}
}
break;
case LESSON_TRUEFALSE :
if (empty($_POST['answerid'])) {
$noanswer = true;
break;
}
$answerid = $_POST['answerid'];
if (!$answer = get_record("lesson_answers", "id", $answerid)) {
error("Continue: answer record not found");
}
if (lesson_iscorrect($pageid, $answer->jumpto)) {
$correctanswer = true;
}
$newpageid = $answer->jumpto;
if (!$response = trim($answer->response)) {
if ($correctanswer) {
$response = get_string("thatsthecorrectanswer", "lesson");
} else {
$response = get_string("thatsthewronganswer", "lesson");
}
}
break;
case LESSON_MULTICHOICE :
if ($page->qoption) {
// MULTIANSWER allowed, user's answer is an array
if (isset($_POST['answer'])) {
$useranswers = $_POST['answer'];
} else {
$noanswer = true;
break;
}
if (!$answers = get_records("lesson_answers", "pageid", $pageid, "id")) {
error("Continue: No answers found");
}
$ncorrect = 0;
$nhits = 0;
$correctresponse = '';
$wrongresponse = '';
foreach ($answers as $answer) {
if (lesson_iscorrect($pageid, $answer->jumpto)) {
$ncorrect++;
foreach ($useranswers as $key => $answerid) {
if ($answerid == $answer->id) {
$nhits++;
}
}
// save the first jumpto page id, may be needed!...
if (!isset($correctpageid)) {
// leave in its "raw" state - will converted into a proper page id later
$correctpageid = $answer->jumpto;
}
// ...also save any response from the correct answers...
if (trim(strip_tags($answer->response))) {
$correctresponse = $answer->response;
}
} else {
// save the first jumpto page id, may be needed!...
if (!isset($wrongpageid)) {
// leave in its "raw" state - will converted into a proper page id later
$wrongpageid = $answer->jumpto;
}
// ...and from the incorrect ones, don't know which to use at this stage
if (trim(strip_tags($answer->response))) {
$wrongresponse = $answer->response;
}
}
}
if ((count($useranswers) == $ncorrect) and ($nhits == $ncorrect)) {
$correctanswer = true;
if (!$response = $correctresponse) {
$response = get_string("thatsthecorrectanswer", "lesson");
}
$newpageid = $correctpageid;
} else {
if (!$response = $wrongresponse) {
$response = get_string("thatsthewronganswer", "lesson");
}
$newpageid = $wrongpageid;
}
} else {
// only one answer allowed
if (empty($_POST['answerid'])) {
$noanswer = true;
break;
}
$answerid = $_POST['answerid'];
if (!$answer = get_record("lesson_answers", "id", $answerid)) {
error("Continue: answer record not found");
}
if (lesson_iscorrect($pageid, $answer->jumpto)) {
$correctanswer = true;
}
$newpageid = $answer->jumpto;
if (!$response = trim($answer->response)) {
if ($correctanswer) {
$response = get_string("thatsthecorrectanswer", "lesson");
} else {
$response = get_string("thatsthewronganswer", "lesson");
}
}
}
break;
case LESSON_MATCHING :
if (isset($_POST['response'])) {
$response = $_POST['response'];
} else {
$noanswer = true;
break;
}
if (!$answers = get_records("lesson_answers", "pageid", $pageid, "id")) {
error("Continue: No answers found");
}
$ncorrect = 0;
$i = 0;
foreach ($answers as $answer) {
if ($answer->response == $response[$answer->id]) {
$ncorrect++;
}
if ($i == 0) {
$correctpageid = $answer->jumpto;
}
if ($i == 1) {
$wrongpageid = $answer->jumpto;
}
$i++;
}
if ($ncorrect == count($answers)) {
$response = get_string("thatsthecorrectanswer", "lesson");
$newpageid = $correctpageid;
$correctanswer = true;
} else {
$response = get_string("numberofcorrectmatches", "lesson", $ncorrect);
$newpageid = $wrongpageid;
}
break;
case LESSON_NUMERICAL :
// set defaults
$response = '';
$newpageid = 0;
if (!$useranswer = (float) $_POST['answer']) {
$noanswer = true;
break;
}
if (!$answers = get_records("lesson_answers", "pageid", $pageid, "id")) {
error("Continue: No answers found");
}
foreach ($answers as $answer) {
if (strpos($answer->answer, ':')) {
// there's a pairs of values
list($min, $max) = explode(':', $answer->answer);
$minimum = (float) $min;
$maximum = (float) $max;
} else {
// there's only one value
$minimum = (float) $answer->answer;
$maximum = $minimum;
}
if (($useranswer >= $minimum) and ($useranswer <= $maximum)) {
$newpageid = $answer->jumpto;
$response = trim($answer->response);
if (lesson_iscorrect($pageid, $newpageid)) {
$correctanswer = true;
}
break;
}
}
if ($correctanswer) {
if (!$response) {
$response = get_string("thatsthecorrectanswer", "lesson");
}
} else {
if (!$response) {
$response = get_string("thatsthewronganswer", "lesson");
}
}
break;
}
if ($noanswer) {
$newpageid = $pageid; // display same page again
print_simple_box(get_string("noanswer", "lesson"), "center");
} else {
$ntries = count_records("lesson_grades", "lessonid", $lesson->id, "userid", $USER->id);
if (isstudent($course->id)) {
// record student's attempt
$attempt->lessonid = $lesson->id;
$attempt->pageid = $pageid;
$attempt->userid = $USER->id;
$attempt->answerid = $answerid;
$attempt->retry = $ntries;
$attempt->correct = $correctanswer;
$attempt->timeseen = time();
if (!$newattemptid = insert_record("lesson_attempts", $attempt)) {
error("Continue: attempt not inserted");
}
if (!$correctanswer and ($newpageid == 0)) {
// wrong answer and student is stuck on this page - check how many attempts
// the student has had at this page/question
$nattempts = count_records("lesson_attempts", "pageid", $pageid, "userid", $USER->id,
"retry", $ntries);
if ($nattempts >= $lesson->maxattempts) {
print_heading(get_string("maximumnumberofattempts", "lesson")."
".
get_string("movingtonextpage", "lesson"));
$newpageid = LESSON_NEXTPAGE;
}
}
}
// convert jumpto page into a proper page id
if ($newpageid == 0) {
$newpageid = $pageid;
} elseif ($newpageid == LESSON_NEXTPAGE) {
if ($lesson->nextpagedefault) {
// in Flash Card mode...
// ... first get the page ids (lessonid the 5th param is needed to make get_records play)
$allpages = get_records("lesson_pages", "lessonid", $lesson->id, "id", "id,lessonid");
shuffle ($allpages);
$found = false;
if ($lesson->nextpagedefault == LESSON_UNSEENPAGE) {
foreach ($allpages as $thispage) {
if (!count_records("lesson_attempts", "pageid", $thispage->id, "userid",
$USER->id, "retry", $ntries)) {
$found = true;
break;
}
}
} elseif ($lesson->nextpagedefault == LESSON_UNANSWEREDPAGE) {
foreach ($allpages as $thispage) {
if (!count_records_select("lesson_attempts", "pageid = $thispage->id AND
userid = $USER->id AND correct = 1 AND retry = $ntries")) {
$found = true;
break;
}
}
}
if ($found) {
$newpageid = $thispage->id;
if ($lesson->maxpages) {
// check number of pages viewed (in the lesson)
if (count_records("lesson_attempts", "lessonid", $lesson->id, "userid", $USER->id,
"retry", $ntries) >= $lesson->maxpages) {
$newpageid = LESSON_EOL;
}
}
} else {
$newpageid = LESSON_EOL;
}
} elseif (!$newpageid = $page->nextpageid) {
// no nextpage go to end of lesson
$newpageid = LESSON_EOL;
}
}
// display response (if there is one - there should be!)
if ($response) {
$title = get_field("lesson_pages", "title", "id", $pageid);
print_heading($title);
echo "
\n"; print_simple_box(format_text($response), 'center'); echo " |
...
$newanswer->lessonid = $lesson->id;
$newanswer->pageid = $newpageid;
$newanswer->timecreated = $timenow;
$newanswer->answer = trim($form->answer[$i]);
$newanswer->response = trim($form->response[$i]);
if (isset($form->jumpto[$i])) {
$newanswer->jumpto = $form->jumpto[$i];
}
$newanswerid = insert_record("lesson_answers", $newanswer);
if (!$newanswerid) {
error("Insert Page: answer record $i not inserted");
}
} else {
break;
}
}
redirect("view.php?id=$cm->id", get_string("ok"));
}
/****************** move ************************************/
elseif ($action == 'move') {
if (!isteacher($course->id)) {
error("Only teachers can look at this page");
}
$pageid = $_GET['pageid'];
$title = get_field("lesson_pages", "title", "id", $pageid);
print_heading(get_string("moving", "lesson", $title));
if (!$page = get_record_select("lesson_pages", "lessonid = $lesson->id AND prevpageid = 0")) {
error("Move: first page not found");
}
echo "
id&action=moveit&pageid=$pageid&after=0\">". get_string("movepagehere", "lesson")." |
cellheading2\">$page->title |
id&action=moveit&pageid=$pageid&after={$page->id}\">". get_string("movepagehere", "lesson")." |
...
if ($form->answerid[$i]) {
$oldanswer->id = $form->answerid[$i];
$oldanswer->timemodified = $timenow;
$oldanswer->answer = trim($form->answer[$i]);
$oldanswer->response = trim($form->response[$i]);
$oldanswer->jumpto = $form->jumpto[$i];
if (!update_record("lesson_answers", $oldanswer)) {
error("Update page: answer $i not updated");
}
} else {
// it's a new answer
$newanswer->lessonid = $lesson->id;
$newanswer->pageid = $page->id;
$newanswer->timecreated = $timenow;
$newanswer->answer = trim($form->answer[$i]);
$newanswer->response = trim($form->response[$i]);
$newanswer->jumpto = $form->jumpto[$i];
$newanswerid = insert_record("lesson_answers", $newanswer);
if (!$newanswerid) {
error("Update page: answer record not inserted");
}
}
} else {
if ($form->answerid[$i]) {
// need to delete blanked out answer
if (!delete_records("lesson_answers", "id", $form->answerid[$i])) {
error("Update page: unable to delete answer record");
}
}
}
}
redirect("view.php?id=$cm->id", get_string("ok"));
}
/*************** no man's land **************************************/
else {
error("Fatal Error: Unknown Action: ".$action."\n");
}
print_footer($course);
?>