/**
 * Check if language exists
 * @param   string  Original language name (Occitan, Wallon, Vlaams)
 * @param   string  English language name (occitan, wallon, flanders)
 * @param   string  ISO code (fr_FR, ...)
 * @param   int     Whether the sublanguage is published (0=unpublished, 1=published)
 * @return  array   Array describing the number of items found that match the
 *                  current language insert attempt (original_name => true,
 *                  english_name => true, isocode => true,
 *                  execute_add => true/false). If execute_add is true, then we
 *                  can proceed.
 * @todo This function is not transaction-safe and should probably be included
 *       inside the add_sub_language function.
 */
function check_if_language_exist($original_name, $english_name, $isocode, $sublanguage_available)
{
    $tbl_admin_languages = Database::get_main_table(TABLE_MAIN_LANGUAGE);
    $sql_original_name = 'SELECT count(*) AS count_original_name FROM ' . $tbl_admin_languages . ' WHERE original_name="' . Database::escape_string($original_name) . '" ';
    $sql_english_name = 'SELECT count(*) AS count_english_name FROM ' . $tbl_admin_languages . ' WHERE english_name="' . Database::escape_string($english_name) . '" ';
    //$sql_isocode='SELECT count(*) AS count_isocode FROM '.$tbl_admin_languages.' WHERE isocode="'.Database::escape_string($isocode).'" ';
    $rs_original_name = Database::query($sql_original_name);
    $rs_english_name = Database::query($sql_english_name);
    //$rs_isocode=Database::query($sql_isocode);
    $count_original_name = Database::result($rs_original_name, 0, 'count_original_name');
    $count_english_name = Database::result($rs_english_name, 0, 'count_english_name');
    //$count_isocode=Database::result($rs_isocode,0,'count_isocode');
    $has_error = false;
    $message_information = array();
    if ($count_original_name == 1) {
        $has_error = true;
        $message_information['original_name'] = true;
    }
    if ($count_english_name == 1) {
        $has_error = true;
        $message_information['english_name'] = true;
    }
    $iso_list = api_get_platform_isocodes();
    $iso_list = array_values($iso_list);
    if (!in_array($isocode, $iso_list)) {
        $has_error = true;
        $message_information['isocode'] = true;
    }
    if ($has_error === true) {
        $message_information['execute_add'] = false;
    }
    if ($has_error === false) {
        $message_information['execute_add'] = true;
    }
    return $message_information;
}
 /**
  * Update user information with all the parameters passed to this function
  * @param int The ID of the user to be updated
  * @param string The user's firstname
  * @param string The user's lastname
  * @param string The user's username (login)
  * @param string The user's password
  * @param string The authentication source (default: "platform")
  * @param string The user's e-mail address
  * @param int The user's status
  * @param string The user's official code (usually just an internal institutional code)
  * @param string The user's phone number
  * @param string The user's picture URL (internal to the Chamilo directory)
  * @param int The user ID of the person who registered this user (optional, defaults to null)
  * @param int The department of HR in which the user is registered (optional, defaults to 0)
  * @param    array    A series of additional fields to add to this user as extra fields (optional, defaults to null)
  * @return boolean true if the user information was updated
  * @assert (false) === false
  */
 public static function update_user($user_id, $firstname, $lastname, $username, $password = null, $auth_source = null, $email = null, $status = STUDENT, $official_code = null, $phone = null, $picture_uri = null, $expiration_date = null, $active = 1, $creator_id = null, $hr_dept_id = 0, $extra = null, $language = 'english', $encrypt_method = '', $send_email = false, $reset_password = 0)
 {
     global $_configuration;
     $original_password = $password;
     $user_info = api_get_user_info($user_id, false, true);
     if ($reset_password == 0) {
         $password = null;
         $auth_source = $user_info['auth_source'];
     } elseif ($reset_password == 1) {
         $original_password = $password = api_generate_password();
         $auth_source = PLATFORM_AUTH_SOURCE;
     } elseif ($reset_password == 2) {
         $password = $password;
         $auth_source = PLATFORM_AUTH_SOURCE;
     } elseif ($reset_password == 3) {
         $password = $password;
         $auth_source = $auth_source;
     }
     if ($user_id != strval(intval($user_id))) {
         return false;
     }
     if ($user_id === false) {
         return false;
     }
     // Checking the user language.
     $languages = api_get_platform_isocodes();
     if (!in_array($language, $languages)) {
         $language = Container::getTranslator()->getLocale();
     }
     if (!is_null($password)) {
         if ($encrypt_method == '') {
             $password = api_get_encrypted_password($password);
         } else {
             if ($_configuration['password_encryption'] === $encrypt_method) {
                 if ($encrypt_method == 'md5' && !preg_match('/^[A-Fa-f0-9]{32}$/', $password)) {
                     return api_set_failure('encrypt_method invalid');
                 } else {
                     if ($encrypt_method == 'sha1' && !preg_match('/^[A-Fa-f0-9]{40}$/', $password)) {
                         return api_set_failure('encrypt_method invalid');
                     }
                 }
             } else {
                 return api_set_failure('encrypt_method invalid');
             }
         }
     }
     $em = Database::getManager();
     /** @var Chamilo\UserBundle\Entity\User $user */
     $user = $em->getRepository('ChamiloUserBundle:User')->find($user_id);
     if (is_array($extra) && count($extra) > 0) {
         $res = true;
         foreach ($extra as $name => $value) {
             //$userField = $em->getRepository('ChamiloUserBundle:UserField')->findOneByName($name);
             $res = $res && self::update_extra_field_value($user_id, $name, $value);
         }
     }
     if ($user_info['active'] != $active) {
         self::change_active_state($user_id, $active);
     }
     // Updating user
     $user->setLastname($lastname)->setFirstname($firstname)->setUsername($username)->setAuthSource($auth_source)->setLanguage($language)->setEmail($email)->setOfficialCode($official_code)->setPhone($phone)->setPictureUri($picture_uri)->setExpirationDate($expiration_date)->setActive($active)->setHrDeptId($hr_dept_id);
     if (!empty($original_password)) {
         $user->setPlainPassword($original_password);
     }
     if (is_array($status)) {
         foreach ($status as $groupId) {
             $group = $em->getRepository('ChamiloUserBundle:Group')->find($groupId);
             $user->addGroup($group);
         }
     } else {
         $group = $em->getRepository('ChamiloUserBundle:Group')->find($status);
         $user->addGroup($group);
     }
     Container::getUserManager()->updateUser($user, true);
     if (!empty($email) && $send_email) {
         $recipient_name = api_get_person_name($firstname, $lastname, null, PERSON_NAME_EMAIL_ADDRESS);
         $emailsubject = '[' . api_get_setting('platform.site_name') . '] ' . get_lang('YourReg') . ' ' . api_get_setting('platform.site_name');
         $sender_name = api_get_person_name(api_get_setting('platform.administrator_name'), api_get_setting('platform.administrator_surname'), null, PERSON_NAME_EMAIL_ADDRESS);
         $email_admin = api_get_setting('platform.administrator_email');
         $emailbody = null;
         /*api_mail_html($recipient_name, $email, $emailsubject,
           $emailbody, $sender_name, $email_admin);*/
     }
     $user_info = api_get_user_info($user_id);
     Event::addEvent(LOG_USER_UPDATED, LOG_USER_ID, $user_id, api_get_utc_datetime(), api_get_user_id());
     Event::addEvent(LOG_USER_UPDATED, LOG_USER_OBJECT, $user_info, api_get_utc_datetime(), api_get_user_id());
     return $user_id;
 }