Adding attachement in work comments see BT#6418

1.9.x
Julio Montoya 11 years ago
parent 6a5310e224
commit 28e9d64292
  1. 19
      main/template/default/work/view.tpl
  2. 47
      main/work/download_comment_file.php
  3. 1
      main/work/extra.sql
  4. 24
      main/work/view.php
  5. 112
      main/work/work.lib.php

@ -20,10 +20,23 @@
<img height="24" src="{{ comment.picture }}"/> {{ comment.username }}
</a>- {{ comment.sent_at | api_get_local_time }}
</div>
<p>
{{ comment.comment }}
</p>
<p>
{{ comment.comment }}
</p>
{% if comment.file_url is not empty %}
<p>
<a href="{{ comment.file_url }}">
<img src="{{ "attachment.gif"|icon(32) }}">
{{ 'Download' | get_lang }}
</a>
{% if is_allowed_to_edit %}
<a href="{{ comment.delete_file_url }}">
<img src="{{ "delete.png"|icon(22) }}">
</a>
{% endif %}
</p>
{% endif %}
</li>
{% endfor %}
</ul>

@ -0,0 +1,47 @@
<?php
/* For licensing terms, see /license.txt */
/**
* This file is responsible for passing requested documents to the browser.
* Html files are parsed to fix a few problems with URLs,
* but this code will hopefully be replaced soon by an Apache URL
* rewrite mechanism.
*
* @package chamilo.work
*/
require_once '../inc/global.inc.php';
require_once 'work.lib.php';
// Course protection
api_protect_course_script(true);
$commentId = isset($_GET['comment_id']) ? intval($_GET['comment_id']) : null;
if (empty($commentId)) {
api_not_allowed(true);
}
$workData = getWorkComment($commentId);
$courseInfo = api_get_course_info();
if (!empty($workData)) {
if (
empty($workData['file_path']) ||
(isset($workData['file_path']) && !file_exists($workData['file_path']))
) {
api_not_allowed(true);
}
$work = get_work_data_by_id($workData['work_id']);
allowOnlySubscribedUser(api_get_user_id(), $work['parent_id'], $courseInfo['real_id']);
if (user_is_author($workData['work_id']) || $courseInfo['show_score'] == 0 && $work['active'] == 1 && $work['accepted'] == 1) {
if (Security::check_abs_path($workData['file_path'], api_get_path(SYS_COURSE_PATH).api_get_course_path().'/')) {
DocumentManager::file_send_for_download($workData['file_path'], true);
}
} else {
api_not_allowed(true);
}
} else {
api_not_allowed(true);
}

@ -19,6 +19,7 @@ CREATE TABLE IF NOT EXISTS c_student_publication_comment (
work_id INT NOT NULL,
c_id INT NOT NULL,
comment text,
file VARCHAR(255),
user_id int NOT NULL,
sent_at datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

@ -34,12 +34,23 @@ if (user_is_author($id) || $course_info['show_score'] == 0 && $work['active'] ==
if (($course_info['show_score'] == 0 && $work['active'] == 1 && $work['accepted'] == 1) ||
api_is_allowed_to_edit() || user_is_author($id)
) {
if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'send_comment') {
addWorkComment(api_get_user_id(), $work, $_POST);
$url = api_get_path(WEB_CODE_PATH).'work/view.php?id='.$work['id'].'&'.api_get_cidreq();
header('Location: '.$url);
exit;
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : null;
switch($action) {
case 'send_comment':
if (isset($_FILES["file"])) {
$_POST['file'] = $_FILES["file"];
}
addWorkComment(api_get_course_info(), api_get_user_id(), $work, $_POST);
$url = api_get_path(WEB_CODE_PATH).'work/view.php?id='.$work['id'].'&'.api_get_cidreq();
header('Location: '.$url);
exit;
break;
case 'delete_attachment':
deleteCommentFile($_REQUEST['comment_id'], api_get_course_info());
$url = api_get_path(WEB_CODE_PATH).'work/view.php?id='.$work['id'].'&'.api_get_cidreq();
header('Location: '.$url);
exit;
break;
}
$comments = getWorkComments($work);
@ -50,6 +61,7 @@ if (user_is_author($id) || $course_info['show_score'] == 0 && $work['active'] ==
$tpl->assign('work_comment_enabled', ALLOW_USER_COMMENTS);
$tpl->assign('comments', $comments);
$tpl->assign('form', $commentForm);
$tpl->assign('is_allowed_to_edit', api_is_allowed_to_edit());
$template = $tpl->get_template('work/view.tpl');
$content = $tpl->fetch($template);

@ -18,16 +18,18 @@ use ChamiloSession as Session;
*/
require_once api_get_path(SYS_CODE_PATH).'document/document.inc.php';
require_once api_get_path(LIBRARY_PATH).'fileDisplay.lib.php';
require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
$_configuration['add_document_to_work'] = true;
if (isset($_configuration['add_document_to_work'])) {
define('ADD_DOCUMENT_TO_WORK', $_configuration['add_document_to_work']);
} else {
define('ADD_DOCUMENT_TO_WORK', false);
}
//$_configuration['work_user_comments'] = false;
$_configuration['work_user_comments'] = true;
if (isset($_configuration['work_user_comments'])) {
define('ALLOW_USER_COMMENTS', $_configuration['work_user_comments']);
} else {
@ -2773,7 +2775,6 @@ function getWorkDescriptionToolbar()
*/
function getWorkComments($work)
{
if (ADD_DOCUMENT_TO_WORK == false) {
return array();
}
@ -2791,21 +2792,100 @@ function getWorkComments($work)
";
$result = Database::query($sql);
$comments = Database::store_result($result, 'ASSOC');
$urlPath = api_get_path(WEB_CODE_PATH).'work/download_comment_file.php?'.api_get_cidreq();
$deleteUrl = api_get_path(WEB_CODE_PATH).'work/view.php?'.api_get_cidreq().'&id='.$workId.'&action=delete_attachment';
foreach ($comments as &$comment) {
$pictureInfo = UserManager::get_picture_user($comment['user_id'], $comment['picture_uri'], 24, USER_IMAGE_SIZE_SMALL);
$pictureInfo = UserManager::get_picture_user(
$comment['user_id'],
$comment['picture_uri'],
24,
USER_IMAGE_SIZE_SMALL
);
if (!empty($comment['file'])) {
$comment['file_url'] = $urlPath.'&comment_id='.$comment['id'];
$comment['delete_file_url'] = $deleteUrl.'&comment_id='.$comment['id'];
}
$comment['picture'] = $pictureInfo['file'];
}
return $comments;
}
/**
* @param int $id comment id
* @param array $courseInfo
* @return string
*/
function getWorkComment($id, $courseInfo = array())
{
if (ADD_DOCUMENT_TO_WORK == false) {
return array();
}
if (empty($courseInfo)) {
$courseInfo = api_get_course_info();
}
if (empty($courseInfo['real_id'])) {
return array();
}
$commentTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT_COMMENT);
$id = intval($id);
$sql = "SELECT * FROM $commentTable
WHERE id = $id AND c_id = ".$courseInfo['real_id'];
$result = Database::query($sql);
$comment = array();
if (Database::num_rows($result)) {
$comment = Database::fetch_array($result, 'ASSOC');
$filePath = null;
$fileUrl = null;
if (!empty($comment['file'])) {
$work = get_work_data_by_id($comment['work_id']);
$workParent = get_work_data_by_id($work['parent_id']);
$filePath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/work/'.$workParent['url'].'/'.$comment['file'];
$fileUrl = api_get_path(WEB_CODE_PATH).'work/download_comment_file.php?comment_id='.$id;
}
$comment['file_path'] = $filePath;
$comment['file_url'] = $fileUrl;
}
return $comment;
}
/**
* @param int $id
* @param array $courseInfo
*/
function deleteCommentFile($id, $courseInfo = array())
{
$workComment = getWorkComment($id, $courseInfo);
if (isset($workComment['file']) && !empty($workComment['file'])) {
if (file_exists($workComment['file_path'])) {
$result = my_delete($workComment['file_path']);
if ($result) {
$commentTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT_COMMENT);
$params = array('file' => '');
Database::update(
$commentTable,
$params,
array('id = ? AND c_id = ? ' => array($workComment['id'], $workComment['c_id']))
);
}
}
}
}
/**
* Adds a comments to the work document
* @param array $courseInfo
* @param int $userId
* @param array $work
* @param array $data
* @return int
*/
function addWorkComment($userId, $work, $data)
function addWorkComment($courseInfo, $userId, $work, $data)
{
if (ADD_DOCUMENT_TO_WORK == false) {
return null;
@ -2824,7 +2904,28 @@ function addWorkComment($userId, $work, $data)
'comment' => $data['comment'],
'sent_at' => api_get_utc_datetime()
);
return Database::insert($commentTable, $params);
$commentId = Database::insert($commentTable, $params);
$fileData = isset($data['file']) ? $data['file'] : null;
if (!empty($commentId) && !empty($fileData)) {
$workParent = get_work_data_by_id($work['parent_id']);
if (!empty($workParent)) {
$uploadDir = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/work'.$workParent['url'];
$newFileName = 'comment_'.$commentId.'_'.php2phps(
replace_dangerous_char($fileData['name'], 'strict')
);
$newFilePath = $uploadDir.'/'.$newFileName;
$result = move_uploaded_file($fileData['tmp_name'], $newFilePath);
if ($result) {
$params = array('file' => $newFileName);
Database::update(
$commentTable,
$params,
array('id = ? AND c_id = ? ' => array($commentId, $work['c_id']))
);
}
}
}
}
/**
@ -2842,6 +2943,7 @@ function getWorkCommentForm($work)
api_get_path(WEB_CODE_PATH).'work/view.php?id='.$work['id'].'&action=send_comment&'.api_get_cidreq()
);
$form->addElement('textarea', 'comment', get_lang('Comment'), array('class' => 'span5', 'rows' => '8'));
$form->addElement('file', 'file', get_lang('Attachment'));
$form->addRule('comment', get_lang('ThisFieldIsRequired'), 'required');
$form->addElement('hidden', 'id', $work['id']);
$form->addElement('button', 'button', get_lang('Send'));

Loading…
Cancel
Save