MDL-13766

1. Create "local" plugin
2. Improve document
This commit is contained in:
dongsheng
2008-08-08 03:31:51 +00:00
parent 9282e38f89
commit 837ebb781d
3 changed files with 81 additions and 190 deletions
@@ -0,0 +1,6 @@
<?php // $Id$
$string['repositoryname'] = 'Local Moodle';
$string['repositorydesc'] = 'Repository for local Moodle';
$string['notitle'] = 'notitle';
$string['remember'] = 'Remember me';
$string['emptyfilelist'] = 'There is no files.';
+18 -172
View File
@@ -1,202 +1,48 @@
<?php
/**
* repository_flickr class
* repository_local class
* This is a subclass of repository class
*
* @author Dongsheng Cai
* @version 0.1 dev
* @version $Id$
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
require_once($CFG->dirroot.'/repository/flickr/'.'phpFlickr.php');
class repository_flickr extends repository{
private $flickr;
public $photos;
public $type = 'flickr';
class repository_local extends repository{
public $type = 'local';
public function __construct($repositoryid, $context = SITEID, $options = array()){
global $SESSION, $action, $CFG;
$options['page'] = optional_param('p', 1, PARAM_INT);
$options['api_key'] = 'bf85ae2b5b105a2c645f32a32cd6ad59';
$options['secret'] = '7cb2f9d7cf70aebe';
parent::__construct($repositoryid, $context, $options);
$this->flickr = new phpFlickr($this->options['api_key'], $this->options['secret']);
$reset = optional_param('reset', 0, PARAM_INT);
if(!empty($reset)) {
// logout from flickr
unset($SESSION->flickrmail);
set_user_preference('flickrmail', '');
}
if(!empty($SESSION->flickrmail)) {
if(empty($action)) {
$action = 'list';
}
} else {
// get flickr account
$options['flickrmail'] = optional_param('flickrmail', '', PARAM_RAW);
if(!empty($options['flickrmail'])) {
$people = $this->flickr->people_findByEmail($options['flickrmail']);
if(!empty($people)) {
$remember = optional_param('remember', '', PARAM_RAW);
if(!empty($remember)) {
set_user_preference('flickrmail', $options['flickrmail']);
}
$SESSION->flickrmail = $options['flickrmail'];
if(empty($action)) {
$action = 'list';
}
} else {
throw new repository_exception('invalidemail', 'repository_flickr');
}
} else {
if($account = get_user_preferences('flickrmail', '')){
$SESSION->flickrmail = $account;
if(empty($action)) {
$action = 'list';
}
}
}
}
// get the parameter from client side
// $this->context can be used here.
}
public function print_login($ajax = true){
global $SESSION;
if(empty($SESSION->flickrmail)) {
$str =<<<EOD
<form id="moodle-repo-login">
<label for="account">Account (Email)</label><br/>
<input type='text' name='flickrmail' id='account' />
<input type='hidden' name='id' value='$this->repositoryid' /><br/>
<input type='checkbox' name='remember' id="keepid" value='true' /> <label for="keepid">Remember? </label>
<p><input type='button' onclick="repository_client.login()" value="Go" /></p>
</form>
EOD;
if($ajax){
$ret = array();
$e1->label = get_string('username', 'repository_flickr');
$e1->id = 'account';
$e1->type = 'text';
$e1->name = 'flickrmail';
$e2->id = 'keepid';
$e2->label = get_string('remember', 'repository_flickr');
$e2->type = 'checkbox';
$e2->name = 'remember';
$e3->type = 'hidden';
$e3->name = 'repo_id';
$e3->value = $this->repositoryid;
$ret['l'] = array($e1, $e2, $e3);
return $ret;
}else{
echo $str;
}
} else {
return $this->get_listing();
}
// TODO
// Return file list in moodle
// Also, this plugin should have ability to
// upload files in user's computer, a iframe
// need to be created.
return $this->get_listing();
}
public function get_listing($path = '1', $search = ''){
public function get_listing($path = '/', $search = ''){
global $SESSION;
$people = $this->flickr->people_findByEmail($SESSION->flickrmail);
$photos_url = $this->flickr->urls_getUserPhotos($people['nsid']);
if(!empty($search)) {
// do searching, if $path is not empty, ignore it.
$photos = $this->flickr->photos_search(array('user_id'=>$people['nsid'], 'text'=>$search));
} elseif(!empty($path) && empty($search)) {
$photos = $this->flickr->people_getPublicPhotos($people['nsid'], null, 36, $path);
}
$ret = new stdclass;
$ret->url = $photos_url;
$ret->upload = array('name'=>'attachment', 'id'=>'', 'url'=>'');
$ret->list = array();
$ret->pages = $photos['pages'];
if(is_int($path) && $path <= $ret->pages) {
$ret->page = $path;
} else {
$ret->page = 1;
}
foreach ($photos['photo'] as $p) {
if(empty($p['title'])) {
$p['title'] = get_string('notitle', 'repository_flickr');
}
$ret->list[] =
array('title'=>$p['title'],'source'=>$p['id'],'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'), 'date'=>'', 'size'=>'unknown');
}
// call file api get the list of the file
$ret->list[] = array('title'=>'title','source'=>'download url', 'thumbnail'=>'url of thumbnail', 'date'=>'', 'size'=>'unknown');
if(empty($ret)) {
throw new repository_exception('nullphotolist', 'repository_flickr');
throw new repository_exception('emptyfilelist', 'repository_local');
} else {
return $ret;
}
}
public function print_listing(){
if(empty($this->photos)){
$this->get_listing();
}
$str = '';
$str .= '<h2>Account: <span>'.$this->photos['a'].'</span></h2>';
foreach ((array)$this->photos['photo'] as $photo) {
$str .= "<a href='".$this->photos['url'].$photo[id]."'>";
$str .= "<img border='0' alt='$photo[title]' ".
"src=" . $photo['thumbnail'] . ">";
$str .= "</a>";
$i++;
if ($i % 4 == 0) {
$str .= "<br/>";
}
}
$str .= <<<EOD
<style type='text/css'>
#paging{margin-top: 10px; clear:both}
#paging a{padding: 4px; border: 1px solid gray}
</style>
EOD;
$str .= '<div id="paging">';
for($i=1; $i <= $this->photos['pages']; $i++) {
$str .= '<a href="###" onclick="cr('.$this->repositoryid.', '.$i.', 0)">';
$str .= $i;
$str .= '</a> ';
}
$str .= '</div>';
echo $str;
// will be used in non-javascript file picker
}
public function print_search(){
echo '<input type="text" name="Search" value="search terms..." size="40" class="right"/>';
return true;
}
public function get_file($photo_id, $file = ''){
global $CFG;
$result = $this->flickr->photos_getSizes($photo_id);
$url = '';
if(!empty($result[4])) {
$url = $result[4]['source'];
} elseif(!empty($result[3])) {
$url = $result[3]['source'];
} elseif(!empty($result[2])) {
$url = $result[2]['source'];
}
if (!file_exists($CFG->dataroot.'/repository/download')) {
mkdir($CFG->dataroot.'/repository/download/', 0777, true);
}
if(is_dir($CFG->dataroot.'/repository/download')) {
$dir = $CFG->dataroot.'/repository/download/';
}
if(empty($file)) {
$file = $photo_id.'_'.time().'.jpg';
}
if(file_exists($dir.$file)) {
$file = uniqid('m').$file;
}
$fp = fopen($dir.$file, 'w');
$c = new curl;
$c->download(array(
array('url'=>$url, 'file'=>$fp)
));
return $dir.$file;
}
}
?>
+57 -18
View File
@@ -141,9 +141,9 @@ abstract class repository {
*
* @param string $list
* $list = array(
* array('name'=>'moodle.txt', 'size'=>12, 'path'=>'', 'date'=>''),
* array('name'=>'repository.txt', 'size'=>32, 'path'=>'', 'date'=>''),
* array('name'=>'forum.txt', 'size'=>82, 'path'=>'', 'date'=>''),
* array('title'=>'moodle.txt', 'size'=>12, 'source'=>'url', 'date'=>''),
* array('title'=>'repository.txt', 'size'=>32, 'source'=>'', 'date'=>''),
* array('title'=>'forum.txt', 'size'=>82, 'source'=>'', 'date'=>''),
* );
*
* @param boolean $print if printing the listing directly
@@ -180,18 +180,6 @@ abstract class repository {
/**
* Show the login screen, if required
* This is an abstract function, it must be overriden.
* The specific plug-in need to specify authentication types in database
* options field
* Imagine following cases:
* 1. no need of authentication
* 2. Use username and password to authenticate
* 3. Redirect to authentication page, in this case, the repository
* will callback moodle with following common parameters:
* (1) boolean callback To tell moodle this is a callback
* (2) int id Specify repository ID
* The callback page need to use these parameters to init
* the repository plug-ins correctly. Also, auth_token or ticket may
* attach in the callback url, these must be taken into account too.
*
*/
abstract public function print_login();
@@ -209,7 +197,7 @@ abstract class repository {
* @param string $username
* @param string $password
* @param string $userid The id of specific user
* @return array the list of files, including meta infomation
* @return int Id of the record
*/
public function store_login($username = '', $password = '', $userid = 1) {
global $DB;
@@ -259,6 +247,14 @@ class repository_exception extends moodle_exception {
abstract class repository_listing {
}
/**
* Save settings for repository instance
*
* @param int repository Id
* @param int from 1 to 5
* @param array settings
* @return int Id of the record
*/
function repository_set_option($id, $position, $config = array()){
global $DB;
$repository = new stdclass;
@@ -275,6 +271,14 @@ function repository_set_option($id, $position, $config = array()){
}
return false;
}
/**
* Get settings for repository instance
*
* @param int repository Id
* @param int from 1 to 5
* @return array Settings
*/
function repository_get_option($id, $position){
global $DB;
$entry = $DB->get_record('repository', array('id'=>$id));
@@ -283,6 +287,12 @@ function repository_get_option($id, $position){
return $ret;
}
/**
* Get user's repositories
*
* @param object context object
* @return array repository list
*/
function repository_user_instances($context){
global $DB, $CFG, $USER;
$params = array();
@@ -303,6 +313,12 @@ function repository_user_instances($context){
return $repos;
}
/**
* Get single repository instance
*
* @param int repository id
* @return object repository instance
*/
function repository_instance($id){
global $DB, $CFG;
@@ -314,6 +330,12 @@ function repository_instance($id){
$classname = 'repository_' . $instance->repositorytype;
return new $classname($instance->id, $instance->contextid);
}
/**
* Get list of repository plugin
*
* @return array repository plugin list
*/
function repository_get_plugins(){
global $CFG;
$repo = $CFG->dirroot.'/repository/';
@@ -333,6 +355,14 @@ function repository_get_plugins(){
return $ret;
}
/**
* Move file from download folder to file pool using FILE API
*
* @param string file path in download folder
* @param string file name
* @param int itemid to identify a file in filepool
* @return array information of file in file pool
*/
function move_to_filepool($path, $name, $itemid) {
global $DB, $CFG, $USER;
$context = get_context_instance(CONTEXT_USER, $USER->id);
@@ -361,8 +391,12 @@ function move_to_filepool($path, $name, $itemid) {
}
}
// TODO
// Need to pass contextid and contextlevel here
/**
* Return javascript to create file picker to browse repositories
*
* @param object context
* @return array
*/
function get_repository_client($context){
global $CFG, $USER;
$suffix = uniqid();
@@ -603,6 +637,11 @@ function get_repository_client($context){
}
}
// TODO
// If _client.datasource.upload == true
// then create a iframe to upload file
// We may need a new page named repository/upload.php to process this.
_client.viewthumb = function(){
var panel = new YAHOO.util.Element('panel-$suffix');
_client.viewbar.check(1);