2008-09-05 08:51:25 +00:00
|
|
|
<?php
|
2009-11-06 09:07:46 +00:00
|
|
|
// 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/>.
|
|
|
|
|
2008-09-08 09:41:38 +00:00
|
|
|
/**
|
2009-11-06 09:07:46 +00:00
|
|
|
* This file contains classes used to manage the repository plugins in Moodle
|
|
|
|
* and was introduced as part of the changes occuring in Moodle 2.0
|
|
|
|
*
|
|
|
|
* @since 2.0
|
2012-05-12 04:14:53 +08:00
|
|
|
* @package repository
|
|
|
|
* @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2008-09-08 09:41:38 +00:00
|
|
|
*/
|
|
|
|
|
2008-07-28 08:24:55 +00:00
|
|
|
require_once(dirname(dirname(__FILE__)) . '/config.php');
|
2009-05-07 06:47:20 +00:00
|
|
|
require_once($CFG->libdir . '/filelib.php');
|
|
|
|
require_once($CFG->libdir . '/formslib.php');
|
2009-11-02 06:45:12 +00:00
|
|
|
|
2012-05-12 04:14:53 +08:00
|
|
|
define('FILE_EXTERNAL', 1);
|
|
|
|
define('FILE_INTERNAL', 2);
|
|
|
|
define('FILE_REFERENCE', 4);
|
2011-05-02 10:11:19 +08:00
|
|
|
define('RENAME_SUFFIX', '_2');
|
2009-11-02 06:45:12 +00:00
|
|
|
|
2008-09-02 08:58:53 +00:00
|
|
|
/**
|
2009-11-06 09:07:46 +00:00
|
|
|
* This class is used to manage repository plugins
|
|
|
|
*
|
2008-09-02 08:58:53 +00:00
|
|
|
* A repository_type is a repository plug-in. It can be Box.net, Flick-r, ...
|
|
|
|
* A repository type can be edited, sorted and hidden. It is mandatory for an
|
|
|
|
* administrator to create a repository type in order to be able to create
|
|
|
|
* some instances of this type.
|
|
|
|
* Coding note:
|
|
|
|
* - a repository_type object is mapped to the "repository" database table
|
|
|
|
* - "typename" attibut maps the "type" database field. It is unique.
|
|
|
|
* - general "options" for a repository type are saved in the config_plugin table
|
2008-09-04 07:24:31 +00:00
|
|
|
* - when you delete a repository, all instances are deleted, and general
|
|
|
|
* options are also deleted from database
|
2008-09-02 08:58:53 +00:00
|
|
|
* - When you create a type for a plugin that can't have multiple instances, a
|
|
|
|
* instance is automatically created.
|
2009-11-06 09:07:46 +00:00
|
|
|
*
|
2012-05-12 04:14:53 +08:00
|
|
|
* @package repository
|
2009-11-06 09:07:46 +00:00
|
|
|
* @copyright 2009 Jerome Mouneyrac
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2008-09-02 08:58:53 +00:00
|
|
|
*/
|
|
|
|
class repository_type {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Type name (no whitespace) - A type name is unique
|
|
|
|
* Note: for a user-friendly type name see get_readablename()
|
|
|
|
* @var String
|
|
|
|
*/
|
|
|
|
private $_typename;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Options of this type
|
|
|
|
* They are general options that any instance of this type would share
|
|
|
|
* e.g. API key
|
|
|
|
* These options are saved in config_plugin table
|
|
|
|
* @var array
|
|
|
|
*/
|
2008-09-15 07:56:26 +00:00
|
|
|
private $_options;
|
2008-09-02 08:58:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is the repository type visible or hidden
|
|
|
|
* If false (hidden): no instances can be created, edited, deleted, showned , used...
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2008-09-15 07:56:26 +00:00
|
|
|
private $_visible;
|
2008-09-02 08:58:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 0 => not ordered, 1 => first position, 2 => second position...
|
|
|
|
* A not order type would appear in first position (should never happened)
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
private $_sortorder;
|
|
|
|
|
2012-05-12 04:14:53 +08:00
|
|
|
/**
|
2008-10-09 03:02:26 +00:00
|
|
|
* Return if the instance is visible in a context
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
|
|
|
* @todo check if the context visibility has been overwritten by the plugin creator
|
2008-10-09 03:02:26 +00:00
|
|
|
* (need to create special functions to be overvwritten in repository class)
|
2012-05-12 04:14:53 +08:00
|
|
|
* @param stdClass $context context
|
|
|
|
* @return bool
|
2008-10-09 03:02:26 +00:00
|
|
|
*/
|
2009-12-02 10:04:04 +00:00
|
|
|
public function get_contextvisibility($context) {
|
|
|
|
global $USER;
|
2008-11-26 07:03:10 +00:00
|
|
|
|
2009-12-02 10:04:04 +00:00
|
|
|
if ($context->contextlevel == CONTEXT_COURSE) {
|
2008-10-09 03:02:26 +00:00
|
|
|
return $this->_options['enablecourseinstances'];
|
|
|
|
}
|
|
|
|
|
2009-12-02 10:04:04 +00:00
|
|
|
if ($context->contextlevel == CONTEXT_USER) {
|
2008-10-09 03:02:26 +00:00
|
|
|
return $this->_options['enableuserinstances'];
|
|
|
|
}
|
|
|
|
|
|
|
|
//the context is SITE
|
|
|
|
return true;
|
|
|
|
}
|
2008-11-26 07:03:10 +00:00
|
|
|
|
2008-10-09 03:02:26 +00:00
|
|
|
|
|
|
|
|
2008-09-02 08:58:53 +00:00
|
|
|
/**
|
|
|
|
* repository_type constructor
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
|
|
|
* @param int $typename
|
2008-09-02 08:58:53 +00:00
|
|
|
* @param array $typeoptions
|
2012-05-12 04:14:53 +08:00
|
|
|
* @param bool $visible
|
|
|
|
* @param int $sortorder (don't really need set, it will be during create() call)
|
2008-09-02 08:58:53 +00:00
|
|
|
*/
|
2010-03-04 06:47:21 +00:00
|
|
|
public function __construct($typename = '', $typeoptions = array(), $visible = true, $sortorder = 0) {
|
2008-09-02 08:58:53 +00:00
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
//set type attributs
|
|
|
|
$this->_typename = $typename;
|
|
|
|
$this->_visible = $visible;
|
|
|
|
$this->_sortorder = $sortorder;
|
2008-09-03 11:02:25 +00:00
|
|
|
|
2008-09-02 08:58:53 +00:00
|
|
|
//set options attribut
|
|
|
|
$this->_options = array();
|
2010-07-27 09:03:54 +00:00
|
|
|
$options = repository::static_function($typename, 'get_type_option_names');
|
2008-09-02 08:58:53 +00:00
|
|
|
//check that the type can be setup
|
2008-09-18 02:36:17 +00:00
|
|
|
if (!empty($options)) {
|
2008-09-02 08:58:53 +00:00
|
|
|
//set the type options
|
|
|
|
foreach ($options as $config) {
|
2010-07-27 09:03:54 +00:00
|
|
|
if (array_key_exists($config, $typeoptions)) {
|
2008-09-15 07:56:26 +00:00
|
|
|
$this->_options[$config] = $typeoptions[$config];
|
2008-09-02 08:58:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-10-09 03:02:26 +00:00
|
|
|
|
|
|
|
//retrieve visibility from option
|
|
|
|
if (array_key_exists('enablecourseinstances',$typeoptions)) {
|
|
|
|
$this->_options['enablecourseinstances'] = $typeoptions['enablecourseinstances'];
|
2008-10-09 06:01:10 +00:00
|
|
|
} else {
|
|
|
|
$this->_options['enablecourseinstances'] = 0;
|
2008-10-09 03:02:26 +00:00
|
|
|
}
|
2008-11-26 07:03:10 +00:00
|
|
|
|
2008-10-09 03:02:26 +00:00
|
|
|
if (array_key_exists('enableuserinstances',$typeoptions)) {
|
|
|
|
$this->_options['enableuserinstances'] = $typeoptions['enableuserinstances'];
|
2008-10-09 06:01:10 +00:00
|
|
|
} else {
|
|
|
|
$this->_options['enableuserinstances'] = 0;
|
2008-10-09 03:02:26 +00:00
|
|
|
}
|
2008-11-26 07:03:10 +00:00
|
|
|
|
2008-09-02 08:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the type name (no whitespace)
|
|
|
|
* For a human readable name, use get_readablename()
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
|
|
|
* @return string the type name
|
2008-09-02 08:58:53 +00:00
|
|
|
*/
|
2008-09-15 07:56:26 +00:00
|
|
|
public function get_typename() {
|
2008-09-02 08:58:53 +00:00
|
|
|
return $this->_typename;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a human readable and user-friendly type name
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
2008-09-02 08:58:53 +00:00
|
|
|
* @return string user-friendly type name
|
|
|
|
*/
|
2008-09-15 07:56:26 +00:00
|
|
|
public function get_readablename() {
|
MDL-22984 using standard plugin name string for repositories
AMOS BEGIN
MOV [repositoryname,repository_alfresco],[pluginname,repository_alfresco]
MOV [repositoryname,repository_boxnet],[pluginname,repository_boxnet]
MOV [repositoryname,repository_dropbox],[pluginname,repository_dropbox]
MOV [repositoryname,repository_filesystem],[pluginname,repository_filesystem]
MOV [repositoryname,repository_flickr],[pluginname,repository_flickr]
MOV [repositoryname,repository_flickr_public],[pluginname,repository_flickr_public]
MOV [repositoryname,repository_googledocs],[pluginname,repository_googledocs]
MOV [repositoryname,repository_local],[pluginname,repository_local]
MOV [repositoryname,repository_merlot],[pluginname,repository_merlot]
MOV [repositoryname,repository_picasa],[pluginname,repository_picasa]
MOV [repositoryname,repository_recent],[pluginname,repository_recent]
MOV [repositoryname,repository_s3],[pluginname,repository_s3]
MOV [repositoryname,repository_upload],[pluginname,repository_upload]
MOV [repositoryname,repository_url],[pluginname,repository_url]
MOV [repositoryname,repository_user],[pluginname,repository_user]
MOV [repositoryname,repository_webdav],[pluginname,repository_webdav]
MOV [repositoryname,repository_wikimedia],[pluginname,repository_wikimedia]
MOV [repositoryname,repository_youtube],[pluginname,repository_youtube]
AMOS END
2010-07-04 12:52:10 +00:00
|
|
|
return get_string('pluginname','repository_'.$this->_typename);
|
2008-09-02 08:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return general options
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
2008-09-02 08:58:53 +00:00
|
|
|
* @return array the general options
|
|
|
|
*/
|
2008-09-15 07:56:26 +00:00
|
|
|
public function get_options() {
|
2008-09-02 08:58:53 +00:00
|
|
|
return $this->_options;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return visibility
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
|
|
|
* @return bool
|
2008-09-02 08:58:53 +00:00
|
|
|
*/
|
2008-09-15 07:56:26 +00:00
|
|
|
public function get_visible() {
|
2008-09-02 08:58:53 +00:00
|
|
|
return $this->_visible;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return order / position of display in the file picker
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
|
|
|
* @return int
|
2008-09-02 08:58:53 +00:00
|
|
|
*/
|
2008-09-15 07:56:26 +00:00
|
|
|
public function get_sortorder() {
|
2008-09-02 08:58:53 +00:00
|
|
|
return $this->_sortorder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a repository type (the type name must not already exist)
|
2012-05-12 04:14:53 +08:00
|
|
|
* @param bool $silent throw exception?
|
2009-03-05 04:40:51 +00:00
|
|
|
* @return mixed return int if create successfully, return false if
|
2008-09-02 08:58:53 +00:00
|
|
|
*/
|
2009-03-05 04:40:51 +00:00
|
|
|
public function create($silent = false) {
|
2008-09-02 08:58:53 +00:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
//check that $type has been set
|
|
|
|
$timmedtype = trim($this->_typename);
|
|
|
|
if (empty($timmedtype)) {
|
2008-09-15 07:56:26 +00:00
|
|
|
throw new repository_exception('emptytype', 'repository');
|
2008-09-02 08:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//set sortorder as the last position in the list
|
2008-09-15 07:56:26 +00:00
|
|
|
if (!isset($this->_sortorder) || $this->_sortorder == 0 ) {
|
2008-09-02 08:58:53 +00:00
|
|
|
$sql = "SELECT MAX(sortorder) FROM {repository}";
|
|
|
|
$this->_sortorder = 1 + $DB->get_field_sql($sql);
|
|
|
|
}
|
|
|
|
|
|
|
|
//only create a new type if it doesn't already exist
|
|
|
|
$existingtype = $DB->get_record('repository', array('type'=>$this->_typename));
|
2008-09-15 07:56:26 +00:00
|
|
|
if (!$existingtype) {
|
|
|
|
//create the type
|
2010-09-21 08:54:01 +00:00
|
|
|
$newtype = new stdClass();
|
2008-09-15 07:56:26 +00:00
|
|
|
$newtype->type = $this->_typename;
|
|
|
|
$newtype->visible = $this->_visible;
|
|
|
|
$newtype->sortorder = $this->_sortorder;
|
2009-03-05 04:40:51 +00:00
|
|
|
$plugin_id = $DB->insert_record('repository', $newtype);
|
2008-09-15 07:56:26 +00:00
|
|
|
//save the options in DB
|
|
|
|
$this->update_options();
|
|
|
|
|
2010-07-27 09:03:54 +00:00
|
|
|
$instanceoptionnames = repository::static_function($this->_typename, 'get_instance_option_names');
|
|
|
|
|
2008-09-18 05:21:51 +00:00
|
|
|
//if the plugin type has no multiple instance (e.g. has no instance option name) so it wont
|
2008-09-15 07:56:26 +00:00
|
|
|
//be possible for the administrator to create a instance
|
|
|
|
//in this case we need to create an instance
|
2008-09-18 05:21:51 +00:00
|
|
|
if (empty($instanceoptionnames)) {
|
2008-09-15 07:56:26 +00:00
|
|
|
$instanceoptions = array();
|
2010-07-27 09:03:54 +00:00
|
|
|
if (empty($this->_options['pluginname'])) {
|
|
|
|
// when moodle trying to install some repo plugin automatically
|
|
|
|
// this option will be empty, get it from language string when display
|
|
|
|
$instanceoptions['name'] = '';
|
|
|
|
} else {
|
|
|
|
// when admin trying to add a plugin manually, he will type a name
|
|
|
|
// for it
|
|
|
|
$instanceoptions['name'] = $this->_options['pluginname'];
|
|
|
|
}
|
2008-11-26 07:03:10 +00:00
|
|
|
repository::static_function($this->_typename, 'create', $this->_typename, 0, get_system_context(), $instanceoptions);
|
2008-09-15 07:56:26 +00:00
|
|
|
}
|
2009-03-05 05:40:56 +00:00
|
|
|
//run plugin_init function
|
|
|
|
if (!repository::static_function($this->_typename, 'plugin_init')) {
|
|
|
|
if (!$silent) {
|
|
|
|
throw new repository_exception('cannotinitplugin', 'repository');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:40:51 +00:00
|
|
|
if(!empty($plugin_id)) {
|
|
|
|
// return plugin_id if create successfully
|
|
|
|
return $plugin_id;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2008-09-16 09:08:36 +00:00
|
|
|
|
2008-09-15 07:56:26 +00:00
|
|
|
} else {
|
2009-03-05 04:40:51 +00:00
|
|
|
if (!$silent) {
|
|
|
|
throw new repository_exception('existingrepository', 'repository');
|
|
|
|
}
|
|
|
|
// If plugin existed, return false, tell caller no new plugins were created.
|
|
|
|
return false;
|
2008-09-02 08:58:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update plugin options into the config_plugin table
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
2008-09-02 08:58:53 +00:00
|
|
|
* @param array $options
|
2012-05-12 04:14:53 +08:00
|
|
|
* @return bool
|
2008-09-02 08:58:53 +00:00
|
|
|
*/
|
2008-09-15 07:56:26 +00:00
|
|
|
public function update_options($options = null) {
|
2010-07-27 09:03:54 +00:00
|
|
|
global $DB;
|
|
|
|
$classname = 'repository_' . $this->_typename;
|
|
|
|
$instanceoptions = repository::static_function($this->_typename, 'get_instance_option_names');
|
|
|
|
if (empty($instanceoptions)) {
|
|
|
|
// update repository instance name if this plugin type doesn't have muliti instances
|
|
|
|
$params = array();
|
|
|
|
$params['type'] = $this->_typename;
|
|
|
|
$instances = repository::get_instances($params);
|
|
|
|
$instance = array_pop($instances);
|
|
|
|
if ($instance) {
|
|
|
|
$DB->set_field('repository_instances', 'name', $options['pluginname'], array('id'=>$instance->id));
|
|
|
|
}
|
|
|
|
unset($options['pluginname']);
|
|
|
|
}
|
|
|
|
|
2008-09-15 07:56:26 +00:00
|
|
|
if (!empty($options)) {
|
2008-09-02 08:58:53 +00:00
|
|
|
$this->_options = $options;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->_options as $name => $value) {
|
2010-07-27 09:03:54 +00:00
|
|
|
set_config($name, $value, $this->_typename);
|
2008-09-02 08:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update visible database field with the value given as parameter
|
|
|
|
* or with the visible value of this object
|
|
|
|
* This function is private.
|
|
|
|
* For public access, have a look to switch_and_update_visibility()
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
|
|
|
* @param bool $visible
|
|
|
|
* @return bool
|
2008-09-02 08:58:53 +00:00
|
|
|
*/
|
2008-09-15 07:56:26 +00:00
|
|
|
private function update_visible($visible = null) {
|
2008-09-02 08:58:53 +00:00
|
|
|
global $DB;
|
|
|
|
|
2008-09-15 07:56:26 +00:00
|
|
|
if (!empty($visible)) {
|
2008-09-02 08:58:53 +00:00
|
|
|
$this->_visible = $visible;
|
|
|
|
}
|
2008-09-15 07:56:26 +00:00
|
|
|
else if (!isset($this->_visible)) {
|
2008-09-02 08:58:53 +00:00
|
|
|
throw new repository_exception('updateemptyvisible', 'repository');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $DB->set_field('repository', 'visible', $this->_visible, array('type'=>$this->_typename));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update database sortorder field with the value given as parameter
|
|
|
|
* or with the sortorder value of this object
|
|
|
|
* This function is private.
|
|
|
|
* For public access, have a look to move_order()
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
|
|
|
* @param int $sortorder
|
|
|
|
* @return bool
|
2008-09-02 08:58:53 +00:00
|
|
|
*/
|
2008-09-15 07:56:26 +00:00
|
|
|
private function update_sortorder($sortorder = null) {
|
2008-09-02 08:58:53 +00:00
|
|
|
global $DB;
|
|
|
|
|
2008-09-15 07:56:26 +00:00
|
|
|
if (!empty($sortorder) && $sortorder!=0) {
|
2008-09-02 08:58:53 +00:00
|
|
|
$this->_sortorder = $sortorder;
|
|
|
|
}
|
|
|
|
//if sortorder is not set, we set it as the ;ast position in the list
|
2008-09-15 07:56:26 +00:00
|
|
|
else if (!isset($this->_sortorder) || $this->_sortorder == 0 ) {
|
2008-09-02 08:58:53 +00:00
|
|
|
$sql = "SELECT MAX(sortorder) FROM {repository}";
|
|
|
|
$this->_sortorder = 1 + $DB->get_field_sql($sql);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $DB->set_field('repository', 'sortorder', $this->_sortorder, array('type'=>$this->_typename));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change order of the type with its adjacent upper or downer type
|
|
|
|
* (database fields are updated)
|
|
|
|
* Algorithm details:
|
|
|
|
* 1. retrieve all types in an array. This array is sorted by sortorder,
|
|
|
|
* and the array keys start from 0 to X (incremented by 1)
|
|
|
|
* 2. switch sortorder values of this type and its adjacent type
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
2008-09-02 08:58:53 +00:00
|
|
|
* @param string $move "up" or "down"
|
|
|
|
*/
|
|
|
|
public function move_order($move) {
|
|
|
|
global $DB;
|
|
|
|
|
2008-11-26 07:03:10 +00:00
|
|
|
$types = repository::get_types(); // retrieve all types
|
2008-09-15 07:56:26 +00:00
|
|
|
|
2012-05-12 04:14:53 +08:00
|
|
|
// retrieve this type into the returned array
|
2008-09-15 07:56:26 +00:00
|
|
|
$i = 0;
|
|
|
|
while (!isset($indice) && $i<count($types)) {
|
|
|
|
if ($types[$i]->get_typename() == $this->_typename) {
|
2008-09-02 08:58:53 +00:00
|
|
|
$indice = $i;
|
|
|
|
}
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
|
2012-05-12 04:14:53 +08:00
|
|
|
// retrieve adjacent indice
|
2008-09-02 08:58:53 +00:00
|
|
|
switch ($move) {
|
|
|
|
case "up":
|
|
|
|
$adjacentindice = $indice - 1;
|
2008-09-15 07:56:26 +00:00
|
|
|
break;
|
2008-09-02 08:58:53 +00:00
|
|
|
case "down":
|
|
|
|
$adjacentindice = $indice + 1;
|
2008-09-15 07:56:26 +00:00
|
|
|
break;
|
2008-09-02 08:58:53 +00:00
|
|
|
default:
|
2008-09-15 07:56:26 +00:00
|
|
|
throw new repository_exception('movenotdefined', 'repository');
|
2008-09-02 08:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//switch sortorder of this type and the adjacent type
|
|
|
|
//TODO: we could reset sortorder for all types. This is not as good in performance term, but
|
|
|
|
//that prevent from wrong behaviour on a screwed database. As performance are not important in this particular case
|
|
|
|
//it worth to change the algo.
|
2008-09-15 07:56:26 +00:00
|
|
|
if ($adjacentindice>=0 && !empty($types[$adjacentindice])) {
|
2008-09-02 08:58:53 +00:00
|
|
|
$DB->set_field('repository', 'sortorder', $this->_sortorder, array('type'=>$types[$adjacentindice]->get_typename()));
|
|
|
|
$this->update_sortorder($types[$adjacentindice]->get_sortorder());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-04-30 08:03:32 +00:00
|
|
|
* 1. Change visibility to the value chosen
|
2008-09-02 08:58:53 +00:00
|
|
|
* 2. Update the type
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
|
|
|
* @param bool $visible
|
|
|
|
* @return bool
|
2008-09-02 08:58:53 +00:00
|
|
|
*/
|
2010-04-30 08:03:32 +00:00
|
|
|
public function update_visibility($visible = null) {
|
|
|
|
if (is_bool($visible)) {
|
|
|
|
$this->_visible = $visible;
|
|
|
|
} else {
|
|
|
|
$this->_visible = !$this->_visible;
|
|
|
|
}
|
2008-09-02 08:58:53 +00:00
|
|
|
return $this->update_visible();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2008-09-04 07:24:31 +00:00
|
|
|
* Delete a repository_type (general options are removed from config_plugin
|
|
|
|
* table, and all instances are deleted)
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
|
|
|
* @param bool $downloadcontents download external contents if exist
|
|
|
|
* @return bool
|
2008-09-02 08:58:53 +00:00
|
|
|
*/
|
2012-05-12 04:14:53 +08:00
|
|
|
public function delete($downloadcontents = false) {
|
2008-09-02 08:58:53 +00:00
|
|
|
global $DB;
|
2008-09-03 11:02:25 +00:00
|
|
|
|
|
|
|
//delete all instances of this type
|
2009-11-06 09:07:46 +00:00
|
|
|
$params = array();
|
|
|
|
$params['context'] = array();
|
|
|
|
$params['onlyvisible'] = false;
|
|
|
|
$params['type'] = $this->_typename;
|
|
|
|
$instances = repository::get_instances($params);
|
2008-09-15 07:56:26 +00:00
|
|
|
foreach ($instances as $instance) {
|
2012-05-12 04:14:53 +08:00
|
|
|
$instance->delete($downloadcontents);
|
2008-09-03 11:02:25 +00:00
|
|
|
}
|
|
|
|
|
2008-09-04 07:24:31 +00:00
|
|
|
//delete all general options
|
2008-09-15 07:56:26 +00:00
|
|
|
foreach ($this->_options as $name => $value) {
|
2008-09-04 07:24:31 +00:00
|
|
|
set_config($name, null, $this->_typename);
|
|
|
|
}
|
|
|
|
|
2012-05-12 04:14:53 +08:00
|
|
|
try {
|
|
|
|
$DB->delete_records('repository', array('type' => $this->_typename));
|
|
|
|
} catch (dml_exception $ex) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2008-09-02 08:58:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-12 04:14:53 +08:00
|
|
|
* This is the base class of the repository class.
|
2008-09-16 05:33:55 +00:00
|
|
|
*
|
2012-05-12 04:14:53 +08:00
|
|
|
* To create repository plugin, see: {@link http://docs.moodle.org/dev/Repository_plugins}
|
|
|
|
* See an example: {@link repository_boxnet}
|
2008-09-16 05:33:55 +00:00
|
|
|
*
|
2012-05-12 04:14:53 +08:00
|
|
|
* @package repository
|
|
|
|
* @category repository
|
|
|
|
* @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
|
2009-11-06 09:07:46 +00:00
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2008-09-02 08:58:53 +00:00
|
|
|
*/
|
2008-06-27 05:48:24 +00:00
|
|
|
abstract class repository {
|
2008-10-28 03:13:31 +00:00
|
|
|
// $disabled can be set to true to disable a plugin by force
|
|
|
|
// example: self::$disabled = true
|
2012-05-12 04:14:53 +08:00
|
|
|
/** @var bool force disable repository instance */
|
2008-10-28 03:13:31 +00:00
|
|
|
public $disabled = false;
|
2012-05-12 04:14:53 +08:00
|
|
|
/** @var int repository instance id */
|
2008-08-13 04:09:13 +00:00
|
|
|
public $id;
|
2012-05-12 04:14:53 +08:00
|
|
|
/** @var stdClass current context */
|
2008-08-13 04:09:13 +00:00
|
|
|
public $context;
|
2012-05-12 04:14:53 +08:00
|
|
|
/** @var array repository options */
|
2008-08-13 04:09:13 +00:00
|
|
|
public $options;
|
2012-05-12 04:14:53 +08:00
|
|
|
/** @var bool Whether or not the repository instance is editable */
|
2008-09-16 09:08:36 +00:00
|
|
|
public $readonly;
|
2012-05-12 04:14:53 +08:00
|
|
|
/** @var int return types */
|
2009-11-02 06:45:12 +00:00
|
|
|
public $returntypes;
|
2012-05-12 04:14:53 +08:00
|
|
|
/** @var stdClass repository instance database record */
|
2010-07-27 09:03:54 +00:00
|
|
|
public $instance;
|
2009-11-06 09:32:44 +00:00
|
|
|
/**
|
2012-05-12 04:14:53 +08:00
|
|
|
* Constructor
|
2009-11-06 09:32:44 +00:00
|
|
|
*
|
2012-05-12 04:14:53 +08:00
|
|
|
* @param int $repositoryid repository instance id
|
|
|
|
* @param int|stdClass $context a context id or context object
|
2010-07-05 07:27:49 +00:00
|
|
|
* @param array $options repository options
|
2012-05-12 04:14:53 +08:00
|
|
|
* @param int $readonly indicate this repo is readonly or not
|
2009-11-06 09:32:44 +00:00
|
|
|
*/
|
2010-07-05 07:27:49 +00:00
|
|
|
public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array(), $readonly = 0) {
|
2010-07-27 09:03:54 +00:00
|
|
|
global $DB;
|
2009-11-06 09:32:44 +00:00
|
|
|
$this->id = $repositoryid;
|
2010-07-19 17:44:23 +00:00
|
|
|
if (is_object($context)) {
|
2010-07-05 07:27:49 +00:00
|
|
|
$this->context = $context;
|
|
|
|
} else {
|
|
|
|
$this->context = get_context_instance_by_id($context);
|
|
|
|
}
|
2010-07-27 09:03:54 +00:00
|
|
|
$this->instance = $DB->get_record('repository_instances', array('id'=>$this->id));
|
2009-11-06 09:32:44 +00:00
|
|
|
$this->readonly = $readonly;
|
|
|
|
$this->options = array();
|
2010-07-05 07:27:49 +00:00
|
|
|
|
2009-11-06 09:32:44 +00:00
|
|
|
if (is_array($options)) {
|
2012-05-12 04:14:53 +08:00
|
|
|
// The get_option() method will get stored options in database.
|
2009-11-06 09:32:44 +00:00
|
|
|
$options = array_merge($this->get_option(), $options);
|
|
|
|
} else {
|
|
|
|
$options = $this->get_option();
|
|
|
|
}
|
|
|
|
foreach ($options as $n => $v) {
|
|
|
|
$this->options[$n] = $v;
|
|
|
|
}
|
|
|
|
$this->name = $this->get_name();
|
|
|
|
$this->returntypes = $this->supported_returntypes();
|
|
|
|
$this->super_called = true;
|
|
|
|
}
|
2008-06-25 08:09:25 +00:00
|
|
|
|
2012-05-12 04:14:53 +08:00
|
|
|
/**
|
|
|
|
* Get repository instance using repository id
|
|
|
|
*
|
|
|
|
* @param int $repositoryid repository ID
|
|
|
|
* @param stdClass|int $context context instance or context ID
|
|
|
|
* @return repository
|
|
|
|
*/
|
|
|
|
public static function get_repository_by_id($repositoryid, $context) {
|
|
|
|
global $CFG, $DB;
|
|
|
|
|
|
|
|
$sql = 'SELECT i.name, i.typeid, r.type FROM {repository} r, {repository_instances} i WHERE i.id=? AND i.typeid=r.id';
|
|
|
|
|
|
|
|
if (!$record = $DB->get_record_sql($sql, array($repositoryid))) {
|
|
|
|
throw new repository_exception('invalidrepositoryid', 'repository');
|
|
|
|
} else {
|
|
|
|
$type = $record->type;
|
|
|
|
if (file_exists($CFG->dirroot . "/repository/$type/lib.php")) {
|
|
|
|
require_once($CFG->dirroot . "/repository/$type/lib.php");
|
|
|
|
$classname = 'repository_' . $type;
|
|
|
|
$contextid = $context;
|
|
|
|
if (is_object($context)) {
|
|
|
|
$contextid = $context->id;
|
|
|
|
}
|
|
|
|
$repository = new $classname($repositoryid, $contextid, array('type'=>$type));
|
|
|
|
return $repository;
|
|
|
|
} else {
|
|
|
|
throw new moodle_exception('error');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-26 07:03:10 +00:00
|
|
|
/**
|
2010-07-05 07:27:49 +00:00
|
|
|
* Get a repository type object by a given type name.
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
|
|
|
* @static
|
2010-07-05 07:27:49 +00:00
|
|
|
* @param string $typename the repository type name
|
|
|
|
* @return repository_type|bool
|
2008-11-26 07:03:10 +00:00
|
|
|
*/
|
|
|
|
public static function get_type_by_typename($typename) {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
if (!$record = $DB->get_record('repository',array('type' => $typename))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new repository_type($typename, (array)get_config($typename), $record->visible, $record->sortorder);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-07-05 07:27:49 +00:00
|
|
|
* Get the repository type by a given repository type id.
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
|
|
|
* @static
|
2010-03-16 03:29:45 +00:00
|
|
|
* @param int $id the type id
|
|
|
|
* @return object
|
2008-11-26 07:03:10 +00:00
|
|
|
*/
|
|
|
|
public static function get_type_by_id($id) {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
if (!$record = $DB->get_record('repository',array('id' => $id))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new repository_type($record->type, (array)get_config($record->type), $record->visible, $record->sortorder);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-07-05 07:27:49 +00:00
|
|
|
* Return all repository types ordered by sortorder field
|
|
|
|
* first repository type in returnedarray[0], second repository type in returnedarray[1], ...
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @param bool $visible can return types by visiblity, return all types if null
|
2008-11-26 07:03:10 +00:00
|
|
|
* @return array Repository types
|
|
|
|
*/
|
|
|
|
public static function get_types($visible=null) {
|
2010-04-01 06:09:53 +00:00
|
|
|
global $DB, $CFG;
|
2008-11-26 07:03:10 +00:00
|
|
|
|
|
|
|
$types = array();
|
|
|
|
$params = null;
|
|
|
|
if (!empty($visible)) {
|
|
|
|
$params = array('visible' => $visible);
|
|
|
|
}
|
|
|
|
if ($records = $DB->get_records('repository',$params,'sortorder')) {
|
|
|
|
foreach($records as $type) {
|
2010-07-05 07:27:49 +00:00
|
|
|
if (file_exists($CFG->dirroot . '/repository/'. $type->type .'/lib.php')) {
|
2010-04-01 06:09:53 +00:00
|
|
|
$types[] = new repository_type($type->type, (array)get_config($type->type), $type->visible, $type->sortorder);
|
|
|
|
}
|
2008-11-26 07:03:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $types;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-07-05 07:27:49 +00:00
|
|
|
* To check if the context id is valid
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
|
|
|
* @static
|
2010-07-05 07:27:49 +00:00
|
|
|
* @param int $contextid
|
2012-05-12 04:14:53 +08:00
|
|
|
* @param stdClass $instance
|
|
|
|
* @return bool
|
2008-11-26 07:03:10 +00:00
|
|
|
*/
|
2010-07-19 17:44:23 +00:00
|
|
|
public static function check_capability($contextid, $instance) {
|
2010-07-05 07:27:49 +00:00
|
|
|
$context = get_context_instance_by_id($contextid);
|
2010-07-19 17:44:23 +00:00
|
|
|
$capability = has_capability('repository/'.$instance->type.':view', $context);
|
|
|
|
if (!$capability) {
|
|
|
|
throw new repository_exception('nopermissiontoaccess', 'repository');
|
2008-11-26 07:03:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-02 10:11:19 +08:00
|
|
|
/**
|
|
|
|
* Check if file already exists in draft area
|
|
|
|
*
|
2012-05-12 04:14:53 +08:00
|
|
|
* @static
|
2011-05-02 10:11:19 +08:00
|
|
|
* @param int $itemid
|
|
|
|
* @param string $filepath
|
|
|
|
* @param string $filename
|
2012-05-12 04:14:53 +08:00
|
|
|
* @return bool
|
2011-05-02 10:11:19 +08:00
|
|
|
*/
|
|
|
|
public static function draftfile_exists($itemid, $filepath, $filename) {
|
|
|
|
global $USER;
|
|
|
|
$fs = get_file_storage();
|
|
|
|
$usercontext = get_context_instance(CONTEXT_USER, $USER->id);
|
|
|
|
if ($fs->get_file($usercontext->id, 'user', 'draft', $itemid, $filepath, $filename)) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-06 14:07:25 +08:00
|
|
|
/**
|
|
|
|
* Parses the 'source' returned by moodle repositories and returns an instance of stored_file
|
|
|
|
*
|
|
|
|
* @param string $source
|
|
|
|
* @return stored_file|null
|
|
|
|
*/
|
|
|
|
public static function get_moodle_file($source) {
|
|
|
|
$params = unserialize(base64_decode($source));
|
|
|
|
if (empty($params) || !is_array($params)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
foreach (array('contextid', 'itemid', 'filename', 'filepath', 'component') as $key) {
|
|
|
|
if (!array_key_exists($key, $params)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$contextid = clean_param($params['contextid'], PARAM_INT);
|
|
|
|
$component = clean_param($params['component'], PARAM_COMPONENT);
|
|
|
|
$filearea = clean_param($params['filearea'], PARAM_AREA);
|
|
|
|
$itemid = clean_param($params['itemid'], PARAM_INT);
|
|
|
|
$filepath = clean_param($params['filepath'], PARAM_PATH);
|
|
|
|
$filename = clean_param($params['filename'], PARAM_FILE);
|
|
|
|
$fs = get_file_storage();
|
|
|
|
return $fs->get_file($contextid, $component, $filearea, $itemid, $filepath, $filename);
|
|
|
|
}
|
|
|
|
|
2011-05-02 10:11:19 +08:00
|
|
|
/**
|
|
|
|
* This function is used to copy a moodle file to draft area
|
|
|
|
*
|
|
|
|
* @param string $encoded The metainfo of file, it is base64 encoded php serialized data
|
2012-05-31 12:58:34 +08:00
|
|
|
* @param stdClass|array $filerecord contains itemid, filepath, filename and optionally other
|
|
|
|
* attributes of the new file
|
|
|
|
* @param int $maxbytes maximum allowed size of file, -1 if unlimited. If size of file exceeds
|
|
|
|
* the limit, the file_exception is thrown.
|
2011-05-02 10:11:19 +08:00
|
|
|
* @return array The information of file
|
|
|
|
*/
|
2012-05-31 12:58:34 +08:00
|
|
|
public function copy_to_area($encoded, $filerecord, $maxbytes = -1) {
|
|
|
|
global $USER;
|
2012-05-12 04:14:53 +08:00
|
|
|
$fs = get_file_storage();
|
|
|
|
$browser = get_file_browser();
|
2011-05-02 10:11:19 +08:00
|
|
|
|
|
|
|
if ($this->has_moodle_files() == false) {
|
2012-05-12 04:14:53 +08:00
|
|
|
throw new coding_exception('Only repository used to browse moodle files can use repository::copy_to_area()');
|
2011-05-02 10:11:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
$params = unserialize(base64_decode($encoded));
|
2012-05-31 12:58:34 +08:00
|
|
|
$user_context = context_user::instance($USER->id);
|
|
|
|
|
|
|
|
$filerecord = (array)$filerecord;
|
|
|
|
// make sure the new file will be created in user draft area
|
|
|
|
$filerecord['component'] = 'user';
|
|
|
|
$filerecord['filearea'] = 'draft';
|
|
|
|
$filerecord['contextid'] = $user_context->id;
|
|
|
|
$draftitemid = $filerecord['itemid'];
|
|
|
|
$new_filepath = $filerecord['filepath'];
|
|
|
|
$new_filename = $filerecord['filename'];
|
2011-05-02 10:11:19 +08:00
|
|
|
|
|
|
|
$contextid = clean_param($params['contextid'], PARAM_INT);
|
|
|
|
$fileitemid = clean_param($params['itemid'], PARAM_INT);
|
|
|
|
$filename = clean_param($params['filename'], PARAM_FILE);
|
|
|
|
$filepath = clean_param($params['filepath'], PARAM_PATH);;
|
2011-09-24 15:07:27 +02:00
|
|
|
$filearea = clean_param($params['filearea'], PARAM_AREA);
|
|
|
|
$component = clean_param($params['component'], PARAM_COMPONENT);
|
2011-05-02 10:11:19 +08:00
|
|
|
|
|
|
|
$context = get_context_instance_by_id($contextid);
|
|
|
|
// the file needs to copied to draft area
|
|
|
|
$file_info = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename);
|
2012-05-31 12:58:34 +08:00
|
|
|
if ($maxbytes !== -1 && $file_info->get_filesize() > $maxbytes) {
|
|
|
|
throw new file_exception('maxbytes');
|
|
|
|
}
|
2011-05-02 10:11:19 +08:00
|
|
|
|
|
|
|
if (repository::draftfile_exists($draftitemid, $new_filepath, $new_filename)) {
|
|
|
|
// create new file
|
|
|
|
$unused_filename = repository::get_unused_filename($draftitemid, $new_filepath, $new_filename);
|
2012-05-31 12:58:34 +08:00
|
|
|
$filerecord['filename'] = $unused_filename;
|
|
|
|
$file_info->copy_to_storage($filerecord);
|
2011-05-02 10:11:19 +08:00
|
|
|
$event = array();
|
|
|
|
$event['event'] = 'fileexists';
|
|
|
|
$event['newfile'] = new stdClass;
|
|
|
|
$event['newfile']->filepath = $new_filepath;
|
|
|
|
$event['newfile']->filename = $unused_filename;
|
|
|
|
$event['newfile']->url = moodle_url::make_draftfile_url($draftitemid, $new_filepath, $unused_filename)->out();
|
|
|
|
$event['existingfile'] = new stdClass;
|
|
|
|
$event['existingfile']->filepath = $new_filepath;
|
|
|
|
$event['existingfile']->filename = $new_filename;
|
2012-05-31 12:58:34 +08:00
|
|
|
$event['existingfile']->url = moodle_url::make_draftfile_url($draftitemid, $new_filepath, $new_filename)->out();;
|
2011-05-02 10:11:19 +08:00
|
|
|
return $event;
|
|
|
|
} else {
|
2012-05-31 12:58:34 +08:00
|
|
|
$file_info->copy_to_storage($filerecord);
|
2011-05-02 10:11:19 +08:00
|
|
|
$info = array();
|
|
|
|
$info['itemid'] = $draftitemid;
|
2012-05-31 12:58:34 +08:00
|
|
|
$info['file'] = $new_filename;
|
2011-05-02 10:11:19 +08:00
|
|
|
$info['title'] = $new_filename;
|
|
|
|
$info['contextid'] = $user_context->id;
|
|
|
|
$info['url'] = moodle_url::make_draftfile_url($draftitemid, $new_filepath, $new_filename)->out();;
|
|
|
|
$info['filesize'] = $file_info->get_filesize();
|
|
|
|
return $info;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get unused filename by appending suffix
|
|
|
|
*
|
2012-05-12 04:14:53 +08:00
|
|
|
* @static
|
2011-05-02 10:11:19 +08:00
|
|
|
* @param int $itemid
|
|
|
|
* @param string $filepath
|
|
|
|
* @param string $filename
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function get_unused_filename($itemid, $filepath, $filename) {
|
|
|
|
global $USER;
|
|
|
|
$fs = get_file_storage();
|
|
|
|
while (repository::draftfile_exists($itemid, $filepath, $filename)) {
|
|
|
|
$filename = repository::append_suffix($filename);
|
|
|
|
}
|
|
|
|
return $filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append a suffix to filename
|
|
|
|
*
|
2012-05-12 04:14:53 +08:00
|
|
|
* @static
|
2011-05-02 10:11:19 +08:00
|
|
|
* @param string $filename
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-04-10 14:38:30 +08:00
|
|
|
public static function append_suffix($filename) {
|
2011-05-02 10:11:19 +08:00
|
|
|
$pathinfo = pathinfo($filename);
|
|
|
|
if (empty($pathinfo['extension'])) {
|
|
|
|
return $filename . RENAME_SUFFIX;
|
|
|
|
} else {
|
|
|
|
return $pathinfo['filename'] . RENAME_SUFFIX . '.' . $pathinfo['extension'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-26 07:03:10 +00:00
|
|
|
/**
|
|
|
|
* Return all types that you a user can create/edit and which are also visible
|
|
|
|
* Note: Mostly used in order to know if at least one editable type can be set
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @param stdClass $context the context for which we want the editable types
|
2008-11-26 07:03:10 +00:00
|
|
|
* @return array types
|
|
|
|
*/
|
|
|
|
public static function get_editable_types($context = null) {
|
|
|
|
|
|
|
|
if (empty($context)) {
|
|
|
|
$context = get_system_context();
|
|
|
|
}
|
|
|
|
|
|
|
|
$types= repository::get_types(true);
|
|
|
|
$editabletypes = array();
|
|
|
|
foreach ($types as $type) {
|
|
|
|
$instanceoptionnames = repository::static_function($type->get_typename(), 'get_instance_option_names');
|
|
|
|
if (!empty($instanceoptionnames)) {
|
2009-12-02 10:04:04 +00:00
|
|
|
if ($type->get_contextvisibility($context)) {
|
2008-11-26 07:03:10 +00:00
|
|
|
$editabletypes[]=$type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $editabletypes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return repository instances
|
2010-01-28 05:12:35 +00:00
|
|
|
*
|
2012-05-12 04:14:53 +08:00
|
|
|
* @static
|
2010-01-28 05:12:35 +00:00
|
|
|
* @param array $args Array containing the following keys:
|
|
|
|
* currentcontext
|
|
|
|
* context
|
|
|
|
* onlyvisible
|
|
|
|
* type
|
|
|
|
* accepted_types
|
2010-03-16 03:29:45 +00:00
|
|
|
* return_types
|
2010-01-28 05:12:35 +00:00
|
|
|
* userid
|
|
|
|
*
|
2008-11-26 07:03:10 +00:00
|
|
|
* @return array repository instances
|
|
|
|
*/
|
2009-11-06 09:07:46 +00:00
|
|
|
public static function get_instances($args = array()) {
|
2008-11-26 07:03:10 +00:00
|
|
|
global $DB, $CFG, $USER;
|
|
|
|
|
2009-11-06 09:07:46 +00:00
|
|
|
if (isset($args['currentcontext'])) {
|
|
|
|
$current_context = $args['currentcontext'];
|
|
|
|
} else {
|
|
|
|
$current_context = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($args['context'])) {
|
|
|
|
$contexts = $args['context'];
|
|
|
|
} else {
|
|
|
|
$contexts = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$onlyvisible = isset($args['onlyvisible']) ? $args['onlyvisible'] : true;
|
2010-07-05 07:27:49 +00:00
|
|
|
$returntypes = isset($args['return_types']) ? $args['return_types'] : 3;
|
2009-11-06 09:07:46 +00:00
|
|
|
$type = isset($args['type']) ? $args['type'] : null;
|
|
|
|
|
2008-11-26 07:03:10 +00:00
|
|
|
$params = array();
|
2010-07-03 14:33:48 +00:00
|
|
|
$sql = "SELECT i.*, r.type AS repositorytype, r.sortorder, r.visible
|
|
|
|
FROM {repository} r, {repository_instances} i
|
|
|
|
WHERE i.typeid = r.id ";
|
2008-11-26 07:03:10 +00:00
|
|
|
|
2010-05-31 06:57:43 +00:00
|
|
|
if (!empty($args['disable_types']) && is_array($args['disable_types'])) {
|
2011-04-14 15:13:28 +02:00
|
|
|
list($types, $p) = $DB->get_in_or_equal($args['disable_types'], SQL_PARAMS_QM, 'param', false);
|
2010-07-03 14:33:48 +00:00
|
|
|
$sql .= " AND r.type $types";
|
2010-05-31 06:57:43 +00:00
|
|
|
$params = array_merge($params, $p);
|
|
|
|
}
|
|
|
|
|
2009-11-06 09:07:46 +00:00
|
|
|
if (!empty($args['userid']) && is_numeric($args['userid'])) {
|
2010-07-03 14:33:48 +00:00
|
|
|
$sql .= " AND (i.userid = 0 or i.userid = ?)";
|
2009-11-06 09:07:46 +00:00
|
|
|
$params[] = $args['userid'];
|
2008-11-26 07:03:10 +00:00
|
|
|
}
|
2010-05-31 06:57:43 +00:00
|
|
|
|
2008-11-26 07:03:10 +00:00
|
|
|
foreach ($contexts as $context) {
|
|
|
|
if (empty($firstcontext)) {
|
|
|
|
$firstcontext = true;
|
2010-07-03 14:33:48 +00:00
|
|
|
$sql .= " AND ((i.contextid = ?)";
|
2008-11-26 07:03:10 +00:00
|
|
|
} else {
|
2010-07-03 14:33:48 +00:00
|
|
|
$sql .= " OR (i.contextid = ?)";
|
2008-11-26 07:03:10 +00:00
|
|
|
}
|
|
|
|
$params[] = $context->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($firstcontext)) {
|
|
|
|
$sql .=')';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($onlyvisible == true) {
|
2010-07-03 14:33:48 +00:00
|
|
|
$sql .= " AND (r.visible = 1)";
|
2008-11-26 07:03:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($type)) {
|
2010-07-03 14:33:48 +00:00
|
|
|
$sql .= " AND (r.type = ?)";
|
2008-11-26 07:03:10 +00:00
|
|
|
$params[] = $type;
|
|
|
|
}
|
2010-07-03 14:33:48 +00:00
|
|
|
$sql .= " ORDER BY r.sortorder, i.name";
|
2009-08-10 05:49:18 +00:00
|
|
|
|
2009-11-06 09:07:46 +00:00
|
|
|
if (!$records = $DB->get_records_sql($sql, $params)) {
|
|
|
|
$records = array();
|
2008-11-26 07:03:10 +00:00
|
|
|
}
|
|
|
|
|
2009-11-06 09:07:46 +00:00
|
|
|
$repositories = array();
|
2010-01-28 06:59:34 +00:00
|
|
|
if (isset($args['accepted_types'])) {
|
|
|
|
$accepted_types = $args['accepted_types'];
|
|
|
|
} else {
|
|
|
|
$accepted_types = '*';
|
|
|
|
}
|
2012-02-07 20:56:04 +00:00
|
|
|
// Sortorder should be unique, which is not true if we use $record->sortorder
|
|
|
|
// and there are multiple instances of any repository type
|
|
|
|
$sortorder = 1;
|
2009-11-06 09:07:46 +00:00
|
|
|
foreach ($records as $record) {
|
2010-07-05 07:27:49 +00:00
|
|
|
if (!file_exists($CFG->dirroot . '/repository/'. $record->repositorytype.'/lib.php')) {
|
2009-12-15 08:09:00 +00:00
|
|
|
continue;
|
|
|
|
}
|
2010-07-05 07:27:49 +00:00
|
|
|
require_once($CFG->dirroot . '/repository/'. $record->repositorytype.'/lib.php');
|
2009-11-06 09:07:46 +00:00
|
|
|
$options['visible'] = $record->visible;
|
|
|
|
$options['type'] = $record->repositorytype;
|
|
|
|
$options['typeid'] = $record->typeid;
|
2012-02-07 20:56:04 +00:00
|
|
|
$options['sortorder'] = $sortorder++;
|
2008-12-09 02:11:57 +00:00
|
|
|
// tell instance what file types will be accepted by file picker
|
2009-11-06 09:07:46 +00:00
|
|
|
$classname = 'repository_' . $record->repositorytype;
|
|
|
|
|
|
|
|
$repository = new $classname($record->id, $record->contextid, $options, $record->readonly);
|
|
|
|
|
2008-12-08 05:19:09 +00:00
|
|
|
$is_supported = true;
|
2008-11-26 07:03:10 +00:00
|
|
|
|
2009-01-08 06:10:29 +00:00
|
|
|
if (empty($repository->super_called)) {
|
2009-11-06 09:07:46 +00:00
|
|
|
// to make sure the super construct is called
|
|
|
|
debugging('parent::__construct must be called by '.$record->repositorytype.' plugin.');
|
2009-01-08 06:10:29 +00:00
|
|
|
} else {
|
2009-11-06 09:07:46 +00:00
|
|
|
// check mimetypes
|
2010-01-15 07:48:38 +00:00
|
|
|
if ($accepted_types !== '*' and $repository->supported_filetypes() !== '*') {
|
2012-05-21 15:17:53 +08:00
|
|
|
$accepted_ext = file_get_typegroup('extension', $accepted_types);
|
|
|
|
$supported_ext = file_get_typegroup('extension', $repository->supported_filetypes());
|
|
|
|
$valid_ext = array_intersect($accepted_ext, $supported_ext);
|
|
|
|
$is_supported = !empty($valid_ext);
|
2008-12-08 05:19:09 +00:00
|
|
|
}
|
2009-11-06 09:07:46 +00:00
|
|
|
// check return values
|
2009-11-02 06:45:12 +00:00
|
|
|
if ($returntypes !== 3 and $repository->supported_returntypes() !== 3) {
|
|
|
|
$type = $repository->supported_returntypes();
|
|
|
|
if ($type & $returntypes) {
|
|
|
|
//
|
|
|
|
} else {
|
|
|
|
$is_supported = false;
|
2009-01-08 06:10:29 +00:00
|
|
|
}
|
2008-12-08 05:19:09 +00:00
|
|
|
}
|
2010-08-31 03:39:33 +00:00
|
|
|
|
2009-01-08 06:10:29 +00:00
|
|
|
if (!$onlyvisible || ($repository->is_visible() && !$repository->disabled)) {
|
2009-11-06 09:07:46 +00:00
|
|
|
// check capability in current context
|
|
|
|
if (!empty($current_context)) {
|
|
|
|
$capability = has_capability('repository/'.$record->repositorytype.':view', $current_context);
|
|
|
|
} else {
|
|
|
|
$capability = has_capability('repository/'.$record->repositorytype.':view', get_system_context());
|
|
|
|
}
|
2010-08-31 03:39:33 +00:00
|
|
|
if ($record->repositorytype == 'coursefiles') {
|
|
|
|
// coursefiles plugin needs managefiles permission
|
2012-01-14 21:57:01 +01:00
|
|
|
if (!empty($current_context)) {
|
|
|
|
$capability = $capability && has_capability('moodle/course:managefiles', $current_context);
|
|
|
|
} else {
|
|
|
|
$capability = $capability && has_capability('moodle/course:managefiles', get_system_context());
|
|
|
|
}
|
2010-08-31 03:39:33 +00:00
|
|
|
}
|
2009-08-07 10:02:47 +00:00
|
|
|
if ($is_supported && $capability) {
|
2010-01-15 07:48:38 +00:00
|
|
|
$repositories[$repository->id] = $repository;
|
2009-01-08 06:10:29 +00:00
|
|
|
}
|
2008-12-08 05:19:09 +00:00
|
|
|
}
|
2008-11-26 07:03:10 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-06 09:07:46 +00:00
|
|
|
return $repositories;
|
2008-11-26 07:03:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get single repository instance
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
|
|
|
* @static
|
2008-11-26 07:03:10 +00:00
|
|
|
* @param integer $id repository id
|
|
|
|
* @return object repository instance
|
|
|
|
*/
|
|
|
|
public static function get_instance($id) {
|
|
|
|
global $DB, $CFG;
|
2010-07-03 14:33:48 +00:00
|
|
|
$sql = "SELECT i.*, r.type AS repositorytype, r.visible
|
|
|
|
FROM {repository} r
|
|
|
|
JOIN {repository_instances} i ON i.typeid = r.id
|
|
|
|
WHERE i.id = ?";
|
2008-11-26 07:03:10 +00:00
|
|
|
|
2010-07-03 14:33:48 +00:00
|
|
|
if (!$instance = $DB->get_record_sql($sql, array($id))) {
|
2008-11-26 07:03:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-07-05 07:27:49 +00:00
|
|
|
require_once($CFG->dirroot . '/repository/'. $instance->repositorytype.'/lib.php');
|
2008-11-26 07:03:10 +00:00
|
|
|
$classname = 'repository_' . $instance->repositorytype;
|
|
|
|
$options['typeid'] = $instance->typeid;
|
|
|
|
$options['type'] = $instance->repositorytype;
|
|
|
|
$options['name'] = $instance->name;
|
2009-01-08 06:10:29 +00:00
|
|
|
$obj = new $classname($instance->id, $instance->contextid, $options, $instance->readonly);
|
|
|
|
if (empty($obj->super_called)) {
|
|
|
|
debugging('parent::__construct must be called by '.$classname.' plugin.');
|
|
|
|
}
|
|
|
|
return $obj;
|
2008-11-26 07:03:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-07-05 07:27:49 +00:00
|
|
|
* Call a static function. Any additional arguments than plugin and function will be passed through.
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @param string $plugin repository plugin name
|
|
|
|
* @param string $function funciton name
|
2009-04-21 10:02:59 +00:00
|
|
|
* @return mixed
|
2008-11-26 07:03:10 +00:00
|
|
|
*/
|
|
|
|
public static function static_function($plugin, $function) {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
//check that the plugin exists
|
2010-07-05 07:27:49 +00:00
|
|
|
$typedirectory = $CFG->dirroot . '/repository/'. $plugin . '/lib.php';
|
2008-11-26 07:03:10 +00:00
|
|
|
if (!file_exists($typedirectory)) {
|
2008-12-11 03:19:46 +00:00
|
|
|
//throw new repository_exception('invalidplugin', 'repository');
|
|
|
|
return false;
|
2008-11-26 07:03:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$pname = null;
|
|
|
|
if (is_object($plugin) || is_array($plugin)) {
|
|
|
|
$plugin = (object)$plugin;
|
|
|
|
$pname = $plugin->name;
|
|
|
|
} else {
|
|
|
|
$pname = $plugin;
|
|
|
|
}
|
|
|
|
|
|
|
|
$args = func_get_args();
|
|
|
|
if (count($args) <= 2) {
|
|
|
|
$args = array();
|
2010-07-05 07:27:49 +00:00
|
|
|
} else {
|
2008-11-26 07:03:10 +00:00
|
|
|
array_shift($args);
|
|
|
|
array_shift($args);
|
|
|
|
}
|
|
|
|
|
|
|
|
require_once($typedirectory);
|
2010-03-04 09:28:17 +00:00
|
|
|
return call_user_func_array(array('repository_' . $plugin, $function), $args);
|
2008-11-26 07:03:10 +00:00
|
|
|
}
|
|
|
|
|
2011-08-07 11:26:03 +02:00
|
|
|
/**
|
|
|
|
* Scan file, throws exception in case of infected file.
|
|
|
|
*
|
|
|
|
* Please note that the scanning engine must be able to access the file,
|
|
|
|
* permissions of the file are not modified here!
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @param string $thefile
|
|
|
|
* @param string $filename name of the file
|
|
|
|
* @param bool $deleteinfected
|
|
|
|
*/
|
|
|
|
public static function antivir_scan_file($thefile, $filename, $deleteinfected) {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
if (!is_readable($thefile)) {
|
|
|
|
// this should not happen
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($CFG->runclamonupload) or empty($CFG->pathtoclam)) {
|
|
|
|
// clam not enabled
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$CFG->pathtoclam = trim($CFG->pathtoclam);
|
|
|
|
|
|
|
|
if (!file_exists($CFG->pathtoclam) or !is_executable($CFG->pathtoclam)) {
|
|
|
|
// misconfigured clam - use the old notification for now
|
|
|
|
require("$CFG->libdir/uploadlib.php");
|
|
|
|
$notice = get_string('clamlost', 'moodle', $CFG->pathtoclam);
|
|
|
|
clam_message_admins($notice);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// do NOT mess with permissions here, the calling party is responsible for making
|
|
|
|
// sure the scanner engine can access the files!
|
|
|
|
|
|
|
|
// execute test
|
|
|
|
$cmd = escapeshellcmd($CFG->pathtoclam).' --stdout '.escapeshellarg($thefile);
|
|
|
|
exec($cmd, $output, $return);
|
|
|
|
|
|
|
|
if ($return == 0) {
|
|
|
|
// perfect, no problem found
|
|
|
|
return;
|
|
|
|
|
|
|
|
} else if ($return == 1) {
|
|
|
|
// infection found
|
|
|
|
if ($deleteinfected) {
|
|
|
|
unlink($thefile);
|
|
|
|
}
|
|
|
|
throw new moodle_exception('virusfounduser', 'moodle', '', array('filename'=>$filename));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
//unknown problem
|
|
|
|
require("$CFG->libdir/uploadlib.php");
|
|
|
|
$notice = get_string('clamfailed', 'moodle', get_clam_error_code($return));
|
|
|
|
$notice .= "\n\n". implode("\n", $output);
|
|
|
|
clam_message_admins($notice);
|
|
|
|
if ($CFG->clamfailureonupload === 'actlikevirus') {
|
|
|
|
if ($deleteinfected) {
|
|
|
|
unlink($thefile);
|
|
|
|
}
|
|
|
|
throw new moodle_exception('virusfounduser', 'moodle', '', array('filename'=>$filename));
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-12 04:14:53 +08:00
|
|
|
/**
|
2012-06-13 12:11:06 +08:00
|
|
|
* Repository method to serve the referenced file
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
2012-06-13 12:11:06 +08:00
|
|
|
* @see send_stored_file
|
|
|
|
*
|
|
|
|
* @param stored_file $storedfile the file that contains the reference
|
2012-05-12 04:14:53 +08:00
|
|
|
* @param int $lifetime Number of seconds before the file should expire from caches (default 24 hours)
|
|
|
|
* @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($storedfile, $lifetime=86400 , $filter=0, $forcedownload=false, array $options = null) {
|
2012-06-13 12:11:06 +08:00
|
|
|
if ($this->has_moodle_files()) {
|
|
|
|
$fs = get_file_storage();
|
|
|
|
$params = file_storage::unpack_reference($storedfile->get_reference(), true);
|
|
|
|
$srcfile = null;
|
|
|
|
if (is_array($params)) {
|
|
|
|
$srcfile = $fs->get_file($params['contextid'], $params['component'], $params['filearea'],
|
|
|
|
$params['itemid'], $params['filepath'], $params['filename']);
|
|
|
|
}
|
|
|
|
if (empty($options)) {
|
|
|
|
$options = array();
|
|
|
|
}
|
|
|
|
if (!isset($options['filename'])) {
|
|
|
|
$options['filename'] = $storedfile->get_filename();
|
|
|
|
}
|
|
|
|
if (!$srcfile) {
|
|
|
|
send_file_not_found();
|
|
|
|
} else {
|
|
|
|
send_stored_file($srcfile, $lifetime, $filter, $forcedownload, $options);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new coding_exception("Repository plugin must implement send_file() method.");
|
|
|
|
}
|
2012-05-12 04:14:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return reference file life time
|
|
|
|
*
|
|
|
|
* @param string $ref
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function get_reference_file_lifetime($ref) {
|
|
|
|
// One day
|
|
|
|
return 60 * 60 * 24;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decide whether or not the file should be synced
|
|
|
|
*
|
|
|
|
* @param stored_file $storedfile
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function sync_individual_file(stored_file $storedfile) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return human readable reference information
|
|
|
|
* {@link stored_file::get_reference()}
|
|
|
|
*
|
|
|
|
* @param string $reference
|
2012-06-13 12:11:06 +08:00
|
|
|
* @param int $filestatus status of the file, 0 - ok, 666 - source missing
|
|
|
|
* @return string
|
2012-05-12 04:14:53 +08:00
|
|
|
*/
|
2012-06-13 12:11:06 +08:00
|
|
|
public function get_reference_details($reference, $filestatus = 0) {
|
|
|
|
if ($this->has_moodle_files()) {
|
|
|
|
$fileinfo = null;
|
|
|
|
$params = file_storage::unpack_reference($reference, true);
|
|
|
|
if (is_array($params)) {
|
|
|
|
$context = get_context_instance_by_id($params['contextid']);
|
|
|
|
if ($context) {
|
|
|
|
$browser = get_file_browser();
|
|
|
|
$fileinfo = $browser->get_file_info($context, $params['component'], $params['filearea'], $params['itemid'], $params['filepath'], $params['filename']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (empty($fileinfo)) {
|
|
|
|
if ($filestatus == 666) {
|
|
|
|
if (is_siteadmin() || ($context && has_capability('moodle/course:managefiles', $context))) {
|
|
|
|
return get_string('lostsource', 'repository',
|
|
|
|
$params['contextid']. '/'. $params['component']. '/'. $params['filearea']. '/'. $params['itemid']. $params['filepath']. $params['filename']);
|
|
|
|
} else {
|
|
|
|
return get_string('lostsource', 'repository', '');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return get_string('undisclosedsource', 'repository');
|
|
|
|
} else {
|
|
|
|
return $fileinfo->get_readable_fullname();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return '';
|
2012-05-12 04:14:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache file from external repository by reference
|
|
|
|
* {@link repository::get_file_reference()}
|
|
|
|
* {@link repository::get_file()}
|
|
|
|
* Invoked at MOODLE/repository/repository_ajax.php
|
|
|
|
*
|
|
|
|
* @param string $reference this reference is generated by
|
|
|
|
* repository::get_file_reference()
|
|
|
|
* @param stored_file $storedfile created file reference
|
|
|
|
*/
|
2012-05-14 11:53:18 +08:00
|
|
|
public function cache_file_by_reference($reference, $storedfile) {
|
2012-05-12 04:14:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-06-13 12:11:06 +08:00
|
|
|
* Returns information about file in this repository by reference
|
2012-05-12 04:14:53 +08:00
|
|
|
* {@link repository::get_file_reference()}
|
|
|
|
* {@link repository::get_file()}
|
|
|
|
*
|
2012-06-13 12:11:06 +08:00
|
|
|
* Returns null if file not found or is not readable
|
|
|
|
*
|
2012-05-12 04:14:53 +08:00
|
|
|
* @param stdClass $reference file reference db record
|
2012-06-13 12:11:06 +08:00
|
|
|
* @return stdClass|null contains one of the following:
|
|
|
|
* - 'contenthash' and 'filesize'
|
|
|
|
* - 'filepath'
|
|
|
|
* - 'handle'
|
|
|
|
* - 'content'
|
2012-05-12 04:14:53 +08:00
|
|
|
*/
|
|
|
|
public function get_file_by_reference($reference) {
|
2012-06-13 12:11:06 +08:00
|
|
|
if ($this->has_moodle_files() && isset($reference->reference)) {
|
|
|
|
$fs = get_file_storage();
|
|
|
|
$params = file_storage::unpack_reference($reference->reference, true);
|
|
|
|
if (!is_array($params) || !($storedfile = $fs->get_file($params['contextid'],
|
|
|
|
$params['component'], $params['filearea'], $params['itemid'], $params['filepath'],
|
|
|
|
$params['filename']))) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (object)array(
|
|
|
|
'contenthash' => $storedfile->get_contenthash(),
|
|
|
|
'filesize' => $storedfile->get_filesize()
|
|
|
|
);
|
|
|
|
}
|
2012-05-12 04:14:53 +08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2012-06-10 17:47:00 +10:00
|
|
|
/**
|
|
|
|
* Return the source information
|
|
|
|
*
|
|
|
|
* @param stdClass $url
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function get_file_source_info($url) {
|
2012-06-20 16:09:37 +08:00
|
|
|
if ($this->has_moodle_files()) {
|
|
|
|
return $this->get_reference_details($url, 0);
|
|
|
|
}
|
2012-06-10 17:47:00 +10:00
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
2008-11-26 07:03:10 +00:00
|
|
|
/**
|
|
|
|
* Move file from download folder to file pool using FILE API
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
|
|
|
* @todo MDL-28637
|
|
|
|
* @static
|
2010-03-16 03:29:45 +00:00
|
|
|
* @param string $thefile file path in download folder
|
2012-05-12 04:14:53 +08:00
|
|
|
* @param stdClass $record
|
2010-01-29 05:50:40 +00:00
|
|
|
* @return array containing the following keys:
|
|
|
|
* icon
|
|
|
|
* file
|
|
|
|
* id
|
|
|
|
* url
|
2008-11-26 07:03:10 +00:00
|
|
|
*/
|
2010-03-25 07:54:19 +00:00
|
|
|
public static function move_to_filepool($thefile, $record) {
|
2009-06-30 02:12:16 +00:00
|
|
|
global $DB, $CFG, $USER, $OUTPUT;
|
2011-08-07 11:26:03 +02:00
|
|
|
|
|
|
|
// scan for viruses if possible, throws exception if problem found
|
|
|
|
self::antivir_scan_file($thefile, $record->filename, empty($CFG->repository_no_delete)); //TODO: MDL-28637 this repository_no_delete is a bloody hack!
|
|
|
|
|
2008-11-26 07:03:10 +00:00
|
|
|
$fs = get_file_storage();
|
2012-05-12 04:14:53 +08:00
|
|
|
// If file name being used.
|
|
|
|
if (repository::draftfile_exists($record->itemid, $record->filepath, $record->filename)) {
|
2011-05-02 10:11:19 +08:00
|
|
|
$draftitemid = $record->itemid;
|
|
|
|
$new_filename = repository::get_unused_filename($draftitemid, $record->filepath, $record->filename);
|
|
|
|
$old_filename = $record->filename;
|
2012-05-12 04:14:53 +08:00
|
|
|
// Create a tmp file.
|
2011-05-02 10:11:19 +08:00
|
|
|
$record->filename = $new_filename;
|
|
|
|
$newfile = $fs->create_file_from_pathname($record, $thefile);
|
|
|
|
$event = array();
|
|
|
|
$event['event'] = 'fileexists';
|
|
|
|
$event['newfile'] = new stdClass;
|
|
|
|
$event['newfile']->filepath = $record->filepath;
|
|
|
|
$event['newfile']->filename = $new_filename;
|
|
|
|
$event['newfile']->url = moodle_url::make_draftfile_url($draftitemid, $record->filepath, $new_filename)->out();
|
|
|
|
|
|
|
|
$event['existingfile'] = new stdClass;
|
|
|
|
$event['existingfile']->filepath = $record->filepath;
|
|
|
|
$event['existingfile']->filename = $old_filename;
|
|
|
|
$event['existingfile']->url = moodle_url::make_draftfile_url($draftitemid, $record->filepath, $old_filename)->out();;
|
|
|
|
return $event;
|
2008-12-02 07:05:15 +00:00
|
|
|
}
|
2010-03-25 07:54:19 +00:00
|
|
|
if ($file = $fs->create_file_from_pathname($record, $thefile)) {
|
2009-03-13 02:52:12 +00:00
|
|
|
if (empty($CFG->repository_no_delete)) {
|
2010-01-15 07:48:38 +00:00
|
|
|
$delete = unlink($thefile);
|
2009-03-13 02:52:12 +00:00
|
|
|
unset($CFG->repository_no_delete);
|
|
|
|
}
|
2010-07-03 13:37:13 +00:00
|
|
|
return array(
|
2010-07-11 13:30:33 +00:00
|
|
|
'url'=>moodle_url::make_draftfile_url($file->get_itemid(), $file->get_filepath(), $file->get_filename())->out(),
|
2010-07-03 13:37:13 +00:00
|
|
|
'id'=>$file->get_itemid(),
|
|
|
|
'file'=>$file->get_filename(),
|
|
|
|
'icon' => $OUTPUT->pix_url(file_extension_icon($thefile, 32))->out(),
|
|
|
|
);
|
2008-11-26 07:03:10 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-12 04:14:53 +08:00
|
|
|
* Builds a tree of files This function is then called recursively.
|
2008-11-26 07:03:10 +00:00
|
|
|
*
|
2012-05-12 04:14:53 +08:00
|
|
|
* @static
|
|
|
|
* @todo take $search into account, and respect a threshold for dynamic loading
|
|
|
|
* @param file_info $fileinfo an object returned by file_browser::get_file_info()
|
|
|
|
* @param string $search searched string
|
|
|
|
* @param bool $dynamicmode no recursive call is done when in dynamic mode
|
|
|
|
* @param array $list the array containing the files under the passed $fileinfo
|
2008-11-26 07:03:10 +00:00
|
|
|
* @returns int the number of files found
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function build_tree($fileinfo, $search, $dynamicmode, &$list) {
|
2009-06-30 02:12:16 +00:00
|
|
|
global $CFG, $OUTPUT;
|
2008-11-26 07:03:10 +00:00
|
|
|
|
|
|
|
$filecount = 0;
|
|
|
|
$children = $fileinfo->get_children();
|
|
|
|
|
|
|
|
foreach ($children as $child) {
|
|
|
|
$filename = $child->get_visible_name();
|
|
|
|
$filesize = $child->get_filesize();
|
|
|
|
$filesize = $filesize ? display_size($filesize) : '';
|
|
|
|
$filedate = $child->get_timemodified();
|
|
|
|
$filedate = $filedate ? userdate($filedate) : '';
|
|
|
|
$filetype = $child->get_mimetype();
|
|
|
|
|
|
|
|
if ($child->is_directory()) {
|
|
|
|
$path = array();
|
|
|
|
$level = $child->get_parent();
|
|
|
|
while ($level) {
|
|
|
|
$params = $level->get_params();
|
2010-07-03 13:37:13 +00:00
|
|
|
$path[] = array($params['filepath'], $level->get_visible_name());
|
2008-11-26 07:03:10 +00:00
|
|
|
$level = $level->get_parent();
|
|
|
|
}
|
|
|
|
|
|
|
|
$tmp = array(
|
|
|
|
'title' => $child->get_visible_name(),
|
|
|
|
'size' => 0,
|
|
|
|
'date' => $filedate,
|
|
|
|
'path' => array_reverse($path),
|
2012-05-21 15:17:53 +08:00
|
|
|
'thumbnail' => $OUTPUT->pix_url(file_folder_icon(90))->out(false)
|
2008-11-26 07:03:10 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
//if ($dynamicmode && $child->is_writable()) {
|
|
|
|
// $tmp['children'] = array();
|
|
|
|
//} else {
|
|
|
|
// if folder name matches search, we send back all files contained.
|
|
|
|
$_search = $search;
|
|
|
|
if ($search && stristr($tmp['title'], $search) !== false) {
|
|
|
|
$_search = false;
|
|
|
|
}
|
|
|
|
$tmp['children'] = array();
|
|
|
|
$_filecount = repository::build_tree($child, $_search, $dynamicmode, $tmp['children']);
|
|
|
|
if ($search && $_filecount) {
|
|
|
|
$tmp['expanded'] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
2009-04-30 09:42:24 +00:00
|
|
|
if (!$search || $_filecount || (stristr($tmp['title'], $search) !== false)) {
|
2008-11-26 07:03:10 +00:00
|
|
|
$filecount += $_filecount;
|
|
|
|
$list[] = $tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else { // not a directory
|
|
|
|
// skip the file, if we're in search mode and it's not a match
|
|
|
|
if ($search && (stristr($filename, $search) === false)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$params = $child->get_params();
|
2010-07-03 13:37:13 +00:00
|
|
|
$source = serialize(array($params['contextid'], $params['component'], $params['filearea'], $params['itemid'], $params['filepath'], $params['filename']));
|
2008-11-26 07:03:10 +00:00
|
|
|
$list[] = array(
|
|
|
|
'title' => $filename,
|
|
|
|
'size' => $filesize,
|
|
|
|
'date' => $filedate,
|
|
|
|
//'source' => $child->get_url(),
|
|
|
|
'source' => base64_encode($source),
|
2012-05-21 15:17:53 +08:00
|
|
|
'icon'=>$OUTPUT->pix_url(file_file_icon($child, 24))->out(false),
|
|
|
|
'thumbnail'=>$OUTPUT->pix_url(file_file_icon($child, 90))->out(false),
|
2008-11-26 07:03:10 +00:00
|
|
|
);
|
|
|
|
$filecount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $filecount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a repository instance list (with edit/delete/create links)
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @param stdClass $context the context for which we display the instance
|
2008-11-26 07:03:10 +00:00
|
|
|
* @param string $typename if set, we display only one type of instance
|
|
|
|
*/
|
|
|
|
public static function display_instances_list($context, $typename = null) {
|
2009-08-10 05:49:18 +00:00
|
|
|
global $CFG, $USER, $OUTPUT;
|
2008-11-26 07:03:10 +00:00
|
|
|
|
2009-08-10 05:49:18 +00:00
|
|
|
$output = $OUTPUT->box_start('generalbox');
|
2008-11-26 07:03:10 +00:00
|
|
|
//if the context is SYSTEM, so we call it from administration page
|
|
|
|
$admin = ($context->id == SYSCONTEXTID) ? true : false;
|
|
|
|
if ($admin) {
|
2010-07-05 07:27:49 +00:00
|
|
|
$baseurl = new moodle_url('/'.$CFG->admin.'/repositoryinstance.php', array('sesskey'=>sesskey()));
|
|
|
|
$output .= $OUTPUT->heading(get_string('siteinstances', 'repository'));
|
2008-11-26 07:03:10 +00:00
|
|
|
} else {
|
2010-07-05 07:27:49 +00:00
|
|
|
$baseurl = new moodle_url('/repository/manage_instances.php', array('contextid'=>$context->id, 'sesskey'=>sesskey()));
|
2008-11-26 07:03:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$namestr = get_string('name');
|
|
|
|
$pluginstr = get_string('plugin', 'repository');
|
|
|
|
$settingsstr = get_string('settings');
|
|
|
|
$deletestr = get_string('delete');
|
|
|
|
//retrieve list of instances. In administration context we want to display all
|
|
|
|
//instances of a type, even if this type is not visible. In course/user context we
|
2009-10-07 10:16:45 +00:00
|
|
|
//want to display only visible instances, but for every type types. The repository::get_instances()
|
2008-11-26 07:03:10 +00:00
|
|
|
//third parameter displays only visible type.
|
2009-11-06 09:07:46 +00:00
|
|
|
$params = array();
|
|
|
|
$params['context'] = array($context, get_system_context());
|
|
|
|
$params['currentcontext'] = $context;
|
|
|
|
$params['onlyvisible'] = !$admin;
|
|
|
|
$params['type'] = $typename;
|
|
|
|
$instances = repository::get_instances($params);
|
2008-11-26 07:03:10 +00:00
|
|
|
$instancesnumber = count($instances);
|
|
|
|
$alreadyplugins = array();
|
|
|
|
|
2009-08-20 08:49:23 +00:00
|
|
|
$table = new html_table();
|
2010-04-30 08:03:32 +00:00
|
|
|
$table->head = array($namestr, $pluginstr, $settingsstr, $deletestr);
|
2008-11-26 07:03:10 +00:00
|
|
|
$table->align = array('left', 'left', 'center','center');
|
|
|
|
$table->data = array();
|
|
|
|
|
|
|
|
$updowncount = 1;
|
|
|
|
|
|
|
|
foreach ($instances as $i) {
|
|
|
|
$settings = '';
|
|
|
|
$delete = '';
|
2009-12-02 10:04:04 +00:00
|
|
|
|
|
|
|
$type = repository::get_type_by_id($i->options['typeid']);
|
|
|
|
|
|
|
|
if ($type->get_contextvisibility($context)) {
|
|
|
|
if (!$i->readonly) {
|
|
|
|
|
2012-05-12 04:14:53 +08:00
|
|
|
$settingurl = new moodle_url($baseurl);
|
|
|
|
$settingurl->param('type', $i->options['type']);
|
|
|
|
$settingurl->param('edit', $i->id);
|
|
|
|
$settings .= html_writer::link($settingurl, $settingsstr);
|
2009-12-02 10:04:04 +00:00
|
|
|
|
2012-05-12 04:14:53 +08:00
|
|
|
$deleteurl = new moodle_url($baseurl);
|
|
|
|
$deleteurl->param('delete', $i->id);
|
|
|
|
$deleteurl->param('type', $i->options['type']);
|
|
|
|
$delete .= html_writer::link($deleteurl, $deletestr);
|
2009-12-02 10:04:04 +00:00
|
|
|
}
|
2008-11-26 07:03:10 +00:00
|
|
|
}
|
|
|
|
|
2009-06-04 06:40:35 +00:00
|
|
|
$type = repository::get_type_by_id($i->options['typeid']);
|
2012-06-19 12:07:46 +08:00
|
|
|
$table->data[] = array(format_string($i->name), $type->get_readablename(), $settings, $delete);
|
2008-11-26 07:03:10 +00:00
|
|
|
|
|
|
|
//display a grey row if the type is defined as not visible
|
|
|
|
if (isset($type) && !$type->get_visible()) {
|
2009-07-15 05:37:31 +00:00
|
|
|
$table->rowclasses[] = 'dimmed_text';
|
2008-11-26 07:03:10 +00:00
|
|
|
} else {
|
2009-07-15 05:37:31 +00:00
|
|
|
$table->rowclasses[] = '';
|
2008-11-26 07:03:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!in_array($i->name, $alreadyplugins)) {
|
|
|
|
$alreadyplugins[] = $i->name;
|
|
|
|
}
|
|
|
|
}
|
2010-03-20 22:15:54 +00:00
|
|
|
$output .= html_writer::table($table);
|
2008-11-26 07:03:10 +00:00
|
|
|
$instancehtml = '<div>';
|
|
|
|
$addable = 0;
|
|
|
|
|
|
|
|
//if no type is set, we can create all type of instance
|
|
|
|
if (!$typename) {
|
|
|
|
$instancehtml .= '<h3>';
|
|
|
|
$instancehtml .= get_string('createrepository', 'repository');
|
|
|
|
$instancehtml .= '</h3><ul>';
|
|
|
|
$types = repository::get_editable_types($context);
|
|
|
|
foreach ($types as $type) {
|
|
|
|
if (!empty($type) && $type->get_visible()) {
|
|
|
|
$instanceoptionnames = repository::static_function($type->get_typename(), 'get_instance_option_names');
|
|
|
|
if (!empty($instanceoptionnames)) {
|
2010-07-05 07:27:49 +00:00
|
|
|
$baseurl->param('new', $type->get_typename());
|
|
|
|
$instancehtml .= '<li><a href="'.$baseurl->out().'">'.get_string('createxxinstance', 'repository', get_string('pluginname', 'repository_'.$type->get_typename())). '</a></li>';
|
|
|
|
$baseurl->remove_params('new');
|
2008-11-26 07:03:10 +00:00
|
|
|
$addable++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$instancehtml .= '</ul>';
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$instanceoptionnames = repository::static_function($typename, 'get_instance_option_names');
|
|
|
|
if (!empty($instanceoptionnames)) { //create a unique type of instance
|
|
|
|
$addable = 1;
|
2010-07-05 07:27:49 +00:00
|
|
|
$baseurl->param('new', $typename);
|
2012-05-12 04:14:53 +08:00
|
|
|
$output .= $OUTPUT->single_button($baseurl, get_string('createinstance', 'repository'), 'get');
|
2010-07-05 07:27:49 +00:00
|
|
|
$baseurl->remove_params('new');
|
2008-11-26 07:03:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($addable) {
|
|
|
|
$instancehtml .= '</div>';
|
|
|
|
$output .= $instancehtml;
|
|
|
|
}
|
|
|
|
|
2009-08-10 05:49:18 +00:00
|
|
|
$output .= $OUTPUT->box_end();
|
2008-11-26 07:03:10 +00:00
|
|
|
|
|
|
|
//print the list + creation links
|
|
|
|
print($output);
|
|
|
|
}
|
2008-06-25 08:09:25 +00:00
|
|
|
|
2012-05-12 04:14:53 +08:00
|
|
|
/**
|
|
|
|
* Prepare file reference information
|
|
|
|
*
|
|
|
|
* @param string $source
|
|
|
|
* @return string file referece
|
|
|
|
*/
|
|
|
|
public function get_file_reference($source) {
|
2012-06-13 12:11:06 +08:00
|
|
|
if ($this->has_moodle_files() && ($this->supported_returntypes() & FILE_REFERENCE)) {
|
|
|
|
$params = file_storage::unpack_reference($source);
|
|
|
|
if (!is_array($params)) {
|
|
|
|
throw new repository_exception('invalidparams', 'repository');
|
|
|
|
}
|
|
|
|
return file_storage::pack_reference($params);
|
|
|
|
}
|
2012-05-12 04:14:53 +08:00
|
|
|
return $source;
|
|
|
|
}
|
2008-07-16 05:15:14 +00:00
|
|
|
/**
|
2010-07-05 07:27:49 +00:00
|
|
|
* Decide where to save the file, can be overwriten by subclass
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
|
|
|
* @param string $filename file name
|
|
|
|
* @return file path
|
2008-07-16 05:15:14 +00:00
|
|
|
*/
|
2009-09-03 03:37:55 +00:00
|
|
|
public function prepare_file($filename) {
|
2008-07-16 05:15:14 +00:00
|
|
|
global $CFG;
|
2011-08-11 03:03:58 +09:30
|
|
|
if (!file_exists($CFG->tempdir.'/download')) {
|
|
|
|
mkdir($CFG->tempdir.'/download/', $CFG->directorypermissions, true);
|
2008-07-17 03:54:20 +00:00
|
|
|
}
|
2011-08-11 03:03:58 +09:30
|
|
|
if (is_dir($CFG->tempdir.'/download')) {
|
|
|
|
$dir = $CFG->tempdir.'/download/';
|
2008-07-17 03:54:20 +00:00
|
|
|
}
|
2009-09-03 03:37:55 +00:00
|
|
|
if (empty($filename)) {
|
2011-09-10 12:05:29 +02:00
|
|
|
$filename = uniqid('repo', true).'_'.time().'.tmp';
|
2008-07-18 07:44:14 +00:00
|
|
|
}
|
2009-09-03 03:37:55 +00:00
|
|
|
if (file_exists($dir.$filename)) {
|
|
|
|
$filename = uniqid('m').$filename;
|
2008-07-16 05:15:14 +00:00
|
|
|
}
|
2009-09-03 03:37:55 +00:00
|
|
|
return $dir.$filename;
|
2009-03-16 02:16:50 +00:00
|
|
|
}
|
|
|
|
|
2012-05-12 04:14:53 +08:00
|
|
|
/**
|
|
|
|
* Does this repository used to browse moodle files?
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function has_moodle_files() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-11-08 13:20:37 +00:00
|
|
|
/**
|
|
|
|
* Return file URL, for most plugins, the parameter is the original
|
|
|
|
* url, but some plugins use a file id, so we need this function to
|
|
|
|
* convert file id to original url.
|
|
|
|
*
|
|
|
|
* @param string $url the url of file
|
2010-03-16 03:29:45 +00:00
|
|
|
* @return string
|
2009-11-08 13:20:37 +00:00
|
|
|
*/
|
|
|
|
public function get_link($url) {
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
2009-03-16 02:16:50 +00:00
|
|
|
/**
|
2012-05-12 04:14:53 +08:00
|
|
|
* Download a file, this function can be overridden by subclass. {@link curl}
|
2009-03-16 02:16:50 +00:00
|
|
|
*
|
|
|
|
* @param string $url the url of file
|
2009-09-03 03:37:55 +00:00
|
|
|
* @param string $filename save location
|
2012-06-13 12:11:06 +08:00
|
|
|
* @return array with elements:
|
|
|
|
* path: internal location of the file
|
|
|
|
* url: URL to the source (from parameters)
|
2009-03-16 02:16:50 +00:00
|
|
|
*/
|
2009-09-03 03:37:55 +00:00
|
|
|
public function get_file($url, $filename = '') {
|
2009-03-16 02:16:50 +00:00
|
|
|
global $CFG;
|
2009-11-02 06:45:12 +00:00
|
|
|
$path = $this->prepare_file($filename);
|
|
|
|
$fp = fopen($path, 'w');
|
|
|
|
$c = new curl;
|
2012-06-13 12:11:06 +08:00
|
|
|
$result = $c->download(array(array('url'=>$url, 'file'=>$fp)));
|
2012-05-12 04:14:53 +08:00
|
|
|
// Close file handler.
|
|
|
|
fclose($fp);
|
2012-06-13 12:11:06 +08:00
|
|
|
if (empty($result)) {
|
|
|
|
unlink($path);
|
|
|
|
return null;
|
|
|
|
}
|
2010-03-29 03:39:08 +00:00
|
|
|
return array('path'=>$path, 'url'=>$url);
|
2008-06-27 03:33:40 +00:00
|
|
|
}
|
2008-06-25 08:09:25 +00:00
|
|
|
|
2011-09-15 09:38:09 +05:30
|
|
|
/**
|
|
|
|
* Return size of a file in bytes.
|
|
|
|
*
|
|
|
|
* @param string $source encoded and serialized data of file
|
2012-05-12 04:14:53 +08:00
|
|
|
* @return int file size in bytes
|
2011-09-15 09:38:09 +05:30
|
|
|
*/
|
|
|
|
public function get_file_size($source) {
|
2012-05-31 12:58:34 +08:00
|
|
|
// TODO MDL-33297 remove this function completely?
|
2011-09-15 09:38:09 +05:30
|
|
|
$browser = get_file_browser();
|
|
|
|
$params = unserialize(base64_decode($source));
|
|
|
|
$contextid = clean_param($params['contextid'], PARAM_INT);
|
|
|
|
$fileitemid = clean_param($params['itemid'], PARAM_INT);
|
|
|
|
$filename = clean_param($params['filename'], PARAM_FILE);
|
|
|
|
$filepath = clean_param($params['filepath'], PARAM_PATH);
|
2011-09-24 15:07:27 +02:00
|
|
|
$filearea = clean_param($params['filearea'], PARAM_AREA);
|
|
|
|
$component = clean_param($params['component'], PARAM_COMPONENT);
|
2011-09-15 09:38:09 +05:30
|
|
|
$context = get_context_instance_by_id($contextid);
|
|
|
|
$file_info = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename);
|
|
|
|
if (!empty($file_info)) {
|
|
|
|
$filesize = $file_info->get_filesize();
|
|
|
|
} else {
|
|
|
|
$filesize = null;
|
|
|
|
}
|
|
|
|
return $filesize;
|
|
|
|
}
|
|
|
|
|
2008-10-09 03:02:26 +00:00
|
|
|
/**
|
|
|
|
* Return is the instance is visible
|
|
|
|
* (is the type visible ? is the context enable ?)
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
|
|
|
* @return bool
|
2008-10-09 03:02:26 +00:00
|
|
|
*/
|
2008-11-26 07:03:10 +00:00
|
|
|
public function is_visible() {
|
2009-05-29 09:14:32 +00:00
|
|
|
$type = repository::get_type_by_id($this->options['typeid']);
|
2008-11-26 07:03:10 +00:00
|
|
|
$instanceoptions = repository::static_function($type->get_typename(), 'get_instance_option_names');
|
2008-10-09 03:02:26 +00:00
|
|
|
|
|
|
|
if ($type->get_visible()) {
|
|
|
|
//if the instance is unique so it's visible, otherwise check if the instance has a enabled context
|
2009-12-02 10:04:04 +00:00
|
|
|
if (empty($instanceoptions) || $type->get_contextvisibility($this->context)) {
|
2008-10-09 03:02:26 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-09-04 07:24:31 +00:00
|
|
|
/**
|
2008-09-10 02:49:53 +00:00
|
|
|
* Return the name of this instance, can be overridden.
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
2009-04-21 10:02:59 +00:00
|
|
|
* @return string
|
2008-09-04 07:24:31 +00:00
|
|
|
*/
|
2008-09-15 07:56:26 +00:00
|
|
|
public function get_name() {
|
2008-09-02 04:05:11 +00:00
|
|
|
global $DB;
|
2010-07-27 09:03:54 +00:00
|
|
|
if ( $name = $this->instance->name ) {
|
|
|
|
return $name;
|
2008-09-02 04:05:11 +00:00
|
|
|
} else {
|
2010-07-27 09:03:54 +00:00
|
|
|
return get_string('pluginname', 'repository_' . $this->options['type']);
|
2008-09-02 04:05:11 +00:00
|
|
|
}
|
|
|
|
}
|
2008-07-14 05:31:01 +00:00
|
|
|
|
2008-11-26 03:26:33 +00:00
|
|
|
/**
|
2012-05-12 04:14:53 +08:00
|
|
|
* What kind of files will be in this repository?
|
|
|
|
*
|
2008-11-26 03:26:33 +00:00
|
|
|
* @return array return '*' means this repository support any files, otherwise
|
|
|
|
* return mimetypes of files, it can be an array
|
|
|
|
*/
|
2008-12-08 05:19:09 +00:00
|
|
|
public function supported_filetypes() {
|
2008-11-26 03:26:33 +00:00
|
|
|
// return array('text/plain', 'image/gif');
|
|
|
|
return '*';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-06-25 16:03:25 +08:00
|
|
|
* Tells how the file can be picked from this repository
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
2012-06-25 16:03:25 +08:00
|
|
|
* Maximum value is FILE_INTERNAL | FILE_EXTERNAL | FILE_REFERENCE
|
|
|
|
*
|
|
|
|
* @return int
|
2008-11-26 03:26:33 +00:00
|
|
|
*/
|
2009-11-02 06:45:12 +00:00
|
|
|
public function supported_returntypes() {
|
2012-06-25 16:03:25 +08:00
|
|
|
return (FILE_INTERNAL | FILE_EXTERNAL);
|
2009-10-07 10:16:45 +00:00
|
|
|
}
|
2008-11-26 03:26:33 +00:00
|
|
|
|
2008-06-27 03:33:40 +00:00
|
|
|
/**
|
2008-08-28 06:39:09 +00:00
|
|
|
* Provide repository instance information for Ajax
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
|
|
|
* @return stdClass
|
2008-06-27 03:33:40 +00:00
|
|
|
*/
|
2009-05-29 09:14:32 +00:00
|
|
|
final public function get_meta() {
|
2010-07-03 14:40:33 +00:00
|
|
|
global $CFG, $OUTPUT;
|
2010-09-21 08:54:01 +00:00
|
|
|
$meta = new stdClass();
|
2009-05-29 09:14:32 +00:00
|
|
|
$meta->id = $this->id;
|
2012-06-19 11:20:21 +08:00
|
|
|
$meta->name = format_string($this->get_name());
|
2009-05-29 09:14:32 +00:00
|
|
|
$meta->type = $this->options['type'];
|
2010-07-03 14:40:33 +00:00
|
|
|
$meta->icon = $OUTPUT->pix_url('icon', 'repository_'.$meta->type)->out(false);
|
2012-05-21 15:17:53 +08:00
|
|
|
$meta->supported_types = file_get_typegroup('extension', $this->supported_filetypes());
|
2009-11-06 09:07:46 +00:00
|
|
|
$meta->return_types = $this->supported_returntypes();
|
2012-01-10 10:48:38 +00:00
|
|
|
$meta->sortorder = $this->options['sortorder'];
|
2009-05-29 09:14:32 +00:00
|
|
|
return $meta;
|
2008-08-13 04:09:13 +00:00
|
|
|
}
|
2008-06-25 08:09:25 +00:00
|
|
|
|
2008-08-14 09:39:39 +00:00
|
|
|
/**
|
|
|
|
* Create an instance for this plug-in
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
|
|
|
* @static
|
2008-08-28 06:15:19 +00:00
|
|
|
* @param string $type the type of the repository
|
2012-05-12 04:14:53 +08:00
|
|
|
* @param int $userid the user id
|
|
|
|
* @param stdClass $context the context
|
2008-08-28 06:15:19 +00:00
|
|
|
* @param array $params the options for this instance
|
2012-05-12 04:14:53 +08:00
|
|
|
* @param int $readonly whether to create it readonly or not (defaults to not)
|
2009-04-21 10:02:59 +00:00
|
|
|
* @return mixed
|
2008-08-14 09:39:39 +00:00
|
|
|
*/
|
2010-06-22 07:40:30 +00:00
|
|
|
public static function create($type, $userid, $context, $params, $readonly=0) {
|
2008-08-14 09:39:39 +00:00
|
|
|
global $CFG, $DB;
|
|
|
|
$params = (array)$params;
|
2010-07-05 07:27:49 +00:00
|
|
|
require_once($CFG->dirroot . '/repository/'. $type . '/lib.php');
|
2008-08-14 09:39:39 +00:00
|
|
|
$classname = 'repository_' . $type;
|
2008-08-28 02:23:09 +00:00
|
|
|
if ($repo = $DB->get_record('repository', array('type'=>$type))) {
|
2010-09-21 08:54:01 +00:00
|
|
|
$record = new stdClass();
|
2008-08-28 02:23:09 +00:00
|
|
|
$record->name = $params['name'];
|
|
|
|
$record->typeid = $repo->id;
|
|
|
|
$record->timecreated = time();
|
|
|
|
$record->timemodified = time();
|
|
|
|
$record->contextid = $context->id;
|
2008-09-16 09:08:36 +00:00
|
|
|
$record->readonly = $readonly;
|
2008-08-28 02:23:09 +00:00
|
|
|
$record->userid = $userid;
|
|
|
|
$id = $DB->insert_record('repository_instances', $record);
|
2008-09-04 02:07:58 +00:00
|
|
|
$options = array();
|
2008-09-18 05:21:51 +00:00
|
|
|
$configs = call_user_func($classname . '::get_instance_option_names');
|
2008-11-03 04:44:53 +00:00
|
|
|
if (!empty($configs)) {
|
|
|
|
foreach ($configs as $config) {
|
2011-06-23 17:13:46 +08:00
|
|
|
if (isset($params[$config])) {
|
|
|
|
$options[$config] = $params[$config];
|
|
|
|
} else {
|
|
|
|
$options[$config] = null;
|
|
|
|
}
|
2008-11-03 04:44:53 +00:00
|
|
|
}
|
2008-08-28 02:23:09 +00:00
|
|
|
}
|
2008-10-31 00:30:36 +00:00
|
|
|
|
2008-08-28 02:23:09 +00:00
|
|
|
if (!empty($id)) {
|
|
|
|
unset($options['name']);
|
2008-11-26 07:03:10 +00:00
|
|
|
$instance = repository::get_instance($id);
|
2008-08-28 02:23:09 +00:00
|
|
|
$instance->set_option($options);
|
|
|
|
return $id;
|
|
|
|
} else {
|
|
|
|
return null;
|
2008-08-14 09:39:39 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2008-08-28 06:15:19 +00:00
|
|
|
|
2008-06-27 03:33:40 +00:00
|
|
|
/**
|
2008-08-13 04:09:13 +00:00
|
|
|
* delete a repository instance
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
|
|
|
* @param bool $downloadcontents
|
|
|
|
* @return bool
|
2008-06-27 03:33:40 +00:00
|
|
|
*/
|
2012-05-12 04:14:53 +08:00
|
|
|
final public function delete($downloadcontents = false) {
|
2008-08-13 04:09:13 +00:00
|
|
|
global $DB;
|
2012-05-12 04:14:53 +08:00
|
|
|
if ($downloadcontents) {
|
|
|
|
$this->convert_references_to_local();
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
$DB->delete_records('repository_instances', array('id'=>$this->id));
|
|
|
|
$DB->delete_records('repository_instance_config', array('instanceid'=>$this->id));
|
|
|
|
} catch (dml_exception $ex) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-08-13 04:09:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
2008-08-28 06:15:19 +00:00
|
|
|
|
2008-08-13 04:09:13 +00:00
|
|
|
/**
|
|
|
|
* Hide/Show a repository
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
2008-08-28 06:15:19 +00:00
|
|
|
* @param string $hide
|
2012-05-12 04:14:53 +08:00
|
|
|
* @return bool
|
2008-08-13 04:09:13 +00:00
|
|
|
*/
|
2008-09-15 07:56:26 +00:00
|
|
|
final public function hide($hide = 'toggle') {
|
2008-08-13 04:09:13 +00:00
|
|
|
global $DB;
|
|
|
|
if ($entry = $DB->get_record('repository', array('id'=>$this->id))) {
|
|
|
|
if ($hide === 'toggle' ) {
|
|
|
|
if (!empty($entry->visible)) {
|
|
|
|
$entry->visible = 0;
|
|
|
|
} else {
|
|
|
|
$entry->visible = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!empty($hide)) {
|
|
|
|
$entry->visible = 0;
|
|
|
|
} else {
|
|
|
|
$entry->visible = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $DB->update_record('repository', $entry);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2008-06-25 08:09:25 +00:00
|
|
|
|
|
|
|
/**
|
2008-08-13 04:09:13 +00:00
|
|
|
* Save settings for repository instance
|
2008-08-28 06:15:19 +00:00
|
|
|
* $repo->set_option(array('api_key'=>'f2188bde132', 'name'=>'dongsheng'));
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
2008-08-28 06:15:19 +00:00
|
|
|
* @param array $options settings
|
2012-05-12 04:14:53 +08:00
|
|
|
* @return bool
|
2008-06-25 08:09:25 +00:00
|
|
|
*/
|
2008-09-15 07:56:26 +00:00
|
|
|
public function set_option($options = array()) {
|
2008-08-13 04:09:13 +00:00
|
|
|
global $DB;
|
2008-09-15 07:56:26 +00:00
|
|
|
|
2008-08-26 08:00:47 +00:00
|
|
|
if (!empty($options['name'])) {
|
2010-09-21 08:22:04 +00:00
|
|
|
$r = new stdClass();
|
2008-08-26 08:00:47 +00:00
|
|
|
$r->id = $this->id;
|
|
|
|
$r->name = $options['name'];
|
|
|
|
$DB->update_record('repository_instances', $r);
|
|
|
|
unset($options['name']);
|
|
|
|
}
|
2008-08-26 07:20:56 +00:00
|
|
|
foreach ($options as $name=>$value) {
|
|
|
|
if ($id = $DB->get_field('repository_instance_config', 'id', array('name'=>$name, 'instanceid'=>$this->id))) {
|
2010-09-03 18:14:55 +00:00
|
|
|
$DB->set_field('repository_instance_config', 'value', $value, array('id'=>$id));
|
2008-08-26 07:20:56 +00:00
|
|
|
} else {
|
2010-09-21 08:22:04 +00:00
|
|
|
$config = new stdClass();
|
2008-08-26 07:20:56 +00:00
|
|
|
$config->instanceid = $this->id;
|
|
|
|
$config->name = $name;
|
|
|
|
$config->value = $value;
|
2010-09-03 18:14:55 +00:00
|
|
|
$DB->insert_record('repository_instance_config', $config);
|
2008-08-26 07:20:56 +00:00
|
|
|
}
|
2008-08-13 04:09:13 +00:00
|
|
|
}
|
2010-09-03 18:14:55 +00:00
|
|
|
return true;
|
2008-06-25 08:09:25 +00:00
|
|
|
}
|
|
|
|
|
2008-08-13 04:09:13 +00:00
|
|
|
/**
|
|
|
|
* Get settings for repository instance
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
2009-04-21 10:02:59 +00:00
|
|
|
* @param string $config
|
2008-08-13 04:09:13 +00:00
|
|
|
* @return array Settings
|
|
|
|
*/
|
2008-09-15 07:56:26 +00:00
|
|
|
public function get_option($config = '') {
|
2008-08-13 04:09:13 +00:00
|
|
|
global $DB;
|
2008-08-26 07:20:56 +00:00
|
|
|
$entries = $DB->get_records('repository_instance_config', array('instanceid'=>$this->id));
|
|
|
|
$ret = array();
|
|
|
|
if (empty($entries)) {
|
|
|
|
return $ret;
|
2008-08-13 04:09:13 +00:00
|
|
|
}
|
2008-09-15 07:56:26 +00:00
|
|
|
foreach($entries as $entry) {
|
2008-08-26 07:20:56 +00:00
|
|
|
$ret[$entry->name] = $entry->value;
|
2008-08-13 04:09:13 +00:00
|
|
|
}
|
|
|
|
if (!empty($config)) {
|
2010-10-22 05:31:16 +00:00
|
|
|
if (isset($ret[$config])) {
|
2009-07-23 07:48:30 +00:00
|
|
|
return $ret[$config];
|
2010-10-22 05:31:16 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2008-08-13 04:09:13 +00:00
|
|
|
} else {
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
}
|
2008-07-23 04:43:53 +00:00
|
|
|
|
2012-05-12 04:14:53 +08:00
|
|
|
/**
|
|
|
|
* Filter file listing to display specific types
|
|
|
|
*
|
|
|
|
* @param array $value
|
|
|
|
* @return bool
|
|
|
|
*/
|
2008-12-09 02:11:57 +00:00
|
|
|
public function filter(&$value) {
|
2011-08-17 16:27:15 +02:00
|
|
|
$accepted_types = optional_param_array('accepted_types', '', PARAM_RAW);
|
2009-04-17 08:00:59 +00:00
|
|
|
if (isset($value['children'])) {
|
|
|
|
if (!empty($value['children'])) {
|
|
|
|
$value['children'] = array_filter($value['children'], array($this, 'filter'));
|
2009-01-08 05:12:49 +00:00
|
|
|
}
|
2012-05-21 15:17:53 +08:00
|
|
|
return true; // always return directories
|
2008-12-09 02:11:57 +00:00
|
|
|
} else {
|
2009-01-13 05:04:15 +00:00
|
|
|
if ($accepted_types == '*' or empty($accepted_types)
|
|
|
|
or (is_array($accepted_types) and in_array('*', $accepted_types))) {
|
2012-05-21 15:17:53 +08:00
|
|
|
return true;
|
|
|
|
} else {
|
2012-05-22 14:24:11 +08:00
|
|
|
foreach ($accepted_types as $ext) {
|
|
|
|
if (preg_match('#'.$ext.'$#i', $value['title'])) {
|
2012-05-21 15:17:53 +08:00
|
|
|
return true;
|
2008-12-09 02:11:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-05-21 15:17:53 +08:00
|
|
|
return false;
|
2008-12-09 02:11:57 +00:00
|
|
|
}
|
|
|
|
|
2008-08-13 04:09:13 +00:00
|
|
|
/**
|
|
|
|
* Given a path, and perhaps a search, get a list of files.
|
|
|
|
*
|
2012-05-12 04:14:53 +08:00
|
|
|
* See details on {@link http://docs.moodle.org/dev/Repository_plugins}
|
2008-08-22 05:59:08 +00:00
|
|
|
*
|
2012-05-12 04:14:53 +08:00
|
|
|
* @param string $path this parameter can a folder name, or a identification of folder
|
|
|
|
* @param string $page the page number of file list
|
2010-01-29 05:50:40 +00:00
|
|
|
* @return array the list of files, including meta infomation, containing the following keys
|
|
|
|
* manage, url to manage url
|
|
|
|
* client_id
|
|
|
|
* login, login form
|
|
|
|
* repo_id, active repository id
|
|
|
|
* login_btn_action, the login button action
|
|
|
|
* login_btn_label, the login button label
|
|
|
|
* total, number of results
|
|
|
|
* perpage, items per page
|
|
|
|
* page
|
|
|
|
* pages, total pages
|
2010-10-14 08:51:08 +00:00
|
|
|
* issearchresult, is it a search result?
|
2010-01-29 05:50:40 +00:00
|
|
|
* list, file list
|
|
|
|
* path, current path and parent path
|
2008-08-13 04:09:13 +00:00
|
|
|
*/
|
2009-02-27 06:25:56 +00:00
|
|
|
public function get_listing($path = '', $page = '') {
|
|
|
|
}
|
2008-06-25 08:09:25 +00:00
|
|
|
|
2008-09-12 08:16:09 +00:00
|
|
|
/**
|
MDL-14636: Support table view in Filepicker, provide more info about files
- Added table view in Filepicker where for each file we show Name, Last modified, Size and Type. Sortable by column
- Preprocess list of files returned from repositories to include formatted size, dates, type and also add filetype icon
- Make sure that local repositories (coursefiles, filesystem, local, recent, user) return information about file size, datemodified, datecreated, author, license
2012-04-17 14:20:20 +08:00
|
|
|
* Prepares list of files before passing it to AJAX, makes sure data is in the correct
|
2012-05-10 15:20:09 +08:00
|
|
|
* format and stores formatted values.
|
MDL-14636: Support table view in Filepicker, provide more info about files
- Added table view in Filepicker where for each file we show Name, Last modified, Size and Type. Sortable by column
- Preprocess list of files returned from repositories to include formatted size, dates, type and also add filetype icon
- Make sure that local repositories (coursefiles, filesystem, local, recent, user) return information about file size, datemodified, datecreated, author, license
2012-04-17 14:20:20 +08:00
|
|
|
*
|
2012-05-02 14:59:09 +08:00
|
|
|
* @param array|stdClass $listing result of get_listing() or search() or file_get_drafarea_files()
|
MDL-14636: Support table view in Filepicker, provide more info about files
- Added table view in Filepicker where for each file we show Name, Last modified, Size and Type. Sortable by column
- Preprocess list of files returned from repositories to include formatted size, dates, type and also add filetype icon
- Make sure that local repositories (coursefiles, filesystem, local, recent, user) return information about file size, datemodified, datecreated, author, license
2012-04-17 14:20:20 +08:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function prepare_listing($listing) {
|
|
|
|
global $OUTPUT;
|
2012-05-24 13:43:01 +08:00
|
|
|
|
|
|
|
$defaultfoldericon = $OUTPUT->pix_url(file_folder_icon(24))->out(false);
|
|
|
|
// prepare $listing['path'] or $listing->path
|
2012-05-25 11:44:04 +08:00
|
|
|
if (is_array($listing) && isset($listing['path']) && is_array($listing['path'])) {
|
2012-05-24 13:43:01 +08:00
|
|
|
$path = &$listing['path'];
|
2012-05-25 11:25:57 +08:00
|
|
|
} else if (is_object($listing) && isset($listing->path) && is_array($listing->path)) {
|
2012-05-24 13:43:01 +08:00
|
|
|
$path = &$listing->path;
|
|
|
|
}
|
|
|
|
if (isset($path)) {
|
|
|
|
$len = count($path);
|
|
|
|
for ($i=0; $i<$len; $i++) {
|
|
|
|
if (is_array($path[$i]) && !isset($path[$i]['icon'])) {
|
|
|
|
$path[$i]['icon'] = $defaultfoldericon;
|
|
|
|
} else if (is_object($path[$i]) && !isset($path[$i]->icon)) {
|
|
|
|
$path[$i]->icon = $defaultfoldericon;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepare $listing['list'] or $listing->list
|
2012-05-25 11:25:57 +08:00
|
|
|
if (is_array($listing) && isset($listing['list']) && is_array($listing['list'])) {
|
2012-05-10 15:20:09 +08:00
|
|
|
$listing['list'] = array_values($listing['list']); // convert to array
|
2012-05-02 14:59:09 +08:00
|
|
|
$files = &$listing['list'];
|
2012-05-25 11:25:57 +08:00
|
|
|
} else if (is_object($listing) && isset($listing->list) && is_array($listing->list)) {
|
2012-05-10 15:20:09 +08:00
|
|
|
$listing->list = array_values($listing->list); // convert to array
|
2012-05-02 14:59:09 +08:00
|
|
|
$files = &$listing->list;
|
|
|
|
} else {
|
MDL-14636: Support table view in Filepicker, provide more info about files
- Added table view in Filepicker where for each file we show Name, Last modified, Size and Type. Sortable by column
- Preprocess list of files returned from repositories to include formatted size, dates, type and also add filetype icon
- Make sure that local repositories (coursefiles, filesystem, local, recent, user) return information about file size, datemodified, datecreated, author, license
2012-04-17 14:20:20 +08:00
|
|
|
return $listing;
|
|
|
|
}
|
2012-05-02 14:59:09 +08:00
|
|
|
$len = count($files);
|
MDL-14636: Support table view in Filepicker, provide more info about files
- Added table view in Filepicker where for each file we show Name, Last modified, Size and Type. Sortable by column
- Preprocess list of files returned from repositories to include formatted size, dates, type and also add filetype icon
- Make sure that local repositories (coursefiles, filesystem, local, recent, user) return information about file size, datemodified, datecreated, author, license
2012-04-17 14:20:20 +08:00
|
|
|
for ($i=0; $i<$len; $i++) {
|
2012-05-02 14:59:09 +08:00
|
|
|
if (is_object($files[$i])) {
|
|
|
|
$file = (array)$files[$i];
|
|
|
|
$converttoobject = true;
|
|
|
|
} else {
|
|
|
|
$file = & $files[$i];
|
|
|
|
$converttoobject = false;
|
|
|
|
}
|
2012-04-19 10:40:21 +08:00
|
|
|
if (isset($file['size'])) {
|
|
|
|
$file['size'] = (int)$file['size'];
|
|
|
|
$file['size_f'] = display_size($file['size']);
|
|
|
|
}
|
|
|
|
if (isset($file['license']) &&
|
|
|
|
get_string_manager()->string_exists($file['license'], 'license')) {
|
|
|
|
$file['license_f'] = get_string($file['license'], 'license');
|
|
|
|
}
|
|
|
|
if (isset($file['image_width']) && isset($file['image_height'])) {
|
2012-05-10 15:20:09 +08:00
|
|
|
$a = array('width' => $file['image_width'], 'height' => $file['image_height']);
|
|
|
|
$file['dimensions'] = get_string('imagesize', 'repository', (object)$a);
|
MDL-14636: Support table view in Filepicker, provide more info about files
- Added table view in Filepicker where for each file we show Name, Last modified, Size and Type. Sortable by column
- Preprocess list of files returned from repositories to include formatted size, dates, type and also add filetype icon
- Make sure that local repositories (coursefiles, filesystem, local, recent, user) return information about file size, datemodified, datecreated, author, license
2012-04-17 14:20:20 +08:00
|
|
|
}
|
|
|
|
foreach (array('date', 'datemodified', 'datecreated') as $key) {
|
2012-04-19 10:40:21 +08:00
|
|
|
if (!isset($file[$key]) && isset($file['date'])) {
|
|
|
|
$file[$key] = $file['date'];
|
MDL-14636: Support table view in Filepicker, provide more info about files
- Added table view in Filepicker where for each file we show Name, Last modified, Size and Type. Sortable by column
- Preprocess list of files returned from repositories to include formatted size, dates, type and also add filetype icon
- Make sure that local repositories (coursefiles, filesystem, local, recent, user) return information about file size, datemodified, datecreated, author, license
2012-04-17 14:20:20 +08:00
|
|
|
}
|
2012-04-19 10:40:21 +08:00
|
|
|
if (isset($file[$key])) {
|
MDL-14636: Support table view in Filepicker, provide more info about files
- Added table view in Filepicker where for each file we show Name, Last modified, Size and Type. Sortable by column
- Preprocess list of files returned from repositories to include formatted size, dates, type and also add filetype icon
- Make sure that local repositories (coursefiles, filesystem, local, recent, user) return information about file size, datemodified, datecreated, author, license
2012-04-17 14:20:20 +08:00
|
|
|
// must be UNIX timestamp
|
2012-04-19 10:40:21 +08:00
|
|
|
$file[$key] = (int)$file[$key];
|
|
|
|
if (!$file[$key]) {
|
|
|
|
unset($file[$key]);
|
MDL-14636: Support table view in Filepicker, provide more info about files
- Added table view in Filepicker where for each file we show Name, Last modified, Size and Type. Sortable by column
- Preprocess list of files returned from repositories to include formatted size, dates, type and also add filetype icon
- Make sure that local repositories (coursefiles, filesystem, local, recent, user) return information about file size, datemodified, datecreated, author, license
2012-04-17 14:20:20 +08:00
|
|
|
} else {
|
2012-04-19 10:40:21 +08:00
|
|
|
$file[$key.'_f'] = userdate($file[$key], get_string('strftimedatetime', 'langconfig'));
|
|
|
|
$file[$key.'_f_s'] = userdate($file[$key], get_string('strftimedatetimeshort', 'langconfig'));
|
MDL-14636: Support table view in Filepicker, provide more info about files
- Added table view in Filepicker where for each file we show Name, Last modified, Size and Type. Sortable by column
- Preprocess list of files returned from repositories to include formatted size, dates, type and also add filetype icon
- Make sure that local repositories (coursefiles, filesystem, local, recent, user) return information about file size, datemodified, datecreated, author, license
2012-04-17 14:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-05-02 14:59:09 +08:00
|
|
|
$isfolder = (array_key_exists('children', $file) || (isset($file['type']) && $file['type'] == 'folder'));
|
|
|
|
$filename = null;
|
|
|
|
if (isset($file['title'])) {
|
|
|
|
$filename = $file['title'];
|
|
|
|
}
|
|
|
|
else if (isset($file['fullname'])) {
|
|
|
|
$filename = $file['fullname'];
|
|
|
|
}
|
|
|
|
if (!isset($file['mimetype']) && !$isfolder && $filename) {
|
2012-05-21 15:17:53 +08:00
|
|
|
$file['mimetype'] = get_mimetype_description(array('filename' => $filename));
|
MDL-14636: Support table view in Filepicker, provide more info about files
- Added table view in Filepicker where for each file we show Name, Last modified, Size and Type. Sortable by column
- Preprocess list of files returned from repositories to include formatted size, dates, type and also add filetype icon
- Make sure that local repositories (coursefiles, filesystem, local, recent, user) return information about file size, datemodified, datecreated, author, license
2012-04-17 14:20:20 +08:00
|
|
|
}
|
2012-05-02 14:59:09 +08:00
|
|
|
if (!isset($file['icon'])) {
|
|
|
|
if ($isfolder) {
|
2012-05-24 13:43:01 +08:00
|
|
|
$file['icon'] = $defaultfoldericon;
|
2012-05-02 14:59:09 +08:00
|
|
|
} else if ($filename) {
|
2012-05-21 15:17:53 +08:00
|
|
|
$file['icon'] = $OUTPUT->pix_url(file_extension_icon($filename, 24))->out(false);
|
MDL-14636: Support table view in Filepicker, provide more info about files
- Added table view in Filepicker where for each file we show Name, Last modified, Size and Type. Sortable by column
- Preprocess list of files returned from repositories to include formatted size, dates, type and also add filetype icon
- Make sure that local repositories (coursefiles, filesystem, local, recent, user) return information about file size, datemodified, datecreated, author, license
2012-04-17 14:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
2012-05-02 14:59:09 +08:00
|
|
|
if ($converttoobject) {
|
|
|
|
$files[$i] = (object)$file;
|
|
|
|
}
|
MDL-14636: Support table view in Filepicker, provide more info about files
- Added table view in Filepicker where for each file we show Name, Last modified, Size and Type. Sortable by column
- Preprocess list of files returned from repositories to include formatted size, dates, type and also add filetype icon
- Make sure that local repositories (coursefiles, filesystem, local, recent, user) return information about file size, datemodified, datecreated, author, license
2012-04-17 14:20:20 +08:00
|
|
|
}
|
|
|
|
return $listing;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search files in repository
|
|
|
|
* When doing global search, $search_text will be used as
|
|
|
|
* keyword.
|
2008-09-16 03:11:17 +00:00
|
|
|
*
|
2012-05-12 04:14:53 +08:00
|
|
|
* @param string $search_text search key word
|
|
|
|
* @param int $page page
|
|
|
|
* @return mixed {@see repository::get_listing}
|
2008-09-12 08:16:09 +00:00
|
|
|
*/
|
2012-03-26 11:47:15 +02:00
|
|
|
public function search($search_text, $page = 0) {
|
2008-09-16 03:11:17 +00:00
|
|
|
$list = array();
|
|
|
|
$list['list'] = array();
|
|
|
|
return false;
|
2008-09-12 08:16:09 +00:00
|
|
|
}
|
|
|
|
|
2008-09-15 09:31:41 +00:00
|
|
|
/**
|
|
|
|
* Logout from repository instance
|
|
|
|
* By default, this function will return a login form
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function logout(){
|
|
|
|
return $this->print_login();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* To check whether the user is logged in.
|
|
|
|
*
|
2012-05-12 04:14:53 +08:00
|
|
|
* @return bool
|
2008-09-15 09:31:41 +00:00
|
|
|
*/
|
|
|
|
public function check_login(){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-06-25 08:09:25 +00:00
|
|
|
|
2008-08-13 04:09:13 +00:00
|
|
|
/**
|
|
|
|
* Show the login screen, if required
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
|
|
|
* @return string
|
2008-08-13 04:09:13 +00:00
|
|
|
*/
|
2008-09-18 02:25:28 +00:00
|
|
|
public function print_login(){
|
|
|
|
return $this->get_listing();
|
|
|
|
}
|
2008-06-25 08:09:25 +00:00
|
|
|
|
2008-08-13 04:09:13 +00:00
|
|
|
/**
|
|
|
|
* Show the search screen, if required
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
|
|
|
* @return string
|
2008-08-13 04:09:13 +00:00
|
|
|
*/
|
2009-09-03 03:37:55 +00:00
|
|
|
public function print_search() {
|
2012-03-30 10:11:43 +08:00
|
|
|
global $PAGE;
|
2012-04-26 15:28:53 +08:00
|
|
|
$renderer = $PAGE->get_renderer('core', 'files');
|
2012-03-30 10:11:43 +08:00
|
|
|
return $renderer->repository_default_searchform();
|
2008-09-12 07:28:40 +00:00
|
|
|
}
|
2008-08-13 04:09:13 +00:00
|
|
|
|
2010-05-14 08:29:33 +00:00
|
|
|
/**
|
|
|
|
* For oauth like external authentication, when external repository direct user back to moodle,
|
|
|
|
* this funciton will be called to set up token and token_secret
|
|
|
|
*/
|
|
|
|
public function callback() {
|
|
|
|
}
|
|
|
|
|
2008-09-08 05:38:13 +00:00
|
|
|
/**
|
|
|
|
* is it possible to do glboal search?
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
|
|
|
* @return bool
|
2008-09-08 05:38:13 +00:00
|
|
|
*/
|
2008-09-15 07:56:26 +00:00
|
|
|
public function global_search() {
|
2008-09-08 05:38:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-08-28 06:15:19 +00:00
|
|
|
/**
|
2008-09-02 08:58:53 +00:00
|
|
|
* Defines operations that happen occasionally on cron
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
|
|
|
* @return bool
|
2008-08-28 06:15:19 +00:00
|
|
|
*/
|
2008-09-02 08:58:53 +00:00
|
|
|
public function cron() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-09-15 08:42:56 +00:00
|
|
|
/**
|
2008-09-18 05:54:23 +00:00
|
|
|
* function which is run when the type is created (moodle administrator add the plugin)
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
|
|
|
* @return bool success or fail?
|
2008-09-15 08:42:56 +00:00
|
|
|
*/
|
2008-12-11 03:19:46 +00:00
|
|
|
public static function plugin_init() {
|
2008-10-27 02:57:29 +00:00
|
|
|
return true;
|
2008-09-15 08:42:56 +00:00
|
|
|
}
|
|
|
|
|
2008-09-02 08:58:53 +00:00
|
|
|
/**
|
2008-09-18 02:36:17 +00:00
|
|
|
* Edit/Create Admin Settings Moodle form
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
|
|
|
* @param moodleform $mform Moodle form (passed by reference)
|
2010-08-02 06:33:34 +00:00
|
|
|
* @param string $classname repository class name
|
2008-09-02 08:58:53 +00:00
|
|
|
*/
|
2012-03-26 11:47:15 +02:00
|
|
|
public static function type_config_form($mform, $classname = 'repository') {
|
2010-08-02 06:33:34 +00:00
|
|
|
$instnaceoptions = call_user_func(array($classname, 'get_instance_option_names'), $mform, $classname);
|
2010-07-27 09:03:54 +00:00
|
|
|
if (empty($instnaceoptions)) {
|
|
|
|
// this plugin has only one instance
|
|
|
|
// so we need to give it a name
|
|
|
|
// it can be empty, then moodle will look for instance name from language string
|
|
|
|
$mform->addElement('text', 'pluginname', get_string('pluginname', 'repository'), array('size' => '40'));
|
|
|
|
$mform->addElement('static', 'pluginnamehelp', '', get_string('pluginnamehelp', 'repository'));
|
2012-06-19 11:20:21 +08:00
|
|
|
$mform->setType('pluginname', PARAM_TEXT);
|
2010-07-27 09:03:54 +00:00
|
|
|
}
|
2008-08-13 04:09:13 +00:00
|
|
|
}
|
2008-11-26 07:03:10 +00:00
|
|
|
|
2011-07-28 16:06:25 +01:00
|
|
|
/**
|
|
|
|
* Validate Admin Settings Moodle form
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @param moodleform $mform Moodle form (passed by reference)
|
|
|
|
* @param array $data array of ("fieldname"=>value) of submitted data
|
|
|
|
* @param array $errors array of ("fieldname"=>errormessage) of errors
|
2011-07-28 16:06:25 +01:00
|
|
|
* @return array array of errors
|
|
|
|
*/
|
|
|
|
public static function type_form_validation($mform, $data, $errors) {
|
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-06 09:07:46 +00:00
|
|
|
/**
|
2008-09-18 02:36:17 +00:00
|
|
|
* Edit/Create Instance Settings Moodle form
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
|
|
|
* @param moodleform $mform Moodle form (passed by reference)
|
2008-09-02 08:58:53 +00:00
|
|
|
*/
|
2012-06-12 22:15:09 +08:00
|
|
|
public static function instance_config_form($mform) {
|
2008-09-02 08:58:53 +00:00
|
|
|
}
|
2008-08-13 04:09:13 +00:00
|
|
|
|
2008-09-02 08:58:53 +00:00
|
|
|
/**
|
2012-05-12 04:14:53 +08:00
|
|
|
* Return names of the general options.
|
2008-09-02 08:58:53 +00:00
|
|
|
* By default: no general option name
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
2008-09-02 08:58:53 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2008-09-18 03:19:52 +00:00
|
|
|
public static function get_type_option_names() {
|
2010-07-27 09:03:54 +00:00
|
|
|
return array('pluginname');
|
2008-09-02 08:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-12 04:14:53 +08:00
|
|
|
* Return names of the instance options.
|
2008-09-02 08:58:53 +00:00
|
|
|
* By default: no instance option name
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
2008-09-02 08:58:53 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2008-09-15 07:56:26 +00:00
|
|
|
public static function get_instance_option_names() {
|
2008-09-02 08:58:53 +00:00
|
|
|
return array();
|
2008-06-30 05:24:00 +00:00
|
|
|
}
|
2010-07-05 07:27:49 +00:00
|
|
|
|
2012-05-12 04:14:53 +08:00
|
|
|
/**
|
|
|
|
* Validate repository plugin instance form
|
|
|
|
*
|
|
|
|
* @param moodleform $mform moodle form
|
|
|
|
* @param array $data form data
|
|
|
|
* @param array $errors errors
|
|
|
|
* @return array errors
|
|
|
|
*/
|
2010-08-09 09:03:07 +00:00
|
|
|
public static function instance_form_validation($mform, $data, $errors) {
|
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
|
2012-05-12 04:14:53 +08:00
|
|
|
/**
|
|
|
|
* Create a shorten filename
|
|
|
|
*
|
|
|
|
* @param string $str filename
|
|
|
|
* @param int $maxlength max file name length
|
|
|
|
* @return string short filename
|
|
|
|
*/
|
2010-06-22 03:55:45 +00:00
|
|
|
public function get_short_filename($str, $maxlength) {
|
2011-09-10 10:21:31 +02:00
|
|
|
if (textlib::strlen($str) >= $maxlength) {
|
|
|
|
return trim(textlib::substr($str, 0, $maxlength)).'...';
|
2010-06-22 03:55:45 +00:00
|
|
|
} else {
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
}
|
2011-05-02 10:11:19 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Overwrite an existing file
|
|
|
|
*
|
|
|
|
* @param int $itemid
|
|
|
|
* @param string $filepath
|
|
|
|
* @param string $filename
|
|
|
|
* @param string $newfilepath
|
|
|
|
* @param string $newfilename
|
2012-05-12 04:14:53 +08:00
|
|
|
* @return bool
|
2011-05-02 10:11:19 +08:00
|
|
|
*/
|
2012-04-10 14:38:30 +08:00
|
|
|
public static function overwrite_existing_draftfile($itemid, $filepath, $filename, $newfilepath, $newfilename) {
|
2011-05-02 10:11:19 +08:00
|
|
|
global $USER;
|
|
|
|
$fs = get_file_storage();
|
|
|
|
$user_context = get_context_instance(CONTEXT_USER, $USER->id);
|
|
|
|
if ($file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $filepath, $filename)) {
|
|
|
|
if ($tempfile = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $newfilepath, $newfilename)) {
|
|
|
|
// delete existing file to release filename
|
|
|
|
$file->delete();
|
|
|
|
// create new file
|
|
|
|
$newfile = $fs->create_file_from_storedfile(array('filepath'=>$filepath, 'filename'=>$filename), $tempfile);
|
|
|
|
// remove temp file
|
|
|
|
$tempfile->delete();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a temp file from draft area
|
|
|
|
*
|
|
|
|
* @param int $draftitemid
|
|
|
|
* @param string $filepath
|
|
|
|
* @param string $filename
|
2012-05-12 04:14:53 +08:00
|
|
|
* @return bool
|
2011-05-02 10:11:19 +08:00
|
|
|
*/
|
2012-04-10 14:38:30 +08:00
|
|
|
public static function delete_tempfile_from_draft($draftitemid, $filepath, $filename) {
|
2011-05-02 10:11:19 +08:00
|
|
|
global $USER;
|
|
|
|
$fs = get_file_storage();
|
|
|
|
$user_context = get_context_instance(CONTEXT_USER, $USER->id);
|
|
|
|
if ($file = $fs->get_file($user_context->id, 'user', 'draft', $draftitemid, $filepath, $filename)) {
|
|
|
|
$file->delete();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-05-12 04:14:53 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find all external files in this repo and import them
|
|
|
|
*/
|
|
|
|
public function convert_references_to_local() {
|
|
|
|
$fs = get_file_storage();
|
|
|
|
$files = $fs->get_external_files($this->id);
|
|
|
|
foreach ($files as $storedfile) {
|
|
|
|
$fs->import_external_file($storedfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-14 16:21:04 +08:00
|
|
|
/**
|
|
|
|
* Called from phpunit between tests, resets whatever was cached
|
|
|
|
*/
|
|
|
|
public static function reset_caches() {
|
|
|
|
self::sync_external_file(null, true);
|
|
|
|
}
|
2012-05-12 04:14:53 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Call to request proxy file sync with repository source.
|
|
|
|
*
|
|
|
|
* @param stored_file $file
|
2012-06-14 16:21:04 +08:00
|
|
|
* @param bool $resetsynchistory whether to reset all history of sync (used by phpunit)
|
2012-05-12 04:14:53 +08:00
|
|
|
* @return bool success
|
|
|
|
*/
|
2012-06-14 16:21:04 +08:00
|
|
|
public static function sync_external_file($file, $resetsynchistory = false) {
|
2012-05-12 04:14:53 +08:00
|
|
|
global $DB;
|
2012-06-14 16:21:04 +08:00
|
|
|
// TODO MDL-25290 static should be replaced with MUC code.
|
2012-06-13 12:11:06 +08:00
|
|
|
static $synchronized = array();
|
2012-06-14 16:21:04 +08:00
|
|
|
if ($resetsynchistory) {
|
|
|
|
$synchronized = array();
|
|
|
|
}
|
2012-05-12 04:14:53 +08:00
|
|
|
|
|
|
|
$fs = get_file_storage();
|
|
|
|
|
2012-06-14 16:21:04 +08:00
|
|
|
if (!$file || !$file->get_referencefileid()) {
|
2012-06-13 12:11:06 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (array_key_exists($file->get_id(), $synchronized)) {
|
|
|
|
return $synchronized[$file->get_id()];
|
|
|
|
}
|
|
|
|
|
|
|
|
// remember that we already cached in current request to prevent from querying again
|
|
|
|
$synchronized[$file->get_id()] = false;
|
|
|
|
|
2012-05-12 04:14:53 +08:00
|
|
|
if (!$reference = $DB->get_record('files_reference', array('id'=>$file->get_referencefileid()))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($reference->lastsync) and ($reference->lastsync + $reference->lifetime > time())) {
|
2012-06-13 12:11:06 +08:00
|
|
|
$synchronized[$file->get_id()] = true;
|
|
|
|
return true;
|
2012-05-12 04:14:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$repository = self::get_repository_by_id($reference->repositoryid, SYSCONTEXTID)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$repository->sync_individual_file($file)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fileinfo = $repository->get_file_by_reference($reference);
|
|
|
|
if ($fileinfo === null) {
|
|
|
|
// does not exist any more - set status to missing
|
2012-06-13 12:11:06 +08:00
|
|
|
$file->set_missingsource();
|
2012-05-12 04:14:53 +08:00
|
|
|
//TODO: purge content from pool if we set some other content hash and it is no used any more
|
2012-06-13 12:11:06 +08:00
|
|
|
$synchronized[$file->get_id()] = true;
|
2012-05-12 04:14:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$contenthash = null;
|
|
|
|
$filesize = null;
|
|
|
|
if (!empty($fileinfo->contenthash)) {
|
|
|
|
// contenthash returned, file already in moodle
|
|
|
|
$contenthash = $fileinfo->contenthash;
|
|
|
|
$filesize = $fileinfo->filesize;
|
|
|
|
} else if (!empty($fileinfo->filepath)) {
|
|
|
|
// File path returned
|
|
|
|
list($contenthash, $filesize, $newfile) = $fs->add_file_to_pool($fileinfo->filepath);
|
|
|
|
} else if (!empty($fileinfo->handle) && is_resource($fileinfo->handle)) {
|
|
|
|
// File handle returned
|
|
|
|
$contents = '';
|
|
|
|
while (!feof($fileinfo->handle)) {
|
|
|
|
$contents .= fread($handle, 8192);
|
|
|
|
}
|
|
|
|
fclose($fileinfo->handle);
|
|
|
|
list($contenthash, $filesize, $newfile) = $fs->add_string_to_pool($content);
|
|
|
|
} else if (isset($fileinfo->content)) {
|
|
|
|
// File content returned
|
|
|
|
list($contenthash, $filesize, $newfile) = $fs->add_string_to_pool($fileinfo->content);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($contenthash) or !isset($filesize)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update files table
|
2012-06-13 12:11:06 +08:00
|
|
|
$file->set_synchronized($contenthash, $filesize);
|
|
|
|
$synchronized[$file->get_id()] = true;
|
2012-05-12 04:14:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
2012-06-10 17:47:00 +10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Build draft file's source field
|
|
|
|
*
|
|
|
|
* {@link file_restore_source_field_from_draft_file()}
|
|
|
|
* XXX: This is a hack for file manager (MDL-28666)
|
|
|
|
* For newly created draft files we have to construct
|
|
|
|
* source filed in php serialized data format.
|
|
|
|
* File manager needs to know the original file information before copying
|
|
|
|
* to draft area, so we append these information in mdl_files.source field
|
|
|
|
*
|
|
|
|
* @param string $source
|
|
|
|
* @return string serialised source field
|
|
|
|
*/
|
|
|
|
public static function build_source_field($source) {
|
|
|
|
$sourcefield = new stdClass;
|
|
|
|
$sourcefield->source = $source;
|
|
|
|
return serialize($sourcefield);
|
|
|
|
}
|
2008-06-30 05:24:00 +00:00
|
|
|
}
|
2008-08-08 03:31:51 +00:00
|
|
|
|
|
|
|
/**
|
2009-11-06 09:07:46 +00:00
|
|
|
* Exception class for repository api
|
|
|
|
*
|
|
|
|
* @since 2.0
|
2012-05-12 04:14:53 +08:00
|
|
|
* @package repository
|
|
|
|
* @category repository
|
|
|
|
* @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
|
2009-11-06 09:07:46 +00:00
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2008-08-08 03:31:51 +00:00
|
|
|
*/
|
2008-08-13 04:09:13 +00:00
|
|
|
class repository_exception extends moodle_exception {
|
2008-06-30 05:24:00 +00:00
|
|
|
}
|
2008-08-07 08:36:12 +00:00
|
|
|
|
2008-08-28 06:15:19 +00:00
|
|
|
/**
|
2009-11-06 09:07:46 +00:00
|
|
|
* This is a class used to define a repository instance form
|
|
|
|
*
|
|
|
|
* @since 2.0
|
2012-05-12 04:14:53 +08:00
|
|
|
* @package repository
|
|
|
|
* @category repository
|
|
|
|
* @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
|
2009-11-06 09:07:46 +00:00
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2008-08-28 06:15:19 +00:00
|
|
|
*/
|
2008-09-02 08:58:53 +00:00
|
|
|
final class repository_instance_form extends moodleform {
|
2012-05-12 04:14:53 +08:00
|
|
|
/** @var stdClass repository instance */
|
2008-08-13 04:09:13 +00:00
|
|
|
protected $instance;
|
2012-05-12 04:14:53 +08:00
|
|
|
/** @var string repository plugin type */
|
2008-08-13 04:09:13 +00:00
|
|
|
protected $plugin;
|
2012-05-12 04:14:53 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Added defaults to moodle form
|
|
|
|
*/
|
2010-06-22 07:40:30 +00:00
|
|
|
protected function add_defaults() {
|
2008-08-13 04:09:13 +00:00
|
|
|
$mform =& $this->_form;
|
|
|
|
$strrequired = get_string('required');
|
|
|
|
|
|
|
|
$mform->addElement('hidden', 'edit', ($this->instance) ? $this->instance->id : 0);
|
2009-09-26 17:07:08 +00:00
|
|
|
$mform->setType('edit', PARAM_INT);
|
2008-08-13 04:09:13 +00:00
|
|
|
$mform->addElement('hidden', 'new', $this->plugin);
|
2009-09-26 17:07:08 +00:00
|
|
|
$mform->setType('new', PARAM_FORMAT);
|
2008-08-13 04:09:13 +00:00
|
|
|
$mform->addElement('hidden', 'plugin', $this->plugin);
|
2011-09-24 15:07:27 +02:00
|
|
|
$mform->setType('plugin', PARAM_PLUGIN);
|
2008-08-26 07:20:56 +00:00
|
|
|
$mform->addElement('hidden', 'typeid', $this->typeid);
|
2009-09-26 17:07:08 +00:00
|
|
|
$mform->setType('typeid', PARAM_INT);
|
2008-09-05 06:30:18 +00:00
|
|
|
$mform->addElement('hidden', 'contextid', $this->contextid);
|
2009-09-26 17:07:08 +00:00
|
|
|
$mform->setType('contextid', PARAM_INT);
|
2008-08-13 04:09:13 +00:00
|
|
|
|
|
|
|
$mform->addElement('text', 'name', get_string('name'), 'maxlength="100" size="30"');
|
|
|
|
$mform->addRule('name', $strrequired, 'required', null, 'client');
|
2012-06-19 11:20:21 +08:00
|
|
|
$mform->setType('name', PARAM_TEXT);
|
2010-06-22 07:40:30 +00:00
|
|
|
}
|
2008-08-13 04:09:13 +00:00
|
|
|
|
2012-05-12 04:14:53 +08:00
|
|
|
/**
|
|
|
|
* Define moodle form elements
|
|
|
|
*/
|
2010-06-22 07:40:30 +00:00
|
|
|
public function definition() {
|
|
|
|
global $CFG;
|
|
|
|
// type of plugin, string
|
|
|
|
$this->plugin = $this->_customdata['plugin'];
|
|
|
|
$this->typeid = $this->_customdata['typeid'];
|
|
|
|
$this->contextid = $this->_customdata['contextid'];
|
|
|
|
$this->instance = (isset($this->_customdata['instance'])
|
|
|
|
&& is_subclass_of($this->_customdata['instance'], 'repository'))
|
|
|
|
? $this->_customdata['instance'] : null;
|
|
|
|
|
|
|
|
$mform =& $this->_form;
|
2008-09-19 06:43:06 +00:00
|
|
|
|
2010-06-22 07:40:30 +00:00
|
|
|
$this->add_defaults();
|
2012-06-12 22:15:09 +08:00
|
|
|
|
|
|
|
// Add instance config options.
|
|
|
|
$result = repository::static_function($this->plugin, 'instance_config_form', $mform);
|
|
|
|
if ($result === false) {
|
|
|
|
// Remove the name element if no other config options.
|
|
|
|
$mform->removeElement('name');
|
|
|
|
}
|
|
|
|
if ($this->instance) {
|
2008-10-09 03:02:26 +00:00
|
|
|
$data = array();
|
|
|
|
$data['name'] = $this->instance->name;
|
|
|
|
if (!$this->instance->readonly) {
|
|
|
|
// and set the data if we have some.
|
2008-09-19 06:43:06 +00:00
|
|
|
foreach ($this->instance->get_instance_option_names() as $config) {
|
2009-06-04 06:40:35 +00:00
|
|
|
if (!empty($this->instance->options[$config])) {
|
|
|
|
$data[$config] = $this->instance->options[$config];
|
2008-09-19 06:43:06 +00:00
|
|
|
} else {
|
|
|
|
$data[$config] = '';
|
|
|
|
}
|
2008-08-26 07:20:56 +00:00
|
|
|
}
|
2008-08-13 04:09:13 +00:00
|
|
|
}
|
2008-10-09 03:02:26 +00:00
|
|
|
$this->set_data($data);
|
2008-09-19 06:43:06 +00:00
|
|
|
}
|
|
|
|
|
2010-06-22 07:40:30 +00:00
|
|
|
if ($result === false) {
|
|
|
|
$mform->addElement('cancel');
|
|
|
|
} else {
|
|
|
|
$this->add_action_buttons(true, get_string('save','repository'));
|
|
|
|
}
|
2008-08-13 04:09:13 +00:00
|
|
|
}
|
|
|
|
|
2012-05-12 04:14:53 +08:00
|
|
|
/**
|
|
|
|
* Validate moodle form data
|
|
|
|
*
|
|
|
|
* @param array $data form data
|
|
|
|
* @param array $files files in form
|
|
|
|
* @return array errors
|
|
|
|
*/
|
2012-03-18 18:31:32 +01:00
|
|
|
public function validation($data, $files) {
|
2008-08-13 04:09:13 +00:00
|
|
|
global $DB;
|
|
|
|
$errors = array();
|
2010-08-09 09:03:07 +00:00
|
|
|
$plugin = $this->_customdata['plugin'];
|
|
|
|
$instance = (isset($this->_customdata['instance'])
|
|
|
|
&& is_subclass_of($this->_customdata['instance'], 'repository'))
|
|
|
|
? $this->_customdata['instance'] : null;
|
|
|
|
if (!$instance) {
|
|
|
|
$errors = repository::static_function($plugin, 'instance_form_validation', $this, $data, $errors);
|
|
|
|
} else {
|
|
|
|
$errors = $instance->instance_form_validation($this, $data, $errors);
|
|
|
|
}
|
|
|
|
|
2010-07-03 14:33:48 +00:00
|
|
|
$sql = "SELECT count('x')
|
|
|
|
FROM {repository_instances} i, {repository} r
|
|
|
|
WHERE r.type=:plugin AND r.id=i.typeid AND i.name=:name";
|
2009-04-03 07:31:43 +00:00
|
|
|
if ($DB->count_records_sql($sql, array('name' => $data['name'], 'plugin' => $data['plugin'])) > 1) {
|
2010-08-09 09:03:07 +00:00
|
|
|
$errors['name'] = get_string('erroruniquename', 'repository');
|
2008-08-13 04:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
}
|
2008-09-02 08:58:53 +00:00
|
|
|
|
|
|
|
/**
|
2009-11-06 09:07:46 +00:00
|
|
|
* This is a class used to define a repository type setting form
|
|
|
|
*
|
|
|
|
* @since 2.0
|
2012-05-12 04:14:53 +08:00
|
|
|
* @package repository
|
|
|
|
* @category repository
|
|
|
|
* @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
|
2009-11-06 09:07:46 +00:00
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2008-09-02 08:58:53 +00:00
|
|
|
*/
|
2008-09-18 05:48:25 +00:00
|
|
|
final class repository_type_form extends moodleform {
|
2012-05-12 04:14:53 +08:00
|
|
|
/** @var stdClass repository instance */
|
2008-09-02 08:58:53 +00:00
|
|
|
protected $instance;
|
2012-05-12 04:14:53 +08:00
|
|
|
/** @var string repository plugin name */
|
2008-09-02 08:58:53 +00:00
|
|
|
protected $plugin;
|
2012-05-12 04:14:53 +08:00
|
|
|
/** @var string action */
|
2010-04-30 08:03:32 +00:00
|
|
|
protected $action;
|
2008-09-02 08:58:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Definition of the moodleform
|
|
|
|
*/
|
|
|
|
public function definition() {
|
|
|
|
global $CFG;
|
|
|
|
// type of plugin, string
|
|
|
|
$this->plugin = $this->_customdata['plugin'];
|
|
|
|
$this->instance = (isset($this->_customdata['instance'])
|
|
|
|
&& is_a($this->_customdata['instance'], 'repository_type'))
|
|
|
|
? $this->_customdata['instance'] : null;
|
|
|
|
|
2010-04-30 08:03:32 +00:00
|
|
|
$this->action = $this->_customdata['action'];
|
2010-07-27 09:03:54 +00:00
|
|
|
$this->pluginname = $this->_customdata['pluginname'];
|
2008-09-02 08:58:53 +00:00
|
|
|
$mform =& $this->_form;
|
|
|
|
$strrequired = get_string('required');
|
2008-09-04 07:03:01 +00:00
|
|
|
|
2010-04-30 08:03:32 +00:00
|
|
|
$mform->addElement('hidden', 'action', $this->action);
|
|
|
|
$mform->setType('action', PARAM_TEXT);
|
|
|
|
$mform->addElement('hidden', 'repos', $this->plugin);
|
2011-09-24 15:07:27 +02:00
|
|
|
$mform->setType('repos', PARAM_PLUGIN);
|
2008-10-22 05:49:15 +00:00
|
|
|
|
2008-09-18 02:36:17 +00:00
|
|
|
// let the plugin add its specific fields
|
2010-07-27 09:03:54 +00:00
|
|
|
$classname = 'repository_' . $this->plugin;
|
|
|
|
require_once($CFG->dirroot . '/repository/' . $this->plugin . '/lib.php');
|
2008-10-09 03:02:26 +00:00
|
|
|
//add "enable course/user instances" checkboxes if multiple instances are allowed
|
2008-11-26 07:03:10 +00:00
|
|
|
$instanceoptionnames = repository::static_function($this->plugin, 'get_instance_option_names');
|
2010-08-02 06:33:34 +00:00
|
|
|
|
|
|
|
$result = call_user_func(array($classname, 'type_config_form'), $mform, $classname);
|
|
|
|
|
2010-07-27 09:03:54 +00:00
|
|
|
if (!empty($instanceoptionnames)) {
|
2010-08-02 06:33:34 +00:00
|
|
|
$sm = get_string_manager();
|
|
|
|
$component = 'repository';
|
|
|
|
if ($sm->string_exists('enablecourseinstances', 'repository_' . $this->plugin)) {
|
|
|
|
$component .= ('_' . $this->plugin);
|
|
|
|
}
|
|
|
|
$mform->addElement('checkbox', 'enablecourseinstances', get_string('enablecourseinstances', $component));
|
|
|
|
|
|
|
|
$component = 'repository';
|
|
|
|
if ($sm->string_exists('enableuserinstances', 'repository_' . $this->plugin)) {
|
|
|
|
$component .= ('_' . $this->plugin);
|
|
|
|
}
|
|
|
|
$mform->addElement('checkbox', 'enableuserinstances', get_string('enableuserinstances', $component));
|
2008-10-09 03:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// set the data if we have some.
|
2008-09-02 08:58:53 +00:00
|
|
|
if ($this->instance) {
|
|
|
|
$data = array();
|
2008-09-18 03:19:52 +00:00
|
|
|
$option_names = call_user_func(array($classname,'get_type_option_names'));
|
2008-10-09 03:02:26 +00:00
|
|
|
if (!empty($instanceoptionnames)){
|
|
|
|
$option_names[] = 'enablecourseinstances';
|
|
|
|
$option_names[] = 'enableuserinstances';
|
|
|
|
}
|
|
|
|
|
2008-09-02 08:58:53 +00:00
|
|
|
$instanceoptions = $this->instance->get_options();
|
|
|
|
foreach ($option_names as $config) {
|
|
|
|
if (!empty($instanceoptions[$config])) {
|
|
|
|
$data[$config] = $instanceoptions[$config];
|
|
|
|
} else {
|
|
|
|
$data[$config] = '';
|
|
|
|
}
|
|
|
|
}
|
2010-07-27 09:03:54 +00:00
|
|
|
// XXX: set plugin name for plugins which doesn't have muliti instances
|
|
|
|
if (empty($instanceoptionnames)){
|
|
|
|
$data['pluginname'] = $this->pluginname;
|
|
|
|
}
|
2008-09-02 08:58:53 +00:00
|
|
|
$this->set_data($data);
|
|
|
|
}
|
2008-11-26 07:03:10 +00:00
|
|
|
|
2008-09-03 11:02:25 +00:00
|
|
|
$this->add_action_buttons(true, get_string('save','repository'));
|
2008-09-02 08:58:53 +00:00
|
|
|
}
|
2011-07-28 16:06:25 +01:00
|
|
|
|
2012-05-12 04:14:53 +08:00
|
|
|
/**
|
|
|
|
* Validate moodle form data
|
|
|
|
*
|
|
|
|
* @param array $data moodle form data
|
|
|
|
* @param array $files
|
|
|
|
* @return array errors
|
|
|
|
*/
|
2012-03-18 18:31:32 +01:00
|
|
|
public function validation($data, $files) {
|
2011-07-28 16:06:25 +01:00
|
|
|
$errors = array();
|
|
|
|
$plugin = $this->_customdata['plugin'];
|
|
|
|
$instance = (isset($this->_customdata['instance'])
|
|
|
|
&& is_subclass_of($this->_customdata['instance'], 'repository'))
|
|
|
|
? $this->_customdata['instance'] : null;
|
|
|
|
if (!$instance) {
|
|
|
|
$errors = repository::static_function($plugin, 'type_form_validation', $this, $data, $errors);
|
|
|
|
} else {
|
|
|
|
$errors = $instance->type_form_validation($this, $data, $errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $errors;
|
|
|
|
}
|
2008-09-02 08:58:53 +00:00
|
|
|
}
|
|
|
|
|
2010-03-16 03:29:45 +00:00
|
|
|
/**
|
|
|
|
* Generate all options needed by filepicker
|
|
|
|
*
|
2012-05-12 04:14:53 +08:00
|
|
|
* @param array $args including following keys
|
2010-03-16 03:29:45 +00:00
|
|
|
* context
|
|
|
|
* accepted_types
|
|
|
|
* return_types
|
|
|
|
*
|
|
|
|
* @return array the list of repository instances, including meta infomation, containing the following keys
|
|
|
|
* externallink
|
|
|
|
* repositories
|
|
|
|
* accepted_types
|
|
|
|
*/
|
2010-01-15 07:48:38 +00:00
|
|
|
function initialise_filepicker($args) {
|
2009-07-03 06:38:41 +00:00
|
|
|
global $CFG, $USER, $PAGE, $OUTPUT;
|
2012-04-26 15:28:53 +08:00
|
|
|
static $templatesinitialized;
|
2010-03-29 03:39:08 +00:00
|
|
|
require_once($CFG->libdir . '/licenselib.php');
|
2010-03-31 07:41:31 +00:00
|
|
|
|
2010-09-21 08:54:01 +00:00
|
|
|
$return = new stdClass();
|
2010-03-31 03:08:05 +00:00
|
|
|
$licenses = array();
|
2010-03-31 07:41:31 +00:00
|
|
|
if (!empty($CFG->licenses)) {
|
|
|
|
$array = explode(',', $CFG->licenses);
|
|
|
|
foreach ($array as $license) {
|
2010-09-21 08:54:01 +00:00
|
|
|
$l = new stdClass();
|
2010-03-31 07:41:31 +00:00
|
|
|
$l->shortname = $license;
|
|
|
|
$l->fullname = get_string($license, 'license');
|
|
|
|
$licenses[] = $l;
|
|
|
|
}
|
2010-03-31 03:08:05 +00:00
|
|
|
}
|
2010-05-19 02:50:45 +00:00
|
|
|
if (!empty($CFG->sitedefaultlicense)) {
|
|
|
|
$return->defaultlicense = $CFG->sitedefaultlicense;
|
|
|
|
}
|
2010-03-29 03:39:08 +00:00
|
|
|
|
|
|
|
$return->licenses = $licenses;
|
|
|
|
|
|
|
|
$return->author = fullname($USER);
|
|
|
|
|
2010-01-15 07:48:38 +00:00
|
|
|
if (empty($args->context)) {
|
|
|
|
$context = $PAGE->context;
|
2009-04-20 08:53:21 +00:00
|
|
|
} else {
|
2010-01-15 07:48:38 +00:00
|
|
|
$context = $args->context;
|
|
|
|
}
|
2010-05-31 03:03:21 +00:00
|
|
|
$disable_types = array();
|
|
|
|
if (!empty($args->disable_types)) {
|
|
|
|
$disable_types = $args->disable_types;
|
|
|
|
}
|
2010-01-15 07:48:38 +00:00
|
|
|
|
2009-04-20 08:53:21 +00:00
|
|
|
$user_context = get_context_instance(CONTEXT_USER, $USER->id);
|
2010-01-15 07:48:38 +00:00
|
|
|
|
2010-09-15 05:53:44 +00:00
|
|
|
list($context, $course, $cm) = get_context_info_array($context->id);
|
|
|
|
$contexts = array($user_context, get_system_context());
|
|
|
|
if (!empty($course)) {
|
|
|
|
// adding course context
|
|
|
|
$contexts[] = get_context_instance(CONTEXT_COURSE, $course->id);
|
|
|
|
}
|
2010-01-15 07:48:38 +00:00
|
|
|
$externallink = (int)get_config(null, 'repositoryallowexternallinks');
|
|
|
|
$repositories = repository::get_instances(array(
|
2010-09-15 05:53:44 +00:00
|
|
|
'context'=>$contexts,
|
2010-01-15 07:48:38 +00:00
|
|
|
'currentcontext'=> $context,
|
2010-05-11 06:49:51 +00:00
|
|
|
'accepted_types'=>$args->accepted_types,
|
2010-05-31 03:03:21 +00:00
|
|
|
'return_types'=>$args->return_types,
|
|
|
|
'disable_types'=>$disable_types
|
2010-01-15 07:48:38 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
$return->repositories = array();
|
|
|
|
|
|
|
|
if (empty($externallink)) {
|
|
|
|
$return->externallink = false;
|
|
|
|
} else {
|
|
|
|
$return->externallink = true;
|
|
|
|
}
|
|
|
|
|
2012-06-11 14:55:13 +08:00
|
|
|
$return->userprefs = array();
|
|
|
|
$return->userprefs['recentrepository'] = get_user_preferences('filepicker_recentrepository', '');
|
|
|
|
$return->userprefs['recentlicense'] = get_user_preferences('filepicker_recentlicense', '');
|
|
|
|
$return->userprefs['recentviewmode'] = get_user_preferences('filepicker_recentviewmode', '');
|
|
|
|
|
|
|
|
user_preference_allow_ajax_update('filepicker_recentrepository', PARAM_INT);
|
|
|
|
user_preference_allow_ajax_update('filepicker_recentlicense', PARAM_SAFEDIR);
|
|
|
|
user_preference_allow_ajax_update('filepicker_recentviewmode', PARAM_INT);
|
|
|
|
|
|
|
|
|
2010-01-15 07:48:38 +00:00
|
|
|
// provided by form element
|
2012-05-21 15:17:53 +08:00
|
|
|
$return->accepted_types = file_get_typegroup('extension', $args->accepted_types);
|
2010-07-27 04:06:09 +00:00
|
|
|
$return->return_types = $args->return_types;
|
2010-01-15 07:48:38 +00:00
|
|
|
foreach ($repositories as $repository) {
|
|
|
|
$meta = $repository->get_meta();
|
2012-01-10 12:21:24 +13:00
|
|
|
// Please note that the array keys for repositories are used within
|
|
|
|
// JavaScript a lot, the key NEEDS to be the repository id.
|
2012-01-10 12:18:34 +13:00
|
|
|
$return->repositories[$repository->id] = $meta;
|
2010-01-15 07:48:38 +00:00
|
|
|
}
|
2012-04-26 15:28:53 +08:00
|
|
|
if (!$templatesinitialized) {
|
|
|
|
// we need to send filepicker templates to the browser just once
|
|
|
|
$fprenderer = $PAGE->get_renderer('core', 'files');
|
|
|
|
$templates = $fprenderer->filepicker_js_templates();
|
|
|
|
$PAGE->requires->js_init_call('M.core_filepicker.set_templates', array($templates), true);
|
|
|
|
$templatesinitialized = true;
|
|
|
|
}
|
2010-01-15 07:48:38 +00:00
|
|
|
return $return;
|
2009-04-20 08:53:21 +00:00
|
|
|
}
|
2011-05-02 10:11:19 +08:00
|
|
|
/**
|
|
|
|
* Small function to walk an array to attach repository ID
|
2012-05-12 04:14:53 +08:00
|
|
|
*
|
2011-05-02 10:11:19 +08:00
|
|
|
* @param array $value
|
|
|
|
* @param string $key
|
|
|
|
* @param int $id
|
|
|
|
*/
|
|
|
|
function repository_attach_id(&$value, $key, $id){
|
|
|
|
$value['repo_id'] = $id;
|
|
|
|
}
|