Update add missing php files from "tests" folder

pull/2944/head
Julio Montoya 6 years ago
parent 151e691390
commit edf8243f5d
  1. 134
      tests/scripts/check_extra_fields.php
  2. 67
      tests/scripts/delete_deleted_exercises_with_questions.php
  3. 61
      tests/scripts/delete_items_from_csv.php
  4. 26
      tests/scripts/delete_orphan_questions.php
  5. 36
      tests/scripts/edit_course_html_files.php
  6. 134
      tests/scripts/fix_documents_path.php
  7. 40
      tests/scripts/fix_lp_calendar.php
  8. 276
      tests/scripts/fix_lp_id_to_iid_v2.php
  9. 85
      tests/scripts/restore_deleted_documents.php

@ -0,0 +1,134 @@
<?php
/* For licensing terms, see /license.txt */
/**
* This script checks if the default extra fields are present in the platform.
* If a default extra field doesn't exists then it will created.
* Extra field list as in 1.11.8
*/
exit;
require_once __DIR__.'/../../main/inc/global.inc.php';
$em = Database::getManager();
$repo = $em->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;
}

@ -0,0 +1,67 @@
<?php
/* For licensing terms, see /license.txt */
exit;
require_once __DIR__ . '/../../main/inc/global.inc.php';
$sql = 'SELECT iid, c_id, title
FROM c_quiz
WHERE active = -1
ORDER BY iid';
$result = Database::query($sql);
$data = Database::store_result($result);
$counter = 1;
$total = count($data);
echo 'Exercises to delete: '.$total.PHP_EOL;
foreach ($data as $row) {
$id = $row['iid'];
$courseId = $row['c_id'];
//$exercise = new Exercise($courseId);
//$exercise->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);
}

@ -0,0 +1,61 @@
<?php
/* For licensing terms, see /license.txt */
exit;
require_once __DIR__ . '/../../main/inc/global.inc.php';
$file = 'delete.csv';
if (!file_exists($file)) {
echo "File $file doesn't exists";
exit;
}
$data = Import::csv_reader($file);
echo 'Number of lines '.count($data).PHP_EOL;
$counter = 1;
foreach ($data as $row) {
if (isset($row['SessionID'])) {
$sessionId = SessionManager::getSessionIdFromOriginalId($row['SessionID'], 'external_session_id');
if (!empty($sessionId)) {
$sessionInfo = api_get_session_info($sessionId);
if (!empty($sessionInfo)) {
$sessionId = $sessionInfo['id'];
$sessionName = $sessionInfo['name'];
echo "Line: $counter. Session will be deleted: $sessionName #$sessionId ".PHP_EOL;
//SessionManager::delete($sessionId, true);
} else {
echo "Line: $counter. Session not found: $sessionName".PHP_EOL;
}
}
}
// Course
$courseId = isset($row['CourseID']) ? $row['CourseID'] : '';
if (!empty($courseId)) {
$courseInfo = CourseManager::getCourseInfoFromOriginalId($courseId, 'external_course_id');
if (!empty($courseInfo) && isset($courseInfo['id'])) {
$courseCode = $courseInfo['code'];
$courseId = $courseInfo['id'];
CourseManager::delete_course($courseCode);
echo "Line: $counter. Course will be deleted: $courseCode #$courseId".PHP_EOL;
} else {
echo "Line: $counter. Course not found: $courseCode".PHP_EOL;
}
}
// User
$userName = isset($row['UserName']) ? $row['UserName'] : '';
if (!empty($userName)) {
$userInfo = api_get_user_info_from_username($userName);
if (!empty($userInfo) && isset($userInfo['id'])) {
$userId = $userInfo['id'];
//UserManager::delete_user($userId);
echo "Line: $counter. User will be deleted: $userId".PHP_EOL;
} else {
echo "Line: $counter. User not found: $userName".PHP_EOL;
}
}
$counter++;
}

@ -0,0 +1,26 @@
<?php
/* For licensing terms, see /license.txt */
exit;
require_once __DIR__ . '/../../main/inc/global.inc.php';
$sql = 'SELECT iid, c_id, question
FROM c_quiz_question
WHERE iid not in (SELECT question_id from c_quiz_rel_question)
ORDER BY iid';
$result = Database::query($sql);
$data = Database::store_result($result);
$counter = 1;
$totalQuestions = count($data);
echo 'Questions to delete: '.$totalQuestions.PHP_EOL;
foreach ($data as $row) {
$courseInfo = api_get_course_info_by_id($row['c_id']);
$question = Question::read($row['iid'], $courseInfo);
if (empty($question->exerciseList)) {
$question->delete(1);
}
echo 'Deleting question '.$counter.'/'.$totalQuestions.' - #'.$row['iid'].PHP_EOL;
$counter++;
}

@ -0,0 +1,36 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Goes through all HTML files of the courses directory and replaces
* the first string by the second string.
* This is useful when a portal was installed under one URL and then
* changed URL (or port), to ensure documents are not pointing to the
* previous URL.
* This script is designed to be run from the browser, so maybe you
* need to move it to an executable folder and change the first require.
* @author Yannick Warnier <yannick.warnier@beeznest.com>
*/
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' {} +<br />");
$i++;
//if ($i == 2) {
// exit;
//}
echo "Replaced all $search in ".$dir.$courseDir."<br />";
}
echo "Done";

@ -0,0 +1,134 @@
<?php
/* For licensing terms, see /license.txt */
/**
* This script will find paths with:
*
* https://example.fr/../../../../../../../LOLOS/CHAMILO/document/Chamilo/Fonctionalite_de_groupe.html
*
* And will be transformed to:
*
* /NEWFOLDER/courses/CHAMILO/document/Chamilo/Fonctionalite_de_groupe.html
*
* You need to choose the /NEWFOLDER in the $newUrlAppend variable
*
* The script will edit HTML documents inside every document course folder and also edit
* exercises (question, answer) and course description in the database.
*
*/
exit;
require_once __DIR__.'/../../main/inc/global.inc.php';
/*$test = '
https://example.fr/../../../../../../../LOLOS/CHAMILO/document/Chamilo/Fonctionalite_de_groupe.html
http://example.fr/../../../../../../../LOLOS/CHAMILO/document/Chamilo/Fonctionalite_de_groupe.html
';*/
$courses = CourseManager::get_courses_list();
// New values
$newUrlAppend = '/NEWFOLDER'; // Url append of new portal
$slashes = '/../../../../../../../';
$pathToSearch = '#http(.*)://(.*)'.$slashes.'([^/]+)/(.*)#';
$courseSysPath = api_get_path(SYS_COURSE_PATH);
foreach ($courses as $course) {
$courseId = $course['real_id'];
$docsPath = $courseSysPath.$course['directory'].'/document/';
echo "<h4>Course in: $docsPath</h4>".'<br />';
if (is_dir($docsPath)) {
$finder = new \Symfony\Component\Finder\Finder();
$finder->files()->in($docsPath)->name('*.html');
foreach ($finder as $file) {
echo $file->getRealPath().'<br />';
$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 "<h4>Path doesn't exist</h4>".'<br />';
}
}

@ -0,0 +1,40 @@
<?php
/* For licensing terms, see /license.txt */
require __DIR__ . '/../../main/inc/global.inc.php';
$extraField = new ExtraField('lp_item');
$extraFieldInfo = $extraField->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;

@ -0,0 +1,276 @@
<?php
/* For licensing terms, see /license.txt */
/**
* This script fixes use of id instead of iid for the learning path
*/
exit;
require_once '../../main/inc/global.inc.php';
/** @var int $courseId */
$onlyCourseId = 0;
/** @var int $lpId lp id */
$lpId = 0;
$courses = Database::select('id, title, code', Database::get_main_table(TABLE_MAIN_COURSE));
$tblCLp = Database::get_course_table(TABLE_LP_MAIN);
$tblCLpItem = Database::get_course_table(TABLE_LP_ITEM);
$toolTable = Database::get_course_table(TABLE_TOOL_LIST);
// Start custom changes
// Delete inconsistencies from old base
$sql = 'DELETE FROM c_lp_item_view WHERE c_id = 0';
Database::query($sql);
var_dump($sql);
error_log($sql);
// This is a custom change, probably you don't needed it in your script (removing an empty attempt)
$sql = 'DELETE FROM c_lp_item_view WHERE lp_view_id = 18 and c_id = 4';
Database::query($sql);
var_dump($sql);
error_log($sql);
$sql = 'DELETE FROM c_lp_view where id = 18 and c_id = 4';
Database::query($sql);
var_dump($sql);
error_log($sql);
///update c_lp_item_view set status = 'not attempted', suspend_data = null where iid = 2148;
// end custom changes
$sessions = Database::select('id', Database::get_main_table(TABLE_MAIN_SESSION));
if (!empty($sessions)) {
$sessions = array_column($sessions, 'id');
// Add session_id = 0
$sessions[] = 0;
} else {
$sessions = [0];
}
foreach ($courses as $course) {
if (!empty($onlyCourseId)) {
if ($onlyCourseId != $course['id']) {
continue;
}
}
$courseId = $course['id'];
$sql = "SELECT * FROM $tblCLp WHERE c_id = $courseId ORDER by iid";
echo 'Select all lps';
var_dump($sql);
error_log($sql);
$result = Database::query($sql);
$myOnlyLpList = [];
if (Database::num_rows($result)) {
while ($lpInfo = Database::fetch_array($result, 'ASSOC')) {
$lpIid = $lpInfo['iid'];
$oldId = $lpInfo['id'];
$sql = "SELECT * FROM $tblCLpItem
WHERE c_id = $courseId AND lp_id = $oldId ORDER by iid";
//echo "<h3>$sql</h3>";
//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');

@ -0,0 +1,85 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Script to restore some deleted documents
*/
use Chamilo\CourseBundle\Entity\CDocument;
use Chamilo\CourseBundle\Entity\CItemProperty;
exit;
require __DIR__.'/../../main/inc/global.inc.php';
api_protect_admin_script();
$em = Database::getManager();
$cId = 0;
$path = '%_DELETED_%';
//// --->
$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;
}
Loading…
Cancel
Save