Skip to content

Commit

Permalink
MDL-58220 onedrive: Add import from skydrive
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Damyon Wiese committed Apr 3, 2017
1 parent 86a5f1e commit e7688f5
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
57 changes: 57 additions & 0 deletions repository/onedrive/importskydrive.php
@@ -0,0 +1,57 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Import files from skydrive.
*
* @package repository_onedrive
* @copyright 2017 Damyon Wiese <damyon@moodle.com>
* @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();
}
5 changes: 5 additions & 0 deletions repository/onedrive/lang/en/repository_onedrive.php
Expand Up @@ -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.';
Expand All @@ -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.';

82 changes: 82 additions & 0 deletions repository/onedrive/lib.php
Expand Up @@ -980,18 +980,99 @@ 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.
*
* @param moodleform $mform Moodle form (passed by reference).
* @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();
Expand Down Expand Up @@ -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);

}
}

Expand Down

0 comments on commit e7688f5

Please sign in to comment.