From 101b1d9ce1adb007577ad6ac976a3f2307131f38 Mon Sep 17 00:00:00 2001
From: Yang Yang <doraemonyoung@yahoo.com>
Date: Tue, 5 Jun 2012 13:42:30 +1200
Subject: [PATCH] MDL-31863 added modified to parameters and modifiedtime to
 description

---
 files/externallib.php |   83 +++++++++++++++++++++++++++++--------------------
 1 file changed, 50 insertions(+), 33 deletions(-)

diff --git a/files/externallib.php b/files/externallib.php
index ab7e2c3..dc80851 100644
--- a/files/externallib.php
+++ b/files/externallib.php
@@ -51,7 +51,9 @@ public static function get_files_parameters() {
                 'filearea'  => new external_value(PARAM_TEXT, 'file area'),
                 'itemid'    => new external_value(PARAM_INT, 'associated id'),
                 'filepath'  => new external_value(PARAM_PATH, 'file path'),
-                'filename'  => new external_value(PARAM_FILE, 'file name')
+                'filename'  => new external_value(PARAM_FILE, 'file name'),
+                'modified' => new external_value(PARAM_INT, 'timestamp, return files which the last ' .
+                    'timemodified time is the same or later than the specified time', VALUE_DEFAULT, null)
             )
         );
     }
@@ -65,12 +67,16 @@ public static function get_files_parameters() {
      * @param int $itemid item id
      * @param string $filepath file path
      * @param string $filename file name
+     * @param int $modified, timestamp, timestamp, 
+     *        return files which the last timemodified time is the same or later than the specified time
      * @return array
      * @since Moodle 2.2
      */
-    public static function get_files($contextid, $component, $filearea, $itemid, $filepath, $filename) {
+    public static function get_files($contextid, $component, $filearea, $itemid, $filepath, $filename, $modified = null) {
         global $CFG, $USER, $OUTPUT;
-        $fileinfo = self::validate_parameters(self::get_files_parameters(), array('contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid, 'filepath'=>$filepath, 'filename'=>$filename));
+        $fileinfo = self::validate_parameters(self::get_files_parameters(), array(
+                    'contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea,
+                    'itemid'=>$itemid, 'filepath'=>$filepath, 'filename'=>$filename, 'modified'=>$modified));
 
         $browser = get_file_browser();
 
@@ -98,7 +104,9 @@ public static function get_files($contextid, $component, $filearea, $itemid, $fi
         $return = array();
         $return['parents'] = array();
         $return['files'] = array();
-        if ($file = $browser->get_file_info($context, $fileinfo['component'], $fileinfo['filearea'], $fileinfo['itemid'], $fileinfo['filepath'], $fileinfo['filename'])) {
+        if ($file = $browser->get_file_info(
+            $context, $fileinfo['component'], $fileinfo['filearea'], $fileinfo['itemid'],
+                $fileinfo['filepath'], $fileinfo['filename'])) {
             $level = $file->get_parent();
             while ($level) {
                 $params = $level->get_params();
@@ -111,31 +119,38 @@ public static function get_files($contextid, $component, $filearea, $itemid, $fi
             foreach ($children as $child) {
 
                 $params = $child->get_params();
+                $timemodified = $child->get_timemodified();
 
                 if ($child->is_directory()) {
-                    $node = array(
-                        'contextid' => $params['contextid'],
-                        'component' => $params['component'],
-                        'filearea'  => $params['filearea'],
-                        'itemid'    => $params['itemid'],
-                        'filepath'  => $params['filepath'],
-                        'filename'  => $child->get_visible_name(),
-                        'url'       => null,
-                        'isdir'     => true
-                    );
-                    $list[] = $node;
+                    if ((is_null($modified)) or ($modified <= $timemodified)) {
+                        $node = array(
+                            'contextid' => $params['contextid'],
+                            'component' => $params['component'],
+                            'filearea'  => $params['filearea'],
+                            'itemid'    => $params['itemid'],
+                            'filepath'  => $params['filepath'],
+                            'filename'  => $child->get_visible_name(),
+                            'url'       => null,
+                            'isdir'     => true,
+                            'timemodified' => $timemodified
+                           );
+                           $list[] = $node;
+                    }
                 } else {
-                    $node = array(
-                        'contextid' => $params['contextid'],
-                        'component' => $params['component'],
-                        'filearea'  => $params['filearea'],
-                        'itemid'    => $params['itemid'],
-                        'filepath'  => $params['filepath'],
-                        'filename'  => $child->get_visible_name(),
-                        'url'       => $child->get_url(),
-                        'isdir'     => false
-                    );
-                    $list[] = $node;
+                    if ((is_null($modified)) or ($modified <= $timemodified)) {
+                        $node = array(
+                            'contextid' => $params['contextid'],
+                            'component' => $params['component'],
+                            'filearea'  => $params['filearea'],
+                            'itemid'    => $params['itemid'],
+                            'filepath'  => $params['filepath'],
+                            'filename'  => $child->get_visible_name(),
+                            'url'       => $child->get_url(),
+                            'isdir'     => false,
+                            'timemodified' => $timemodified
+                        );
+                           $list[] = $node;
+                    }
                 }
             }
         }
@@ -175,6 +190,7 @@ public static function get_files_returns() {
                             'filename' => new external_value(PARAM_FILE, ''),
                             'isdir'    => new external_value(PARAM_BOOL, ''),
                             'url'      => new external_value(PARAM_TEXT, ''),
+                            'timemodified' => new external_value(PARAM_INT, ''),
                         )
                     )
                 )
@@ -218,12 +234,14 @@ public static function upload_parameters() {
     public static function upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent) {
         global $USER, $CFG;
 
-        $fileinfo = self::validate_parameters(self::upload_parameters(), array('contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid, 'filepath'=>$filepath, 'filename'=>$filename, 'filecontent'=>$filecontent));
+        $fileinfo = self::validate_parameters(self::upload_parameters(), array(
+            'contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid,
+            'filepath'=>$filepath, 'filename'=>$filename, 'filecontent'=>$filecontent));
 
         if (!isset($fileinfo['filecontent'])) {
             throw new moodle_exception('nofile');
         }
-        // saving file
+        // Saving file.
         $dir = make_temp_directory('wsupload');
 
         if (empty($fileinfo['filename'])) {
@@ -238,7 +256,6 @@ public static function upload($contextid, $component, $filearea, $itemid, $filep
             $savedfilepath = $dir.$filename;
         }
 
-
         file_put_contents($savedfilepath, base64_decode($fileinfo['filecontent']));
         unset($fileinfo['filecontent']);
 
@@ -249,7 +266,7 @@ public static function upload($contextid, $component, $filearea, $itemid, $filep
         }
 
         if (isset($fileinfo['itemid'])) {
-            // TODO MDL-31116 in user private area, itemid is always 0
+            // TODO MDL-31116 in user private area, itemid is always 0.
             $itemid = 0;
         } else {
             throw new coding_exception('itemid cannot be empty');
@@ -264,19 +281,19 @@ public static function upload($contextid, $component, $filearea, $itemid, $filep
         if (!($fileinfo['component'] == 'user' and $fileinfo['filearea'] == 'private')) {
             throw new coding_exception('File can be uploaded to user private area only');
         } else {
-            // TODO MDL-31116 hard-coded to use user_private area
+            // TODO MDL-31116 hard-coded to use user_private area.
             $component = 'user';
             $filearea = 'private';
         }
 
         $browser = get_file_browser();
 
-        // check existing file
+        // Check existing file.
         if ($file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
             throw new moodle_exception('fileexist');
         }
 
-        // move file to filepool
+        // Move file to filepool.
         if ($dir = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, '.')) {
             $info = $dir->create_file_from_pathname($filename, $savedfilepath);
             $params = $info->get_params();
-- 
1.7.10

