Skip to content

Commit

Permalink
Admin: Add config 'disable_user_conditions_sender_id'
Browse files Browse the repository at this point in the history
Add disable_user_conditions.php script, disabled by default,
  • Loading branch information
jmontoyaa committed Apr 29, 2021
1 parent 0a4e75d commit c8536f2
Show file tree
Hide file tree
Showing 2 changed files with 251 additions and 0 deletions.
3 changes: 3 additions & 0 deletions main/install/configuration.dist.php
Expand Up @@ -1511,6 +1511,9 @@
// ALTER TABLE session ADD COLUMN status INT DEFAULT 0;
// $_configuration['allow_session_status'] = false;

// Sets the sender id when using the script tests/scripts/disable_user_conditions.php
// $_configuration['disable_user_conditions_sender_id'] = 0;

// Set the default tab in the admin session list. Values: all, close, active, custom.
//$_configuration['default_session_list_view'] = 'all';

Expand Down
248 changes: 248 additions & 0 deletions tests/scripts/disable_user_conditions.php
@@ -0,0 +1,248 @@
<?php

/* For licensing terms, see /license.txt */

require_once __DIR__.'/../../main/inc/global.inc.php';

$senderId = api_get_configuration_value('disable_user_conditions_sender_id');

if (empty($senderId)) {
exit;
}

$test = true;
$newLine = '<br />';
if (PHP_SAPI == 'cli') {
$newLine = PHP_EOL;
}
$userReportList = [];
$extraFieldValue = new ExtraField('user');
$extraFieldInfo = $extraFieldValue->get_handler_field_info_by_field_variable('termactivated');
$fieldId = $extraFieldInfo['id'];
$senderInfo = api_get_user_info($senderId);
if (empty($senderInfo)) {
echo 'Please set the configuration value: "disable_user_conditions_sender_id" for a valid user.';
}

$statusCondition = ' AND u.status = '.STUDENT;

$date = new Datetime();
$now = $date->format('Y-m-d H:i:s');

$date = $date->sub(new DateInterval('P3M'));
$date3Months = $date->format('Y-m-d H:i:s');

$date = new Datetime();
$date = $date->sub(new DateInterval('P6M'));
$date6Months = $date->format('Y-m-d H:i:s');

// 1. Not contract not connected in 3 months
$sql = "SELECT u.id
FROM user u
LEFT JOIN extra_field_values ev
ON u.id = ev.item_id AND field_id = $fieldId
WHERE
(ev.value IS NULL OR ev.value = '') AND
u.active = 1
$statusCondition
";

$result = Database::query($sql);
$students = Database::store_result($result);
foreach ($students as $student) {
$studentId = $student['id'];
$lastDate = Tracking::get_last_connection_date($studentId, false, true);
$lastDate = api_get_utc_datetime($lastDate);

if ($date3Months > $lastDate) {
$disabledUser = api_get_user_info($studentId);
if (empty($disabledUser)) {
continue;
}
if (!isset($userReportList[$studentId]['message'])) {
$userReportList[$studentId]['message'] = '';
}
$userReportList[$studentId]['message'] .= $newLine."User# $studentId (".$disabledUser['username'].") to be disabled. Case 1. Last connection: $lastDate - 3 months: $date3Months ";

$language = $disabledUser['language'];
$subject = get_lang('AccountDisabled', null, $language).': '.$disabledUser['complete_name'];
$content = get_lang('DisableUserCase1', null, $language);

$userReportList[$studentId]['message'] .= $newLine.
'Mail will be send to: '.$disabledUser['username'].' ('.$disabledUser['email'].')'.$newLine.
'Subject: '.$subject.$newLine.
'Content: '.$content.$newLine;

if (false === $test) {
MessageManager::send_message(
$studentId,
$subject,
$content,
[],
[],
0,
0,
0,
0,
$senderId
);
UserManager::disable($studentId);
}
}
}

// 3. Certificate completed not connected 6 months.
$sql = "SELECT c.user_id, MAX(login_date) latest_login_date
FROM gradebook_certificate c
INNER JOIN track_e_login l
ON (l.login_user_id = c.user_id)
INNER JOIN user u
ON (l.login_user_id = u.id)
WHERE 1=1
$statusCondition
GROUP BY c.user_id ";

$result = Database::query($sql);
$students = Database::store_result($result);
foreach ($students as $student) {
$studentId = $student['user_id'];
$lastDate = $student['latest_login_date'];

if ($date6Months > $lastDate) {
$disabledUser = api_get_user_info($studentId);
if (empty($disabledUser)) {
continue;
}

if (1 != $disabledUser['active']) {
continue;
}

$language = $disabledUser['language'];
$subject = get_lang('AccountDisabled', null, $language).': '.$disabledUser['complete_name'];
$content = get_lang('DisableUserCase3Student', null, $language);

if (!isset($userReportList[$studentId]['message'])) {
$userReportList[$studentId]['message'] = '';
}

$userReportList[$studentId]['message'] .= $newLine."User# $studentId (".$disabledUser['username'].") to be disabled. Case 3. Last connection: $lastDate - 6 months: $date6Months ";
$userReportList[$studentId]['message'] .= $newLine.
'Mail will be send to: '.$disabledUser['username'].$newLine.
'Subject: '.$subject.$newLine.
'Content: '.$content;

if (false === $test) {
MessageManager::send_message(
$studentId,
$subject,
$content,
[],
[],
0,
0,
0,
0,
$senderId
);
UserManager::disable($studentId);
}
}
}

// 2. Validated contract, not connected + 6 months
$sql = "SELECT u.id
FROM user u
INNER JOIN extra_field_values ev
ON u.id = ev.item_id AND field_id = $fieldId
WHERE
ev.value = 1 AND
u.active = 1
$statusCondition
";

$result = Database::query($sql);
$students = Database::store_result($result);
foreach ($students as $student) {
$studentId = $student['id'];
$lastDate = Tracking::get_last_connection_date($studentId, false, true);
$lastDate = api_get_utc_datetime($lastDate);

if ($date6Months > $lastDate) {
$disabledUser = api_get_user_info($studentId);
if (empty($disabledUser)) {
continue;
}

if (!isset($userReportList[$studentId]['message'])) {
$userReportList[$studentId]['message'] = '';
}

$userReportList[$studentId]['message'] .= $newLine."User# $studentId (".$disabledUser['username'].") to be disabled. Case 2 . Last connection: $lastDate - 6 months: $date6Months ";

$subject = get_lang('AccountDisabled', null, $disabledUser['language']).': '.$disabledUser['complete_name'];
$content = get_lang('DisableUserCase2', null, $disabledUser['language']);

$userReportList[$studentId]['message'] .= $newLine.'Mail will be send to: '.$disabledUser['username'].$newLine.'Subject: '.$subject.$newLine.'Content: '.$content.$newLine;

$subjectBoss = '';
$contentBoss = '';
$studentBoss = UserManager::getFirstStudentBoss($studentId);
$bossInfo = [];
if (!empty($studentBoss)) {
$bossInfo = api_get_user_info($studentBoss);
if ($bossInfo) {
$subjectBoss = get_lang('AccountDisabled', null, $bossInfo['language']).': '.$disabledUser['complete_name'];
$contentBoss = sprintf(get_lang('DisableUserCase2StudentX', null, $bossInfo['language']), $disabledUser['complete_name']);
$userReportList[$studentId]['message'] .= $newLine.'Mail will be send to: '.$bossInfo['username'].$newLine.'Subject: '.$subjectBoss.$newLine.'Content: '.$contentBoss.$newLine;
}
}

if (false === $test) {
MessageManager::send_message(
$studentId,
$subject,
$content,
[],
[],
0,
0,
0,
0,
$senderId
);
UserManager::disable($studentId);
if (!empty($bossInfo) && !empty($subjectBoss)) {
MessageManager::send_message(
$studentBoss,
$subjectBoss,
$contentBoss,
[],
[],
0,
0,
0,
0,
$senderId
);
}
UserManager::removeAllBossFromStudent($studentId);
}
}
}

if ($test) {
echo '<h3>No changes have been made.</h3>'.$newLine;
}

echo 'Sender user: '.$senderInfo['complete_name'].' ('.$senderInfo['email'].') '.$newLine;
echo "Now: $now".$newLine;
echo "3 Months old: $date3Months".$newLine;
echo "6 Months old: $date6Months".$newLine;
echo $newLine;

ksort($userReportList);

foreach ($userReportList as $data) {
echo $data['message'].$newLine;
}

0 comments on commit c8536f2

Please sign in to comment.