Kaynağa Gözat

Script to update extra field in batch see BT#9104

Julio Montoya 10 yıl önce
ebeveyn
işleme
b9d48abcec
1 değiştirilmiş dosya ile 44 ekleme ve 0 silme
  1. 44 0
      tests/scripts/update_user_extra_field.php

+ 44 - 0
tests/scripts/update_user_extra_field.php

@@ -0,0 +1,44 @@
+<?php
+/* For licensing terms, see /license.txt */
+
+/**
+ * Updates an user extra field
+ * a file is needed with this format:
+ *
+ * user;country
+ * julio;France
+ *
+ * Where:
+ * "country "is the name of the user extra field,
+ * "France" is the value to save.
+ * "julio" is the username of the user to be updated
+ *
+ */
+
+exit;
+
+require __DIR__.'/../../main/inc/global.inc.php';
+
+// Define origin and destination courses' code
+$extraFieldName = 'dni';
+$debug = true;
+api_protect_admin_script();
+
+$extraField = new ExtraField('user');
+$file = 'file.csv';
+$users = Import :: csv_to_array($file);
+foreach ($users as $user) {
+    $userInfo = api_get_user_info_from_username($user['user']);
+    if (!empty($userInfo)) {
+        if ($debug == false) {
+            UserManager::update_extra_field_value(
+                $userInfo['user_id'],
+                $extraFieldName,
+                $user[$extraFieldName]
+            );
+        }
+        echo 'Updating extrafield "'.$extraFieldName.'":  '.$user[$extraFieldName].'<br />';
+    } else {
+        echo 'User does not exists: '.$user['user'].'<br />';
+    }
+}