MDL-33483 repository: Allow file import formats be configurable.
authorStephen Bourget <steve.bourget@sau19.org>
Thu, 12 Jan 2017 13:43:31 +0000 (08:43 -0500)
committerStephen Bourget <steve.bourget@sau19.org>
Thu, 12 Jan 2017 13:43:31 +0000 (08:43 -0500)
repository/googledocs/db/upgrade.php
repository/googledocs/lang/en/repository_googledocs.php
repository/googledocs/lib.php
repository/googledocs/tests/generator/lib.php
repository/googledocs/version.php

index 97c0ed4..89f475f 100644 (file)
@@ -38,5 +38,15 @@ function xmldb_repository_googledocs_upgrade($oldversion) {
     // Automatically generated Moodle v3.2.0 release upgrade line.
     // Put any upgrade step following this.
 
+    if ($oldversion < 2017011100) {
+        // Set default import formats from Google.
+        set_config('documentformat', 'rtf', 'googledocs');
+        set_config('drawingformat', 'pdf', 'googledocs');
+        set_config('presentationformat', 'pptx', 'googledocs');
+        set_config('spreadsheetformat', 'xlsx', 'googledocs');
+
+        // Plugin savepoint reached.
+        upgrade_plugin_savepoint(true, 2017011100, 'repository', 'googledocs');
+    }
     return true;
 }
index 1666176..ca4d255 100644 (file)
 
 $string['clientid'] = 'Client ID';
 $string['configplugin'] = 'Configure Google Drive plugin';
+$string['docsformat'] = 'Default document import format';
+$string['drawingformat'] = 'Default drawing import format';
 $string['googledocs:view'] = 'View Google Drive repository';
+$string['importformat'] = 'Configure the default import formats from google';
 $string['oauthinfo'] = '<p>To use this plugin, you must register your site with Google, as described in the documentation <a href="{$a->docsurl}">Google OAuth 2.0 setup</a>.</p><p>As part of the registration process, you will need to enter the following URL as \'Authorized Redirect URIs\':</p><p>{$a->callbackurl}</p><p>Once registered, you will be provided with a client ID and secret which can be used to configure all Google Drive and Picasa plugins.</p><p>Please also note that you will have to enable the service \'Drive API\'.</p>';
 $string['pluginname'] = 'Google Drive';
+$string['presentationformat'] = 'Default presentation import format';
 $string['secret'] = 'Secret';
 $string['servicenotenabled'] = 'Access not configured. Make sure the service \'Drive API\' is enabled.';
+$string['spreadsheetformat'] = 'Default spreadsheet import format';
index ca35669..26ff6ed 100644 (file)
@@ -306,6 +306,7 @@ class repository_googledocs extends repository {
         $folders = array();
         $fields = "items(id,title,mimeType,downloadUrl,fileExtension,exportLinks,modifiedDate,fileSize,thumbnailLink)";
         $params = array('q' => $q, 'fields' => $fields);
+        $config = get_config('googledocs');
 
         try {
             // Retrieving files and folders.
@@ -344,18 +345,35 @@ class repository_googledocs extends repository {
                     $type = str_replace('application/vnd.google-apps.', '', $item['mimeType']);
                     $title = '';
                     $exportType = '';
+                    $types = get_mimetypes_array();
+
                     switch ($type){
                         case 'document':
-                            $title = $item['title'] . '.rtf';
-                            $exportType = 'application/rtf';
+                            $ext = $config->documentformat;
+                            $title = $item['title'] . '.'. $ext;
+                            if ($ext === 'rtf') {
+                                // Moodle user 'text/rtf' as the MIME type for RTF files.
+                                // Google uses 'application/rtf' for the same type of file.
+                                // See https://developers.google.com/drive/v3/web/manage-downloads.
+                                $exportType = 'application/rtf';
+                            } else {
+                                $exportType = $types[$ext]['type'];
+                            }
                             break;
                         case 'presentation':
-                            $title = $item['title'] . '.pptx';
-                            $exportType = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
+                            $ext = $config->presentationformat;
+                            $title = $item['title'] . '.'. $ext;
+                            $exportType = $types[$ext]['type'];
                             break;
                         case 'spreadsheet':
-                            $title = $item['title'] . '.xlsx';
-                            $exportType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
+                            $ext = $config->spreadsheetformat;
+                            $title = $item['title'] . '.'. $ext;
+                            $exportType = $types[$ext]['type'];
+                            break;
+                        case 'drawing':
+                            $ext = $config->drawingformat;
+                            $title = $item['title'] . '.'. $ext;
+                            $exportType = $types[$ext]['type'];
                             break;
                     }
                     // Skips invalid/unknown types.
@@ -472,7 +490,9 @@ class repository_googledocs extends repository {
      * @return array
      */
     public static function get_type_option_names() {
-        return array('clientid', 'secret', 'pluginname');
+        return array('clientid', 'secret', 'pluginname',
+            'documentformat', 'drawingformat',
+            'presentationformat', 'spreadsheetformat');
     }
 
     /**
@@ -500,6 +520,58 @@ class repository_googledocs extends repository {
         $strrequired = get_string('required');
         $mform->addRule('clientid', $strrequired, 'required', null, 'client');
         $mform->addRule('secret', $strrequired, 'required', null, 'client');
+
+        $mform->addElement('static', null, '', get_string('importformat', 'repository_googledocs', $a));
+
+        // Documents.
+        $docsformat = array();
+        $docsformat['html'] = 'html';
+        $docsformat['docx'] = 'docx';
+        $docsformat['odt'] = 'odt';
+        $docsformat['pdf'] = 'pdf';
+        $docsformat['rtf'] = 'rtf';
+        $docsformat['txt'] = 'txt';
+        core_collator::ksort($docsformat, core_collator::SORT_NATURAL);
+
+        $mform->addElement('select', 'documentformat', get_string('docsformat', 'repository_googledocs'), $docsformat);
+        $mform->setDefault('documentformat', $docsformat['rtf']);
+        $mform->setType('documentformat', PARAM_ALPHANUM);
+
+        // Drawing.
+        $drawingformat = array();
+        $drawingformat['jpeg'] = 'jpeg';
+        $drawingformat['png'] = 'png';
+        $drawingformat['svg'] = 'svg';
+        $drawingformat['pdf'] = 'pdf';
+        core_collator::ksort($drawingformat, core_collator::SORT_NATURAL);
+
+        $mform->addElement('select', 'drawingformat', get_string('drawingformat', 'repository_googledocs'), $drawingformat);
+        $mform->setDefault('drawingformat', $drawingformat['pdf']);
+        $mform->setType('drawingformat', PARAM_ALPHANUM);
+
+        // Presentation.
+        $presentationformat = array();
+        $presentationformat['pdf'] = 'pdf';
+        $presentationformat['pptx'] = 'pptx';
+        $presentationformat['txt'] = 'txt';
+        core_collator::ksort($presentationformat, core_collator::SORT_NATURAL);
+
+        $mform->addElement('select', 'presentationformat', get_string('presentationformat', 'repository_googledocs'), $presentationformat);
+        $mform->setDefault('presentationformat', $presentationformat['pptx']);
+        $mform->setType('presentationformat', PARAM_ALPHANUM);
+
+        // Spreadsheet.
+        $spreadsheetformat = array();
+        $spreadsheetformat['csv'] = 'csv';
+        $spreadsheetformat['ods'] = 'ods';
+        $spreadsheetformat['pdf'] = 'pdf';
+        $spreadsheetformat['xlsx'] = 'xlsx';
+        core_collator::ksort($spreadsheetformat, core_collator::SORT_NATURAL);
+
+        $mform->addElement('select', 'spreadsheetformat', get_string('spreadsheetformat', 'repository_googledocs'), $spreadsheetformat);
+        $mform->setDefault('spreadsheetformat', $spreadsheetformat['xlsx']);
+        $mform->setType('spreadsheetformat', PARAM_ALPHANUM);
+
     }
 }
 // Icon from: http://www.iconspedia.com/icon/google-2706.html.
index af7f955..168c406 100644 (file)
@@ -47,6 +47,18 @@ class repository_googledocs_generator extends testing_repository_generator {
         if (!isset($record['secret'])) {
             $record['secret'] = 'secret';
         }
+        if (!isset($record['documentformat'])) {
+            $record['documentformat'] = 'pdf';
+        }
+        if (!isset($record['presentationformat'])) {
+            $record['presentationformat'] = 'pdf';
+        }
+        if (!isset($record['drawingformat'])) {
+            $record['drawingformat'] = 'pdf';
+        }
+        if (!isset($record['spreadsheetformat'])) {
+            $record['spreadsheetformat'] = 'pdf';
+        }
         return $record;
     }
 
index 1f58df8..04eaa0e 100644 (file)
@@ -25,6 +25,6 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version   = 2016120500;        // The current plugin version (Date: YYYYMMDDXX).
+$plugin->version   = 2017011100;        // The current plugin version (Date: YYYYMMDDXX).
 $plugin->requires  = 2016112900;        // Requires this Moodle version.
 $plugin->component = 'repository_googledocs'; // Full name of the plugin (used for diagnostics).