|
108 | 108 | $course = api_get_course_entity(); |
109 | 109 | $results = []; |
110 | 110 |
|
111 | | - $createDocument = function(string $path, string $filename, string $mimetype, int $filesize, bool $ifExists) use ( |
112 | | - $repo, $em, $course, $directoryParentId, &$results |
| 111 | + // --------- LP context (optional): auto-create LP items when uploading from LP builder ---------- |
| 112 | + $lpId = (int) ($_REQUEST['lp_id'] ?? ($_POST['lp_id'] ?? 0)); |
| 113 | + $lpAutoAdd = ((int) ($_REQUEST['lp_auto_add'] ?? ($_POST['lp_auto_add'] ?? 0))) === 1; |
| 114 | + |
| 115 | + $oLP = null; |
| 116 | + $lpItemsCreated = []; |
| 117 | + |
| 118 | + if ($lpAutoAdd && $lpId > 0) { |
| 119 | + $lp = Container::getLpRepository()->find($lpId); |
| 120 | + if ($lp) { |
| 121 | + $courseInfo = api_get_course_info(); |
| 122 | + $oLP = new learnpath($lp, $courseInfo, api_get_user_id()); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + $createDocument = function( |
| 127 | + string $path, |
| 128 | + string $filename, |
| 129 | + string $mimetype, |
| 130 | + int $filesize, |
| 131 | + string $ifExists |
| 132 | + ) use ( |
| 133 | + $repo, |
| 134 | + $em, |
| 135 | + $course, |
| 136 | + $directoryParentId, |
| 137 | + &$results, |
| 138 | + $oLP, |
| 139 | + &$lpItemsCreated |
113 | 140 | ) { |
114 | 141 | $qb = $em->createQueryBuilder() |
115 | 142 | ->select('d') |
|
153 | 180 |
|
154 | 181 | $repo->addFileFromPath($doc, $filename, $path); |
155 | 182 |
|
| 183 | + // --------- If in LP mode, also create the LP item (c_lp_item) ---------- |
| 184 | + $createdLpItemId = 0; |
| 185 | + if ($oLP) { |
| 186 | + $lpItemRepo = Container::getLpItemRepository(); |
| 187 | + $root = $lpItemRepo->getRootItem($oLP->get_id()); |
| 188 | + |
| 189 | + $createdLpItemId = (int) $oLP->add_item( |
| 190 | + $root, // parent |
| 191 | + '', // previous |
| 192 | + TOOL_DOCUMENT, // item type |
| 193 | + (string) $doc->getIid(), // path = document iid |
| 194 | + $doc->getTitle(), |
| 195 | + '', // description |
| 196 | + '' // prerequisites |
| 197 | + ); |
| 198 | + |
| 199 | + $oLP->set_modified_on(); |
| 200 | + $lpItemsCreated[] = $createdLpItemId; |
| 201 | + } |
| 202 | + |
156 | 203 | $results[] = [ |
157 | | - 'name' => api_htmlentities($doc->getTitle()), |
158 | | - 'url' => $repo->getResourceFileUrl($doc), |
159 | | - 'size' => format_file_size($filesize), |
160 | | - 'type' => api_htmlentities($mimetype), |
161 | | - 'result' => Display::return_icon('accept.png', get_lang('Uploaded.')) |
| 204 | + 'name' => api_htmlentities($doc->getTitle()), |
| 205 | + 'url' => $repo->getResourceFileUrl($doc), |
| 206 | + 'size' => format_file_size($filesize), |
| 207 | + 'type' => api_htmlentities($mimetype), |
| 208 | + 'result' => Display::return_icon('accept.png', get_lang('Uploaded.')), |
| 209 | + 'lp_item_id' => $createdLpItemId, |
162 | 210 | ]; |
163 | 211 | }; |
164 | 212 |
|
|
182 | 230 | $f->getFilename(), |
183 | 231 | mime_content_type($f->getRealPath()), |
184 | 232 | $f->getSize(), |
185 | | - $ifExists |
| 233 | + (string) $ifExists |
186 | 234 | ); |
187 | 235 | } |
188 | 236 | } |
|
194 | 242 | $fileInfo['tmp_name'], |
195 | 243 | $fileInfo['name'], |
196 | 244 | $fileInfo['type'], |
197 | | - $fileInfo['size'], |
198 | | - $ifExists |
| 245 | + (int) $fileInfo['size'], |
| 246 | + (string) $ifExists |
199 | 247 | ); |
200 | 248 | } |
201 | 249 |
|
202 | 250 | header('Content-Type: application/json'); |
203 | | - echo json_encode(['files' => $results]); |
| 251 | + echo json_encode([ |
| 252 | + 'files' => $results, |
| 253 | + 'lp_refresh' => !empty($lpItemsCreated), |
| 254 | + 'lp_items_created' => $lpItemsCreated, |
| 255 | + ]); |
204 | 256 | exit; |
205 | 257 | } |
206 | 258 | exit; |
0 commit comments