diff --git a/main/inc/lib/plugin.class.php b/main/inc/lib/plugin.class.php index 0b509ad622..dbb8ac0ac6 100755 --- a/main/inc/lib/plugin.class.php +++ b/main/inc/lib/plugin.class.php @@ -295,6 +295,21 @@ class Plugin $attributes ); break; + case 'user': + $options = []; + if (!empty($value)) { + $userInfo = api_get_user_info($value); + if ($userInfo) { + $options[$value] = $userInfo['complete_name']; + } + } + $result->addSelectAjax( + $name, + [$this->get_lang($name), $help], + $options, + ['url' => api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?a=get_user_like'] + ); + break; } } @@ -303,7 +318,6 @@ class Plugin $checkboxGroup, null, ['', $help] - //[$this->get_lang('sms_types'), $help] ); } $result->setDefaults($defaults); diff --git a/plugin/pausetraining/PauseTraining.php b/plugin/pausetraining/PauseTraining.php index b4c6fedb26..d1ada60802 100644 --- a/plugin/pausetraining/PauseTraining.php +++ b/plugin/pausetraining/PauseTraining.php @@ -4,8 +4,6 @@ class PauseTraining extends Plugin { - public $isCoursePlugin = false; - protected function __construct() { parent::__construct( @@ -15,6 +13,7 @@ class PauseTraining extends Plugin 'tool_enable' => 'boolean', 'allow_users_to_edit_pause_formation' => 'boolean', 'cron_alert_users_if_inactive_days' => 'text', // Example: "5" or "5,10,15" + 'sender_id' => 'user', ] ); } @@ -94,64 +93,80 @@ class PauseTraining extends Plugin public function runCron() { $enable = $this->get('tool_enable'); + $senderId = $this->get('sender_id'); $enableDays = $this->get('cron_alert_users_if_inactive_days'); - if ($enable && !empty($enableDays)) { - $enableDaysList = explode(',', $enableDays); - rsort($enableDaysList); - - $loginTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS); - $userTable = Database::get_main_table(TABLE_MAIN_USER); - $now = api_get_utc_datetime(); - $usersNotificationPerDay = []; - $users = []; - foreach ($enableDaysList as $day) { - $day = (int) $day; - - $sql = "SELECT - stats_login.user_id, - MAX(stats_login.login_course_date) max_date - FROM $loginTable stats_login - INNER JOIN $userTable u - ON (u.id = stats_login.user_id) - WHERE - u.status <> ".ANONYMOUS." AND - u.active = 1 - GROUP BY stats_login.user_id - HAVING DATE_SUB('$now', INTERVAL '$day' DAY) > max_date "; - - $rs = Database::query($sql); - while ($user = Database::fetch_array($rs)) { - $userId = $user['user_id']; - - if (in_array($userId, $users)) { - continue; - } - $users[] = $userId; - $usersNotificationPerDay[$day][] = $userId; + if ('true' !== $enable) { + echo 'Plugin not enabled'; + return false; + } + + if (empty($senderId)) { + echo 'Sender id not configured'; + return false; + } + + $senderInfo = api_get_user_info($senderId); + + if (empty($senderInfo)) { + echo "Sender #$senderId not found"; + return false; + } + + $enableDaysList = explode(',', $enableDays); + rsort($enableDaysList); + + $loginTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS); + $userTable = Database::get_main_table(TABLE_MAIN_USER); + $now = api_get_utc_datetime(); + $usersNotificationPerDay = []; + $users = []; + foreach ($enableDaysList as $day) { + $day = (int) $day; + + $sql = "SELECT + stats_login.user_id, + MAX(stats_login.login_course_date) max_date + FROM $loginTable stats_login + INNER JOIN $userTable u + ON (u.id = stats_login.user_id) + WHERE + u.status <> ".ANONYMOUS." AND + u.active = 1 + GROUP BY stats_login.user_id + HAVING DATE_SUB('$now', INTERVAL '$day' DAY) > max_date "; + + $rs = Database::query($sql); + while ($user = Database::fetch_array($rs)) { + $userId = $user['user_id']; + + if (in_array($userId, $users)) { + continue; } + $users[] = $userId; + $usersNotificationPerDay[$day][] = $userId; } + } - if (!empty($usersNotificationPerDay)) { - ksort($usersNotificationPerDay); - $extraFieldValue = new ExtraFieldValue('user'); - foreach ($usersNotificationPerDay as $day => $userList) { - $template = new Template(); - $title = sprintf($this->get_lang('InactivityXDays'), $day); - - foreach ($userList as $userId) { - $userInfo = api_get_user_info($userId); - $pause = $extraFieldValue->get_values_by_handler_and_field_variable($userId, 'pause_formation'); - if (!empty($pause) && isset($pause['value']) && 1 == $pause['value']) { - // Skip user because he paused his formation. - continue; - } - - $template->assign('days', $day); - $template->assign('user', $userInfo); - $content = $template->fetch('pausetraining/view/notification_content.tpl'); - MessageManager::send_message($userId, $title, $content); + if (!empty($usersNotificationPerDay)) { + ksort($usersNotificationPerDay); + $extraFieldValue = new ExtraFieldValue('user'); + foreach ($usersNotificationPerDay as $day => $userList) { + $template = new Template(); + $title = sprintf($this->get_lang('InactivityXDays'), $day); + + foreach ($userList as $userId) { + $userInfo = api_get_user_info($userId); + $pause = $extraFieldValue->get_values_by_handler_and_field_variable($userId, 'pause_formation'); + if (!empty($pause) && isset($pause['value']) && 1 == $pause['value']) { + // Skip user because he paused his formation. + continue; } + + $template->assign('days', $day); + $template->assign('user', $userInfo); + $content = $template->fetch('pausetraining/view/notification_content.tpl'); + MessageManager::send_message_simple($userId, $title, $content, $senderId); } } } diff --git a/plugin/pausetraining/lang/english.php b/plugin/pausetraining/lang/english.php index 4dcf71bdfa..aa64fe3294 100644 --- a/plugin/pausetraining/lang/english.php +++ b/plugin/pausetraining/lang/english.php @@ -15,3 +15,4 @@ $strings['InactivityXDays'] = 'Inactivity for %s days'; $strings['YouAreConnectedInPlatformXLinkXSinceXDays'] = ' We have noticed that you have not connected to the platform %s (%s) for %s days. This is an automatic message to remind you of your activity.'; +$strings['sender_id'] = 'User that will send the cron emails';