From 71ba4dce3cb952902471ea7953217331d9b81ee2 Mon Sep 17 00:00:00 2001 From: michaelpenne Date: Thu, 9 Jun 2005 21:24:17 +0000 Subject: [PATCH] Discovered a bug with lesson grades. While calculating the grade, intval was used to truncate the decimals from the grade. intavl is now replaced with round. round will now round the grade to 5 decimal places. The database only holds int(3). So, this has been changed to float to hold decimal numbers. We have found this to be much more accurate when converting the stored percentage in the database back to a point value (before this fix, users were getting numbers like 46.5 points instead of their original 47 points due to an inaccurate percent value). Files changed for this fix include: locallib.php report.php version.php view.php db/mysql.php db/mysql.sql db/postgres7.php db/postgres7.sql --- mod/lesson/db/mysql.php | 4 +++- mod/lesson/db/mysql.sql | 2 +- mod/lesson/db/postgres7.php | 4 ++++ mod/lesson/db/postgres7.sql | 2 +- mod/lesson/locallib.php | 4 ++-- mod/lesson/report.php | 4 ++-- mod/lesson/version.php | 2 +- mod/lesson/view.php | 4 ++-- 8 files changed, 16 insertions(+), 10 deletions(-) diff --git a/mod/lesson/db/mysql.php b/mod/lesson/db/mysql.php index 2c102533dc245..adc25a43b53f4 100644 --- a/mod/lesson/db/mysql.php +++ b/mod/lesson/db/mysql.php @@ -164,7 +164,9 @@ function lesson_upgrade($oldversion) { modify_database('','ALTER TABLE prefix_lesson_pages ADD INDEX lessonid (lessonid);'); } - + if ($oldversion < 2005060900) { + table_column('lesson_grades', 'grade', 'grade', 'float', '', 'unsigned', '0', 'not null'); + } return true; } diff --git a/mod/lesson/db/mysql.sql b/mod/lesson/db/mysql.sql index 866079db9ade4..bda22481425a9 100644 --- a/mod/lesson/db/mysql.sql +++ b/mod/lesson/db/mysql.sql @@ -98,7 +98,7 @@ CREATE TABLE `prefix_lesson_grades` ( `id` int(10) unsigned NOT NULL auto_increment, `lessonid` int(10) unsigned NOT NULL default '0', `userid` int(10) unsigned NOT NULL default '0', - `grade` int(3) unsigned NOT NULL default '0', + `grade` float unsigned NOT NULL default '0', `late` int(3) unsigned NOT NULL default '0', `completed` int(10) unsigned NOT NULL default '0', PRIMARY KEY (`id`), diff --git a/mod/lesson/db/postgres7.php b/mod/lesson/db/postgres7.php index 986cc88b076e2..a3a17600542d0 100644 --- a/mod/lesson/db/postgres7.php +++ b/mod/lesson/db/postgres7.php @@ -211,6 +211,10 @@ function lesson_upgrade($oldversion) { modify_database('','CREATE INDEX prefix_lesson_grades_userid_idx ON prefix_lesson_grades (userid);'); modify_database('','CREATE INDEX prefix_lesson_pages_lessonid_idx ON prefix_lesson_pages (lessonid);'); } + + if ($oldversion < 2005060900) { + table_column('lesson_grades', 'grade', 'grade', 'real', '', 'unsigned', '0', 'not null'); + } return true; } diff --git a/mod/lesson/db/postgres7.sql b/mod/lesson/db/postgres7.sql index c7d285aa525f4..e8c9bb86f6e21 100644 --- a/mod/lesson/db/postgres7.sql +++ b/mod/lesson/db/postgres7.sql @@ -87,7 +87,7 @@ CREATE TABLE prefix_lesson_grades ( id SERIAL8 PRIMARY KEY, lessonid INT8 NOT NULL default '0', userid INT8 NOT NULL default '0', - grade INT NOT NULL default '0', + grade real NOT NULL default '0', late INT NOT NULL default '0', completed INT8 NOT NULL default '0' ) ; diff --git a/mod/lesson/locallib.php b/mod/lesson/locallib.php index a51507a25b590..c03551e2fed54 100644 --- a/mod/lesson/locallib.php +++ b/mod/lesson/locallib.php @@ -1028,7 +1028,7 @@ function lesson_calculate_ongoing_score($lesson, $userid, $retries, $return=fals } $nviewed = count($temp); // this counts number of Questions the user viewed if ($nviewed != 0) { - $thegrade = intval(100 * $ncorrect / $nviewed); + $thegrade = round(100 * $ncorrect / $nviewed, 5); } else { $thegrade = 0; } @@ -1093,7 +1093,7 @@ function lesson_calculate_ongoing_score($lesson, $userid, $retries, $return=fals } $bestscore = array_sum($bestscores); - $thegrade = intval(100 * $score / $bestscore); + $thegrade = round(100 * $score / $bestscore, 5); } diff --git a/mod/lesson/report.php b/mod/lesson/report.php index 530a2ed4a6155..815888f32bb0f 100644 --- a/mod/lesson/report.php +++ b/mod/lesson/report.php @@ -101,7 +101,7 @@ // see if n is = to the retry if ($n == $attempt->retry) { // get grade info - $usergrade = $grade->grade; + $usergrade = round($grade->grade, 2); // round it here so we only have to do it once break; } $n++; // if not equal, then increment n @@ -802,7 +802,7 @@ $grade = -1; } else { $grade = current($grades); - $grade = $grade->grade; + $grade = round($grade->grade, 2); } if (!$times = get_records_select("lesson_timer", "lessonid = $lesson->id and userid = $userid", "starttime", "*", $try, 1)) { $timetotake = -1; diff --git a/mod/lesson/version.php b/mod/lesson/version.php index 48a1296971b03..46b1e85284db8 100644 --- a/mod/lesson/version.php +++ b/mod/lesson/version.php @@ -5,7 +5,7 @@ /// This fragment is called by moodle_needs_upgrading() and /admin/index.php ///////////////////////////////////////////////////////////////////////////////// -$module->version = 2005021600; // The current module version (Date: YYYYMMDDXX) +$module->version = 2005060900; // The current module version (Date: YYYYMMDDXX) $module->requires = 2005021600; // Requires this Moodle version $module->cron = 0; // Period for cron to check this module (secs) diff --git a/mod/lesson/view.php b/mod/lesson/view.php index 9c77a036f8e8f..65fdccd61f5d7 100644 --- a/mod/lesson/view.php +++ b/mod/lesson/view.php @@ -935,7 +935,7 @@ } echo "

".get_string("numberofcorrectanswers", "lesson", $ncorrect). "

\n"; - $thegrade = intval(100 * $ncorrect / $nviewed); + $thegrade = round(100 * $ncorrect / $nviewed, 5); echo "

".get_string("gradeis", "lesson", number_format($thegrade * $lesson->grade / 100, 1)). " (".get_string("outof", "lesson", $lesson->grade).")

\n"; @@ -991,7 +991,7 @@ $bestscore = array_sum($bestscores); } - $thegrade = intval(100 * $score / $bestscore); + $thegrade = round(100 * $score / $bestscore, 5); $a = new stdClass; if ($essayquestions > 0) { $a->score = $score;