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
+89
View File
@@ -0,0 +1,89 @@
<?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/>.
/**
* Callback for equella repository.
*
* @since Moodle 2.3
* @package repository_equella
* @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require(__DIR__.'/../../config.php');
require_once($CFG->dirroot . '/repository/lib.php');
$json = required_param('tlelinks', PARAM_RAW);
require_login();
$decodedinfo = json_decode($json);
$info = array_pop($decodedinfo);
$url = '';
if (isset($info->url)) {
$url = s(clean_param($info->url, PARAM_URL));
}
$filename = '';
// Use $info->filename if exists, $info->name is a display name,
// it may not have extension
if (isset($info->filename)) {
$filename = s(clean_param($info->filename, PARAM_FILE));
} else if (isset($info->name)) {
$filename = s(clean_param($info->name, PARAM_FILE));
}
$thumbnail = '';
if (isset($info->thumbnail)) {
$thumbnail = s(clean_param($info->thumbnail, PARAM_URL));
}
$author = '';
if (isset($info->owner)) {
$author = s(clean_param($info->owner, PARAM_NOTAGS));
}
$license = '';
if (isset($info->license)) {
$license = s(clean_param($info->license, PARAM_ALPHAEXT));
}
$source = base64_encode(json_encode(array('url'=>$url,'filename'=>$filename)));
$sourcekey = sha1($source . repository::get_secret_key() . sesskey());
$js =<<<EOD
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script type="text/javascript">
window.onload = function() {
var resource = {};
resource.title = "$filename";
resource.source = "$source";
resource.sourcekey = "$sourcekey";
resource.thumbnail = '$thumbnail';
resource.author = "$author";
resource.license = "$license";
parent.M.core_filepicker.select_file(resource);
}
</script>
</head>
<body><noscript></noscript></body>
</html>
EOD;
header('Content-Type: text/html; charset=utf-8');
die($js);
@@ -0,0 +1,46 @@
<?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 Subsystem implementation for repository_equella.
*
* @package repository_equella
* @copyright 2018 Zig Tan <zig@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace repository_equella\privacy;
defined('MOODLE_INTERNAL') || die();
/**
* Privacy Subsystem for repository_equella implementing null_provider.
*
* @copyright 2018 Zig Tan <zig@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\null_provider {
/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
*
* @return string
*/
public static function get_reason(): string {
return 'privacy:metadata';
}
}
+36
View File
@@ -0,0 +1,36 @@
<?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/>.
/**
* Capabilities for equella repository.
*
* @package repository_equella
* @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
* @author Dongsheng Cai <dongsheng@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$capabilities = array(
'repository/equella:view' => array(
'captype' => 'read',
'contextlevel' => CONTEXT_MODULE,
'archetypes' => array(
'user' => CAP_ALLOW
)
)
);
@@ -0,0 +1,49 @@
<?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 equella repository.
*
* @package repository_equella
* @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['breadcrumb'] = 'EQUELLA';
$string['configplugin'] = 'Configuration for EQUELLA repository';
$string['equella:view'] = 'View EQUELLA repository';
$string['equellaaction'] = 'EQUELLA action';
$string['equellaoptions'] = 'EQUELLA options';
$string['equellaurl'] = 'EQUELLA URL';
$string['equellauserfield'] = 'EQUELLA user field';
$string['equellauserfield_help'] = 'Choose the user field to be used for matching the EQUELLA user. (Only user fields of type short text are listed.)';
$string['equellausername'] = 'Username';
$string['group'] = '{$a} role settings';
$string['groupdefault'] = 'Default';
$string['pluginname'] = 'EQUELLA repository';
$string['privacy:metadata'] = 'The EQUELLA repository plugin does not store any personal data, but does transmit user data from Moodle to the remote system.';
$string['restrictionattachmentsonly'] = 'Attached resource only';
$string['restrictionitemsonly'] = 'Item summary only';
$string['restrictionnone'] = 'No restriction';
$string['search'] = 'Search EQUELLA';
$string['selectrestriction'] = 'Restrict selection';
$string['selectrestriction.desc'] = 'Choose whether course editors should only be able to select an item summary, an attached resources or either';
$string['sharedid'] = 'Shared secret ID';
$string['sharedidtitle'] = 'Shared secret ID';
$string['sharedsecrets'] = 'Shared secret';
$string['sharedsecretsheading'] = 'Shared Secret Settings';
$string['sharedsecretshelp'] = '<p>Below you can set a default EQUELLA shared secret for single signing-on users. You can configure different shared secrets for general (read) usage, and a specialised role based shared secret for each <em>write</em> role in your Moodle site. If a shared secret ID is not configured for a role then the default shared secret ID and shared secret are used.</p><p>All shared secret IDs and shared secrets must also be configured within EQUELLA and the shared secret module enabled. This configuration is found in the EQUELLA Administration Console under User Management > Shared Secrets.</p>';
$string['sharedsecrettitle'] = 'Shared secret';
+468
View File
@@ -0,0 +1,468 @@
<?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/>.
/**
* This plugin is used to access equella repositories.
*
* @since Moodle 2.3
* @package repository_equella
* @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/repository/lib.php');
/**
* repository_equella class implements equella_client
*
* @since Moodle 2.3
* @package repository_equella
* @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class repository_equella extends repository {
/** @var array mimetype filter */
private $mimetypes = array();
/**
* Constructor
*
* @param int $repositoryid repository instance id
* @param int|stdClass $context a context id or context object
* @param array $options repository options
*/
public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
parent::__construct($repositoryid, $context, $options);
if (isset($this->options['mimetypes'])) {
$mt = $this->options['mimetypes'];
if (!empty($mt) && is_array($mt) && !in_array('*', $mt)) {
$this->mimetypes = array_unique(array_map(array($this, 'to_mime_type'), $mt));
}
}
}
/**
* Display embedded equella interface
*
* @param string $path
* @param mixed $page
* @return array
*/
public function get_listing($path = null, $page = null) {
global $COURSE;
$callbackurl = new moodle_url('/repository/equella/callback.php', array('repo_id'=>$this->id));
$mimetypesstr = '';
$restrict = '';
if (!empty($this->mimetypes)) {
$mimetypesstr = '&mimeTypes=' . implode(',', $this->mimetypes);
// We're restricting to a mime type, so we always restrict to selecting resources only.
$restrict = '&attachmentonly=true';
} else if ($this->get_option('equella_select_restriction') != 'none') {
// The option value matches the EQUELLA paramter name.
$restrict = '&' . $this->get_option('equella_select_restriction') . '=true';
}
$url = $this->get_option('equella_url')
. '?method=lms'
. '&returnurl='.urlencode($callbackurl)
. '&returnprefix=tle'
. '&template=standard'
. '&token='.urlencode($this->getssotoken('write'))
. '&courseId='.urlencode($COURSE->idnumber)
. '&courseCode='.urlencode($COURSE->shortname)
. '&action=searchThin'
. '&forcePost=true'
. '&cancelDisabled=true'
. '&attachmentUuidUrls=true'
. '&options='.urlencode($this->get_option('equella_options') . $mimetypesstr)
. $restrict;
$manageurl = $this->get_option('equella_url');
$manageurl = str_ireplace('signon.do', 'logon.do', $manageurl);
$manageurl = $this->appendtoken($manageurl);
$list = array();
$list['object'] = array();
$list['object']['type'] = 'text/html';
$list['object']['src'] = $url;
$list['nologin'] = true;
$list['nosearch'] = true;
$list['norefresh'] = true;
$list['manage'] = $manageurl;
return $list;
}
/**
* Supported equella file types
*
* @return int
*/
public function supported_returntypes() {
return (FILE_INTERNAL | FILE_REFERENCE);
}
/**
* Prepare file reference information
*
* @param string $source
* @return string file referece
*/
public function get_file_reference($source) {
// Internally we store serialized value but user input is json-encoded for security reasons.
$ref = json_decode(base64_decode($source));
$filename = clean_param($ref->filename, PARAM_FILE);
$url = clean_param($ref->url, PARAM_URL);
return base64_encode(serialize((object)array('url' => $url, 'filename' => $filename)));
}
/**
* Counts the number of failed connections.
*
* If we received the connection timeout more than 3 times in a row, we don't attemt to
* connect to the server any more during this request.
*
* This function is used by {@link repository_equella::sync_reference()} that
* synchronises the file size of referenced files.
*
* @param int $errno omit if we just want to know the return value, the last curl_errno otherwise
* @return bool true if we had less than 3 failed connections, false if no more connections
* attempts recommended
*/
private function connection_result($errno = null) {
static $countfailures = array();
$sess = sesskey();
if (!array_key_exists($sess, $countfailures)) {
$countfailures[$sess] = 0;
}
if ($errno !== null) {
if ($errno == 0) {
// reset count of failed connections
$countfailures[$sess] = 0;
} else if ($errno == 7 /*CURLE_COULDNT_CONNECT*/ || $errno == 9 /*CURLE_REMOTE_ACCESS_DENIED*/) {
// problems with server
$countfailures[$sess]++;
}
}
return ($countfailures[$sess] < 3);
}
/**
* Download a file, this function can be overridden by subclass. {@link curl}
*
* @param string $reference the source of the file
* @param string $filename filename (without path) to save the downloaded file in the
* temporary directory
* @return null|array null if download failed or array with elements:
* path: internal location of the file
* url: URL to the source (from parameters)
*/
public function get_file($reference, $filename = '') {
global $USER, $CFG;
$ref = @unserialize(base64_decode($reference));
if (!isset($ref->url) || !($url = $this->appendtoken($ref->url))) {
// Occurs when the user isn't known..
return null;
}
$path = $this->prepare_file($filename);
$cookiepathname = $this->prepare_file($USER->id. '_'. uniqid('', true). '.cookie');
$c = new curl(array('cookie'=>$cookiepathname));
$result = $c->download_one($url, null, array('filepath' => $path, 'followlocation' => true, 'timeout' => $CFG->repositorygetfiletimeout));
// Delete cookie jar.
if (file_exists($cookiepathname)) {
unlink($cookiepathname);
}
if ($result !== true) {
throw new moodle_exception('errorwhiledownload', 'repository', '', $result);
}
return array('path'=>$path, 'url'=>$url);
}
public function sync_reference(stored_file $file) {
global $USER, $CFG;
if ($file->get_referencelastsync() + DAYSECS > time() || !$this->connection_result()) {
// Synchronise not more often than once a day.
// if we had several unsuccessfull attempts to connect to server - do not try any more.
return false;
}
$ref = @unserialize(base64_decode($file->get_reference()));
if (!isset($ref->url) || !($url = $this->appendtoken($ref->url))) {
// Occurs when the user isn't known..
$file->set_missingsource();
return true;
}
$cookiepathname = $this->prepare_file($USER->id. '_'. uniqid('', true). '.cookie');
$c = new curl(array('cookie' => $cookiepathname));
if (file_extension_in_typegroup($ref->filename, 'web_image')) {
$path = $this->prepare_file('');
$result = $c->download_one($url, null, array('filepath' => $path, 'followlocation' => true, 'timeout' => $CFG->repositorysyncimagetimeout));
if ($result === true) {
$file->set_synchronised_content_from_file($path);
return true;
}
} else {
$result = $c->head($url, array('followlocation' => true, 'timeout' => $CFG->repositorysyncfiletimeout));
}
// Delete cookie jar.
if (file_exists($cookiepathname)) {
unlink($cookiepathname);
}
$this->connection_result($c->get_errno());
$curlinfo = $c->get_info();
if (isset($curlinfo['http_code']) && $curlinfo['http_code'] == 200
&& array_key_exists('download_content_length', $curlinfo)
&& $curlinfo['download_content_length'] >= 0) {
// we received a correct header and at least can tell the file size
$file->set_synchronized(null, $curlinfo['download_content_length']);
return true;
}
$file->set_missingsource();
return true;
}
/**
* Repository method to serve the referenced file
*
* @param stored_file $storedfile the file that contains the reference
* @param int $lifetime Number of seconds before the file should expire from caches (null means $CFG->filelifetime)
* @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only
* @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin
* @param array $options additional options affecting the file serving
*/
public function send_file($stored_file, $lifetime=null , $filter=0, $forcedownload=false, array $options = null) {
$reference = unserialize(base64_decode($stored_file->get_reference()));
$url = $this->appendtoken($reference->url);
if ($url) {
header('Location: ' . $url);
} else {
send_file_not_found();
}
}
/**
* Add Instance settings input to Moodle form
*
* @param MoodleQuickForm $mform
*/
public static function instance_config_form($mform) {
global $CFG;
require_once("{$CFG->dirroot}/user/profile/lib.php");
$mform->addElement('text', 'equella_url', get_string('equellaurl', 'repository_equella'));
$mform->setType('equella_url', PARAM_URL);
$strrequired = get_string('required');
$mform->addRule('equella_url', $strrequired, 'required', null, 'client');
$userfieldoptions = ['default' => get_string('equellausername', 'repository_equella')];
foreach (profile_get_custom_fields() as $field) {
if ($field->datatype != 'text') {
continue;
}
$userfieldoptions[$field->shortname] = format_string($field->name, true, ['context' => context_system::instance()]);
}
$mform->addElement('select', 'equella_userfield', get_string('equellauserfield', 'repository_equella'), $userfieldoptions);
$mform->setDefault('equella_userfield', $userfieldoptions['default']);
$mform->addHelpButton('equella_userfield', 'equellauserfield', 'repository_equella');
$mform->addElement('text', 'equella_options', get_string('equellaoptions', 'repository_equella'));
$mform->setType('equella_options', PARAM_NOTAGS);
$choices = array(
'none' => get_string('restrictionnone', 'repository_equella'),
'itemonly' => get_string('restrictionitemsonly', 'repository_equella'),
'attachmentonly' => get_string('restrictionattachmentsonly', 'repository_equella'),
);
$mform->addElement('select', 'equella_select_restriction', get_string('selectrestriction', 'repository_equella'), $choices);
$mform->addElement('header', 'groupheader',
get_string('group', 'repository_equella', get_string('groupdefault', 'repository_equella')));
$mform->addElement('text', 'equella_shareid', get_string('sharedid', 'repository_equella'));
$mform->setType('equella_shareid', PARAM_RAW);
$mform->addRule('equella_shareid', $strrequired, 'required', null, 'client');
$mform->addElement('text', 'equella_sharedsecret', get_string('sharedsecrets', 'repository_equella'));
$mform->setType('equella_sharedsecret', PARAM_RAW);
$mform->addRule('equella_sharedsecret', $strrequired, 'required', null, 'client');
foreach (self::get_all_editing_roles() as $role) {
$mform->addElement('header', 'groupheader_'.$role->shortname, get_string('group', 'repository_equella',
role_get_name($role)));
$mform->addElement('text', "equella_{$role->shortname}_shareid", get_string('sharedid', 'repository_equella'));
$mform->setType("equella_{$role->shortname}_shareid", PARAM_RAW);
$mform->addElement('text', "equella_{$role->shortname}_sharedsecret",
get_string('sharedsecrets', 'repository_equella'));
$mform->setType("equella_{$role->shortname}_sharedsecret", PARAM_RAW);
}
}
/**
* Names of the instance settings
*
* @return array
*/
public static function get_instance_option_names() {
$rv = array('equella_url', 'equella_select_restriction', 'equella_options',
'equella_shareid', 'equella_sharedsecret', 'equella_userfield',
);
foreach (self::get_all_editing_roles() as $role) {
array_push($rv, "equella_{$role->shortname}_shareid");
array_push($rv, "equella_{$role->shortname}_sharedsecret");
}
return $rv;
}
/**
* Generate equella token
*
* @param string $username
* @param string $shareid
* @param string $sharedsecret
* @return string
*/
private static function getssotoken_raw($username, $shareid, $sharedsecret) {
$time = time() . '000';
return urlencode($username)
. ':'
. $shareid
. ':'
. $time
. ':'
. base64_encode(pack('H*', md5($username . $shareid . $time . $sharedsecret)));
}
/**
* Append token
*
* @param string $url
* @param $readwrite
* @return string
*/
private function appendtoken($url, $readwrite = null) {
$ssotoken = $this->getssotoken($readwrite);
if (!$ssotoken) {
return false;
}
return $url . (strpos($url, '?') != false ? '&' : '?') . 'token=' . urlencode($ssotoken);
}
/**
* Generate equella sso token
*
* @param string $readwrite
* @return string
*/
private function getssotoken($readwrite = 'read') {
global $USER;
if (empty($USER->username)) {
return false;
}
$equellauserfield = $this->get_userfield_value();
if ($readwrite == 'write') {
foreach (self::get_all_editing_roles() as $role) {
if (user_has_role_assignment($USER->id, $role->id, $this->context->id)) {
// See if the user has a role that is linked to an equella role.
$shareid = $this->get_option("equella_{$role->shortname}_shareid");
if (!empty($shareid)) {
return $this->getssotoken_raw($equellauserfield, $shareid,
$this->get_option("equella_{$role->shortname}_sharedsecret"));
}
}
}
}
// If we are only reading, use the unadorned shareid and secret.
$shareid = $this->get_option('equella_shareid');
if (!empty($shareid)) {
return $this->getssotoken_raw($equellauserfield, $shareid, $this->get_option('equella_sharedsecret'));
}
}
private static function get_all_editing_roles() {
return get_roles_with_capability('moodle/course:manageactivities', CAP_ALLOW);
}
/**
* Convert moodle mimetypes list to equella format
*
* @param string $value
* @return string
*/
private static function to_mime_type($value) {
return mimeinfo('type', $value);
}
/**
* Return the source information
*
* @param string $source
* @return string|null
*/
public function get_file_source_info($source) {
$ref = json_decode(base64_decode($source));
$filename = clean_param($ref->filename, PARAM_FILE);
return 'EQUELLA: ' . $filename;
}
/**
* Return human readable reference information
* {@link stored_file::get_reference()}
*
* @param string $reference
* @param int $filestatus status of the file, 0 - ok, 666 - source missing
* @return string
*/
public function get_reference_details($reference, $filestatus = 0) {
if (!$filestatus) {
$ref = unserialize(base64_decode($reference));
return $this->get_name(). ': '. $ref->filename;
} else {
return get_string('lostsource', 'repository', '');
}
}
/**
* Is this repository accessing private data?
*
* @return bool
*/
public function contains_private_data() {
return false;
}
/**
* Retrieve the userfield/username.
*
* @return string
*/
public function get_userfield_value(): string {
global $USER;
$userfield = $this->get_option('equella_userfield');
if ($userfield != 'default' && isset($USER->profile[$userfield])) {
return $USER->profile[$userfield];
} else {
return $USER->username;
}
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 471 B

@@ -0,0 +1,75 @@
<?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/>.
/**
* Equella repository data generator
*
* @package repository_equella
* @category test
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Equella repository data generator class
*
* @package repository_equella
* @category test
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class repository_equella_generator extends testing_repository_generator {
/**
* Fill in record defaults.
*
* @param array $record
* @return array
*/
protected function prepare_record(array $record) {
$record = parent::prepare_record($record);
if (!isset($record['equella_url'])) {
$record['equella_url'] = 'http://dummy.url.com';
}
if (!isset($record['equella_userfield'])) {
$record['equella_userfield'] = 'default';
}
if (!isset($record['equella_select_restriction'])) {
$record['equella_select_restriction'] = 'none';
}
if (!isset($record['equella_options'])) {
$record['equella_options'] = '';
}
if (!isset($record['equella_shareid'])) {
$record['equella_shareid'] = 'id';
}
if (!isset($record['equella_sharedsecret'])) {
$record['equella_sharedsecret'] = 'secret';
}
// Set defaults for all generated options for each role with editing capabilities.
$editingroles = get_roles_with_capability('moodle/course:manageactivities', CAP_ALLOW);
foreach ($editingroles as $role) {
if (!isset($record["equella_{$role->shortname}_shareid"])) {
$record["equella_{$role->shortname}_shareid"] = '';
}
if (!isset($record["equella_{$role->shortname}_sharedsecret"])) {
$record["equella_{$role->shortname}_sharedsecret"] = '';
}
}
return $record;
}
}
+239
View File
@@ -0,0 +1,239 @@
<?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/>.
/**
* This file contains tests for the repository_equella class.
*
* @package repository_equella
*
* @author Guillaume BARAT <guillaumebarat@catalyst-au.net>
* @copyright Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace repository_equella;
use repository_equella;
use stdClass;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/repository/lib.php');
require_once($CFG->libdir . '/webdavlib.php');
/**
* Class repository_equella_lib_testcase
*
* @group repository_equella
* @copyright Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class lib_test extends \advanced_testcase {
/** @var null|\repository_equella the repository_equella object, which the tests are run on. */
private $repo = null;
/**
* Create some data for repository.
*
* @return stdClass
*/
private function create_new_form_data(): stdClass {
$record = new stdClass();
$record->equella_url = 'http://dummy.url.com';
$record->equella_userfield = 'default';
$record->equella_select_restriction = 'none';
$record->equella_options = '';
$record->equella_shareid = 'id';
$record->equella_sharedsecret = 'secret';
$record->equella_manager_shareid = '';
$record->equella_manager_sharedsecret = '';
$record->equella_editingteacher_shareid = '';
$record->equella_editingteacher_sharedsecret = '';
return $record;
}
/**
* Create repository for testing.
*
* @return repository_equella
*/
private function create_repository(): repository_equella {
$record = new \stdClass();
$this->getDataGenerator()->create_repository_type('equella', $record);
$generator = $this->getDataGenerator()->get_plugin_generator('repository_equella');
$instance = $generator->create_instance();
$this->repo = new repository_equella($instance->id);
return $this->repo;
}
/**
* Test that environment is created.
* @covers \repository_equella::get_repository_by_id
* @return void
*/
public function test_repository_is_created(): void {
$this->initialise_repository();
$actual = repository_equella::get_repository_by_id($this->repo->id, $this->repo->context);
$this->assertEquals($this->repo->options['equella_url'], $actual->get_option('equella_url'));
$this->assertEquals($this->repo->options['equella_userfield'], $actual->get_option('equella_userfield'));
$this->assertEquals($this->repo->options['equella_select_restriction'],
$actual->get_option('equella_select_restriction'));
$this->assertEquals($this->repo->options['equella_options'], $actual->get_option('equella_options'));
$this->assertEquals($this->repo->options['equella_shareid'], $actual->get_option('equella_shareid'));
$this->assertEquals($this->repo->options['equella_sharedsecret'], $actual->get_option('equella_sharedsecret'));
$this->assertEquals($this->repo->options['equella_manager_shareid'], $actual->get_option('equella_manager_shareid'));
$this->assertEquals($this->repo->options['equella_manager_sharedsecret'],
$actual->get_option('equella_manager_sharedsecret'));
$this->assertEquals($this->repo->options['equella_editingteacher_shareid'],
$actual->get_option('equella_editingteacher_shareid'));
$this->assertEquals($this->repo->options['equella_editingteacher_sharedsecret'],
$actual->get_option('equella_editingteacher_sharedsecret'));
$this->resetAfterTest(true);
}
/**
* Data provider for get_userfield_value.
*
* @return array
* @covers ::get_userfield_value
*/
public static function get_userfield_value_provider(): array {
return [
[
'input' => [
'userfield' => 'nickname',
'value' => 'administrator',
],
'expected' => [
'username' => 'administrator',
],
], [
'input' => [
'userfield' => 'default',
'value' => 'default',
],
'expected' => [
'username' => 'admin',
],
], [
'input' => [
'userfield' => 'test',
'value' => 'test',
],
'expected' => [
'username' => 'test',
],
],
];
}
/**
* Test method get_userfield_value.
*
* @dataProvider get_userfield_value_provider
*
* @param array $input
* @param array $expected
* @covers ::get_userfield_value
*
* @return void
*/
public function test_get_userfield_value($input, $expected): void {
global $USER;
$this->initialise_repository();
$USER->profile[$input['userfield']] = $input['value'];
$this->repo->set_option(['equella_userfield' => $input['userfield']]);
$return = $this->repo->get_userfield_value();
$this->assertEquals($expected['username'], $return);
}
/**
* Data provider for get_listing.
*
* @return array
* @covers ::get_listing
*/
public static function get_listing_provider(): array {
return [
[
'input' => [
'url' => 'http://dummy.url.com',
'userfield' => 'nickname',
'value' => 'administrator',
],
'expected' => [
'username' => 'administrator',
],
], [
'input' => [
'url' => 'http://dummy.url.com',
'userfield' => 'default',
'value' => '',
],
'expected' => [
'username' => 'admin',
],
], [
'input' => [
'url' => 'http://dummy.url.com',
'userfield' => 'test',
'value' => 'test',
],
'expected' => [
'username' => 'test',
],
],
];
}
/**
* Test that the method get_listing return the correct array.
*
* @dataProvider get_listing_provider
*
* @param array $input
* @param array $expected
* @covers ::get_listing
*
* @return void
*/
public function test_get_listing($input, $expected): void {
global $USER;
$this->initialise_repository();
$USER->profile[$input['userfield']] = $input['value'];
$this->repo->set_option(['url' => $input['url'],
'equella_userfield' => $input['userfield']]);
$listing = $this->repo->get_listing();
$this->assertArrayHasKey('manage', $listing);
$this->assertStringContainsString($expected['username'], $listing['manage']);
}
/**
* Create and initialise the repository for test.
* @return void
*/
public function initialise_repository(): void {
$this->resetAfterTest(true);
// Admin is neccessary to create repository.
$this->setAdminUser();
$this->create_repository();
$this->create_new_form_data();
}
}
+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 information for equella repository.
*
* @since Moodle 2.3
* @package repository_equella
* @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
* @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 = 'repository_equella'; // Full name of the plugin (used for diagnostics).