From 27b87c10bfaa55d8192f5bb58baf259cabbf0dcf Mon Sep 17 00:00:00 2001 From: Dan Marsden Date: Fri, 23 Aug 2013 10:17:22 +1200 Subject: [PATCH] MDL-28579 SCORM: use filemanager instead of old file picker --- mod/scorm/lang/en/scorm.php | 1 + mod/scorm/lib.php | 59 ++++++++++++++++++++++--------------- mod/scorm/locallib.php | 3 ++ mod/scorm/mod_form.php | 49 ++++++++++++++++++------------ 4 files changed, 69 insertions(+), 43 deletions(-) diff --git a/mod/scorm/lang/en/scorm.php b/mod/scorm/lang/en/scorm.php index 9d370e5161f..240d91ad51f 100644 --- a/mod/scorm/lang/en/scorm.php +++ b/mod/scorm/lang/en/scorm.php @@ -330,6 +330,7 @@ $string['typelocal'] = 'Uploaded package'; $string['typelocalsync'] = 'Downloaded package'; $string['unziperror'] = 'An error occurs during package unzip'; $string['updatefreq'] = 'Auto-update frequency'; +$string['updatefreq_error'] = 'Auto-update frequency can only be set when the package file is hosted externally'; $string['updatefreq_help'] = 'This allows the external package to be automatically downloaded and updated'; $string['updatefreqdesc'] = 'This preference sets the default auto-update frequency of an activity'; $string['validateascorm'] = 'Validate a package'; diff --git a/mod/scorm/lib.php b/mod/scorm/lib.php index 02da6c9ee2b..8f487a26b0c 100644 --- a/mod/scorm/lib.php +++ b/mod/scorm/lib.php @@ -113,20 +113,24 @@ function scorm_add_instance($scorm, $mform=null) { $id = $DB->insert_record('scorm', $scorm); - /// update course module record - from now on this instance properly exists and all function may be used + // Update course module record - from now on this instance properly exists and all function may be used. $DB->set_field('course_modules', 'instance', $id, array('id'=>$cmid)); - /// reload scorm instance + // Reload scorm instance. $record = $DB->get_record('scorm', array('id'=>$id)); - /// store the package and verify + // Store the package and verify. if ($record->scormtype === SCORM_TYPE_LOCAL) { - if ($mform) { - $filename = $mform->get_new_filename('packagefile'); + if ($data = $mform->get_data()) { + $fs = get_file_storage(); + $fs->delete_area_files($context->id, 'mod_scorm', 'package'); + file_save_draft_area_files($data->packagefile, $context->id, 'mod_scorm', 'package', + 0, array('subdirs' => 0, 'maxfiles' => 1)); + // Get filename of zip that was uploaded. + $files = $fs->get_area_files($context->id, 'mod_scorm', 'package', 0, '', false); + $file = reset($files); + $filename = $file->get_filename(); if ($filename !== false) { - $fs = get_file_storage(); - $fs->delete_area_files($context->id, 'mod_scorm', 'package'); - $mform->save_stored_file('packagefile', $context->id, 'mod_scorm', 'package', 0, '/', $filename); $record->reference = $filename; } } @@ -142,10 +146,10 @@ function scorm_add_instance($scorm, $mform=null) { return false; } - // save reference + // Save reference. $DB->update_record('scorm', $record); - /// extra fields required in grade related functions + // Extra fields required in grade related functions. $record->course = $courseid; $record->cmidnumber = $cmidnumber; $record->cmid = $cmid; @@ -193,13 +197,17 @@ function scorm_update_instance($scorm, $mform=null) { $context = context_module::instance($cmid); if ($scorm->scormtype === SCORM_TYPE_LOCAL) { - if ($mform) { - $filename = $mform->get_new_filename('packagefile'); + if ($data = $mform->get_data()) { + $fs = get_file_storage(); + $fs->delete_area_files($context->id, 'mod_scorm', 'package'); + file_save_draft_area_files($data->packagefile, $context->id, 'mod_scorm', 'package', + 0, array('subdirs' => 0, 'maxfiles' => 1)); + // Get filename of zip that was uploaded. + $files = $fs->get_area_files($context->id, 'mod_scorm', 'package', 0, '', false); + $file = reset($files); + $filename = $file->get_filename(); if ($filename !== false) { $scorm->reference = $filename; - $fs = get_file_storage(); - $fs->delete_area_files($context->id, 'mod_scorm', 'package'); - $mform->save_stored_file('packagefile', $context->id, 'mod_scorm', 'package', 0, '/', $filename); } } @@ -511,9 +519,9 @@ function scorm_cron () { require_once($CFG->dirroot.'/mod/scorm/locallib.php'); $sitetimezone = $CFG->timezone; - /// Now see if there are any scorm updates to be done + // Now see if there are any scorm updates to be done. - if (!isset($CFG->scorm_updatetimelast)) { // To catch the first time + if (!isset($CFG->scorm_updatetimelast)) { // To catch the first time. set_config('scorm_updatetimelast', 0); } @@ -524,17 +532,17 @@ function scorm_cron () { set_config('scorm_updatetimelast', $timenow); - mtrace('Updating scorm packages which require daily update');//We are updating + mtrace('Updating scorm packages which require daily update');// We are updating. - $scormsupdate = $DB->get_records_select('scorm', 'updatefreq = ? AND scormtype <> ?', array(SCORM_UPDATE_EVERYDAY, SCORM_TYPE_LOCAL)); + $scormsupdate = $DB->get_records('scorm', array('updatefreq' => SCORM_UPDATE_EVERYDAY)); foreach ($scormsupdate as $scormupdate) { scorm_parse($scormupdate, true); } - //now clear out AICC session table with old session data - $cfg_scorm = get_config('scorm'); - if (!empty($cfg_scorm->allowaicchacp)) { - $expiretime = time() - ($cfg_scorm->aicchacpkeepsessiondata*24*60*60); + // Now clear out AICC session table with old session data. + $cfgscorm = get_config('scorm'); + if (!empty($cfgscorm->allowaicchacp)) { + $expiretime = time() - ($cfgscorm->aicchacpkeepsessiondata*24*60*60); $DB->delete_records_select('scorm_aicc_session', 'timemodified < ?', array($expiretime)); } } @@ -1331,6 +1339,9 @@ function scorm_set_completion($scorm, $userid, $completionstate = COMPLETION_COM function scorm_validate_package($file) { $packer = get_file_packer('application/zip'); $errors = array(); + if ($file->is_external_file()) { // Get zip file so we can check it is correct. + $file->import_external_file_contents(); + } $filelist = $file->list_files($packer); if (!is_array($filelist)) { @@ -1358,4 +1369,4 @@ function scorm_validate_package($file) { } } return $errors; -} \ No newline at end of file +} diff --git a/mod/scorm/locallib.php b/mod/scorm/locallib.php index 561da6f74a3..7c8c43ae19a 100644 --- a/mod/scorm/locallib.php +++ b/mod/scorm/locallib.php @@ -204,6 +204,9 @@ function scorm_parse($scorm, $full) { if ($scorm->scormtype === SCORM_TYPE_LOCAL) { if ($packagefile = $fs->get_file($context->id, 'mod_scorm', 'package', 0, '/', $scorm->reference)) { + if ($packagefile->is_external_file()) { // Get zip file so we can check it is correct. + $packagefile->import_external_file_contents(); + } $newhash = $packagefile->get_contenthash(); } else { $newhash = null; diff --git a/mod/scorm/mod_form.php b/mod/scorm/mod_form.php index 9a405a62fac..99d9d875f90 100644 --- a/mod/scorm/mod_form.php +++ b/mod/scorm/mod_form.php @@ -81,20 +81,20 @@ class mod_scorm_mod_form extends moodleform_mod { $mform->setType('scormtype', PARAM_ALPHA); } - if (count($scormtypes) > 1) { - // Update packages timing. - $mform->addElement('select', 'updatefreq', get_string('updatefreq', 'scorm'), scorm_get_updatefreq_array()); - $mform->setType('updatefreq', PARAM_INT); - $mform->setDefault('updatefreq', $cfgscorm->updatefreq); - $mform->addHelpButton('updatefreq', 'updatefreq', 'scorm'); - $mform->disabledIf('updatefreq', 'scormtype', 'eq', SCORM_TYPE_LOCAL); - } else { - $mform->addElement('hidden', 'updatefreq', 0); - $mform->setType('updatefreq', PARAM_INT); - } + // Update packages timing. + $mform->addElement('select', 'updatefreq', get_string('updatefreq', 'scorm'), scorm_get_updatefreq_array()); + $mform->setType('updatefreq', PARAM_INT); + $mform->setDefault('updatefreq', $cfgscorm->updatefreq); + $mform->addHelpButton('updatefreq', 'updatefreq', 'scorm'); // New local package upload. - $mform->addElement('filepicker', 'packagefile', get_string('package', 'scorm')); + $filemanageroptions = array(); + $filemanageroptions['accepted_types'] = array('.zip'); + $filemanageroptions['maxbytes'] = 0; + $filemanageroptions['maxfiles'] = 1; + $filemanageroptions['subdirs'] = 0; + + $mform->addElement('filemanager', 'packagefile', get_string('package', 'scorm'), null, $filemanageroptions); $mform->addHelpButton('packagefile', 'package', 'scorm'); $mform->disabledIf('packagefile', 'scormtype', 'noteq', SCORM_TYPE_LOCAL); @@ -280,7 +280,8 @@ class mod_scorm_mod_form extends moodleform_mod { $coursescorm = current($scorms); $draftitemid = file_get_submitted_draft_itemid('packagefile'); - file_prepare_draft_area($draftitemid, $this->context->id, 'mod_scorm', 'package', 0); + file_prepare_draft_area($draftitemid, $this->context->id, 'mod_scorm', 'package', 0, + array('subdirs' => 0, 'maxfiles' => 1)); $defaultvalues['packagefile'] = $draftitemid; if (($COURSE->format == 'singleactivity') && ((count($scorms) == 0) || ($defaultvalues['instance'] == $coursescorm->id))) { @@ -323,25 +324,35 @@ class mod_scorm_mod_form extends moodleform_mod { } public function validation($data, $files) { - global $CFG; + global $CFG, $USER; $errors = parent::validation($data, $files); $type = $data['scormtype']; if ($type === SCORM_TYPE_LOCAL) { - if (!empty($data['update'])) { - // OK, not required. - - } else if (empty($data['packagefile'])) { + if (empty($data['packagefile'])) { $errors['packagefile'] = get_string('required'); } else { - $files = $this->get_draft_files('packagefile'); + $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'); + } $errors = array_merge($errors, scorm_validate_package($file)); }