2008-09-05 08:51:25 +00:00
|
|
|
<?php
|
|
|
|
// $Id$
|
2008-06-25 08:09:25 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// //
|
|
|
|
// NOTICE OF COPYRIGHT //
|
|
|
|
// //
|
|
|
|
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
|
|
|
|
// http://moodle.com //
|
|
|
|
// //
|
|
|
|
// Copyright (C) 2008 onwards Moodle Pty Ltd http://moodle.com //
|
|
|
|
// //
|
|
|
|
// This program 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 2 of the License, or //
|
|
|
|
// (at your option) any later version. //
|
|
|
|
// //
|
|
|
|
// This program 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: //
|
|
|
|
// //
|
|
|
|
// http://www.gnu.org/copyleft/gpl.html //
|
|
|
|
// //
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2008-09-08 09:41:38 +00:00
|
|
|
/**
|
|
|
|
* About repository/lib.php:
|
|
|
|
* two main classes:
|
|
|
|
* 1. repository_type => a repository plugin, You can activate a plugin into
|
|
|
|
* Moodle. You also can set some general settings/options for this type of repository.
|
|
|
|
* All instances would share the same options (for example: a API key for the connection
|
|
|
|
* to the repository)
|
|
|
|
* 2. repository => an instance of a plugin. You can also call it an access or
|
|
|
|
* an account. An instance has specific settings (for example: a public url) and a specific
|
|
|
|
* name. That's this name which is displayed in the file picker.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-07-28 08:24:55 +00:00
|
|
|
require_once(dirname(dirname(__FILE__)) . '/config.php');
|
2008-09-16 03:52:11 +00:00
|
|
|
require_once(dirname(dirname(__FILE__)) . '/lib/filelib.php');
|
|
|
|
require_once(dirname(dirname(__FILE__)) . '/lib/formslib.php');
|
|
|
|
// File picker javascript code
|
2008-06-25 08:09:25 +00:00
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
|
2008-10-09 03:02:26 +00:00
|
|
|
/**
|
|
|
|
* Return if the instance is visible in a context
|
|
|
|
* TODO: check if the context visibility has been overwritten by the plugin creator
|
|
|
|
* (need to create special functions to be overvwritten in repository class)
|
|
|
|
* @param objet $contextlevel - context level
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function get_contextvisibility($contextlevel) {
|
2008-11-26 07:03:10 +00:00
|
|
|
|
2008-10-09 03:02:26 +00:00
|
|
|
if ($contextlevel == CONTEXT_COURSE) {
|
|
|
|
return $this->_options['enablecourseinstances'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($contextlevel == CONTEXT_USER) {
|
|
|
|
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
|
2009-04-21 10:02:59 +00:00
|
|
|
* @global object $CFG
|
2008-09-02 08:58:53 +00:00
|
|
|
* @param integer $typename
|
|
|
|
* @param array $typeoptions
|
|
|
|
* @param boolean $visible
|
|
|
|
* @param integer $sortorder (don't really need set, it will be during create() call)
|
|
|
|
*/
|
2008-09-15 07:56:26 +00:00
|
|
|
public function __construct($typename = '', $typeoptions = array(), $visible = false, $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();
|
2008-11-26 07:03:10 +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) {
|
2008-09-15 07:56:26 +00:00
|
|
|
if (array_key_exists($config,$typeoptions)) {
|
|
|
|
$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()
|
|
|
|
* @return String the type name
|
|
|
|
*/
|
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
|
|
|
|
* @return string user-friendly type name
|
|
|
|
*/
|
2008-09-15 07:56:26 +00:00
|
|
|
public function get_readablename() {
|
2008-09-02 08:58:53 +00:00
|
|
|
return get_string('repositoryname','repository_'.$this->_typename);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return general options
|
|
|
|
* @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
|
|
|
|
* @return boolean
|
|
|
|
*/
|
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
|
|
|
|
* @return integer
|
|
|
|
*/
|
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)
|
2009-03-05 04:40:51 +00:00
|
|
|
* @param boolean throw exception?
|
|
|
|
* @return mixed return int if create successfully, return false if
|
|
|
|
* any errors
|
2008-09-02 08:58:53 +00:00
|
|
|
* @global object $DB
|
|
|
|
*/
|
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
|
|
|
|
$newtype = new stdclass;
|
|
|
|
$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();
|
|
|
|
|
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-11-26 07:03:10 +00:00
|
|
|
$instanceoptionnames = repository::static_function($this->_typename, 'get_instance_option_names');
|
2008-09-18 05:21:51 +00:00
|
|
|
if (empty($instanceoptionnames)) {
|
2008-09-15 07:56:26 +00:00
|
|
|
$instanceoptions = array();
|
|
|
|
$instanceoptions['name'] = $this->_typename;
|
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
|
|
|
|
* @param array $options
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2008-09-15 07:56:26 +00:00
|
|
|
public function update_options($options = null) {
|
|
|
|
if (!empty($options)) {
|
2008-09-02 08:58:53 +00:00
|
|
|
$this->_options = $options;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->_options as $name => $value) {
|
|
|
|
set_config($name,$value,$this->_typename);
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
* @global object $DB
|
|
|
|
* @param boolean $visible
|
|
|
|
* @return boolean
|
|
|
|
*/
|
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()
|
|
|
|
* @global object $DB
|
|
|
|
* @param integer $sortorder
|
|
|
|
* @return boolean
|
|
|
|
*/
|
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
|
|
|
|
* @global object $DB
|
|
|
|
* @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
|
|
|
|
|
|
|
/// retrieve this type into the returned array
|
|
|
|
$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++;
|
|
|
|
}
|
|
|
|
|
2008-09-15 07:56:26 +00: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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 1. Switch the visibility OFF if it's ON, and ON if it's OFF.
|
|
|
|
* 2. Update the type
|
2009-04-21 10:02:59 +00:00
|
|
|
* @return boolean
|
2008-09-02 08:58:53 +00:00
|
|
|
*/
|
2008-09-15 07:56:26 +00:00
|
|
|
public function switch_and_update_visibility() {
|
2008-09-02 08:58:53 +00:00
|
|
|
$this->_visible = !$this->_visible;
|
|
|
|
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)
|
2008-09-02 08:58:53 +00:00
|
|
|
* @global object $DB
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2008-09-15 07:56:26 +00:00
|
|
|
public function delete() {
|
2008-09-02 08:58:53 +00:00
|
|
|
global $DB;
|
2008-09-03 11:02:25 +00:00
|
|
|
|
|
|
|
//delete all instances of this type
|
2008-11-26 07:03:10 +00:00
|
|
|
$instances = repository::get_instances(array(),null,false,$this->_typename);
|
2008-09-15 07:56:26 +00:00
|
|
|
foreach ($instances as $instance) {
|
2008-09-03 11:02:25 +00:00
|
|
|
$instance->delete();
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2008-09-02 08:58:53 +00:00
|
|
|
return $DB->delete_records('repository', array('type' => $this->_typename));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-09-16 05:33:55 +00:00
|
|
|
* This is the base class of the repository class
|
|
|
|
*
|
|
|
|
* To use repository plugin, see:
|
|
|
|
* http://docs.moodle.org/en/Development:Repository_How_to_Create_Plugin
|
|
|
|
*
|
|
|
|
* class repository is an abstract class, some functions must be implemented in subclass.
|
|
|
|
*
|
|
|
|
* See an example: repository/boxnet/repository.class.php
|
|
|
|
*
|
|
|
|
* A few notes:
|
|
|
|
* // for ajax file picker, this will print a json string to tell file picker
|
|
|
|
* // how to build a login form
|
|
|
|
* $repo->print_login();
|
|
|
|
* // for ajax file picker, this will return a files list.
|
|
|
|
* $repo->get_listing();
|
|
|
|
* // this function will be used for non-javascript version.
|
|
|
|
* $repo->print_listing();
|
|
|
|
* // print a search box
|
|
|
|
* $repo->print_search();
|
|
|
|
*
|
|
|
|
* @package repository
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
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
|
|
|
|
public $disabled = false;
|
2008-08-13 04:09:13 +00:00
|
|
|
public $id;
|
|
|
|
public $context;
|
|
|
|
public $options;
|
2008-09-16 09:08:36 +00:00
|
|
|
public $readonly;
|
2008-06-25 08:09:25 +00:00
|
|
|
|
2008-11-26 07:03:10 +00:00
|
|
|
/**
|
|
|
|
* Return a type for a given type name.
|
|
|
|
* @global object $DB
|
|
|
|
* @param string $typename the type name
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a type for a given type id.
|
|
|
|
* @global object $DB
|
|
|
|
* @param string $typename the type name
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all repository types ordered by sortorder
|
|
|
|
* first type in returnedarray[0], second type in returnedarray[1], ...
|
|
|
|
* @global object $DB
|
|
|
|
* @param boolean $visible can return types by visiblity, return all types if null
|
|
|
|
* @return array Repository types
|
|
|
|
*/
|
|
|
|
public static function get_types($visible=null) {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$types = array();
|
|
|
|
$params = null;
|
|
|
|
if (!empty($visible)) {
|
|
|
|
$params = array('visible' => $visible);
|
|
|
|
}
|
|
|
|
if ($records = $DB->get_records('repository',$params,'sortorder')) {
|
|
|
|
foreach($records as $type) {
|
|
|
|
$types[] = new repository_type($type->type, (array)get_config($type->type), $type->visible, $type->sortorder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $types;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check context
|
|
|
|
* @param int $ctx_id
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public static function check_context($ctx_id) {
|
|
|
|
global $USER;
|
|
|
|
|
|
|
|
$context = get_context_instance_by_id($ctx_id);
|
|
|
|
$level = $context->contextlevel;
|
|
|
|
|
|
|
|
if ($level == CONTEXT_COURSE) {
|
|
|
|
if (!has_capability('moodle/course:view', $context)) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if ($level == CONTEXT_USER) {
|
|
|
|
$c = get_context_instance(CONTEXT_USER, $USER->id);
|
|
|
|
if ($c->id == $ctx_id) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if ($level == CONTEXT_SYSTEM) {
|
|
|
|
// it is always ok in system level
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* @param object $context the context for which we want the editable types
|
|
|
|
* @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)) {
|
|
|
|
if ($type->get_contextvisibility($context->contextlevel)) {
|
|
|
|
$editabletypes[]=$type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $editabletypes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return repository instances
|
|
|
|
* @global object $DB
|
|
|
|
* @global object $CFG
|
|
|
|
* @global object $USER
|
|
|
|
* @param object $contexts contexts for which the instances are set
|
|
|
|
* @param integer $userid
|
|
|
|
* @param boolean $onlyvisible if visible == true, return visible instances only,
|
|
|
|
* otherwise, return all instances
|
|
|
|
* @param string $type a type name to retrieve
|
|
|
|
* @return array repository instances
|
|
|
|
*/
|
2008-12-09 02:11:57 +00:00
|
|
|
public static function get_instances($contexts=array(), $userid = null, $onlyvisible = true, $type=null, $accepted_types = '*', $returnvalue = '*') {
|
2008-11-26 07:03:10 +00:00
|
|
|
global $DB, $CFG, $USER;
|
|
|
|
|
|
|
|
$params = array();
|
|
|
|
$sql = 'SELECT i.*, r.type AS repositorytype, r.sortorder, r.visible FROM {repository} r, {repository_instances} i WHERE ';
|
|
|
|
$sql .= 'i.typeid = r.id ';
|
|
|
|
|
|
|
|
if (!empty($userid) && is_numeric($userid)) {
|
|
|
|
$sql .= ' AND (i.userid = 0 or i.userid = ?)';
|
|
|
|
$params[] = $userid;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($contexts as $context) {
|
|
|
|
if (empty($firstcontext)) {
|
|
|
|
$firstcontext = true;
|
|
|
|
$sql .= ' AND ((i.contextid = ?)';
|
|
|
|
} else {
|
|
|
|
$sql .= ' OR (i.contextid = ?)';
|
|
|
|
}
|
|
|
|
$params[] = $context->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($firstcontext)) {
|
|
|
|
$sql .=')';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($onlyvisible == true) {
|
|
|
|
$sql .= ' AND (r.visible = 1)';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($type)) {
|
|
|
|
$sql .= ' AND (r.type = ?)';
|
|
|
|
$params[] = $type;
|
|
|
|
}
|
|
|
|
$sql .= ' order by r.sortorder, i.name';
|
|
|
|
|
|
|
|
if (!$repos = $DB->get_records_sql($sql, $params)) {
|
|
|
|
$repos = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$ret = array();
|
2008-12-09 02:11:57 +00:00
|
|
|
$ft = new file_type_to_ext();
|
2008-11-26 07:03:10 +00:00
|
|
|
foreach ($repos as $repo) {
|
|
|
|
require_once($CFG->dirroot . '/repository/'. $repo->repositorytype.'/repository.class.php');
|
|
|
|
$options['visible'] = $repo->visible;
|
|
|
|
$options['name'] = $repo->name;
|
|
|
|
$options['type'] = $repo->repositorytype;
|
|
|
|
$options['typeid'] = $repo->typeid;
|
2008-12-09 02:11:57 +00:00
|
|
|
// tell instance what file types will be accepted by file picker
|
|
|
|
$options['accepted_types'] = $ft->get_file_ext($accepted_types);
|
2008-11-26 07:03:10 +00:00
|
|
|
$classname = 'repository_' . $repo->repositorytype;//
|
2008-12-08 05:19:09 +00:00
|
|
|
$is_supported = true;
|
2008-11-26 07:03:10 +00:00
|
|
|
|
|
|
|
$repository = new $classname($repo->id, $repo->contextid, $options, $repo->readonly);
|
2009-01-08 06:10:29 +00:00
|
|
|
if (empty($repository->super_called)) {
|
|
|
|
debugging('parent::__construct must be called by '.$repo->repositorytype.' plugin.');
|
|
|
|
} else {
|
|
|
|
if ($accepted_types !== '*' and $repository->supported_filetypes() !== '*') {
|
|
|
|
$accepted_types = $ft->get_file_ext($accepted_types);
|
|
|
|
$supported_filetypes = $ft->get_file_ext($repository->supported_filetypes());
|
|
|
|
$is_supported = false;
|
|
|
|
foreach ($supported_filetypes as $type) {
|
|
|
|
if (in_array($type, $accepted_types)) {
|
|
|
|
$is_supported = true;
|
|
|
|
}
|
2008-12-08 05:19:09 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-08 06:10:29 +00:00
|
|
|
if ($returnvalue !== '*' and $repository->supported_return_value() !== '*') {
|
|
|
|
$tmp = $repository->supported_return_value();
|
|
|
|
if ($tmp != $returnvalue) {
|
|
|
|
$is_supported = false;
|
|
|
|
}
|
2008-12-08 05:19:09 +00:00
|
|
|
}
|
2009-01-08 06:10:29 +00:00
|
|
|
if (!$onlyvisible || ($repository->is_visible() && !$repository->disabled)) {
|
|
|
|
// super_called will make sure the parent construct function is called
|
|
|
|
// by repository construct function
|
|
|
|
if ($is_supported) {
|
|
|
|
$ret[] = $repository;
|
|
|
|
}
|
2008-12-08 05:19:09 +00:00
|
|
|
}
|
2008-11-26 07:03:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get single repository instance
|
|
|
|
* @global object $DB
|
|
|
|
* @global object $CFG
|
|
|
|
* @param integer $id repository id
|
|
|
|
* @return object repository instance
|
|
|
|
*/
|
|
|
|
public static function get_instance($id) {
|
|
|
|
global $DB, $CFG;
|
|
|
|
$sql = 'SELECT i.*, r.type AS repositorytype, r.visible FROM {repository} r, {repository_instances} i WHERE ';
|
|
|
|
$sql .= 'i.typeid = r.id AND ';
|
|
|
|
$sql .= 'i.id = '.$id;
|
|
|
|
|
|
|
|
if(!$instance = $DB->get_record_sql($sql)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
require_once($CFG->dirroot . '/repository/'. $instance->repositorytype
|
|
|
|
. '/repository.class.php');
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* call a static function
|
2009-04-21 10:02:59 +00:00
|
|
|
* @global object $CFG
|
|
|
|
* @param string $plugin
|
|
|
|
* @param string $function
|
2008-11-26 07:03:10 +00:00
|
|
|
* @param type $nocallablereturnvalue default value if function not found
|
|
|
|
* it's mostly used when you don't want to display an error but
|
|
|
|
* return a boolean
|
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
|
|
|
|
$typedirectory = $CFG->dirroot . '/repository/'. $plugin . '/repository.class.php';
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
array_shift($args);
|
|
|
|
array_shift($args);
|
|
|
|
}
|
|
|
|
|
|
|
|
require_once($typedirectory);
|
|
|
|
return call_user_func_array(array('repository_' . $plugin, $function), $args);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move file from download folder to file pool using FILE API
|
|
|
|
* @global object $DB
|
|
|
|
* @global object $CFG
|
|
|
|
* @global object $USER
|
|
|
|
* @param string $path file path in download folder
|
|
|
|
* @param string $name file name
|
|
|
|
* @param integer $itemid item id to identify a file in filepool
|
|
|
|
* @param string $filearea file area
|
|
|
|
* @return array information of file in file pool
|
|
|
|
*/
|
|
|
|
public static function move_to_filepool($path, $name, $itemid, $filearea = 'user_draft') {
|
|
|
|
global $DB, $CFG, $USER;
|
|
|
|
$context = get_context_instance(CONTEXT_USER, $USER->id);
|
|
|
|
$now = time();
|
|
|
|
$entry = new object();
|
|
|
|
$entry->filearea = $filearea;
|
|
|
|
$entry->contextid = $context->id;
|
|
|
|
$entry->filename = $name;
|
2008-12-02 07:05:15 +00:00
|
|
|
//$entry->filepath = '/'.uniqid().'/';
|
|
|
|
$entry->filepath = '/';
|
2008-11-26 07:03:10 +00:00
|
|
|
$entry->timecreated = $now;
|
|
|
|
$entry->timemodified = $now;
|
2008-12-02 07:05:15 +00:00
|
|
|
$entry->userid = $USER->id;
|
|
|
|
$entry->mimetype = mimeinfo('type', $path);
|
2008-11-26 07:03:10 +00:00
|
|
|
if(is_numeric($itemid)) {
|
|
|
|
$entry->itemid = $itemid;
|
|
|
|
} else {
|
|
|
|
$entry->itemid = 0;
|
|
|
|
}
|
|
|
|
$fs = get_file_storage();
|
|
|
|
$browser = get_file_browser();
|
2008-12-02 07:05:15 +00:00
|
|
|
if ($existingfile = $fs->get_file($context->id, $filearea, $itemid, $path, $name)) {
|
|
|
|
$existingfile->delete();
|
|
|
|
}
|
2008-11-26 07:03:10 +00:00
|
|
|
if ($file = $fs->create_file_from_pathname($entry, $path)) {
|
2009-03-13 02:52:12 +00:00
|
|
|
if (empty($CFG->repository_no_delete)) {
|
|
|
|
$delete = unlink($path);
|
|
|
|
unset($CFG->repository_no_delete);
|
|
|
|
}
|
2008-11-26 07:03:10 +00:00
|
|
|
$ret = $browser->get_file_info($context, $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename());
|
|
|
|
if(!empty($ret)) {
|
2008-12-02 07:05:15 +00:00
|
|
|
return array('url'=>$ret->get_url(),
|
|
|
|
'id'=>$file->get_itemid(),
|
|
|
|
'file'=>$file->get_filename(),
|
2009-01-14 03:26:47 +00:00
|
|
|
'icon'=>$CFG->pixpath.'/f/'.mimeinfo('icon32', $path)
|
2008-12-02 07:05:15 +00:00
|
|
|
);
|
2008-11-26 07:03:10 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save file to local filesystem pool
|
|
|
|
* @param string $elname name of element
|
|
|
|
* @param string $filearea
|
|
|
|
* @param string $filepath
|
|
|
|
* @param string $filename - use specified filename, if not specified name of uploaded file used
|
|
|
|
* @param bool $override override file if exists
|
|
|
|
* @return mixed stored_file object or false if error; may throw exception if duplicate found
|
|
|
|
*/
|
2008-12-02 07:05:15 +00:00
|
|
|
public static function store_to_filepool($elname, $filearea='user_draft', $filepath='/', $itemid='', $filename = '', $override = false) {
|
2008-11-26 07:03:10 +00:00
|
|
|
global $USER;
|
|
|
|
if (!isset($_FILES[$elname])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$filename) {
|
|
|
|
$filename = $_FILES[$elname]['name'];
|
|
|
|
}
|
|
|
|
$context = get_context_instance(CONTEXT_USER, $USER->id);
|
2008-12-02 07:05:15 +00:00
|
|
|
if (empty($itemid)) {
|
|
|
|
$itemid = (int)substr(hexdec(uniqid()), 0, 9)+rand(1,100);
|
|
|
|
}
|
2008-11-26 07:03:10 +00:00
|
|
|
$fs = get_file_storage();
|
|
|
|
$browser = get_file_browser();
|
|
|
|
|
|
|
|
if ($file = $fs->get_file($context->id, $filearea, $itemid, $filepath, $filename)) {
|
|
|
|
if ($override) {
|
|
|
|
$file->delete();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$file_record = new object();
|
|
|
|
$file_record->contextid = $context->id;
|
|
|
|
$file_record->filearea = $filearea;
|
|
|
|
$file_record->itemid = $itemid;
|
|
|
|
$file_record->filepath = $filepath;
|
|
|
|
$file_record->filename = $filename;
|
|
|
|
$file_record->userid = $USER->id;
|
|
|
|
|
|
|
|
$file = $fs->create_file_from_pathname($file_record, $_FILES[$elname]['tmp_name']);
|
|
|
|
$info = $browser->get_file_info($context, $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename());
|
|
|
|
$ret = array('url'=>$info->get_url(),'id'=>$itemid, 'file'=>$file->get_filename());
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-04-17 08:00:59 +00:00
|
|
|
* Return the user files tree in a format to be returned by the function get_listing
|
2009-04-21 10:02:59 +00:00
|
|
|
* @global object $CFG
|
|
|
|
* @param string $search
|
|
|
|
* @return array
|
2008-11-26 07:03:10 +00:00
|
|
|
*/
|
|
|
|
public static function get_user_file_tree($search = ""){
|
|
|
|
global $CFG;
|
|
|
|
$ret = array();
|
|
|
|
$ret['nologin'] = true;
|
|
|
|
$ret['manage'] = $CFG->wwwroot .'/files/index.php'; // temporary
|
|
|
|
$browser = get_file_browser();
|
|
|
|
$itemid = null;
|
|
|
|
$filename = null;
|
|
|
|
$filearea = null;
|
|
|
|
$path = '/';
|
|
|
|
$ret['dynload'] = false;
|
|
|
|
|
|
|
|
if ($fileinfo = $browser->get_file_info(get_system_context(), $filearea, $itemid, $path, $filename)) {
|
|
|
|
|
|
|
|
$ret['path'] = array();
|
|
|
|
$params = $fileinfo->get_params();
|
|
|
|
$filearea = $params['filearea'];
|
|
|
|
$ret['path'][] = repository::encode_path($filearea, $path, $fileinfo->get_visible_name());
|
|
|
|
if ($fileinfo->is_directory()) {
|
|
|
|
$level = $fileinfo->get_parent();
|
|
|
|
while ($level) {
|
|
|
|
$params = $level->get_params();
|
|
|
|
$ret['path'][] = repository::encode_path($params['filearea'], $params['filepath'], $level->get_visible_name());
|
|
|
|
$level = $level->get_parent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$filecount = repository::build_tree($fileinfo, $search, $ret['dynload'], $ret['list']);
|
|
|
|
$ret['path'] = array_reverse($ret['path']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($ret['list'])) {
|
|
|
|
//exit(mnet_server_fault(9016, get_string('emptyfilelist', 'repository_local')));
|
|
|
|
throw new Exception('emptyfilelist');
|
|
|
|
} else {
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2009-04-21 10:02:59 +00:00
|
|
|
* @param string $filearea
|
|
|
|
* @param string $path
|
|
|
|
* @param string $visiblename
|
|
|
|
* @return array
|
2008-11-26 07:03:10 +00:00
|
|
|
*/
|
|
|
|
public static function encode_path($filearea, $path, $visiblename) {
|
|
|
|
return array('path'=>serialize(array($filearea, $path)), 'name'=>$visiblename);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds a tree of files This function is
|
|
|
|
* then called recursively.
|
|
|
|
*
|
|
|
|
* @param $fileinfo an object returned by file_browser::get_file_info()
|
|
|
|
* @param $search searched string
|
|
|
|
* @param $dynamicmode bool no recursive call is done when in dynamic mode
|
|
|
|
* @param $list - the array containing the files under the passed $fileinfo
|
|
|
|
* @returns int the number of files found
|
|
|
|
*
|
|
|
|
* todo: take $search into account, and respect a threshold for dynamic loading
|
|
|
|
*/
|
|
|
|
public static function build_tree($fileinfo, $search, $dynamicmode, &$list) {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$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();
|
|
|
|
$path[] = repository::encode_path($params['filearea'], $params['filepath'], $level->get_visible_name());
|
|
|
|
$level = $level->get_parent();
|
|
|
|
}
|
|
|
|
|
|
|
|
$tmp = array(
|
|
|
|
'title' => $child->get_visible_name(),
|
|
|
|
'size' => 0,
|
|
|
|
'date' => $filedate,
|
|
|
|
'path' => array_reverse($path),
|
|
|
|
'thumbnail' => $CFG->pixpath .'/f/folder.gif'
|
|
|
|
);
|
|
|
|
|
|
|
|
//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;
|
|
|
|
}
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
//Uncomment this following line if you wanna display all directory ()even empty
|
|
|
|
//if (!$search || $_filecount || (stristr($tmp['title'], $search) !== false)) {
|
|
|
|
if ($_filecount) {
|
|
|
|
$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();
|
|
|
|
$source = serialize(array($params['contextid'], $params['filearea'], $params['itemid'], $params['filepath'], $params['filename']));
|
|
|
|
$list[] = array(
|
|
|
|
'title' => $filename,
|
|
|
|
'size' => $filesize,
|
|
|
|
'date' => $filedate,
|
|
|
|
//'source' => $child->get_url(),
|
|
|
|
'source' => base64_encode($source),
|
2009-01-14 03:26:47 +00:00
|
|
|
'thumbnail' => $CFG->pixpath .'/f/'. mimeinfo('icon32', $filename)
|
2008-11-26 07:03:10 +00:00
|
|
|
);
|
|
|
|
$filecount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $filecount;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a repository instance list (with edit/delete/create links)
|
|
|
|
* @global object $CFG
|
|
|
|
* @global object $USER
|
|
|
|
* @param object $context the context for which we display the instance
|
|
|
|
* @param string $typename if set, we display only one type of instance
|
|
|
|
*/
|
|
|
|
public static function display_instances_list($context, $typename = null) {
|
|
|
|
global $CFG, $USER;
|
|
|
|
|
|
|
|
$output = print_box_start('generalbox','',true);
|
|
|
|
//if the context is SYSTEM, so we call it from administration page
|
|
|
|
$admin = ($context->id == SYSCONTEXTID) ? true : false;
|
|
|
|
if ($admin) {
|
2009-01-01 14:25:29 +00:00
|
|
|
$baseurl = "$CFG->httpswwwroot/$CFG->admin/repositoryinstance.php?sesskey=" . sesskey();
|
2008-11-26 07:03:10 +00:00
|
|
|
$output .= "<div ><h2 style='text-align: center'>" . get_string('siteinstances', 'repository') . " ";
|
|
|
|
$output .= "</h2></div>";
|
|
|
|
} else {
|
|
|
|
$baseurl = $CFG->httpswwwroot . '/repository/manage_instances.php?contextid=' . $context->id . '&sesskey=' . sesskey();
|
|
|
|
}
|
|
|
|
|
|
|
|
$namestr = get_string('name');
|
|
|
|
$pluginstr = get_string('plugin', 'repository');
|
|
|
|
$settingsstr = get_string('settings');
|
|
|
|
$deletestr = get_string('delete');
|
|
|
|
$updown = get_string('updown', 'repository');
|
|
|
|
//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
|
|
|
|
//want to display only visible instances, but for every type types. The repository_get_instances()
|
|
|
|
//third parameter displays only visible type.
|
|
|
|
$instances = repository::get_instances(array($context),null,!$admin,$typename);
|
|
|
|
$instancesnumber = count($instances);
|
|
|
|
$alreadyplugins = array();
|
|
|
|
|
|
|
|
$table = new StdClass;
|
|
|
|
$table->head = array($namestr, $pluginstr, $deletestr, $settingsstr);
|
|
|
|
$table->align = array('left', 'left', 'center','center');
|
|
|
|
$table->data = array();
|
|
|
|
|
|
|
|
$updowncount = 1;
|
|
|
|
|
|
|
|
foreach ($instances as $i) {
|
|
|
|
$settings = '';
|
|
|
|
$delete = '';
|
|
|
|
$settings .= '<a href="' . $baseurl . '&type='.$typename.'&edit=' . $i->id . '">' . $settingsstr . '</a>' . "\n";
|
|
|
|
if (!$i->readonly) {
|
|
|
|
$delete .= '<a href="' . $baseurl . '&type='.$typename.'&delete=' . $i->id . '">' . $deletestr . '</a>' . "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$type = repository::get_type_by_id($i->typeid);
|
|
|
|
$table->data[] = array($i->name, $type->get_readablename(), $delete, $settings);
|
|
|
|
|
|
|
|
//display a grey row if the type is defined as not visible
|
|
|
|
if (isset($type) && !$type->get_visible()) {
|
|
|
|
$table->rowclass[] = 'dimmed_text';
|
|
|
|
} else {
|
|
|
|
$table->rowclass[] = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!in_array($i->name, $alreadyplugins)) {
|
|
|
|
$alreadyplugins[] = $i->name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$output .= print_table($table, true);
|
|
|
|
$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)) {
|
2009-04-03 02:26:43 +00:00
|
|
|
$instancehtml .= '<li><a href="'.$baseurl.'&new='.$type->get_typename().'">'.get_string('createxxinstance', 'repository', get_string('repositoryname', 'repository_'.$type->get_typename()))
|
|
|
|
.'</a></li>';
|
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;
|
|
|
|
$instancehtml .= "<form action='".$baseurl."&new=".$typename."' method='post'>
|
|
|
|
<p style='text-align:center'><input type='submit' value='".get_string('createinstance', 'repository')."'/></p>
|
|
|
|
</form>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($addable) {
|
|
|
|
$instancehtml .= '</div>';
|
|
|
|
$output .= $instancehtml;
|
|
|
|
}
|
|
|
|
|
|
|
|
$output .= print_box_end(true);
|
|
|
|
|
|
|
|
//print the list + creation links
|
|
|
|
print($output);
|
|
|
|
}
|
2008-06-25 08:09:25 +00:00
|
|
|
/**
|
2008-08-28 06:39:09 +00:00
|
|
|
* 1. Initialize context and options
|
|
|
|
* 2. Accept necessary parameters
|
|
|
|
*
|
2008-08-28 06:15:19 +00:00
|
|
|
* @param integer $repositoryid
|
|
|
|
* @param integer $contextid
|
|
|
|
* @param array $options
|
2008-06-25 08:09:25 +00:00
|
|
|
*/
|
2008-09-16 09:08:36 +00:00
|
|
|
public function __construct($repositoryid, $contextid = SITEID, $options = array(), $readonly = 0) {
|
2008-08-13 04:09:13 +00:00
|
|
|
$this->id = $repositoryid;
|
|
|
|
$this->context = get_context_instance_by_id($contextid);
|
2008-09-16 09:08:36 +00:00
|
|
|
$this->readonly = $readonly;
|
2008-08-13 04:09:13 +00:00
|
|
|
$this->options = array();
|
2008-06-27 05:48:24 +00:00
|
|
|
if (is_array($options)) {
|
2008-08-13 04:09:13 +00:00
|
|
|
$options = array_merge($this->get_option(), $options);
|
|
|
|
} else {
|
|
|
|
$options = $this->get_option();
|
|
|
|
}
|
2008-08-14 09:39:39 +00:00
|
|
|
$this->options = array();
|
2008-08-13 04:09:13 +00:00
|
|
|
foreach ($options as $n => $v) {
|
|
|
|
$this->options[$n] = $v;
|
2008-06-27 03:33:40 +00:00
|
|
|
}
|
2008-09-02 04:05:11 +00:00
|
|
|
$this->name = $this->get_name();
|
2009-01-08 06:10:29 +00:00
|
|
|
$this->super_called = true;
|
2008-06-25 08:09:25 +00:00
|
|
|
}
|
|
|
|
|
2008-08-28 06:15:19 +00:00
|
|
|
/**
|
2008-08-28 06:39:09 +00:00
|
|
|
* set options for repository instance
|
|
|
|
*
|
2008-08-28 06:15:19 +00:00
|
|
|
* @param string $name
|
2008-08-28 06:39:09 +00:00
|
|
|
* @param mixed $value
|
2008-08-28 06:15:19 +00:00
|
|
|
*/
|
2008-06-27 05:48:24 +00:00
|
|
|
public function __set($name, $value) {
|
2008-06-27 03:33:40 +00:00
|
|
|
$this->options[$name] = $value;
|
|
|
|
}
|
2008-06-25 08:09:25 +00:00
|
|
|
|
2008-08-28 06:15:19 +00:00
|
|
|
/**
|
2008-08-28 06:39:09 +00:00
|
|
|
* get options for repository instance
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return mixed
|
2008-08-28 06:15:19 +00:00
|
|
|
*/
|
2008-06-27 05:48:24 +00:00
|
|
|
public function __get($name) {
|
2008-09-15 07:56:26 +00:00
|
|
|
if (array_key_exists($name, $this->options)) {
|
2008-06-27 03:33:40 +00:00
|
|
|
return $this->options[$name];
|
|
|
|
}
|
|
|
|
trigger_error('Undefined property: '.$name, E_USER_NOTICE);
|
|
|
|
return null;
|
|
|
|
}
|
2008-06-25 08:09:25 +00:00
|
|
|
|
2008-08-28 06:15:19 +00:00
|
|
|
/**
|
2008-08-28 06:39:09 +00:00
|
|
|
* test option name
|
|
|
|
*
|
|
|
|
* @param string name
|
2008-08-28 06:15:19 +00:00
|
|
|
*/
|
2008-06-27 05:48:24 +00:00
|
|
|
public function __isset($name) {
|
2008-06-27 03:33:40 +00:00
|
|
|
return isset($this->options[$name]);
|
2008-06-25 08:09:25 +00:00
|
|
|
}
|
|
|
|
|
2008-08-28 06:15:19 +00:00
|
|
|
/**
|
2008-08-28 06:39:09 +00:00
|
|
|
* Return the name of the repository class
|
2009-04-21 10:02:59 +00:00
|
|
|
* @return string
|
2008-08-28 06:15:19 +00:00
|
|
|
*/
|
2008-06-27 05:48:24 +00:00
|
|
|
public function __toString() {
|
2008-06-27 03:33:40 +00:00
|
|
|
return 'Repository class: '.__CLASS__;
|
|
|
|
}
|
2008-08-13 04:09:13 +00:00
|
|
|
|
2008-07-16 05:15:14 +00:00
|
|
|
/**
|
2009-03-16 02:16:50 +00:00
|
|
|
* Decide where to save the file, can be
|
|
|
|
* reused by sub class
|
|
|
|
* @param string filename
|
2008-07-16 05:15:14 +00:00
|
|
|
*/
|
2009-03-16 02:16:50 +00:00
|
|
|
public function prepare_file($file) {
|
2008-07-16 05:15:14 +00:00
|
|
|
global $CFG;
|
2008-08-28 06:23:23 +00:00
|
|
|
if (!file_exists($CFG->dataroot.'/temp/download')) {
|
|
|
|
mkdir($CFG->dataroot.'/temp/download/', 0777, true);
|
2008-07-17 03:54:20 +00:00
|
|
|
}
|
2008-09-15 07:56:26 +00:00
|
|
|
if (is_dir($CFG->dataroot.'/temp/download')) {
|
2008-08-28 06:23:23 +00:00
|
|
|
$dir = $CFG->dataroot.'/temp/download/';
|
2008-07-17 03:54:20 +00:00
|
|
|
}
|
2008-09-15 07:56:26 +00:00
|
|
|
if (empty($file)) {
|
2008-07-18 07:44:14 +00:00
|
|
|
$file = uniqid('repo').'_'.time().'.tmp';
|
|
|
|
}
|
2008-09-15 07:56:26 +00:00
|
|
|
if (file_exists($dir.$file)) {
|
2008-07-18 07:44:14 +00:00
|
|
|
$file = uniqid('m').$file;
|
2008-07-16 05:15:14 +00:00
|
|
|
}
|
2009-03-16 02:16:50 +00:00
|
|
|
return $dir.$file;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Download a file, this function can be overridden by
|
|
|
|
* subclass.
|
|
|
|
*
|
|
|
|
* @global object $CFG
|
|
|
|
* @param string $url the url of file
|
|
|
|
* @param string $file save location
|
|
|
|
* @return string the location of the file
|
|
|
|
* @see curl package
|
|
|
|
*/
|
|
|
|
public function get_file($url, $file = '') {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$path = $this->prepare_file($file);
|
|
|
|
$fp = fopen($path, 'w');
|
2008-07-18 07:44:14 +00:00
|
|
|
$c = new curl;
|
|
|
|
$c->download(array(
|
2008-09-15 07:56:26 +00:00
|
|
|
array('url'=>$url, 'file'=>$fp)
|
|
|
|
));
|
2009-03-16 02:16:50 +00:00
|
|
|
return $path;
|
2008-06-27 03:33:40 +00:00
|
|
|
}
|
2008-06-25 08:09:25 +00:00
|
|
|
|
2008-10-09 03:02:26 +00:00
|
|
|
/**
|
|
|
|
* Return is the instance is visible
|
|
|
|
* (is the type visible ? is the context enable ?)
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2008-11-26 07:03:10 +00:00
|
|
|
public function is_visible() {
|
|
|
|
$type = repository::get_type_by_id($this->typeid);
|
|
|
|
$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
|
|
|
|
if (empty($instanceoptions) || $type->get_contextvisibility($this->context->contextlevel)) {
|
|
|
|
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.
|
2009-04-21 10:02:59 +00:00
|
|
|
* @global object $DB
|
|
|
|
* @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;
|
|
|
|
// We always verify instance id from database,
|
|
|
|
// so we always know repository name before init
|
|
|
|
// a repository, so we don't enquery repository
|
|
|
|
// name from database again here.
|
|
|
|
if (isset($this->options['name'])) {
|
|
|
|
return $this->options['name'];
|
|
|
|
} else {
|
|
|
|
if ( $repo = $DB->get_record('repository_instances', array('id'=>$this->id)) ) {
|
|
|
|
return $repo->name;
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-07-14 05:31:01 +00:00
|
|
|
|
2008-11-26 03:26:33 +00:00
|
|
|
/**
|
|
|
|
* what kind of files will be in this repository?
|
|
|
|
* @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 '*';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* does it return a file url or a item_id
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function supported_return_value() {
|
|
|
|
// return 'link';
|
|
|
|
// return 'ref_id';
|
2008-12-08 05:19:09 +00:00
|
|
|
return 'ref_id';
|
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
|
2008-08-28 06:15:19 +00:00
|
|
|
* @global object $CFG
|
2008-08-13 04:09:13 +00:00
|
|
|
* @return object
|
2008-06-27 03:33:40 +00:00
|
|
|
*/
|
2008-08-14 09:39:39 +00:00
|
|
|
final public function ajax_info() {
|
2008-08-13 04:09:13 +00:00
|
|
|
global $CFG;
|
2008-12-08 06:53:58 +00:00
|
|
|
$ft = new file_type_to_ext;
|
2008-08-13 04:09:13 +00:00
|
|
|
$repo = new stdclass;
|
2008-08-26 07:20:56 +00:00
|
|
|
$repo->id = $this->id;
|
2008-09-02 04:05:11 +00:00
|
|
|
$repo->name = $this->get_name();
|
2008-08-13 04:09:13 +00:00
|
|
|
$repo->type = $this->options['type'];
|
2008-09-08 03:07:07 +00:00
|
|
|
$repo->icon = $CFG->httpswwwroot.'/repository/'.$repo->type.'/icon.png';
|
2008-12-09 02:11:57 +00:00
|
|
|
$repo->supported_types = $ft->get_file_ext($this->supported_filetypes());
|
|
|
|
$repo->accepted_types = $this->accepted_types;
|
2008-08-13 04:09:13 +00:00
|
|
|
return $repo;
|
|
|
|
}
|
2008-06-25 08:09:25 +00:00
|
|
|
|
2008-08-14 09:39:39 +00:00
|
|
|
/**
|
|
|
|
* Create an instance for this plug-in
|
2008-08-28 06:15:19 +00:00
|
|
|
* @global object $CFG
|
|
|
|
* @global object $DB
|
|
|
|
* @param string $type the type of the repository
|
|
|
|
* @param integer $userid the user id
|
|
|
|
* @param object $context the context
|
|
|
|
* @param array $params the options for this instance
|
2009-04-21 10:02:59 +00:00
|
|
|
* @return mixed
|
2008-08-14 09:39:39 +00:00
|
|
|
*/
|
2008-09-16 09:08:36 +00:00
|
|
|
final public static function create($type, $userid, $context, $params, $readonly=0) {
|
2008-08-14 09:39:39 +00:00
|
|
|
global $CFG, $DB;
|
|
|
|
$params = (array)$params;
|
|
|
|
require_once($CFG->dirroot . '/repository/'. $type . '/repository.class.php');
|
|
|
|
$classname = 'repository_' . $type;
|
2008-08-28 02:23:09 +00:00
|
|
|
if ($repo = $DB->get_record('repository', array('type'=>$type))) {
|
|
|
|
$record = new stdclass;
|
|
|
|
$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) {
|
|
|
|
$options[$config] = $params[$config];
|
|
|
|
}
|
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
|
2008-08-28 06:15:19 +00:00
|
|
|
* @global object $DB
|
2009-04-21 10:02:59 +00:00
|
|
|
* @return mixed
|
2008-06-27 03:33:40 +00:00
|
|
|
*/
|
2008-09-15 07:56:26 +00:00
|
|
|
final public function delete() {
|
2008-08-13 04:09:13 +00:00
|
|
|
global $DB;
|
2008-08-26 08:00:47 +00:00
|
|
|
$DB->delete_records('repository_instances', array('id'=>$this->id));
|
2009-03-05 04:59:48 +00:00
|
|
|
$DB->delete_records('repository_instance_config', array('instanceid'=>$this->id));
|
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
|
2008-08-28 06:15:19 +00:00
|
|
|
* @global object $DB
|
|
|
|
* @param string $hide
|
2009-04-21 10:02:59 +00:00
|
|
|
* @return boolean
|
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-06-27 03:33:40 +00:00
|
|
|
* Cache login details for repositories
|
2008-08-28 06:15:19 +00:00
|
|
|
* @global object $DB
|
2008-06-27 03:33:40 +00:00
|
|
|
* @param string $username
|
|
|
|
* @param string $password
|
2008-08-28 06:15:19 +00:00
|
|
|
* @param integer $userid The id of specific user
|
|
|
|
* @return integer Id of the record
|
2008-06-25 08:09:25 +00:00
|
|
|
*/
|
2008-08-07 03:33:47 +00:00
|
|
|
public function store_login($username = '', $password = '', $userid = 1) {
|
2008-06-27 05:48:24 +00:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$repository = new stdclass;
|
2008-08-13 04:09:13 +00:00
|
|
|
if (!empty($this->id)) {
|
|
|
|
$repository->id = $this->id;
|
2008-07-28 08:24:55 +00:00
|
|
|
} else {
|
|
|
|
$repository->userid = $userid;
|
|
|
|
$repository->repositorytype = $this->type;
|
2008-08-07 03:33:47 +00:00
|
|
|
$repository->contextid = $this->context->id;
|
2008-07-28 08:24:55 +00:00
|
|
|
}
|
2008-06-27 05:48:24 +00:00
|
|
|
if ($entry = $DB->get_record('repository', $repository)) {
|
|
|
|
$repository->id = $entry->id;
|
|
|
|
$repository->username = $username;
|
|
|
|
$repository->password = $password;
|
|
|
|
return $DB->update_record('repository', $repository);
|
|
|
|
} else {
|
|
|
|
$repository->username = $username;
|
|
|
|
$repository->password = $password;
|
|
|
|
return $DB->insert_record('repository', $repository);
|
|
|
|
}
|
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'));
|
|
|
|
* @global object $DB
|
|
|
|
* @param array $options settings
|
2008-08-13 04:09:13 +00:00
|
|
|
* @return int Id of the record
|
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'])) {
|
|
|
|
$r = new object();
|
|
|
|
$r->id = $this->id;
|
|
|
|
$r->name = $options['name'];
|
|
|
|
$DB->update_record('repository_instances', $r);
|
|
|
|
unset($options['name']);
|
|
|
|
}
|
2008-10-30 07:12:54 +00:00
|
|
|
$result = true;
|
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))) {
|
|
|
|
if ($value===null) {
|
2008-10-30 07:12:54 +00:00
|
|
|
$result = $result && $DB->delete_records('repository_instance_config', array('name'=>$name, 'instanceid'=>$this->id));
|
2008-08-26 07:20:56 +00:00
|
|
|
} else {
|
2008-10-30 07:12:54 +00:00
|
|
|
$result = $result && $DB->set_field('repository_instance_config', 'value', $value, array('id'=>$id));
|
2008-08-26 07:20:56 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($value===null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$config = new object();
|
|
|
|
$config->instanceid = $this->id;
|
|
|
|
$config->name = $name;
|
|
|
|
$config->value = $value;
|
2008-10-30 07:12:54 +00:00
|
|
|
$result = $result && $DB->insert_record('repository_instance_config', $config);
|
2008-08-26 07:20:56 +00:00
|
|
|
}
|
2008-08-13 04:09:13 +00:00
|
|
|
}
|
2008-10-30 07:12:54 +00:00
|
|
|
return $result;
|
2008-06-25 08:09:25 +00:00
|
|
|
}
|
|
|
|
|
2008-08-13 04:09:13 +00:00
|
|
|
/**
|
|
|
|
* Get settings for repository instance
|
2008-08-28 06:15:19 +00:00
|
|
|
* @global object $DB
|
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)) {
|
|
|
|
return $ret[$config];
|
|
|
|
} else {
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
}
|
2008-07-23 04:43:53 +00:00
|
|
|
|
2008-12-09 02:11:57 +00:00
|
|
|
public function filter(&$value) {
|
|
|
|
$pass = false;
|
|
|
|
$accepted_types = optional_param('accepted_types', '', PARAM_RAW);
|
|
|
|
$ft = new file_type_to_ext;
|
|
|
|
$ext = $ft->get_file_ext($this->supported_filetypes());
|
2009-04-17 08:00:59 +00:00
|
|
|
if (isset($value['children'])) {
|
2008-12-09 02:11:57 +00:00
|
|
|
$pass = true;
|
2009-04-17 08:00:59 +00:00
|
|
|
if (!empty($value['children'])) {
|
|
|
|
$value['children'] = array_filter($value['children'], array($this, 'filter'));
|
2009-01-08 05:12:49 +00:00
|
|
|
}
|
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))) {
|
2008-12-09 02:11:57 +00:00
|
|
|
$pass = true;
|
|
|
|
} elseif (is_array($accepted_types)) {
|
|
|
|
foreach ($accepted_types as $type) {
|
|
|
|
if (preg_match('#'.$type.'$#', $value['title'])) {
|
|
|
|
$pass = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $pass;
|
|
|
|
}
|
|
|
|
|
2008-08-13 04:09:13 +00:00
|
|
|
/**
|
|
|
|
* Given a path, and perhaps a search, get a list of files.
|
|
|
|
*
|
2009-04-21 10:02:59 +00:00
|
|
|
* See details on http://docs.moodle.org/en/Development:Repository_plugins
|
2008-08-22 05:59:08 +00:00
|
|
|
*
|
2008-08-13 04:09:13 +00:00
|
|
|
* @param string $parent The parent path, this parameter can
|
|
|
|
* a folder name, or a identification of folder
|
|
|
|
* @return array the list of files, including meta infomation
|
|
|
|
*/
|
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
|
|
|
/**
|
2008-09-16 03:11:17 +00:00
|
|
|
* Search files in repository
|
|
|
|
* When doing global search, $search_text will be used as
|
2008-11-26 07:03:10 +00:00
|
|
|
* keyword.
|
2008-09-16 03:11:17 +00:00
|
|
|
*
|
2008-09-12 08:16:09 +00:00
|
|
|
* @return mixed, see get_listing()
|
|
|
|
*/
|
2008-09-16 03:11:17 +00:00
|
|
|
public function search($search_text) {
|
|
|
|
$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.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
*/
|
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
|
|
|
|
* @return null
|
|
|
|
*/
|
2008-09-12 07:28:40 +00:00
|
|
|
public function print_search() {
|
2009-04-21 05:53:35 +00:00
|
|
|
$str = '';
|
|
|
|
$str .= '<input type="hidden" name="repo_id" value="'.$this->id.'" />';
|
|
|
|
$str .= '<input type="hidden" name="ctx_id" value="'.$this->context->id.'" />';
|
|
|
|
$str .= '<input type="hidden" name="seekey" value="'.sesskey().'" />';
|
|
|
|
$str .= '<label>'.get_string('keyword', 'repository').': </label><br/><input name="s" value="" /><br/>';
|
|
|
|
return $str;
|
2008-09-12 07:28:40 +00:00
|
|
|
}
|
2008-08-13 04:09:13 +00:00
|
|
|
|
2008-09-08 05:38:13 +00:00
|
|
|
/**
|
|
|
|
* is it possible to do glboal search?
|
|
|
|
* @return boolean
|
|
|
|
*/
|
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
|
2009-04-21 10:02:59 +00:00
|
|
|
* @return boolean
|
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)
|
2008-10-27 02:57:29 +00:00
|
|
|
* @return boolean 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
|
|
|
|
* @param object $ Moodle form (passed by reference)
|
2008-09-02 08:58:53 +00:00
|
|
|
*/
|
2008-09-18 03:06:10 +00:00
|
|
|
public function type_config_form(&$mform) {
|
2008-08-13 04:09:13 +00:00
|
|
|
}
|
2008-11-26 07:03:10 +00:00
|
|
|
|
2008-09-18 02:36:17 +00:00
|
|
|
/**
|
|
|
|
* Edit/Create Instance Settings Moodle form
|
|
|
|
* @param object $ Moodle form (passed by reference)
|
2008-09-02 08:58:53 +00:00
|
|
|
*/
|
2008-09-18 02:36:17 +00:00
|
|
|
public 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
|
|
|
/**
|
|
|
|
* Return names of the general options
|
|
|
|
* By default: no general option name
|
|
|
|
* @return array
|
|
|
|
*/
|
2008-09-18 03:19:52 +00:00
|
|
|
public static function get_type_option_names() {
|
2008-09-02 08:58:53 +00:00
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return names of the instance options
|
|
|
|
* By default: no instance option name
|
|
|
|
* @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
|
|
|
}
|
2008-10-23 09:51:48 +00:00
|
|
|
|
2008-10-29 06:42:48 +00:00
|
|
|
/**
|
|
|
|
* Override it if you need to implement need mnet function
|
|
|
|
* @return array
|
|
|
|
*/
|
2008-10-23 09:51:48 +00:00
|
|
|
public static function mnet_publishes() {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2008-06-30 05:24:00 +00:00
|
|
|
}
|
2008-08-08 03:31:51 +00:00
|
|
|
|
|
|
|
/**
|
2008-08-13 04:09:13 +00:00
|
|
|
* exception class for repository api
|
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-10-30 02:30:26 +00:00
|
|
|
|
|
|
|
|
2008-08-28 06:15:19 +00:00
|
|
|
/**
|
|
|
|
* TODO: write comment
|
|
|
|
*/
|
2008-09-02 08:58:53 +00:00
|
|
|
final class repository_instance_form extends moodleform {
|
2008-08-13 04:09:13 +00:00
|
|
|
protected $instance;
|
|
|
|
protected $plugin;
|
|
|
|
|
2008-08-28 06:15:19 +00:00
|
|
|
/**
|
|
|
|
* TODO: write comment
|
2009-04-21 10:02:59 +00:00
|
|
|
* @global object $CFG
|
2008-08-28 06:15:19 +00:00
|
|
|
*/
|
2008-08-13 04:09:13 +00:00
|
|
|
public function definition() {
|
|
|
|
global $CFG;
|
|
|
|
// type of plugin, string
|
|
|
|
$this->plugin = $this->_customdata['plugin'];
|
2008-08-26 07:20:56 +00:00
|
|
|
$this->typeid = $this->_customdata['typeid'];
|
2008-09-05 06:30:18 +00:00
|
|
|
$this->contextid = $this->_customdata['contextid'];
|
2008-08-13 04:09:13 +00:00
|
|
|
$this->instance = (isset($this->_customdata['instance'])
|
|
|
|
&& is_subclass_of($this->_customdata['instance'], 'repository'))
|
|
|
|
? $this->_customdata['instance'] : null;
|
|
|
|
|
|
|
|
$mform =& $this->_form;
|
|
|
|
$strrequired = get_string('required');
|
|
|
|
|
|
|
|
$mform->addElement('hidden', 'edit', ($this->instance) ? $this->instance->id : 0);
|
|
|
|
$mform->addElement('hidden', 'new', $this->plugin);
|
|
|
|
$mform->addElement('hidden', 'plugin', $this->plugin);
|
2008-08-26 07:20:56 +00:00
|
|
|
$mform->addElement('hidden', 'typeid', $this->typeid);
|
2008-09-05 06:30:18 +00:00
|
|
|
$mform->addElement('hidden', 'contextid', $this->contextid);
|
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');
|
|
|
|
|
2008-09-19 06:43:06 +00:00
|
|
|
|
2008-10-09 03:02:26 +00:00
|
|
|
//add fields
|
|
|
|
if (!$this->instance) {
|
2008-11-26 07:03:10 +00:00
|
|
|
$result = repository::static_function($this->plugin, 'instance_config_form', $mform);
|
2008-10-09 03:02:26 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$data = array();
|
|
|
|
$data['name'] = $this->instance->name;
|
|
|
|
if (!$this->instance->readonly) {
|
|
|
|
$result = $this->instance->instance_config_form($mform);
|
|
|
|
// 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) {
|
|
|
|
if (!empty($this->instance->$config)) {
|
|
|
|
$data[$config] = $this->instance->$config;
|
|
|
|
} 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
|
|
|
}
|
|
|
|
|
2008-09-03 11:02:25 +00:00
|
|
|
$this->add_action_buttons(true, get_string('save','repository'));
|
2008-08-13 04:09:13 +00:00
|
|
|
}
|
|
|
|
|
2008-08-28 06:15:19 +00:00
|
|
|
/**
|
|
|
|
* TODO: write comment
|
2009-04-21 10:02:59 +00:00
|
|
|
* @global object $DB
|
|
|
|
* @param mixed $data
|
|
|
|
* @return mixed
|
2008-08-28 06:15:19 +00:00
|
|
|
*/
|
2008-08-13 04:09:13 +00:00
|
|
|
public function validation($data) {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$errors = array();
|
2009-04-03 07:31:43 +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";
|
|
|
|
if ($DB->count_records_sql($sql, array('name' => $data['name'], 'plugin' => $data['plugin'])) > 1) {
|
2008-08-13 04:09:13 +00:00
|
|
|
$errors = array('name' => get_string('err_uniquename', 'repository'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
}
|
2008-09-02 08:58:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a form with the general option fields of a type
|
|
|
|
*/
|
2008-09-18 05:48:25 +00:00
|
|
|
final class repository_type_form extends moodleform {
|
2008-09-02 08:58:53 +00:00
|
|
|
protected $instance;
|
|
|
|
protected $plugin;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Definition of the moodleform
|
|
|
|
* @global object $CFG
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
|
|
|
|
$mform =& $this->_form;
|
|
|
|
$strrequired = get_string('required');
|
2008-09-04 07:03:01 +00:00
|
|
|
|
2008-09-02 08:58:53 +00:00
|
|
|
$mform->addElement('hidden', 'edit', ($this->instance) ? $this->instance->get_typename() : 0);
|
|
|
|
$mform->addElement('hidden', 'new', $this->plugin);
|
|
|
|
$mform->addElement('hidden', 'plugin', $this->plugin);
|
2008-10-22 05:49:15 +00:00
|
|
|
|
2008-09-18 02:36:17 +00:00
|
|
|
// let the plugin add its specific fields
|
|
|
|
if (!$this->instance) {
|
2009-03-13 02:52:12 +00:00
|
|
|
$result = repository::static_function($this->plugin, 'type_config_form', $mform);
|
|
|
|
} else {
|
|
|
|
$classname = 'repository_' . $this->instance->get_typename();
|
|
|
|
$result = call_user_func(array($classname, 'type_config_form'), $mform);
|
2008-09-02 08:58:53 +00:00
|
|
|
}
|
|
|
|
|
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');
|
2008-10-09 03:02:26 +00:00
|
|
|
if (!empty($instanceoptionnames)){
|
|
|
|
$mform->addElement('checkbox', 'enablecourseinstances', get_string('enablecourseinstances', 'repository'));
|
|
|
|
$mform->addElement('checkbox', 'enableuserinstances', get_string('enableuserinstances', 'repository'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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] = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-05 04:40:51 +00:00
|
|
|
function repository_setup_default_plugins() {
|
|
|
|
//if the plugin type has no multiple instance (e.g. has no instance option name)
|
|
|
|
//repository_type::create will create an instance automatically
|
|
|
|
$local_plugin = new repository_type('local', array(), true);
|
|
|
|
$local_plugin_id = $local_plugin->create(true);
|
|
|
|
$upload_plugin = new repository_type('upload', array(), true);
|
|
|
|
$upload_plugin_id = $upload_plugin->create(true);
|
|
|
|
if (is_int($local_plugin_id) or is_int($upload_plugin_id)) {
|
|
|
|
print_box(get_string('setupdefaultplugins', 'repository'));
|
|
|
|
}
|
2009-03-10 02:01:24 +00:00
|
|
|
return true;
|
2009-03-05 04:40:51 +00:00
|
|
|
}
|
2009-04-20 08:53:21 +00:00
|
|
|
/**
|
|
|
|
* Return javascript to create file picker to browse repositories
|
|
|
|
* @global object $CFG
|
|
|
|
* @global object $USER
|
|
|
|
* @param object $context the context
|
|
|
|
* @param string $id unique id for every file picker
|
|
|
|
* @param string $accepted_filetypes
|
|
|
|
* @param string $returnvalue the return value of file picker
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function repository_get_client($context, $id = '', $accepted_filetypes = '*', $returnvalue = '*') {
|
|
|
|
global $CFG, $USER;
|
|
|
|
|
|
|
|
$ft = new file_type_to_ext();
|
|
|
|
$image_file_ext = json_encode($ft->get_file_ext(array('image')));
|
|
|
|
$video_file_ext = json_encode($ft->get_file_ext(array('video')));
|
|
|
|
$accepted_file_ext = json_encode($ft->get_file_ext($accepted_filetypes));
|
|
|
|
|
|
|
|
$css = '';
|
|
|
|
$js = '';
|
|
|
|
if (!isset($CFG->filepickerjsloaded)) {
|
|
|
|
$css .= <<<EOD
|
|
|
|
<style type="text/css">
|
|
|
|
@import "$CFG->httpswwwroot/lib/yui/resize/assets/skins/sam/resize.css";
|
|
|
|
@import "$CFG->httpswwwroot/lib/yui/container/assets/skins/sam/container.css";
|
|
|
|
@import "$CFG->httpswwwroot/lib/yui/layout/assets/skins/sam/layout.css";
|
|
|
|
@import "$CFG->httpswwwroot/lib/yui/button/assets/skins/sam/button.css";
|
|
|
|
@import "$CFG->httpswwwroot/lib/yui/assets/skins/sam/treeview.css";
|
|
|
|
</style>
|
|
|
|
<style type="text/css">
|
|
|
|
.file-picker{font-size:12px;}
|
|
|
|
.file-picker strong{background:#FFFFCC}
|
|
|
|
.file-picker a{color: #336699}
|
|
|
|
.file-picker a:hover{background:#003366;color:white}
|
|
|
|
.fp-panel{padding:0;margin:0; text-align:left;}
|
|
|
|
.fp-login-form{text-align:center}
|
|
|
|
.fp-searchbar{float:right}
|
|
|
|
.fp-viewbar{width:300px;float:left}
|
|
|
|
.fp-toolbar{padding: .8em;background: #FFFFCC;color:white;text-align:center}
|
|
|
|
.fp-toolbar a{padding: 0 .5em}
|
|
|
|
.fp-list{list-style-type:none;padding:0;float:left;width:100%;margin:0;}
|
|
|
|
.fp-list li{border-bottom:1px dotted gray;margin-bottom: 1em;}
|
|
|
|
.fp-repo-name{display:block;padding: .5em;margin-bottom: .5em}
|
|
|
|
.fp-pathbar{margin: .4em;border-bottom: 1px dotted gray;}
|
|
|
|
.fp-pathbar a{padding: .4em;}
|
|
|
|
.fp-rename-form{text-align:center}
|
|
|
|
.fp-rename-form p{margin: 1em;}
|
|
|
|
.fp-upload-form{margin: 2em 0;text-align:center}
|
|
|
|
.fp-upload-btn a{cursor: default;background: white;border:1px solid gray;color:black;padding: .5em}
|
|
|
|
.fp-upload-btn a:hover {background: grey;color:white}
|
|
|
|
.fp-paging{margin:1em .5em; clear:both;text-align:center;line-height: 2.5em;}
|
|
|
|
.fp-paging a{padding: .5em;border: 1px solid #CCC}
|
|
|
|
.fp-paging a.cur_page{border: 1px solid blue}
|
|
|
|
.fp-popup{text-align:center}
|
|
|
|
.fp-grid{float:left;text-align:center;}
|
|
|
|
.fp-grid div{overflow: hidden}
|
|
|
|
.fp-grid p{margin:0;padding:0;background: #FFFFCC}
|
|
|
|
.fp-grid .label{height:48px;text-align:center}
|
|
|
|
.fp-grid span{color:gray}
|
|
|
|
</style>
|
|
|
|
|
|
|
|
<!--[if IE 6]>
|
2009-04-21 10:02:59 +00:00
|
|
|
<style type="text/css">
|
|
|
|
/* Fix for IE6 */
|
|
|
|
.yui-skin-sam .yui-panel .hd{}
|
|
|
|
</style>
|
2009-04-20 08:53:21 +00:00
|
|
|
<![endif]-->
|
|
|
|
EOD;
|
|
|
|
|
|
|
|
require_js(array(
|
|
|
|
'yui_yahoo',
|
|
|
|
'yui_dom',
|
|
|
|
'yui_event',
|
|
|
|
'yui_element',
|
|
|
|
'yui_treeview',
|
|
|
|
'yui_dragdrop',
|
|
|
|
'yui_container',
|
|
|
|
'yui_resize',
|
|
|
|
'yui_layout',
|
|
|
|
'yui_connection',
|
|
|
|
'yui_json',
|
|
|
|
'yui_button',
|
|
|
|
'yui_selector',
|
|
|
|
'repository/repository.js'
|
|
|
|
));
|
|
|
|
$lang = array();
|
|
|
|
$lang['title'] = get_string('title', 'repository');
|
|
|
|
$lang['preview'] = get_string('preview', 'repository');
|
|
|
|
$lang['add'] = get_string('add', 'repository');
|
|
|
|
$lang['back'] = get_string('back', 'repository');
|
|
|
|
$lang['cancel'] = get_string('cancel');
|
|
|
|
$lang['close'] = get_string('close', 'repository');
|
|
|
|
$lang['ccache'] = get_string('cleancache', 'repository');
|
|
|
|
$lang['copying'] = get_string('copying', 'repository');
|
|
|
|
$lang['downbtn'] = get_string('getfile', 'repository');
|
|
|
|
$lang['download'] = get_string('downloadsucc', 'repository');
|
|
|
|
$lang['date'] = get_string('date', 'repository').': ';
|
|
|
|
$lang['error'] = get_string('error', 'repository');
|
|
|
|
$lang['filenotnull'] = get_string('filenotnull', 'repository');
|
|
|
|
$lang['federatedsearch'] = get_string('federatedsearch', 'repository');
|
|
|
|
$lang['help'] = get_string('help');
|
|
|
|
$lang['refresh'] = get_string('refresh', 'repository');
|
|
|
|
$lang['invalidjson'] = get_string('invalidjson', 'repository');
|
|
|
|
$lang['listview'] = get_string('listview', 'repository');
|
|
|
|
$lang['login'] = get_string('login', 'repository');
|
|
|
|
$lang['logout'] = get_string('logout', 'repository');
|
|
|
|
$lang['loading'] = get_string('loading', 'repository');
|
|
|
|
$lang['thumbview'] = get_string('thumbview', 'repository');
|
|
|
|
$lang['title'] = get_string('title', 'repository');
|
|
|
|
$lang['noresult'] = get_string('noresult', 'repository');
|
|
|
|
$lang['mgr'] = get_string('manageurl', 'repository');
|
|
|
|
$lang['noenter'] = get_string('noenter', 'repository');
|
|
|
|
$lang['save'] = get_string('save', 'repository');
|
|
|
|
$lang['saveas'] = get_string('saveas', 'repository').': ';
|
|
|
|
$lang['saved'] = get_string('saved', 'repository');
|
|
|
|
$lang['saving'] = get_string('saving', 'repository');
|
|
|
|
$lang['size'] = get_string('size', 'repository').': ';
|
|
|
|
$lang['sync'] = get_string('sync', 'repository');
|
|
|
|
$lang['search'] = get_string('search', 'repository');
|
|
|
|
$lang['searching'] = get_string('searching', 'repository');
|
|
|
|
$lang['submit'] = get_string('submit', 'repository');
|
|
|
|
$lang['preview'] = get_string('preview', 'repository');
|
|
|
|
$lang['popup'] = get_string('popup', 'repository');
|
|
|
|
$lang['upload'] = get_string('upload', 'repository').'...';
|
|
|
|
$lang['uploading'] = get_string('uploading', 'repository');
|
2009-04-21 10:02:59 +00:00
|
|
|
// fp_lang includes language strings
|
2009-04-20 08:53:21 +00:00
|
|
|
$js .= print_js_config($lang, 'fp_lang', true);
|
|
|
|
|
|
|
|
$options = array();
|
|
|
|
$context = get_system_context();
|
|
|
|
$options['contextid'] = $context->id;
|
2009-04-21 10:02:59 +00:00
|
|
|
// fp_config includes filepicker options
|
2009-04-20 08:53:21 +00:00
|
|
|
$js .= print_js_config($options, 'fp_config', true);
|
|
|
|
|
|
|
|
$accepted_file_ext = json_encode($ft->get_file_ext($accepted_filetypes));
|
|
|
|
$js .= <<<EOD
|
|
|
|
<script>
|
2009-04-21 10:02:59 +00:00
|
|
|
file_extensions.image = $image_file_ext;
|
|
|
|
file_extensions.media = $video_file_ext;
|
2009-04-20 08:53:21 +00:00
|
|
|
</script>
|
|
|
|
EOD;
|
|
|
|
|
|
|
|
$CFG->filepickerjsloaded = true;
|
|
|
|
} else {
|
|
|
|
// if yui and repository javascript libs are loaded
|
|
|
|
$js = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// print instances listing
|
|
|
|
$user_context = get_context_instance(CONTEXT_USER, $USER->id);
|
|
|
|
if (is_array($accepted_filetypes) && in_array('*', $accepted_filetypes)) {
|
|
|
|
$accepted_filetypes = '*';
|
|
|
|
}
|
|
|
|
$repos = repository::get_instances(array($user_context, $context, get_system_context()), null, true, null, $accepted_filetypes, $returnvalue);
|
2009-04-21 10:02:59 +00:00
|
|
|
|
|
|
|
// print repository instances listing
|
2009-04-20 08:53:21 +00:00
|
|
|
$js .= <<<EOD
|
|
|
|
<script>
|
2009-04-21 10:02:59 +00:00
|
|
|
repository_listing['$id'] = {};
|
2009-04-20 08:53:21 +00:00
|
|
|
EOD;
|
|
|
|
foreach ($repos as $repo) {
|
|
|
|
$info = $repo->ajax_info();
|
|
|
|
$js .= "\r\n";
|
|
|
|
$js .= 'repository_listing[\''.$id.'\'][\''.$info->id.'\']='.json_encode($repo->ajax_info()).';';
|
|
|
|
$js .= "\n";
|
|
|
|
}
|
|
|
|
$js .= "\r\n";
|
|
|
|
$js .= "</script>";
|
|
|
|
|
|
|
|
return array('css'=>$css, 'js'=>$js);
|
|
|
|
}
|