From 75564228161fca8b24f42eceeb9f1ba5c5a31c15 Mon Sep 17 00:00:00 2001 From: sam_marshall Date: Wed, 18 Feb 2009 17:50:13 +0000 Subject: [PATCH] MDL-18272, MDL-18271: Conditional availability - grade condition to use percentages not raw grades; use wording rather than >= symbol in form because (apparently) teachers are functionally innumerate. ;) --- course/moodleform_mod.php | 3 ++- lang/en_utf8/condition.php | 4 ++-- lib/conditionlib.php | 34 +++++++++++++++++++++++----------- lib/db/install.xml | 4 ++-- lib/db/upgrade.php | 21 +++++++++++++++++++++ version.php | 2 +- 6 files changed, 51 insertions(+), 17 deletions(-) diff --git a/course/moodleform_mod.php b/course/moodleform_mod.php index 2a0283e057..74ece3fc97 100644 --- a/course/moodleform_mod.php +++ b/course/moodleform_mod.php @@ -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', diff --git a/lang/en_utf8/condition.php b/lang/en_utf8/condition.php index eb5ebb9a92..4aae29d6f2 100644 --- a/lang/en_utf8/condition.php +++ b/lang/en_utf8/condition.php @@ -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 ≥'; -$string['grade_upto']='and <'; +$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'; diff --git a/lib/conditionlib.php b/lib/conditionlib.php index d14916b542..5b9b503ef9 100644 --- a/lib/conditionlib.php +++ b/lib/conditionlib.php @@ -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; diff --git a/lib/db/install.xml b/lib/db/install.xml index 17815019b1..ab65869d9e 100644 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -210,8 +210,8 @@ - - + + diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 36e5cb0603..f8ef3ecd5c 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -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; } diff --git a/version.php b/version.php index ddc1c4f680..64778cae2b 100644 --- a/version.php +++ b/version.php @@ -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 -- 2.39.5