Internal: Fix queries, fix legacy code.

pull/3466/head
Julio Montoya 5 years ago
parent d7e8927039
commit 1a81860219
  1. 91
      public/main/forum/forumfunction.inc.php
  2. 20
      public/main/forum/iframe_thread.php
  3. 3
      public/main/forum/reply.php
  4. 5
      public/main/gradebook/lib/be/category.class.php
  5. 4
      public/main/inc/lib/groupmanager.lib.php
  6. 19
      public/main/lp/aicc.class.php
  7. 4
      public/main/lp/lp_subscribe_users.php

@ -960,8 +960,6 @@ function store_forum($values, $courseInfo = [], $returnId = false)
$forumId = $forum->getIid();
if ($forumId > 0) {
$sql = "UPDATE $table_forums SET forum_id = iid WHERE iid = $forumId";
Database::query($sql);
$courseCode = $courseInfo['code'];
$subscribe = (int) api_get_course_setting('subscribe_users_to_forum_notifications');
@ -1045,7 +1043,7 @@ function delete_post($post_id)
/** @var CForumPost $post */
$post = $em
->getRepository('ChamiloCourseBundle:CForumPost')
->findOneBy(['cId' => $course_id, 'postId' => $post_id]);
->findOneBy(['cId' => $course_id, 'iid' => $post_id]);
if ($post) {
$em
@ -1061,7 +1059,7 @@ function delete_post($post_id)
->execute([
'parent_of_deleted_post' => $post->getPostParentId(),
'course' => $course_id,
'post' => $post->getPostId(),
'post' => $post->getIid(),
'thread_of_deleted_post' => $post->getThread() ? $post->getThread()->getIid() : 0,
'forum_of_deleted_post' => $post->getForum(),
]);
@ -1084,9 +1082,9 @@ function delete_post($post_id)
$sql = "UPDATE $table_threads
SET
thread_replies = thread_replies - 1,
thread_last_post = ".(int) ($last_post_of_thread['post_id']).",
thread_last_post = ".(int) ($last_post_of_thread['iid']).",
thread_date='".Database::escape_string($last_post_of_thread['post_date'])."'
WHERE c_id = $course_id AND thread_id = ".(int) ($_GET['thread']);
WHERE c_id = $course_id AND iid = ".(int) ($_GET['thread']);
Database::query($sql);
return 'PostDeleted';
@ -1094,7 +1092,7 @@ function delete_post($post_id)
if (!$last_post_of_thread) {
// We deleted the very single post of the thread so we need to delete the entry in the thread table also.
$sql = "DELETE FROM $table_threads
WHERE c_id = $course_id AND thread_id = ".(int) ($_GET['thread']);
WHERE c_id = $course_id AND iid = ".(int) ($_GET['thread']);
Database::query($sql);
return 'PostDeletedSpecial';
@ -1124,9 +1122,9 @@ function check_if_last_post_of_thread($thread_id)
$result = Database::query($sql);
if (Database::num_rows($result) > 0) {
return Database::fetch_array($result);
} else {
return false;
}
return false;
}
/**
@ -1675,7 +1673,7 @@ function get_last_post_by_thread($course_id, $thread_id, $forum_id, $show_visibl
$sql .= ' AND visible = 1 ';
}
$sql .= ' ORDER BY post_id DESC LIMIT 1';
$sql .= ' ORDER BY iid DESC LIMIT 1';
$result = Database::query($sql);
if (Database::num_rows($result)) {
return Database::fetch_array($result, 'ASSOC');
@ -1719,7 +1717,7 @@ function get_last_post_information($forum_id, $show_invisibles = false, $course_
// First get the threads to make sure there is no inconsistency in the
// database between forum and thread
$sql = "SELECT thread_id FROM $table_threads
$sql = "SELECT iid as thread_id FROM $table_threads
WHERE
forum_id = $forum_id AND
c_id = $course_id AND
@ -1736,7 +1734,7 @@ function get_last_post_information($forum_id, $show_invisibles = false, $course_
$threadsList = implode(',', $threads);
// Now get the posts that are linked to these threads
$sql = "SELECT
post.post_id,
post.iid as post_id,
post.forum_id,
post.poster_id,
post.poster_name,
@ -1754,11 +1752,10 @@ function get_last_post_information($forum_id, $show_invisibles = false, $course_
$table_item_property thread_properties,
$table_item_property forum_properties
WHERE
post.forum_id = $forum_id
AND post.thread_id IN ($threadsList)
AND post.poster_id = users.user_id
AND post.thread_id = thread_properties.ref
AND thread_properties.tool='".TOOL_FORUM_THREAD."'
post.forum_id = $forum_id AND
post.thread_id IN ($threadsList) AND
post.poster_id = users.id AND
post.thread_id = thread_properties.ref AND thread_properties.tool='".TOOL_FORUM_THREAD."'
AND post.forum_id=forum_properties.ref
AND forum_properties.tool='".TOOL_FORUM."'
AND post.c_id = $course_id AND
@ -1920,11 +1917,12 @@ function getThreadInfo($threadId, $cId)
{
$repo = Database::getManager()->getRepository('ChamiloCourseBundle:CForumThread');
/** @var CForumThread $forumThread */
$forumThread = $repo->findOneBy(['threadId' => $threadId, 'cId' => $cId]);
$forumThread = $repo->findOneBy(['iid' => $threadId, 'cId' => $cId]);
$thread = [];
if ($forumThread) {
$thread['threadId'] = $forumThread->getThreadId();
$thread['iid'] = $forumThread->getIid();
$thread['threadId'] = $forumThread->getIid();
$thread['threadTitle'] = $forumThread->getThreadTitle();
$thread['forumId'] = $forumThread->getForum() ? $forumThread->getForum()->getIid() : 0;
$thread['sessionId'] = $forumThread->getSessionId();
@ -1999,7 +1997,7 @@ function getPosts(
$qb = $em->getRepository('ChamiloCourseBundle:CForumPost')->createQueryBuilder('p');
$qb->select('p')
->addCriteria($criteria)
->addOrderBy('p.postId', $orderDirection);
->addOrderBy('p.iid', $orderDirection);
if ($filterModerated && 1 == $forum->isModerated()) {
if (!api_is_allowed_to_edit(false, true)) {
@ -2023,7 +2021,7 @@ function getPosts(
$postInfo = [
'iid' => $post->getIid(),
'c_id' => $post->getCId(),
'post_id' => $post->getPostId(),
'post_id' => $post->getIid(),
'post_title' => $post->getPostTitle(),
'post_text' => $post->getPostText(),
'thread_id' => $post->getThread() ? $post->getThread()->getIid() : 0,
@ -2043,7 +2041,7 @@ function getPosts(
if (!empty($posterId)) {
$user = api_get_user_entity($posterId);
if ($user) {
$postInfo['user_id'] = $user->getUserId();
$postInfo['user_id'] = $user->getId();
$postInfo['username'] = $user->getUsername();
$postInfo['username_canonical'] = $user->getUsernameCanonical();
$postInfo['lastname'] = $user->getLastname();
@ -2064,7 +2062,7 @@ function getPosts(
$threadId,
$orderDirection,
$recursive,
$post->getPostId(),
$post->getIid(),
$depth
)
);
@ -2368,7 +2366,7 @@ function updateThread($values)
'thread_title' => $values['thread_title'],
'thread_sticky' => isset($values['thread_sticky']) ? $values['thread_sticky'] : 0,
];
$where = ['c_id = ? AND thread_id = ?' => [$courseId, $values['thread_id']]];
$where = ['c_id = ? AND iid = ?' => [$courseId, $values['thread_id']]];
Database::update($threadTable, $params, $where);
$id = $values['thread_id'];
@ -2382,8 +2380,7 @@ function updateThread($values)
$gradebookLink = null;
$em = Database::getManager();
if (!empty($linkInfo) && isset($linkInfo['id'])) {
$linkId = $linkInfo['id'];
$gradebookLink = $em->getRepository('ChamiloCoreBundle:GradebookLink')->find($linkId);
$gradebookLink = $em->getRepository('ChamiloCoreBundle:GradebookLink')->find($linkInfo['id']);
}
// values 1 or 0
@ -2430,7 +2427,7 @@ function updateThread($values)
'thread_weight' => 0,
'thread_peer_qualify' => 0,
];
$where = ['c_id = ? AND thread_id = ?' => [$courseId, $values['thread_id']]];
$where = ['c_id = ? AND iid = ?' => [$courseId, $values['thread_id']]];
Database::update($threadTable, $params, $where);
if (!empty($linkInfo)) {
@ -2540,10 +2537,6 @@ function store_thread(
);
}
$thread->setThreadId($thread->getIid());
$em->persist($thread);
$em->flush();
/*api_item_property_update(
$courseInfo,
TOOL_FORUM_THREAD,
@ -2641,19 +2634,13 @@ function store_thread(
];
Event::registerLog($logInfo);
if ($postId) {
$post->setPostId($postId);
$em->persist($post);
$em->flush();
}
// Now we have to update the thread table to fill the thread_last_post
// field (so that we know when the thread has been updated for the last time).
$sql = "UPDATE $table_threads
SET thread_last_post = '".$postId."'
WHERE
c_id = $course_id AND
thread_id='".$thread->getIid()."'";
iid = '".$thread->getIid()."'";
Database::query($sql);
$message = get_lang('The new thread has been added');
@ -3300,11 +3287,6 @@ function saveThreadScore(
VALUES (".$course_id.", '".$user_id."','".$thread_id."',".(float) $thread_qualify.", '".$currentUserId."','".$qualify_time."','".$session_id."')";
Database::query($sql);
$insertId = Database::insert_id();
if ($insertId) {
$sql = "UPDATE $table_threads_qualify SET id = iid
WHERE iid = $insertId";
Database::query($sql);
}
return 'insert';
} else {
@ -3485,13 +3467,6 @@ function saveThreadScoreHistory(
$sql = "INSERT INTO $table_threads_qualify_log (c_id, user_id, thread_id, qualify, qualify_user_id,qualify_time,session_id)
VALUES(".$course_id.", '".$user_id."','".$thread_id."',".(float) $row[0].", '".$qualify_user_id."','".$row[1]."','')";
Database::query($sql);
$insertId = Database::insert_id();
if ($insertId) {
$sql = "UPDATE $table_threads_qualify_log SET id = iid
WHERE iid = $insertId";
Database::query($sql);
}
}
}
@ -4033,7 +4008,7 @@ function increase_thread_view($thread_id)
SET thread_views = thread_views + 1
WHERE
c_id = $course_id AND
thread_id = '".(int) $thread_id."'";
iid = '".(int) $thread_id."'";
Database::query($sql);
}
@ -4058,7 +4033,7 @@ function updateThreadInfo($threadId, $lastPostId, $post_date)
thread_date = '".Database::escape_string($post_date)."'
WHERE
c_id = $course_id AND
thread_id='".Database::escape_string($threadId)."'"; // this needs to be cleaned first
iid ='".Database::escape_string($threadId)."'"; // this needs to be cleaned first
Database::query($sql);
}
@ -4110,7 +4085,7 @@ function get_whats_new()
post_date > '".Database::escape_string($lastForumAccess)."'";
$result = Database::query($sql);
while ($row = Database::fetch_array($result)) {
$postInfo[$row['forum_id']][$row['thread_id']][$row['post_id']] = $row['post_date'];
$postInfo[$row['forum_id']][$row['thread_id']][$row['iid']] = $row['post_date'];
}
Session::write('whatsnew_post_info', $postInfo);
}
@ -4587,11 +4562,11 @@ function store_move_post($values)
// Resetting the last post of the old thread and decreasing the number of replies and the thread.
$sql = "SELECT * FROM $table_posts
WHERE c_id = $course_id AND thread_id='".$threadId."'
ORDER BY post_id DESC";
ORDER BY iid DESC";
$result = Database::query($sql);
$row = Database::fetch_array($result);
$sql = "UPDATE $table_threads SET
thread_last_post='".$row['post_id']."',
thread_last_post='".$row['iid']."',
thread_replies=thread_replies-1
WHERE
c_id = $course_id AND
@ -5523,7 +5498,7 @@ function get_notifications($content, $id)
$id = (int) $id;
$sql = "SELECT user.id as user_id, user.firstname, user.lastname, user.email, user.user_id user
$sql = "SELECT user.id as user_id, user.firstname, user.lastname, user.email, user.id user
FROM $table_users user, $table_notification notification
WHERE
notification.c_id = $course_id AND user.active = 1 AND
@ -6288,12 +6263,12 @@ function getAttachmentIdsByPostId($postId, $courseId = 0)
}
if ($courseId > 0) {
$forumAttachmentTable = Database::get_course_table(TABLE_FORUM_ATTACHMENT);
$sql = "SELECT id FROM $forumAttachmentTable
$sql = "SELECT iid FROM $forumAttachmentTable
WHERE c_id = $courseId AND post_id = $postId";
$result = Database::query($sql);
if (false !== $result && Database::num_rows($result) > 0) {
while ($row = Database::fetch_array($result, 'ASSOC')) {
$array[] = $row['id'];
$array[] = $row['iid'];
}
}
}

@ -56,7 +56,9 @@ $sessionEntity = api_get_session_entity(api_get_session_id());
// if the user is not a course administrator and the forum is hidden
// then the user is not allowed here.
if (!api_is_allowed_to_edit(false, true) &&
(false == $forumEntity->isVisible($courseEntity, $sessionEntity) || false == $threadEntity->isVisible($courseEntity, $sessionEntity))
( false == $forumEntity->isVisible($courseEntity, $sessionEntity) ||
false == $threadEntity->isVisible($courseEntity, $sessionEntity)
)
) {
api_not_allowed(false);
}
@ -66,18 +68,14 @@ $course_id = api_get_course_int_id();
$table_posts = Database::get_course_table(TABLE_FORUM_POST);
$table_users = Database::get_main_table(TABLE_MAIN_USER);
/* Display Forum Category and the Forum information */
// We are getting all the information about the current forum and forum category.
// Note pcool: I tried to use only one sql statement (and function) for this,
// but the problem is that the visibility of the forum AND forum cateogory are stored in the item_property table.
$sql = "SELECT * FROM $table_posts posts
INNER JOIN $table_users users
ON (posts.poster_id = users.user_id)
$sql = "SELECT username, firstname, lastname, u.id, post_date, post_title, post_text
FROM $table_posts posts
INNER JOIN $table_users u
ON (posts.poster_id = u.id)
WHERE
posts.c_id = $course_id AND
posts.thread_id='".$threadEntity->getIid()."'
ORDER BY posts.post_id ASC";
ORDER BY posts.iid ASC";
$result = Database::query($sql);
$template = new Template('', false, false);
@ -87,7 +85,7 @@ while ($row = Database::fetch_array($result)) {
$content .= '<tr>';
$content .= '<td rowspan="2" class="forum_message_left">';
$username = api_htmlentities(sprintf(get_lang('Login: %s'), $row['username']), ENT_QUOTES);
if ('0' == $row['user_id']) {
if ('0' == $row['id']) {
$name = $row['poster_name'];
} else {
$name = api_get_person_name($row['firstname'], $row['lastname']);

@ -71,7 +71,8 @@ if (!api_is_allowed_to_edit(false, true) &&
) {
api_not_allowed(true);
}
if (!$_user['user_id'] && 0 == $forum->getAllowAnonymous()) {
if (!$_user['user_id'] &&
0 == $forum->getAllowAnonymous()) {
api_not_allowed(true);
}

@ -545,8 +545,9 @@ class Category implements GradebookItem
$category->set_id($gradebookCategory->getId());
$category->set_name($gradebookCategory->getName());
$category->set_description($gradebookCategory->getDescription());
$category->set_user_id($gradebookCategory->getUserId());
$category->set_course_code($gradebookCategory->getCourseCode());
$category->set_user_id($gradebookCategory->getId());
//$category->set_course_code($gradebookCategory->getCourseCode());
$category->setCourseId($gradebookCategory->getCourse()->getId());
$category->set_parent_id($gradebookCategory->getParentId());
$category->set_weight($gradebookCategory->getWeight());
$category->set_visible($gradebookCategory->getVisible());

@ -1004,10 +1004,6 @@ class GroupManager
$categoryId = Database::insert($table, $params);
if ($categoryId) {
$sql = "UPDATE $table SET id = iid
WHERE iid = $categoryId";
Database::query($sql);
return $categoryId;
}

@ -276,11 +276,8 @@ class aicc extends learnpath
$lp_id = Database::insert_id();
if ($lp_id) {
$sql = "UPDATE $new_lp SET id = iid WHERE iid = $lp_id";
Database::query($sql);
$this->lp_id = $lp_id;
/*
api_item_property_update(
$courseInfo,
TOOL_LEARNPATH,
@ -295,7 +292,7 @@ class aicc extends learnpath
$this->lp_id,
'visible',
api_get_user_id()
);
);*/
}
$previous = 0;
@ -330,12 +327,6 @@ class aicc extends learnpath
error_log('New LP - In aicc::import_aicc() - inserting item : '.$sql_item.' : ', 0);
}
$item_id = Database::insert_id();
if ($item_id) {
$sql = "UPDATE $new_lp_item SET id = iid WHERE iid = $lp_id";
Database::query($sql);
}
// Now update previous item to change next_item_id.
if (0 != $previous) {
$upd = "UPDATE $new_lp_item SET next_item_id = $item_id WHERE c_id = $course_id AND id = $previous";
@ -647,7 +638,7 @@ class aicc extends learnpath
$lp = $this->get_id();
if (0 != $lp) {
$tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
$sql = "UPDATE $tbl_lp SET preview_image = '$preview_image'
$sql = "UPDATE $tbl_lp SET preview_image = '$preview_image'
WHERE c_id = ".$course_id." id = ".$lp;
Database::query($sql);
@ -670,7 +661,7 @@ class aicc extends learnpath
$lp = $this->get_id();
if (0 != $lp) {
$tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
$sql = "UPDATE $tbl_lp SET author = '$author'
$sql = "UPDATE $tbl_lp SET author = '$author'
WHERE c_id = ".$course_id." id = ".$lp;
Database::query($sql);
@ -696,7 +687,7 @@ class aicc extends learnpath
$lp = $this->get_id();
if (0 != $lp) {
$tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
$sql = "UPDATE $tbl_lp SET content_maker = '$maker'
$sql = "UPDATE $tbl_lp SET content_maker = '$maker'
WHERE c_id = ".$course_id." id = ".$lp;
Database::query($sql);

@ -69,7 +69,7 @@ if (!$session) {
if ($list) {
/** @var SessionRelCourseRelUser $sessionCourseUser */
foreach ($list as $sessionCourseUser) {
$subscribedUsers[$sessionCourseUser->getUser()->getUserId()] = $sessionCourseUser->getUser();
$subscribedUsers[$sessionCourseUser->getUser()->getId()] = $sessionCourseUser->getUser();
}
}
}
@ -78,7 +78,7 @@ if (!$session) {
$choices = [];
/** @var User $user */
foreach ($subscribedUsers as $user) {
$choices[$user->getUserId()] = $user->getCompleteNameWithClasses();
$choices[$user->getId()] = $user->getCompleteNameWithClasses();
}
// Getting subscribed users to a LP.

Loading…
Cancel
Save