]> git.mjollnir.org Git - moodle.git/commitdiff
Moved high scores logic out of view.php into highscores.php
authormark-nielsen <mark-nielsen>
Sun, 10 Sep 2006 01:20:00 +0000 (01:20 +0000)
committermark-nielsen <mark-nielsen>
Sun, 10 Sep 2006 01:20:00 +0000 (01:20 +0000)
Changed: now need to enter password on password protected lessons before being able to view high scores
Added logs to high scores and reworked logic

mod/lesson/action/continue.php
mod/lesson/highscores.php [new file with mode: 0644]
mod/lesson/lesson.php
mod/lesson/locallib.php
mod/lesson/tabs.php
mod/lesson/view.php

index a67de22384e6089ca7244635ea2ca4e4ba62ed2c..49bf28fa21ebc5e6a6861e69d82f0b99210d3468 100644 (file)
         lesson_set_message('('.get_string("maximumnumberofattemptsreached", "lesson").')');
     }
     
-    lesson_print_header($cm, $course, $lesson, 'navigation', false);
+    lesson_print_header($cm, $course, $lesson, 'navigation');
 
     include($CFG->wwwroot.'/mod/lesson/action/continue.html');
 ?>
diff --git a/mod/lesson/highscores.php b/mod/lesson/highscores.php
new file mode 100644 (file)
index 0000000..bcffe24
--- /dev/null
@@ -0,0 +1,210 @@
+<?php  // $Id$
+/**
+ * Provides the interface for viewing and adding high scores
+ *
+ * @version $Id$
+ * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
+ * @package lesson
+ **/
+
+    require_once('../../config.php');
+    require_once('locallib.php');
+    require_once('lib.php');
+
+    $id      = required_param('id', PARAM_INT);             // Course Module ID
+    $mode    = optional_param('mode', '', PARAM_ALPHA);
+    $link = optional_param('link', 0, PARAM_INT);
+
+    list($cm, $course, $lesson) = lesson_get_basics($id);
+    
+    require_login($course->id, false, $cm);
+    
+    $context = get_context_instance(CONTEXT_MODULE, $cm->id);
+    
+    require_capability('mod/lesson:view', $context);
+
+    switch ($mode) {
+        case 'add':
+            // Ensure that we came from view.php
+            if (!confirm_sesskey() or !data_submitted("$CFG->wwwroot/mod/lesson/view.php")) {
+                error('Incorrect Form Data');
+            }
+            break;
+            
+        case 'save':
+            if (confirm_sesskey() and $form = data_submitted($CFG->wwwroot.'/mod/lesson/view.php')) {
+                $name = trim(optional_param('name', '', PARAM_CLEAN));
+                
+                // Make sure it is not empty
+                if (empty($name)) {
+                    lesson_set_message(get_string('missingname', 'lesson'));
+                    $mode = 'add';
+                    break;
+                }     
+                // Check for censored words
+                $filterwords = explode(',', get_string('censorbadwords'));
+                foreach ($filterwords as $filterword) {
+                    if (strstr($name, $filterword)) {
+                        lesson_set_message(get_string('namereject', 'lesson'));
+                        $mode = 'add';
+                        break;
+                    }
+                }
+                // Bad word was found
+                if ($mode == 'add') {
+                    break;
+                }
+                
+                if (!$grades = get_records_select('lesson_grades', "lessonid = $lesson->id", 'completed')) {
+                    error('Error: could not find grades');
+                }
+                if (!$newgrade = get_record_sql("SELECT * 
+                                                   FROM {$CFG->prefix}lesson_grades 
+                                                  WHERE lessonid = $lesson->id
+                                                    AND userid = $USER->id 
+                                               ORDER BY completed DESC", true)) {
+                    error('Error: could not find newest grade');
+                }
+                
+                // Check for multiple submissions
+                if (record_exists('lesson_high_scores', 'gradeid', $newgrade->id)) {
+                    error('Only one posting per grade');
+                }
+                
+                // Find out if we need to delete any records
+                if ($highscores = get_records_sql("SELECT h.*, g.grade 
+                                                     FROM {$CFG->prefix}lesson_grades g, {$CFG->prefix}lesson_high_scores h 
+                                                    WHERE h.gradeid = g.id
+                                                    AND h.lessonid = $lesson->id
+                                                    ORDER BY g.grade DESC")) {
+                    // Only count unique scores in our total for max high scores
+                    $uniquescores = array();
+                    foreach ($highscores as $highscore) {
+                        $uniquescores[$highscore->grade] = 1;
+                    }
+                    if (count($uniquescores) >= $lesson->maxhighscores) {
+                        // Top scores list is full, might need to delete a score
+                        $flag = true;                
+                        // See if the new score is already listed in the top scores list
+                        // if it is listed, then dont need to delete any records
+                        foreach ($highscores as $highscore) {
+                            if ($newgrade->grade == $highscore->grade) {
+                                $flag = false;
+                            }
+                        }    
+                        if ($flag) {
+                            // Pushing out the lowest score (could be multiple records)
+                            $lowscore = 0;
+                            foreach ($highscores as $highscore) {
+                                if (empty($lowscore) or $lowscore > $highscore->grade) {
+                                    $lowscore = $highscore->grade;
+                                }
+                            }
+                            // Now, delete all high scores with the low score
+                            foreach ($highscores as $highscore) {
+                                if ($highscore->grade == $lowscore) {
+                                    delete_records('lesson_high_scores', 'id', $highscore->id);
+                                }
+                            }
+                        }
+                    }
+                }
+
+                $newhighscore = new stdClass;
+                $newhighscore->lessonid = $lesson->id;
+                $newhighscore->userid = $USER->id;
+                $newhighscore->gradeid = $newgrade->id;
+                $newhighscore->nickname = $name;
+
+                if (!insert_record('lesson_high_scores', $newhighscore)) {
+                    error("Insert of new high score Failed!");
+                }
+                
+                // Log it
+                add_to_log($course->id, 'lesson', 'update highscores', "highscores.php?id=$cm->id", $name, $cm->id);
+                
+                lesson_set_message(get_string('postsuccess', 'lesson'), 'notifysuccess');
+                redirect("$CFG->wwwroot/mod/lesson/highscores.php?id=$cm->id&amp;link=1");
+            } else {
+                error('Something is wrong with the form data');
+            }
+            break;
+    }
+
+    // Log it
+    add_to_log($course->id, 'lesson', 'view highscores', "highscores.php?id=$cm->id", $lesson->name, $cm->id);
+
+    lesson_print_header($cm, $course, $lesson, 'highscores');
+
+    switch ($mode) {
+        case 'add':
+            print_simple_box_start('center');
+            echo '<div align="center">
+                 <form name="nickname" method ="post" action="'.$CFG->wwwroot.'/mod/lesson/highscores.php">
+                 <input type="hidden" name="id" value="'.$cm->id.'" />
+                 <input type="hidden" name="mode" value="save" />
+                 <input type="hidden" name="sesskey" value="'.sesskey().'" />';
+
+            echo get_string("entername", "lesson").": <input type=\"text\" name=\"name\" size=\"7\" maxlength=\"5\">\n<p>\n";
+            lesson_print_submit_link(get_string("submitname", "lesson"), 'nickname');
+            echo "</p>\n</form>\n</div>\n";
+            print_simple_box_end();
+            break;
+        default:
+            if (!$grades = get_records_select("lesson_grades", "lessonid = $lesson->id", "completed")) {
+                $grades = array();
+            }
+        
+            print_heading(get_string("topscorestitle", "lesson", $lesson->maxhighscores), 'center', 4);
+
+            if (!$highscores = get_records_select("lesson_high_scores", "lessonid = $lesson->id")) {
+                print_heading(get_string("nohighscores", "lesson"), 'center', 3);
+            } else {
+                foreach ($highscores as $highscore) {
+                    $grade = $grades[$highscore->gradeid]->grade;
+                    $topscores[$grade][] = $highscore->nickname;
+                }
+                krsort($topscores);
+                       
+                $table = new stdClass;
+                $table->align = array('center', 'left', 'right');
+                $table->wrap = array();
+                $table->width = "30%";
+                $table->cellspacing = '10px';
+                $table->size = array('*', '*', '*');
+            
+                $table->head = array(get_string("rank", "lesson"), $course->students, get_string("scores", "lesson"));
+            
+                $printed = 0;
+                while (true) {
+                    $temp = current($topscores);
+                    $score = key($topscores);
+                    $rank = $printed + 1;
+                    sort($temp); 
+                    foreach ($temp as $student) {
+                        $table->data[] = array($rank, $student, $score.'%');
+                    }
+                    $printed++;
+                    if (!next($topscores) || !($printed < $lesson->maxhighscores)) { 
+                        break;
+                    }
+                }
+                print_table($table);
+            }
+        
+            if (!has_capability('mod/lesson:manage', $context)) {  // teachers don't need the links
+                echo '<div align="center">';
+                if ($link) {
+                    echo "<br /><div class=\"lessonbutton standardbutton\"><a href=\"../../course/view.php?id=$course->id\">".get_string("returntocourse", "lesson")."</a></div>";
+                } else {
+                    echo "<br /><span class=\"lessonbutton standardbutton\"><a href=\"../../course/view.php?id=$course->id\">".get_string("cancel", "lesson").'</a></span> '.
+                        " <span class=\"lessonbutton standardbutton\"><a href=\"view.php?id=$cm->id&amp;action=navigation\">".get_string("startlesson", "lesson").'</a></span>';
+                }
+                echo "</div>";
+            }
+            break;
+    }
+    
+    print_footer($course);
+
+?>
\ No newline at end of file
index 08cb72eb91f4e3ae031eb9d40dab2c81441f9d52..e0cf6b023203d63461c6d2662f003fcb5d4eed8c 100644 (file)
@@ -40,7 +40,7 @@
         case 'confirmdelete':
         case 'editpage':
         case 'move':
-            lesson_print_header($cm, $course, $lesson, '', false);
+            lesson_print_header($cm, $course, $lesson);
         case 'addcluster':
         case 'addendofbranch':
         case 'addendofcluster':
index 64c54f46465478701635e448d376b58e53be7244..aa75be66d1351f5b1b03d996bf5aa274ed604c0f 100644 (file)
@@ -217,7 +217,7 @@ if (!defined("LESSON_RESPONSE_EDITOR")) {
  * @param boolean $printheading Print the a heading with the lesson name
  * @return void
  **/
-function lesson_print_header($cm, $course, $lesson, $currenttab = '', $printheading = true) {
+function lesson_print_header($cm, $course, $lesson, $currenttab = '') {
     global $CFG, $USER;
     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
     
@@ -238,8 +238,10 @@ function lesson_print_header($cm, $course, $lesson, $currenttab = '', $printhead
                   '', '', true, update_module_button($cm->id, $course->id, $strlesson),
                   navmenu($course, $cm));
                   
-    if ($printheading) {
+    if (has_capability('mod/lesson:manage')) {
         print_heading_with_help(format_string($lesson->name, true), "overview", "lesson");
+    } else {
+        print_heading($lesson->name);
     }
     
     if (!empty($currenttab) and has_capability('mod/lesson:manage', $context)) {
index 12d8e9759df1c5e638593aece4da9aec293a24d3..be5e093a6ee9758baeaa0a46eb9650cf5e84e154 100644 (file)
@@ -43,7 +43,7 @@
         $row[] = new tabobject('essay', "$CFG->wwwroot/mod/lesson/essay.php?id=$cm->id", get_string('manualgrading', 'lesson'));
     }
     if ($lesson->highscores) {
-        $row[] = new tabobject('highscores', "$CFG->wwwroot/mod/lesson/view.php?id=$cm->id&amp;action=highscores", get_string('highscores', 'lesson'));
+        $row[] = new tabobject('highscores', "$CFG->wwwroot/mod/lesson/highscores.php?id=$cm->id", get_string('highscores', 'lesson'));
     }
 
     $tabs[] = $row;
index cd9e6c12ba598c996bc536db9d01d6273fadc886..701f4a3a5f45f87f805397b88915f408f7e030cb 100644 (file)
             print_simple_box_end();
             print_footer($course);
             exit();
-        } elseif ($lesson->highscores && !$lesson->practice) {
-            $action = 'highscores';
+        } elseif ($lesson->highscores and !$lesson->practice) {
+            if ($lesson->usepassword and empty($USER->lessonloggedin[$lesson->id])) {
+                // User needs to be logged in
+                $action = 'navigation';
+            } else {
+                redirect("$CFG->wwwroot/mod/lesson/highscores.php?id=$cm->id");
+            }
         } else {
             $action = 'navigation';
         }
                 if ($lesson->password == md5(trim($password))) {
                     $USER->lessonloggedin[$lesson->id] = true;
                     $correctpass = true;
+                    if ($lesson->highscores) {
+                        // Logged in, now we can show high scores
+                        redirect("$CFG->wwwroot/mod/lesson/highscores.php?id=$cm->id", '', 0);
+                    }
                 }
             } elseif (isset($USER->lessonloggedin[$lesson->id])) {
                 $correctpass = true;
             // high scores code
             if ($lesson->highscores && !has_capability('mod/lesson:manage', $context) && !$lesson->practice) {
                 echo "<div align=\"center\"><br>";
-                if (!$grades = get_records_select("lesson_grades", "lessonid = $lesson->id", "completed")) {
-                    echo get_string("youmadehighscore", "lesson", $lesson->maxhighscores)."<br>";
-                    echo "<a href=\"view.php?id=$cm->id&amp;action=nameforhighscores\">".get_string("clicktopost", "lesson")."</a><br>";
-                } else {
-                    if (!$highscores = get_records_select("lesson_high_scores", "lessonid = $lesson->id")) {
-                        echo get_string("youmadehighscore", "lesson", $lesson->maxhighscores)."<br>";
-                        echo "<div class=\"lessonbutton standardbutton\"><a href=\"view.php?id=$cm->id&amp;action=nameforhighscores\">".get_string("clicktopost", "lesson")."</a></div><br/>";
-                    } else {
+                if ($grades = get_records_select("lesson_grades", "lessonid = $lesson->id", "completed")) {
+                    $madeit = false;
+                    if ($highscores = get_records_select("lesson_high_scores", "lessonid = $lesson->id")) {
                         // get all the high scores into an array
                         foreach ($highscores as $highscore) {
                             $grade = $grades[$highscore->gradeid]->grade;
                         sort($topscores);
                         $lowscore = $topscores[0];
                         
-                        if ($thegrade >= $lowscore || count($topscores) <= $lesson->maxhighscores) {
-                            echo get_string("youmadehighscore", "lesson", $lesson->maxhighscores)."<br>";
-                            echo "<div class=\"lessonbutton standardbutton\"><a href=\"view.php?id=$cm->id&amp;action=nameforhighscores\">".get_string("clicktopost", "lesson")."</a></div><br />";
-                        } else {
-                            echo get_string("nothighscore", "lesson", $lesson->maxhighscores)."<br>";
+                        if ($gradeinfo->grade >= $lowscore || count($topscores) <= $lesson->maxhighscores) {
+                            $madeit = true;
                         }
                     }
+                    if (!$highscores or $madeit) {
+                        echo '<p>'.get_string("youmadehighscore", "lesson", $lesson->maxhighscores).
+                             '</p><p>
+                              <form method="post" name="highscores" action="'.$CFG->wwwroot.'/mod/lesson/highscores.php">
+                              <input type="hidden" name="mode" value="add" />
+                              <input type="hidden" name="id" value="'.$cm->id.'" />
+                              <input type="hidden" name="sesskey" value="'.sesskey().'" />
+                              <p>';
+                              lesson_print_submit_link(get_string('clicktopost', 'lesson'), 'highscores');
+                        echo '</p>
+                              </form>';
+                    } else {
+                        echo get_string("nothighscore", "lesson", $lesson->maxhighscores)."<br>";
+                    }
                 }
-                echo "<br /><div style=\"padding: 5px;\" class=\"lessonbutton standardbutton\"><a href=\"view.php?id=$cm->id&amp;action=highscores&link=1\">".get_string("viewhighscores", "lesson").'</a></div>';
+                echo "<br /><div style=\"padding: 5px;\" class=\"lessonbutton standardbutton\"><a href=\"$CFG->wwwroot/mod/lesson/highscores.php?id=$cm->id&amp;link=1\">".get_string("viewhighscores", "lesson").'</a></div>';
                 echo "</div>";                            
             }
 
             echo "</table></form>\n";
         } 
     }
-
-    /*******************high scores **************************************/
-    elseif ($action == 'highscores') {
-        print_heading_with_help(format_string($lesson->name,true), "overview", "lesson");
-
-        if (!$grades = get_records_select("lesson_grades", "lessonid = $lesson->id", "completed")) {
-            $grades = array();
-        }
-        
-        print_heading(get_string("topscorestitle", "lesson", $lesson->maxhighscores), 'center', 4);
-
-        if (!$highscores = get_records_select("lesson_high_scores", "lessonid = $lesson->id")) {
-            print_heading(get_string("nohighscores", "lesson"), 'center', 3);
-        } else {
-            foreach ($highscores as $highscore) {
-                $grade = $grades[$highscore->gradeid]->grade;
-                $topscores[$grade][] = $highscore->nickname;
-            }
-            krsort($topscores);
-                       
-            $table = new stdClass;
-            $table->align = array('center', 'left', 'right');
-            $table->wrap = array();
-            $table->width = "30%";
-            $table->cellspacing = '10px';
-            $table->size = array('*', '*', '*');
-            
-            $table->head = array(get_string("rank", "lesson"), $course->students, get_string("scores", "lesson"));
-            
-            $printed = 0;
-            while (true) {
-                $temp = current($topscores);
-                $score = key($topscores);
-                $rank = $printed + 1;
-                sort($temp); 
-                foreach ($temp as $student) {
-                    $table->data[] = array($rank, $student, $score);
-                }
-                $printed++;
-                if (!next($topscores) || !($printed < $lesson->maxhighscores)) { 
-                    break;
-                }
-            }
-            print_table($table);
-        }
-        
-        if (!has_capability('mod/lesson:manage', $context)) {  // teachers don't need the links
-            echo '<div align="center">';
-            if (optional_param('link', 0, PARAM_INT)) {
-                echo "<br /><div class=\"lessonbutton standardbutton\"><a href=\"../../course/view.php?id=$course->id\">".get_string("returntocourse", "lesson")."</a></div>";
-            } else {
-                echo "<br /><span class=\"lessonbutton standardbutton\"><a href=\"../../course/view.php?id=$course->id\">".get_string("cancel", "lesson").'</a></span> '.
-                    " <span class=\"lessonbutton standardbutton\"><a href=\"view.php?id=$cm->id&amp;action=navigation\">".get_string("startlesson", "lesson").'</a></span>';
-            }
-            echo "</div>";
-        }
-    }
-    /*******************update high scores **************************************/
-    elseif ($action == 'updatehighscores') {
-        print_heading_with_help(format_string($lesson->name,true), "overview", "lesson");
-    
-        confirm_sesskey();
-
-        if (!$grades = get_records_select("lesson_grades", "lessonid = $lesson->id", "completed")) {
-            error("Error: could not find grades");
-        }
-        if (!$usergrades = get_records_select("lesson_grades", "lessonid = $lesson->id and userid = $USER->id", "completed DESC")) {
-            error("Error: could not find grades");
-        }
-        echo "<div align=\"center\">";
-        echo get_string("waitpostscore", "lesson")."<br>";
-        
-        foreach ($usergrades as $usergrade) {
-            // get their latest grade
-            $newgrade = $usergrade;
-            break;
-        }
-        
-        if ($pasthighscore = get_record_select("lesson_high_scores", "lessonid = $lesson->id and userid = $USER->id")) {
-            $pastgrade = $grades[$pasthighscore->gradeid]->grade;
-            if ($pastgrade >= $newgrade->grade) {
-                redirect("view.php?id=$cm->id&amp;action=highscores&amp;link=1", "Update Successful");
-            } else {
-                // delete old and find out where new one goes
-                if (!delete_records("lesson_high_scores", "id", $pasthighscore->id)) {
-                    error("Error: could not delete old high score");
-                }
-            }
-        }
-        // find out if we need to delete any records
-        if ($highscores = get_records_select("lesson_high_scores", "lessonid = $lesson->id")) {  // if no high scores... then just insert our new one
-            foreach ($highscores as $highscore) {
-                $grade = $grades[$highscore->gradeid]->grade;
-                $topscores[$grade][] = $highscore->userid;
-            }
-            if (!(count($topscores) < $lesson->maxhighscores)) { // if the top scores list is not full then dont need to worry about removing old scores
-                $scores = array_keys($topscores);
-                $flag = true;                
-                // see if the new score is already listed in the top scores list
-                // if it is listed, then dont need to delete any records
-                foreach ($scores as $score) {
-                    if ($score = $newgrade->grade) {
-                        $flag = false;
-                    }
-                }    
-                if ($flag) { // if the score does not exist in the top scores list, then the lowest scores get thrown out.
-                    ksort($topscores); // sort so the lowest score is first element
-                    $lowscore = current($topscores);
-                    // making a delete statement to delete all users with the lowest score
-                    $deletestmt = 'lessonid = '. $lesson->id .' and userid = ';
-                    $deletestmt .= current($lowscore);
-                    while (next($lowscore)) {
-                        $deletestmt .= " or userid = ".current($lowscore);
-                    }
-                    if (!delete_records_select('lesson_high_scores', $deletestmt)) {
-                        /// not a big deal...
-                        error('Did not delete extra high score(s)');
-                    }
-                }
-            }
-        }
-        
-        $newhighscore = new stdClass;
-        $newhighscore->lessonid = $lesson->id;
-        $newhighscore->userid = $USER->id;
-        $newhighscore->gradeid = $newgrade->id;
-        $newhighscore->nickname = optional_param('name', '', PARAM_CLEAN);
-        
-        if (!insert_record("lesson_high_scores", $newhighscore)) {
-            error("Insert of new high score Failed!");
-        }
-        
-        redirect("view.php?id=$cm->id&amp;action=highscores&amp;link=1", get_string("postsuccess", "lesson"));
-        echo "</div>";
-    }
-    /*******************name for highscores **************************************/
-    elseif ($action == 'nameforhighscores') {
-        print_heading_with_help(format_string($lesson->name,true), "overview", "lesson");
-        echo "<div align=\"center\">";
-        if ($name = trim(optional_param('name', '', PARAM_CLEAN))) {
-            if (lesson_check_nickname($name)) {
-                redirect("view.php?id=$cm->id&amp;action=updatehighscores&amp;name=$name&amp;sesskey=".$USER->sesskey, get_string("nameapproved", "lesson"));
-            } else {
-                echo get_string("namereject", "lesson")."<br /><br />";
-            }
-        }
-                
-        echo "<form name=\"nickname\" method =\"post\" action=\"view.php\">";
-        echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />";
-        echo "<input type=\"hidden\" name=\"action\" value=\"nameforhighscores\" />";
         
-        echo get_string("entername", "lesson").": <input type=\"text\" name=\"name\" maxlength=\"5\"><br />";
-        echo "<input type=\"submit\" value=\"".get_string("submitname", "lesson")."\" />";
-        echo "</form>";
-        echo "</div>";
-    }    
     /*************** no man's land **************************************/
     else {
         error("Fatal Error: Unknown Action: ".$action."\n");