|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00028 require_once('../../config.php'); 00029 require_once($CFG->dirroot.'/mod/lesson/locallib.php'); 00030 00031 $id = required_param('id', PARAM_INT); // Course Module ID 00032 $mode = optional_param('mode', '', PARAM_ALPHA); 00033 $link = optional_param('link', 0, PARAM_INT); 00034 00035 $cm = get_coursemodule_from_id('lesson', $id, 0, false, MUST_EXIST);; 00036 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); 00037 $lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST)); 00038 00039 require_login($course, false, $cm); 00040 00041 $url = new moodle_url('/mod/lesson/highscores.php', array('id'=>$id)); 00042 if ($mode !== '') { 00043 $url->param('mode', $mode); 00044 } 00045 if ($link !== 0) { 00046 $url->param('link', $link); 00047 } 00048 $PAGE->set_url($url); 00049 00050 $context = get_context_instance(CONTEXT_MODULE, $cm->id); 00051 00052 switch ($mode) { 00053 case 'add': 00054 // Ensure that we came from view.php 00055 if (!confirm_sesskey() or !data_submitted()) { 00056 print_error('invalidformdata'); 00057 } 00058 break; 00059 00060 case 'save': 00061 if (confirm_sesskey() and $form = data_submitted($CFG->wwwroot.'/mod/lesson/view.php')) { 00062 $name = trim(optional_param('name', '', PARAM_CLEAN)); 00063 00064 // Make sure it is not empty 00065 if (empty($name)) { 00066 $lesson->add_message(get_string('missingname', 'lesson')); 00067 $mode = 'add'; 00068 break; 00069 } 00070 // Check for censored words 00071 $filterwords = explode(',', get_string('censorbadwords')); 00072 foreach ($filterwords as $filterword) { 00073 if (strstr($name, $filterword)) { 00074 $lesson->add_message(get_string('namereject', 'lesson')); 00075 $mode = 'add'; 00076 break; 00077 } 00078 } 00079 // Bad word was found 00080 if ($mode == 'add') { 00081 break; 00082 } 00083 $params = array ("lessonid" => $lesson->id, "userid" => $USER->id); 00084 if (!$grades = $DB->get_records_select('lesson_grades', "lessonid = :lessonid", $params, 'completed')) { 00085 print_error('cannotfindfirstgrade', 'lesson'); 00086 } 00087 00088 if (!$newgrade = $DB->get_record_sql("SELECT * 00089 FROM {lesson_grades} 00090 WHERE lessonid = :lessonid 00091 AND userid = :userid 00092 ORDER BY completed DESC", $params, true)) { 00093 print_error('cannotfindnewestgrade', 'lesson'); 00094 } 00095 00096 // Check for multiple submissions 00097 if ($DB->record_exists('lesson_high_scores', array('gradeid' => $newgrade->id))) { 00098 print_error('onpostperpage', 'lesson'); 00099 } 00100 00101 // Find out if we need to delete any records 00102 if ($highscores = $DB->get_records_sql("SELECT h.*, g.grade 00103 FROM {lesson_grades} g, {lesson_high_scores} h 00104 WHERE h.gradeid = g.id 00105 AND h.lessonid = :lessonid 00106 ORDER BY g.grade DESC", $params)) { 00107 // Only count unique scores in our total for max high scores 00108 $uniquescores = array(); 00109 foreach ($highscores as $highscore) { 00110 $uniquescores[$highscore->grade] = 1; 00111 } 00112 if (count($uniquescores) >= $lesson->maxhighscores) { 00113 // Top scores list is full, might need to delete a score 00114 $flag = true; 00115 // See if the new score is already listed in the top scores list 00116 // if it is listed, then dont need to delete any records 00117 foreach ($highscores as $highscore) { 00118 if ($newgrade->grade == $highscore->grade) { 00119 $flag = false; 00120 } 00121 } 00122 if ($flag) { 00123 // Pushing out the lowest score (could be multiple records) 00124 $lowscore = 0; 00125 foreach ($highscores as $highscore) { 00126 if (empty($lowscore) or $lowscore > $highscore->grade) { 00127 $lowscore = $highscore->grade; 00128 } 00129 } 00130 // Now, delete all high scores with the low score 00131 foreach ($highscores as $highscore) { 00132 if ($highscore->grade == $lowscore) { 00133 $DB->delete_records('lesson_high_scores', array('id' => $highscore->id)); 00134 } 00135 } 00136 } 00137 } 00138 } 00139 00140 $newhighscore = new stdClass; 00141 $newhighscore->lessonid = $lesson->id; 00142 $newhighscore->userid = $USER->id; 00143 $newhighscore->gradeid = $newgrade->id; 00144 $newhighscore->nickname = $name; 00145 00146 $DB->insert_record('lesson_high_scores', $newhighscore); 00147 00148 // Log it 00149 add_to_log($course->id, 'lesson', 'update highscores', "highscores.php?id=$cm->id", $name, $cm->id); 00150 00151 $lesson->add_message(get_string('postsuccess', 'lesson'), 'notifysuccess'); 00152 redirect("$CFG->wwwroot/mod/lesson/highscores.php?id=$cm->id&link=1"); 00153 } else { 00154 print_error('invalidformdata'); 00155 } 00156 break; 00157 } 00158 00159 // Log it 00160 add_to_log($course->id, 'lesson', 'view highscores', "highscores.php?id=$cm->id", $lesson->name, $cm->id); 00161 00162 $lessonoutput = $PAGE->get_renderer('mod_lesson'); 00163 echo $lessonoutput->header($lesson, $cm, 'highscores'); 00164 00165 switch ($mode) { 00166 case 'add': 00167 echo $lessonoutput->add_highscores_form($lesson); 00168 break; 00169 default: 00170 $params = array ("lessonid" => $lesson->id); 00171 if (!$grades = $DB->get_records_select("lesson_grades", "lessonid = :lessonid", $params, "completed")) { 00172 $grades = array(); 00173 } 00174 00175 echo $OUTPUT->heading(get_string("topscorestitle", "lesson", $lesson->maxhighscores), 4); 00176 00177 if (!$highscores = $DB->get_records_select("lesson_high_scores", "lessonid = :lessonid", $params)) { 00178 echo $OUTPUT->heading(get_string("nohighscores", "lesson"), 3); 00179 } else { 00180 foreach ($highscores as $highscore) { 00181 $grade = $grades[$highscore->gradeid]->grade; 00182 $topscores[$grade][] = $highscore->nickname; 00183 } 00184 krsort($topscores); 00185 00186 $table = new html_table(); 00187 $table->align = array('center', 'left', 'right'); 00188 $table->wrap = array(); 00189 $table->width = "30%"; 00190 $table->cellspacing = '10px'; 00191 $table->size = array('*', '*', '*'); 00192 00193 $table->head = array(get_string("rank", "lesson"), get_string('name'), get_string("scores", "lesson")); 00194 00195 $printed = 0; 00196 while (true) { 00197 $temp = current($topscores); 00198 $score = key($topscores); 00199 $rank = $printed + 1; 00200 sort($temp); 00201 foreach ($temp as $student) { 00202 $table->data[] = array($rank, $student, $score.'%'); 00203 } 00204 $printed++; 00205 if (!next($topscores) || !($printed < $lesson->maxhighscores)) { 00206 break; 00207 } 00208 } 00209 echo html_writer::table($table); 00210 } 00211 00212 if (!has_capability('mod/lesson:manage', $context)) { // teachers don't need the links 00213 echo $OUTPUT->box_start('mdl-align'); 00214 echo $OUTPUT->box_start('lessonbutton standardbutton'); 00215 if ($link) { 00216 echo html_writer::link(new moodle_url('/course/view.php', array('id'=>$course->id)), get_string("returntocourse", "lesson")); 00217 } else { 00218 echo html_writer::link(new moodle_url('/course/view.php', array('id'=>$course->id)), get_string("cancel", "lesson")). ' '; 00219 echo html_writer::link(new moodle_url('/mod/lesson/view.php', array('id'=>$cm->id, 'viewed'=>'1')), get_string("startlesson", "lesson")); 00220 } 00221 echo $OUTPUT->box_end(); 00222 echo $OUTPUT->box_end(); 00223 } 00224 break; 00225 } 00226 00227 echo $lessonoutput->footer();