Completed
Push — 1.10.x ( ba0bf0...97c0d2 )
by Angel Fernando Quiroz
44:06
created

functions.inc.php ➔ external_add_user()   F

Complexity

Conditions 13
Paths 4096

Size

Total Lines 30
Code Lines 27

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 13
eloc 27
nc 4096
nop 1
dl 0
loc 30
rs 2.7716

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
//define('USERINFO_TABLE', 'danone_userinfo');
4
//define('DEFAULT_PASSWORD', 'danonelearning');
5
//TODO : Please implements this function for this module to work.
6
/**
7
 * Gets user info from external source
8
 * @param string login
9
 * @param string password
10
 * @return user array with at least the following fields:
11
 *       firstname
12
 *       lastname
13
 *       status
14
 *       email
15
 *       login
16
 *       password
17
 *   or false if no data
18
 * */
19
function external_get_user_info($login, $password) {
20
    //Those are the mandatory fields for user creation.
21
    //See external_add_user function for all the fields you can have.
22
    $table = USERINFO_TABLE;
23
    $sql = "SELECT * from $table where username='" . Database::escape_string($login) . "'";
24
    $result = Database::query($sql);
25
26
    if (Database::num_rows($result) == 0) { //false password
27
        return false;
28
    }
29
    $user_info = Database::fetch_assoc($result);
30
    // User status
31
    $admin = false;
32
    switch ($user_info['status']) {
33
        case 'admin':
34
            $status = COURSEMANAGER;
35
            $admin = true;
36
            break;
37
        case 'teacher':
38
            $status = COURSEMANAGER;
39
            break;
40
        case 'user':
41
            $status = STUDENT;
42
            break;
43
        default:
44
            $status = STUDENT;
45
    }
46
    // Language
47
    switch ($user_info['language']) {
48
        case 'FR' :
49
            $language = 'french';
50
            break;
51
        case 'EN' :
52
            $language = 'english';
53
            break;
54
        default :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a DEFAULT statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in the default statement.

switch ($expr) {
    default : //wrong
        doSomething();
        break;
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
55
            $language = 'english';
56
            break;
57
    }
58
    //Can Send Message ?
59
    $can_send_message = ($user_info['can_send_message'] == 1) ? 'yes' : 'no';
60
61
62
    $u = array(
63
        'firstname' => $user_info['firstname'],
64
        'lastname' => $user_info['lastname'],
65
        'status' => $status,
66
        'admin' => $admin,
67
        'email' => $user_info['email'],
68
        'username' => $user_info['username'],
69
        'language' => $language,
70
        'password' => DEFAULT_PASSWORD,
71
        'courses' => $user_info['courses'],
72
        'profile_link' => $user_info['profile_link'],
73
        'worldwide_bu' => $user_info['worlwide_bu'],
74
        'manager' => $user_info['manager'],
75
        'extra' => array(
76
            'position_title' => $user_info['position_title'],
77
            'country' => $user_info['country'],
78
            'job_family' => $user_info['job_family'],
79
            'country_bu' => $user_info['country_bu'],
80
            'worldwide_bu' => $user_info['worldwide_bu'],
81
            'profile_link' => $user_info['profile_link'],
82
            'can_send_message' => $can_send_message,
83
            'update_type' => 'external_logininfo')
84
    );
85
86
    return $u; //Please return false if user does not exist
87
    //return false;
88
}
89
90
/**
91
 * Return an array with all user info
92
 * @param associative array with at least thes fields setted :
93
  firstname, lastname, status, email, login, password
94
 * @return mixed   new user id - if the new user creation succeeds, false otherwise
95
 * */
96
function external_add_user($u) {
97
    //Setting default
98
    if (empty($u['password']))
99
        $u['password'] = null;
100
    if (empty($u['status']))
101
        $u['status'] = 5;
102
    if (!isset($u['official_code']))
103
        $u['official_code'] = '';
104
    if (!isset($u['language']))
105
        $u['language'] = '';
106
    if (!isset($u['phone']))
107
        $u['phone'] = '';
108
    if (!isset($u['picture_uri']))
109
        $u['picture_uri'] = '';
110
    if (!isset($u['auth_source']))
111
        $u['auth_source'] = PLATFORM_AUTH_SOURCE;
112
    if (!isset($u['expiration_date']))
113
        $u['expiration_date'] = '0000-00-00 00:00:00';
114
    if (!isset($u['active']))
115
        $u['active'] = 1;
116
    if (!isset($u['hr_dept_id']))
117
        $u['hr_dept_id'] = 0; //id of responsible HR
118
    if (!isset($u['extra']))
119
        $u['extra'] = null;
120
    if (!isset($u['encrypt_method']))
121
        $u['encrypt_method'] = '';
122
123
    $chamilo_uid = UserManager::create_user($u['firstname'], $u['lastname'], $u['status'], $u['email'], $u['username'], $u['password'], $u['official_code'], $u['language'], $u['phone'], $u['picture_uri'], $u['auth_source'], $u['expiration_date'], $u['active'], $u['hr_dept_id'], $u['extra'], $u['encrypt_method']);
124
    return $chamilo_uid;
125
}
126
127
/**
128
 * Update the user in chamilo database. It upgrade only info that is present in the
129
 * new_user array
130
 *
131
 * @param $new_user associative array with the value to upgrade
132
 *    WARNING user_id key is MANDATORY
133
 *    Possible keys are :
134
 *      - firstname
135
 *      - lastname
136
 *      - username
137
 *      - auth_source
138
 *      - email
139
 *      - status
140
 *      - official_code
141
 *      - phone
142
 *      - picture_uri
143
 *      - expiration_date
144
 *      - active
145
 *      - creator_id
146
 *      - hr_dept_id
147
 *      - extra : array of custom fields
148
 *      - language
149
 *      - courses : string of all courses code separated by '|'
150
 *      - admin : boolean
151
 * @return boolean
152
 * @author ndiechburg <[email protected]>
153
 * */
154
function external_update_user($new_user) {
155
    $old_user = api_get_user_info($new_user['user_id']);
156
    $u = array_merge($old_user, $new_user);
157
    $updated = UserManager::update_user($u['user_id'], $u['firstname'], $u['lastname'], $u['username'], null, $u['auth_source'], $u['email'], $u['status'], $u['official_code'], $u['phone'], $u['picture_uri'], $u['expiration_date'], $u['active'], $u['creator_id'], $u['hr_dept_id'], $u['extra'], $u['language'], '');
158
    if (isset($u['courses']) && !empty($u['courses'])) {
159
        $autoSubscribe = explode('|', $u['courses']);
160
        foreach ($autoSubscribe as $code) {
161
            if (CourseManager::course_exists($code)) {
162
                CourseManager::subscribe_user($u['user_id'], $code);
163
            }
164
        }
165
    }
166
    // Is User Admin ?
167
    //TODO decomments and check that user_is is not already in admin table
168
    /*
169
      if (isset($u['admin']) && $u['admin']){
170
171
      $table = Database::get_main_table(TABLE_MAIN_ADMIN);
172
      $res = Database::query("SELECT * from $table WHERE user_id = ".$u['user_id']);
173
      } */
174
}
175
176
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
177