';
}
return $html;
}
/**
* @param string $languageIsoCode
*
* @return string
*/
function languageToCountryIsoCode($languageIsoCode)
{
$allow = api_get_configuration_value('language_flags_by_country');
// @todo save in DB
switch ($languageIsoCode) {
case 'ar':
$country = 'ae';
break;
case 'bs':
$country = 'ba';
break;
case 'ca':
$country = 'es';
if ($allow) {
$country = 'catalan';
}
break;
case 'cs':
$country = 'cz';
break;
case 'da':
$country = 'dk';
break;
case 'el':
$country = 'ae';
break;
case 'en':
$country = 'gb';
break;
case 'eu': // Euskera
$country = 'es';
if ($allow) {
$country = 'basque';
}
break;
case 'gl': // galego
$country = 'es';
if ($allow) {
$country = 'galician';
}
break;
case 'he':
$country = 'il';
break;
case 'ja':
$country = 'jp';
break;
case 'ka':
$country = 'ge';
break;
case 'ko':
$country = 'kr';
break;
case 'ms':
$country = 'my';
break;
case 'pt-BR':
$country = 'br';
break;
case 'qu':
$country = 'pe';
break;
case 'sl':
$country = 'si';
break;
case 'sv':
$country = 'se';
break;
case 'uk': // Ukraine
$country = 'ua';
break;
case 'zh-TW':
case 'zh':
$country = 'cn';
break;
default:
$country = $languageIsoCode;
break;
}
$country = strtolower($country);
return $country;
}
/**
* Returns a list of all the languages that are made available by the admin.
*
* @return array An array with all languages. Structure of the array is
* array['name'] = An array with the name of every language
* array['folder'] = An array with the corresponding names of the language-folders in the filesystem
*/
function api_get_languages()
{
$table = Database::get_main_table(TABLE_MAIN_LANGUAGE);
$sql = "SELECT * FROM $table WHERE available='1'
ORDER BY original_name ASC";
$result = Database::query($sql);
$languages = [];
while ($row = Database::fetch_array($result, 'ASSOC')) {
$languages[$row['isocode']] = $row['original_name'];
}
return $languages;
}
/**
* Returns a list of all the languages that are made available by the admin.
*
* @return array
*/
function api_get_languages_to_array()
{
$tbl_language = Database::get_main_table(TABLE_MAIN_LANGUAGE);
$sql = "SELECT * FROM $tbl_language WHERE available='1' ORDER BY original_name ASC";
$result = Database::query($sql);
$languages = [];
while ($row = Database::fetch_array($result)) {
$languages[$row['dokeos_folder']] = $row['original_name'];
}
return $languages;
}
/**
* Returns the id (the database id) of a language.
*
* @param string language name (the corresponding name of the language-folder in the filesystem)
*
* @return int id of the language
*/
function api_get_language_id($language)
{
$tbl_language = Database::get_main_table(TABLE_MAIN_LANGUAGE);
if (empty($language)) {
return null;
}
$language = Database::escape_string($language);
$sql = "SELECT id FROM $tbl_language
WHERE dokeos_folder = '$language' LIMIT 1";
$result = Database::query($sql);
$row = Database::fetch_array($result);
return $row['id'];
}
/**
* Get the language information by its id.
*
* @param int $languageId
*
* @throws Exception
*
* @return array
*/
function api_get_language_info($languageId)
{
if (empty($languageId)) {
return [];
}
$language = Database::getManager()->find(Language::class, $languageId);
if (!$language) {
return [];
}
return [
'id' => $language->getId(),
'original_name' => $language->getOriginalName(),
'english_name' => $language->getEnglishName(),
'isocode' => $language->getIsocode(),
'dokeos_folder' => $language->getDokeosFolder(),
'available' => $language->getAvailable(),
'parent_id' => $language->getParent() ? $language->getParent()->getId() : null,
];
}
/**
* @param string $code
*
* @return Language
*/
function api_get_language_from_iso($code)
{
$em = Database::getManager();
return $em->getRepository(Language::class)->findOneBy(['isocode' => $code]);
}
/**
* Returns the name of the visual (CSS) theme to be applied on the current page.
* The returned name depends on the platform, course or user -wide settings.
*
* @return string The visual theme's name, it is the name of a folder inside web/css/themes
*/
function api_get_visual_theme()
{
static $visual_theme;
if (!isset($visual_theme)) {
// Get style directly from DB
/*$styleFromDatabase = api_get_settings_params_simple(
[
'variable = ? AND access_url = ?' => [
'stylesheets',
api_get_current_access_url_id(),
],
]
);
if ($styleFromDatabase) {
$platform_theme = $styleFromDatabase['selected_value'];
} else {
$platform_theme = api_get_setting('stylesheets');
}*/
$platform_theme = api_get_setting('stylesheets');
// Platform's theme.
$visual_theme = $platform_theme;
if ('true' == api_get_setting('user_selected_theme')) {
$user_info = api_get_user_info();
if (isset($user_info['theme'])) {
$user_theme = $user_info['theme'];
if (!empty($user_theme)) {
$visual_theme = $user_theme;
// User's theme.
}
}
}
$course_id = api_get_course_id();
if (!empty($course_id)) {
if ('true' == api_get_setting('allow_course_theme')) {
$course_theme = api_get_course_setting('course_theme', $course_id);
if (!empty($course_theme) && -1 != $course_theme) {
if (!empty($course_theme)) {
// Course's theme.
$visual_theme = $course_theme;
}
}
$allow_lp_theme = api_get_course_setting('allow_learning_path_theme');
if (1 == $allow_lp_theme) {
/*global $lp_theme_css, $lp_theme_config;
// These variables come from the file lp_controller.php.
if (!$lp_theme_config) {
if (!empty($lp_theme_css)) {
// LP's theme.
$visual_theme = $lp_theme_css;
}
}*/
}
}
}
if (empty($visual_theme)) {
$visual_theme = 'chamilo';
}
/*global $lp_theme_log;
if ($lp_theme_log) {
$visual_theme = $platform_theme;
}*/
}
return $visual_theme;
}
/**
* Returns a list of CSS themes currently available in the CSS folder
* The folder must have a default.css file.
*
* @param bool $getOnlyThemeFromVirtualInstance Used by the vchamilo plugin
*
* @return array list of themes directories from the css folder
* Note: Directory names (names of themes) in the file system should contain ASCII-characters only
*/
function api_get_themes($getOnlyThemeFromVirtualInstance = false)
{
// This configuration value is set by the vchamilo plugin
$virtualTheme = api_get_configuration_value('virtual_css_theme_folder');
$readCssFolder = function ($dir) use ($virtualTheme) {
$finder = new Finder();
$themes = $finder->directories()->in($dir)->depth(0)->sortByName();
$list = [];
/** @var Symfony\Component\Finder\SplFileInfo $theme */
foreach ($themes as $theme) {
$folder = $theme->getFilename();
// A theme folder is consider if there's a default.css file
if (!file_exists($theme->getPathname().'/default.css')) {
continue;
}
$name = ucwords(str_replace('_', ' ', $folder));
if ($folder == $virtualTheme) {
continue;
}
$list[$folder] = $name;
}
return $list;
};
$dir = api_get_path(SYS_CSS_PATH).'themes/';
$list = $readCssFolder($dir);
if (!empty($virtualTheme)) {
$newList = $readCssFolder($dir.'/'.$virtualTheme);
if ($getOnlyThemeFromVirtualInstance) {
return $newList;
}
$list = $list + $newList;
asort($list);
}
return $list;
}
/**
* Find the largest sort value in a given user_course_category
* This function is used when we are moving a course to a different category
* and also when a user subscribes to courses (the new course is added at the end of the main category.
*
* @param int $courseCategoryId the id of the user_course_category
* @param int $userId
*
* @return int the value of the highest sort of the user_course_category
*/
function api_max_sort_value($courseCategoryId, $userId)
{
$user = api_get_user_entity($userId);
$userCourseCategory = Database::getManager()->getRepository(UserCourseCategory::class)->find($courseCategoryId);
return null === $user ? 0 : $user->getMaxSortValue($userCourseCategory);
}
/**
* Transforms a number of seconds in hh:mm:ss format.
*
* @author Julian Prud'homme
*
* @param int $seconds number of seconds
* @param string $space
* @param bool $showSeconds
* @param bool $roundMinutes
*
* @return string the formatted time
*/
function api_time_to_hms($seconds, $space = ':', $showSeconds = true, $roundMinutes = false)
{
// $seconds = -1 means that we have wrong data in the db.
if (-1 == $seconds) {
return
get_lang('Unknown').
Display::return_icon(
'info2.gif',
get_lang('The datas about this user were registered when the calculation of time spent on the platform wasn\'t possible.'),
['align' => 'absmiddle', 'hspace' => '3px']
);
}
// How many hours ?
$hours = floor($seconds / 3600);
// How many minutes ?
$min = floor(($seconds - ($hours * 3600)) / 60);
if ($roundMinutes) {
if ($min >= 45) {
$min = 45;
}
if ($min >= 30 && $min <= 44) {
$min = 30;
}
if ($min >= 15 && $min <= 29) {
$min = 15;
}
if ($min >= 0 && $min <= 14) {
$min = 0;
}
}
// How many seconds
$sec = floor($seconds - ($hours * 3600) - ($min * 60));
if ($hours < 10) {
$hours = "0$hours";
}
if ($sec < 10) {
$sec = "0$sec";
}
if ($min < 10) {
$min = "0$min";
}
$seconds = '';
if ($showSeconds) {
$seconds = $space.$sec;
}
return $hours.$space.$min.$seconds;
}
/* FILE SYSTEM RELATED FUNCTIONS */
/**
* Returns the permissions to be assigned to every newly created directory by the web-server.
* The return value is based on the platform administrator's setting
* "Administration > Configuration settings > Security > Permissions for new directories".
*
* @return int returns the permissions in the format "Owner-Group-Others, Read-Write-Execute", as an integer value
*/
function api_get_permissions_for_new_directories()
{
static $permissions;
if (!isset($permissions)) {
$permissions = trim(api_get_setting('permissions_for_new_directories'));
// The default value 0777 is according to that in the platform administration panel after fresh system installation.
$permissions = octdec(!empty($permissions) ? $permissions : '0777');
}
return $permissions;
}
/**
* Returns the permissions to be assigned to every newly created directory by the web-server.
* The return value is based on the platform administrator's setting
* "Administration > Configuration settings > Security > Permissions for new files".
*
* @return int returns the permissions in the format
* "Owner-Group-Others, Read-Write-Execute", as an integer value
*/
function api_get_permissions_for_new_files()
{
static $permissions;
if (!isset($permissions)) {
$permissions = trim(api_get_setting('permissions_for_new_files'));
// The default value 0666 is according to that in the platform
// administration panel after fresh system installation.
$permissions = octdec(!empty($permissions) ? $permissions : '0666');
}
return $permissions;
}
/**
* Deletes a file, or a folder and its contents.
*
* @author Aidan Lister