diff --git a/main/auth/external_login/newUser.ldap.php b/main/auth/external_login/newUser.ldap.php index 7eb6979e27..583ee09665 100755 --- a/main/auth/external_login/newUser.ldap.php +++ b/main/auth/external_login/newUser.ldap.php @@ -63,7 +63,7 @@ if ($ldap_user !== false) { } Event::eventLogin($chamiloUser->getId()); - MessageManager::sendNotificationByRegisteredUser($chamiloUser); + MessageManager::sendNotificationOfNewRegisteredUser($chamiloUser); } } else { $loginFailed = true; diff --git a/main/auth/inscription.php b/main/auth/inscription.php index 3e75330f89..e65f397663 100755 --- a/main/auth/inscription.php +++ b/main/auth/inscription.php @@ -834,50 +834,8 @@ if ($form->validate()) { sent a mail to the platform admin and exit the page.*/ if (api_get_setting('allow_registration') === 'approval') { // 1. Send mail to all platform admin - $emailsubject = get_lang('ApprovalForNewAccount').': '.$values['username']; - $emailbody = get_lang('ApprovalForNewAccount')."\n"; - $emailbody .= get_lang('UserName').': '.$values['username']."\n"; - - if (api_is_western_name_order()) { - $emailbody .= get_lang('FirstName').': '.$values['firstname']."\n"; - $emailbody .= get_lang('LastName').': '.$values['lastname']."\n"; - } else { - $emailbody .= get_lang('LastName').': '.$values['lastname']."\n"; - $emailbody .= get_lang('FirstName').': '.$values['firstname']."\n"; - } - $emailbody .= get_lang('Email').': '.$values['email']."\n"; - $emailbody .= get_lang('Status').': '.$values['status']."\n\n"; - - $url_edit = Display::url( - api_get_path(WEB_CODE_PATH).'admin/user_edit.php?user_id='.$user_id, - api_get_path(WEB_CODE_PATH).'admin/user_edit.php?user_id='.$user_id - ); - - $emailbody .= get_lang('ManageUser').": $url_edit"; - - if (api_get_configuration_value('send_inscription_notification_to_general_admin_only')) { - $email = api_get_setting('emailAdministrator'); - $firtname = api_get_setting('administratorSurname'); - $lastname = api_get_setting('administratorName'); - - api_mail_html("$firtname $lastname", $email, $emailsubject, $emailbody); - } else { - $admins = UserManager::get_all_administrators(); - foreach ($admins as $admin_info) { - MessageManager::send_message( - $admin_info['user_id'], - $emailsubject, - $emailbody, - [], - [], - null, - null, - null, - null, - $user_id - ); - } - } + $chamiloUser = api_get_user_entity($user_id); + MessageManager::sendNotificationOfNewRegisteredUserApproval($chamiloUser); // 2. set account inactive UserManager::disable($user_id); diff --git a/main/inc/lib/MailTemplateManager.php b/main/inc/lib/MailTemplateManager.php index 643e951537..809a88f2e4 100644 --- a/main/inc/lib/MailTemplateManager.php +++ b/main/inc/lib/MailTemplateManager.php @@ -209,4 +209,25 @@ class MailTemplateManager extends Model return false; } + /** + * Gets a custom mail template by the name of the template it replaces + * @param string $templateType Name of the template file it replaces + * @return string + */ + public function getTemplateByType($templateType) + { + if (empty($templateType)) { + return ''; + } + $result = Database::select( + 'template', + $this->table, + ['where' => ['type = ? ' => $templateType, ' AND url_id = ? ' => api_get_current_access_url_id()]], + 'first' + ); + if (empty($result)) { + return ''; + } + return $result['template']; + } } diff --git a/main/inc/lib/message.lib.php b/main/inc/lib/message.lib.php index da475289ad..6eea65d685 100755 --- a/main/inc/lib/message.lib.php +++ b/main/inc/lib/message.lib.php @@ -2655,7 +2655,7 @@ class MessageManager * * @param User $user */ - public static function sendNotificationByRegisteredUser(User $user) + public static function sendNotificationOfNewRegisteredUser(User $user) { $tplMailBody = new Template( null, @@ -2696,6 +2696,88 @@ class MessageManager } } + /** + * Send a notification to all admins when a new user is registered + * while the approval method is used for users registration + * + * @param User $user + */ + public static function sendNotificationOfNewRegisteredUserApproval(User $user) + { + $tplMailBody = new Template( + null, + false, + false, + false, + false, + false, + false + ); + $tplMailBody->assign('user', $user); + $tplMailBody->assign('is_western_name_order', api_is_western_name_order()); + $userId = $user->getId(); + $url_edit = Display::url( + api_get_path(WEB_CODE_PATH).'admin/user_edit.php?user_id='.$userId, + api_get_path(WEB_CODE_PATH).'admin/user_edit.php?user_id='.$userId + ); + $tplMailBody->assign( + 'manageUrl', + $url_edit + ); + // Get extra field values for this user and reformat the array + $extraFieldValues = new ExtraFieldValue('user'); + $userExtraFields = $extraFieldValues->getAllValuesByItem($userId); + $values = []; + foreach($userExtraFields as $field => $value) { + $values[$value['variable']] = $value['value']; + } + $tplMailBody->assign( + 'extra', + $values + ); + $layoutContent = ''; + $emailbody = ''; + if (api_get_configuration_value('mail_template_system') == true) { + $mailTemplateManager = new MailTemplateManager(); + $templateText = $mailTemplateManager->getTemplateByType('new_user_mail_to_admin_approval.tpl'); + if (empty($templateText)) { + } else { + // custom procedure to load a template as a string (doesn't use cache so may slow down) + $template = $tplMailBody->twig->createTemplate($templateText); + $emailbody = $template->render($tplMailBody->params); + } + } + if (empty($emailbody)) { + $layoutContent = $tplMailBody->get_template('mail/new_user_mail_to_admin_approval.tpl'); + $emailbody = $tplMailBody->fetch($layoutContent); + } + + $emailsubject = '['.get_lang('ApprovalForNewAccount').'] '.$user->getUsername(); + + if (api_get_configuration_value('send_inscription_notification_to_general_admin_only')) { + $email = api_get_setting('emailAdministrator'); + $firstname = api_get_setting('administratorSurname'); + $lastname = api_get_setting('administratorName'); + api_mail_html("$firstname $lastname", $email, $emailsubject, $emailbody); + } else { + $admins = UserManager::get_all_administrators(); + foreach ($admins as $admin_info) { + self::send_message( + $admin_info['user_id'], + $emailsubject, + $emailbody, + [], + [], + null, + null, + null, + null, + $userId + ); + } + } + } + /** * Get the error log from failed mailing * This assumes a complex setup where you have a cron script regularly copying the mail queue log diff --git a/main/template/default/mail/new_user_mail_to_admin_approval.tpl b/main/template/default/mail/new_user_mail_to_admin_approval.tpl new file mode 100644 index 0000000000..628d23d65a --- /dev/null +++ b/main/template/default/mail/new_user_mail_to_admin_approval.tpl @@ -0,0 +1,14 @@ +
{{ 'ManageUser'|get_lang }}: {{ manageUrl }}