MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / lesson / highscores.php
blob858f5eb7a3c0ef8410939aab524da730ccd6331d
1 <?php // $Id$
2 /**
3 * Provides the interface for viewing and adding high scores
5 * @version $Id$
6 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
7 * @package lesson
8 **/
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);
25 switch ($mode) {
26 case 'add':
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');
31 break;
33 case 'save':
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
38 if (empty($name)) {
39 lesson_set_message(get_string('missingname', 'lesson'));
40 $mode = 'add';
41 break;
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'));
48 $mode = 'add';
49 break;
52 // Bad word was found
53 if ($mode == 'add') {
54 break;
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
86 $flag = true;
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) {
91 $flag = false;
94 if ($flag) {
95 // Pushing out the lowest score (could be multiple records)
96 $lowscore = 0;
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!");
122 // Log it
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&amp;link=1");
127 } else {
128 error('Something is wrong with the form data');
130 break;
133 // Log it
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');
138 switch ($mode) {
139 case 'add':
140 print_simple_box_start('center');
141 echo '<div align="center">
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();
151 break;
152 default:
153 if (!$grades = get_records_select("lesson_grades", "lessonid = $lesson->id", "completed")) {
154 $grades = array();
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);
161 } else {
162 foreach ($highscores as $highscore) {
163 $grade = $grades[$highscore->gradeid]->grade;
164 $topscores[$grade][] = $highscore->nickname;
166 krsort($topscores);
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"));
177 $printed = 0;
178 while (true) {
179 $temp = current($topscores);
180 $score = key($topscores);
181 $rank = $printed + 1;
182 sort($temp);
183 foreach ($temp as $student) {
184 $table->data[] = array($rank, $student, $score.'%');
186 $printed++;
187 if (!next($topscores) || !($printed < $lesson->maxhighscores)) {
188 break;
191 print_table($table);
194 if (!has_capability('mod/lesson:manage', $context)) { // teachers don't need the links
195 echo '<div align="center">';
196 if ($link) {
197 echo "<br /><div class=\"lessonbutton standardbutton\"><a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">".get_string("returntocourse", "lesson")."</a></div>";
198 } else {
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&amp;viewed=1\">".get_string("startlesson", "lesson").'</a></span>';
202 echo "</div>";
204 break;
207 print_footer($course);