Exemplo n.º 1
0
     $unique = str_replace('.', '', microtime(true) . rand(0, 100000));
     $dirname = "{$CFG->tempdir}/offlinequiz/import/{$unique}";
     check_dir_exists($dirname, true, true);
     $warningpathname = $dirname . '/' . $filerecord['filename'];
     imagepng($scanner->image, $warningpathname);
     $newfile = $scanner->save_image($filerecord, $warningpathname);
     $scannedpage->warningfilename = $newfile->get_filename();
     $DB->set_field('offlinequiz_scanned_pages', 'warningfilename', $newfile->get_filename(), array('id' => $scannedpage->id));
     unlink($warningpathname);
     remove_dir($dirname);
 }
 if (!$unknown) {
     $scannedpage = offlinequiz_submit_scanned_page($offlinequiz, $scannedpage, $choicesdata, $startindex, $endindex);
     if ($scannedpage->status == 'submitted') {
         $result = $DB->get_record('offlinequiz_results', array('id' => $scannedpage->resultid));
         if (offlinequiz_check_result_completed($offlinequiz, $group, $result)) {
             echo $OUTPUT->notification(get_string('userimported', 'offlinequiz', fullname($user) . " (" . $user->{$offlinequizconfig->ID_field} . ")"), 'notifysuccess');
         } else {
             echo $OUTPUT->notification(get_string('userpageimported', 'offlinequiz', fullname($user) . " (" . $user->{$offlinequizconfig->ID_field} . ")"), 'notifysuccess');
         }
         echo '<html>';
         echo '<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>';
         if ($overwrite) {
             echo "<input type=\"button\" value=\"" . get_string('closewindow') . "\" onClick=\"window.opener.location.replace('" . $CFG->wwwroot . '/mod/offlinequiz/review.php?q=' . $offlinequiz->id . '&resultid=' . $scannedpage->resultid . "'); window.close(); return false;\">";
         } else {
             echo "<input type=\"button\" value=\"" . get_string('closewindow') . "\" onClick=\"window.opener.location.reload(1); self.close(); return false;\">";
         }
         echo '<html>';
         return;
     }
 } else {
Exemplo n.º 2
0
/**
 * Stores the choices made on a scanned page in the table offlinequiz_choices. If there are no insecure markings
 * the page is also submitted, i.e. the answers are processed by the question usage by activiy (quba).
 *
 * @param unknown_type $offlinequiz
 * @param unknown_type $scanner
 * @param unknown_type $scannedpage
 * @param unknown_type $teacherid
 * @param unknown_type $coursecontext
 */
function offlinequiz_process_scanned_page($offlinequiz, offlinequiz_page_scanner $scanner, $scannedpage, $teacherid, $questionsperpage, $coursecontext, $submit = false)
{
    global $DB;
    $offlinequizconfig = get_config('offlinequiz');
    if (property_exists($scannedpage, 'resultid') && $scannedpage->resultid) {
        $group = $DB->get_record('offlinequiz_groups', array('offlinequizid' => $offlinequiz->id, 'number' => $scannedpage->groupnumber));
        $user = $DB->get_record('user', array($offlinequizconfig->ID_field => $scannedpage->userkey));
        $result = $DB->get_record('offlinequiz_results', array('id' => $scannedpage->resultid));
        $quba = offlinequiz_load_questions_usage_by_activity($result->usageid);
        // Retrieve the answers. This initialises the answer hotspots.
        $answers = $scanner->get_answers();
        if (empty($answers)) {
            $scannedpage->status = 'error';
            $scannedpage->error = 'notadjusted';
            return $scannedpage;
        }
        $slots = $quba->get_slots();
        // We start at the top of the page (e.g. 0, 96, etc).
        $startindex = ($scannedpage->pagenumber - 1) * $questionsperpage;
        // We end on the bottom of the page or when the questions are gone (e.g., 95, 105).
        $endindex = min($scannedpage->pagenumber * $questionsperpage, count($slots));
        $answerindex = 0;
        $insecuremarkings = false;
        $choicesdata = array();
        for ($slotindex = $startindex; $slotindex < $endindex; $slotindex++) {
            $slot = $slots[$slotindex];
            $slotquestion = $quba->get_question($slot);
            $attempt = $quba->get_question_attempt($slot);
            $order = $slotquestion->get_order($attempt);
            // Order of the answers.
            // Note: The array length of a row is $maxanswers, so probably bigger than the number of answers in the slot.
            $row = $answers[$answerindex++];
            $count = 0;
            $response = array();
            if (!isset($choicesdata[$slot]) || !is_array($choicesdata[$slot])) {
                $choicesdata[$slot] = array();
            }
            // Go through all answers of the slot question.
            foreach ($order as $key => $notused) {
                // Create the data structure for the offlinequiz_choices table.
                $choice = new stdClass();
                $choice->scannedpageid = $scannedpage->id;
                $choice->slotnumber = $slot;
                $choice->choicenumber = $key;
                // Check what the scanner recognised.
                if ($row[$key] == 'marked') {
                    $choice->value = 1;
                } else {
                    if ($row[$key] == 'empty') {
                        $choice->value = 0;
                    } else {
                        $choice->value = -1;
                        $insecuremarkings = true;
                    }
                }
                // We really want to save every single cross  in the database.
                $choice->id = $DB->insert_record('offlinequiz_choices', $choice);
                $choicesdata[$slot][$key] = $choice;
            }
        }
        // End for (slot...
        if (!$insecuremarkings and $submit) {
            $scannedpage = offlinequiz_submit_scanned_page($offlinequiz, $scannedpage, $choicesdata, $startindex, $endindex);
            if ($scannedpage->status == 'submitted') {
                offlinequiz_check_result_completed($offlinequiz, $group, $result);
            }
        }
        // If insecure markings have been found, set the status appropriately.
        if (($scannedpage->status == 'ok' || $scannedpage->status == 'suspended') && $insecuremarkings) {
            $scannedpage->status = 'error';
            $scannedpage->error = 'insecuremarkings';
            $scannedpage->time = time();
            $DB->update_record('offlinequiz_scanned_pages', $scannedpage);
        }
    }
    // End if status ok.
    return $scannedpage;
}