Add auto subscribe users to all forum notifications see BT#15932

Course setting  "subscribe_users_to_forum_notifications"
pull/2970/head
Julio Montoya 6 years ago
parent b1cbe90de7
commit 3600bdc528
  1. 2
      main/auth/courses_list.php
  2. 6
      main/course_info/infocours.php
  3. 133
      main/forum/forumfunction.inc.php
  4. 15
      main/forum/index.php
  5. 11
      main/inc/lib/course.lib.php

@ -51,7 +51,7 @@ if (!empty($user_course_categories)) {
} }
if ($action != 'unsubscribe') { if ($action != 'unsubscribe') {
if ($allowCollapsable) { if ($allowCollapsable) {
if ($row['collapsed'] == 0) { if (isset($row['collapsed']) && $row['collapsed'] == 0) {
echo Display::url( echo Display::url(
'<i class="fa fa-folder-open"></i>', '<i class="fa fa-folder-open"></i>',
$url.'&action=set_collapsable&option=1' $url.'&action=set_collapsable&option=1'

@ -875,9 +875,15 @@ $groupNotification = [
$form->createElement('radio', 'hide_forum_notifications', null, get_lang('No'), 2), $form->createElement('radio', 'hide_forum_notifications', null, get_lang('No'), 2),
]; ];
$addUsers = [
$form->createElement('radio', 'subscribe_users_to_forum_notifications', null, get_lang('Yes'), 1),
$form->createElement('radio', 'subscribe_users_to_forum_notifications', null, get_lang('No'), 2),
];
$globalGroup = [ $globalGroup = [
get_lang('EnableForumAutoLaunch') => $group, get_lang('EnableForumAutoLaunch') => $group,
get_lang('HideForumNotifications') => $groupNotification, get_lang('HideForumNotifications') => $groupNotification,
get_lang('SubscribeUsersToAllForumNotifications') => $addUsers,
'' => $myButton, '' => $myButton,
]; ];

@ -99,7 +99,7 @@ function handle_forum_and_forumcategories($lp_id = null)
$get_content = isset($_GET['content']) ? $_GET['content'] : ''; $get_content = isset($_GET['content']) ? $_GET['content'] : '';
$post_submit_cat = isset($_POST['SubmitForumCategory']) ? true : false; $post_submit_cat = isset($_POST['SubmitForumCategory']) ? true : false;
$post_submit_forum = isset($_POST['SubmitForum']) ? true : false; $post_submit_forum = isset($_POST['SubmitForum']) ? true : false;
$get_id = isset($_GET['id']) ? intval($_GET['id']) : ''; $get_id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
$forum_categories_list = get_forum_categories(); $forum_categories_list = get_forum_categories();
// Verify if forum category exists // Verify if forum category exists
@ -110,23 +110,23 @@ function handle_forum_and_forumcategories($lp_id = null)
$content = ''; $content = '';
// Adding a forum category // Adding a forum category
if (($action_forum_cat == 'add' && $get_content == 'forumcategory') || $post_submit_cat) { if (($action_forum_cat === 'add' && $get_content === 'forumcategory') || $post_submit_cat) {
$content = show_add_forumcategory_form([], $lp_id); //$lp_id when is called from learning path $content = show_add_forumcategory_form([], $lp_id); //$lp_id when is called from learning path
} }
// Adding a forum // Adding a forum
if ((($action_forum_cat == 'add' || $action_forum_cat == 'edit') && $get_content == 'forum') || if ((($action_forum_cat === 'add' || $action_forum_cat === 'edit') && $get_content === 'forum') ||
$post_submit_forum $post_submit_forum
) { ) {
$inputvalues = []; $inputvalues = [];
if ($action_forum_cat == 'edit' && $get_id || $post_submit_forum) { if ($action_forum_cat === 'edit' && $get_id || $post_submit_forum) {
$inputvalues = get_forums($get_id); $inputvalues = get_forums($get_id);
} }
$content = show_add_forum_form($inputvalues, $lp_id); $content = show_add_forum_form($inputvalues, $lp_id);
} }
// Edit a forum category // Edit a forum category
if (($action_forum_cat == 'edit' && $get_content == 'forumcategory') || if (($action_forum_cat === 'edit' && $get_content === 'forumcategory') ||
(isset($_POST['SubmitEditForumCategory'])) ? true : false (isset($_POST['SubmitEditForumCategory'])) ? true : false
) { ) {
$forum_category = get_forum_categories($get_id); $forum_category = get_forum_categories($get_id);
@ -134,10 +134,8 @@ function handle_forum_and_forumcategories($lp_id = null)
} }
// Delete a forum category // Delete a forum category
if ($action_forum_cat == 'delete') { if ($action_forum_cat === 'delete') {
$id_forum = intval($get_id); $list_threads = get_threads($get_id);
$list_threads = get_threads($id_forum);
for ($i = 0; $i < count($list_threads); $i++) { for ($i = 0; $i < count($list_threads); $i++) {
deleteForumCategoryThread('thread', $list_threads[$i]['thread_id']); deleteForumCategoryThread('thread', $list_threads[$i]['thread_id']);
$link_info = GradebookUtils::isResourceInCourseGradebook( $link_info = GradebookUtils::isResourceInCourseGradebook(
@ -154,21 +152,21 @@ function handle_forum_and_forumcategories($lp_id = null)
} }
// Change visibility of a forum or a forum category. // Change visibility of a forum or a forum category.
if ($action_forum_cat == 'invisible' || $action_forum_cat == 'visible') { if ($action_forum_cat === 'invisible' || $action_forum_cat === 'visible') {
$return_message = change_visibility($get_content, $get_id, $action_forum_cat); $return_message = change_visibility($get_content, $get_id, $action_forum_cat);
Display::addFlash( Display::addFlash(
Display::return_message($return_message, 'confirmation', false) Display::return_message($return_message, 'confirmation', false)
); );
} }
// Change lock status of a forum or a forum category. // Change lock status of a forum or a forum category.
if ($action_forum_cat == 'lock' || $action_forum_cat == 'unlock') { if ($action_forum_cat === 'lock' || $action_forum_cat === 'unlock') {
$return_message = change_lock_status($get_content, $get_id, $action_forum_cat); $return_message = change_lock_status($get_content, $get_id, $action_forum_cat);
Display::addFlash( Display::addFlash(
Display::return_message($return_message, 'confirmation', false) Display::return_message($return_message, 'confirmation', false)
); );
} }
// Move a forum or a forum category. // Move a forum or a forum category.
if ($action_forum_cat == 'move' && isset($_GET['direction'])) { if ($action_forum_cat === 'move' && isset($_GET['direction'])) {
$return_message = move_up_down($get_content, $_GET['direction'], $get_id); $return_message = move_up_down($get_content, $_GET['direction'], $get_id);
Display::addFlash( Display::addFlash(
Display::return_message($return_message, 'confirmation', false) Display::return_message($return_message, 'confirmation', false)
@ -267,14 +265,14 @@ function show_add_forum_form($inputvalues = [], $lp_id)
$form_title = get_lang('EditForum'); $form_title = get_lang('EditForum');
} }
$form->addElement('header', $form_title); $form->addHeader($form_title);
// We have a hidden field if we are editing. // We have a hidden field if we are editing.
if (!empty($inputvalues) && is_array($inputvalues)) { if (!empty($inputvalues) && is_array($inputvalues)) {
$my_forum_id = isset($inputvalues['forum_id']) ? $inputvalues['forum_id'] : null; $my_forum_id = isset($inputvalues['forum_id']) ? $inputvalues['forum_id'] : null;
$form->addElement('hidden', 'forum_id', $my_forum_id); $form->addElement('hidden', 'forum_id', $my_forum_id);
} }
$lp_id = intval($lp_id); $lp_id = (int) $lp_id;
// hidden field if from learning path // hidden field if from learning path
$form->addElement('hidden', 'lp_id', $lp_id); $form->addElement('hidden', 'lp_id', $lp_id);
@ -892,7 +890,6 @@ function store_forum($values, $courseInfo = [], $returnId = false)
$logInfo = [ $logInfo = [
'tool' => TOOL_FORUM, 'tool' => TOOL_FORUM,
'tool_id' => $values['forum_id'], 'tool_id' => $values['forum_id'],
'tool_id_detail' => 0,
'action' => 'update-forum', 'action' => 'update-forum',
'action_details' => 'forum', 'action_details' => 'forum',
'info' => $values['forum_title'], 'info' => $values['forum_title'],
@ -921,7 +918,7 @@ function store_forum($values, $courseInfo = [], $returnId = false)
'end_time' => !empty($values['end_time']) ? api_get_utc_datetime($values['end_time']) : null, 'end_time' => !empty($values['end_time']) ? api_get_utc_datetime($values['end_time']) : null,
'forum_order' => isset($new_max) ? $new_max : null, 'forum_order' => isset($new_max) ? $new_max : null,
'session_id' => $session_id, 'session_id' => $session_id,
'lp_id' => isset($values['lp_id']) ? intval($values['lp_id']) : 0, 'lp_id' => isset($values['lp_id']) ? (int) $values['lp_id'] : 0,
'locked' => 0, 'locked' => 0,
'forum_id' => 0, 'forum_id' => 0,
]; ];
@ -930,6 +927,25 @@ function store_forum($values, $courseInfo = [], $returnId = false)
if ($forumId > 0) { if ($forumId > 0) {
$sql = "UPDATE $table_forums SET forum_id = iid WHERE iid = $forumId"; $sql = "UPDATE $table_forums SET forum_id = iid WHERE iid = $forumId";
Database::query($sql); Database::query($sql);
$courseCode = $courseInfo['code'];
$subscribe = (int) api_get_course_setting('subscribe_users_to_forum_notifications', $courseCode);
$status = STUDENT;
if (!empty($session_id)) {
$status = 0;
}
if ($subscribe === 1) {
$userList = CourseManager::get_user_list_from_course_code(
$courseCode,
$session_id,
null,
null,
$status
);
foreach ($userList as $userInfo) {
set_notification('forum', $forumId, false, $userInfo, $courseInfo);
}
}
api_item_property_update( api_item_property_update(
$courseInfo, $courseInfo,
@ -950,7 +966,6 @@ function store_forum($values, $courseInfo = [], $returnId = false)
$logInfo = [ $logInfo = [
'tool' => TOOL_FORUM, 'tool' => TOOL_FORUM,
'tool_id' => $forumId, 'tool_id' => $forumId,
'tool_id_detail' => 0,
'action' => 'new-forum', 'action' => 'new-forum',
'action_details' => 'forum', 'action_details' => 'forum',
'info' => $values['forum_title'], 'info' => $values['forum_title'],
@ -999,7 +1014,7 @@ function deleteForumCategoryThread($content, $id)
$groupId = api_get_group_id(); $groupId = api_get_group_id();
$groupInfo = GroupManager::get_group_properties($groupId); $groupInfo = GroupManager::get_group_properties($groupId);
$userId = api_get_user_id(); $userId = api_get_user_id();
$id = intval($id); $id = (int) $id;
// Delete all attachment file about this tread id. // Delete all attachment file about this tread id.
$sql = "SELECT post_id FROM $table_forums_post $sql = "SELECT post_id FROM $table_forums_post
@ -1011,7 +1026,7 @@ function deleteForumCategoryThread($content, $id)
$tool_constant = null; $tool_constant = null;
$return_message = ''; $return_message = '';
if ($content == 'forumcategory') { if ($content === 'forumcategory') {
$tool_constant = TOOL_FORUM_CATEGORY; $tool_constant = TOOL_FORUM_CATEGORY;
$return_message = get_lang('ForumCategoryDeleted'); $return_message = get_lang('ForumCategoryDeleted');
@ -1033,7 +1048,7 @@ function deleteForumCategoryThread($content, $id)
} }
} }
if ($content == 'forum') { if ($content === 'forum') {
$tool_constant = TOOL_FORUM; $tool_constant = TOOL_FORUM;
$return_message = get_lang('ForumDeleted'); $return_message = get_lang('ForumDeleted');
@ -1055,7 +1070,7 @@ function deleteForumCategoryThread($content, $id)
} }
} }
if ($content == 'thread') { if ($content === 'thread') {
$tool_constant = TOOL_FORUM_THREAD; $tool_constant = TOOL_FORUM_THREAD;
$return_message = get_lang('ThreadDeleted'); $return_message = get_lang('ThreadDeleted');
Skill::deleteSkillsFromItem($id, ITEM_TYPE_FORUM_THREAD); Skill::deleteSkillsFromItem($id, ITEM_TYPE_FORUM_THREAD);
@ -5623,56 +5638,67 @@ function get_forums_of_group($groupInfo)
* *
* @param string $content does the user want to be notified about a forum or about a thread * @param string $content does the user want to be notified about a forum or about a thread
* @param int $id the id of the forum or thread * @param int $id the id of the forum or thread
* @param bool $addOnly
* @param array $userInfo
* @param array $courseInfo
* *
* @return string language variable * @return string language variable
* *
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
* * @author Julio Montoya
* @version May 2008, dokeos 1.8.5
* *
* @since May 2008, dokeos 1.8.5 * @since May 2008 v1.8.5
*/ */
function set_notification($content, $id, $add_only = false) function set_notification($content, $id, $addOnly = false, $userInfo = [], $courseInfo = [])
{ {
$_user = api_get_user_info(); $userInfo = empty($userInfo) ? api_get_user_info() : $userInfo;
$courseInfo = empty($courseInfo) ? api_get_course_info() : $courseInfo;
$id = (int) $id;
if (empty($userInfo) || empty($courseInfo) || empty($id) || empty($content)) {
return false;
}
// Database table definition // Database table definition
$table_notification = Database::get_course_table(TABLE_FORUM_NOTIFICATION); $table_notification = Database::get_course_table(TABLE_FORUM_NOTIFICATION);
$course_id = api_get_course_int_id(); $course_id = $courseInfo['real_id'];
// Which database field do we have to store the id in? // Which database field do we have to store the id in?
if ($content == 'forum') { $field = 'thread_id';
$database_field = 'forum_id'; if ($content === 'forum') {
} else { $field = 'forum_id';
$database_field = 'thread_id';
} }
$userId = $userInfo['user_id'];
// First we check if the notification is already set for this. // First we check if the notification is already set for this.
$sql = "SELECT * FROM $table_notification $sql = "SELECT * FROM $table_notification
WHERE WHERE
c_id = $course_id AND c_id = $course_id AND
$database_field = '".Database::escape_string($id)."' AND $field = $id AND
user_id = '".intval($_user['user_id'])."'"; user_id = $userId ";
$result = Database::query($sql); $result = Database::query($sql);
$total = Database::num_rows($result); $total = Database::num_rows($result);
error_log($total);
// If the user did not indicate that (s)he wanted to be notified already // If the user did not indicate that (s)he wanted to be notified already
// then we store the notification request (to prevent double notification requests). // then we store the notification request (to prevent double notification requests).
if ($total <= 0) { if ($total <= 0) {
$sql = "INSERT INTO $table_notification (c_id, $database_field, user_id) $sql = "INSERT INTO $table_notification (c_id, $field, user_id)
VALUES (".$course_id.", '".Database::escape_string($id)."','".intval($_user['user_id'])."')"; VALUES ($course_id, '$id','$userId')";
Database::query($sql); Database::query($sql);
Session::erase('forum_notification'); Session::erase('forum_notification');
getNotificationsPerUser(0, true); getNotificationsPerUser(0, true);
error_log($sql);
return get_lang('YouWillBeNotifiedOfNewPosts'); return get_lang('YouWillBeNotifiedOfNewPosts');
} else { } else {
if (!$add_only) { if (!$addOnly) {
$sql = "DELETE FROM $table_notification $sql = "DELETE FROM $table_notification
WHERE WHERE
c_id = $course_id AND c_id = $course_id AND
$database_field = '".Database::escape_string($id)."' AND $field = $id AND
user_id = '".intval($_user['user_id'])."'"; user_id = $userId ";
Database::query($sql); Database::query($sql);
Session::erase('forum_notification'); Session::erase('forum_notification');
getNotificationsPerUser(0, true); getNotificationsPerUser(0, true);
@ -5692,6 +5718,7 @@ function set_notification($content, $id, $add_only = false)
* @return array returns * @return array returns
* *
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
* @author Julio Montoya
* *
* @version May 2008, dokeos 1.8.5 * @version May 2008, dokeos 1.8.5
* *
@ -5705,18 +5732,19 @@ function get_notifications($content, $id)
$course_id = api_get_course_int_id(); $course_id = api_get_course_int_id();
// Which database field contains the notification? // Which database field contains the notification?
if ($content == 'forum') { $field = 'thread_id';
$database_field = 'forum_id'; if ($content === 'forum') {
} else { $field = 'forum_id';
$database_field = 'thread_id';
} }
$id = (int) $id;
$sql = "SELECT user.user_id, user.firstname, user.lastname, user.email, user.user_id user $sql = "SELECT user.user_id, user.firstname, user.lastname, user.email, user.user_id user
FROM $table_users user, $table_notification notification FROM $table_users user, $table_notification notification
WHERE WHERE
notification.c_id = $course_id AND user.active = 1 AND notification.c_id = $course_id AND user.active = 1 AND
user.user_id = notification.user_id AND user.user_id = notification.user_id AND
notification.$database_field= '".Database::escape_string($id)."'"; notification.$field = $id ";
$result = Database::query($sql); $result = Database::query($sql);
$return = []; $return = [];
@ -5746,20 +5774,7 @@ function get_notifications($content, $id)
*/ */
function send_notifications($forum_id = 0, $thread_id = 0, $post_id = 0) function send_notifications($forum_id = 0, $thread_id = 0, $post_id = 0)
{ {
//$_course = api_get_course_info();
/*$forumCourseId = api_get_configuration_value('global_forums_course_id');
if (!empty($forumCourseId)) {
if ($_course['real_id'] == $forumCourseId) {
return false;
}
}*/
$forum_id = (int) $forum_id; $forum_id = (int) $forum_id;
// The content of the mail
//$thread_link = api_get_path(WEB_CODE_PATH).'forum/viewthread.php?'.api_get_cidreq().'&forum='.$forum_id.'&thread='.$thread_id;
// Users who subscribed to the forum // Users who subscribed to the forum
if ($forum_id != 0) { if ($forum_id != 0) {
$users_to_be_notified_by_forum = get_notifications('forum', $forum_id); $users_to_be_notified_by_forum = get_notifications('forum', $forum_id);
@ -5768,8 +5783,6 @@ function send_notifications($forum_id = 0, $thread_id = 0, $post_id = 0)
} }
$current_thread = get_thread_information($forum_id, $thread_id); $current_thread = get_thread_information($forum_id, $thread_id);
//$current_forum = get_forum_information($current_thread['forum_id']);
//$subject = get_lang('NewForumPost').' - '.$_course['official_code'].' - '.$current_forum['forum_title'].' - '.$current_thread['thread_title'];
// User who subscribed to the thread // User who subscribed to the thread
if ($thread_id != 0) { if ($thread_id != 0) {

@ -62,9 +62,9 @@ if (api_is_in_gradebook()) {
$search_forum = isset($_GET['search']) ? Security::remove_XSS($_GET['search']) : ''; $search_forum = isset($_GET['search']) ? Security::remove_XSS($_GET['search']) : '';
/* ACTIONS */ /* ACTIONS */
$actions = isset($_GET['action']) ? $_GET['action'] : ''; $action = isset($_GET['action']) ? $_GET['action'] : '';
if ($actions === 'add') { if ($action === 'add') {
switch ($_GET['content']) { switch ($_GET['content']) {
case 'forum': case 'forum':
$interbreadcrumb[] = [ $interbreadcrumb[] = [
@ -102,12 +102,12 @@ $form_count = 0;
$formContent = ''; $formContent = '';
if (api_is_allowed_to_edit(false, true)) { if (api_is_allowed_to_edit(false, true)) {
//if is called from a learning path lp_id //if is called from a learning path lp_id
$lp_id = isset($_REQUEST['lp_id']) ? intval($_REQUEST['lp_id']) : null; $lp_id = isset($_REQUEST['lp_id']) ? (int) $_REQUEST['lp_id'] : null;
$formContent = handle_forum_and_forumcategories($lp_id); $formContent = handle_forum_and_forumcategories($lp_id);
} }
// Notification // Notification
if ($actions == 'notify' && isset($_GET['content']) && isset($_GET['id'])) { if ($action == 'notify' && isset($_GET['content']) && isset($_GET['id'])) {
if (api_get_session_id() != 0 && if (api_get_session_id() != 0 &&
api_is_allowed_to_session_edit(false, true) == false api_is_allowed_to_session_edit(false, true) == false
) { ) {
@ -120,14 +120,11 @@ if ($actions == 'notify' && isset($_GET['content']) && isset($_GET['id'])) {
get_whats_new(); get_whats_new();
$whatsnew_post_info = Session::read('whatsnew_post_info'); $whatsnew_post_info = Session::read('whatsnew_post_info');
/* TRACKING */
Event::event_access_tool(TOOL_FORUM); Event::event_access_tool(TOOL_FORUM);
$logInfo = [ $logInfo = [
'tool' => TOOL_FORUM, 'tool' => TOOL_FORUM,
'tool_id' => 0, 'action' => !empty($action) ? $action : 'list-category',
'tool_id_detail' => 0,
'action' => !empty($actions) ? $actions : 'list-category',
'action_details' => isset($_GET['content']) ? $_GET['content'] : '', 'action_details' => isset($_GET['content']) ? $_GET['content'] : '',
]; ];
Event::registerLog($logInfo); Event::registerLog($logInfo);
@ -557,7 +554,7 @@ if (is_array($forumCategories)) {
} }
} }
if (!api_is_anonymous() && api_is_allowed_to_session_edit(false, true) && $hideNotifications == false) { if ($hideNotifications == false && !api_is_anonymous() && api_is_allowed_to_session_edit(false, true)) {
$toolActions .= '<a href="'.api_get_self().'?'.api_get_cidreq() $toolActions .= '<a href="'.api_get_self().'?'.api_get_cidreq()
.'&action=notify&content=forum&id='.$forum['forum_id'].'">' .'&action=notify&content=forum&id='.$forum['forum_id'].'">'
.Display::return_icon($iconnotify, get_lang('NotifyMe'), null, ICON_SIZE_SMALL) .Display::return_icon($iconnotify, get_lang('NotifyMe'), null, ICON_SIZE_SMALL)

@ -731,6 +731,16 @@ class CourseManager
); );
} }
$subscribe = (int) api_get_course_setting('subscribe_users_to_forum_notifications', $courseCode);
if ($subscribe === 1) {
require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php';
$forums = get_forums(0, $courseCode, true, $sessionId);
foreach ($forums as $forum) {
$forumId = $forum['iid'];
set_notification('forum', $forumId, false, $userInfo, $courseInfo);
}
}
// Add event to the system log // Add event to the system log
Event::addEvent( Event::addEvent(
LOG_SUBSCRIBE_USER_TO_COURSE, LOG_SUBSCRIBE_USER_TO_COURSE,
@ -5277,6 +5287,7 @@ class CourseManager
'student_delete_own_publication', 'student_delete_own_publication',
'hide_forum_notifications', 'hide_forum_notifications',
'quiz_question_limit_per_day', 'quiz_question_limit_per_day',
'subscribe_users_to_forum_notifications',
]; ];
$courseModels = ExerciseLib::getScoreModels(); $courseModels = ExerciseLib::getScoreModels();

Loading…
Cancel
Save