3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Provides the interface for viewing and adding high scores
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 /** include required files */
27 require_once('../../config.php');
28 require_once($CFG->dirroot
.'/mod/lesson/locallib.php');
30 $id = required_param('id', PARAM_INT
); // Course Module ID
31 $mode = optional_param('mode', '', PARAM_ALPHA
);
32 $link = optional_param('link', 0, PARAM_INT
);
34 $cm = get_coursemodule_from_id('lesson', $id, 0, false, MUST_EXIST
);
35 $course = $DB->get_record('course', array('id' => $cm->course
), '*', MUST_EXIST
);
36 $lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance
), '*', MUST_EXIST
));
38 require_login($course, false, $cm);
40 $url = new moodle_url('/mod/lesson/highscores.php', array('id'=>$id));
42 $url->param('mode', $mode);
45 $url->param('link', $link);
49 $context = context_module
::instance($cm->id
);
53 // Ensure that we came from view.php
54 if (!confirm_sesskey() or !data_submitted()) {
55 print_error('invalidformdata');
60 if (confirm_sesskey() and $form = data_submitted($CFG->wwwroot
.'/mod/lesson/view.php')) {
61 $name = trim(optional_param('name', '', PARAM_CLEAN
));
63 // Make sure it is not empty
65 $lesson->add_message(get_string('missingname', 'lesson'));
69 // Check for censored words
70 $filterwords = explode(',', get_string('censorbadwords'));
71 foreach ($filterwords as $filterword) {
72 if (strstr($name, $filterword)) {
73 $lesson->add_message(get_string('namereject', 'lesson'));
82 $params = array ("lessonid" => $lesson->id
, "userid" => $USER->id
);
83 if (!$grades = $DB->get_records_select('lesson_grades', "lessonid = :lessonid", $params, 'completed')) {
84 print_error('cannotfindfirstgrade', 'lesson');
87 if (!$newgrade = $DB->get_record_sql("SELECT *
89 WHERE lessonid = :lessonid
91 ORDER BY completed DESC", $params, true)) {
92 print_error('cannotfindnewestgrade', 'lesson');
95 // Check for multiple submissions
96 if ($DB->record_exists('lesson_high_scores', array('gradeid' => $newgrade->id
))) {
97 print_error('onpostperpage', 'lesson');
100 // Find out if we need to delete any records
101 if ($highscores = $DB->get_records_sql("SELECT h.*, g.grade
102 FROM {lesson_grades} g, {lesson_high_scores} h
103 WHERE h.gradeid = g.id
104 AND h.lessonid = :lessonid
105 ORDER BY g.grade DESC", $params)) {
106 // Only count unique scores in our total for max high scores
107 $uniquescores = array();
108 foreach ($highscores as $highscore) {
109 $uniquescores[$highscore->grade
] = 1;
111 if (count($uniquescores) >= $lesson->maxhighscores
) {
112 // Top scores list is full, might need to delete a score
114 // See if the new score is already listed in the top scores list
115 // if it is listed, then dont need to delete any records
116 foreach ($highscores as $highscore) {
117 if ($newgrade->grade
== $highscore->grade
) {
122 // Pushing out the lowest score (could be multiple records)
124 foreach ($highscores as $highscore) {
125 if (empty($lowscore) or $lowscore > $highscore->grade
) {
126 $lowscore = $highscore->grade
;
129 // Now, delete all high scores with the low score
130 foreach ($highscores as $highscore) {
131 if ($highscore->grade
== $lowscore) {
132 $DB->delete_records('lesson_high_scores', array('id' => $highscore->id
));
139 $newhighscore = new stdClass
;
140 $newhighscore->lessonid
= $lesson->id
;
141 $newhighscore->userid
= $USER->id
;
142 $newhighscore->gradeid
= $newgrade->id
;
143 $newhighscore->nickname
= $name;
145 $newhighscore->id
= $DB->insert_record('lesson_high_scores', $newhighscore);
147 // Trigger highscore updated event.
148 $event = \mod_lesson\event\highscore_added
::create(array(
149 'objectid' => $newhighscore->id
,
150 'context' => $context,
151 'courseid' => $course->id
,
153 'lessonid' => $lesson->id
,
154 'nickname' => $newhighscore->nickname
159 $lesson->add_message(get_string('postsuccess', 'lesson'), 'notifysuccess');
160 redirect("$CFG->wwwroot/mod/lesson/highscores.php?id=$cm->id&link=1");
162 print_error('invalidformdata');
167 // Trigger highscore viewed event.
168 $event = \mod_lesson\event\highscores_viewed
::create(array(
169 'objectid' => $lesson->properties()->id
,
170 'context' => $context,
171 'courseid' => $course->id
175 $lessonoutput = $PAGE->get_renderer('mod_lesson');
176 echo $lessonoutput->header($lesson, $cm, 'highscores', false, null, get_string('viewhighscores', 'lesson'));
180 echo $lessonoutput->add_highscores_form($lesson);
183 $params = array ("lessonid" => $lesson->id
);
184 if (!$grades = $DB->get_records_select("lesson_grades", "lessonid = :lessonid", $params, "completed")) {
188 echo $OUTPUT->heading(get_string("topscorestitle", "lesson", $lesson->maxhighscores
), 4);
190 if (!$highscores = $DB->get_records_select("lesson_high_scores", "lessonid = :lessonid", $params)) {
191 echo $OUTPUT->heading(get_string("nohighscores", "lesson"), 3);
193 foreach ($highscores as $highscore) {
194 $grade = $grades[$highscore->gradeid
]->grade
;
195 $topscores[$grade][] = $highscore->nickname
;
199 $table = new html_table();
200 $table->align
= array('center', 'left', 'right');
201 $table->wrap
= array();
202 $table->width
= "30%";
203 $table->cellspacing
= '10px';
204 $table->size
= array('*', '*', '*');
206 $table->head
= array(get_string("rank", "lesson"), get_string('name'), get_string("scores", "lesson"));
210 $temp = current($topscores);
211 $score = key($topscores);
212 $rank = $printed +
1;
214 foreach ($temp as $student) {
215 $table->data
[] = array($rank, $student, $score.'%');
218 if (!next($topscores) ||
!($printed < $lesson->maxhighscores
)) {
222 echo html_writer
::table($table);
225 if (!has_capability('mod/lesson:manage', $context)) { // teachers don't need the links
226 echo $OUTPUT->box_start('mdl-align');
227 echo $OUTPUT->box_start('lessonbutton standardbutton');
229 echo html_writer
::link(new moodle_url('/course/view.php', array('id'=>$course->id
)), get_string("returntocourse", "lesson"));
231 echo html_writer
::link(new moodle_url('/course/view.php', array('id'=>$course->id
)), get_string("cancel", "lesson")). ' ';
232 echo html_writer
::link(new moodle_url('/mod/lesson/view.php', array('id'=>$cm->id
, 'viewed'=>'1')), get_string("startlesson", "lesson"));
234 echo $OUTPUT->box_end();
235 echo $OUTPUT->box_end();
240 echo $lessonoutput->footer();