Browse Source

Adding ClassId support when importing users see #6680

Julio Montoya 11 years ago
parent
commit
6199ea8f71
2 changed files with 37 additions and 45 deletions
  1. 35 28
      main/admin/user_import.php
  2. 2 17
      main/inc/lib/usergroup.lib.php

+ 35 - 28
main/admin/user_import.php

@@ -30,10 +30,14 @@ function validate_data($users)
 
     // 1. Check if mandatory fields are set.
     $mandatory_fields = array('LastName', 'FirstName');
+
     if (api_get_setting('registration', 'email') == 'true') {
         $mandatory_fields[] = 'Email';
     }
-    foreach ($users as $index => $user) {
+    $classExistList = array();
+    $usergroup = new UserGroup();
+
+    foreach ($users as $user) {
         foreach ($mandatory_fields as $field) {
             if (empty($user[$field])) {
                 $user['error'] = get_lang($field.'Mandatory');
@@ -65,17 +69,24 @@ function validate_data($users)
             $user['error'] = get_lang('WrongStatus');
             $errors[] = $user;
         }
-        // 4. Check classname
-        $usergroup = new UserGroup();
-        if (!empty($user['ClassName'])) {
-            $className = explode('|', trim($user['ClassName']));
-            foreach ($className as $class) {
-                if (!$usergroup->usergroup_exists($class)) {
-                    $user['error'] = get_lang('ClassNameNotAvailable');
+
+        // 4. Check ClassId
+        if (!empty($user['ClassId'])) {
+            $classId = explode('|', trim($user['ClassId']));
+            foreach ($classId as $id) {
+                if (in_array($id, $classExistList)) {
+                    continue;
+                }
+                $info = $usergroup->get($id);
+                if (empty($info)) {
+                    $user['error'] = sprintf(get_lang('ClassIdDoesntExists'), $id);
                     $errors[] = $user;
+                } else {
+                    $classExistList[] = $info['id'];
                 }
             }
         }
+
         // 5. Check authentication source
         if (!empty($user['AuthSource'])) {
             if (!in_array($user['AuthSource'], $defined_auth_sources)) {
@@ -84,6 +95,7 @@ function validate_data($users)
             }
         }
     }
+
     return $errors;
 }
 
@@ -128,6 +140,7 @@ function save_data($users)
         $inserted_in_course = array();
     }
     require_once api_get_path(LIBRARY_PATH).'mail.lib.inc.php';
+    $usergroup = new UserGroup();
     $send_mail = $_POST['sendMail'] ? true : false;
     if (is_array($users)) {
         foreach ($users as $user) {
@@ -176,17 +189,13 @@ function save_data($users)
                     }
                 }
             }
-            $usergroup = new UserGroup();
-            if (!empty($user['ClassName'])) {
-                $className = explode('|', trim($user['ClassName']));
-                foreach ($className as $class) {
-                    $classId = $usergroup->get_id_by_name($class);
-                    $usergroup->addUser($user_id, $classId);
+            if (!empty($user['ClassId'])) {
+                $classId = explode('|', trim($user['ClassId']));
+                foreach ($classId as $id) {
+                    $usergroup->subscribe_users_to_usergroup($id, array($user_id), false);
                 }
-                
             }
 
-
             // Saving extra fields.
             global $extra_fields;
 
@@ -306,14 +315,14 @@ if (is_array($extAuthSource)) {
 }
 
 $tool_name = get_lang('ImportUserListXMLCSV');
-$interbreadcrumb[] = array ("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
+$interbreadcrumb[] = array("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
 
 set_time_limit(0);
 $extra_fields = UserManager::get_extra_fields(0, 0, 5, 'ASC', true);
 $user_id_error = array();
 $error_message = '';
 
-if ($_POST['formSent'] and $_FILES['import_file']['size'] !== 0) {
+if (isset($_POST['formSent']) && $_POST['formSent'] AND $_FILES['import_file']['size'] !== 0) {
     $file_type = $_POST['file_type'];
     Security::clear_token();
     $tok = Security::get_token();
@@ -413,14 +422,14 @@ $group[] = $form->createElement(
     '',
     'CSV (<a href="example.csv" target="_blank">'.get_lang('ExampleCSVFile').'</a>)',
     'csv'
-    );
+);
 $group[] = $form->createElement(
     'radio',
     'file_type',
     null,
     'XML (<a href="example.xml" target="_blank">'.get_lang('ExampleXMLFile').'</a>)',
     'xml'
-    );
+);
 $form->addGroup($group, '', get_lang('FileType'), '<br/>');
 
 $group = array();
@@ -455,17 +464,14 @@ if ($count_fields > 0) {
 
 ?>
     <p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
-
     <blockquote>
 <pre>
-<b>LastName</b>;<b>FirstName</b>;<b>Email</b>;UserName;Password;AuthSource;OfficialCode;PhoneNumber;Status;<span style="color:red;"><?php if (count($list) > 0) echo implode(';', $list).';'; ?></span>Courses;
-<b>xxx</b>;<b>xxx</b>;<b>xxx</b>;xxx;xxx;<?php echo implode('/', $defined_auth_sources); ?>;xxx;xxx;user/teacher/drh;<span style="color:red;"><?php if (count($list_reponse) > 0) echo implode(';', $list_reponse).';'; ?></span>xxx1|xxx2|xxx3;<br />
+<b>LastName</b>;<b>FirstName</b>;<b>Email</b>;UserName;Password;AuthSource;OfficialCode;PhoneNumber;Status;<span style="color:red;"><?php if (count($list) > 0) echo implode(';', $list).';'; ?></span>Courses;ClassId;
+<b>xxx</b>;<b>xxx</b>;<b>xxx</b>;xxx;xxx;<?php echo implode('/', $defined_auth_sources); ?>;xxx;xxx;user/teacher/drh;<span style="color:red;"><?php if (count($list_reponse) > 0) echo implode(';', $list_reponse).';'; ?></span>xxx1|xxx2|xxx3;1;<br />
 </pre>
-    </blockquote>
-
-    <p><?php echo get_lang('XMLMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
-
-    <blockquote>
+</blockquote>
+<p><?php echo get_lang('XMLMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
+<blockquote>
 <pre>
 &lt;?xml version=&quot;1.0&quot; encoding=&quot;<?php echo api_refine_encoding_id(api_get_system_encoding()); ?>&quot;?&gt;
 &lt;Contacts&gt;
@@ -480,6 +486,7 @@ if ($count_fields > 0) {
         &lt;PhoneNumber&gt;xxx&lt;/PhoneNumber&gt;
         &lt;Status&gt;user/teacher/drh<?php if ($result_xml != '') { echo '<br /><span style="color:red;">', $result_xml; echo '</span>'; } ?>&lt;/Status&gt;
         &lt;Courses&gt;xxx1|xxx2|xxx3&lt;/Courses&gt;
+        &lt;ClassId&gt;1&lt;/ClassId&gt;
         &lt;/Contact&gt;
 &lt;/Contacts&gt;
 </pre>

+ 2 - 17
main/inc/lib/usergroup.lib.php

@@ -706,9 +706,9 @@ class UserGroup extends Model
             $urlId = api_get_current_access_url_id();
             $from = $this->table." u INNER JOIN {$this->access_url_rel_usergroup} a ON (u.id = a.usergroup_id)";
             $options = array('where' => array('access_url_id = ? ' => $urlId));
-            return Database::select('name, description', $from, $options);
+            return Database::select('a.id, name, description', $from, $options);
         } else {
-            return Database::select('name, description', $this->table, $options);
+            return Database::select('id, name, description', $this->table, $options);
         }
     }
 
@@ -835,20 +835,5 @@ class UserGroup extends Model
 
         return $response;
     }
-
-    /**
-    * Add a user to a class. If the class is subscribed to a course, the new
-    * user will also be subscribed to that course.
-    * @param int $user_id The user id
-    * @param int $class_id The class id
-    */
-    public function addUser($userId, $classId)
-    {
-        $table_rel_user = Database::get_main_table(TABLE_USERGROUP_REL_USER);
-        $userId  = intval($userId);
-        $classId = intval($classId);
-        $sql = "INSERT INTO $table_rel_user SET user_id = '".$userId."', usergroup_id='".$classId."'";
-	    Database::query($sql);
-    }
 }
 /* CREATE TABLE IF NOT EXISTS access_url_rel_usergroup (access_url_id int unsigned NOT NULL, usergroup_id int unsigned NOT NULL, PRIMARY KEY (access_url_id, usergroup_id));*/