| Conditions | 15 |
| Paths | 337 |
| Total Lines | 172 |
| Code Lines | 116 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 31 | public function up(Schema $schema): void |
||
| 32 | { |
||
| 33 | $container = $this->getContainer(); |
||
| 34 | $doctrine = $container->get('doctrine'); |
||
| 35 | $em = $doctrine->getManager(); |
||
| 36 | /** @var Connection $connection */ |
||
| 37 | $connection = $em->getConnection(); |
||
| 38 | |||
| 39 | $studentPublicationRepo = $container->get(CStudentPublicationRepository::class); |
||
| 40 | $studentPublicationCommentRepo = $container->get(CStudentPublicationCommentRepository::class); |
||
| 41 | $studentPublicationCorrectionRepo = $container->get(CStudentPublicationCorrectionRepository::class); |
||
| 42 | |||
| 43 | $courseRepo = $container->get(CourseRepository::class); |
||
| 44 | $sessionRepo = $container->get(SessionRepository::class); |
||
| 45 | $groupRepo = $container->get(CGroupRepository::class); |
||
| 46 | $userRepo = $container->get(UserRepository::class); |
||
| 47 | /** @var Kernel $kernel */ |
||
| 48 | $kernel = $container->get('kernel'); |
||
| 49 | $rootPath = $kernel->getProjectDir(); |
||
| 50 | |||
| 51 | $admin = $this->getAdmin(); |
||
| 52 | |||
| 53 | $q = $em->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c'); |
||
| 54 | /** @var Course $course */ |
||
| 55 | foreach ($q->toIterable() as $course) { |
||
| 56 | $courseId = $course->getId(); |
||
| 57 | $course = $courseRepo->find($courseId); |
||
| 58 | |||
| 59 | // Assignments folders |
||
| 60 | $sql = "SELECT * FROM c_student_publication WHERE contains_file = 0 AND c_id = $courseId |
||
| 61 | ORDER BY iid"; |
||
| 62 | $result = $connection->executeQuery($sql); |
||
| 63 | $items = $result->fetchAllAssociative(); |
||
| 64 | foreach ($items as $itemData) { |
||
| 65 | $id = $itemData['iid']; |
||
| 66 | /** @var CStudentPublication $resource */ |
||
| 67 | $resource = $studentPublicationRepo->find($id); |
||
| 68 | if ($resource->hasResourceNode()) { |
||
| 69 | continue; |
||
| 70 | } |
||
| 71 | |||
| 72 | $result = $this->fixItemProperty( |
||
| 73 | 'work', |
||
| 74 | $studentPublicationRepo, |
||
| 75 | $course, |
||
| 76 | $admin, |
||
| 77 | $resource, |
||
| 78 | $course |
||
| 79 | ); |
||
| 80 | |||
| 81 | if (false === $result) { |
||
| 82 | continue; |
||
| 83 | } |
||
| 84 | |||
| 85 | $em->persist($resource); |
||
| 86 | $em->flush(); |
||
| 87 | } |
||
| 88 | |||
| 89 | $em->flush(); |
||
| 90 | $em->clear(); |
||
| 91 | |||
| 92 | // Assignments files |
||
| 93 | $sql = "SELECT * FROM c_student_publication |
||
| 94 | WHERE contains_file = 1 AND c_id = $courseId |
||
| 95 | ORDER BY iid"; |
||
| 96 | $result = $connection->executeQuery($sql); |
||
| 97 | $items = $result->fetchAllAssociative(); |
||
| 98 | foreach ($items as $itemData) { |
||
| 99 | $id = $itemData['iid']; |
||
| 100 | $path = $itemData['url']; |
||
| 101 | $title = $itemData['title']; |
||
| 102 | $parentId = $itemData['parent_id']; |
||
| 103 | /** @var CStudentPublication $resource */ |
||
| 104 | $resource = $studentPublicationRepo->find($id); |
||
| 105 | if ($resource->hasResourceNode()) { |
||
| 106 | continue; |
||
| 107 | } |
||
| 108 | |||
| 109 | $parent = $studentPublicationRepo->find($parentId); |
||
| 110 | |||
| 111 | $result = $this->fixItemProperty( |
||
| 112 | 'work', |
||
| 113 | $studentPublicationRepo, |
||
| 114 | $course, |
||
| 115 | $admin, |
||
| 116 | $resource, |
||
| 117 | $parent |
||
| 118 | ); |
||
| 119 | |||
| 120 | if (false === $result) { |
||
| 121 | continue; |
||
| 122 | } |
||
| 123 | |||
| 124 | $filePath = $rootPath.'/app/courses/'.$course->getDirectory().'/'.$path; |
||
| 125 | $this->addLegacyFileToResource($filePath, $studentPublicationRepo, $resource, $id, $title); |
||
| 126 | $em->persist($resource); |
||
| 127 | $em->flush(); |
||
| 128 | } |
||
| 129 | |||
| 130 | $em->flush(); |
||
| 131 | $em->clear(); |
||
| 132 | |||
| 133 | $admin = $this->getAdmin(); |
||
| 134 | |||
| 135 | // Corrections. |
||
| 136 | $sql = "SELECT * FROM c_student_publication |
||
| 137 | WHERE |
||
| 138 | (title_correction <> '' OR title_correction IS NOT NULL) AND |
||
| 139 | c_id = $courseId |
||
| 140 | ORDER BY iid"; |
||
| 141 | $result = $connection->executeQuery($sql); |
||
| 142 | $items = $result->fetchAllAssociative(); |
||
| 143 | foreach ($items as $itemData) { |
||
| 144 | $id = $itemData['iid']; |
||
| 145 | $title = $itemData['title_correction']; |
||
| 146 | $path = $itemData['url_correction']; |
||
| 147 | |||
| 148 | $course = $courseRepo->find($courseId); |
||
| 149 | /** @var CStudentPublication $studentPublication */ |
||
| 150 | $studentPublication = $studentPublicationRepo->find($id); |
||
| 151 | $correction = $studentPublication->getCorrection(); |
||
| 152 | if (null !== $correction) { |
||
| 153 | continue; |
||
| 154 | } |
||
| 155 | |||
| 156 | $correction = new CStudentPublicationCorrection(); |
||
| 157 | $correction->setTitle($title); |
||
| 158 | $correction->setParent($studentPublication); |
||
| 159 | $studentPublicationCorrectionRepo->addResourceNode($correction, $admin, $studentPublication); |
||
| 160 | $em->persist($correction); |
||
| 161 | |||
| 162 | $filePath = $rootPath.'/app/courses/'.$course->getDirectory().'/'.$path; |
||
| 163 | $this->addLegacyFileToResource($filePath, $studentPublicationCorrectionRepo, $correction, null, $title); |
||
| 164 | $em->persist($correction); |
||
| 165 | $em->flush(); |
||
| 166 | } |
||
| 167 | |||
| 168 | $em->flush(); |
||
| 169 | $em->clear(); |
||
| 170 | |||
| 171 | // Comments |
||
| 172 | $sql = "SELECT * FROM c_student_publication_comment WHERE c_id = $courseId |
||
| 173 | ORDER BY iid"; |
||
| 174 | $result = $connection->executeQuery($sql); |
||
| 175 | $items = $result->fetchAllAssociative(); |
||
| 176 | foreach ($items as $itemData) { |
||
| 177 | $id = $itemData['iid']; |
||
| 178 | $file = $itemData['file']; |
||
| 179 | $workId = $itemData['work_id']; |
||
| 180 | /** @var CStudentPublicationComment $resource */ |
||
| 181 | $resource = $studentPublicationCommentRepo->find($id); |
||
| 182 | if ($resource->hasResourceNode()) { |
||
| 183 | continue; |
||
| 184 | } |
||
| 185 | /** @var CStudentPublication $parent */ |
||
| 186 | $parent = $studentPublicationRepo->find($workId); |
||
| 187 | $sql = "SELECT * FROM c_student_publication WHERE c_id = $courseId AND id = $workId"; |
||
| 188 | $result = $connection->executeQuery($sql); |
||
| 189 | $work = $result->fetchAssociative(); |
||
| 190 | if (empty($work)) { |
||
| 191 | continue; |
||
| 192 | } |
||
| 193 | $session = empty($work['session_id']) ? null : $sessionRepo->find($work['session_id']); |
||
| 194 | $group = empty($work['post_group_id']) ? null : $groupRepo->find($work['post_group_id']); |
||
| 195 | |||
| 196 | $resource->setParent($parent); |
||
| 197 | $resource->addCourseLink($course, $session, $group, ResourceLink::VISIBILITY_PUBLISHED); |
||
| 198 | |||
| 199 | $filePath = $rootPath.'/app/courses/'.$course->getDirectory().'/'.$file; |
||
| 200 | $this->addLegacyFileToResource($filePath, $studentPublicationRepo, $resource, $id, $title); |
||
|
|
|||
| 201 | $em->persist($resource); |
||
| 202 | $em->flush(); |
||
| 203 | } |
||
| 207 |