Skip to content

Commit 36b43d3

Browse files
committed
Add setting 'private_messages_about_user' see BT#12622
- Allow send messages between boss, teachers that have access to main/mySpace/myStudents.php?student=x - Messages are related of a user.
1 parent d8aad8a commit 36b43d3

File tree

5 files changed

+160
-25
lines changed

5 files changed

+160
-25
lines changed

main/inc/lib/api.lib.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,8 @@
537537
define('MESSAGE_STATUS_WALL', '8');
538538
define('MESSAGE_STATUS_WALL_DELETE', '9');
539539
define('MESSAGE_STATUS_WALL_POST', '10');
540+
define('MESSAGE_STATUS_CONVERSATION', '11');
541+
540542
// Images
541543
define('IMAGE_WALL_SMALL_SIZE', 200);
542544
define('IMAGE_WALL_MEDIUM_SIZE', 500);

main/inc/lib/display.lib.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2460,6 +2460,7 @@ public static function returnFontAwesomeIcon(
24602460
* @param bool|true $open
24612461
* @param bool|false $fullClickable
24622462
* @return null|string
2463+
* @todo rework function to easy use
24632464
*/
24642465
public static function panelCollapse(
24652466
$title,

main/inc/lib/message.lib.php

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,67 @@ public static function get_message_data($from, $number_of_items, $column, $direc
176176
return $message_list;
177177
}
178178

179+
/**
180+
* @param array $aboutUserInfo
181+
* @param array $fromUserInfo
182+
* @param string $subject
183+
* @param string $content
184+
* @return bool
185+
*/
186+
public static function sendMessageAboutUser(
187+
$aboutUserInfo,
188+
$fromUserInfo,
189+
$subject,
190+
$content
191+
) {
192+
if (empty($aboutUserInfo) || empty($fromUserInfo)) {
193+
return false;
194+
}
195+
196+
if (empty($fromUserInfo['id']) || empty($aboutUserInfo['id'])) {
197+
return false;
198+
}
199+
200+
$table = Database::get_main_table(TABLE_MESSAGE);
201+
$now = api_get_utc_datetime();
202+
$params = [
203+
'user_sender_id' => $fromUserInfo['id'],
204+
'user_receiver_id' => $aboutUserInfo['id'],
205+
'msg_status' => MESSAGE_STATUS_CONVERSATION,
206+
'send_date' => $now,
207+
'title' => $subject,
208+
'content' => $content,
209+
'group_id' => 0,
210+
'parent_id' => 0,
211+
'update_date' => $now
212+
];
213+
$id = Database::insert($table, $params);
214+
if ($id) {
215+
return true;
216+
}
217+
218+
return false;
219+
}
220+
221+
/**
222+
* @param array $aboutUserInfo
223+
* @return array
224+
*/
225+
public static function getMessagesAboutUser($aboutUserInfo)
226+
{
227+
if (!empty($aboutUserInfo)) {
228+
$criteria = [
229+
'userReceiverId' => $aboutUserInfo['id'],
230+
'msgStatus' => MESSAGE_STATUS_CONVERSATION
231+
];
232+
$repo = Database::getManager()->getRepository('ChamiloCoreBundle:Message');
233+
$messages = $repo->findBy($criteria, ['sendDate' => 'DESC']);
234+
235+
return $messages;
236+
}
237+
238+
return [];
239+
}
179240

180241
/**
181242
* Sends a message to a user/group
@@ -247,7 +308,7 @@ public static function send_message(
247308
if (empty($subject) && empty($group_id)) {
248309
Display::addFlash(Display::return_message(get_lang('YouShouldWriteASubject'), 'warning'));
249310
return false;
250-
} else if ($total_filesize > intval(api_get_setting('message_max_upload_filesize'))) {
311+
} elseif ($total_filesize > intval(api_get_setting('message_max_upload_filesize'))) {
251312
$warning = sprintf(
252313
get_lang("FilesSizeExceedsX"),
253314
format_file_size(api_get_setting('message_max_upload_filesize'))

main/install/configuration.dist.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,3 +513,7 @@
513513
// ------
514514
// Hide search form in session list
515515
//$_configuration['hide_search_form_in_session_list'] = false;
516+
// Allow conversation of student boss and teachers about user privately
517+
//$_configuration['private_messages_about_user'] = false;
518+
519+

main/mySpace/myStudents.php

Lines changed: 91 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use Chamilo\CourseBundle\Entity\CLp;
55
use Chamilo\CourseBundle\Entity\CLpCategory;
6+
use Chamilo\CoreBundle\Entity\Message;
67

78
/**
89
* Implements the tracking of students in the Reporting pages
@@ -17,10 +18,18 @@
1718
$origin = isset($_GET['origin']) ? Security::remove_XSS($_GET['origin']) : '';
1819
$course_code = isset($_GET['course']) ? Security::remove_XSS($_GET['course']) : '';
1920
$courseInfo = api_get_course_info($course_code);
20-
$student_id = intval($_GET['student']);
21+
$student_id = isset($_GET['student']) ? (int) $_GET['student'] : 0;
2122

22-
$allowedToTrackUser = true;
23+
$currentUrl = api_get_self().'?student='.$student_id.'&course='.$course_code.'&id_session='.$sessionId.'&origin='.$origin;
24+
25+
if (empty($student_id)) {
26+
api_not_allowed(true);
27+
}
2328

29+
// user info
30+
$user_info = api_get_user_info($student_id);
31+
32+
$allowedToTrackUser = true;
2433
if (!api_is_session_admin() &&
2534
!api_is_drh() &&
2635
!api_is_student_boss() &&
@@ -184,11 +193,23 @@ function show_image(image,width,height) {
184193
$tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
185194
$tbl_stats_exercices = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
186195
$user_id = isset($_GET['user_id']) && !empty($_GET['user_id']) ? (int) $_GET['user_id'] : api_get_user_id();
187-
188-
// Action behaviour
189196
$action = isset($_GET['action']) ? $_GET['action'] : '';
190197

191198
switch ($action) {
199+
case 'send_message':
200+
$subject = isset($_POST['subject']) ? $_POST['subject'] : '';
201+
$message = isset($_POST['message']) ? $_POST['message'] : '';
202+
203+
MessageManager::sendMessageAboutUser(
204+
$user_info,
205+
api_get_user_info(),
206+
$subject,
207+
$message
208+
);
209+
Display::addFlash(Display::return_message(get_lang('MessageSent')));
210+
header('Location: '.$currentUrl);
211+
exit;
212+
break;
192213
case 'send_legal':
193214
$subject = get_lang('SendLegalSubject');
194215
$content = sprintf(
@@ -225,18 +246,15 @@ function show_image(image,width,height) {
225246

226247
// @todo delete the stats.track_e_exercises records.
227248
// First implement this http://support.chamilo.org/issues/1334
228-
$message = Display::return_message(
249+
Display::addFlash(Display::return_message(
229250
get_lang('LPWasReset'),
230251
'success'
231-
);
252+
));
232253
}
233254
break;
234255
default:
235256
break;
236257
}
237-
238-
// user info
239-
$user_info = api_get_user_info($student_id);
240258
$courses_in_session = array();
241259

242260
//See #4676
@@ -252,11 +270,15 @@ function show_image(image,width,height) {
252270
// RRHH or session admin
253271
if (api_is_session_admin() || api_is_drh()) {
254272
$courses = CourseManager::get_courses_followed_by_drh(api_get_user_id());
255-
$session_by_session_admin = SessionManager::get_sessions_followed_by_drh(api_get_user_id());
273+
$session_by_session_admin = SessionManager::get_sessions_followed_by_drh(
274+
api_get_user_id()
275+
);
256276

257277
if (!empty($session_by_session_admin)) {
258278
foreach ($session_by_session_admin as $session_coached_by_user) {
259-
$courses_followed_by_coach = Tracking :: get_courses_list_from_session($session_coached_by_user['id']);
279+
$courses_followed_by_coach = Tracking::get_courses_list_from_session(
280+
$session_coached_by_user['id']
281+
);
260282
$courses_in_session_by_coach[$session_coached_by_user['id']] = $courses_followed_by_coach;
261283
}
262284
}
@@ -278,7 +300,7 @@ function show_image(image,width,height) {
278300
user_id = ".intval($user_info['user_id']);
279301
$rs = Database::query($sql);
280302

281-
while ($row = Database :: fetch_array($rs)) {
303+
while ($row = Database::fetch_array($rs)) {
282304
if ($drh_can_access_all_courses) {
283305
$courses_in_session[0][] = $row['c_id'];
284306
} else {
@@ -294,7 +316,7 @@ function show_image(image,width,height) {
294316
WHERE user_id=' . intval($user_info['user_id']);
295317
$rs = Database::query($sql);
296318
$tmp_sessions = array();
297-
while ($row = Database :: fetch_array($rs, 'ASSOC')) {
319+
while ($row = Database::fetch_array($rs, 'ASSOC')) {
298320
$tmp_sessions[] = $row['session_id'];
299321
if ($drh_can_access_all_courses) {
300322
if (in_array($row['session_id'], $tmp_sessions)) {
@@ -339,17 +361,8 @@ function show_image(image,width,height) {
339361
}
340362

341363
Display::display_header($nameTools);
342-
343-
if (isset($message)) {
344-
echo $message;
345-
}
346-
347364
$token = Security::get_token();
348365

349-
if (empty($student_id)) {
350-
api_not_allowed();
351-
}
352-
353366
// Actions bar
354367
echo '<div class="actions">';
355368
echo '<a href="javascript: window.history.go(-1);">'.
@@ -980,7 +993,7 @@ function show_image(image,width,height) {
980993
$lps = $query->getResult();
981994
*/
982995
if (true) {
983-
$categoriesTempList = learnpath::getCategories(api_get_course_int_id());
996+
$categoriesTempList = learnpath::getCategories($courseInfo['real_id']);
984997
$categoryTest = new CLpCategory();
985998
$categoryTest->setId(0);
986999
$categoryTest->setName(get_lang('WithOutCategory'));
@@ -1002,7 +1015,7 @@ function show_image(image,width,height) {
10021015

10031016
$list = new LearnpathList(
10041017
api_get_user_id(),
1005-
null,
1018+
$courseInfo['code'],
10061019
null,
10071020
null,
10081021
false,
@@ -1567,6 +1580,60 @@ function show_image(image,width,height) {
15671580
$sessionId
15681581
);
15691582

1583+
$allowMessages = api_get_configuration_value('private_messages_about_user');
1584+
if ($allowMessages === true) {
1585+
// Messages
1586+
echo Display::page_subheader2(get_lang('Messages'));
1587+
$messages = MessageManager::getMessagesAboutUser($user_info);
1588+
1589+
if (!empty($messages)) {
1590+
/** @var Message $message */
1591+
foreach ($messages as $message) {
1592+
$tag = 'message_'.$message->getId();
1593+
$tagAccordion = 'accordion_'.$message->getId();
1594+
$tagCollapse = 'collapse_'.$message->getId();
1595+
$date = Display::dateToStringAgoAndLongDate(
1596+
$message->getSendDate()
1597+
);
1598+
$senderId = $message->getUserSenderId();
1599+
$senderInfo = api_get_user_info($senderId);
1600+
echo Display::panelCollapse(
1601+
$message->getTitle(),
1602+
$message->getContent().'<br />'.$date.'<br />'.get_lang(
1603+
'Author'
1604+
).': '.$senderInfo['complete_name_with_message_link'],
1605+
$tag,
1606+
null,
1607+
$tagAccordion,
1608+
$tagCollapse,
1609+
false
1610+
);
1611+
}
1612+
}
1613+
1614+
echo Display::url(
1615+
get_lang('NewMessage'),
1616+
'javascript: void(0);',
1617+
[
1618+
'onClick' => "$('#compose_message').show();",
1619+
'class' => 'btn btn-default'
1620+
]
1621+
);
1622+
1623+
$form = new FormValidator(
1624+
'messages',
1625+
'post',
1626+
api_get_self().'?action=send_message&student='.$student_id
1627+
);
1628+
$form->addHtml('<div id="compose_message" style="display:none;">');
1629+
$form->addText('subject', get_lang('Subject'));
1630+
$form->addHtmlEditor('message', get_lang('Message'));
1631+
$form->addButtonSend(get_lang('Send'));
1632+
$form->addHidden('sec_token', $token);
1633+
$form->addHtml('</div>');
1634+
$form->display();
1635+
}
1636+
15701637
if ($export) {
15711638
ob_end_clean();
15721639
switch ($export) {

0 commit comments

Comments
 (0)