From 6bf197b34ce7b289ac83367cf01cfe1069a982e1 Mon Sep 17 00:00:00 2001 From: Dongsheng Cai Date: Mon, 31 May 2010 03:03:21 +0000 Subject: [PATCH] MDL-22591, user_private plugin, disable it from private file block, hide user_private area from local plugin --- blocks/private_files/block_private_files.php | 1 + lib/outputcomponents.php | 1 + repository/lib.php | 27 ++- repository/local/repository.class.php | 8 +- repository/repository_ajax.php | 2 +- repository/user/db/access.php | 31 +++ repository/user/db/install.php | 27 +++ repository/user/db/upgrade.php | 35 ++++ repository/user/icon.png | Bin 0 -> 582 bytes repository/user/lang/en/repository_user.php | 31 +++ repository/user/repository.class.php | 196 +++++++++++++++++++ repository/user/version.php | 18 ++ 12 files changed, 368 insertions(+), 9 deletions(-) create mode 100644 repository/user/db/access.php create mode 100755 repository/user/db/install.php create mode 100755 repository/user/db/upgrade.php create mode 100755 repository/user/icon.png create mode 100755 repository/user/lang/en/repository_user.php create mode 100755 repository/user/repository.class.php create mode 100755 repository/user/version.php diff --git a/blocks/private_files/block_private_files.php b/blocks/private_files/block_private_files.php index 51205fd80b1..bbd6b586061 100755 --- a/blocks/private_files/block_private_files.php +++ b/blocks/private_files/block_private_files.php @@ -66,6 +66,7 @@ class block_private_files extends block_base { $options->accepted_types = '*'; $options->return_types = FILE_INTERNAL; $options->context = $PAGE->context; + $options->disable_types = array('user'); $this->content = new stdClass; $this->content->text = $OUTPUT->file_manager($options); diff --git a/lib/outputcomponents.php b/lib/outputcomponents.php index bdcdfe59678..a5ecf656050 100644 --- a/lib/outputcomponents.php +++ b/lib/outputcomponents.php @@ -191,6 +191,7 @@ class file_manager implements renderable { $params->return_types = $options->return_types; $params->context = $options->context; $params->env = 'filemanager'; + $params->disable_types = !empty($options->disable_types)?$options->disable_types:array(); $filepicker_options = initialise_filepicker($params); $this->options->filepicker = $filepicker_options; } diff --git a/repository/lib.php b/repository/lib.php index 371c8519056..2fae02f9ae3 100644 --- a/repository/lib.php +++ b/repository/lib.php @@ -637,7 +637,9 @@ abstract class repository { $sql .= ' AND (i.userid = 0 or i.userid = ?)'; $params[] = $args['userid']; } - + if (!empty($args['disable_types']) && is_array($args['disable_types'])) { + $sql .= ' AND r.type NOT IN ("'.implode('","', $args['disable_types']).'")'; + } foreach ($contexts as $context) { if (empty($firstcontext)) { $firstcontext = true; @@ -1815,6 +1817,10 @@ function initialise_filepicker($args) { } else { $context = $args->context; } + $disable_types = array(); + if (!empty($args->disable_types)) { + $disable_types = $args->disable_types; + } $user_context = get_context_instance(CONTEXT_USER, $USER->id); @@ -1823,7 +1829,8 @@ function initialise_filepicker($args) { 'context'=>array($user_context, get_system_context()), 'currentcontext'=> $context, 'accepted_types'=>$args->accepted_types, - 'return_types'=>$args->return_types + 'return_types'=>$args->return_types, + 'disable_types'=>$disable_types )); $return->repositories = array(); @@ -1854,12 +1861,18 @@ function repository_setup_default_plugins() { global $OUTPUT; //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); + $user_plugin = new repository_type('user', array(), true); + $upload_plugin->create(true); + $recent_plugin = new repository_type('recent', array(), true); - $recent_plugin_id = $upload_plugin->create(true); + $upload_plugin->create(true); + + $upload_plugin = new repository_type('upload', array(), true); + $upload_plugin->create(true); + + $local_plugin = new repository_type('local', array(), true); + $local_plugin->create(true); + echo $OUTPUT->notification(get_string('setupdefaultplugins', 'repository'), 'notifysuccess'); return true; } diff --git a/repository/local/repository.class.php b/repository/local/repository.class.php index 64623f12fad..49602714482 100755 --- a/repository/local/repository.class.php +++ b/repository/local/repository.class.php @@ -108,7 +108,13 @@ class repository_local extends repository { $children = $fileinfo->get_children(); foreach ($children as $child) { if ($child->is_directory()) { - $encodedpath = base64_encode(serialize($child->get_params())); + $params = $child->get_params(); + $encodedpath = base64_encode(serialize($params)); + // hide user_private area from local plugin, user should + // use private file plugin to access private files + if ($params['filearea'] == 'user_private') { + continue; + } $node = array( 'title' => $child->get_visible_name(), 'size' => 0, diff --git a/repository/repository_ajax.php b/repository/repository_ajax.php index e79aef31a63..0226577784b 100755 --- a/repository/repository_ajax.php +++ b/repository/repository_ajax.php @@ -228,7 +228,7 @@ switch ($action) { // We have two special repoisitory type need to deal with // local and recent plugins don't added new files to moodle, just add new records to database // so we don't check user quota and maxbytes here - if ($repo->options['type'] == 'local' || $repo->options['type'] == 'recent' ) { + if (in_array($repo->options['type'], array('local', 'recent', 'user'))) { try { $fileinfo = $repo->copy_to_area($source, $saveas_filearea, $itemid, $saveas_path, $saveas_filename); } catch (Exception $e) { diff --git a/repository/user/db/access.php b/repository/user/db/access.php new file mode 100644 index 00000000000..f68640a8cf7 --- /dev/null +++ b/repository/user/db/access.php @@ -0,0 +1,31 @@ +. + +$capabilities = array( + + 'repository/user:view' => array( + 'captype' => 'read', + 'contextlevel' => CONTEXT_SYSTEM, + 'archetypes' => array( + 'user' => CAP_ALLOW, + 'student' => CAP_ALLOW, + 'teacher' => CAP_ALLOW, + 'editingteacher' => CAP_ALLOW, + 'manager' => CAP_ALLOW + ) + ) +); diff --git a/repository/user/db/install.php b/repository/user/db/install.php new file mode 100755 index 00000000000..8ca122877f5 --- /dev/null +++ b/repository/user/db/install.php @@ -0,0 +1,27 @@ +. + +function xmldb_repository_user_install() { + global $CFG; + $result = true; + require_once($CFG->dirroot.'/repository/lib.php'); + $user_plugin = new repository_type('user', array(), true); + if(!$id = $user_plugin->create(true)) { + $result = false; + } + return $result; +} diff --git a/repository/user/db/upgrade.php b/repository/user/db/upgrade.php new file mode 100755 index 00000000000..744fa06a87b --- /dev/null +++ b/repository/user/db/upgrade.php @@ -0,0 +1,35 @@ +. + +function xmldb_repository_user_upgrade($oldversion) { + + global $CFG, $DB; + + $dbman = $DB->get_manager(); + $result = true; + +/// And upgrade begins here. For each one, you'll need one +/// block of code similar to the next one. Please, delete +/// this comment lines once this file start handling proper +/// upgrade code. + +/// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php +/// $result = result of database_manager methods +/// } + + return $result; +} diff --git a/repository/user/icon.png b/repository/user/icon.png new file mode 100755 index 0000000000000000000000000000000000000000..5131cf3de1da9a246a7ea410df97a56454f75901 GIT binary patch literal 582 zcmV-M0=fN(P)sc!?Oa|W?(3ZQitt8@U9a!$t!d={vG7L$Qdv3nt_fEurUGPi#$jEje}jwZZ~A-j((jg^X( zl8wZYFT;>M>zM`7ol4uBKkA+h=AI_*pa|xkFQ==ts;sB`qXGV)3jd!L-lt0cqYP)Ps~P{Q8U3pp{Hz)Ktr`EVA^xr<`m!SbvKarcBf7o9 z|F}c{zD51NNdLZ0_rg&6!b|VJfB(Qz{=!oA#bN%!VEx2h{l{DX$5!ITuKvhg|JjBA z=)jY8&gcLD00DGTPE!Ct=GbNc0004EOGiWihy@);0001~Nkl$KDDbS|FmcAAr?t&acy0W$yqz~S6bU0DR+w!S1?U9<-vDvl~^OABWG{l0ud*r=$g zDNKzl-<@qQ&L9};Y$$KuI$oWcUR#+Ucsgol{j!za^EQI7ok^Zgd-=HOCivVR. + +/** + * Strings for component 'repository_user', language 'en', branch 'MOODLE_20_STABLE' + * + * @package repository_user + * @copyright 2010 Dongsheng Cai + * @author Dongsheng Cai + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +$string['configplugin'] = 'Configuration for user private files repository'; +$string['repositorydesc'] = 'Files in user private area'; +$string['repositoryname'] = 'Private files'; +$string['emptyfilelist'] = 'There are no files to show'; +$string['user:view'] = 'View user private files'; diff --git a/repository/user/repository.class.php b/repository/user/repository.class.php new file mode 100755 index 00000000000..fa63be4bf39 --- /dev/null +++ b/repository/user/repository.class.php @@ -0,0 +1,196 @@ +. + +/** + * repository_user class is used to browse user private files + * + * @since 2.0 + * @package moodlecore + * @subpackage repository + * @copyright 2010 Dongsheng Cai + * @author Dongsheng Cai + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +class repository_user extends repository { + + /** + * initialize user plugin + * @param int $repositoryid + * @param int $context + * @param array $options + */ + public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) { + parent::__construct($repositoryid, $context, $options); + } + + /** + * user plugin doesn't require login + * @return mixed + */ + public function print_login() { + return $this->get_listing(); + } + + /** + * Not supported by File API yet + * @param string $search_text + * @return mixed + */ + public function search($search_text) { + return array(); + } + + /** + * Get file listing + * + * @param string $encodedpath + * @return mixed + */ + public function get_listing($encodedpath = '') { + global $CFG, $USER, $OUTPUT; + $ret = array(); + $ret['dynload'] = true; + $ret['nosearch'] = true; + $ret['nologin'] = true; + $list = array(); + + if (!empty($encodedpath)) { + $params = unserialize(base64_decode($encodedpath)); + if (is_array($params)) { + $itemid = $params['itemid']; + $filename = $params['filename']; + $filearea = $params['filearea']; + $filepath = $params['filepath']; + $context = get_context_instance_by_id($params['contextid']); + } + } else { + $itemid = 0; + $filename = null; + $filearea = 'user_private'; + $filepath = '/'; + $context = get_context_instance(CONTEXT_USER, $USER->id); + } + + try { + $browser = get_file_browser(); + + if ($fileinfo = $browser->get_file_info($context, $filearea, $itemid, $filepath, $filename)) { + $pathnodes = array(); + $level = $fileinfo; + $params = $fileinfo->get_params(); + while ($level && $params['filearea'] == 'user_private') { + $encodedpath = base64_encode(serialize($level->get_params())); + $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath); + $level = $level->get_parent(); + $params = $level->get_params(); + } + $ret['path'] = array_reverse($pathnodes); + + // build file tree + $children = $fileinfo->get_children(); + foreach ($children as $child) { + if ($child->is_directory()) { + $encodedpath = base64_encode(serialize($child->get_params())); + $node = array( + 'title' => $child->get_visible_name(), + 'size' => 0, + 'date' => '', + 'path' => $encodedpath, + 'children'=>array(), + 'thumbnail' => $OUTPUT->pix_url('f/folder-32') . '' + ); + $list[] = $node; + } else { + $encodedpath = base64_encode(serialize($child->get_params())); + $icon = 'f/'.str_replace('.gif', '', mimeinfo('icon', $child->get_visible_name())).'-32'; + $node = array( + 'title' => $child->get_visible_name(), + 'size' => 0, + 'date' => '', + 'source'=> $encodedpath, + 'thumbnail' => $OUTPUT->pix_url($icon) . '', + ); + $list[] = $node; + } + } + } + } catch (Exception $e) { + throw new repository_exception('emptyfilelist', 'repository_user'); + } + $ret['list'] = $list; + return $ret; + } + + /** + * Set repository name + * + * @return string repository name + */ + public function get_name(){ + return get_string('areauserpersonal', 'repository');; + } + + /** + * User file don't support to link to external links + * + * @return int + */ + public function supported_returntypes() { + return FILE_INTERNAL; + } + + /** + * Copy a file to file area + * + * @global object $USER + * @global object $DB + * @param string $encoded The metainfo of file, it is base64 encoded php seriablized data + * @param string $new_filename The intended name of file + * @param string $new_itemid itemid + * @param string $new_filepath the new path in draft area + * @return array The information of file + */ + public function copy_to_area($encoded, $new_filearea='user_draft', $new_itemid = '', $new_filepath = '/', $new_filename = '') { + global $USER, $DB; + $info = array(); + + $browser = get_file_browser(); + $params = unserialize(base64_decode($encoded)); + $user_context = get_context_instance(CONTEXT_USER, $USER->id); + // the final file + $contextid = $params['contextid']; + $filearea = $params['filearea']; + $filepath = $params['filepath']; + $filename = $params['filename']; + $fileitemid = $params['itemid']; + $context = get_context_instance_by_id($contextid); + try { + $file_info = $browser->get_file_info($context, $filearea, $fileitemid, $filepath, $filename); + $file_info->copy_to_storage($user_context->id, $new_filearea, $new_itemid, $new_filepath, $new_filename); + } catch (Exception $e) { + throw $e; + } + + $info['itemid'] = $new_itemid; + $info['title'] = $new_filename; + $info['contextid'] = $user_context->id; + $info['filesize'] = $file_info->get_filesize(); + + return $info; + } +} diff --git a/repository/user/version.php b/repository/user/version.php new file mode 100755 index 00000000000..54fa40b5ee4 --- /dev/null +++ b/repository/user/version.php @@ -0,0 +1,18 @@ +. + +$plugin->version = 2010052700;