Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 58 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
History | |
0.00% |
0 / 58 |
|
0.00% |
0 / 5 |
132 | |
0.00% |
0 / 1 |
getRevisionService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
index | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
12 | |||
restoreRevision | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
12 | |||
commitResource | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
2 | |||
getValidatedResource | |
0.00% |
0 / 7 |
|
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) 2015 (original work) Open Assessment Technologies SA; |
19 | */ |
20 | |
21 | namespace oat\taoRevision\controller; |
22 | |
23 | use common_Exception; |
24 | use common_exception_MissingParameter; |
25 | use common_exception_ResourceNotFound; |
26 | use core_kernel_classes_Resource; |
27 | use oat\generis\model\OntologyAwareTrait; |
28 | use oat\oatbox\service\ConfigurableService; |
29 | use oat\oatbox\service\exception\InvalidServiceManagerException; |
30 | use oat\oatbox\service\ServiceManagerAwareTrait; |
31 | use oat\tao\helpers\UserHelper; |
32 | use oat\taoRevision\model\RepositoryInterface; |
33 | use oat\taoRevision\model\RevisionNotFoundException; |
34 | use tao_actions_CommonModule; |
35 | use tao_helpers_Date; |
36 | use tao_helpers_Display; |
37 | |
38 | /** |
39 | * Revision history management controller |
40 | * |
41 | * @author Open Assessment Technologies SA |
42 | * @package taoRevision |
43 | * @license GPL-2.0 |
44 | * |
45 | */ |
46 | class History extends tao_actions_CommonModule |
47 | { |
48 | use ServiceManagerAwareTrait; |
49 | use OntologyAwareTrait; |
50 | |
51 | /** |
52 | * @return ConfigurableService |
53 | * @throws InvalidServiceManagerException |
54 | */ |
55 | protected function getRevisionService() |
56 | { |
57 | return $this->getServiceManager()->get(RepositoryInterface::SERVICE_ID); |
58 | } |
59 | |
60 | /** |
61 | * @requiresRight id WRITE |
62 | * |
63 | * @throws InvalidServiceManagerException |
64 | * @throws common_Exception |
65 | * @throws common_exception_MissingParameter |
66 | * @throws common_exception_ResourceNotFound |
67 | */ |
68 | public function index() |
69 | { |
70 | $bodyContent = $this->getPsrRequest()->getParsedBody(); |
71 | |
72 | $resource = $this->getValidatedResource($bodyContent); |
73 | |
74 | $revisions = $this->getRevisionService()->getAllRevisions($resource->getUri()); |
75 | |
76 | $revisionsList = []; |
77 | foreach ($revisions as $revision) { |
78 | if ($author = $revision->getAuthorId()) { |
79 | $author = UserHelper::renderHtmlUser($revision->getAuthorId()); |
80 | } |
81 | $revisionsList[] = [ |
82 | 'id' => $revision->getVersion(), |
83 | 'modified' => tao_helpers_Date::displayeDate($revision->getDateCreated()), |
84 | 'author' => $author, |
85 | 'message' => tao_helpers_Display::htmlize($revision->getMessage()), |
86 | ]; |
87 | } |
88 | |
89 | $this->setData('resourceLabel', tao_helpers_Display::htmlize($resource->getLabel())); |
90 | $this->setData('id', $resource->getUri()); |
91 | $this->setData('revisions', $revisionsList); |
92 | $this->setData('allowCreateRevision', $this->hasAccess(self::class, 'commitResource', [])); |
93 | $this->setData('allowRestoreRevision', $this->hasAccess(self::class, 'restoreRevision', [])); |
94 | $this->setData('revisions', $revisionsList); |
95 | $this->setView('History/index.tpl'); |
96 | } |
97 | |
98 | /** |
99 | * @requiresRight id WRITE |
100 | * |
101 | * @throws InvalidServiceManagerException |
102 | * @throws RevisionNotFoundException |
103 | * @throws common_Exception |
104 | * @throws common_exception_MissingParameter |
105 | * @throws common_exception_ResourceNotFound |
106 | */ |
107 | public function restoreRevision() |
108 | { |
109 | $bodyContent = $this->getPsrRequest()->getParsedBody(); |
110 | |
111 | $resource = $this->getValidatedResource($bodyContent); |
112 | $previousVersion = $bodyContent['revisionId'] ?? null; |
113 | $commitMessage = $bodyContent['message'] ?? ''; |
114 | |
115 | if ($previousVersion === null) { |
116 | throw new common_exception_MissingParameter('revisionId'); |
117 | } |
118 | |
119 | $previousRevision = $this->getRevisionService()->getRevision($resource->getUri(), $previousVersion); |
120 | |
121 | if ($this->getRevisionService()->restore($previousRevision)) { |
122 | $newRevision = $this->getRevisionService()->commit($resource, $commitMessage); |
123 | $this->returnJson([ |
124 | 'success' => true, |
125 | 'id' => $newRevision->getVersion(), |
126 | 'modified' => tao_helpers_Date::displayeDate($newRevision->getDateCreated()), |
127 | 'author' => UserHelper::renderHtmlUser($newRevision->getAuthorId()), |
128 | 'message' => $newRevision->getMessage(), |
129 | ]); |
130 | } else { |
131 | $this->returnError(__('Unable to restore the selected version')); |
132 | } |
133 | } |
134 | |
135 | /** |
136 | * @requiresRight id WRITE |
137 | * |
138 | * @throws InvalidServiceManagerException |
139 | * @throws common_Exception |
140 | * @throws common_exception_MissingParameter |
141 | * @throws common_exception_ResourceNotFound |
142 | */ |
143 | public function commitResource() |
144 | { |
145 | $bodyContent = $this->getPsrRequest()->getParsedBody(); |
146 | |
147 | $resource = $this->getValidatedResource($bodyContent); |
148 | $message = $bodyContent['message'] ?? ''; |
149 | |
150 | $revision = $this->getRevisionService()->commit($resource, $message); |
151 | |
152 | $this->returnJson([ |
153 | 'success' => true, |
154 | 'id' => $revision->getVersion(), |
155 | 'modified' => tao_helpers_Date::displayeDate($revision->getDateCreated()), |
156 | 'author' => UserHelper::renderHtmlUser($revision->getAuthorId()), |
157 | 'message' => $revision->getMessage(), |
158 | 'allowRestoreRevision' => $this->hasAccess(self::class, 'restoreRevision', []), |
159 | 'commitMessage' => __('%s has been committed', $resource->getLabel()), |
160 | ]); |
161 | } |
162 | |
163 | /** |
164 | * @param array $body |
165 | * |
166 | * @return core_kernel_classes_Resource |
167 | * @throws common_exception_MissingParameter |
168 | * @throws common_exception_ResourceNotFound |
169 | */ |
170 | private function getValidatedResource(array $body) |
171 | { |
172 | $id = $body['id'] ?? null; |
173 | |
174 | if ($id === null) { |
175 | throw new common_exception_MissingParameter('id'); |
176 | } |
177 | |
178 | $resource = $this->getResource($id); |
179 | |
180 | if (!$resource->exists()) { |
181 | throw new common_exception_ResourceNotFound(sprintf('Resource not found for requested id %s', $id)); |
182 | } |
183 | |
184 | return $resource; |
185 | } |
186 | } |