From d3959c2695dc0890de192e718c4a5280e0dd1ae0 Mon Sep 17 00:00:00 2001
From: Dongsheng Cai <unoter@gmail.com>
Date: Tue, 31 Aug 2010 03:39:33 +0000
Subject: [PATCH] MDL-23308, coursefiles repository plugin added

---
 repository/coursefiles/db/access.php          |  12 ++
 .../lang/en/repository_coursefiles.php        |  31 +++
 repository/coursefiles/lib.php                | 181 ++++++++++++++++++
 repository/coursefiles/pix/icon.png           | Bin 0 -> 582 bytes
 repository/coursefiles/version.php            |  18 ++
 repository/lib.php                            |   5 +
 repository/repository_ajax.php                |   2 +-
 7 files changed, 248 insertions(+), 1 deletion(-)
 create mode 100755 repository/coursefiles/db/access.php
 create mode 100755 repository/coursefiles/lang/en/repository_coursefiles.php
 create mode 100755 repository/coursefiles/lib.php
 create mode 100755 repository/coursefiles/pix/icon.png
 create mode 100755 repository/coursefiles/version.php

diff --git a/repository/coursefiles/db/access.php b/repository/coursefiles/db/access.php
new file mode 100755
index 00000000000..3c670c91272
--- /dev/null
+++ b/repository/coursefiles/db/access.php
@@ -0,0 +1,12 @@
+<?php
+
+$capabilities = array(
+
+    'repository/coursefiles:view' => array(
+        'captype' => 'read',
+        'contextlevel' => CONTEXT_MODULE,
+        'archetypes' => array(
+            'user' => CAP_ALLOW
+        )
+    )
+);
diff --git a/repository/coursefiles/lang/en/repository_coursefiles.php b/repository/coursefiles/lang/en/repository_coursefiles.php
new file mode 100755
index 00000000000..41004a1bb1b
--- /dev/null
+++ b/repository/coursefiles/lang/en/repository_coursefiles.php
@@ -0,0 +1,31 @@
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Strings for component 'repository_coursefiles', language 'en', branch 'MOODLE_20_STABLE'
+ *
+ * @package   repository_coursefiles
+ * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+$string['configplugin'] = 'Configuration for course files repository';
+$string['emptyfilelist'] = 'There are no files to show';
+$string['notitle'] = 'notitle';
+$string['remember'] = 'Remember me';
+$string['pluginname_help'] = 'Course files';
+$string['pluginname'] = 'Course files';
diff --git a/repository/coursefiles/lib.php b/repository/coursefiles/lib.php
new file mode 100755
index 00000000000..fb12ddd2594
--- /dev/null
+++ b/repository/coursefiles/lib.php
@@ -0,0 +1,181 @@
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * repository_coursefiles class is used to browse course files
+ *
+ * @since 2.0
+ * @package moodlecore
+ * @subpackage repository
+ * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+class repository_coursefiles extends repository {
+
+    /**
+     * coursefiles plugin doesn't require login, so list all files
+     * @return mixed
+     */
+    public function print_login() {
+        return $this->get_listing();
+    }
+
+    /**
+     * 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();
+        $component = 'course';
+        $filearea  = 'legacy';
+        $itemid = 0;
+
+        $browser = get_file_browser();
+
+        if (!empty($encodedpath)) {
+            $params = unserialize(base64_decode($encodedpath));
+            if (is_array($params)) {
+                $filepath  = is_null($params['filepath']) ? NULL : clean_param($params['filepath'], PARAM_PATH);;
+                $filename  = is_null($params['filename']) ? NULL : clean_param($params['filename'], PARAM_FILE);
+                $context = get_context_instance_by_id(clean_param($params['contextid'], PARAM_INT));
+            }
+        } else {
+            $filename = null;
+            $filepath = null;
+            list($context, $course, $cm) = get_context_info_array($this->context->id);
+            $courseid = is_object($course) ? $course->id : SITEID;
+            $context = get_context_instance(CONTEXT_COURSE, $courseid);
+        }
+
+        if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
+            // build path navigation
+            $pathnodes = array();
+            $encodedpath = base64_encode(serialize($fileinfo->get_params()));
+            $pathnodes[] = array('name'=>$fileinfo->get_visible_name(), 'path'=>$encodedpath);
+            $level = $fileinfo->get_parent();
+            while ($level) {
+                $params = $level->get_params();
+                $encodedpath = base64_encode(serialize($params));
+                if ($params['contextid'] != $context->id) {
+                    break;
+                }
+                $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath);
+                $level = $level->get_parent();
+            }
+            if (!empty($pathnodes) && is_array($pathnodes)) {
+                $pathnodes = array_reverse($pathnodes);
+                $ret['path'] = $pathnodes;
+            }
+            // build file tree
+            $children = $fileinfo->get_children();
+            foreach ($children as $child) {
+                $shorttitle = $this->get_short_filename($child->get_visible_name(), 12);
+                if ($child->is_directory()) {
+                    $params = $child->get_params();
+                    $subdir_children = $child->get_children();
+                    $encodedpath = base64_encode(serialize($params));
+                    $node = array(
+                        'title' => $child->get_visible_name(),
+                        'shorttitle'=>$shorttitle,
+                        'size' => 0,
+                        'date' => '',
+                        'path' => $encodedpath,
+                        'children'=>array(),
+                        'thumbnail' => $OUTPUT->pix_url('f/folder-32')->out(false)
+                    );
+                    $list[] = $node;
+                } else {
+                    $encodedpath = base64_encode(serialize($child->get_params()));
+                    $node = array(
+                        'title' => $child->get_visible_name(),
+                        'shorttitle'=>$shorttitle,
+                        'size' => 0,
+                        'date' => '',
+                        'source'=> $encodedpath,
+                        'thumbnail' => $OUTPUT->pix_url(file_extension_icon($child->get_visible_name(), 32))->out(false)
+                    );
+                    $list[] = $node;
+                }
+            }
+        } else {
+            $list = array();
+        }
+        $ret['list'] = array_filter($list, array($this, 'filter'));
+        return $ret;
+    }
+
+    /**
+     * course files 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 serialized data
+     * @param string $draftitemid itemid
+     * @param string $new_filename The intended name of file
+     * @param string $new_filepath the new path in draft area
+     * @return array The information of file
+     */
+    public function copy_to_area($encoded, $draftitemid, $new_filepath, $new_filename) {
+        global $USER, $DB;
+        $info = array();
+
+        $browser = get_file_browser();
+        $fs = get_file_storage();
+        $user_context = get_context_instance(CONTEXT_USER, $USER->id);
+
+        // the final file
+        $params = unserialize(base64_decode($encoded));
+        $contextid  = clean_param($params['contextid'], PARAM_INT);
+        $fileitemid = clean_param($params['itemid'], PARAM_INT);
+        $filename = clean_param($params['filename'], PARAM_FILE);
+        $filepath = clean_param($params['filepath'], PARAM_PATH);;
+        $filearea = clean_param($params['filearea'], PARAM_ALPHAEXT);
+        $component = clean_param($params['component'], PARAM_ALPHAEXT);
+        $context = get_context_instance_by_id($contextid);
+
+        if ($existingfile = $fs->get_file($user_context->id, 'user', 'draft', $draftitemid, $new_filepath, $new_filename)) {
+            throw new moodle_exception('fileexists');
+        }
+
+        $file_info = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename);
+        $file_info->copy_to_storage($user_context->id, 'user', 'draft', $draftitemid, $new_filepath, $new_filename);
+
+        $info['itemid'] = $draftitemid;
+        $info['title']  = $new_filename;
+        $info['contextid'] = $user_context->id;
+        $info['filesize'] = $file_info->get_filesize();
+
+        return $info;
+    }
+}
diff --git a/repository/coursefiles/pix/icon.png b/repository/coursefiles/pix/icon.png
new file mode 100755
index 0000000000000000000000000000000000000000..5131cf3de1da9a246a7ea410df97a56454f75901
GIT binary patch
literal 582
zcmV-M0=fN(P)<h;3K|Lk000e1NJLTq000mG000mO0{{R3C@l|D0003#P)t-s02u%t
z9v&kmFDWQCFEukVH!(IhJW4erJU~B4LPSSLM?_3PN=QpdO-@frPDM~wQ%zA)Qc_f3
zSz2RaX<t^6WdxRJ5{7j>sc!?Oa|W?(3ZQitt8@U9a!<K$0G)Rtws8QlbpWw*45)Vy
zxp4xkb`Gm~3VnZay>$t!d={vG7L$Qdv3nt_fEurUGPi#$jEje}jwZZ~A-j((jg^X(
zl8wZYFT;>M>zM`7ol4uBKkA+h=AI_*pa|xkFQ==ts;sB`qXGV)3jd!L-lt0cqY<mI
zum7eI`luALvatNB8vm#t`>P)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<Z7#Rh@g(3t|006)d
z>$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<?*7A
zx9b)*y&jD7a5BLARR<*5m?RgI<YP#(G55Jae)2uA(3N4nzlo*cMgRZ+0002|1N&+j
U6CTXz*#H0l07*qoM6N<$f{XeCKmY&$

literal 0
HcmV?d00001

diff --git a/repository/coursefiles/version.php b/repository/coursefiles/version.php
new file mode 100755
index 00000000000..fa638d3eda0
--- /dev/null
+++ b/repository/coursefiles/version.php
@@ -0,0 +1,18 @@
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+$plugin->version = 2010083100;
diff --git a/repository/lib.php b/repository/lib.php
index eb2109653f7..ed5e3dc367a 100644
--- a/repository/lib.php
+++ b/repository/lib.php
@@ -739,6 +739,7 @@ abstract class repository {
                         $is_supported = false;
                     }
                 }
+
                 if (!$onlyvisible || ($repository->is_visible() && !$repository->disabled)) {
                     // check capability in current context
                     if (!empty($current_context)) {
@@ -746,6 +747,10 @@ abstract class repository {
                     } else {
                         $capability = has_capability('repository/'.$record->repositorytype.':view', get_system_context());
                     }
+                    if ($record->repositorytype == 'coursefiles') {
+                        // coursefiles plugin needs managefiles permission
+                        $capability = $capability && has_capability('moodle/course:managefiles', $current_context);
+                    }
                     if ($is_supported && $capability) {
                         $repositories[$repository->id] = $repository;
                     }
diff --git a/repository/repository_ajax.php b/repository/repository_ajax.php
index 22397feff7f..7d96041f152 100755
--- a/repository/repository_ajax.php
+++ b/repository/repository_ajax.php
@@ -174,7 +174,7 @@ switch ($action) {
         // We have two special repository 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 (in_array($repo->options['type'], array('local', 'recent', 'user'))) {
+        if (in_array($repo->options['type'], array('local', 'recent', 'user', 'coursefiles'))) {
             $fileinfo = $repo->copy_to_area($source, $itemid, $saveas_path, $saveas_filename);
             $info = array();
             $info['file'] = $fileinfo['title'];