id, false, $cm); $context = get_context_instance(CONTEXT_MODULE, $cm->id); 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 '