From edf8243f5d1742904d87bcaeb3f02573c17bda36 Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Wed, 5 Jun 2019 15:02:03 +0200 Subject: [PATCH] Update add missing php files from "tests" folder --- tests/scripts/check_extra_fields.php | 134 +++++++++ ...elete_deleted_exercises_with_questions.php | 67 +++++ tests/scripts/delete_items_from_csv.php | 61 ++++ tests/scripts/delete_orphan_questions.php | 26 ++ tests/scripts/edit_course_html_files.php | 36 +++ tests/scripts/fix_documents_path.php | 134 +++++++++ tests/scripts/fix_lp_calendar.php | 40 +++ tests/scripts/fix_lp_id_to_iid_v2.php | 276 ++++++++++++++++++ tests/scripts/restore_deleted_documents.php | 85 ++++++ 9 files changed, 859 insertions(+) create mode 100644 tests/scripts/check_extra_fields.php create mode 100644 tests/scripts/delete_deleted_exercises_with_questions.php create mode 100644 tests/scripts/delete_items_from_csv.php create mode 100644 tests/scripts/delete_orphan_questions.php create mode 100644 tests/scripts/edit_course_html_files.php create mode 100644 tests/scripts/fix_documents_path.php create mode 100644 tests/scripts/fix_lp_calendar.php create mode 100644 tests/scripts/fix_lp_id_to_iid_v2.php create mode 100644 tests/scripts/restore_deleted_documents.php diff --git a/tests/scripts/check_extra_fields.php b/tests/scripts/check_extra_fields.php new file mode 100644 index 0000000000..c4fc3cc7c6 --- /dev/null +++ b/tests/scripts/check_extra_fields.php @@ -0,0 +1,134 @@ +getRepository('ChamiloCoreBundle:ExtraField'); +$extraFields = $repo->findAll(); + +$list = [ + 'legal_accept' => "INSERT INTO extra_field (extra_field_type, field_type, variable, display_text, visible_to_self, changeable, created_at) VALUES (1, 1, 'legal_accept','Legal',0,0, NOW());", + 'already_logged_in' => "INSERT INTO extra_field (extra_field_type, field_type, variable, display_text, visible_to_self, changeable, created_at) VALUES (1, 1, 'already_logged_in','Already logged in',0,0, NOW());", + 'update_type' => "INSERT INTO extra_field (extra_field_type, field_type, variable, display_text, visible_to_self, changeable, created_at) VALUES (1, 1, 'update_type','Update script type',0,0, NOW())", + 'rssfeeds' => "INSERT INTO extra_field (extra_field_type, field_type, variable, display_text, visible_to_self, changeable, created_at) VALUES (1, 1, 'rssfeeds','RSS',0,0, NOW())", + 'dashboard' => "INSERT INTO extra_field (extra_field_type, field_type, variable, display_text, visible_to_self, changeable, created_at) VALUES (1, 1, 'dashboard', 'Dashboard', 0, 0, NOW())", + 'timezone' => "INSERT INTO extra_field (extra_field_type, field_type, variable, display_text, visible_to_self, changeable, created_at) VALUES (1, 11, 'timezone', 'Timezone', 0, 0, NOW())", + 'user_chat_status' => "INSERT INTO extra_field (extra_field_type, field_type, variable, display_text, visible_to_self, changeable, created_at) VALUES (1, 1, 'user_chat_status','User chat status',0,0, NOW())", + 'google_calendar_url' => "INSERT INTO extra_field (extra_field_type, field_type, variable, display_text, visible_to_self, changeable, created_at) VALUES (1, 1, 'google_calendar_url','Google Calendar URL',0,0, NOW())", + 'special_course' => "INSERT INTO extra_field (extra_field_type, field_type, variable, display_text, visible_to_self, changeable, default_value, created_at) VALUES (2, 13, 'special_course', 'Special course', 1 , 1, '', NOW())", + 'video_url' => "INSERT INTO extra_field (extra_field_type, field_type, variable, display_text, visible_to_self, changeable, created_at) VALUES (2, 19, 'video_url', 'VideoUrl', 1, 1, NOW())", + 'image' => "INSERT INTO extra_field (extra_field_type, field_type, variable, display_text, visible_to_self, changeable, created_at) VALUES (3, 16, 'image', 'Image', 1, 1, NOW())", + 'captcha_blocked_until_date' => "INSERT INTO extra_field (extra_field_type, field_type, variable, display_text, visible_to_self, changeable, created_at) VALUES (1, 1, 'captcha_blocked_until_date', 'Account locked until', 0, 0, NOW())", +]; + +$extraFieldList = []; +/** @var \Chamilo\CoreBundle\Entity\ExtraField $extraField */ +foreach ($extraFields as $extraField) { + $extraFieldList[$extraField->getVariable()] = $extraField; +} +$extraFieldVariableList = array_keys($extraFieldList); + +$queriesExecuted = []; +foreach ($list as $variable => $sql) { + if (!in_array($variable, $extraFieldVariableList)) { + Database::query($sql); + $queriesExecuted[] = $sql; + } +} + +if (!isset($extraFieldList['mail_notify_invitation'])) { + $sql = "INSERT INTO extra_field (extra_field_type, field_type, variable, display_text, visible_to_self, changeable, default_value, created_at) values (1, 4, 'mail_notify_invitation', 'MailNotifyInvitation',0,1,'1', NOW())"; + Database::query($sql); + $queriesExecuted[] = $sql; + $id = Database::insert_id(); + $sql = "INSERT INTO extra_field_options (field_id, option_value, display_text, option_order) VALUES ($id, '1', 'AtOnce',1)"; + Database::query($sql); + $queriesExecuted[] = $sql; + $sql = "INSERT INTO extra_field_options (field_id, option_value, display_text, option_order) VALUES ($id, '8', 'Daily',2)"; + Database::query($sql); + $queriesExecuted[] = $sql; + $sql = "INSERT INTO extra_field_options (field_id, option_value, display_text, option_order) VALUES ($id, '0', 'No',3)"; + Database::query($sql); + $queriesExecuted[] = $sql; +} + +if (!isset($extraFieldList['mail_notify_message'])) { + $sql = "INSERT INTO extra_field (extra_field_type, field_type, variable, display_text, visible_to_self, changeable, default_value, created_at) values (1, 4, 'mail_notify_message', 'MailNotifyMessage',0,1,'1', NOW())"; + Database::query($sql); + $queriesExecuted[] = $sql; + + $id = Database::insert_id(); + if ($id) { + $sql = "INSERT INTO extra_field_options (field_id, option_value, display_text, option_order) VALUES ($id, '1', 'AtOnce',1)"; + Database::query($sql); + $queriesExecuted[] = $sql; + + $sql = "INSERT INTO extra_field_options (field_id, option_value, display_text, option_order) VALUES ($id, '8', 'Daily',2)"; + Database::query($sql); + $queriesExecuted[] = $sql; + + $sql = "INSERT INTO extra_field_options (field_id, option_value, display_text, option_order) VALUES ($id, '0', 'No',3)"; + Database::query($sql); + $queriesExecuted[] = $sql; + } +} + +if (!isset($extraFieldList['mail_notify_group_message'])) { + $sql = "INSERT INTO extra_field (extra_field_type, field_type, variable, display_text, visible_to_self, changeable, default_value, created_at) values (1, 4, 'mail_notify_group_message','MailNotifyGroupMessage',0,1,'1', NOW())"; + Database::query($sql); + $queriesExecuted[] = $sql; + $id = Database::insert_id(); + if ($id) { + $sql = "INSERT INTO extra_field_options (field_id, option_value, display_text, option_order) VALUES ($id, '1', 'AtOnce',1)"; + Database::query($sql); + $queriesExecuted[] = $sql; + $sql = "INSERT INTO extra_field_options (field_id, option_value, display_text, option_order) VALUES ($id, '8', 'Daily',2)"; + Database::query($sql); + $queriesExecuted[] = $sql; + $sql = "INSERT INTO extra_field_options (field_id, option_value, display_text, option_order) VALUES ($id, '0', 'No',3)"; + Database::query($sql); + $queriesExecuted[] = $sql; + } +} + +$tag1Exists = false; +$tag2Exists = false; + +foreach ($extraFields as $extraField) { + if ($extraField->getVariable() === 'tags' && $extraField->getExtraFieldType() == 1) { + $tag1Exists = true; + } + if ($extraField->getVariable() === 'tags' && $extraField->getExtraFieldType() == 2) { + $tag2Exists = true; + } +} + +if ($tag1Exists === false) { + $sql = "INSERT INTO extra_field (extra_field_type, field_type, variable, display_text, visible_to_self, changeable, created_at) VALUES (1, 10, 'tags','tags',0,0, NOW())"; + Database::query($sql); + $queriesExecuted[] = $sql; +} + +if ($tag2Exists === false) { + $sql = "INSERT INTO extra_field (extra_field_type, field_type, variable, display_text, visible_to_self, changeable, created_at) VALUES (2, 10, 'tags', 'Tags', 1, 1, NOW())"; + Database::query($sql); + $queriesExecuted[] = $sql; +} + +if (empty($queriesExecuted)) { + echo 'No database changes'; + exit; +} + +foreach ($queriesExecuted as $query) { + echo $query.PHP_EOL; +} diff --git a/tests/scripts/delete_deleted_exercises_with_questions.php b/tests/scripts/delete_deleted_exercises_with_questions.php new file mode 100644 index 0000000000..bd3b5ab4e5 --- /dev/null +++ b/tests/scripts/delete_deleted_exercises_with_questions.php @@ -0,0 +1,67 @@ +read($id); + + $courseInfo = api_get_course_info_by_id($row['c_id']); + + $sql = "SELECT question_id, c_id + FROM c_quiz_rel_question + WHERE exercice_id = $id + ORDER BY iid"; + $result = Database::query($sql); + $questions = Database::store_result($result); + $totalQuestions = count($questions); + + echo PHP_EOL.'-------'; + echo PHP_EOL.'Deleting exercise "'.$row['title'].'" - #'.$row['iid'].PHP_EOL; + echo '-------'.PHP_EOL.PHP_EOL; + + if (!empty($questions)) { + $counter = 1; + foreach ($questions as $questionData) { + $questionId = $questionData['question_id']; + // Check if question is used in another exercise: + $sql = "SELECT count(iid) + FROM c_quiz_rel_question + WHERE exercice_id != $id AND question_id = $questionId"; + $result = Database::query($sql); + $dataQuestion = Database::fetch_array($result); + + $count = $dataQuestion['count']; + if (empty($count)) { + $question = Question::read($questionId, $courseInfo); + $question->delete(); + echo 'Deleting question '.$counter.'/'.$totalQuestions.' - #'.$questionId.PHP_EOL; + } else { + echo 'Cannot delete question, it\'s been used by another exercise'.$counter.'/'.$totalQuestions.' - #'.$questionId.PHP_EOL; + } + $counter++; + } + } + + $sql = "DELETE FROM c_quiz WHERE iid = $id"; + $result = Database::query($sql); + + $sql = "DELETE FROM c_item_property WHERE ref = $id AND c_id = $courseId and tool = 'quiz' "; + $result = Database::query($sql); +} \ No newline at end of file diff --git a/tests/scripts/delete_items_from_csv.php b/tests/scripts/delete_items_from_csv.php new file mode 100644 index 0000000000..ed7379f48d --- /dev/null +++ b/tests/scripts/delete_items_from_csv.php @@ -0,0 +1,61 @@ +exerciseList)) { + $question->delete(1); + } + echo 'Deleting question '.$counter.'/'.$totalQuestions.' - #'.$row['iid'].PHP_EOL; + $counter++; +} \ No newline at end of file diff --git a/tests/scripts/edit_course_html_files.php b/tests/scripts/edit_course_html_files.php new file mode 100644 index 0000000000..6aa3c15642 --- /dev/null +++ b/tests/scripts/edit_course_html_files.php @@ -0,0 +1,36 @@ + + */ +require __DIR__.'/../../main/inc/global.inc.php'; +api_protect_admin_script(); + +// Search string +$search = 'be:8181'; +$replace = 'be'; + +$dir = api_get_path(SYS_COURSE_PATH); +$courses = scandir($dir); +$i = 0; +foreach ($courses as $courseDir) { + if (substr($courseDir, 0, 1) === '.') { + continue; + } + exec('find '.$dir.$courseDir.'/document/ -type f -name "*.html" -exec sed -i '."'s/hn:8181/hn/g' {} +"); + //print('find '.$dir.$courseDir.'/document/ -type f -name "*.html" -exec sed -i '."'s/hn:8181/hn/g' {} +
"); + $i++; + //if ($i == 2) { + // exit; + //} + echo "Replaced all $search in ".$dir.$courseDir."
"; +} +echo "Done"; diff --git a/tests/scripts/fix_documents_path.php b/tests/scripts/fix_documents_path.php new file mode 100644 index 0000000000..9e0f4115fc --- /dev/null +++ b/tests/scripts/fix_documents_path.php @@ -0,0 +1,134 @@ +Course in: $docsPath".'
'; + if (is_dir($docsPath)) { + $finder = new \Symfony\Component\Finder\Finder(); + $finder->files()->in($docsPath)->name('*.html'); + + foreach ($finder as $file) { + echo $file->getRealPath().'
'; + $contents = file_get_contents($file->getRealPath()); + $newContent = preg_replace($pathToSearch, $newUrlAppend.'/courses/${4}', $contents); + file_put_contents($file->getRealPath(), $newContent); + } + + // Updating exercises + $sql = "SELECT iid, description FROM c_quiz WHERE c_id = $courseId"; + $result = Database::query($sql); + $items = Database::store_result($result); + foreach ($items as $item) { + $id = $item['iid']; + $newContent = preg_replace($pathToSearch, $newUrlAppend.'/courses/${4}', $item['description']); + $newContent = Database::escape_string($newContent); + $sql = "UPDATE c_quiz SET description = '$newContent' WHERE iid = $id"; + Database::query($sql); + } + + // Updating questions + $sql = "SELECT iid, question, description FROM c_quiz_question WHERE c_id = $courseId"; + $result = Database::query($sql); + $items = Database::store_result($result); + foreach ($items as $item) { + $id = $item['iid']; + $description = preg_replace($pathToSearch, $newUrlAppend.'/courses/${4}', $item['description']); + $description = Database::escape_string($description); + + $question = preg_replace($pathToSearch, $newUrlAppend.'/courses/${4}', $item['question']); + $question = Database::escape_string($question); + + $sql = "UPDATE c_quiz_question SET + description = '$description', + question = '$question' + WHERE iid = $id"; + Database::query($sql); + } + + // Updating answer + $sql = "SELECT iid, answer, comment FROM c_quiz_answer WHERE c_id = $courseId"; + $result = Database::query($sql); + $items = Database::store_result($result); + foreach ($items as $item) { + $id = $item['iid']; + $answer = preg_replace($pathToSearch, $newUrlAppend.'/courses/${4}', $item['answer']); + $answer = Database::escape_string($answer); + + $comment = preg_replace($pathToSearch, $newUrlAppend.'/courses/${4}', $item['comment']); + $comment = Database::escape_string($comment); + + $sql = "UPDATE c_quiz_answer SET + answer = '$answer', + comment = '$comment' + WHERE iid = $id"; + Database::query($sql); + } + + // Updating intros + $sql = "SELECT iid, intro_text FROM c_tool_intro WHERE c_id = $courseId"; + $result = Database::query($sql); + $items = Database::store_result($result); + foreach ($items as $item) { + $id = $item['iid']; + $text = preg_replace($pathToSearch, $newUrlAppend.'/courses/${4}', $item['intro_text']); + $text = Database::escape_string($text); + + $sql = "UPDATE c_tool_intro SET + intro_text = '$text' + WHERE iid = $id"; + Database::query($sql); + } + + // Updating Glossary + $sql = "SELECT iid, description FROM c_glossary WHERE c_id = $courseId"; + $result = Database::query($sql); + $items = Database::store_result($result); + foreach ($items as $item) { + $id = $item['iid']; + $gdescription = preg_replace($pathToSearch, $newUrlAppend.'/courses/${4}', $item['description']); + $gdescription = Database::escape_string($gdescription); + + $sql = "UPDATE c_glossary SET description = '$gdescription' WHERE iid = $id"; + Database::query($sql); + } + } else { + echo "

Path doesn't exist

".'
'; + } +} diff --git a/tests/scripts/fix_lp_calendar.php b/tests/scripts/fix_lp_calendar.php new file mode 100644 index 0000000000..d954f1a056 --- /dev/null +++ b/tests/scripts/fix_lp_calendar.php @@ -0,0 +1,40 @@ +get_handler_field_info_by_field_variable('calendar'); +if (empty($extraFieldInfo)) { + echo 'No calendar extra field'; + exit; +} + +$extraFieldId = $extraFieldInfo['id']; + +$sql = 'select iid from c_lp_item where title like "%(+)%"'; +$result = Database::query($sql); +while ($row = Database::fetch_array($result)) { + $lpItemId = $row['iid']; + + $extraField = new ExtraFieldValue('lp_item'); + $values = $extraField->get_values_by_handler_and_field_variable($lpItemId, 'calendar'); + + if (empty($values)) { + $value = 1; + $params = [ + 'field_id' => $extraFieldId, + 'value' => $value, + 'item_id' => $lpItemId, + ]; + //var_dump($params); + echo 'LP item id added: '.$lpItemId.PHP_EOL; + $extraField->save($params); + } +} + +exit; + + + + diff --git a/tests/scripts/fix_lp_id_to_iid_v2.php b/tests/scripts/fix_lp_id_to_iid_v2.php new file mode 100644 index 0000000000..feea3a7ab1 --- /dev/null +++ b/tests/scripts/fix_lp_id_to_iid_v2.php @@ -0,0 +1,276 @@ +$sql"; + //echo "New lp.iid $lpIid / old lp.id $oldId"; + $items = Database::store_result(Database::query($sql),'ASSOC'); + $lpInfo['lp_list'] = $items; + $myOnlyLpList[] = $lpInfo; + } + } + + if (!empty($myOnlyLpList)) { + foreach ($myOnlyLpList as $lpInfo) { + $lpIid = $lpInfo['iid']; + $oldId = $lpInfo['id']; + + if (!empty($lpId)) { + if ($lpId != $oldId) { + continue; + } + } + + if (empty($lpInfo['lp_list'])) { + continue; + } + + $items = $lpInfo['lp_list']; + + $itemList = []; + foreach ($items as $subItem) { + $itemList[$subItem['id']] = $subItem['iid']; + } + + $variablesToFix = [ + 'parent_item_id', + 'next_item_id', + 'prerequisite', + 'previous_item_id' + ]; + + foreach ($sessions as $sessionId) { + $correctLink = "lp/lp_controller.php?action=view&lp_id=$lpIid&id_session=$sessionId"; + $link = "newscorm/lp_controller.php?action=view&lp_id=$oldId&id_session=$sessionId"; + $secondLink = "lp/lp_controller.php?action=view&lp_id=$oldId&id_session=$sessionId"; + + $sql = "UPDATE $toolTable + SET link = '$correctLink' + WHERE + c_id = $courseId AND + (link = '$link' OR link ='$secondLink' )"; + Database::query($sql); + } + + foreach ($items as $item) { + $itemIid = $item['iid']; + $itemId = $item['id']; + + foreach ($variablesToFix as $variable) { + if (!empty($item[$variable]) && isset($itemList[$item[$variable]])) { + $newId = $itemList[$item[$variable]]; + $sql = "UPDATE $tblCLpItem SET $variable = $newId + WHERE iid = $itemIid AND c_id = $courseId AND lp_id = $oldId"; + Database::query($sql); + var_dump($sql); + } + } + + // c_lp_view + $sql = "UPDATE c_lp_view SET last_item = $itemIid + WHERE c_id = $courseId AND last_item = $itemId AND lp_id = $oldId"; + Database::query($sql); + var_dump($sql); + + // c_lp_item_view + $sql = "UPDATE c_lp_item_view SET lp_item_id = $itemIid + WHERE c_id = $courseId AND lp_item_id = $itemId "; + Database::query($sql); + var_dump($sql); + + // Update track_exercises + $sql = "UPDATE track_e_exercises SET orig_lp_item_id = $itemIid + WHERE c_id = $courseId AND orig_lp_id = $oldId AND orig_lp_item_id = $itemId"; + Database::query($sql); + var_dump($sql); + + // c_forum_thread + $sql = "UPDATE c_forum_thread SET lp_item_id = $itemIid + WHERE c_id = $courseId AND lp_item_id = $itemId"; + Database::query($sql); + var_dump($sql); + + // orig_lp_item_view_id + $sql = "SELECT * FROM c_lp_view + WHERE c_id = $courseId AND lp_id = $oldId AND id <> iid"; + error_log($sql); + + $viewList = Database::store_result(Database::query($sql),'ASSOC'); + if ($viewList) { + error_log("c_lp_view list: ".count( $viewList)); + foreach ($viewList as $view) { + $oldViewId = $view['id']; + $newViewId = $prefixViewId = $view['iid']; + $userId = $view['user_id']; + + if (empty($oldViewId)) { + continue; + } + + $view['iid'] = null; + + // Create new c_lp_view to avoid conflicts + $newViewId = Database::insert('c_lp_view', $view); + var_dump($newViewId); + if (empty($newViewId)) { + continue; + } + $sql = "UPDATE c_lp_view SET id = iid WHERE iid = $newViewId"; + Database::query($sql); + + // Delete old c_lp_view + $sql = "DELETE FROM c_lp_view WHERE id = $oldViewId AND iid = $prefixViewId "; + Database::query($sql); + + $sql = "UPDATE track_e_exercises + SET orig_lp_item_view_id = $newViewId + WHERE + c_id = $courseId AND + orig_lp_id = $oldId AND + orig_lp_item_id = $itemIid AND + orig_lp_item_view_id = $oldViewId AND + exe_user_id = $userId + "; + Database::query($sql); + var_dump($sql); + + $sql = "SELECT * FROM c_lp_item_view WHERE lp_view_id = $oldViewId AND c_id = $courseId "; + error_log($sql); + $list = Database::store_result(Database::query($sql),'ASSOC'); + + if (!empty($list)) { + foreach ($list as $itemView) { + $itemView['lp_view_id'] = $newViewId; + $itemView['iid'] = null; + //var_dump($itemView); + $itemViewId = Database::insert('c_lp_item_view', $itemView); + if ($itemViewId) { + $sql = "UPDATE c_lp_item_view SET id = iid WHERE iid = $itemViewId"; + var_dump($sql); + Database::query($sql); + } + } + + $sql = "DELETE FROM c_lp_item_view WHERE lp_view_id = $oldViewId AND c_id = $courseId"; + Database::query($sql); + var_dump($sql); + } + + /*$sql = "UPDATE c_lp_item_view + SET lp_view_id = $newViewId + WHERE + lp_view_id = $oldViewId AND + lp_item_id = $itemIid AND + c_id = $courseId + "; + Database::query($sql);*/ + + /*$sql = "UPDATE c_lp_view SET id = iid + WHERE id = $oldViewId "; + Database::query($sql);*/ + } + } + + $sql = "UPDATE $tblCLpItem SET lp_id = $lpIid + WHERE c_id = $courseId AND lp_id = $oldId AND id = $itemId"; + Database::query($sql); + var_dump($sql); + + $sql = "UPDATE $tblCLpItem SET id = iid + WHERE c_id = $courseId AND lp_id = $oldId AND id = $itemId"; + Database::query($sql); + var_dump($sql); + } + + $sql = "UPDATE c_lp_view SET lp_id = $lpIid WHERE c_id = $courseId AND lp_id = $oldId"; + Database::query($sql); + var_dump($sql); + + $sql = "UPDATE c_forum_forum SET lp_id = $lpIid WHERE c_id = $courseId AND lp_id = $oldId"; + Database::query($sql); + var_dump($sql); + + // Update track_exercises + $sql = "UPDATE track_e_exercises SET orig_lp_id = $lpIid + WHERE c_id = $courseId AND orig_lp_id = $oldId"; + Database::query($sql); + var_dump($sql); + + $sql = "UPDATE $tblCLp SET id = iid WHERE c_id = $courseId AND id = $oldId "; + Database::query($sql); + var_dump($sql); + } + } +} + +echo 'finished'; +error_log('finished'); diff --git a/tests/scripts/restore_deleted_documents.php b/tests/scripts/restore_deleted_documents.php new file mode 100644 index 0000000000..89ed92bf97 --- /dev/null +++ b/tests/scripts/restore_deleted_documents.php @@ -0,0 +1,85 @@ + + +$currentUserId = api_get_user_id(); +$course = api_get_course_entity($cId); +$courseDirectory = api_get_path(SYS_COURSE_PATH).$course->getDirectory().'/document'; + +$documents = $em + ->createQuery( + 'SELECT d FROM ChamiloCourseBundle:CDocument d + WHERE d.cId = :cid AND d.path LIKE :path AND d.sessionId = 0' + ) + ->setParameters(['cid' => (int) $cId, 'path' => $path]) + ->getResult(); + +header('Content-Type: text/plain'); + +/** @var CDocument $document */ +foreach ($documents as $document) { + $properties = $em + ->createQuery( + 'SELECT i FROM ChamiloCourseBundle:CItemProperty i + WHERE i.course = :course AND i.session IS NULL AND i.ref = :ref' + ) + ->setParameters(['course' => $course, 'ref' => $document->getIid()]) + ->getResult(); + + /** @var CItemProperty $property */ + foreach ($properties as $property) { + if (!in_array($property->getLasteditType(), ['DocumentDeleted', 'delete'])) { + continue; + } + switch ($property->getLasteditType()) { + case 'DocumentDeleted': + echo "Changing 'deleted' log to 'added' log".PHP_EOL; + $property + ->setLasteditType('DocumentAdded') + ->setLasteditUserId($currentUserId) + ->setLasteditDate( + api_get_utc_datetime(null, false, true) + ) + ->setVisibility(1); + + $em->persist($property); + break; + case 'delete': + echo "Removing delete log".PHP_EOL; + $em->remove($property); + break; + } + } + + $filePath = $courseDirectory.$document->getPath(); + $newPath = str_replace('_DELETED_'.$document->getIid(), '', $document->getPath()); + $newFilePath = $courseDirectory.$newPath; + + $document->setPath($newPath); + $em->flush(); + + $renaming = rename($filePath, $newFilePath); + + echo "Renaming $filePath to $newFilePath : "; + echo $renaming ? 'y' : 'n'; + echo PHP_EOL; +}