Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 37 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ItemUpdater | |
0.00% |
0 / 37 |
|
0.00% |
0 / 3 |
90 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| update | |
0.00% |
0 / 33 |
|
0.00% |
0 / 1 |
42 | |||
| getCheckedFiles | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| updateItem | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| 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) 2016 (original work) Open Assessment Technologies SA ; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoQtiItem\model\update; |
| 23 | |
| 24 | use oat\taoQtiItem\model\qti\ParserFactory; |
| 25 | use RecursiveIteratorIterator; |
| 26 | use RecursiveDirectoryIterator; |
| 27 | |
| 28 | abstract class ItemUpdater |
| 29 | { |
| 30 | protected $itemPath = ''; |
| 31 | protected $checkedFiles = []; |
| 32 | |
| 33 | /** |
| 34 | * Init the item updater with the item directory path |
| 35 | * |
| 36 | * @param string $itemRootPath |
| 37 | * @throws \common_Exception |
| 38 | */ |
| 39 | public function __construct($itemRootPath) |
| 40 | { |
| 41 | if (file_exists($itemRootPath)) { |
| 42 | $this->itemPath = $itemRootPath; |
| 43 | } else { |
| 44 | throw new \common_Exception('the given item root path does not exist'); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Update all the item files found within the $itemRootPath |
| 50 | * @param boolean $changeItemContent - tells if the item files will be written with the updated content or not |
| 51 | * @return array of modified item instances |
| 52 | */ |
| 53 | public function update($changeItemContent = false) |
| 54 | { |
| 55 | $returnValue = []; |
| 56 | $objects = new RecursiveIteratorIterator( |
| 57 | new RecursiveDirectoryIterator($this->itemPath), |
| 58 | RecursiveIteratorIterator::SELF_FIRST |
| 59 | ); |
| 60 | $i = 0; |
| 61 | $fixed = 0; |
| 62 | |
| 63 | foreach ($objects as $itemFile => $cursor) { |
| 64 | if (is_file($itemFile)) { |
| 65 | $this->checkedFiles[$itemFile] = false; |
| 66 | |
| 67 | if (basename($itemFile) === 'qti.xml') { |
| 68 | $i++; |
| 69 | $xml = new \DOMDocument(); |
| 70 | $xml->load($itemFile); |
| 71 | |
| 72 | $parser = new ParserFactory($xml); |
| 73 | $item = $parser->load(); |
| 74 | \common_Logger::i( |
| 75 | 'checking item #' . $i . ' id:' . $item->attr('identifier') . ' file:' . $itemFile |
| 76 | ); |
| 77 | |
| 78 | if ($this->updateItem($item, $itemFile)) { |
| 79 | $this->checkedFiles[$itemFile] = true; |
| 80 | $returnValue[$itemFile] = $item; |
| 81 | \common_Logger::i( |
| 82 | 'fixed required for #' . $i . ' id:' . $item->attr('identifier') . ' file:' . $itemFile |
| 83 | ); |
| 84 | |
| 85 | if ($changeItemContent) { |
| 86 | $fixed++; |
| 87 | \common_Logger::i( |
| 88 | 'item fixed #' . $i . ' id:' . $item->attr('identifier') . ' file:' . $itemFile |
| 89 | ); |
| 90 | file_put_contents($itemFile, $item->toXML()); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | \common_Logger::i('total item fixed : ' . $fixed); |
| 98 | return $returnValue; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Get the list of checked files |
| 103 | * @return array |
| 104 | */ |
| 105 | public function getCheckedFiles() |
| 106 | { |
| 107 | return $this->checkedFiles; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Try updating an single item instance, |
| 112 | * Returns true if the content has been changed, false otherwise |
| 113 | * |
| 114 | * @param oat\taoQtiItem\modal\Item $item |
| 115 | * @param string $itemFile |
| 116 | * @return boolean |
| 117 | */ |
| 118 | abstract protected function updateItem(\oat\taoQtiItem\model\qti\Item $item, $itemFile); |
| 119 | } |