Completed
Push — master ( f77763...d3c71d )
by Julito
11:18
created

unsubscribeUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
/* For license terms, see /license.txt */
3
4
require_once __DIR__.'/../../main/inc/global.inc.php';
5
6
$plugin = Justification::create();
7
$courseId = api_get_setting('justification_default_course_id', 'justification');
8
9
echo 'Justification CRON - '.api_get_local_time().PHP_EOL;
10
11
if (empty($courseId)) {
12
    echo 'No course was set';
13
    exit;
14
}
15
16
$courseInfo = api_get_course_info_by_id($courseId);
17
if (empty($courseInfo)) {
18
    echo "Course #$courseId doesn't exist";
19
    exit;
20
}
21
22
$fieldList = $plugin->getList();
23
$totalFields = count($fieldList);
24
25
if (empty($fieldList)) {
26
    echo 'No fields to check. Please add them in the justification plugin';
27
    exit;
28
}
29
30
$userList = UserManager::get_user_list();
31
$count = count($userList);
32
33
echo "#$count users found".PHP_EOL;
34
$currentDate = api_get_utc_datetime();
35
36
foreach ($userList as $user) {
37
    $userId = $user['id'];
38
39
    echo "Checking user id #$userId".PHP_EOL;
40
41
    $userJustificationList = $plugin->getUserJustificationList($userId);
42
    $userJustificationDocumentList = array_column($userJustificationList, 'date_validity', 'justification_document_id');
43
44
    if (count($userJustificationList) < $totalFields) {
45
        unsubscribeUser($userId, $courseInfo);
46
        continue;
47
    }
48
49
    if (count($userJustificationList) >= $totalFields) {
50
        $successList = [];
51
        foreach ($fieldList as $field) {
52
            if (isset($userJustificationDocumentList[$field['id']])) {
53
                $dateValidity = $userJustificationDocumentList[$field['id']];
54
                if ($dateValidity > $currentDate) {
55
                    $successList[] = true;
56
                }
57
            }
58
        }
59
        $countSuccess = count($successList);
60
        if ($countSuccess === $totalFields) {
61
            subscribeUser($userId, $courseInfo);
62
            continue;
63
        } else {
64
            echo "User #$userId only got $countSuccess justification(s) out of $totalFields.".PHP_EOL;
65
        }
66
    }
67
68
    unsubscribeUser($userId, $courseInfo);
69
}
70
71
function unsubscribeUser($userId, $courseInfo)
72
{
73
    $courseId = $courseInfo['real_id'];
74
    CourseManager::unsubscribe_user($userId, $courseInfo['code']);
75
    echo "Unsubscribe user id #$userId to course #$courseId".PHP_EOL;
76
}
77
78
function subscribeUser($userId, $courseInfo)
79
{
80
    $courseId = $courseInfo['real_id'];
81
    $isUserSubscribed = CourseManager::is_user_subscribed_in_course($userId, $courseInfo['code']);
82
    if ($isUserSubscribed === false) {
83
        CourseManager::subscribeUser($userId, $courseInfo['code'], STUDENT);
84
        echo "Subscribe user id #$userId to course #$courseId".PHP_EOL;
85
    } else {
86
        echo "Nothing to do user id #$userId is already subscribed to #$courseId".PHP_EOL;
87
    }
88
}
89