diff --git a/main/template/default/work/view.tpl b/main/template/default/work/view.tpl
index 61632b389b..989f6406ad 100644
--- a/main/template/default/work/view.tpl
+++ b/main/template/default/work/view.tpl
@@ -20,10 +20,23 @@
{{ comment.username }}
- {{ comment.sent_at | api_get_local_time }}
+
+ {{ comment.comment }}
+
-
- {{ comment.comment }}
-
+ {% if comment.file_url is not empty %}
+
+
+
+ {{ 'Download' | get_lang }}
+
+ {% if is_allowed_to_edit %}
+
+
+
+ {% endif %}
+
+ {% endif %}
{% endfor %}
diff --git a/main/work/download_comment_file.php b/main/work/download_comment_file.php
new file mode 100644
index 0000000000..ea5ec7f177
--- /dev/null
+++ b/main/work/download_comment_file.php
@@ -0,0 +1,47 @@
+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);
diff --git a/main/work/work.lib.php b/main/work/work.lib.php
index 1abc297f73..af29c0102a 100644
--- a/main/work/work.lib.php
+++ b/main/work/work.lib.php
@@ -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'));