Skip to content

Commit

Permalink
Fix mysql error see BT#8329
Browse files Browse the repository at this point in the history
  • Loading branch information
jmontoyaa committed Aug 22, 2016
1 parent eea03df commit 4b0c319
Showing 1 changed file with 54 additions and 56 deletions.
110 changes: 54 additions & 56 deletions src/Chamilo/CourseBundle/Component/CourseCopy/CourseRestorer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Chamilo\CourseBundle\Component\CourseCopy;

use Chamilo\CourseBundle\Component\CourseCopy\Resources\GradeBookBackup;
use Chamilo\CourseBundle\Entity\CQuizAnswer;
use DocumentManager;
use Database;
Expand All @@ -22,16 +23,16 @@
*/
class CourseRestorer
{
/**
* The course-object
*/
/**
* The course-object
*/
public $course;
public $destination_course_info;

/**
* What to do with files with same name (FILE_SKIP, FILE_RENAME or
* FILE_OVERWRITE)
*/
/**
* What to do with files with same name (FILE_SKIP, FILE_RENAME or
* FILE_OVERWRITE)
*/
public $file_option;
public $set_tools_invisible_by_default;
public $skip_content;
Expand Down Expand Up @@ -74,9 +75,9 @@ class CourseRestorer
public function __construct($course)
{
$this->course = $course;
$course_info = api_get_course_info($this->course->code);
if (!empty($course_info)) {
$this->course_origin_id = $course_info['real_id'];
$courseInfo = api_get_course_info($this->course->code);
if (!empty($courseInfo)) {
$this->course_origin_id = $courseInfo['real_id'];
} else {
$this->course_origin_id = null;
}
Expand Down Expand Up @@ -168,7 +169,7 @@ public function restore(
unset($sample_text[$key]);
}
}
$sample_text = join("\n", $sample_text);
$sample_text = implode("\n", $sample_text);
$this->course->encoding = api_detect_encoding($sample_text, $course_info['language']);
}

Expand Down Expand Up @@ -215,10 +216,7 @@ public function restore(
$property['tool']
);
$property['insert_user_id'] = $this->checkUserId($property['insert_user_id']);

$params['insert_user_id'] = self::DBUTF8(
$property['insert_user_id']
);
$params['insert_user_id'] = (int) $property['insert_user_id'];
$params['insert_date'] = self::DBUTF8(
$property['insert_date']
);
Expand All @@ -229,9 +227,8 @@ public function restore(
$params['lastedit_type'] = self::DBUTF8(
$property['lastedit_type']
);
$params['lastedit_user_id'] = self::DBUTF8(
$property['lastedit_user_id']
);
$params['lastedit_user_id'] = (int) $property['lastedit_user_id'];

$params['visibility'] = self::DBUTF8(
$property['visibility']
);
Expand All @@ -243,10 +240,7 @@ public function restore(
);

$property['to_user_id'] = $this->checkUserId($property['to_user_id'], true);

$params['to_user_id'] = self::DBUTF8(
$property['to_user_id']
);
$params['to_user_id'] = (int) $property['to_user_id'];
//$params['to_group_id'] = 'NULL';

$id = Database::insert($table, $params);
Expand Down Expand Up @@ -281,7 +275,7 @@ public function restore_course_settings($destination_course_code)
$params['category_code'] = $course_info['categoryCode'];
$params['subscribe'] = $course_info['subscribe_allowed'];
$params['unsubscribe'] = $course_info['unsubscribe'];
CourseManager::update_attributes($origin_course_info['real_id'], $params);
CourseManager::update_attributes($origin_course_info['real_id'], $params);
}

/**
Expand Down Expand Up @@ -489,7 +483,7 @@ public function restore_documents($session_id = 0, $respect_base_content = false
'title' => self::DBUTF8($document->title),
'filetype' => self::DBUTF8($document->file_type),
'size' => self::DBUTF8($document->size),
'session_id' => $my_session_id,
'session_id' => $my_session_id
];

Database::update(
Expand Down Expand Up @@ -533,15 +527,17 @@ public function restore_documents($session_id = 0, $respect_base_content = false

if (in_array($file_info['extension'], array('html', 'htm'))) {
$content = file_get_contents($path.$document->path);
if (UTF8_CONVERT) $content = utf8_encode($content);
if (UTF8_CONVERT) {
$content = utf8_encode($content);
}
$content = DocumentManager::replace_urls_inside_content_html_from_copy_course(
$content,
$this->course->code,
$this->course->destination_path,
$this->course->backup_path,
$this->course->info['path']
);
file_put_contents($path.$document->path,$content);
file_put_contents($path.$document->path, $content);
}

$params = [
Expand All @@ -555,8 +551,8 @@ public function restore_documents($session_id = 0, $respect_base_content = false
[
'c_id = ? AND id = ?' => [
$this->destination_course_id,
$document_id,
],
$document_id
]
]
);
}
Expand Down Expand Up @@ -591,8 +587,7 @@ public function restore_documents($session_id = 0, $respect_base_content = false
}

if (!empty($session_id)) {

$document_path = explode('/',$document->path,3);
$document_path = explode('/', $document->path, 3);
$course_path = $path;
$orig_base_folder = $document_path[1];
$orig_base_path = $course_path.$document_path[0].'/'.$document_path[1];
Expand All @@ -610,7 +605,8 @@ public function restore_documents($session_id = 0, $respect_base_content = false

$folder_exists = file_exists($new_base_path);
if ($folder_exists) {
$_SESSION['orig_base_foldername'] = $new_base_foldername; // e.g: carpeta1 in session
// e.g: carpeta1 in session
$_SESSION['orig_base_foldername'] = $new_base_foldername;
$x = '';
while ($folder_exists) {
$x = $x + 1;
Expand Down Expand Up @@ -849,8 +845,8 @@ public function restore_documents($session_id = 0, $respect_base_content = false
//Replace old course code with the new destination code see BT#1985
if (file_exists($path.$document->path)) {
$file_info = pathinfo($path.$document->path);
if (in_array($file_info['extension'], array('html','htm'))) {
$content = file_get_contents($path.$document->path);
if (isset($file_info['extension']) && in_array($file_info['extension'], array('html','htm'))) {
$content = file_get_contents($path.$document->path);
if (UTF8_CONVERT) {
$content = utf8_encode($content);
}
Expand Down Expand Up @@ -1119,6 +1115,7 @@ public function restore_forum_category($my_id = null, $sessionId = 0)

$this->course->resources[RESOURCE_FORUMCATEGORY][$id]->destination_id = $new_id;
if (!empty($my_id)) {

return $new_id;
}
}
Expand Down Expand Up @@ -1199,6 +1196,7 @@ public function restore_post($id, $topic_id, $forum_id, $sessionId = 0)
$params['post_date'] = api_get_utc_datetime();
$params['post_id'] = 0;
unset($params['iid']);

$params['post_text'] = DocumentManager::replace_urls_inside_content_html_from_copy_course(
$params['post_text'],
$this->course->code,
Expand Down Expand Up @@ -1306,7 +1304,7 @@ public function restore_link_category($id, $session_id = 0)
$link_cat = $resources[RESOURCE_LINKCATEGORY][$id];
if (is_object($link_cat) && !$link_cat->is_restored()) {
$sql = "SELECT MAX(display_order) FROM $link_cat_table
WHERE c_id = ".$this->destination_course_id." ";
WHERE c_id = ".$this->destination_course_id;
$result=Database::query($sql);
list($orderMax)=Database::fetch_array($result,'NUM');
$display_order=$orderMax+1;
Expand Down Expand Up @@ -1340,7 +1338,7 @@ public function restore_tool_intro($sessionId = 0)
$tool_intro_table = Database :: get_course_table(TABLE_TOOL_INTRO);
$resources = $this->course->resources;
foreach ($resources[RESOURCE_TOOL_INTRO] as $id => $tool_intro) {
$sql = "DELETE FROM ".$tool_intro_table."
$sql = "DELETE FROM $tool_intro_table
WHERE
c_id = ".$this->destination_course_id." AND
id='".self::DBUTF8escapestring($tool_intro->id)."'";
Expand Down Expand Up @@ -1404,7 +1402,7 @@ public function restore_events($sessionId = 0)
'all_day' => $event->all_day,
'start_date' => $event->start_date,
'end_date' => $event->end_date,
'session_id' => $sessionId,
'session_id' => $sessionId
];
$new_event_id = Database::insert($table, $params);

Expand Down Expand Up @@ -1446,9 +1444,9 @@ public function restore_events($sessionId = 0)
'c_id' => $this->destination_course_id,
'path' => self::DBUTF8($new_filename),
'comment' => self::DBUTF8($attachment_event->comment),
'size' => $attachment_event->size,
'filename' => $attachment_event->filename,
'agenda_id' => $new_event_id,
'size' => isset($attachment_event->size) ? $attachment_event->size : '',
'filename' => isset($attachment_event->filename) ? $attachment_event->filename : '',
'agenda_id' => $new_event_id
];
$id = Database::insert($table_attachment, $params);
if ($id) {
Expand All @@ -1472,9 +1470,9 @@ public function restore_events($sessionId = 0)
'c_id' => $this->destination_course_id,
'path' => self::DBUTF8($new_filename),
'comment' => self::DBUTF8($event->attachment_comment),
'size' => $event->size,
'filename' => $event->filename,
'agenda_id' => $new_event_id,
'size' => isset($event->size) ? $event->size : '',
'filename' => isset($event->filename) ? $event->filename : '',
'agenda_id' => $new_event_id
];
$id = Database::insert($table_attachment, $params);

Expand Down Expand Up @@ -1686,7 +1684,7 @@ public function restore_quizzes(
if (!empty($quiz->sound)) {
if (isset($this->course->resources[RESOURCE_DOCUMENT][$quiz->sound]) &&
$this->course->resources[RESOURCE_DOCUMENT][$quiz->sound]->is_restored()) {
$sql = "SELECT path FROM " . $table_doc . "
$sql = "SELECT path FROM $table_doc
WHERE
c_id = " . $this->destination_course_id . " AND
id = " . $resources[RESOURCE_DOCUMENT][$quiz->sound]->destination_id;
Expand Down Expand Up @@ -1776,8 +1774,8 @@ public function restore_quizzes(
$question_order = $quiz->question_orders[$index] ? $quiz->question_orders[$index] : ++$order;
$sql = "INSERT IGNORE INTO $table_rel SET
c_id = " . $this->destination_course_id . ",
question_id = " . $qid . ",
exercice_id = " . $new_id . ",
question_id = $qid ,
exercice_id = $new_id ,
question_order = " . $question_order;
Database::query($sql);
}
Expand Down Expand Up @@ -1824,7 +1822,7 @@ public function restore_quiz_question($id)
'type' => self::DBUTF8($question->quiz_type),
'picture' => self::DBUTF8($question->picture),
'level' => self::DBUTF8($question->level),
'extra' => self::DBUTF8($question->extra),
'extra' => self::DBUTF8($question->extra)
];

$new_id = Database::insert($table_que, $params);
Expand Down Expand Up @@ -1867,7 +1865,6 @@ public function restore_quiz_question($id)
}

foreach ($temp as $index => $answer) {
//id = '".$index."',
$quizAnswer = new CQuizAnswer();
$quizAnswer
->setCId($this->destination_course_id)
Expand Down Expand Up @@ -1991,8 +1988,8 @@ public function restore_quiz_question($id)
'iid = ? AND c_id = ? AND question_id = ? ' => array(
$answer_item['iid'],
$this->destination_course_id,
$new_id,
),
$new_id
)
),
false
);
Expand Down Expand Up @@ -2027,8 +2024,8 @@ public function restore_quiz_question($id)
'id = ? AND c_id = ? AND question_id = ? ' => array(
$answer_id,
$this->destination_course_id,
$new_id,
),
$new_id
)
),
false
);
Expand Down Expand Up @@ -2191,7 +2188,7 @@ public function restore_surveys($sessionId = 0)
'answered' => '0',
'invite_mail' => self::DBUTF8($survey->invite_mail),
'reminder_mail' => self::DBUTF8($survey->reminder_mail),
'session_id' => $sessionId,
'session_id' => $sessionId
];

//An existing survey exists with the same code and the same language
Expand All @@ -2218,10 +2215,10 @@ public function restore_surveys($sessionId = 0)
$this->course->resources[RESOURCE_SURVEY][$id]->destination_id = $new_id;
foreach ($survey->question_ids as $index => $question_id) {
$qid = $this->restore_survey_question($question_id, $new_id);
$sql = "UPDATE ".$table_que." SET survey_id = ".$new_id."
$sql = "UPDATE $table_que SET survey_id = $new_id
WHERE c_id = ".$this->destination_course_id." AND question_id = $qid";
Database::query($sql);
$sql = "UPDATE ".$table_ans." SET survey_id = ".$new_id."
$sql = "UPDATE $table_ans SET survey_id = $new_id
WHERE c_id = ".$this->destination_course_id." AND question_id = $qid";
Database::query($sql);
}
Expand Down Expand Up @@ -2306,7 +2303,7 @@ public function is_survey_code_available($survey_code)
$sql = "SELECT * FROM $table_sur
WHERE
c_id = ".$this->destination_course_id." AND
code='".self::DBUTF8escapestring($survey_code)."'";
code = '".self::DBUTF8escapestring($survey_code)."'";
$result = Database::query($sql);
if (Database::num_rows($result) > 0) {
return false;
Expand Down Expand Up @@ -2823,7 +2820,7 @@ public function allow_create_all_directory($source, $dest, $overwrite = false)
public function get_new_id($tool, $ref)
{
// Check if the value exist in the current array.
if ($tool == 'hotpotatoes') {
if ($tool === 'hotpotatoes') {
$tool = 'document';
}

Expand Down Expand Up @@ -3323,6 +3320,7 @@ private function checkUserId($userId, $returnNull = false) {
if (!empty($userId)) {
$userInfo = api_get_user_info($userId);
if (empty($userInfo)) {

return api_get_user_id();
}
}
Expand Down

0 comments on commit 4b0c319

Please sign in to comment.