$lrx = $lowerright->x;
     $lry = $lowerright->y;
     $scannedpage->status = 'ok';
     $scannedpage->error = '';
     $scannedpage->listnumber = 0;
     $offlinequizconfig->papergray = $offlinequiz->papergray;
     if ($newfile = $scanner->rotate_180()) {
         $scannedpage->filename = $newfile->get_filename();
         $DB->update_record('offlinequiz_scanned_p_pages', $scannedpage);
         // Create a completely new scanner.
         $scanner = new offlinequiz_participants_scanner($offlinequiz, $context->id, 0, 0);
         // Load the stored picture file.
         $sheetloaded = $scanner->load_stored_image($scannedpage->filename, array($upperleft, $upperright, $lowerleft, $lowerright));
         // The following calibrates the scanner.
         $scanner->get_list();
         $participants = $scanner->get_participants();
     }
 } else {
     if ($action == 'setpage') {
         if (!confirm_sesskey()) {
             print_error('invalidsesskey');
             echo "<input class=\"imagebutton\" type=\"submit\" value=\"" . get_string('cancel') . "\" name=\"submitbutton4\"\nonClick=\"self.close(); return false;\"><br />";
             die;
         }
         $listid = required_param('listid', PARAM_INT);
         if (!$listid) {
             $listnumber = $scannedpage->listnumber;
         } else {
             $list = $DB->get_record('offlinequiz_p_lists', array('id' => $listid));
             $scannedpage->listnumber = intval($list->number);
             $scannedpage->status = 'ok';
/**
 * Processes the markings on a scanned list of paritipants page
 *
 * @param unknown_type $offlinequiz
 * @param offlinequiz_participants_scanner $scanner
 * @param unknown_type $scannedpage
 * @param unknown_type $teacherid
 * @param unknown_type $coursecontext
 * @return unknown
 */
function offlinequiz_process_scanned_participants_page($offlinequiz, offlinequiz_participants_scanner $scanner, $scannedpage, $teacherid, $coursecontext)
{
    global $DB;
    // Check the participants entries.
    $participants = $scanner->get_participants();
    if (!property_exists($scannedpage, 'participants') || empty($scannedpage->participants)) {
        $scannedpage->participants = $participants;
    }
    $insecuremarkings = false;
    if (!empty($participants)) {
        foreach ($participants as $participant) {
            $choice = new StdClass();
            $choice->scannedppageid = $scannedpage->id;
            $choice->userid = $participant->userid;
            if ($participant->value == 'unknown' || $participant->userid == 0) {
                $choice->value = -1;
                $insecuremarkings = true;
            } else {
                if ($participant->value == 'marked') {
                    $choice->value = 1;
                } else {
                    $choice->value = 0;
                }
            }
            if (is_string($participant->userid) && ($intuserid = intval($participant->userid))) {
                $participant->userid = $intuserid;
            }
            if ($participant->userid != false) {
                $choice->userid = $participant->userid;
            } else {
                $choice->userid = 0;
            }
            // We really want to save every single choice in the database.
            if ($choice->userid) {
                if (!($oldchoice = $DB->get_record('offlinequiz_p_choices', array('scannedppageid' => $scannedpage->id, 'userid' => $choice->userid)))) {
                    $choice->id = $DB->insert_record('offlinequiz_p_choices', $choice);
                } else {
                    $DB->set_field('offlinequiz_p_choices', 'value', $choice->value, array('id' => $oldchoice->id));
                }
            }
        }
    }
    if ($scannedpage->status == 'ok' && $insecuremarkings) {
        $scannedpage->status = 'error';
        $scannedpage->error = 'insecuremarkings';
        $scannedpage->time = time();
        $DB->update_record('offlinequiz_scanned_p_pages', $scannedpage);
    }
    // Check if all users are in the offlinequiz_p_list.
    if ($scannedpage->status == 'ok') {
        $list = $DB->get_record('offlinequiz_p_lists', array('offlinequizid' => $offlinequiz->id, 'number' => $scannedpage->listnumber));
        $userdata = $DB->get_records('offlinequiz_participants', array('listid' => $list->id));
        // Index the user data by userid.
        $users = array();
        foreach ($userdata as $user) {
            $users[$user->userid] = $user->userid;
        }
        foreach ($participants as $participant) {
            if ($participant->userid > 0 && empty($users[$participant->userid])) {
                $scannedpage->status = 'error';
                $scannedpage->error = 'usernotinlist';
                $DB->update_record('offlinequiz_scanned_p_pages', $scannedpage);
            }
        }
    }
    return $scannedpage;
}