--- /dev/null
+<?php // $Id$
+/**
+ * Provides the interface for viewing and adding high scores
+ *
+ * @version $Id$
+ * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
+ * @package lesson
+ **/
+
+ require_once('../../config.php');
+ require_once('locallib.php');
+ require_once('lib.php');
+
+ $id = required_param('id', PARAM_INT); // Course Module ID
+ $mode = optional_param('mode', '', PARAM_ALPHA);
+ $link = optional_param('link', 0, PARAM_INT);
+
+ list($cm, $course, $lesson) = lesson_get_basics($id);
+
+ require_login($course->id, false, $cm);
+
+ $context = get_context_instance(CONTEXT_MODULE, $cm->id);
+
+ require_capability('mod/lesson:view', $context);
+
+ switch ($mode) {
+ case 'add':
+ // Ensure that we came from view.php
+ if (!confirm_sesskey() or !data_submitted("$CFG->wwwroot/mod/lesson/view.php")) {
+ error('Incorrect Form Data');
+ }
+ break;
+
+ case 'save':
+ if (confirm_sesskey() and $form = data_submitted($CFG->wwwroot.'/mod/lesson/view.php')) {
+ $name = trim(optional_param('name', '', PARAM_CLEAN));
+
+ // Make sure it is not empty
+ if (empty($name)) {
+ lesson_set_message(get_string('missingname', 'lesson'));
+ $mode = 'add';
+ break;
+ }
+ // Check for censored words
+ $filterwords = explode(',', get_string('censorbadwords'));
+ foreach ($filterwords as $filterword) {
+ if (strstr($name, $filterword)) {
+ lesson_set_message(get_string('namereject', 'lesson'));
+ $mode = 'add';
+ break;
+ }
+ }
+ // Bad word was found
+ if ($mode == 'add') {
+ break;
+ }
+
+ if (!$grades = get_records_select('lesson_grades', "lessonid = $lesson->id", 'completed')) {
+ error('Error: could not find grades');
+ }
+ if (!$newgrade = get_record_sql("SELECT *
+ FROM {$CFG->prefix}lesson_grades
+ WHERE lessonid = $lesson->id
+ AND userid = $USER->id
+ ORDER BY completed DESC", true)) {
+ error('Error: could not find newest grade');
+ }
+
+ // Check for multiple submissions
+ if (record_exists('lesson_high_scores', 'gradeid', $newgrade->id)) {
+ error('Only one posting per grade');
+ }
+
+ // Find out if we need to delete any records
+ if ($highscores = get_records_sql("SELECT h.*, g.grade
+ FROM {$CFG->prefix}lesson_grades g, {$CFG->prefix}lesson_high_scores h
+ WHERE h.gradeid = g.id
+ AND h.lessonid = $lesson->id
+ ORDER BY g.grade DESC")) {
+ // Only count unique scores in our total for max high scores
+ $uniquescores = array();
+ foreach ($highscores as $highscore) {
+ $uniquescores[$highscore->grade] = 1;
+ }
+ if (count($uniquescores) >= $lesson->maxhighscores) {
+ // Top scores list is full, might need to delete a score
+ $flag = true;
+ // See if the new score is already listed in the top scores list
+ // if it is listed, then dont need to delete any records
+ foreach ($highscores as $highscore) {
+ if ($newgrade->grade == $highscore->grade) {
+ $flag = false;
+ }
+ }
+ if ($flag) {
+ // Pushing out the lowest score (could be multiple records)
+ $lowscore = 0;
+ foreach ($highscores as $highscore) {
+ if (empty($lowscore) or $lowscore > $highscore->grade) {
+ $lowscore = $highscore->grade;
+ }
+ }
+ // Now, delete all high scores with the low score
+ foreach ($highscores as $highscore) {
+ if ($highscore->grade == $lowscore) {
+ delete_records('lesson_high_scores', 'id', $highscore->id);
+ }
+ }
+ }
+ }
+ }
+
+ $newhighscore = new stdClass;
+ $newhighscore->lessonid = $lesson->id;
+ $newhighscore->userid = $USER->id;
+ $newhighscore->gradeid = $newgrade->id;
+ $newhighscore->nickname = $name;
+
+ if (!insert_record('lesson_high_scores', $newhighscore)) {
+ error("Insert of new high score Failed!");
+ }
+
+ // Log it
+ add_to_log($course->id, 'lesson', 'update highscores', "highscores.php?id=$cm->id", $name, $cm->id);
+
+ lesson_set_message(get_string('postsuccess', 'lesson'), 'notifysuccess');
+ redirect("$CFG->wwwroot/mod/lesson/highscores.php?id=$cm->id&link=1");
+ } else {
+ error('Something is wrong with the form data');
+ }
+ break;
+ }
+
+ // Log it
+ add_to_log($course->id, 'lesson', 'view highscores', "highscores.php?id=$cm->id", $lesson->name, $cm->id);
+
+ lesson_print_header($cm, $course, $lesson, 'highscores');
+
+ switch ($mode) {
+ case 'add':
+ print_simple_box_start('center');
+ echo '<div align="center">
+ <form name="nickname" method ="post" action="'.$CFG->wwwroot.'/mod/lesson/highscores.php">
+ <input type="hidden" name="id" value="'.$cm->id.'" />
+ <input type="hidden" name="mode" value="save" />
+ <input type="hidden" name="sesskey" value="'.sesskey().'" />';
+
+ echo get_string("entername", "lesson").": <input type=\"text\" name=\"name\" size=\"7\" maxlength=\"5\">\n<p>\n";
+ lesson_print_submit_link(get_string("submitname", "lesson"), 'nickname');
+ echo "</p>\n</form>\n</div>\n";
+ print_simple_box_end();
+ break;
+ default:
+ if (!$grades = get_records_select("lesson_grades", "lessonid = $lesson->id", "completed")) {
+ $grades = array();
+ }
+
+ print_heading(get_string("topscorestitle", "lesson", $lesson->maxhighscores), 'center', 4);
+
+ if (!$highscores = get_records_select("lesson_high_scores", "lessonid = $lesson->id")) {
+ print_heading(get_string("nohighscores", "lesson"), 'center', 3);
+ } else {
+ foreach ($highscores as $highscore) {
+ $grade = $grades[$highscore->gradeid]->grade;
+ $topscores[$grade][] = $highscore->nickname;
+ }
+ krsort($topscores);
+
+ $table = new stdClass;
+ $table->align = array('center', 'left', 'right');
+ $table->wrap = array();
+ $table->width = "30%";
+ $table->cellspacing = '10px';
+ $table->size = array('*', '*', '*');
+
+ $table->head = array(get_string("rank", "lesson"), $course->students, get_string("scores", "lesson"));
+
+ $printed = 0;
+ while (true) {
+ $temp = current($topscores);
+ $score = key($topscores);
+ $rank = $printed + 1;
+ sort($temp);
+ foreach ($temp as $student) {
+ $table->data[] = array($rank, $student, $score.'%');
+ }
+ $printed++;
+ if (!next($topscores) || !($printed < $lesson->maxhighscores)) {
+ break;
+ }
+ }
+ print_table($table);
+ }
+
+ if (!has_capability('mod/lesson:manage', $context)) { // teachers don't need the links
+ echo '<div align="center">';
+ if ($link) {
+ echo "<br /><div class=\"lessonbutton standardbutton\"><a href=\"../../course/view.php?id=$course->id\">".get_string("returntocourse", "lesson")."</a></div>";
+ } else {
+ echo "<br /><span class=\"lessonbutton standardbutton\"><a href=\"../../course/view.php?id=$course->id\">".get_string("cancel", "lesson").'</a></span> '.
+ " <span class=\"lessonbutton standardbutton\"><a href=\"view.php?id=$cm->id&action=navigation\">".get_string("startlesson", "lesson").'</a></span>';
+ }
+ echo "</div>";
+ }
+ break;
+ }
+
+ print_footer($course);
+
+?>
\ No newline at end of file
print_simple_box_end();
print_footer($course);
exit();
- } elseif ($lesson->highscores && !$lesson->practice) {
- $action = 'highscores';
+ } elseif ($lesson->highscores and !$lesson->practice) {
+ if ($lesson->usepassword and empty($USER->lessonloggedin[$lesson->id])) {
+ // User needs to be logged in
+ $action = 'navigation';
+ } else {
+ redirect("$CFG->wwwroot/mod/lesson/highscores.php?id=$cm->id");
+ }
} else {
$action = 'navigation';
}
if ($lesson->password == md5(trim($password))) {
$USER->lessonloggedin[$lesson->id] = true;
$correctpass = true;
+ if ($lesson->highscores) {
+ // Logged in, now we can show high scores
+ redirect("$CFG->wwwroot/mod/lesson/highscores.php?id=$cm->id", '', 0);
+ }
}
} elseif (isset($USER->lessonloggedin[$lesson->id])) {
$correctpass = true;
// high scores code
if ($lesson->highscores && !has_capability('mod/lesson:manage', $context) && !$lesson->practice) {
echo "<div align=\"center\"><br>";
- if (!$grades = get_records_select("lesson_grades", "lessonid = $lesson->id", "completed")) {
- echo get_string("youmadehighscore", "lesson", $lesson->maxhighscores)."<br>";
- echo "<a href=\"view.php?id=$cm->id&action=nameforhighscores\">".get_string("clicktopost", "lesson")."</a><br>";
- } else {
- if (!$highscores = get_records_select("lesson_high_scores", "lessonid = $lesson->id")) {
- echo get_string("youmadehighscore", "lesson", $lesson->maxhighscores)."<br>";
- echo "<div class=\"lessonbutton standardbutton\"><a href=\"view.php?id=$cm->id&action=nameforhighscores\">".get_string("clicktopost", "lesson")."</a></div><br/>";
- } else {
+ if ($grades = get_records_select("lesson_grades", "lessonid = $lesson->id", "completed")) {
+ $madeit = false;
+ if ($highscores = get_records_select("lesson_high_scores", "lessonid = $lesson->id")) {
// get all the high scores into an array
foreach ($highscores as $highscore) {
$grade = $grades[$highscore->gradeid]->grade;
sort($topscores);
$lowscore = $topscores[0];
- if ($thegrade >= $lowscore || count($topscores) <= $lesson->maxhighscores) {
- echo get_string("youmadehighscore", "lesson", $lesson->maxhighscores)."<br>";
- echo "<div class=\"lessonbutton standardbutton\"><a href=\"view.php?id=$cm->id&action=nameforhighscores\">".get_string("clicktopost", "lesson")."</a></div><br />";
- } else {
- echo get_string("nothighscore", "lesson", $lesson->maxhighscores)."<br>";
+ if ($gradeinfo->grade >= $lowscore || count($topscores) <= $lesson->maxhighscores) {
+ $madeit = true;
}
}
+ if (!$highscores or $madeit) {
+ echo '<p>'.get_string("youmadehighscore", "lesson", $lesson->maxhighscores).
+ '</p><p>
+ <form method="post" name="highscores" action="'.$CFG->wwwroot.'/mod/lesson/highscores.php">
+ <input type="hidden" name="mode" value="add" />
+ <input type="hidden" name="id" value="'.$cm->id.'" />
+ <input type="hidden" name="sesskey" value="'.sesskey().'" />
+ <p>';
+ lesson_print_submit_link(get_string('clicktopost', 'lesson'), 'highscores');
+ echo '</p>
+ </form>';
+ } else {
+ echo get_string("nothighscore", "lesson", $lesson->maxhighscores)."<br>";
+ }
}
- echo "<br /><div style=\"padding: 5px;\" class=\"lessonbutton standardbutton\"><a href=\"view.php?id=$cm->id&action=highscores&link=1\">".get_string("viewhighscores", "lesson").'</a></div>';
+ echo "<br /><div style=\"padding: 5px;\" class=\"lessonbutton standardbutton\"><a href=\"$CFG->wwwroot/mod/lesson/highscores.php?id=$cm->id&link=1\">".get_string("viewhighscores", "lesson").'</a></div>';
echo "</div>";
}
echo "</table></form>\n";
}
}
-
- /*******************high scores **************************************/
- elseif ($action == 'highscores') {
- print_heading_with_help(format_string($lesson->name,true), "overview", "lesson");
-
- if (!$grades = get_records_select("lesson_grades", "lessonid = $lesson->id", "completed")) {
- $grades = array();
- }
-
- print_heading(get_string("topscorestitle", "lesson", $lesson->maxhighscores), 'center', 4);
-
- if (!$highscores = get_records_select("lesson_high_scores", "lessonid = $lesson->id")) {
- print_heading(get_string("nohighscores", "lesson"), 'center', 3);
- } else {
- foreach ($highscores as $highscore) {
- $grade = $grades[$highscore->gradeid]->grade;
- $topscores[$grade][] = $highscore->nickname;
- }
- krsort($topscores);
-
- $table = new stdClass;
- $table->align = array('center', 'left', 'right');
- $table->wrap = array();
- $table->width = "30%";
- $table->cellspacing = '10px';
- $table->size = array('*', '*', '*');
-
- $table->head = array(get_string("rank", "lesson"), $course->students, get_string("scores", "lesson"));
-
- $printed = 0;
- while (true) {
- $temp = current($topscores);
- $score = key($topscores);
- $rank = $printed + 1;
- sort($temp);
- foreach ($temp as $student) {
- $table->data[] = array($rank, $student, $score);
- }
- $printed++;
- if (!next($topscores) || !($printed < $lesson->maxhighscores)) {
- break;
- }
- }
- print_table($table);
- }
-
- if (!has_capability('mod/lesson:manage', $context)) { // teachers don't need the links
- echo '<div align="center">';
- if (optional_param('link', 0, PARAM_INT)) {
- echo "<br /><div class=\"lessonbutton standardbutton\"><a href=\"../../course/view.php?id=$course->id\">".get_string("returntocourse", "lesson")."</a></div>";
- } else {
- echo "<br /><span class=\"lessonbutton standardbutton\"><a href=\"../../course/view.php?id=$course->id\">".get_string("cancel", "lesson").'</a></span> '.
- " <span class=\"lessonbutton standardbutton\"><a href=\"view.php?id=$cm->id&action=navigation\">".get_string("startlesson", "lesson").'</a></span>';
- }
- echo "</div>";
- }
- }
- /*******************update high scores **************************************/
- elseif ($action == 'updatehighscores') {
- print_heading_with_help(format_string($lesson->name,true), "overview", "lesson");
-
- confirm_sesskey();
-
- if (!$grades = get_records_select("lesson_grades", "lessonid = $lesson->id", "completed")) {
- error("Error: could not find grades");
- }
- if (!$usergrades = get_records_select("lesson_grades", "lessonid = $lesson->id and userid = $USER->id", "completed DESC")) {
- error("Error: could not find grades");
- }
- echo "<div align=\"center\">";
- echo get_string("waitpostscore", "lesson")."<br>";
-
- foreach ($usergrades as $usergrade) {
- // get their latest grade
- $newgrade = $usergrade;
- break;
- }
-
- if ($pasthighscore = get_record_select("lesson_high_scores", "lessonid = $lesson->id and userid = $USER->id")) {
- $pastgrade = $grades[$pasthighscore->gradeid]->grade;
- if ($pastgrade >= $newgrade->grade) {
- redirect("view.php?id=$cm->id&action=highscores&link=1", "Update Successful");
- } else {
- // delete old and find out where new one goes
- if (!delete_records("lesson_high_scores", "id", $pasthighscore->id)) {
- error("Error: could not delete old high score");
- }
- }
- }
- // find out if we need to delete any records
- if ($highscores = get_records_select("lesson_high_scores", "lessonid = $lesson->id")) { // if no high scores... then just insert our new one
- foreach ($highscores as $highscore) {
- $grade = $grades[$highscore->gradeid]->grade;
- $topscores[$grade][] = $highscore->userid;
- }
- if (!(count($topscores) < $lesson->maxhighscores)) { // if the top scores list is not full then dont need to worry about removing old scores
- $scores = array_keys($topscores);
- $flag = true;
- // see if the new score is already listed in the top scores list
- // if it is listed, then dont need to delete any records
- foreach ($scores as $score) {
- if ($score = $newgrade->grade) {
- $flag = false;
- }
- }
- if ($flag) { // if the score does not exist in the top scores list, then the lowest scores get thrown out.
- ksort($topscores); // sort so the lowest score is first element
- $lowscore = current($topscores);
- // making a delete statement to delete all users with the lowest score
- $deletestmt = 'lessonid = '. $lesson->id .' and userid = ';
- $deletestmt .= current($lowscore);
- while (next($lowscore)) {
- $deletestmt .= " or userid = ".current($lowscore);
- }
- if (!delete_records_select('lesson_high_scores', $deletestmt)) {
- /// not a big deal...
- error('Did not delete extra high score(s)');
- }
- }
- }
- }
-
- $newhighscore = new stdClass;
- $newhighscore->lessonid = $lesson->id;
- $newhighscore->userid = $USER->id;
- $newhighscore->gradeid = $newgrade->id;
- $newhighscore->nickname = optional_param('name', '', PARAM_CLEAN);
-
- if (!insert_record("lesson_high_scores", $newhighscore)) {
- error("Insert of new high score Failed!");
- }
-
- redirect("view.php?id=$cm->id&action=highscores&link=1", get_string("postsuccess", "lesson"));
- echo "</div>";
- }
- /*******************name for highscores **************************************/
- elseif ($action == 'nameforhighscores') {
- print_heading_with_help(format_string($lesson->name,true), "overview", "lesson");
- echo "<div align=\"center\">";
- if ($name = trim(optional_param('name', '', PARAM_CLEAN))) {
- if (lesson_check_nickname($name)) {
- redirect("view.php?id=$cm->id&action=updatehighscores&name=$name&sesskey=".$USER->sesskey, get_string("nameapproved", "lesson"));
- } else {
- echo get_string("namereject", "lesson")."<br /><br />";
- }
- }
-
- echo "<form name=\"nickname\" method =\"post\" action=\"view.php\">";
- echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />";
- echo "<input type=\"hidden\" name=\"action\" value=\"nameforhighscores\" />";
- echo get_string("entername", "lesson").": <input type=\"text\" name=\"name\" maxlength=\"5\"><br />";
- echo "<input type=\"submit\" value=\"".get_string("submitname", "lesson")."\" />";
- echo "</form>";
- echo "</div>";
- }
/*************** no man's land **************************************/
else {
error("Fatal Error: Unknown Action: ".$action."\n");