3 * Provides the interface for viewing and adding high scores
6 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
10 require_once('../../config.php');
11 require_once('locallib.php');
12 require_once('lib.php');
14 $id = required_param('id', PARAM_INT
); // Course Module ID
15 $mode = optional_param('mode', '', PARAM_ALPHA
);
16 $link = optional_param('link', 0, PARAM_INT
);
18 list($cm, $course, $lesson) = lesson_get_basics($id);
20 require_login($course->id
, false, $cm);
22 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
27 // Ensure that we came from view.php
28 if (!confirm_sesskey() or !data_submitted("$CFG->wwwroot/mod/lesson/view.php")) {
29 error('Incorrect Form Data');
34 if (confirm_sesskey() and $form = data_submitted($CFG->wwwroot
.'/mod/lesson/view.php')) {
35 $name = trim(optional_param('name', '', PARAM_CLEAN
));
37 // Make sure it is not empty
39 lesson_set_message(get_string('missingname', 'lesson'));
43 // Check for censored words
44 $filterwords = explode(',', get_string('censorbadwords'));
45 foreach ($filterwords as $filterword) {
46 if (strstr($name, $filterword)) {
47 lesson_set_message(get_string('namereject', 'lesson'));
57 if (!$grades = get_records_select('lesson_grades', "lessonid = $lesson->id", 'completed')) {
58 error('Error: could not find grades');
60 if (!$newgrade = get_record_sql("SELECT *
61 FROM {$CFG->prefix}lesson_grades
62 WHERE lessonid = $lesson->id
63 AND userid = $USER->id
64 ORDER BY completed DESC", true)) {
65 error('Error: could not find newest grade');
68 // Check for multiple submissions
69 if (record_exists('lesson_high_scores', 'gradeid', $newgrade->id
)) {
70 error('Only one posting per grade');
73 // Find out if we need to delete any records
74 if ($highscores = get_records_sql("SELECT h.*, g.grade
75 FROM {$CFG->prefix}lesson_grades g, {$CFG->prefix}lesson_high_scores h
76 WHERE h.gradeid = g.id
77 AND h.lessonid = $lesson->id
78 ORDER BY g.grade DESC")) {
79 // Only count unique scores in our total for max high scores
80 $uniquescores = array();
81 foreach ($highscores as $highscore) {
82 $uniquescores[$highscore->grade
] = 1;
84 if (count($uniquescores) >= $lesson->maxhighscores
) {
85 // Top scores list is full, might need to delete a score
87 // See if the new score is already listed in the top scores list
88 // if it is listed, then dont need to delete any records
89 foreach ($highscores as $highscore) {
90 if ($newgrade->grade
== $highscore->grade
) {
95 // Pushing out the lowest score (could be multiple records)
97 foreach ($highscores as $highscore) {
98 if (empty($lowscore) or $lowscore > $highscore->grade
) {
99 $lowscore = $highscore->grade
;
102 // Now, delete all high scores with the low score
103 foreach ($highscores as $highscore) {
104 if ($highscore->grade
== $lowscore) {
105 delete_records('lesson_high_scores', 'id', $highscore->id
);
112 $newhighscore = new stdClass
;
113 $newhighscore->lessonid
= $lesson->id
;
114 $newhighscore->userid
= $USER->id
;
115 $newhighscore->gradeid
= $newgrade->id
;
116 $newhighscore->nickname
= $name;
118 if (!insert_record('lesson_high_scores', $newhighscore)) {
119 error("Insert of new high score Failed!");
123 add_to_log($course->id
, 'lesson', 'update highscores', "highscores.php?id=$cm->id", $name, $cm->id
);
125 lesson_set_message(get_string('postsuccess', 'lesson'), 'notifysuccess');
126 redirect("$CFG->wwwroot/mod/lesson/highscores.php?id=$cm->id&link=1");
128 error('Something is wrong with the form data');
134 add_to_log($course->id
, 'lesson', 'view highscores', "highscores.php?id=$cm->id", $lesson->name
, $cm->id
);
136 lesson_print_header($cm, $course, $lesson, 'highscores');
140 print_simple_box_start('center');
141 echo '<div class="mdl-align">
142 <form id="nickname" method ="post" action="'.$CFG->wwwroot
.'/mod/lesson/highscores.php" autocomplete="off">
143 <input type="hidden" name="id" value="'.$cm->id
.'" />
144 <input type="hidden" name="mode" value="save" />
145 <input type="hidden" name="sesskey" value="'.sesskey().'" />';
147 echo get_string("entername", "lesson").": <input type=\"text\" name=\"name\" size=\"7\" maxlength=\"5\" />\n<p>\n";
148 lesson_print_submit_link(get_string("submitname", "lesson"), 'nickname');
149 echo "</p>\n</form>\n</div>\n";
150 print_simple_box_end();
153 if (!$grades = get_records_select("lesson_grades", "lessonid = $lesson->id", "completed")) {
157 print_heading(get_string("topscorestitle", "lesson", $lesson->maxhighscores
), 'center', 4);
159 if (!$highscores = get_records_select("lesson_high_scores", "lessonid = $lesson->id")) {
160 print_heading(get_string("nohighscores", "lesson"), 'center', 3);
162 foreach ($highscores as $highscore) {
163 $grade = $grades[$highscore->gradeid
]->grade
;
164 $topscores[$grade][] = $highscore->nickname
;
168 $table = new stdClass
;
169 $table->align
= array('center', 'left', 'right');
170 $table->wrap
= array();
171 $table->width
= "30%";
172 $table->cellspacing
= '10px';
173 $table->size
= array('*', '*', '*');
175 $table->head
= array(get_string("rank", "lesson"), $course->students
, get_string("scores", "lesson"));
179 $temp = current($topscores);
180 $score = key($topscores);
181 $rank = $printed +
1;
183 foreach ($temp as $student) {
184 $table->data
[] = array($rank, $student, $score.'%');
187 if (!next($topscores) ||
!($printed < $lesson->maxhighscores
)) {
194 if (!has_capability('mod/lesson:manage', $context)) { // teachers don't need the links
195 echo '<div style="text-align:center">';
197 echo "<br /><div class=\"lessonbutton standardbutton\"><a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">".get_string("returntocourse", "lesson")."</a></div>";
199 echo "<br /><span class=\"lessonbutton standardbutton\"><a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">".get_string("cancel", "lesson").'</a></span> '.
200 " <span class=\"lessonbutton standardbutton\"><a href=\"$CFG->wwwroot/mod/lesson/view.php?id=$cm->id&viewed=1\">".get_string("startlesson", "lesson").'</a></span>';
207 print_footer($course);