From e7688f559a2a5c6e69c1f0cab3e4a17a4d05ed86 Mon Sep 17 00:00:00 2001 From: Damyon Wiese Date: Thu, 30 Mar 2017 15:42:41 +0800 Subject: [PATCH] MDL-58220 onedrive: Add import from skydrive If the skydrive repo exists - show a button on the config page for the onedrive repo to "steal" all the files from the other repo and disable it. --- repository/onedrive/importskydrive.php | 57 +++++++++++++ .../onedrive/lang/en/repository_onedrive.php | 5 ++ repository/onedrive/lib.php | 82 +++++++++++++++++++ 3 files changed, 144 insertions(+) create mode 100644 repository/onedrive/importskydrive.php diff --git a/repository/onedrive/importskydrive.php b/repository/onedrive/importskydrive.php new file mode 100644 index 0000000000000..6eeeb13de7d02 --- /dev/null +++ b/repository/onedrive/importskydrive.php @@ -0,0 +1,57 @@ +. + +/** + * Import files from skydrive. + * + * @package repository_onedrive + * @copyright 2017 Damyon Wiese + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require_once(__DIR__ . '/../../config.php'); + +$PAGE->set_url('/repository/onedrive/importskydrive.php'); +$PAGE->set_context(context_system::instance()); +$strheading = get_string('importskydrivefiles', 'repository_onedrive'); +$PAGE->set_title($strheading); +$PAGE->set_heading($strheading); + +require_login(); + +require_capability('moodle/site:config', context_system::instance()); + +$confirm = optional_param('confirm', false, PARAM_BOOL); + +if ($confirm) { + require_sesskey(); + require_once($CFG->dirroot . '/repository/lib.php'); + require_once($CFG->dirroot . '/repository/onedrive/lib.php'); + + if (repository_onedrive::import_skydrive_files()) { + $mesg = get_string('skydrivefilesimported', 'repository_onedrive'); + redirect(new moodle_url('/admin/repository.php'), $mesg, null, \core\output\notification::NOTIFY_SUCCESS); + } else { + $mesg = get_string('skydrivefilesnotimported', 'repository_onedrive'); + redirect(new moodle_url('/admin/repository.php'), $mesg, null, \core\output\notification::NOTIFY_ERROR); + } +} else { + $continueurl = new moodle_url('/repository/onedrive/importskydrive.php', ['confirm' => true]); + $cancelurl = new moodle_url('/admin/repository.php'); + echo $OUTPUT->header(); + echo $OUTPUT->confirm(get_string('confirmimportskydrive', 'repository_onedrive'), $continueurl, $cancelurl); + echo $OUTPUT->footer(); +} diff --git a/repository/onedrive/lang/en/repository_onedrive.php b/repository/onedrive/lang/en/repository_onedrive.php index 015647fa9d1e2..46f32672beb9a 100644 --- a/repository/onedrive/lang/en/repository_onedrive.php +++ b/repository/onedrive/lang/en/repository_onedrive.php @@ -25,6 +25,7 @@ $string['configplugin'] = 'Configure OneDrive plugin'; $string['skydrive:view'] = 'View OneDrive repository'; $string['pluginname'] = 'Microsoft OneDrive'; +$string['importskydrivefiles'] = 'Import files from Microsoft SkyDrive repository'; $string['issuer'] = 'OAuth 2 service'; $string['issuer_help'] = 'Select the OAuth 2 service that is configured to talk to the OneDrive API. If the services does not exist yet, you might need to create it.'; $string['servicenotenabled'] = 'Access not configured.'; @@ -33,9 +34,13 @@ $string['internal'] = 'Internal (files stored in Moodle)'; $string['external'] = 'External (only links stored in Moodle)'; $string['both'] = 'Internal and External'; +$string['skydrivefilesexist'] = 'Files found in the Microsoft SkyDrive repository. This repository is deprecated by Microsoft - the files can be automatically imported to this Microsoft OneDrive repository.'; $string['supportedreturntypes'] = 'Supported files'; $string['defaultreturntype'] = 'Default return type'; $string['fileoptions'] = 'The types and defaults for returned files is configurable here. Note that all files linked externally will be updated so that the owner is the Moodle system account.'; $string['owner'] = 'Owned by: {$a}'; $string['cachedef_folder'] = 'OneDrive File IDs for folders in the system account'; +$string['confirmimportskydrive'] = 'Are you sure you want to import all files from the "Microsoft SkyDrive" repository to the "Microsoft OneDrive" repository? As long as the Microsoft OneDrive repository is already configured and working - all imported files will continue working as before. There is no way to undo these changes.'; +$string['skydrivefilesimported'] = 'All files were imported from the Microsoft SkyDrive repository.'; +$string['skydrivefilesnotimported'] = 'Some files could not be imported from the Microsoft SkyDrive repository.'; diff --git a/repository/onedrive/lib.php b/repository/onedrive/lib.php index 4d5e8d2824269..5e79d9bca9678 100644 --- a/repository/onedrive/lib.php +++ b/repository/onedrive/lib.php @@ -980,6 +980,77 @@ public function get_reference_details($reference, $filestatus = 0) { } } + /** + * Return true if any instances of the skydrive repo exist - and we can import them. + * + * @return bool + */ + public static function can_import_skydrive_files() { + global $DB; + + $skydrive = $DB->get_record('repository', ['type' => 'skydrive'], 'id', IGNORE_MISSING); + $onedrive = $DB->get_record('repository', ['type' => 'onedrive'], 'id', IGNORE_MISSING); + + if (empty($skydrive) || empty($onedrive)) { + return false; + } + + $ready = true; + try { + $issuer = \core\oauth2\api::get_issuer(get_config('onedrive', 'issuerid')); + if (!$issuer->get('enabled')) { + $ready = false; + } + if (!$issuer->is_configured()) { + $ready = false; + } + } catch (dml_missing_record_exception $e) { + $ready = false; + } + if (!$ready) { + return false; + } + + $sql = "SELECT count('x') + FROM {repository_instances} i, {repository} r + WHERE r.type=:plugin AND r.id=i.typeid"; + $params = array('plugin' => 'skydrive'); + return $DB->count_records_sql($sql, $params) > 0; + } + + /** + * Import all the files that were created with the skydrive repo to this repo. + * + * @return bool + */ + public static function import_skydrive_files() { + global $DB; + + if (!self::can_import_skydrive_files()) { + return false; + } + // Should only be one of each. + $skydrivetype = repository::get_type_by_typename('skydrive'); + + $skydriveinstances = repository::get_instances(['type' => 'skydrive']); + $skydriveinstance = reset($skydriveinstances); + $onedriveinstances = repository::get_instances(['type' => 'onedrive']); + $onedriveinstance = reset($onedriveinstances); + + // Update all file references. + $DB->set_field('files_reference', 'repositoryid', $onedriveinstance->id, ['repositoryid' => $skydriveinstance->id]); + + // Delete and disable the skydrive repo. + $skydrivetype->delete(); + core_plugin_manager::reset_caches(); + + $sql = "SELECT count('x') + FROM {repository_instances} i, {repository} r + WHERE r.type=:plugin AND r.id=i.typeid"; + $params = array('plugin' => 'skydrive'); + return $DB->count_records_sql($sql, $params) == 0; + } + /** * Edit/Create Admin Settings Moodle form. * @@ -987,11 +1058,21 @@ public function get_reference_details($reference, $filestatus = 0) { * @param string $classname repository class name. */ public static function type_config_form($mform, $classname = 'repository') { + global $OUTPUT; + $url = new moodle_url('/admin/tool/oauth2/issuers.php'); $url = $url->out(); $mform->addElement('static', null, '', get_string('oauth2serviceslink', 'repository_onedrive', $url)); + if (self::can_import_skydrive_files()) { + $notice = get_string('skydrivefilesexist', 'repository_onedrive'); + $url = new moodle_url('/repository/onedrive/importskydrive.php'); + $attrs = ['class' => 'btn btn-primary']; + $button = $OUTPUT->action_link($url, get_string('importskydrivefiles', 'repository_onedrive'), null, $attrs); + $mform->addElement('static', null, '', $OUTPUT->notification($notice) . $button); + } + parent::type_config_form($mform); $options = []; $issuers = \core\oauth2\api::get_all_issuers(); @@ -1019,6 +1100,7 @@ public static function type_config_form($mform, $classname = 'repository') { FILE_CONTROLLED_LINK => get_string('external', 'repository_onedrive'), ]; $mform->addElement('select', 'defaultreturntype', get_string('defaultreturntype', 'repository_onedrive'), $choices); + } }