Skip to content

Commit

Permalink
Message: Add optional email notifications for user subscriptions - refs
Browse files Browse the repository at this point in the history
  • Loading branch information
christianbeeznest committed Oct 23, 2024
1 parent c609784 commit c4cfef2
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 111 deletions.
12 changes: 12 additions & 0 deletions public/main/inc/lib/course.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,18 @@ public static function subscribeUser(
)
);

$subject = get_lang('You have been enrolled in the course').' '.$course->getTitle();
$message = sprintf(get_lang('Hello %s, you have been enrolled in the course %s.'), UserManager::formatUserFullName($user, true), $course->getTitle());

MessageManager::send_message_simple(
$userId,
$subject,
$message,
api_get_user_id(),
false,
true
);

$send = (int) api_get_course_setting('email_alert_to_teacher_on_new_user_in_course', $course);

if (1 === $send) {
Expand Down
46 changes: 37 additions & 9 deletions public/main/inc/lib/sessionmanager.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2615,7 +2615,9 @@ public static function subscribe_users_to_session_course(
$user_list,
$courseId,
$session_id,
['visibility' => $session_visibility]
['visibility' => $session_visibility],
true,
true
);
}

Expand Down Expand Up @@ -9913,15 +9915,16 @@ public static function insertUsersInCourse(
int $courseId,
int $sessionId,
array $relationInfo = [],
bool $updateSession = true
bool $updateSession = true,
bool $sendNotification = false
) {
$em = Database::getManager();
$course = api_get_course_entity($courseId);
$session = api_get_session_entity($sessionId);


$relationInfo = array_merge(['visibility' => 0, 'status' => Session::STUDENT], $relationInfo);

$usersToInsert = [];
foreach ($studentIds as $studentId) {
$user = api_get_user_entity($studentId);
$session->addUserInCourse($relationInfo['status'], $user, $course)
Expand All @@ -9930,15 +9933,40 @@ public static function insertUsersInCourse(
Event::logUserSubscribedInCourseSession($user, $course, $session);

if ($updateSession) {
$session->addUserInSession(Session::STUDENT, $user);
if (!$session->hasUserInSession($user, Session::STUDENT)) {
$session->addUserInSession(Session::STUDENT, $user);
}
}

$usersToInsert[] = $studentId;
}

try {
$em->persist($session);
$em->flush();
} catch (\Exception $e) {
error_log("Error executing flush: " . $e->getMessage());
$em->persist($session);
$em->flush();

if ($sendNotification && !empty($usersToInsert)) {
foreach ($usersToInsert as $userId) {
$user = api_get_user_entity($userId);
$courseTitle = $course->getTitle();
$sessionTitle = $session->getTitle();

$subject = sprintf(get_lang('You have been enrolled in the course %s for the session %s'), $courseTitle, $sessionTitle);
$message = sprintf(
get_lang('Hello %s, you have been enrolled in the course %s for the session %s.'),
UserManager::formatUserFullName($user, true),
$courseTitle,
$sessionTitle
);

MessageManager::send_message_simple(
$userId,
$subject,
$message,
api_get_user_id(),
false,
true
);
}
}
}

Expand Down
178 changes: 76 additions & 102 deletions public/main/session/add_users_to_session_course.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,9 @@ function change_select(val) {
SessionManager::subscribe_users_to_session_course(
$UserList,
$id_session,
$courseInfo['code']
$courseInfo['code'],
SESSION_VISIBLE_READ_ONLY,
true
);

Display::addFlash(Display::return_message(get_lang('Update successful')));
Expand Down Expand Up @@ -661,12 +663,12 @@ function change_select(val) {
if ('multiple' === $add_type) {
if (is_array($extra_field_list)) {
if (is_array($new_field_list) && count($new_field_list) > 0) {
echo '<h3>'.get_lang('Filter users').'</h3>';
echo '<h3 class="text-xl font-bold mb-4">'.get_lang('Filter users').'</h3>';
foreach ($new_field_list as $new_field) {
echo $new_field['name'];
echo '<label class="block text-gray-700 font-semibold mb-2">'.$new_field['name'].'</label>';
$varname = 'field_'.$new_field['variable'];
$fieldtype = $new_field['type'];
echo '&nbsp;<select name="'.$varname.'">';
echo '<select name="'.$varname.'" class="w-full border border-gray-300 p-2 rounded mb-4">';
echo '<option value="0">--'.get_lang('Select').'--</option>';
foreach ($new_field['data'] as $option) {
$checked = '';
Expand All @@ -693,7 +695,7 @@ function change_select(val) {
echo $extraHidden;
echo '&nbsp;&nbsp;';
}
echo '<input type="button" value="'.get_lang('Filter').'" onclick="validate_filter()" />';
echo '<input type="button" value="'.get_lang('Filter').'" class="btn btn-primary" onclick="validate_filter()" />';
echo '<br /><br />';
}
}
Expand All @@ -702,124 +704,95 @@ function change_select(val) {
<input type="hidden" name="form_sent" value="1"/>
<input type="hidden" name="add_type"/>

<div id="multiple-add-session" class="grid grid-cols-3">
<div class="col-md-4">
<div class="form-group">
<label><?php echo get_lang('Portal users list'); ?> </label>
<div id="multiple-add-session" class="flex justify-between items-start my-5">
<div class="w-1/3 p-2">
<label class="block text-gray-700 font-semibold mb-2"><?php echo get_lang('Portal users list'); ?> </label>
<?php
if (!('multiple' == $add_type)) {
?>
<input type="text" id="user_to_add" onkeyup="xajax_search_users(this.value,'single')"
class="w-full border border-gray-300 p-2 rounded"/>
<div id="ajax_list_users_single" class="mt-2"></div>
<?php
if (!('multiple' == $add_type)) {
?>
<input type="text" id="user_to_add" onkeyup="xajax_search_users(this.value,'single')"
class="form-control"/>
<div id="ajax_list_users_single" class="select-list-ajax"></div>
<?php
} else {
?>
<div id="ajax_list_users_multiple">
<select id="origin_users" name="nosessionUsersList[]" multiple="multiple" size="15"
class="form-control">
<?php
foreach ($nosessionUsersList as $uid => $enreg) {
?>
<option value="<?php echo $uid; ?>" <?php if (in_array($uid, $UserList)) {
echo 'selected="selected"';
} ?>>
<?php
$personName = $enreg['ln'].' '.$enreg['fn'].' ('.$enreg['un'].') '
.$enreg['official_code'];
} else {
?>
<div id="ajax_list_users_multiple">
<select id="origin_users" name="nosessionUsersList[]" multiple="multiple" size="15"
class="w-full border border-gray-300 p-2 rounded">
<?php
foreach ($nosessionUsersList as $uid => $enreg) {
?>
<option value="<?php echo $uid; ?>" <?php if (in_array($uid, $UserList)) {
echo 'selected="selected"';
} ?>>
<?php
$personName = $enreg['ln'].' '.$enreg['fn'].' ('.$enreg['un'].') '
.$enreg['official_code'];
if ($showOfficialCode) {
$officialCode =
!empty($enreg['official_code']) ? $enreg['official_code'].' - '
: '? - ';
$personName =
$officialCode.$enreg['ln'].' '.$enreg['fn'].' ('.$enreg['un'].')';
$officialCode = !empty($enreg['official_code']) ? $enreg['official_code'].' - '
: '? - ';
$personName = $officialCode.$enreg['ln'].' '.$enreg['fn'].' ('.$enreg['un'].')';
}
echo $personName; ?>
</option>
<?php
} ?>
</select>
</div>
<?php
}
unset($nosessionUsersList);
?>
</div>
</option>
<?php
} ?>
</select>
</div>
<?php
}
unset($nosessionUsersList);
?>
</div>

<div class="col-md-4">
<div class="flex flex-col items-center justify-center space-y-3 mt-4">
<?php if ('multiple' == $add_type) {
?>
?>
<?php echo get_lang('First letter (last name)'); ?> :
<select id="first_letter_user" name="firstLetterUser" onchange="change_select(this.value);">
<select class="mb-4" id="first_letter_user" name="firstLetterUser" onchange="change_select(this.value);">
<option value="%">--</option>
<?php
echo Display:: get_alphabet_options(); ?>
</select>
<br/>
<br/>
<?php
} ?>
<div class="control-course">
<?php
if ($ajax_search) {
?>
<div class="separate-action">
<button name="remove_user" class="btn btn--primary" type="button"
onclick="remove_item(document.getElementById('destination_users'))">
<em class="fa fa-chevron-left"></em>
</button>
</div>
<?php
} else {
?>
<div class="separate-action">
<button name="add_user" class="btn btn--primary" type="button"
onclick="moveItem(document.getElementById('origin_users'), document.getElementById('destination_users'))"
onclick="moveItem(document.getElementById('origin_users'), document.getElementById('destination_users'))">
<em class="fa fa-chevron-right"></em>
</button>
</div>
<div class="separate-action">
<button name="remove_user" class="btn btn--primary" type="button"
onclick="moveItem(document.getElementById('destination_users'), document.getElementById('origin_users'))"
onclick="moveItem(document.getElementById('destination_users'), document.getElementById('origin_users'))">
<em class="fa fa-chevron-left"></em>
</button>
</div>

<?php
}
if (!empty($addProcess)) {
echo '<button name="next" class="btn btn--success" type="button" value="" onclick="valide()" >'
.get_lang('Finish session creation').'</button>';
} else {
echo '<button name="next" class="btn btn--success" type="button" value="" onclick="valide()" >'
.get_lang('Subscribe users to this session').'</button>';
}
?>
</div>
} ?>
<?php if ($ajax_search) { ?>
<button name="remove_user" class="btn btn--primary mb-4"
type="button" onclick="remove_item(document.getElementById('destination_users'))">
<em class="pi pi-chevron-left"></em>
</button>
<?php } else { ?>
<button name="add_user" class="btn btn--primary mb-4"
type="button" onclick="moveItem(document.getElementById('origin_users'), document.getElementById('destination_users'))">
<em class="pi pi-chevron-right"></em>
</button>
<button name="remove_user" class="btn btn--primary mb-4"
type="button" onclick="moveItem(document.getElementById('destination_users'), document.getElementById('origin_users'))">
<em class="pi pi-chevron-left"></em>
</button>
<?php } ?>
<button name="next" class="btn btn--success mb-4" type="button"
onclick="valide()">
<?php echo (!empty($addProcess)) ? get_lang('Finish session creation') : get_lang('Subscribe users to this session'); ?>
</button>
</div>

<div class="col-md-4">
<label><?php echo get_lang('List of users registered in this session'); ?> :</label>
<div class="w-1/3 p-2">
<label class="block text-gray-700 font-semibold mb-2"><?php echo get_lang('List of users registered in this session'); ?>:</label>
<select id="destination_users" name="sessionUsersList[]" multiple="multiple" size="15"
class="form-control">
class="w-full border border-gray-300 p-2 rounded">
<?php
foreach ($sessionUsersList as $enreg) {
?>
<option value="<?php echo $enreg['id']; ?>">
<?php
$personName = $enreg['lastname'].' '.$enreg['firstname'].' ('.$enreg['username'].') '
.$enreg['official_code'];
if ($showOfficialCode) {
$officialCode =
if ($showOfficialCode) {
$officialCode =
!empty($enreg['official_code']) ? $enreg['official_code'].' - ' : '? - ';
$personName =
$officialCode.$enreg['lastname'].' '.$enreg['firstname'].' ('.$enreg['username']
.')';
}
echo $personName; ?>
$personName =
$officialCode.$enreg['lastname'].' '.$enreg['firstname'].' ('.$enreg['username'].')';
}
echo $personName; ?>
</option>
<?php
}
Expand Down Expand Up @@ -865,8 +838,9 @@ function mysort(a, b) {

function valide() {
var options = document.getElementById('destination_users').options;
for (i = 0; i < options.length; i++)
for (i = 0; i < options.length; i++) {
options[i].selected = true;
}
document.forms.formulaire.submit();
}

Expand Down
10 changes: 10 additions & 0 deletions src/CoreBundle/Entity/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,16 @@ public function hasUserAsGeneralCoach(User $user): bool
return $this->users->matching($criteria)->count() > 0;
}

public function hasUserInSession(User $user, int $relationType): bool
{
$criteria = Criteria::create()
->where(Criteria::expr()->eq('user', $user))
->andWhere(Criteria::expr()->eq('relationType', $relationType));

return $this->users->matching($criteria)->count() > 0;
}


public function addGeneralCoach(User $coach): self
{
return $this->addUserInSession(self::GENERAL_COACH, $coach);
Expand Down

0 comments on commit c4cfef2

Please sign in to comment.