MDL-18272, MDL-18271: Conditional availability - grade condition to use percentages...
authorsam_marshall <sam_marshall>
Wed, 18 Feb 2009 17:50:13 +0000 (17:50 +0000)
committersam_marshall <sam_marshall>
Wed, 18 Feb 2009 17:50:13 +0000 (17:50 +0000)
course/moodleform_mod.php
lang/en_utf8/condition.php
lib/conditionlib.php
lib/db/install.xml
lib/db/upgrade.php
version.php

index 2a0283e057e15cc934a3e764cf8d71f62e2c7433..74ece3fc97361e38338a7948f6e1964296f54c6d 100644 (file)
@@ -414,8 +414,9 @@ class moodleform_mod extends moodleform {
             $grouparray[] =& $mform->createElement('select','conditiongradeitemid','',$gradeoptions);
             $grouparray[] =& $mform->createElement('static', '', '',' '.get_string('grade_atleast','condition').' ');
             $grouparray[] =& $mform->createElement('text', 'conditiongrademin','',array('size'=>3));
-            $grouparray[] =& $mform->createElement('static', '', '',' '.get_string('grade_upto','condition').' ');
+            $grouparray[] =& $mform->createElement('static', '', '','% '.get_string('grade_upto','condition').' ');
             $grouparray[] =& $mform->createElement('text', 'conditiongrademax','',array('size'=>3));
+            $grouparray[] =& $mform->createElement('static', '', '','%');
             $mform->setType('conditiongrademin',PARAM_FLOAT);            
             $mform->setType('conditiongrademax',PARAM_FLOAT);            
             $group = $mform->createElement('group','conditiongradegroup', 
index eb5ebb9a92ea07aa162d2f7867ada5534d49f0cb..4aae29d6f28229ac608cb08505bbe7e60cfde2f3 100644 (file)
@@ -11,8 +11,8 @@ $string['completion_pass']=' must be complete with pass grade';
 $string['completion_fail']=' must be complete with fail grade';
 $string['configenableavailability']='When enabled, this lets you set conditions (based on date, grade, or completion) that control whether an activity is available.';
 $string['enableavailability']='Enable conditional availability';
-$string['grade_atleast']='must be &ge;';
-$string['grade_upto']='and &lt;';
+$string['grade_atleast']='must be at least';
+$string['grade_upto']='and less than';
 $string['gradecondition']='Grade condition';
 $string['completioncondition']='Activity completion condition';
 $string['help_conditiondates']='available dates';
index d14916b542c68dea26f61ec24e177b3bf623aff2..5b9b503ef9d3a6832a549b189c87f2a8ce3d51ba 100644 (file)
@@ -439,8 +439,9 @@ WHERE
      * @param bool $grabthelot If true, grabs all scores for current user on 
      *   this course, so that later ones come from cache
      * @param int $userid Set if requesting grade for a different user (does 
-     * not use cache)
-     * @return float Grade score, or false if user does not have a grade yet
+     *   not use cache)
+     * @return float Grade score as a percentage in range 0-100 (e.g. 100.0 
+     *   or 37.21), or false if user does not have a grade yet
      */
     private function get_cached_grade_score($gradeitemid,$grabthelot=false,$userid=0) {
         global $USER, $DB, $SESSION;
@@ -455,7 +456,7 @@ WHERE
                     // Get all grades for the current course
                     $rs=$DB->get_recordset_sql("
 SELECT
-    gi.id,gg.finalgrade 
+    gi.id,gg.finalgrade,gg.rawgrademin,gg.rawgrademax 
 FROM 
     {grade_items} gi
     LEFT JOIN {grade_grades} gg ON gi.id=gg.itemid AND gg.userid=?
@@ -464,8 +465,11 @@ WHERE
                     foreach($rs as $record) {
                         $SESSION->gradescorecache[$record->id]=
                             is_null($record->finalgrade)
+                                // No grade = false
                                 ? false 
-                                : $record->finalgrade;
+                                // Otherwise convert grade to percentage
+                                : (($record->finalgrade - $record->rawgrademin) * 100) /
+                                    ($record->rawgrademax - $record->rawgrademin);
 
                     }
                     $rs->close();
@@ -477,11 +481,14 @@ WHERE
                     }
                 } else {
                     // Just get current grade
-                    $score=$DB->get_field('grade_grades','finalgrade',array(
+                    $record = $DB->get_record('grade_grades', array(
                         'userid'=>$USER->id,'itemid'=>$gradeitemid));
-                    // Treat the case where row exists but is null, same as
-                    // case where row doesn't exist
-                    if(is_null($score)) {
+                    if ($record && !is_null($record->finalgrade)) {
+                        $score = (($record->finalgrade - $record->rawgrademin) * 100) /
+                            ($record->rawgrademax - $record->rawgrademin);
+                    } else {
+                        // Treat the case where row exists but is null, same as
+                        // case where row doesn't exist
                         $score=false;
                     }
                     $SESSION->gradescorecache[$gradeitemid]=$score;
@@ -490,9 +497,14 @@ WHERE
             return $SESSION->gradescorecache[$gradeitemid];
         } else {
             // Not the current user, so request the score individually
-            $score=$DB->get_field('grade_grades','finalgrade',array(
-                'userid'=>$userid,'itemid'=>$gradeitemid));
-            if($score===null) {
+            $record = $DB->get_record('grade_grades', array(
+                'userid'=>$userid, 'itemid'=>$gradeitemid));
+            if ($record && !is_null($record->finalgrade)) {
+                $score = (($record->finalgrade - $record->rawgrademin) * 100) /
+                    ($record->rawgrademax - $record->rawgrademin);
+            } else {
+                // Treat the case where row exists but is null, same as
+                // case where row doesn't exist
                 $score=false;
             }
             return $score;
index 17815019b1f012668be63e26b41ef08a2bd8d635..ab65869d9ec5e435ab9b4481bdc2fe254c2b6dc6 100644 (file)
         <FIELD NAME="sourcecmid" TYPE="int" LENGTH="10" NOTNULL="false" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="If this condition is based on completion of another activity, then this is the course-module ID of that activity. Otherwise null." PREVIOUS="coursemoduleid" NEXT="requiredcompletion"/>
         <FIELD NAME="requiredcompletion" TYPE="int" LENGTH="1" NOTNULL="false" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="If this condition is on a module's completion, then this should be set to the required completion state. Otherwise null. Suitable values are 1 = completed, 2 = completed-passed, 3 = completed-failed." PREVIOUS="sourcecmid" NEXT="gradeitemid"/>
         <FIELD NAME="gradeitemid" TYPE="int" LENGTH="10" NOTNULL="false" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="If this condition is based on a gradebook score, the item ID is given here (and the item will now not be available until a value is achieved for that grade). Otherwise null." PREVIOUS="requiredcompletion" NEXT="grademin"/>
-        <FIELD NAME="grademin" TYPE="number" LENGTH="10" NOTNULL="false" UNSIGNED="false" SEQUENCE="false" ENUM="false" DECIMALS="5" COMMENT="If set, this is the minimum grade that must be reached (greater than or equal) in order for this module to appear. Otherwise null." PREVIOUS="gradeitemid" NEXT="grademax"/>
-        <FIELD NAME="grademax" TYPE="number" LENGTH="10" NOTNULL="false" UNSIGNED="false" SEQUENCE="false" ENUM="false" DECIMALS="5" COMMENT="If set, this is the maximum grade that users must be below (less than) in order to display this item. Otherwise null." PREVIOUS="grademin"/>
+        <FIELD NAME="grademin" TYPE="number" LENGTH="10" NOTNULL="false" UNSIGNED="false" SEQUENCE="false" ENUM="false" DECIMALS="5" COMMENT="If set, this is the minimum grade percentage that must be reached (greater than or equal) in order for this module to appear. Otherwise null." PREVIOUS="gradeitemid" NEXT="grademax"/>
+        <FIELD NAME="grademax" TYPE="number" LENGTH="10" NOTNULL="false" UNSIGNED="false" SEQUENCE="false" ENUM="false" DECIMALS="5" COMMENT="If set, this is the maximum grade percentage that users must be below (less than) in order to display this item. Otherwise null." PREVIOUS="grademin"/>
       </FIELDS>
       <KEYS>
         <KEY NAME="primary" TYPE="primary" FIELDS="id" NEXT="coursemoduleid"/>
index 36e5cb0603df4504d02d66562aa59417d857c5b2..f8ef3ecd5c4c98756bf78651065a076e10f376e7 100644 (file)
@@ -1438,6 +1438,27 @@ function xmldb_main_upgrade($oldversion) {
         upgrade_main_savepoint($result, 2009012901);
     }
 
+    if ($result && $oldversion < 2009021800) {
+        // Converting format of grade conditions, if any exist, to percentages. 
+        $DB->execute("
+UPDATE {course_modules_availability} SET grademin=(
+    SELECT 100.0*({course_modules_availability}.grademin-gi.grademin)
+        /(gi.grademax-gi.grademin)        
+    FROM {grade_items} gi 
+    WHERE gi.id={course_modules_availability}.gradeitemid)
+WHERE gradeitemid IS NOT NULL AND grademin IS NOT NULL");
+        $DB->execute("
+UPDATE {course_modules_availability} SET grademax=(
+    SELECT 100.0*({course_modules_availability}.grademax-gi.grademin)
+        /(gi.grademax-gi.grademin)        
+    FROM {grade_items} gi 
+    WHERE gi.id={course_modules_availability}.gradeitemid)
+WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL");
+
+    /// Main savepoint reached
+        upgrade_main_savepoint($result, 2009021800);
+    }
+
     return $result;
 }
 
index ddc1c4f6804ecd2842fc66165aec07ac1d4f6118..64778cae2b5f7df7a8b2d894c8dc46dede5de401 100644 (file)
@@ -6,7 +6,7 @@
 // This is compared against the values stored in the database to determine
 // whether upgrades should be performed (see lib/db/*.php)
 
-    $version = 2009012901;  // YYYYMMDD   = date of the last version bump
+    $version = 2009021800;  // YYYYMMDD   = date of the last version bump
                             //         XX = daily increments
 
     $release = '2.0 dev (Build: 20090218)';  // Human-friendly version name