update_user_extra_field.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Updates an user extra field
  5. * a file is needed with this format:
  6. *
  7. * user;country
  8. * julio;France
  9. *
  10. * Where:
  11. * "country "is the name of the user extra field,
  12. * "France" is the value to save.
  13. * "julio" is the username of the user to be updated
  14. *
  15. */
  16. exit;
  17. require __DIR__.'/../../main/inc/global.inc.php';
  18. // Define origin and destination courses' code
  19. $extraFieldName = 'dni';
  20. $debug = true;
  21. api_protect_admin_script();
  22. $extraField = new ExtraField('user');
  23. $file = 'file.csv';
  24. $users = Import :: csvToArray($file);
  25. foreach ($users as $user) {
  26. $userInfo = api_get_user_info_from_username($user['user']);
  27. if (!empty($userInfo)) {
  28. if ($debug == false) {
  29. UserManager::update_extra_field_value(
  30. $userInfo['user_id'],
  31. $extraFieldName,
  32. $user[$extraFieldName]
  33. );
  34. }
  35. echo 'Updating extrafield "'.$extraFieldName.'": '.$user[$extraFieldName].'<br />';
  36. } else {
  37. echo 'User does not exists: '.$user['user'].'<br />';
  38. }
  39. }