Actualización
This commit is contained in:
350
plugin/pausetraining/PauseTraining.php
Normal file
350
plugin/pausetraining/PauseTraining.php
Normal file
@@ -0,0 +1,350 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
class PauseTraining extends Plugin
|
||||
{
|
||||
protected function __construct()
|
||||
{
|
||||
parent::__construct(
|
||||
'0.1',
|
||||
'Julio Montoya',
|
||||
[
|
||||
'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',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public static function create()
|
||||
{
|
||||
static $result = null;
|
||||
|
||||
return $result ? $result : $result = new self();
|
||||
}
|
||||
|
||||
public function updateUserPauseTraining($userId, $values)
|
||||
{
|
||||
$userInfo = api_get_user_info($userId);
|
||||
if (empty($userInfo)) {
|
||||
throw new Exception("User #$userId does not exists");
|
||||
}
|
||||
|
||||
$variables = [
|
||||
'pause_formation',
|
||||
'start_pause_date',
|
||||
'end_pause_date',
|
||||
'disable_emails',
|
||||
];
|
||||
|
||||
$valuesToUpdate = [
|
||||
'item_id' => $userId,
|
||||
];
|
||||
|
||||
// Check if variables exist.
|
||||
foreach ($variables as $variable) {
|
||||
if (!isset($values[$variable])) {
|
||||
throw new Exception("Variable '$variable' is missing. Cannot updated.");
|
||||
}
|
||||
|
||||
$valuesToUpdate['extra_'.$variable] = $values[$variable];
|
||||
}
|
||||
|
||||
// Clean variables
|
||||
$pause = (int) $valuesToUpdate['extra_pause_formation'];
|
||||
if (empty($pause)) {
|
||||
$valuesToUpdate['extra_pause_formation'] = 0;
|
||||
} else {
|
||||
$valuesToUpdate['extra_pause_formation'] = [];
|
||||
$valuesToUpdate['extra_pause_formation']['extra_pause_formation'] = $pause;
|
||||
}
|
||||
|
||||
$notification = (int) $valuesToUpdate['extra_disable_emails'];
|
||||
if (empty($notification)) {
|
||||
$valuesToUpdate['extra_disable_emails'] = 0;
|
||||
} else {
|
||||
$valuesToUpdate['extra_disable_emails'] = [];
|
||||
$valuesToUpdate['extra_disable_emails']['extra_disable_emails'] = $notification;
|
||||
}
|
||||
|
||||
$check = DateTime::createFromFormat('Y-m-d H:i', $valuesToUpdate['extra_start_pause_date']);
|
||||
|
||||
if (false === $check) {
|
||||
throw new Exception("start_pause_date is not valid date time format should be: Y-m-d H:i");
|
||||
}
|
||||
|
||||
$check = DateTime::createFromFormat('Y-m-d H:i', $valuesToUpdate['extra_end_pause_date']);
|
||||
if (false === $check) {
|
||||
throw new Exception("end_pause_date is not valid date time format should be: Y-m-d H:i");
|
||||
}
|
||||
|
||||
if (api_strtotime($valuesToUpdate['extra_start_pause_date']) > api_strtotime($valuesToUpdate['extra_end_pause_date'])) {
|
||||
throw new Exception("end_pause_date must be bigger than start_pause_date");
|
||||
}
|
||||
|
||||
$extraField = new ExtraFieldValue('user');
|
||||
$extraField->saveFieldValues($valuesToUpdate, true, false, [], [], true);
|
||||
|
||||
return (int) $userId;
|
||||
}
|
||||
|
||||
public function runCron($date = '', $isTest = false)
|
||||
{
|
||||
$enable = $this->get('tool_enable');
|
||||
$senderId = $this->get('sender_id');
|
||||
$enableDays = $this->get('cron_alert_users_if_inactive_days');
|
||||
|
||||
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 in Chamilo";
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$enableDaysList = explode(',', $enableDays);
|
||||
rsort($enableDaysList);
|
||||
|
||||
$track = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
|
||||
$login = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
|
||||
$userTable = Database::get_main_table(TABLE_MAIN_USER);
|
||||
$usersNotificationPerDay = [];
|
||||
$now = api_get_utc_datetime();
|
||||
if (!empty($date)) {
|
||||
$now = $date;
|
||||
}
|
||||
|
||||
if ($isTest) {
|
||||
echo "-------------------------------------------".PHP_EOL;
|
||||
echo "----- Testing date $now ----".PHP_EOL;
|
||||
echo "-------------------------------------------".PHP_EOL;
|
||||
}
|
||||
|
||||
$extraFieldValue = new ExtraFieldValue('user');
|
||||
$sql = "SELECT u.id
|
||||
FROM $userTable u
|
||||
WHERE u.status <> ".ANONYMOUS." AND u.active = 1";
|
||||
$rs = Database::query($sql);
|
||||
|
||||
$users = [];
|
||||
while ($user = Database::fetch_array($rs)) {
|
||||
$userId = $user['id'];
|
||||
|
||||
$sql = "SELECT
|
||||
MAX(t.logout_course_date) max_course_date,
|
||||
MAX(l.logout_date) max_login_date
|
||||
FROM $userTable u
|
||||
LEFT JOIN $track t
|
||||
ON (u.id = t.user_id)
|
||||
LEFT JOIN $login l
|
||||
ON (u.id = l.login_user_id)
|
||||
WHERE
|
||||
u.id = $userId
|
||||
LIMIT 1
|
||||
";
|
||||
$result = Database::query($sql);
|
||||
$data = Database::fetch_array($result);
|
||||
$maxCourseDate = '';
|
||||
$maxLoginDate = '';
|
||||
|
||||
// Take max date value.
|
||||
if ($data) {
|
||||
$maxCourseDate = $data['max_course_date'];
|
||||
$maxLoginDate = $data['max_login_date'];
|
||||
}
|
||||
|
||||
$maxEndPause = null;
|
||||
$pause = $extraFieldValue->get_values_by_handler_and_field_variable($userId, 'pause_formation');
|
||||
if (!empty($pause) && isset($pause['value']) && 1 == $pause['value']) {
|
||||
$endDate = $extraFieldValue->get_values_by_handler_and_field_variable(
|
||||
$userId,
|
||||
'end_pause_date'
|
||||
);
|
||||
if (!empty($endDate) && isset($endDate['value']) && !empty($endDate['value'])) {
|
||||
$maxEndPause = $endDate['value'];
|
||||
}
|
||||
}
|
||||
|
||||
$maxDate = $maxCourseDate;
|
||||
if ($maxLoginDate > $maxCourseDate) {
|
||||
$maxDate = $maxLoginDate;
|
||||
}
|
||||
|
||||
if ($maxEndPause > $maxDate) {
|
||||
$maxDate = $maxEndPause;
|
||||
}
|
||||
|
||||
if (empty($maxDate)) {
|
||||
// Nothing found for that user, skip.
|
||||
continue;
|
||||
}
|
||||
$users[$userId] = $maxDate;
|
||||
}
|
||||
|
||||
$extraFieldValue = new ExtraFieldValue('user');
|
||||
foreach ($enableDaysList as $day) {
|
||||
$day = (int) $day;
|
||||
|
||||
if (0 === $day) {
|
||||
echo 'Day = 0 avoided '.PHP_EOL;
|
||||
continue;
|
||||
}
|
||||
$dayToCheck = $day + 1;
|
||||
$hourStart = $dayToCheck * 24;
|
||||
$hourEnd = ($dayToCheck - 1) * 24;
|
||||
|
||||
$date = new DateTime($now);
|
||||
$date->sub(new DateInterval('PT'.$hourStart.'H'));
|
||||
$hourStart = $date->format('Y-m-d H:i:s');
|
||||
|
||||
$date = new DateTime($now);
|
||||
$date->sub(new DateInterval('PT'.$hourEnd.'H'));
|
||||
$hourEnd = $date->format('Y-m-d H:i:s');
|
||||
|
||||
echo "Processing day $day: $hourStart - $hourEnd ".PHP_EOL.PHP_EOL;
|
||||
|
||||
foreach ($users as $userId => $maxDate) {
|
||||
if (!($maxDate > $hourStart && $maxDate < $hourEnd)) {
|
||||
//echo "Message skipped for user #$userId because max date found: $maxDate not in range $hourStart - $hourEnd ".PHP_EOL;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check if user has selected to pause formation.
|
||||
$pause = $extraFieldValue->get_values_by_handler_and_field_variable($userId, 'pause_formation');
|
||||
if (!empty($pause) && isset($pause['value']) && 1 == $pause['value']) {
|
||||
$startDate = $extraFieldValue->get_values_by_handler_and_field_variable(
|
||||
$userId,
|
||||
'start_pause_date'
|
||||
);
|
||||
$endDate = $extraFieldValue->get_values_by_handler_and_field_variable(
|
||||
$userId,
|
||||
'end_pause_date'
|
||||
);
|
||||
|
||||
if (
|
||||
!empty($startDate) && isset($startDate['value']) && !empty($startDate['value']) &&
|
||||
!empty($endDate) && isset($endDate['value']) && !empty($endDate['value'])
|
||||
) {
|
||||
$startDate = $startDate['value'];
|
||||
$endDate = $endDate['value'];
|
||||
|
||||
if ($startDate > $hourStart && $startDate < $hourStart) {
|
||||
//echo "Message skipped for user #$userId because process date $hourStart is in start pause in $startDate - $endDate ".PHP_EOL;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($endDate > $hourEnd && $endDate < $hourEnd) {
|
||||
//echo "Message skipped for user #$userId because process date $hourEnd is in start pause in $startDate - $endDate ".PHP_EOL;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
echo "User #$userId added to message queue because latest login is $maxDate between $hourStart AND $hourEnd".PHP_EOL;
|
||||
$users[] = $userId;
|
||||
$usersNotificationPerDay[$day][] = $userId;
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($usersNotificationPerDay)) {
|
||||
echo PHP_EOL.'Now processing messages ...'.PHP_EOL;
|
||||
|
||||
ksort($usersNotificationPerDay);
|
||||
foreach ($usersNotificationPerDay as $day => $userList) {
|
||||
$template = new Template(
|
||||
'',
|
||||
true,
|
||||
true,
|
||||
false,
|
||||
false,
|
||||
true,
|
||||
false
|
||||
);
|
||||
$title = sprintf($this->get_lang('InactivityXDays'), $day);
|
||||
|
||||
foreach ($userList as $userId) {
|
||||
$userInfo = api_get_user_info($userId);
|
||||
$template->assign('days', $day);
|
||||
$template->assign('user', $userInfo);
|
||||
$content = $template->fetch('pausetraining/view/notification_content.tpl');
|
||||
echo 'Ready to send a message "'.$title.'" to user #'.$userId.' '.$userInfo['complete_name'].PHP_EOL;
|
||||
if (false === $isTest) {
|
||||
MessageManager::send_message_simple($userId, $title, $content, $senderId);
|
||||
} else {
|
||||
echo 'Message not send because is in test mode.'.PHP_EOL;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function runCronTest()
|
||||
{
|
||||
$now = api_get_utc_datetime();
|
||||
$days = 15;
|
||||
$before = new DateTime($now);
|
||||
$before->sub(new DateInterval('P'.$days.'D'));
|
||||
|
||||
$after = new DateTime($now);
|
||||
$after->add(new DateInterval('P'.$days.'D'));
|
||||
|
||||
$period = new DatePeriod(
|
||||
$before,
|
||||
new DateInterval('P1D'),
|
||||
$after
|
||||
);
|
||||
|
||||
foreach ($period as $key => $value) {
|
||||
self::runCron($value->format('Y-m-d H:i:s'), true);
|
||||
}
|
||||
}
|
||||
|
||||
public function install()
|
||||
{
|
||||
UserManager::create_extra_field(
|
||||
'pause_formation',
|
||||
ExtraField::FIELD_TYPE_CHECKBOX,
|
||||
$this->get_lang('PauseFormation'),
|
||||
''
|
||||
);
|
||||
|
||||
UserManager::create_extra_field(
|
||||
'start_pause_date',
|
||||
ExtraField::FIELD_TYPE_DATETIME,
|
||||
$this->get_lang('StartPauseDateTime'),
|
||||
''
|
||||
);
|
||||
|
||||
UserManager::create_extra_field(
|
||||
'end_pause_date',
|
||||
ExtraField::FIELD_TYPE_DATETIME,
|
||||
$this->get_lang('EndPauseDateTime'),
|
||||
''
|
||||
);
|
||||
|
||||
UserManager::create_extra_field(
|
||||
'disable_emails',
|
||||
ExtraField::FIELD_TYPE_CHECKBOX,
|
||||
$this->get_lang('DisableEmails'),
|
||||
''
|
||||
);
|
||||
}
|
||||
|
||||
public function uninstall()
|
||||
{
|
||||
}
|
||||
}
|
||||
6
plugin/pausetraining/README.md
Normal file
6
plugin/pausetraining/README.md
Normal file
@@ -0,0 +1,6 @@
|
||||
Installation
|
||||
============
|
||||
|
||||
1. Enabled the plugin from the list of plugins.
|
||||
2. Click "Configure" once the plugin was enabled.
|
||||
3. Select tool_enable = Yes and save.
|
||||
4
plugin/pausetraining/config.php
Normal file
4
plugin/pausetraining/config.php
Normal file
@@ -0,0 +1,4 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/../../main/inc/global.inc.php';
|
||||
7
plugin/pausetraining/cron.php
Normal file
7
plugin/pausetraining/cron.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/../../main/inc/global.inc.php';
|
||||
|
||||
PauseTraining::create()->runCron();
|
||||
7
plugin/pausetraining/cronTest.php
Normal file
7
plugin/pausetraining/cronTest.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/../../main/inc/global.inc.php';
|
||||
|
||||
PauseTraining::create()->runCronTest();
|
||||
1
plugin/pausetraining/index.php
Normal file
1
plugin/pausetraining/index.php
Normal file
@@ -0,0 +1 @@
|
||||
<?php
|
||||
9
plugin/pausetraining/install.php
Normal file
9
plugin/pausetraining/install.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
/* For license terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/config.php';
|
||||
|
||||
if (!api_is_platform_admin()) {
|
||||
exit('You must have admin permissions to install plugins');
|
||||
}
|
||||
PauseTraining::create()->install();
|
||||
20
plugin/pausetraining/lang/english.php
Normal file
20
plugin/pausetraining/lang/english.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
$strings['plugin_title'] = "Pause training";
|
||||
$strings['plugin_comment'] = "Adds a 'Pause training' tab on the user profile edition page, to let users indicate if they are to be considered on pause for an (optional) specific timeframe. Can be used to send alerts to specific users (admins or a generic alert e-mail) when this happens.";
|
||||
$strings['tool_enable'] = 'Enable plugin';
|
||||
$strings['tool_enable_help'] = '';
|
||||
$strings['PauseTraining'] = "Pause training";
|
||||
$strings['allow_users_to_edit_pause_formation'] = 'Allow users to edit pause formation';
|
||||
$strings['cron_alert_users_if_inactive_days'] = 'Alert users if inactive days (via cron)';
|
||||
$strings['PauseFormation'] = 'Pause my formation';
|
||||
$strings['StartPauseDateTime'] = 'Start pause date';
|
||||
$strings['EndPauseDateTime'] = 'End pause date';
|
||||
$strings['DisableEmails'] = 'Disable email notifications';
|
||||
$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';
|
||||
19
plugin/pausetraining/lang/french.php
Normal file
19
plugin/pausetraining/lang/french.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
$strings['plugin_title'] = "Pause training";
|
||||
$strings['plugin_comment'] = "Ajoute un onglet 'Pause formation' sur la page d'édition du profil de l'utilisateur, permettant à l'utilisateur d'indiquer qu'il doit être considéré en pause de formation entre (optionnellement) des dates spécifiques. Cet événement peut envoyer une alerte courriel pour signifier cette pause à une adresse spécifique.";
|
||||
$strings['tool_enable'] = 'Enable plugin';
|
||||
$strings['tool_enable_help'] = '';
|
||||
$strings['PauseTraining'] = "Pause training";
|
||||
$strings['allow_users_to_edit_pause_formation'] = 'Allow users to edit pause formation';
|
||||
$strings['cron_alert_users_if_inactive_days'] = 'Alert users if inactive days (via cron)';
|
||||
$strings['PauseFormation'] = 'Mettre en pause ma formation';
|
||||
$strings['StartPauseDateTime'] = 'Date de début de pause';
|
||||
$strings['EndPauseDateTime'] = 'Date de fin de pause';
|
||||
$strings['DisableEmails'] = 'Désactiver les e-mails ';
|
||||
$strings['InactivityXDays'] = 'Inactivité sur la plateforme depuis %s jours';
|
||||
$strings['YouAreConnectedInPlatformXLinkXSinceXDays'] = '
|
||||
Nous avons remarqué que vous ne vous êtes pas connecté à la plateforme %s (%s) depuis %s jours.
|
||||
Ceci est un message automatique pour vous rappeler votre activité.';
|
||||
1
plugin/pausetraining/lang/spanish.php
Normal file
1
plugin/pausetraining/lang/spanish.php
Normal file
@@ -0,0 +1 @@
|
||||
<?php
|
||||
4
plugin/pausetraining/plugin.php
Normal file
4
plugin/pausetraining/plugin.php
Normal file
@@ -0,0 +1,4 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__.'/config.php';
|
||||
$plugin_info = PauseTraining::create()->get_info();
|
||||
7
plugin/pausetraining/view/notification_content.tpl
Normal file
7
plugin/pausetraining/view/notification_content.tpl
Normal file
@@ -0,0 +1,7 @@
|
||||
{{ 'Dear' | get_lang }} {{ user.complete_name }}
|
||||
|
||||
{{ 'YouAreConnectedInPlatformXLinkXSinceXDays'|get_plugin_lang('PauseTraining')|format(_s.institution, _s.institution_url, days) }}
|
||||
|
||||
{{ 'SignatureFormula' | get_lang }}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user