Esempio n. 1
0
 public function test_validate_package()
 {
     global $CFG;
     $this->resetAfterTest(true);
     $filename = "validscorm.zip";
     $file = $this->create_stored_file_from_path($CFG->dirroot . '/mod/scorm/tests/packages/' . $filename, file_archive::OPEN);
     $errors = scorm_validate_package($file);
     $this->assertEmpty($errors);
     $filename = "validaicc.zip";
     $file = $this->create_stored_file_from_path($CFG->dirroot . '/mod/scorm/tests/packages/' . $filename, file_archive::OPEN);
     $errors = scorm_validate_package($file);
     $this->assertEmpty($errors);
     $filename = "invalid.zip";
     $file = $this->create_stored_file_from_path($CFG->dirroot . '/mod/scorm/tests/packages/' . $filename, file_archive::OPEN);
     $errors = scorm_validate_package($file);
     $this->assertArrayHasKey('packagefile', $errors);
     if (isset($errors['packagefile'])) {
         $this->assertEquals(get_string('nomanifest', 'scorm'), $errors['packagefile']);
     }
     $filename = "badscorm.zip";
     $file = $this->create_stored_file_from_path($CFG->dirroot . '/mod/scorm/tests/packages/' . $filename, file_archive::OPEN);
     $errors = scorm_validate_package($file);
     $this->assertArrayHasKey('packagefile', $errors);
     if (isset($errors['packagefile'])) {
         $this->assertEquals(get_string('badimsmanifestlocation', 'scorm'), $errors['packagefile']);
     }
 }
Esempio n. 2
0
File: lib.php Progetto: ruddj/moodle
/**
 * Handle a file that has been uploaded
 * @param object $uploadinfo details of the file / content that has been uploaded
 * @return int instance id of the newly created mod
 */
function scorm_dndupload_handle($uploadinfo) {

    $context = context_module::instance($uploadinfo->coursemodule);
    file_save_draft_area_files($uploadinfo->draftitemid, $context->id, 'mod_scorm', 'package', 0);
    $fs = get_file_storage();
    $files = $fs->get_area_files($context->id, 'mod_scorm', 'package', 0, 'sortorder, itemid, filepath, filename', false);
    $file = reset($files);

    // Validate the file, make sure it's a valid SCORM package!
    $errors = scorm_validate_package($file);
    if (!empty($errors)) {
        return false;
    }
    // Create a default scorm object to pass to scorm_add_instance()!
    $scorm = get_config('scorm');
    $scorm->course = $uploadinfo->course->id;
    $scorm->coursemodule = $uploadinfo->coursemodule;
    $scorm->cmidnumber = '';
    $scorm->name = $uploadinfo->displayname;
    $scorm->scormtype = SCORM_TYPE_LOCAL;
    $scorm->reference = $file->get_filename();
    $scorm->intro = '';
    $scorm->width = $scorm->framewidth;
    $scorm->height = $scorm->frameheight;

    return scorm_add_instance($scorm, null);
}
 public function validation($data, $files)
 {
     global $CFG, $USER;
     $errors = parent::validation($data, $files);
     $type = $data['scormtype'];
     if ($type === SCORM_TYPE_LOCAL) {
         if (empty($data['packagefile'])) {
             $errors['packagefile'] = get_string('required');
         } else {
             $draftitemid = file_get_submitted_draft_itemid('packagefile');
             file_prepare_draft_area($draftitemid, $this->context->id, 'mod_scorm', 'packagefilecheck', null, array('subdirs' => 0, 'maxfiles' => 1));
             // Get file from users draft area.
             $usercontext = context_user::instance($USER->id);
             $fs = get_file_storage();
             $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id', false);
             if (count($files) < 1) {
                 $errors['packagefile'] = get_string('required');
                 return $errors;
             }
             $file = reset($files);
             if (!$file->is_external_file() && !empty($data['updatefreq'])) {
                 // Make sure updatefreq is not set if using normal local file.
                 $errors['updatefreq'] = get_string('updatefreq_error', 'mod_scorm');
             }
             if (strtolower($file->get_filename()) == 'imsmanifest.xml') {
                 if (!$file->is_external_file()) {
                     $errors['packagefile'] = get_string('aliasonly', 'mod_scorm');
                 } else {
                     $repository = repository::get_repository_by_id($file->get_repository_id(), context_system::instance());
                     if (!$repository->supports_relative_file()) {
                         $errors['packagefile'] = get_string('repositorynotsupported', 'mod_scorm');
                     }
                 }
             } else {
                 if (strtolower(substr($file->get_filename(), -3)) == 'xml') {
                     $errors['packagefile'] = get_string('invalidmanifestname', 'mod_scorm');
                 } else {
                     // Validate this SCORM package.
                     $errors = array_merge($errors, scorm_validate_package($file));
                 }
             }
         }
     } else {
         if ($type === SCORM_TYPE_EXTERNAL) {
             $reference = $data['packageurl'];
             // Syntax check.
             if (!preg_match('/(http:\\/\\/|https:\\/\\/|www).*\\/imsmanifest.xml$/i', $reference)) {
                 $errors['packageurl'] = get_string('invalidurl', 'scorm');
             } else {
                 // Availability check.
                 $result = scorm_check_url($reference);
                 if (is_string($result)) {
                     $errors['packageurl'] = $result;
                 }
             }
         } else {
             if ($type === 'packageurl') {
                 $reference = $data['reference'];
                 // Syntax check.
                 if (!preg_match('/(http:\\/\\/|https:\\/\\/|www).*(\\.zip|\\.pif)$/i', $reference)) {
                     $errors['packageurl'] = get_string('invalidurl', 'scorm');
                 } else {
                     // Availability check.
                     $result = scorm_check_url($reference);
                     if (is_string($result)) {
                         $errors['packageurl'] = $result;
                     }
                 }
             } else {
                 if ($type === SCORM_TYPE_AICCURL) {
                     $reference = $data['packageurl'];
                     // Syntax check.
                     if (!preg_match('/(http:\\/\\/|https:\\/\\/|www).*/', $reference)) {
                         $errors['packageurl'] = get_string('invalidurl', 'scorm');
                     } else {
                         // Availability check.
                         $result = scorm_check_url($reference);
                         if (is_string($result)) {
                             $errors['packageurl'] = $result;
                         }
                     }
                 }
             }
         }
     }
     return $errors;
 }