Fix save forum threads when the forum is moderated - refs BT#11356

pull/2487/head
Angel Fernando Quiroz Campos 9 years ago
parent e3bbb08d9c
commit 8625503f7b
  1. 148
      main/forum/forumfunction.inc.php
  2. 12
      src/Chamilo/CourseBundle/Entity/CForumPost.php
  3. 17
      src/Chamilo/CourseBundle/Entity/CForumThread.php

@ -2429,8 +2429,8 @@ function store_thread($current_forum, $values, $courseInfo = array(), $showMessa
$course_id = $courseInfo['real_id']; $course_id = $courseInfo['real_id'];
$courseCode = $courseInfo['code']; $courseCode = $courseInfo['code'];
$em = Database::getManager();
$table_threads = Database :: get_course_table(TABLE_FORUM_THREAD); $table_threads = Database :: get_course_table(TABLE_FORUM_THREAD);
$table_posts = Database :: get_course_table(TABLE_FORUM_POST);
$gradebook = isset($_GET['gradebook']) ? Security::remove_XSS($_GET['gradebook']) : ''; $gradebook = isset($_GET['gradebook']) ? Security::remove_XSS($_GET['gradebook']) : '';
$upload_ok = 1; $upload_ok = 1;
@ -2441,8 +2441,17 @@ function store_thread($current_forum, $values, $courseInfo = array(), $showMessa
$has_attachment = true; $has_attachment = true;
} }
if ($upload_ok) { if (!$upload_ok) {
$post_date = api_get_utc_datetime(); if ($showMessage) {
Display::addFlash(
Display::return_message(get_lang('UplNoFileUploaded'), 'error', false)
);
}
return null;
}
$post_date = new DateTime(api_get_utc_datetime(), new DateTimeZone('UTC'));
if ($current_forum['approval_direct_post'] == '1' && !api_is_allowed_to_edit(null, true)) { if ($current_forum['approval_direct_post'] == '1' && !api_is_allowed_to_edit(null, true)) {
$visible = 0; // The post has not been approved yet. $visible = 0; // The post has not been approved yet.
@ -2453,26 +2462,29 @@ function store_thread($current_forum, $values, $courseInfo = array(), $showMessa
$clean_post_title = $values['post_title']; $clean_post_title = $values['post_title'];
// We first store an entry in the forum_thread table because the thread_id is used in the forum_post table. // We first store an entry in the forum_thread table because the thread_id is used in the forum_post table.
$last_thread_id = Database::insert(
$table_threads, $lastThread = new \Chamilo\CourseBundle\Entity\CForumThread();
[ $lastThread
'c_id' => $course_id, ->setCId($course_id)
'thread_title' => $clean_post_title, ->setThreadTitle($clean_post_title)
'forum_id' => $values['forum_id'], ->setForumId($values['forum_id'])
'thread_poster_id' => $_user['user_id'], ->setThreadPosterId($_user['user_id'])
'thread_poster_name' => isset($values['poster_name']) ? $values['poster_name'] : '', ->setThreadPosterName(isset($values['poster_name']) ? $values['poster_name'] : null)
'thread_date' => $post_date, ->setThreadDate($post_date)
'thread_sticky' => isset($values['thread_sticky']) ? $values['thread_sticky'] : 0, ->setThreadSticky(isset($values['thread_sticky']) ? $values['thread_sticky'] : 0)
'thread_title_qualify' => isset($values['calification_notebook_title']) ? $values['calification_notebook_title'] : '', ->setThreadTitleQualify(
'thread_qualify_max' => isset($values['numeric_calification']) ? (int) $values['numeric_calification'] : 0, isset($values['calification_notebook_title']) ? $values['calification_notebook_title'] : null
'thread_weight' => isset($values['weight_calification']) ? (int) $values['weight_calification'] : 0, )
'thread_peer_qualify' => isset($values['thread_peer_qualify']) ? (int) $values['thread_peer_qualify'] : 0, ->setThreadQualifyMax(isset($values['numeric_calification']) ? (int) $values['numeric_calification'] : 0)
'session_id' => api_get_session_id(), ->setThreadWeight(isset($values['weight_calification']) ? (int) $values['weight_calification'] : 0)
'lp_item_id' => isset($values['lp_item_id']) ? (int) $values['lp_item_id'] : 0, ->setThreadPeerQualify(isset($values['thread_peer_qualify']) ? (int) $values['thread_peer_qualify'] : 0)
'thread_id' => 0, ->setSessionId(api_get_session_id())
'locked' => 0 ->setLpItemId(isset($values['lp_item_id']) ? (int) $values['lp_item_id'] : 0)
] ->setThreadId(0)
); ->setLocked(0);
$em->persist($lastThread);
$em->flush();
// Add option gradebook qualify. // Add option gradebook qualify.
@ -2481,7 +2493,7 @@ function store_thread($current_forum, $values, $courseInfo = array(), $showMessa
) { ) {
// Add function gradebook. // Add function gradebook.
$resourcetype = 5; $resourcetype = 5;
$resourceid = $last_thread_id; $resourceid = $lastThread->getIid();
$resourcename = stripslashes($values['calification_notebook_title']); $resourcename = stripslashes($values['calification_notebook_title']);
$maxqualify = $values['numeric_calification']; $maxqualify = $values['numeric_calification'];
$weigthqualify = $values['weight_calification']; $weigthqualify = $values['weight_calification'];
@ -2500,16 +2512,16 @@ function store_thread($current_forum, $values, $courseInfo = array(), $showMessa
); );
} }
if ($last_thread_id) { if ($lastThread->getIid()) {
$lastThread->setThreadId($lastThread->getIid());
$sql = "UPDATE $table_threads SET thread_id = $last_thread_id $em->merge($lastThread);
WHERE iid = $last_thread_id"; $em->flush();
Database::query($sql);
api_item_property_update( api_item_property_update(
$courseInfo, $courseInfo,
TOOL_FORUM_THREAD, TOOL_FORUM_THREAD,
$last_thread_id, $lastThread->getIid(),
'ForumThreadAdded', 'ForumThreadAdded',
api_get_user_id(), api_get_user_id(),
api_get_group_id(), api_get_group_id(),
@ -2528,7 +2540,7 @@ function store_thread($current_forum, $values, $courseInfo = array(), $showMessa
// to make the thread visible AND the post. // to make the thread visible AND the post.
// Default behaviour // Default behaviour
api_set_default_visibility( api_set_default_visibility(
$last_thread_id, $lastThread->getIid(),
TOOL_FORUM_THREAD, TOOL_FORUM_THREAD,
api_get_group_id(), api_get_group_id(),
$courseInfo $courseInfo
@ -2538,7 +2550,7 @@ function store_thread($current_forum, $values, $courseInfo = array(), $showMessa
api_item_property_update( api_item_property_update(
$courseInfo, $courseInfo,
TOOL_FORUM_THREAD, TOOL_FORUM_THREAD,
$last_thread_id, $lastThread->getIid(),
'invisible', 'invisible',
api_get_user_id(), api_get_user_id(),
api_get_group_id() api_get_group_id()
@ -2549,26 +2561,37 @@ function store_thread($current_forum, $values, $courseInfo = array(), $showMessa
} }
// We now store the content in the table_post table. // We now store the content in the table_post table.
$params = [ $lastPost = new CForumPost();
'c_id' => $course_id, $lastPost
'post_title' => $clean_post_title, ->setCId($course_id)
'post_text' => $values['post_text'], ->setPostTitle($clean_post_title)
'thread_id' => $last_thread_id, ->setPostText($values['post_text'])
'forum_id' => $values['forum_id'], ->setThreadId($lastThread->getIid())
'poster_id' => $_user['user_id'], ->setForumId($values['forum_id'])
'poster_name' => isset($values['poster_name']) ? $values['poster_name'] : '', ->setPosterId($_user['user_id'])
'post_date' => $post_date, ->setPosterName(isset($values['poster_name']) ? $values['poster_name'] : null)
'post_notification' => isset($values['post_notification']) ? (int) $values['post_notification'] : 0, ->setPostDate($post_date)
'post_parent_id' => null, ->setPostNotification(isset($values['post_notification']) ? (int) $values['post_notification'] : null)
'visible' => $visible, ->setPostParentId(null)
'post_id' => 0 ->setVisible($visible)
]; ->setPostId(0)
$last_post_id = Database::insert($table_posts, $params); ->setStatus(CForumPost::STATUS_VALIDATED);
if ($current_forum['moderated']) {
$lastPost->setStatus(
api_is_course_admin() ? CForumPost::STATUS_VALIDATED : CForumPost::STATUS_WAITING_MODERATION
);
}
$em->persist($lastPost);
$em->flush();
$last_post_id = $lastPost->getIid();
if ($last_post_id) { if ($last_post_id) {
$sql = "UPDATE $table_posts SET post_id = $last_post_id $lastPost->setPostId($last_post_id);
WHERE iid = $last_post_id"; $em->merge($lastPost);
Database::query($sql); $em->flush();
} }
// Update attached files // Update attached files
@ -2590,7 +2613,7 @@ function store_thread($current_forum, $values, $courseInfo = array(), $showMessa
SET thread_last_post = '".Database::escape_string($last_post_id)."' SET thread_last_post = '".Database::escape_string($last_post_id)."'
WHERE WHERE
c_id = $course_id AND c_id = $course_id AND
thread_id='".Database::escape_string($last_thread_id)."'"; thread_id='".Database::escape_string($lastThread->getIid())."'";
$result = Database::query($sql); $result = Database::query($sql);
$message = get_lang('NewThreadStored'); $message = get_lang('NewThreadStored');
// Storing the attachments if any. // Storing the attachments if any.
@ -2627,17 +2650,17 @@ function store_thread($current_forum, $values, $courseInfo = array(), $showMessa
} else { } else {
$message .= get_lang('ReturnTo').' <a href="viewforum.php?'.api_get_cidreq().'&forum='.$values['forum_id'].'">'. $message .= get_lang('ReturnTo').' <a href="viewforum.php?'.api_get_cidreq().'&forum='.$values['forum_id'].'">'.
get_lang('Forum').'</a><br />'; get_lang('Forum').'</a><br />';
$message .= get_lang('ReturnTo').' <a href="viewthread.php?'.api_get_cidreq().'&forum='.$values['forum_id'].'&gradebook='.$gradebook.'&thread='.$last_thread_id.'">'. $message .= get_lang('ReturnTo').' <a href="viewthread.php?'.api_get_cidreq().'&forum='.$values['forum_id'].'&gradebook='.$gradebook.'&thread='.$lastThread->getIid().'">'.
get_lang('Message').'</a>'; get_lang('Message').'</a>';
} }
$reply_info['new_post_id'] = $last_post_id; $reply_info['new_post_id'] = $last_post_id;
$my_post_notification = isset($values['post_notification']) ? $values['post_notification'] : null; $my_post_notification = isset($values['post_notification']) ? $values['post_notification'] : null;
if ($my_post_notification == 1) { if ($my_post_notification == 1) {
set_notification('thread', $last_thread_id, true); set_notification('thread', $lastThread->getIid(), true);
} }
send_notification_mails($last_thread_id, $reply_info); send_notification_mails($lastThread->getIid(), $reply_info);
Session::erase('formelements'); Session::erase('formelements');
Session::erase('origin'); Session::erase('origin');
@ -2648,18 +2671,7 @@ function store_thread($current_forum, $values, $courseInfo = array(), $showMessa
if ($showMessage) { if ($showMessage) {
Display::addFlash(Display::return_message($message, 'success', false)); Display::addFlash(Display::return_message($message, 'success', false));
} }
return $last_thread_id; return $lastThread->getIid();
} else {
if ($showMessage) {
Display::addFlash(
Display::return_message(get_lang('UplNoFileUploaded'),
'error',
false
)
);
}
}
} }
/** /**
@ -5949,13 +5961,13 @@ function getPostStatus($current_forum, $row)
$statusIcon = '<br /><br />'; $statusIcon = '<br /><br />';
$row['status'] = empty($row['status']) ? 2 : $row['status']; $row['status'] = empty($row['status']) ? 2 : $row['status'];
switch ($row['status']) { switch ($row['status']) {
case 1: case CForumPost::STATUS_VALIDATED:
$statusIcon .= Display::label(get_lang('Validated'), 'success'); $statusIcon .= Display::label(get_lang('Validated'), 'success');
break; break;
case 2: case CForumPost::STATUS_WAITING_MODERATION:
$statusIcon .= Display::label(get_lang('WaitingModeration'), 'warning'); $statusIcon .= Display::label(get_lang('WaitingModeration'), 'warning');
break; break;
case 3: case CForumPost::STATUS_REJECTED:
$statusIcon .= Display::label(get_lang('Rejected'), 'danger'); $statusIcon .= Display::label(get_lang('Rejected'), 'danger');
break; break;
} }

@ -22,6 +22,10 @@ use Doctrine\ORM\Mapping as ORM;
*/ */
class CForumPost class CForumPost
{ {
const STATUS_VALIDATED = 1;
const STATUS_WAITING_MODERATION = 2;
const STATUS_REJECTED = 3;
/** /**
* @var integer * @var integer
* *
@ -417,4 +421,12 @@ class CForumPost
return $this; return $this;
} }
/**
* Get iid
* @return int
*/
public function getIid()
{
return $this->iid;
}
} }

@ -167,6 +167,8 @@ class CForumThread
public function __construct() public function __construct()
{ {
$this->threadPeerQualify = 0; $this->threadPeerQualify = 0;
$this->threadReplies = 0;
$this->threadViews = 0;
} }
/** /**
@ -178,11 +180,15 @@ class CForumThread
} }
/** /**
* @param boolean $threadPeerQualify * set threadPeerQualify
* @param $threadPeerQualify
* @return $this
*/ */
public function setThreadPeerQualify($threadPeerQualify) public function setThreadPeerQualify($threadPeerQualify)
{ {
$this->threadPeerQualify = $threadPeerQualify; $this->threadPeerQualify = $threadPeerQualify;
return $this;
} }
/** /**
@ -614,4 +620,13 @@ class CForumThread
{ {
return $this->lpItemId; return $this->lpItemId;
} }
/**
* Get iid
* @return int
*/
public function getIid()
{
return $this->iid;
}
} }

Loading…
Cancel
Save