migrate_item_property.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Entity\CDocument;
  4. use Chamilo\CoreBundle\Framework\Container;
  5. use Chamilo\CoreBundle\Entity\Resource\ResourceNode;
  6. use Chamilo\CoreBundle\Entity\Resource\ResourceLink;
  7. use Chamilo\CoreBundle\Entity\Resource\ResourceFile;
  8. use Chamilo\CoreBundle\Entity\Resource\ResourceType;
  9. use Chamilo\CoreBundle\Entity\Resource\ResourceRight;
  10. use Chamilo\MediaBundle\Entity\Media;
  11. use Chamilo\CoreBundle\Security\Authorization\Voter\ResourceNodeVoter;
  12. /**
  13. * Migrate content from c_item_property and c_document tables to the new "Resource" system.
  14. *
  15. */
  16. echo 'First check if table "classification__category" has a default category; if not then run: <br />';
  17. echo 'bin/console sonata:media:fix-media-context';
  18. echo 'change course id in the query';
  19. //exit;
  20. // For tests to clean all resource stuff:
  21. //
  22. require_once __DIR__.'/../../main/inc/global.inc.php';
  23. $sql = "SELECT
  24. d.id,
  25. d.c_id,
  26. d.session_id,
  27. i.to_group_id,
  28. i.to_user_id,
  29. i.iid,
  30. insert_user_id,
  31. insert_date,
  32. lastedit_date,
  33. tool,
  34. visibility
  35. FROM c_item_property i
  36. INNER JOIN c_document d
  37. ON (d.iid = i.ref AND i.c_id = d.c_id)
  38. WHERE
  39. i.tool = 'document' AND
  40. d.c_id = 12
  41. ORDER BY d.path";
  42. $result = Database::query($sql);
  43. $em = Database::getManager();
  44. $resourceType = $em->getRepository('ChamiloCoreBundle:Resource\ResourceType')->findOneBy(['name' => 'document']);
  45. $coursePath = api_get_path(SYS_PATH).'app/courses/';
  46. $mediaManager = Container::$container->get('sonata.media.manager.media');
  47. $documentManager = $em->getRepository('ChamiloCourseBundle:CDocument');
  48. $contextManager = Container::$container->get('sonata.classification.manager.context');
  49. $defaultContext = $contextManager->findOneBy(['id' => 'default']);
  50. while ($row = Database::fetch_array($result, 'ASSOC')) {
  51. $itemIid = $row['iid'];
  52. $courseId = $row['c_id'];
  53. $sessionId = $row['session_id'];
  54. $groupId = $row['to_group_id'];
  55. $toUserId = $row['to_user_id'];
  56. $documentId = $row['id'];
  57. $toUser = api_get_user_entity($toUserId);
  58. $author = api_get_user_entity($row['insert_user_id']);
  59. if (empty($author)) {
  60. error_log("User does not exists in the DB ".$row['insert_user_id']);
  61. continue;
  62. }
  63. $createdAt = api_get_utc_datetime($row['insert_date'], true, true);
  64. $lastUpdatedAt = api_get_utc_datetime($row['lastedit_date'], true, true);
  65. $course = api_get_course_entity($courseId);
  66. if (empty($course)) {
  67. error_log("Course does not exists in the DB $courseId");
  68. continue;
  69. }
  70. $session = api_get_session_entity($sessionId);
  71. $group = api_get_group_entity($groupId);
  72. switch ($row['tool']) {
  73. case 'document':
  74. $documentData = DocumentManager::get_document_data_by_id($documentId, $course->getCode(), true, $sessionId);
  75. if (!$documentData) {
  76. //$documentData = DocumentManager::get_document_data_by_id($row['ref'], $course->getCode(), $sessionId);
  77. error_log("Skipped item property iid #$itemIid");
  78. continue 2;
  79. }
  80. $folderPath = $course->getDirectory().'/document/'.$documentData['path'];
  81. $file = $coursePath.$folderPath;
  82. $document = $documentManager->find($documentData['iid']);
  83. var_dump('Parsing document iid #'.$document->getIid());
  84. // Find parent node
  85. $parentNode = null;
  86. if (!empty($documentData['parent_id'])) {
  87. /** @var CDocument $parentDocument */
  88. $parentDocument = $documentManager->find($documentData['parent_id']);
  89. $parentNode = $parentDocument->getResourceNode();
  90. }
  91. // Creating node
  92. $node = new ResourceNode();
  93. $node
  94. ->setName($documentData['title'])
  95. ->setDescription($documentData['comment'] ?? '')
  96. ->setCreator($author)
  97. ->setParent($parentNode)
  98. ->setResourceType($resourceType)
  99. ->setCreatedAt($createdAt)
  100. ->setUpdatedAt($lastUpdatedAt)
  101. ;
  102. $em->persist($node);
  103. $document->setResourceNode($node);
  104. $em->persist($document);
  105. $rights = [];
  106. switch ($row['visibility']) {
  107. case '0':
  108. $newVisibility = ResourceLink::VISIBILITY_DRAFT;
  109. $editorMask = ResourceNodeVoter::getEditorMask();
  110. $resourceRight = new ResourceRight();
  111. $resourceRight
  112. ->setMask($editorMask)
  113. ->setRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER)
  114. ;
  115. $rights[] = $resourceRight;
  116. /*$resourceRight = new ResourceRight();
  117. $resourceRight
  118. ->setMask($readerMask)
  119. ->setRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_STUDENT)
  120. ;
  121. $rights[] = $resourceRight;*/
  122. break;
  123. case '1':
  124. $newVisibility = ResourceLink::VISIBILITY_PUBLISHED;
  125. break;
  126. case '2':
  127. $newVisibility = ResourceLink::VISIBILITY_DELETED;
  128. break;
  129. }
  130. $link = new ResourceLink();
  131. $link
  132. ->setCourse($course)
  133. ->setSession($session)
  134. ->setGroup($group)
  135. ->setUser($toUser)
  136. ->setResourceNode($node)
  137. ->setVisibility($newVisibility)
  138. ;
  139. if (!empty($rights)) {
  140. foreach ($rights as $right) {
  141. $link->addResourceRight($right);
  142. }
  143. }
  144. switch ($documentData['filetype']) {
  145. case 'folder':
  146. // Folder doesn't need a ResourceFile or Media entity
  147. break;
  148. case 'file':
  149. /** @var Media $media */
  150. $media = $mediaManager->create();
  151. $media->setName($documentData['title']);
  152. $fileName = basename($documentData['path']);
  153. $extension = pathinfo($fileName, PATHINFO_EXTENSION);
  154. $media->setSize($documentData['size']);
  155. $media->setContext('default');
  156. $provider = 'sonata.media.provider.file';
  157. if (in_array($extension, ['jpeg', 'jpg', 'gif', 'png'])) {
  158. $provider = 'sonata.media.provider.image';
  159. }
  160. $media->setProviderName($provider);
  161. $media->setEnabled(true);
  162. $media->setBinaryContent($file);
  163. $mediaManager->save($media, true);
  164. $resourceFile = new ResourceFile();
  165. $resourceFile->setMedia($media);
  166. $resourceFile->setName($documentData['title']);
  167. $node->setResourceFile($resourceFile);
  168. $em->persist($resourceFile);
  169. $em->persist($node);
  170. break;
  171. }
  172. $em->persist($link);
  173. $em->flush();
  174. break;
  175. }
  176. }
  177. /**
  178. default resource type list
  179. | blog_management | blog
  180. | calendar_event |
  181. | calendar_event_attachment |
  182. | course_description |
  183. | document |
  184. | dropbox |
  185. *
  186. *
  187. *
  188. | forum |
  189. | forum_attachment |
  190. | forum_category |
  191. | forum_post |
  192. | forum_thread |
  193. *
  194. | glossary |
  195. *
  196. *
  197. | link |
  198. | link_category |
  199. * -
  200. | test_category |
  201. | thematic |
  202. | thematic_advance |
  203. | thematic_plan |
  204. | work |
  205. */