/**
  * @param $params
  * @return null|soap_fault
  */
 public static function WSSessionGetDetailsByUser($params)
 {
     global $debug;
     if ($debug) {
         error_log('WSUserSubscribedInCourse');
         error_log('Params ' . print_r($params, 1));
     }
     if (!WSHelperVerifyKey($params)) {
         return return_error(WS_ERROR_SECRET_KEY);
     }
     $result = return_error(WS_ERROR_NOT_FOUND_RESULT);
     // Check params
     if (is_array($params) && !empty($params['session_id']) && !empty($params['user_id'])) {
         $userId = UserManager::get_user_id_from_original_id($params['user_id'], $params['user_field']);
         $sessionId = (int) $params['session_id'];
         // Check if user exists
         if (UserManager::is_user_id_valid($userId) && SessionManager::isValidId($sessionId)) {
             // Check if student is already subscribed
             $plugin = AdvancedSubscriptionPlugin::create();
             $isOpen = $plugin->isSessionOpen($sessionId);
             $status = $plugin->getQueueStatus($userId, $sessionId);
             $vacancy = $plugin->getVacancy($sessionId);
             $data = $plugin->getSessionDetails($sessionId);
             $isUserInTargetGroup = $plugin->isUserInTargetGroup($userId, $sessionId);
             if (!empty($data) && is_array($data)) {
                 $data['status'] = $status;
                 // Vacancy and queue status cases:
                 if ($isOpen) {
                     // Go to Course session
                     $data['action_url'] = self::$plugin->getOpenSessionUrl($userId, $params);
                     if (SessionManager::isUserSubscribedAsStudent($sessionId, $userId)) {
                         $data['status'] = 10;
                     }
                 } else {
                     if (!$isUserInTargetGroup) {
                         $data['status'] = -2;
                     } else {
                         try {
                             $isAllowed = self::$plugin->isAllowedToDoRequest($userId, $params);
                             $data['message'] = self::$plugin->getStatusMessage($status, $isAllowed);
                         } catch (\Exception $e) {
                             $data['message'] = $e->getMessage();
                         }
                         $params['action'] = 'subscribe';
                         $params['sessionId'] = intval($sessionId);
                         $params['currentUserId'] = 0;
                         // No needed
                         $params['studentUserId'] = intval($userId);
                         $params['queueId'] = 0;
                         // No needed
                         $params['newStatus'] = ADVANCED_SUBSCRIPTION_QUEUE_STATUS_START;
                         if ($vacancy > 0) {
                             // Check conditions
                             if ($status == ADVANCED_SUBSCRIPTION_QUEUE_STATUS_NO_QUEUE) {
                                 // No in Queue, require queue subscription url action
                                 $data['action_url'] = self::$plugin->getTermsUrl($params);
                             } elseif ($status == ADVANCED_SUBSCRIPTION_QUEUE_STATUS_ADMIN_APPROVED) {
                                 // send url action
                                 $data['action_url'] = self::$plugin->getSessionUrl($sessionId);
                             } else {
                                 // In queue, output status message, no more info.
                             }
                         } else {
                             if ($status == ADVANCED_SUBSCRIPTION_QUEUE_STATUS_ADMIN_APPROVED) {
                                 $data['action_url'] = self::$plugin->getSessionUrl($sessionId);
                             } elseif ($status == ADVANCED_SUBSCRIPTION_QUEUE_STATUS_NO_QUEUE) {
                                 // in Queue or not, cannot be subscribed to session
                                 $data['action_url'] = self::$plugin->getTermsUrl($params);
                             } else {
                                 // In queue, output status message, no more info.
                             }
                         }
                     }
                 }
                 $result = $data;
             } else {
                 // Return soap fault No result was found
                 $result = return_error(WS_ERROR_NOT_FOUND_RESULT);
             }
         } else {
             // Return soap fault No result was found
             $result = return_error(WS_ERROR_NOT_FOUND_RESULT);
         }
     } else {
         // Return soap fault Not valid input params
         $result = return_error(WS_ERROR_INVALID_INPUT);
     }
     return $result;
 }
function WSUpdateUserApiKey($params)
{
    if (!WSHelperVerifyKey($params)) {
        return return_error(WS_ERROR_SECRET_KEY);
    }
    $user_id = UserManager::get_user_id_from_original_id($params['original_user_id_value'], $params['original_user_id_name']);
    if (!$user_id) {
        if (!empty($params['chamilo_username'])) {
            $info = api_get_user_info_from_username($params['chamilo_username']);
            $user_id = $info['user_id'];
            // Save new fieldlabel into user_field table.
            $field_id = UserManager::create_extra_field($params['original_user_id_name'], 1, $params['original_user_id_name'], '');
            // Save the external system's id into user_field_value table.
            $res = UserManager::update_extra_field_value($user_id, $params['original_user_id_name'], $params['original_user_id_value']);
        } else {
            return 0;
        }
    }
    $list = UserManager::get_api_keys($user_id);
    $key_id = UserManager::get_api_key_id($user_id, 'dokeos');
    if (isset($list[$key_id])) {
        $apikey = $list[$key_id];
    } else {
        $lastid = UserManager::update_api_key($user_id, 'dokeos');
        if ($lastid) {
            $apikeys = UserManager::get_api_keys($user_id);
            $apikey = $apikeys[$lastid];
        }
    }
    return $apikey;
}
 /**
  * Gets the real user id based on the user id field name and value. Note that if the user id field name is "chamilo_user_id", it will use the user id
  * in the system database
  * 
  * @param string User id field name
  * @param string User id value
  * @return mixed System user id if the user was found, WSError otherwise
  */
 protected function getUserId($user_id_field_name, $user_id_value)
 {
     if ($user_id_field_name == "chamilo_user_id") {
         if (UserManager::is_user_id_valid(intval($user_id_value))) {
             return intval($user_id_value);
         } else {
             return new WSError(100, "User not found");
         }
     } else {
         $user_id = UserManager::get_user_id_from_original_id($user_id_value, $user_id_field_name);
         if ($user_id == 0) {
             return new WSError(100, "User not found");
         } else {
             return $user_id;
         }
     }
 }
 /**
  * @param string $file
  * @param bool $moveFile
  */
 private function importStudents($file, $moveFile = true)
 {
     $data = Import::csv_to_array($file);
     /*
     * Another users import.
             Unique identifier: official code and username . ok
             Password should never get updated. ok
             If an update should need to occur (because it changed in the .csv),
             we’ll want that logged. We will handle this manually in that case.
             All other fields should be updateable, though passwords should of course not get updated. ok
             If a user gets deleted (not there anymore),
             He should be set inactive one year after the current date.
             So I presume you’ll just update the expiration date.
             We want to grant access to courses up to a year after deletion.
     */
     if (!empty($data)) {
         $language = $this->defaultLanguage;
         $this->logger->addInfo(count($data) . " records found.");
         foreach ($data as $row) {
             $row = $this->cleanUserRow($row);
             $user_id = UserManager::get_user_id_from_original_id($row['extra_' . $this->extraFieldIdNameList['user']], $this->extraFieldIdNameList['user']);
             $userInfo = array();
             $userInfoByOfficialCode = null;
             if (!empty($user_id)) {
                 $userInfo = api_get_user_info($user_id);
                 $userInfoByOfficialCode = api_get_user_info_from_official_code($row['official_code']);
             }
             $expirationDate = api_get_utc_datetime(strtotime("+" . intval($this->expirationDateInUserCreation) . "years"));
             if (empty($userInfo) && empty($userInfoByOfficialCode)) {
                 // Create user
                 $result = UserManager::create_user($row['firstname'], $row['lastname'], STUDENT, $row['email'], $row['username'], $row['password'], $row['official_code'], $language, $row['phone'], null, $row['auth_source'], $expirationDate, 1, 0, null, null, false);
                 if ($result) {
                     foreach ($row as $key => $value) {
                         if (substr($key, 0, 6) == 'extra_') {
                             //an extra field
                             UserManager::update_extra_field_value($result, substr($key, 6), $value);
                         }
                     }
                     $this->logger->addInfo("Students - User created: " . $row['username']);
                 } else {
                     $this->logger->addError("Students - User NOT created: " . $row['username'] . " " . $row['firstname'] . " " . $row['lastname']);
                 }
             } else {
                 if (empty($userInfo)) {
                     $this->logger->addError("Students - Can't update user :"******"Students - User email is not updated : " . $row['username'] . " because the avoid conditions (email).");
                             // Do not change email keep the old email.
                             $email = $userInfo['email'];
                         }
                         // 2. Condition
                         if (!in_array($userInfo['email'], $avoidUsersWithEmail) && !in_array($row['email'], $avoidUsersWithEmail)) {
                             $email = $userInfo['email'];
                         }
                         // 3. Condition
                         if (in_array($userInfo['email'], $avoidUsersWithEmail) && !in_array($row['email'], $avoidUsersWithEmail)) {
                             $email = $row['email'];
                         }
                         // Blocking password update
                         $avoidUsersWithPassword = $this->conditions['importStudents']['update']['avoid']['password'];
                         if ($userInfo['password'] != api_get_encrypted_password($row['password']) && in_array($row['password'], $avoidUsersWithPassword)) {
                             $this->logger->addInfo("Students - User password is not updated: " . $row['username'] . " because the avoid conditions (password).");
                             $password = null;
                             $resetPassword = 0;
                             // disallow password change
                         }
                     }
                 }
                 $expirationDate = api_get_utc_datetime(strtotime("+" . intval($this->expirationDateInUserUpdate) . "years"));
                 // Update user
                 $result = UserManager::update_user($userInfo['user_id'], $row['firstname'], $row['lastname'], $row['username'], $password, $row['auth_source'], $email, STUDENT, $userInfo['official_code'], $userInfo['phone'], $userInfo['picture_uri'], $expirationDate, $userInfo['active'], null, 0, null, null, null, false, $resetPassword);
                 if ($result) {
                     if ($row['username'] != $userInfo['username']) {
                         $this->logger->addInfo("Students - Username was changes from '" . $userInfo['username'] . "' to '" . $row['username'] . "' ");
                     }
                     foreach ($row as $key => $value) {
                         if (substr($key, 0, 6) == 'extra_') {
                             //an extra field
                             UserManager::update_extra_field_value($userInfo['user_id'], substr($key, 6), $value);
                         }
                     }
                     $this->logger->addInfo("Students - User updated: " . $row['username']);
                 } else {
                     $this->logger->addError("Students - User NOT updated: " . $row['username'] . " " . $row['firstname'] . " " . $row['lastname']);
                 }
             }
         }
     }
     if ($moveFile) {
         $this->moveFile($file);
     }
 }
 /**
  * Generated from @assert ('0','---') === 0.
  *
  * @covers UserManager::get_user_id_from_original_id
  */
 public function testGet_user_id_from_original_id()
 {
     $this->assertSame(0, UserManager::get_user_id_from_original_id('0', '---'));
 }