functions.inc.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. //define('USERINFO_TABLE', 'danone_userinfo');
  3. //define('DEFAULT_PASSWORD', 'danonelearning');
  4. //TODO : Please implements this function for this module to work.
  5. /**
  6. * Gets user info from external source
  7. * @param string login
  8. * @param string password
  9. * @return user array with at least the following fields:
  10. * firstname
  11. * lastname
  12. * status
  13. * email
  14. * login
  15. * password
  16. * or false if no data
  17. * */
  18. function external_get_user_info($login, $password) {
  19. //Those are the mandatory fields for user creation.
  20. //See external_add_user function for all the fields you can have.
  21. $table = USERINFO_TABLE;
  22. $sql = "SELECT * from $table where username='" . Database::escape_string($login) . "'";
  23. $result = Database::query($sql);
  24. if (Database::num_rows($result) == 0) { //false password
  25. return false;
  26. }
  27. $user_info = Database::fetch_assoc($result);
  28. // User status
  29. $admin = false;
  30. switch ($user_info['status']) {
  31. case 'admin':
  32. $status = COURSEMANAGER;
  33. $admin = true;
  34. break;
  35. case 'teacher':
  36. $status = COURSEMANAGER;
  37. break;
  38. case 'user':
  39. $status = STUDENT;
  40. break;
  41. default:
  42. $status = STUDENT;
  43. }
  44. // Language
  45. switch ($user_info['language']) {
  46. case 'FR' :
  47. $language = 'french';
  48. break;
  49. case 'EN' :
  50. $language = 'english';
  51. break;
  52. default :
  53. $language = 'english';
  54. break;
  55. }
  56. //Can Send Message ?
  57. $can_send_message = ($user_info['can_send_message'] == 1) ? 'yes' : 'no';
  58. $u = array(
  59. 'firstname' => $user_info['firstname'],
  60. 'lastname' => $user_info['lastname'],
  61. 'status' => $status,
  62. 'admin' => $admin,
  63. 'email' => $user_info['email'],
  64. 'username' => $user_info['username'],
  65. 'language' => $language,
  66. 'password' => DEFAULT_PASSWORD,
  67. 'courses' => $user_info['courses'],
  68. 'profile_link' => $user_info['profile_link'],
  69. 'worldwide_bu' => $user_info['worlwide_bu'],
  70. 'manager' => $user_info['manager'],
  71. 'extra' => array(
  72. 'position_title' => $user_info['position_title'],
  73. 'country' => $user_info['country'],
  74. 'job_family' => $user_info['job_family'],
  75. 'country_bu' => $user_info['country_bu'],
  76. 'worldwide_bu' => $user_info['worldwide_bu'],
  77. 'profile_link' => $user_info['profile_link'],
  78. 'can_send_message' => $can_send_message,
  79. 'update_type' => 'external_logininfo')
  80. );
  81. return $u; //Please return false if user does not exist
  82. //return false;
  83. }
  84. /**
  85. * Return an array with all user info
  86. * @param associative array with at least thes fields setted :
  87. firstname, lastname, status, email, login, password
  88. * @return mixed new user id - if the new user creation succeeds, false otherwise
  89. * */
  90. function external_add_user($u) {
  91. //Setting default
  92. if (empty($u['password']))
  93. $u['password'] = null;
  94. if (empty($u['status']))
  95. $u['status'] = 5;
  96. if (!isset($u['official_code']))
  97. $u['official_code'] = '';
  98. if (!isset($u['language']))
  99. $u['language'] = '';
  100. if (!isset($u['phone']))
  101. $u['phone'] = '';
  102. if (!isset($u['picture_uri']))
  103. $u['picture_uri'] = '';
  104. if (!isset($u['auth_source']))
  105. $u['auth_source'] = PLATFORM_AUTH_SOURCE;
  106. if (!isset($u['expiration_date']))
  107. $u['expiration_date'] = '0000-00-00 00:00:00';
  108. if (!isset($u['active']))
  109. $u['active'] = 1;
  110. if (!isset($u['hr_dept_id']))
  111. $u['hr_dept_id'] = 0; //id of responsible HR
  112. if (!isset($u['extra']))
  113. $u['extra'] = null;
  114. if (!isset($u['encrypt_method']))
  115. $u['encrypt_method'] = '';
  116. $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']);
  117. return $chamilo_uid;
  118. }
  119. /**
  120. * Update the user in chamilo database. It upgrade only info that is present in the
  121. * new_user array
  122. *
  123. * @param $new_user associative array with the value to upgrade
  124. * WARNING user_id key is MANDATORY
  125. * Possible keys are :
  126. * - firstname
  127. * - lastname
  128. * - username
  129. * - auth_source
  130. * - email
  131. * - status
  132. * - official_code
  133. * - phone
  134. * - picture_uri
  135. * - expiration_date
  136. * - active
  137. * - creator_id
  138. * - hr_dept_id
  139. * - extra : array of custom fields
  140. * - language
  141. * - courses : string of all courses code separated by '|'
  142. * - admin : boolean
  143. * @return boolean
  144. * @author ndiechburg <noel@cblue.be>
  145. * */
  146. function external_update_user($new_user) {
  147. $old_user = api_get_user_info($new_user['user_id']);
  148. $u = array_merge($old_user, $new_user);
  149. $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'], '');
  150. if (isset($u['courses']) && !empty($u['courses'])) {
  151. $autoSubscribe = explode('|', $u['courses']);
  152. foreach ($autoSubscribe as $code) {
  153. if (CourseManager::course_exists($code)) {
  154. CourseManager::subscribe_user($u['user_id'], $code);
  155. }
  156. }
  157. }
  158. // Is User Admin ?
  159. //TODO decomments and check that user_is is not already in admin table
  160. /*
  161. if (isset($u['admin']) && $u['admin']){
  162. $table = Database::get_main_table(TABLE_MAIN_ADMIN);
  163. $res = Database::query("SELECT * from $table WHERE user_id = ".$u['user_id']);
  164. } */
  165. }
  166. ?>