functions.inc.php 6.3 KB

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