first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,80 @@
<?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/>.
/**
* Privacy class for requesting user data.
*
* @package portfolio_googledocs
* @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace portfolio_googledocs\privacy;
defined('MOODLE_INTERNAL') || die();
use core_privacy\local\metadata\collection;
/**
* Provider for the portfolio_googledocs plugin.
*
* @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements
// This portfolio plugin does not store any data itself.
// It has no database tables, and it purely acts as a conduit, sending data externally.
\core_privacy\local\metadata\provider,
\core_portfolio\privacy\portfolio_provider {
/**
* Returns meta data about this system.
*
* @param collection $collection The initialised collection to add items to.
* @return collection A listing of user data stored through this system.
*/
public static function get_metadata(collection $collection): collection {
return $collection->add_external_location_link('docs.google.com', ['data' => 'privacy:metadata:data'],
'privacy:metadata');
}
/**
* Export all portfolio data from each portfolio plugin for the specified userid and context.
*
* @param int $userid The user to export.
* @param \context $context The context to export.
* @param array $subcontext The subcontext within the context to export this information to.
* @param array $linkarray The weird and wonderful link array used to display information for a specific item
*/
public static function export_portfolio_user_data(int $userid, \context $context, array $subcontext, array $linkarray) {
}
/**
* Delete all user information for the provided context.
*
* @param \context $context The context to delete user data for.
*/
public static function delete_portfolio_for_context(\context $context) {
}
/**
* Delete all user information for the provided user and context.
*
* @param int $userid The user to delete
* @param \context $context The context to refine the deletion.
*/
public static function delete_portfolio_for_user(int $userid, \context $context) {
}
}
+35
View File
@@ -0,0 +1,35 @@
<?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/>.
/**
* @param int $oldversion the version we are upgrading from
* @return bool result
*/
function xmldb_portfolio_googledocs_upgrade($oldversion) {
// Automatically generated Moodle v4.1.0 release upgrade line.
// Put any upgrade step following this.
// Automatically generated Moodle v4.2.0 release upgrade line.
// Put any upgrade step following this.
// Automatically generated Moodle v4.3.0 release upgrade line.
// Put any upgrade step following this.
// Automatically generated Moodle v4.4.0 release upgrade line.
// Put any upgrade step following this.
return true;
}
@@ -0,0 +1,35 @@
<?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/>.
/**
* Strings for component 'portfolio_googledocs', language 'en', branch 'MOODLE_20_STABLE'
*
* @package portfolio_googledocs
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['clientid'] = 'Client ID';
$string['noauthtoken'] = 'An authentication token has not been received from Google. Please ensure you are allowing Moodle to access your Google account';
$string['nooauthcredentials'] = 'OAuth credentials required.';
$string['nooauthcredentials_help'] = 'To use the Google Drive portfolio plugin you must configure OAuth credentials in the portfolio settings.';
$string['nosessiontoken'] = 'A session token does not exist preventing export to 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 plugins.</p>';
$string['pluginname'] = 'Google Drive';
$string['privacy:metadata'] = 'This plugin sends data externally to a linked Google account. It does not store data locally.';
$string['privacy:metadata:data'] = 'Personal data passed through from the portfolio subsystem.';
$string['sendfailed'] = 'The file {$a} failed to transfer to google';
$string['secret'] = 'Secret';
+265
View File
@@ -0,0 +1,265 @@
<?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/>.
/**
* Google Documents Portfolio Plugin
*
* @author Dan Poltawski <talktodan@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
require_once($CFG->libdir.'/portfolio/plugin.php');
require_once($CFG->libdir . '/google/lib.php');
class portfolio_plugin_googledocs extends portfolio_plugin_push_base {
/**
* Google Client.
* @var Google_Client
*/
private $client = null;
/**
* Google Drive Service.
* @var Google_Service_Drive
*/
private $service = null;
/**
* URL to redirect Google to.
* @var string
*/
const REDIRECTURL = '/admin/oauth2callback.php';
/**
* Key in session which stores token (_drive_file is access level).
* @var string
*/
const SESSIONKEY = 'googledrive_accesstoken_drive_file';
public function supported_formats() {
return array(PORTFOLIO_FORMAT_FILE, PORTFOLIO_FORMAT_RICHHTML);
}
public static function get_name() {
return get_string('pluginname', 'portfolio_googledocs');
}
public function prepare_package() {
// We send the files as they are, no prep required.
return true;
}
public function get_interactive_continue_url() {
return 'http://drive.google.com/';
}
public function expected_time($callertime) {
// We're forcing this to be run 'interactively' because the plugin
// does not support running in cron.
return PORTFOLIO_TIME_LOW;
}
public function send_package() {
if (!$this->client) {
throw new portfolio_plugin_exception('noauthtoken', 'portfolio_googledocs');
}
// Create a parent directory for the export to Google Drive so that all files from the
// same export can be contained in one place for easy downloading.
$now = time();
$exportdirectoryname = $this->exporter->get('caller')->display_name();
$exportdirectoryname = strtolower(join('-', explode(' ', $exportdirectoryname)));
$exportdirectoryname = "/portfolio-export-{$exportdirectoryname}-{$now}";
$directoryids = [];
foreach ($this->exporter->get_tempfiles() as $file) {
$filepath = $exportdirectoryname . $file->get_filepath();
$directories = array_filter(explode('/', $filepath), function($part) {
return !empty($part);
});
// Track how deep into the directory structure we are. This is the key
// we'll use to keep track of previously created directory ids.
$path = '/';
// Track the parent directory so that we can look up it's id for creating
// subdirectories in Google Drive.
$parentpath = null;
// Create each of the directories in Google Drive that we need.
foreach ($directories as $directory) {
// Update the current path for this file.
$path .= "{$directory}/";
if (!isset($directoryids[$path])) {
// This directory hasn't been created yet so let's go ahead and create it.
$parents = !is_null($parentpath) ? [$directoryids[$parentpath]] : [];
try {
$filemetadata = new Google_Service_Drive_DriveFile([
'title' => $directory,
'mimeType' => 'application/vnd.google-apps.folder',
'parents' => $parents
]);
$drivefile = $this->service->files->insert($filemetadata, ['fields' => 'id']);
$directoryids[$path] = ['id' => $drivefile->id];
} catch (Exception $e) {
throw new portfolio_plugin_exception('sendfailed', 'portfolio_gdocs', $directory);
}
}
$parentpath = $path;
}
try {
// Create drivefile object and fill it with data.
$drivefile = new Google_Service_Drive_DriveFile();
$drivefile->setTitle($file->get_filename());
$drivefile->setMimeType($file->get_mimetype());
// Add the parent directory id to make sure the file gets created in the correct
// directory in Google Drive.
$drivefile->setParents([$directoryids[$filepath]]);
$filecontent = $file->get_content();
$this->service->files->insert($drivefile,
array('data' => $filecontent,
'mimeType' => $file->get_mimetype(),
'uploadType' => 'multipart'));
} catch ( Exception $e ) {
throw new portfolio_plugin_exception('sendfailed', 'portfolio_gdocs', $file->get_filename());
}
}
return true;
}
/**
* Gets the access token from session and sets it to client.
*
* @return null|string null or token.
*/
private function get_access_token() {
global $SESSION;
if (isset($SESSION->{self::SESSIONKEY}) && $SESSION->{self::SESSIONKEY}) {
$this->client->setAccessToken($SESSION->{self::SESSIONKEY});
return $SESSION->{self::SESSIONKEY};
}
return null;
}
/**
* Sets the access token to session
*
* @param string $token access token in json format
* @return
*/
private function set_access_token($token) {
global $SESSION;
$SESSION->{self::SESSIONKEY} = $token;
}
public function steal_control($stage) {
global $CFG;
if ($stage != PORTFOLIO_STAGE_CONFIG) {
return false;
}
$this->initialize_oauth();
if ($this->get_access_token()) {
// Ensure that token is not expired.
if (!$this->client->isAccessTokenExpired()) {
return false;
}
}
return $this->client->createAuthUrl();
}
public function post_control($stage, $params) {
if ($stage != PORTFOLIO_STAGE_CONFIG) {
return;
}
// Get the authentication code send by Google.
$code = isset($params['oauth2code']) ? $params['oauth2code'] : null;
// Try to authenticate (throws exception which is catched higher).
$this->client->authenticate($code);
// Make sure we accually have access token at this time
// ...and store it for further use.
if ($accesstoken = $this->client->getAccessToken()) {
$this->set_access_token($accesstoken);
} else {
throw new portfolio_plugin_exception('nosessiontoken', 'portfolio_gdocs');
}
}
public static function allows_multiple_instances() {
return false;
}
public static function has_admin_config() {
return true;
}
public static function get_allowed_config() {
return array('clientid', 'secret');
}
public static function admin_config_form(&$mform) {
$a = new stdClass;
$a->docsurl = get_docs_url('Google_OAuth_2.0_setup');
$a->callbackurl = (new moodle_url(self::REDIRECTURL))->out(false);
$mform->addElement('static', null, '', get_string('oauthinfo', 'portfolio_googledocs', $a));
$mform->addElement('text', 'clientid', get_string('clientid', 'portfolio_googledocs'));
$mform->setType('clientid', PARAM_RAW_TRIMMED);
$mform->addElement('text', 'secret', get_string('secret', 'portfolio_googledocs'));
$mform->setType('secret', PARAM_RAW_TRIMMED);
$strrequired = get_string('required');
$mform->addRule('clientid', $strrequired, 'required', null, 'client');
$mform->addRule('secret', $strrequired, 'required', null, 'client');
}
private function initialize_oauth() {
$redirecturi = new moodle_url(self::REDIRECTURL);
$returnurl = new moodle_url('/portfolio/add.php');
$returnurl->param('postcontrol', 1);
$returnurl->param('id', $this->exporter->get('id'));
$returnurl->param('sesskey', sesskey());
$clientid = $this->get_config('clientid');
$secret = $this->get_config('secret');
// Setup Google client.
$this->client = get_google_client();
$this->client->setClientId($clientid);
$this->client->setClientSecret($secret);
$this->client->setScopes(array(Google_Service_Drive::DRIVE_FILE));
$this->client->setRedirectUri($redirecturi->out(false));
// URL to be called when redirecting from authentication.
$this->client->setState($returnurl->out_as_local_url(false));
// Setup drive upload service.
$this->service = new Google_Service_Drive($this->client);
}
public function instance_sanity_check() {
$clientid = $this->get_config('clientid');
$secret = $this->get_config('secret');
// If there is no oauth config (e.g. plugins upgraded from < 2.3 then
// there will be no config and this plugin should be disabled.
if (empty($clientid) or empty($secret)) {
return 'nooauthcredentials';
}
return 0;
}
}
+109
View File
@@ -0,0 +1,109 @@
<?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/>.
namespace portfolio_googledocs;
use portfolio_admin_form;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/portfoliolib.php');
require_once($CFG->libdir . '/portfolio/forms.php');
/**
* Googledocs portfolio functional test.
*
* @package portfolio_googledocs
* @category tests
* @copyright 2016 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class plugin_test extends \advanced_testcase {
/** @var string name of the portfolio plugin */
protected $pluginname = 'googledocs';
/**
* Creates a new instance of the portfolio plugin
*
* @param string $name name of the instance
* @param \stdClass $data config data for the instance
* @return portfolio_plugin_base
*/
protected function enable_plugin($name = 'Instance name', $data = null) {
$data = $data ?: new \stdClass();
$instance = portfolio_static_function($this->pluginname, 'create_instance', $this->pluginname, $name, $data);
\core_plugin_manager::reset_caches();
return $instance;
}
/**
* Test for method enable_plugin()
*/
public function test_enable(): void {
global $DB;
$this->resetAfterTest();
$instance = $this->enable_plugin();
$record = $DB->get_record('portfolio_instance', ['plugin' => $this->pluginname]);
$this->assertEquals($record->id, $instance->get('id'));
$this->assertEquals('portfolio_plugin_' . $this->pluginname, get_class($instance));
$this->assertEquals(1, $instance->get('visible'));
}
/**
* Test submitting a form for creating an instance
*/
public function test_create_form(): void {
$formdata = ['name' => 'Instance name', 'clientid' => 'CLIENT', 'secret' => 'SECRET'];
portfolio_admin_form::mock_submit($formdata);
$form = new portfolio_admin_form('', array('plugin' => $this->pluginname,
'instance' => null, 'portfolio' => null,
'action' => 'new', 'visible' => 1));
$data = $form->get_data();
$this->assertEquals('new', $data->action);
$this->assertEquals(1, $data->visible);
$this->assertEquals($this->pluginname, $data->plugin);
foreach ($formdata as $key => $value) {
$this->assertEquals($value, $data->$key);
}
}
/**
* Test submitting a form for editing an instance
*/
public function test_edit_form(): void {
$this->resetAfterTest();
$instance = $this->enable_plugin();
$formdata = ['name' => 'New name', 'clientid' => 'CLIENT', 'secret' => 'SECRET'];
portfolio_admin_form::mock_submit($formdata);
$form = new portfolio_admin_form('', array('plugin' => $this->pluginname,
'instance' => $instance, 'portfolio' => $instance->get('id'),
'action' => 'edit', 'visible' => $instance->get('visible')));
$this->assertTrue($form->is_validated());
$this->assertTrue($form->is_submitted());
$data = $form->get_data();
$this->assertEquals('edit', $data->action);
$this->assertEquals($instance->get('visible'), $data->visible);
$this->assertEquals($this->pluginname, $data->plugin);
foreach ($formdata as $key => $value) {
$this->assertEquals($value, $data->$key);
}
}
}
@@ -0,0 +1,47 @@
<?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/>.
/**
* Privacy provider tests.
*
* @package portfolio_googledocs
* @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace portfolio_googledocs\privacy;
defined('MOODLE_INTERNAL') || die();
/**
* Privacy provider tests class.
*
* @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider_test extends \core_privacy\tests\provider_testcase {
/**
* Verify that a collection of metadata is returned for this component and that it just links to an external location.
*/
public function test_get_metadata(): void {
$collection = new \core_privacy\local\metadata\collection('portfolio_googledocs');
$collection = \portfolio_googledocs\privacy\provider::get_metadata($collection);
$this->assertNotEmpty($collection);
$items = $collection->get_collection();
$this->assertEquals(1, count($items));
$this->assertInstanceOf(\core_privacy\local\metadata\types\external_location::class, $items[0]);
}
}
+7
View File
@@ -0,0 +1,7 @@
This files describes API changes in /portfolio/googledocs system,
information provided here is intended especially for developers.
=== 3.9 ===
* The Google Drive portfolio exports are now bundled in a single parent folder. The paths for the exported files will
now be created in Drive so that exported files are in the correct directory structure.
+30
View File
@@ -0,0 +1,30 @@
<?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/>.
/**
* Version details
*
* @package portfolio
* @subpackage googledocs
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2024042200; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2024041600; // Requires this Moodle version.
$plugin->component = 'portfolio_googledocs'; // Full name of the plugin (used for diagnostics).
$plugin->cron = 0;