function mod_stopwatch_update_grades($cm, $stopwatch, $durationarray, $gradearray)
{
    global $DB, $CFG;
    require_once $CFG->libdir . '/gradelib.php';
    $currentgrades = mod_stopwatch_get_all_users($cm, $stopwatch);
    $newgrades = array();
    foreach ($currentgrades as $userid => $record) {
        if ($record->id) {
            $params = array('id' => $record->id);
        } else {
            $params = array('userid' => $userid, 'courseid' => $cm->course, 'stopwatchid' => $stopwatch->id);
        }
        $now = time();
        $updateobj = array();
        if (array_key_exists($userid, $durationarray)) {
            if (empty($durationarray[$userid])) {
                if (!empty($record->duration)) {
                    $updateobj['duration'] = 0;
                }
            } else {
                $duration = mod_stopwatch_string_to_duration($durationarray[$userid]);
                if ($record->duration != $duration) {
                    $updateobj['duration'] = $duration;
                }
            }
        }
        if ($stopwatch->grade && array_key_exists($userid, $gradearray)) {
            $grade = empty($gradearray[$userid]) ? null : (double) $gradearray[$userid];
            $existinggrade = !strlen($record->grade) ? null : (double) $record->grade;
            if ($existinggrade !== $grade) {
                $updateobj['grade'] = $grade;
                $updateobj['timegraded'] = $now;
                $newgrades[$userid] = array('userid' => $userid, 'rawgrade' => $grade);
            }
        }
        if (empty($updateobj)) {
            continue;
        }
        $updateobj += $params;
        $updateobj['timemodified'] = $now;
        if (!empty($updateobj['id'])) {
            $DB->update_record('stopwatch_user', $updateobj);
        } else {
            $updateobj['timecreated'] = $now;
            $DB->insert_record('stopwatch_user', $updateobj);
        }
    }
    if ($newgrades) {
        // Attempt to update the grade item if relevant
        $grademodule = fullclone($stopwatch);
        $grademodule->cmidnumber = $cm->idnumber;
        $grademodule->modname = $cm->modname;
        grade_update_mod_grades($grademodule);
    }
}
    public function display_grades(cm_info $cm, $stopwatch)
    {
        $sesskey = sesskey();
        $action = new moodle_url('/mod/stopwatch/view.php');
        $editlink = html_writer::link(new moodle_url('/course/modedit.php', array('update' => $cm->id)), 'Edit');
        $str = '';
        if ($stopwatch->grade > 0) {
            $str .= "<p>Maximum grade for the module is <b>{$stopwatch->grade}</b>. {$editlink}</p>";
        } else {
            if ($stopwatch->grade < 0) {
                $str .= "<p>Scale grading is not yet implemented! {$editlink}</p>";
            } else {
                $str .= "<p>No grading is enabled for this module. {$editlink}</p>";
            }
        }
        $str = $str . <<<EOD
<form id="stopwatchgradeform" method="POST" action="{$action}">
    <input type="hidden" name="sesskey" value="{$sesskey}" />
    <input type="hidden" name="id" value="{$cm->id}" />
    <input type="hidden" name="cmd" value="updategrades" />
EOD;
        $table = new html_table();
        $table->head = array('User', 'Duration', 'Completed on');
        if ($stopwatch->grade) {
            $table->head[] = 'Grade';
            $table->head[] = 'Graded on';
        }
        $records = mod_stopwatch_get_all_users($cm, $stopwatch);
        foreach ($records as $record) {
            $duration = $record->duration ? mod_stopwatch_duration_to_string($record->duration) : '';
            $durationinput = html_writer::empty_tag('input', array('type' => 'text', 'name' => "duration[{$record->userid}]", 'value' => $duration));
            $data = (array) array(fullname($record), $durationinput, $record->timecreated ? userdate($record->timecreated, '%d/%m/%y <b>%H:%M</b>') : '');
            if ($stopwatch->grade > 0) {
                $gradeinput = html_writer::empty_tag('input', array('type' => 'text', 'name' => "grade[{$record->userid}]", 'value' => strlen($record->grade) ? (double) $record->grade : ''));
                $data[] = $gradeinput;
            } else {
                if ($stopwatch->grade < 0) {
                    $data[] = 'Not implemented, sorry....';
                }
            }
            if ($stopwatch->grade) {
                $data[] = $record->timegraded ? userdate($record->timegraded, '%d/%m/%y <b>%H:%M</b>') : '';
            }
            $table->data[] = $data;
        }
        $str .= html_writer::table($table);
        $str .= '<input id="grade" type="submit" value="Grade" />';
        $str .= '</form >';
        return $str;
    }