MDL-44616 behat: Adding steps to test MDLQA-1709 and MDLQA-62
[moodle.git] / mod / lesson / highscores.php
blobd1c3596363d806e9ee83b3c59f24be8ec22be2f4
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
9 //
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/>.
18 /**
19 * Provides the interface for viewing and adding high scores
21 * @package mod_lesson
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 **/
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));
41 if ($mode !== '') {
42 $url->param('mode', $mode);
44 if ($link !== 0) {
45 $url->param('link', $link);
47 $PAGE->set_url($url);
49 $context = context_module::instance($cm->id);
51 switch ($mode) {
52 case 'add':
53 // Ensure that we came from view.php
54 if (!confirm_sesskey() or !data_submitted()) {
55 print_error('invalidformdata');
57 break;
59 case 'save':
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
64 if (empty($name)) {
65 $lesson->add_message(get_string('missingname', 'lesson'));
66 $mode = 'add';
67 break;
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'));
74 $mode = 'add';
75 break;
78 // Bad word was found
79 if ($mode == 'add') {
80 break;
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 *
88 FROM {lesson_grades}
89 WHERE lessonid = :lessonid
90 AND userid = :userid
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
113 $flag = true;
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) {
118 $flag = false;
121 if ($flag) {
122 // Pushing out the lowest score (could be multiple records)
123 $lowscore = 0;
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 $event->trigger();
155 $lesson->add_message(get_string('postsuccess', 'lesson'), 'notifysuccess');
156 redirect("$CFG->wwwroot/mod/lesson/highscores.php?id=$cm->id&amp;link=1");
157 } else {
158 print_error('invalidformdata');
160 break;
163 // Trigger highscore viewed event.
164 $event = \mod_lesson\event\highscores_viewed::create(array(
165 'objectid' => $lesson->properties()->id,
166 'context' => $context,
167 'courseid' => $course->id
169 $event->trigger();
171 $lessonoutput = $PAGE->get_renderer('mod_lesson');
172 echo $lessonoutput->header($lesson, $cm, 'highscores', false, null, get_string('viewhighscores', 'lesson'));
174 switch ($mode) {
175 case 'add':
176 echo $lessonoutput->add_highscores_form($lesson);
177 break;
178 default:
179 $params = array ("lessonid" => $lesson->id);
180 if (!$grades = $DB->get_records_select("lesson_grades", "lessonid = :lessonid", $params, "completed")) {
181 $grades = array();
184 echo $OUTPUT->heading(get_string("topscorestitle", "lesson", $lesson->maxhighscores), 4);
186 if (!$highscores = $DB->get_records_select("lesson_high_scores", "lessonid = :lessonid", $params)) {
187 echo $OUTPUT->heading(get_string("nohighscores", "lesson"), 3);
188 } else {
189 foreach ($highscores as $highscore) {
190 $grade = $grades[$highscore->gradeid]->grade;
191 $topscores[$grade][] = $highscore->nickname;
193 krsort($topscores);
195 $table = new html_table();
196 $table->align = array('center', 'left', 'right');
197 $table->wrap = array();
198 $table->width = "30%";
199 $table->cellspacing = '10px';
200 $table->size = array('*', '*', '*');
202 $table->head = array(get_string("rank", "lesson"), get_string('name'), get_string("scores", "lesson"));
204 $printed = 0;
205 while (true) {
206 $temp = current($topscores);
207 $score = key($topscores);
208 $rank = $printed + 1;
209 sort($temp);
210 foreach ($temp as $student) {
211 $table->data[] = array($rank, $student, $score.'%');
213 $printed++;
214 if (!next($topscores) || !($printed < $lesson->maxhighscores)) {
215 break;
218 echo html_writer::table($table);
221 if (!has_capability('mod/lesson:manage', $context)) { // teachers don't need the links
222 echo $OUTPUT->box_start('mdl-align');
223 echo $OUTPUT->box_start('lessonbutton standardbutton');
224 if ($link) {
225 echo html_writer::link(new moodle_url('/course/view.php', array('id'=>$course->id)), get_string("returntocourse", "lesson"));
226 } else {
227 echo html_writer::link(new moodle_url('/course/view.php', array('id'=>$course->id)), get_string("cancel", "lesson")). ' ';
228 echo html_writer::link(new moodle_url('/mod/lesson/view.php', array('id'=>$cm->id, 'viewed'=>'1')), get_string("startlesson", "lesson"));
230 echo $OUTPUT->box_end();
231 echo $OUTPUT->box_end();
233 break;
236 echo $lessonoutput->footer();