Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 49 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| taoQtiTest_helpers_ResourceManager | |
0.00% |
0 / 49 |
|
0.00% |
0 / 3 |
156 | |
0.00% |
0 / 1 |
| getBaseDir | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| buildDirectory | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
56 | |||
| buildFile | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2014 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | /** |
| 24 | * This helper class aims at formating the item content folder description |
| 25 | * |
| 26 | */ |
| 27 | class taoQtiTest_helpers_ResourceManager |
| 28 | { |
| 29 | public static function getBaseDir(core_kernel_classes_Resource $test) |
| 30 | { |
| 31 | $testFile = taoQtiTest_models_classes_QtiTestService::singleton()->getTestFile($test); |
| 32 | if (is_null($testFile)) { |
| 33 | throw new common_Exception('No test folder found for ' . $test->getUri()); |
| 34 | ; |
| 35 | } |
| 36 | $baseDir = $testFile->getAbsolutePath() . '/'; |
| 37 | return $baseDir; |
| 38 | } |
| 39 | |
| 40 | public static function buildDirectory( |
| 41 | core_kernel_classes_Resource $test, |
| 42 | $lang, |
| 43 | $relPath = '/', |
| 44 | $depth = 1, |
| 45 | $filters = [] |
| 46 | ) { |
| 47 | $baseDir = self::getBaseDir($test); |
| 48 | $path = $baseDir . ltrim($relPath, '/'); |
| 49 | |
| 50 | $data = [ |
| 51 | 'path' => $relPath |
| 52 | ]; |
| 53 | if ($depth > 0) { |
| 54 | $children = []; |
| 55 | if (is_dir($path)) { |
| 56 | foreach (new DirectoryIterator($path) as $fileinfo) { |
| 57 | if (!$fileinfo->isDot()) { |
| 58 | $subPath = rtrim($relPath, '/') . '/' . $fileinfo->getFilename(); |
| 59 | if ($fileinfo->isDir()) { |
| 60 | $children[] = self::buildDirectory($test, $lang, $subPath, $depth - 1, $filters); |
| 61 | } else { |
| 62 | $file = self::buildFile($test, $lang, $subPath, $filters); |
| 63 | if (!is_null($file)) { |
| 64 | $children[] = $file; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | } else { |
| 70 | common_Logger::w('"' . $path . '" is not a directory'); |
| 71 | } |
| 72 | $data['children'] = $children; |
| 73 | } else { |
| 74 | $data['url'] = _url( |
| 75 | 'files', |
| 76 | 'TestContent', |
| 77 | 'taoQtiTest', |
| 78 | ['uri' => $test->getUri(),'lang' => $lang, 'path' => $relPath] |
| 79 | ); |
| 80 | } |
| 81 | return $data; |
| 82 | } |
| 83 | |
| 84 | public static function buildFile(core_kernel_classes_Resource $test, $lang, $relPath, $filters = []) |
| 85 | { |
| 86 | $file = null; |
| 87 | $baseDir = self::getBaseDir($test); |
| 88 | $path = $baseDir . ltrim($relPath, '/'); |
| 89 | $mime = tao_helpers_File::getMimeType($path); |
| 90 | |
| 91 | if (count($filters) == 0 || in_array($mime, $filters)) { |
| 92 | $file = [ |
| 93 | 'name' => basename($path), |
| 94 | 'mime' => $mime, |
| 95 | 'size' => filesize($path), |
| 96 | 'url' => _url( |
| 97 | 'download', |
| 98 | 'TestContent', |
| 99 | 'taoQtiTest', |
| 100 | ['uri' => $test->getUri(),'lang' => $lang, 'path' => $relPath] |
| 101 | ) |
| 102 | ]; |
| 103 | } |
| 104 | return $file; |
| 105 | } |
| 106 | } |