示例#1
0
/**
 * Serves the offlinequiz files.
 *
 * @param object $course
 * @param object $cm
 * @param object $context
 * @param string $filearea
 * @param array $args
 * @param bool $forcedownload
 * @return bool false if file not found, does not return if found - justsend the file
 */
function offlinequiz_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload)
{
    global $CFG, $DB, $USER;
    require_once $CFG->dirroot . '/mod/offlinequiz/locallib.php';
    require_once $CFG->libdir . '/questionlib.php';
    if ($context->contextlevel != CONTEXT_MODULE) {
        return false;
    }
    require_login($course, false, $cm);
    if (!($offlinequiz = $DB->get_record('offlinequiz', array('id' => $cm->instance)))) {
        return false;
    }
    // The file area 'pdfs' is served by pluginfile.php.
    $fileareas = array('pdfs', 'imagefiles');
    if (!in_array($filearea, $fileareas)) {
        return false;
    }
    $fs = get_file_storage();
    $relativepath = implode('/', $args);
    $fullpath = '/' . $context->id . '/mod_offlinequiz/' . $filearea . '/' . $relativepath;
    if (!($file = $fs->get_file_by_hash(sha1($fullpath))) or $file->is_directory()) {
        return false;
    }
    // Teachers in this context are allowed to see all the files in the context.
    if (has_capability('mod/offlinequiz:viewreports', $context)) {
        if ($filearea == 'pdfs') {
            $filename = clean_filename($course->shortname) . '_' . clean_filename($offlinequiz->name) . '_' . $file->get_filename();
            send_stored_file($file, 86400, 0, $forcedownload, array('filename' => $filename));
        } else {
            send_stored_file($file, 86400, 0, $forcedownload);
        }
    } else {
        // Get the corresponding scanned pages. There might be several in case an image file is used twice.
        if (!($scannedpages = $DB->get_records('offlinequiz_scanned_pages', array('offlinequizid' => $offlinequiz->id, 'warningfilename' => $file->get_filename())))) {
            if (!($scannedpages = $DB->get_records('offlinequiz_scanned_pages', array('offlinequizid' => $offlinequiz->id, 'filename' => $file->get_filename())))) {
                print_error('scanned page not found');
                return false;
            }
        }
        // Actually, there should be only one scannedpage with that filename...
        foreach ($scannedpages as $scannedpage) {
            $sql = "SELECT *\n                      FROM {offlinequiz_results}\n                     WHERE id = :resultid\n                       AND status = 'complete'";
            if (!($result = $DB->get_record_sql($sql, array('resultid' => $scannedpage->resultid)))) {
                return false;
            }
            // Check whether the student is allowed to see scanned sheets.
            $options = offlinequiz_get_review_options($offlinequiz, $result, $context);
            if ($options->sheetfeedback == question_display_options::HIDDEN and $options->gradedsheetfeedback == question_display_options::HIDDEN) {
                return false;
            }
            // If we found a page of a complete result that belongs to the user, we can send the file.
            if ($result->userid == $USER->id) {
                send_stored_file($file, 86400, 0, $forcedownload);
                return true;
            }
        }
    }
}
}
if (!($cm = get_coursemodule_from_instance("offlinequiz", $offlinequiz->id, $course->id))) {
    print_error('nocm', 'offlinequiz', $CFG->wwwroot . '/course/view.php?id=' . $COURSE->id, $scannedpage->offlinequizid);
}
if (!($groups = $DB->get_records('offlinequiz_groups', array('offlinequizid' => $offlinequiz->id), 'number', '*', 0, $offlinequiz->numgroups))) {
    print_error('nogroups', 'offlinequiz', $CFG->wwwroot . '/course/view.php?id=' . $COURSE->id, $scannedpage->offlinequizid);
}
require_login($course->id, false, $cm);
$context = context_module::instance($cm->id);
if (!has_capability('mod/offlinequiz:viewreports', $context) and !has_capability('mod/offlinequiz:attempt', $context)) {
    print_error('noaccess', 'offlinequiz', $CFG->wwwroot . '/course/view.php?id=' . $COURSE->id, $scannedpage->offlinequizid);
}
if (!has_capability('mod/offlinequiz:viewreports', $context) and $result->userid != $USER->id) {
    print_error('noaccess', 'offlinequiz', $CFG->wwwroot . '/course/view.php?id=' . $COURSE->id, $scannedpage->offlinequizid);
}
$options = offlinequiz_get_review_options($offlinequiz, $result, $context);
if (!$options->sheetfeedback and !$options->gradedsheetfeedback) {
    print_error('noaccess', 'offlinequiz', $CFG->wwwroot . '/course/view.php?id=' . $COURSE->id, $scannedpage->offlinequizid);
}
$url = new moodle_url('/mod/offlinequiz/image.php', array('pageid' => $scannedpage->id, 'resultid' => $result->id));
$PAGE->set_url($url);
$PAGE->set_pagelayout('popup');
echo "<style>\n";
echo "body {margin:0px; font-family:Arial,Verdana,Helvetica,sans-serif;}\n";
echo ".imagebutton {width:250px; height:24px; text-align:left; margin-bottom:10px;}\n";
echo "</style>\n";
echo '<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>';
offlinequiz_load_useridentification();
$offlinequizconfig = get_config('offlinequiz');
$group = $groups[$result->offlinegroupid];
$offlinequiz->groupid = -$group->id;