<commit_before><?PHP  // $Id$

/// This page prints a particular instance of lesson
/// (Replace lesson with the name of your module)

    require_once("../../config.php");
    require_once("lib.php");

    require_variable($id);    // Course Module ID, or

    if (! $cm = get_record("course_modules", "id", $id)) {
            error("Course Module ID was incorrect");
    }

    if (! $course = get_record("course", "id", $cm->course)) {
        error("Course is misconfigured");
    }

    if (! $lesson = get_record("lesson", "id", $cm->instance)) {
        error("Course module is incorrect");
    }

    require_login($course->id);

    add_to_log($course->id, "lesson", "view", "view.php?id=$cm->id", "$lesson->id");

/// Print the page header

    if ($course->category) {
        $navigation = "<A HREF=\"../../course/view.php?id=$course->id\">$course->shortname</A> ->";
    }

    $strlessons = get_string("modulenameplural", "lesson");
    $strlesson  = get_string("modulename", "lesson");

    print_header("$course->shortname: $lesson->name", "$course->fullname",
                 "$navigation <A HREF=index.php?id=$course->id>$strlessons</A> -> <a href=\"view.php?id=$cm->id\">$lesson->name</a>", 
                  "", "", true, update_module_button($cm->id, $course->id, $strlesson), 
                  navmenu($course, $cm));

    // set up some general variables
    $usehtmleditor = can_use_html_editor();
    $path = "$CFG->wwwroot/course";
    if (empty($THEME->custompix)) {
        $pixpath = "$path/../pix";
    } else {
        $pixpath = "$path/../theme/$CFG->theme/pix";
    }

    if (empty($action)) {
        if (isteacher($course->id)) {
            $action = 'teacherview';
        } else {
            $action = 'navigation';
        }
    }

    /************** navigation **************************************/
    if ($action == 'navigation') {
        // display individual pages and their sets of answers
        // if pageid is EOL then the end of the lesson has been reached
        print_heading($lesson->name);
		if (empty($_GET['pageid'])) {
            // if no pageid given see if the lesson has been started
            if ($grades = get_records_select("lesson_grades", "lessonid = $lesson->id AND userid = $USER->id",
                        "grade DESC")) {
                $retries = count($grades);
            } else {
                $retries = 0;
            }
            if ($retries) {
                print_heading(get_string("attempt", "lesson", $retries + 1));
            }
            // if there are any questions have been answered correctly in this attempt
            if ($attempts = get_records_select("lesson_attempts", 
                        "lessonid = $lesson->id AND userid = $USER->id AND retry = $retries AND 
                        correct = 1", "timeseen DESC")) {
                // get the first page
                if (!$firstpageid = get_field("lesson_pages", "id", "lessonid", $lesson->id,
                            "prevpageid", 0)) {
                    error("Navigation: first page not found");
                }
                foreach ($attempts as $attempt) {
                    $jumpto = get_field("lesson_answers", "jumpto", "id", $attempt->answerid);
                    // convert the jumpto to a proper page id
                    if ($jumpto == 0) { // unlikely value!
                        $lastpageseen = $attempt->pageid;
                    } elseif ($jumpto == NEXTPAGE) {
                        if (!$lastpageseen = get_field("lesson_pages", "nextpageid", "id", 
                                    $attempt->pageid)) {
                            // no nextpage go to end of lesson
                            $lastpageseen = EOL;
                        }
                    } else {
                        $lastpageseen = $jumpto;
                    }
                    break; // only look at the latest correct attempt 
                }
                if ($lastpageseen != $firstpageid) {
                    notice_yesno(get_string("youhaveseen","lesson"), 
                        "view.php?id=$cm->id&action=navigation&pageid=$lastpageseen", 
                        "view.php?id=$cm->id&action=navigation&pageid=$firstpageid");
                    print_footer($course);
                    exit();
                }
            }
            if ($grades) {
                foreach ($grades as $grade) {
                    $bestgrade = $grade->grade;
                    break;
                }
                if (!$lesson->retake) {
          		    redirect("../../course/view.php?id=$course->id", get_string("alreadytaken", "lesson"));
                // allow student to retake course even if they have the maximum grade
                // } elseif ($bestgrade == 100) {
          		//     redirect("../../course/view.php?id=$course->id", get_string("maximumgradeachieved",
                //                 "lesson"));
                }
            }
            // start at the first page
			if (!$pageid = get_field("lesson_pages", "id", "lessonid", $lesson->id, "prevpageid", 0)) {
                error("Navigation: first page not found");
            }
		} else {
            $pageid = $_GET['pageid'];
        }
        if ($pageid != EOL) {
            if (!$page = get_record("lesson_pages", "id", $pageid)) {
                error("Navigation: the page record not found");
            }
            echo "<table align=\"center\" width=\"80%\" border=\"0\"><tr><td>\n";
            print_heading($page->title);
            print_simple_box(format_text($page->contents), 'center');
            echo "<br />\n";
            if ($answers = get_records("lesson_answers", "pageid", $page->id)) {
                shuffle($answers);
                echo "<form name=\"pageform\" method =\"post\" action=\"lesson.php\">\n";
                echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\">\n";
                echo "<input type=\"hidden\" name=\"action\" value=\"continue\">\n";
                echo "<input type=\"hidden\" name=\"pageid\" value=\"$pageid\">\n";
                print_simple_box_start("center");
                foreach ($answers as $answer) {
                    echo "<p><input type=\"radio\" name=\"answerid\" value=\"{$answer->id}\"> \n";
                    $options->para = false; // no <p></p>
                    echo format_text(trim($answer->answer), FORMAT_MOODLE, $options); 
                    echo "</p>\n";
                }
                print_simple_box_end();
                echo "<p align=\"center\"><input type=\"submit\" name=\"continue\" value=\"".
                    get_string("pleasecheckoneanswer", "lesson")."\"></p>\n";
                echo "</form>\n";
            } else {
                // a page without answers - find the next (logical) page
                if (!$newpageid = get_field("lesson_pages", "nextpageid", "id", $pageid)) {
                    // this is the last page - flag end of lesson
                    $newpageid = EOL;
                }
		        print_continue("view.php?id=$cm->id&action=navigation&pageid=$newpageid");
            }
            echo "</table>\n";
        } else {<commit_msg>Add some formatting to the answers<commit_after><?PHP  // $Id$

/// This page prints a particular instance of lesson
/// (Replace lesson with the name of your module)

    require_once("../../config.php");
    require_once("lib.php");

    require_variable($id);    // Course Module ID, or

    if (! $cm = get_record("course_modules", "id", $id)) {
            error("Course Module ID was incorrect");
    }

    if (! $course = get_record("course", "id", $cm->course)) {
        error("Course is misconfigured");
    }

    if (! $lesson = get_record("lesson", "id", $cm->instance)) {
        error("Course module is incorrect");
    }

    require_login($course->id);

    add_to_log($course->id, "lesson", "view", "view.php?id=$cm->id", "$lesson->id");

/// Print the page header

    if ($course->category) {
        $navigation = "<A HREF=\"../../course/view.php?id=$course->id\">$course->shortname</A> ->";
    }

    $strlessons = get_string("modulenameplural", "lesson");
    $strlesson  = get_string("modulename", "lesson");

    print_header("$course->shortname: $lesson->name", "$course->fullname",
                 "$navigation <A HREF=index.php?id=$course->id>$strlessons</A> -> <a href=\"view.php?id=$cm->id\">$lesson->name</a>", 
                  "", "", true, update_module_button($cm->id, $course->id, $strlesson), 
                  navmenu($course, $cm));

    // set up some general variables
    $usehtmleditor = can_use_html_editor();
    $path = "$CFG->wwwroot/course";
    if (empty($THEME->custompix)) {
        $pixpath = "$path/../pix";
    } else {
        $pixpath = "$path/../theme/$CFG->theme/pix";
    }

    if (empty($action)) {
        if (isteacher($course->id)) {
            $action = 'teacherview';
        } else {
            $action = 'navigation';
        }
    }

    /************** navigation **************************************/
    if ($action == 'navigation') {
        // display individual pages and their sets of answers
        // if pageid is EOL then the end of the lesson has been reached
        print_heading($lesson->name);
		if (empty($_GET['pageid'])) {
            // if no pageid given see if the lesson has been started
            if ($grades = get_records_select("lesson_grades", "lessonid = $lesson->id AND userid = $USER->id",
                        "grade DESC")) {
                $retries = count($grades);
            } else {
                $retries = 0;
            }
            if ($retries) {
                print_heading(get_string("attempt", "lesson", $retries + 1));
            }
            // if there are any questions have been answered correctly in this attempt
            if ($attempts = get_records_select("lesson_attempts", 
                        "lessonid = $lesson->id AND userid = $USER->id AND retry = $retries AND 
                        correct = 1", "timeseen DESC")) {
                // get the first page
                if (!$firstpageid = get_field("lesson_pages", "id", "lessonid", $lesson->id,
                            "prevpageid", 0)) {
                    error("Navigation: first page not found");
                }
                foreach ($attempts as $attempt) {
                    $jumpto = get_field("lesson_answers", "jumpto", "id", $attempt->answerid);
                    // convert the jumpto to a proper page id
                    if ($jumpto == 0) { // unlikely value!
                        $lastpageseen = $attempt->pageid;
                    } elseif ($jumpto == NEXTPAGE) {
                        if (!$lastpageseen = get_field("lesson_pages", "nextpageid", "id", 
                                    $attempt->pageid)) {
                            // no nextpage go to end of lesson
                            $lastpageseen = EOL;
                        }
                    } else {
                        $lastpageseen = $jumpto;
                    }
                    break; // only look at the latest correct attempt 
                }
                if ($lastpageseen != $firstpageid) {
                    notice_yesno(get_string("youhaveseen","lesson"), 
                        "view.php?id=$cm->id&action=navigation&pageid=$lastpageseen", 
                        "view.php?id=$cm->id&action=navigation&pageid=$firstpageid");
                    print_footer($course);
                    exit();
                }
            }
            if ($grades) {
                foreach ($grades as $grade) {
                    $bestgrade = $grade->grade;
                    break;
                }
                if (!$lesson->retake) {
          		    redirect("../../course/view.php?id=$course->id", get_string("alreadytaken", "lesson"));
                // allow student to retake course even if they have the maximum grade
                // } elseif ($bestgrade == 100) {
          		//     redirect("../../course/view.php?id=$course->id", get_string("maximumgradeachieved",
                //                 "lesson"));
                }
            }
            // start at the first page
			if (!$pageid = get_field("lesson_pages", "id", "lessonid", $lesson->id, "prevpageid", 0)) {
                error("Navigation: first page not found");
            }
		} else {
            $pageid = $_GET['pageid'];
        }
        if ($pageid != EOL) {
            if (!$page = get_record("lesson_pages", "id", $pageid)) {
                error("Navigation: the page record not found");
            }
            echo "<table align=\"center\" width=\"80%\" border=\"0\"><tr><td>\n";
            print_heading($page->title);
            print_simple_box(format_text($page->contents), 'center');
            echo "<br />\n";
            if ($answers = get_records("lesson_answers", "pageid", $page->id)) {
                shuffle($answers);
                echo "<form name=\"pageform\" method =\"post\" action=\"lesson.php\">\n";
                echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\">\n";
                echo "<input type=\"hidden\" name=\"action\" value=\"continue\">\n";
                echo "<input type=\"hidden\" name=\"pageid\" value=\"$pageid\">\n";
                print_simple_box_start("center");
                echo '<table width="100%">';
                foreach ($answers as $answer) {
                    echo "<tr><td><input type=\"radio\" name=\"answerid\" value=\"{$answer->id}\"></td>\n";
                    echo "<td>\n";
                    $options->para = false; // no <p></p>
                    echo format_text(trim($answer->answer), FORMAT_MOODLE, $options); 
                    echo "</td></tr>\n";
                }
                echo '</table>';
                print_simple_box_end();
                echo "<p align=\"center\"><input type=\"submit\" name=\"continue\" value=\"".
                    get_string("pleasecheckoneanswer", "lesson")."\"></p>\n";
                echo "</form>\n";
            } else {
                // a page without answers - find the next (logical) page
                if (!$newpageid = get_field("lesson_pages", "nextpageid", "id", $pageid)) {
                    // this is the last page - flag end of lesson
                    $newpageid = EOL;
                }
		        print_continue("view.php?id=$cm->id&action=navigation&pageid=$newpageid");
            }
            echo "</table>\n";
        } else {
{'fraction_non_alphanumeric': 0.1671811758249297, 'fraction_numerical': 0.0018522329697468615, 'mean_word_length': 1.607871198568873, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 151, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3905781', 'n_tokens_mistral': 4158, 'n_tokens_neox': 3933, 'n_words': 903}