From: mark-nielsen Date: Sun, 10 Sep 2006 01:20:00 +0000 (+0000) Subject: Moved high scores logic out of view.php into highscores.php X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=d1b025d0133fba1e788a198fb2adfa61170a742a;p=moodle.git Moved high scores logic out of view.php into highscores.php Changed: now need to enter password on password protected lessons before being able to view high scores Added logs to high scores and reworked logic --- diff --git a/mod/lesson/action/continue.php b/mod/lesson/action/continue.php index a67de22384..49bf28fa21 100644 --- a/mod/lesson/action/continue.php +++ b/mod/lesson/action/continue.php @@ -776,7 +776,7 @@ lesson_set_message('('.get_string("maximumnumberofattemptsreached", "lesson").')'); } - lesson_print_header($cm, $course, $lesson, 'navigation', false); + lesson_print_header($cm, $course, $lesson, 'navigation'); include($CFG->wwwroot.'/mod/lesson/action/continue.html'); ?> diff --git a/mod/lesson/highscores.php b/mod/lesson/highscores.php new file mode 100644 index 0000000000..bcffe24168 --- /dev/null +++ b/mod/lesson/highscores.php @@ -0,0 +1,210 @@ +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 '
+
+ + + '; + + echo get_string("entername", "lesson").": \n

\n"; + lesson_print_submit_link(get_string("submitname", "lesson"), 'nickname'); + echo "

\n
\n
\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 '
'; + if ($link) { + echo "
"; + } else { + echo "
id\">".get_string("cancel", "lesson").' '. + " id&action=navigation\">".get_string("startlesson", "lesson").''; + } + echo "
"; + } + break; + } + + print_footer($course); + +?> \ No newline at end of file diff --git a/mod/lesson/lesson.php b/mod/lesson/lesson.php index 08cb72eb91..e0cf6b0232 100644 --- a/mod/lesson/lesson.php +++ b/mod/lesson/lesson.php @@ -40,7 +40,7 @@ case 'confirmdelete': case 'editpage': case 'move': - lesson_print_header($cm, $course, $lesson, '', false); + lesson_print_header($cm, $course, $lesson); case 'addcluster': case 'addendofbranch': case 'addendofcluster': diff --git a/mod/lesson/locallib.php b/mod/lesson/locallib.php index 64c54f4646..aa75be66d1 100644 --- a/mod/lesson/locallib.php +++ b/mod/lesson/locallib.php @@ -217,7 +217,7 @@ if (!defined("LESSON_RESPONSE_EDITOR")) { * @param boolean $printheading Print the a heading with the lesson name * @return void **/ -function lesson_print_header($cm, $course, $lesson, $currenttab = '', $printheading = true) { +function lesson_print_header($cm, $course, $lesson, $currenttab = '') { global $CFG, $USER; $context = get_context_instance(CONTEXT_MODULE, $cm->id); @@ -238,8 +238,10 @@ function lesson_print_header($cm, $course, $lesson, $currenttab = '', $printhead '', '', true, update_module_button($cm->id, $course->id, $strlesson), navmenu($course, $cm)); - if ($printheading) { + if (has_capability('mod/lesson:manage')) { print_heading_with_help(format_string($lesson->name, true), "overview", "lesson"); + } else { + print_heading($lesson->name); } if (!empty($currenttab) and has_capability('mod/lesson:manage', $context)) { diff --git a/mod/lesson/tabs.php b/mod/lesson/tabs.php index 12d8e9759d..be5e093a6e 100644 --- a/mod/lesson/tabs.php +++ b/mod/lesson/tabs.php @@ -43,7 +43,7 @@ $row[] = new tabobject('essay', "$CFG->wwwroot/mod/lesson/essay.php?id=$cm->id", get_string('manualgrading', 'lesson')); } if ($lesson->highscores) { - $row[] = new tabobject('highscores', "$CFG->wwwroot/mod/lesson/view.php?id=$cm->id&action=highscores", get_string('highscores', 'lesson')); + $row[] = new tabobject('highscores', "$CFG->wwwroot/mod/lesson/highscores.php?id=$cm->id", get_string('highscores', 'lesson')); } $tabs[] = $row; diff --git a/mod/lesson/view.php b/mod/lesson/view.php index cd9e6c12ba..701f4a3a5f 100644 --- a/mod/lesson/view.php +++ b/mod/lesson/view.php @@ -71,8 +71,13 @@ 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'; } @@ -135,6 +140,10 @@ 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; @@ -1089,14 +1098,9 @@ // high scores code if ($lesson->highscores && !has_capability('mod/lesson:manage', $context) && !$lesson->practice) { echo "

"; - if (!$grades = get_records_select("lesson_grades", "lessonid = $lesson->id", "completed")) { - echo get_string("youmadehighscore", "lesson", $lesson->maxhighscores)."
"; - echo "id&action=nameforhighscores\">".get_string("clicktopost", "lesson")."
"; - } else { - if (!$highscores = get_records_select("lesson_high_scores", "lessonid = $lesson->id")) { - echo get_string("youmadehighscore", "lesson", $lesson->maxhighscores)."
"; - echo "
"; - } 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; @@ -1106,15 +1110,26 @@ sort($topscores); $lowscore = $topscores[0]; - if ($thegrade >= $lowscore || count($topscores) <= $lesson->maxhighscores) { - echo get_string("youmadehighscore", "lesson", $lesson->maxhighscores)."
"; - echo "
"; - } else { - echo get_string("nothighscore", "lesson", $lesson->maxhighscores)."
"; + if ($gradeinfo->grade >= $lowscore || count($topscores) <= $lesson->maxhighscores) { + $madeit = true; } } + if (!$highscores or $madeit) { + echo '

'.get_string("youmadehighscore", "lesson", $lesson->maxhighscores). + '

+

+ + + +

'; + lesson_print_submit_link(get_string('clicktopost', 'lesson'), 'highscores'); + echo '

+
'; + } else { + echo get_string("nothighscore", "lesson", $lesson->maxhighscores)."
"; + } } - echo "
'; + echo "
'; echo "
"; } @@ -1525,162 +1540,7 @@ echo "\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 '
'; - if (optional_param('link', 0, PARAM_INT)) { - echo "
"; - } else { - echo "
id\">".get_string("cancel", "lesson").' '. - " id&action=navigation\">".get_string("startlesson", "lesson").''; - } - echo "
"; - } - } - /*******************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 "
"; - echo get_string("waitpostscore", "lesson")."
"; - - 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 "
"; - } - /*******************name for highscores **************************************/ - elseif ($action == 'nameforhighscores') { - print_heading_with_help(format_string($lesson->name,true), "overview", "lesson"); - echo "
"; - 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")."

"; - } - } - - echo "
"; - echo "id\" />"; - echo ""; - echo get_string("entername", "lesson").":
"; - echo ""; - echo "
"; - echo "
"; - } /*************** no man's land **************************************/ else { error("Fatal Error: Unknown Action: ".$action."\n");