Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

How to fix gradebook issues: Difference between revisions

From MoodleDocs
m (Text replacement - "</code>" to "</syntaxhighlight>")
m (Text replacement - "<code php>" to "<syntaxhighlight lang="php">")
Line 12: Line 12:


example:  
example:  
<code php>
<syntaxhighlight lang="php">
$gradebookcalculationsfreeze = get_config('core', 'gradebook_calculations_freeze_' . $courseid);
$gradebookcalculationsfreeze = get_config('core', 'gradebook_calculations_freeze_' . $courseid);
// Stick with the original code if the grade book is frozen.
// Stick with the original code if the grade book is frozen.
Line 28: Line 28:


example (lib/db/upgrade.php):
example (lib/db/upgrade.php):
<code php>
<syntaxhighlight lang="php">
     if ($oldversion < 2015062500.01) {
     if ($oldversion < 2015062500.01) {
         // MDL-48239. Changed calculated grade items so that the maximum and minimum grade can be set.
         // MDL-48239. Changed calculated grade items so that the maximum and minimum grade can be set.
Line 54: Line 54:


example (lib/db/install.php:: xmldb_main_install() line 135):  
example (lib/db/install.php:: xmldb_main_install() line 135):  
<code php>
<syntaxhighlight lang="php">
     'upgrade_calculatedgradeitemsignored' => 1, // New installs should not run this upgrade step.
     'upgrade_calculatedgradeitemsignored' => 1, // New installs should not run this upgrade step.
</syntaxhighlight>
</syntaxhighlight>
Line 63: Line 63:


example (backup/moodle2/restore_stepslib.php):
example (backup/moodle2/restore_stepslib.php):
<code php>
<syntaxhighlight lang="php">
// Calculated grade items need recalculating for backups made between 2.8 release (20141110) and the fix release (20150627).
// Calculated grade items need recalculating for backups made between 2.8 release (20141110) and the fix release (20150627).
if (!$gradebookcalculationsfreeze && $backupbuild >= 20141110 && $backupbuild < 20150627) {
if (!$gradebookcalculationsfreeze && $backupbuild >= 20141110 && $backupbuild < 20150627) {

Revision as of 13:35, 14 July 2021

How to fix Grade book issues

There has been a policy decision that any fixes to the grade book must not result in any changes to grades. Some fixes may result in grades being changed and in these situations we need to freeze the grade book and let the teacher / admin / higher power agree to applying the change.

For future fixes to the grade book we will be putting revision numbers and nested code into the grade book libraries.

Fixing the issue

Choose a freeze number that is greater than the current build number but will be less than the next build number.

When making changes wrap your code in an if statement.

example:

$gradebookcalculationsfreeze = get_config('core', 'gradebook_calculations_freeze_' . $courseid);
// Stick with the original code if the grade book is frozen.
if ($gradebookcalculationsfreeze && (int)$gradebookcalculationsfreeze <= {your code freeze number}) {
    {original code goes here}
} else {
    {new fixed code goes here}
}

Enter in details about your fix at https://docs.moodle.org/29/en/Gradebook_calculation_changes

Upgrade code

Code will be required to determine if the site being upgraded is affected. If so then it should be flagged to be frozen.

example (lib/db/upgrade.php):

    if ($oldversion < 2015062500.01) {
        // MDL-48239. Changed calculated grade items so that the maximum and minimum grade can be set.

        // If the changes are accepted and a regrade is done on the gradebook then some grades may change significantly.
        // This is here to freeze the gradebook in affected courses.

        // This script is included in each major version upgrade process so make sure we don't run it twice.
        if (empty($CFG->upgrade_calculatedgradeitemsignored)) {
            upgrade_calculated_grade_items();

            // To skip running the same script on the upgrade to the next major release.
            set_config('upgrade_calculatedgradeitemsignored', 1);
        }

        // Main savepoint reached.
        upgrade_main_savepoint(true, 2015062500.01);
    }

The code for doing this check should be located in lib/db/upgradelib.php so that it can also be run when a course is being restored.

New installations

Make sure to not run the upgrade step with the new installation

example (lib/db/install.php:: xmldb_main_install() line 135):

    'upgrade_calculatedgradeitemsignored' => 1, // New installs should not run this upgrade step.

Restoration code

Code should be generated so that a regrading of the gradebook will fix the problem. Any solution that requires direct alteration of the grades in the database will result in an extremely complex set of restore code.

example (backup/moodle2/restore_stepslib.php):

// Calculated grade items need recalculating for backups made between 2.8 release (20141110) and the fix release (20150627).
if (!$gradebookcalculationsfreeze && $backupbuild >= 20141110 && $backupbuild < 20150627) {
    require_once($CFG->libdir . '/db/upgradelib.php');
    upgrade_calculated_grade_items($this->get_courseid());
}

Tests

Create a behat test to make sure that when the gradebook is frozen that grades are not changed. Create a behat test to make sure that when the gradebook is not frozen that the issue is solved.

Create unit tests to check functions in the upgradelib.php file.