sub_language.class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class SubLanguageManager.
  5. *
  6. * @package chamilo.admin.sublanguage
  7. */
  8. class SubLanguageManager
  9. {
  10. /**
  11. * Constructor.
  12. */
  13. public function __construct()
  14. {
  15. }
  16. /**
  17. * Get all the languages.
  18. *
  19. * @param bool $onlyActive Whether to return only active languages (default false)
  20. *
  21. * @return array All information about sub-language
  22. */
  23. public static function getAllLanguages($onlyActive = false)
  24. {
  25. $table = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  26. $sql = 'SELECT * FROM '.$table;
  27. if ($onlyActive) {
  28. $sql .= ' WHERE available = 1';
  29. }
  30. $rs = Database::query($sql);
  31. $all_languages = [];
  32. while ($row = Database::fetch_array($rs, 'ASSOC')) {
  33. $all_languages[$row['dokeos_folder']] = $row;
  34. }
  35. return $all_languages;
  36. }
  37. /**
  38. * Get all files of lang folder (forum.inc.php,gradebook.inc.php,notebook.inc.php).
  39. *
  40. * @param string $path The lang path folder (/var/www/my_lms/main/lang/spanish)
  41. * @param bool $only_main_name true if we only want the "subname" trad4all instead of trad4all.inc.php
  42. *
  43. * @return array All file of lang folder
  44. */
  45. public static function get_lang_folder_files_list($path, $only_main_name = false)
  46. {
  47. $content_dir = [];
  48. if (is_dir($path)) {
  49. if ($dh = opendir($path)) {
  50. while (($file = readdir($dh)) !== false) {
  51. if ($file[0] != '.' && substr($file, -4, strlen($file)) == '.php') {
  52. if ($only_main_name) {
  53. if ($file != '' && strpos($file, '.inc.php')) {
  54. $content_dir[] = substr($file, 0, strpos($file, '.inc.php'));
  55. }
  56. } else {
  57. $content_dir[] = $file;
  58. }
  59. }
  60. }
  61. }
  62. closedir($dh);
  63. }
  64. return $content_dir;
  65. }
  66. /**
  67. * Get all information of sub-language.
  68. *
  69. * @param int $parent_id The parent id(Language father id)
  70. * @param int $sub_language_id The sub language id
  71. *
  72. * @return array All information about sub-language
  73. */
  74. public static function get_all_information_of_sub_language($parent_id, $sub_language_id)
  75. {
  76. $table = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  77. $parent_id = intval($parent_id);
  78. $sub_language_id = intval($sub_language_id);
  79. $sql = "SELECT * FROM $table
  80. WHERE
  81. parent_id = $parent_id AND
  82. id = $sub_language_id";
  83. $rs = Database::query($sql);
  84. $all_information = [];
  85. while ($row = Database::fetch_array($rs, 'ASSOC')) {
  86. $all_information = $row;
  87. }
  88. return $all_information;
  89. }
  90. /**
  91. * Get all information of language.
  92. *
  93. * @param int $parent_id The parent id(Language father id)
  94. *
  95. * @return array All information about language
  96. */
  97. public static function get_all_information_of_language($parent_id)
  98. {
  99. $table = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  100. $sql = 'SELECT * FROM '.$table.' WHERE id = "'.intval($parent_id).'"';
  101. $rs = Database::query($sql);
  102. $all_information = [];
  103. while ($row = Database::fetch_array($rs, 'ASSOC')) {
  104. $all_information = $row;
  105. }
  106. return $all_information;
  107. }
  108. /**
  109. * Get all information of chamilo file.
  110. *
  111. * @param string $system_path_file The chamilo path file (/var/www/chamilo/main/lang/spanish/gradebook.inc.php)
  112. * @param bool $get_as_string_index Whether we want to remove the '$' prefix in the results or not
  113. *
  114. * @return array Contains all information of chamilo file
  115. */
  116. public static function get_all_language_variable_in_file($system_path_file, $get_as_string_index = false)
  117. {
  118. $res_list = [];
  119. if (!is_readable($system_path_file)) {
  120. return $res_list;
  121. }
  122. $info_file = file($system_path_file);
  123. foreach ($info_file as $line) {
  124. if (substr($line, 0, 1) != '$') {
  125. continue;
  126. }
  127. list($var, $val) = explode('=', $line, 2);
  128. $var = trim($var);
  129. $val = trim($val);
  130. if ($get_as_string_index) { //remove the prefix $
  131. $var = substr($var, 1);
  132. }
  133. $res_list[$var] = $val;
  134. }
  135. return $res_list;
  136. }
  137. /**
  138. * Add file in sub-language directory and add header(tag php).
  139. *
  140. * @param string $system_path_file The chamilo path file (/var/www/chamilo/main/lang/spanish/gradebook.inc.php)
  141. *
  142. * @return bool
  143. */
  144. public static function add_file_in_language_directory($system_path_file)
  145. {
  146. $return_value = @file_put_contents($system_path_file, '<?php'.PHP_EOL);
  147. return $return_value;
  148. }
  149. /**
  150. * Write in file of sub-language.
  151. *
  152. * @param string $path_file The path file (/var/www/chamilo/main/lang/spanish/gradebook.inc.php)
  153. * @param string $new_term The new sub-language
  154. * @param string $new_variable The language variable
  155. *
  156. * @return bool True on success, False on error
  157. */
  158. public static function write_data_in_file($path_file, $new_term, $new_variable)
  159. {
  160. $return_value = false;
  161. $new_data = $new_variable.'='.$new_term;
  162. $resource = @fopen($path_file, "a");
  163. if (file_exists($path_file) && $resource) {
  164. if (fwrite($resource, $new_data.PHP_EOL) === false) {
  165. //not allow to write
  166. $return_value = false;
  167. } else {
  168. $return_value = true;
  169. }
  170. fclose($resource);
  171. }
  172. return $return_value;
  173. }
  174. /**
  175. * Add directory for sub-language.
  176. *
  177. * @param string $sub_language_dir The sub-language directory ( e.g. 'spanish_corporate' )
  178. *
  179. * @return bool True on success, false on failure
  180. */
  181. public static function add_language_directory($sub_language_dir)
  182. {
  183. if (empty($sub_language_dir)) {
  184. return false;
  185. }
  186. $dir = api_get_path(SYS_LANG_PATH).$sub_language_dir;
  187. if (is_dir($dir)) {
  188. return true;
  189. } //even if the dir already exists, we reach the objective of having the directory there
  190. return @mkdir($dir, api_get_permissions_for_new_directories());
  191. }
  192. /**
  193. * Delete sub-language.
  194. * In order to avoid deletion of main laguages, we check the existence of a parent.
  195. *
  196. * @param int $parent_id The parent id
  197. * @param bool $sub_language_id
  198. *
  199. * @return mixed True on success, false on error
  200. */
  201. public static function remove_sub_language($parent_id, $sub_language_id)
  202. {
  203. if (empty($parent_id) ||
  204. (intval($parent_id) != $parent_id) ||
  205. empty($sub_language_id) ||
  206. (intval($sub_language_id) != $sub_language_id)
  207. ) {
  208. return false;
  209. }
  210. $table = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  211. $sql = 'SELECT dokeos_folder FROM '.$table.'
  212. WHERE parent_id = '.$parent_id.' and id = '.$sub_language_id;
  213. $res = Database::query($sql);
  214. if ($res === false or Database::num_rows($res) < 1) {
  215. return false;
  216. }
  217. $row = Database::fetch_assoc($res);
  218. $res = self::remove_language_directory($row['dokeos_folder']);
  219. if ($res === false) {
  220. return false;
  221. } //can't delete dir, so do not delete language record
  222. $sql = 'DELETE FROM '.$table.'
  223. WHERE id= '.intval($sub_language_id);
  224. $res = Database::query($sql);
  225. return $res;
  226. }
  227. /**
  228. * Remove directory for sub-language.
  229. *
  230. * @param string $sub_language_dir The sub-language path directory ( e.g. 'spanish_corporate'' )
  231. *
  232. * @return bool True on success, false on failure
  233. */
  234. public static function remove_language_directory($sub_language_dir)
  235. {
  236. if (empty($sub_language_dir)) {
  237. return false;
  238. }
  239. $dir = api_get_path(SYS_LANG_PATH).$sub_language_dir;
  240. if (!is_dir($dir)) {
  241. return true;
  242. } //even if the dir does not exist, we reach the objective of not having the directory there
  243. $content = self::get_lang_folder_files_list($dir);
  244. if (count($content) > 0) {
  245. foreach ($content as $value_content) {
  246. $path_file = $dir.'/'.$value_content;
  247. unlink($path_file);
  248. }
  249. return @rmdir($dir);
  250. } else {
  251. return @rmdir($dir);
  252. }
  253. }
  254. /**
  255. * check if language exist by id.
  256. *
  257. * @param int $language_id
  258. *
  259. * @return bool
  260. */
  261. public static function check_if_exist_language_by_id($language_id)
  262. {
  263. $table = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  264. $sql = 'SELECT count(*) as count
  265. FROM '.$table.'
  266. WHERE id="'.intval($language_id).'"';
  267. $rs = Database::query($sql);
  268. if (Database::num_rows($rs) > 0) {
  269. if (Database::result($rs, 0, 'count') == 1) {
  270. return true;
  271. } else {
  272. return false;
  273. }
  274. } else {
  275. return false;
  276. }
  277. }
  278. /**
  279. * Get name of language by id.
  280. *
  281. * @param int $language_id The language id
  282. *
  283. * @return string The original name of language
  284. */
  285. public static function get_name_of_language_by_id($language_id)
  286. {
  287. $table = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  288. $language_id = intval($language_id);
  289. $sql = "SELECT original_name
  290. FROM $table
  291. WHERE id = $language_id";
  292. $rs = Database::query($sql);
  293. if (Database::num_rows($rs) > 0) {
  294. return Database::result($rs, 0, 'original_name');
  295. } else {
  296. return '';
  297. }
  298. }
  299. /**
  300. * Verified if language is sub-language.
  301. *
  302. * @param int $language_id
  303. *
  304. * @return bool
  305. */
  306. public static function check_if_language_is_sub_language($language_id)
  307. {
  308. $table = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  309. $sql = 'SELECT count(*) AS count FROM '.$table.'
  310. WHERE id = '.intval($language_id).' AND NOT ISNULL(parent_id)';
  311. $rs = Database::query($sql);
  312. if (Database::num_rows($rs) > 0 && Database::result($rs, '0', 'count') == 1) {
  313. return true;
  314. } else {
  315. return false;
  316. }
  317. }
  318. /**
  319. * @param int $language_id
  320. *
  321. * @return bool
  322. */
  323. public static function check_if_language_is_used($language_id)
  324. {
  325. $language_info = self::get_all_information_of_language($language_id);
  326. $table = Database::get_main_table(TABLE_MAIN_USER);
  327. $sql = 'SELECT count(*) AS count FROM '.$table.'
  328. WHERE language ="'.Database::escape_string($language_info['english_name']).'"';
  329. $rs = Database::query($sql);
  330. if (Database::num_rows($rs) > 0 && Database::result($rs, '0', 'count') >= 1) {
  331. return true;
  332. } else {
  333. return false;
  334. }
  335. }
  336. /**
  337. * Verified if language is father of an sub-language.
  338. *
  339. * @param int $language_id The language id
  340. *
  341. * @return bool
  342. */
  343. public static function check_if_language_is_father($language_id)
  344. {
  345. $table = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  346. $sql = 'SELECT count(*) AS count FROM '.$table.'
  347. WHERE parent_id= '.intval($language_id).' AND NOT ISNULL(parent_id);';
  348. $rs = Database::query($sql);
  349. if (Database::num_rows($rs) > 0 && Database::result($rs, '0', 'count') == 1) {
  350. return true;
  351. } else {
  352. return false;
  353. }
  354. }
  355. /**
  356. * Make unavailable the language.
  357. *
  358. * @param int $language_id The language id
  359. *
  360. * @return bool
  361. */
  362. public static function make_unavailable_language($language_id)
  363. {
  364. $tbl_admin_languages = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  365. $sql = "UPDATE $tbl_admin_languages SET available='0'
  366. WHERE id = ".intval($language_id)."";
  367. $result = Database::query($sql);
  368. return $result !== false; //only return false on sql error
  369. }
  370. /**
  371. * Make available the language.
  372. *
  373. * @param int $language_id language id
  374. *
  375. * @return bool
  376. */
  377. public static function make_available_language($language_id)
  378. {
  379. $tbl_admin_languages = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  380. $sql = "UPDATE $tbl_admin_languages SET available='1'
  381. WHERE id = ".intval($language_id)."";
  382. $result = Database::query($sql);
  383. return $result !== false; //only return false on sql error
  384. }
  385. /**
  386. * Set platform language.
  387. *
  388. * @param int $language_id The language id
  389. *
  390. * @return bool
  391. */
  392. public static function set_platform_language($language_id)
  393. {
  394. if (empty($language_id) || (intval($language_id) != $language_id)) {
  395. return false;
  396. }
  397. $language_id = intval($language_id);
  398. $tbl_admin_languages = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  399. $tbl_settings_current = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  400. $sql = "SELECT english_name FROM $tbl_admin_languages
  401. WHERE id = $language_id";
  402. $result = Database::query($sql);
  403. $lang = Database::fetch_array($result);
  404. $sql_update_2 = "UPDATE $tbl_settings_current SET selected_value = '".$lang['english_name']."'
  405. WHERE variable='platformLanguage'";
  406. $result_2 = Database::query($sql_update_2);
  407. Event::addEvent(
  408. LOG_PLATFORM_LANGUAGE_CHANGE,
  409. LOG_PLATFORM_LANGUAGE,
  410. $lang['english_name']
  411. );
  412. return $result_2 !== false;
  413. }
  414. /**
  415. * Get platform language ID.
  416. *
  417. * @return int The platform language ID
  418. */
  419. public static function get_platform_language_id()
  420. {
  421. $name = api_get_setting('platformLanguage');
  422. $tbl_admin_languages = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  423. $sql = "SELECT id FROM $tbl_admin_languages WHERE english_name ='$name'";
  424. $res = Database::query($sql);
  425. if (Database::num_rows($res) < 1) {
  426. return false;
  427. }
  428. $row = Database::fetch_array($res);
  429. return (int) $row['id'];
  430. }
  431. /**
  432. * Get parent language path (or null if no parent).
  433. *
  434. * @param string $language_path Children language path
  435. *
  436. * @return string Parent language path or null
  437. */
  438. public static function get_parent_language_path($language_path)
  439. {
  440. $tbl_admin_languages = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  441. $sql = "SELECT dokeos_folder
  442. FROM ".$tbl_admin_languages."
  443. WHERE id = (
  444. SELECT parent_id FROM ".$tbl_admin_languages."
  445. WHERE dokeos_folder = '".Database::escape_string($language_path)."'
  446. )
  447. ";
  448. $result = Database::query($sql);
  449. if (Database::num_rows($result) == 0) {
  450. return null;
  451. }
  452. $row = Database::fetch_array($result);
  453. return $row['dokeos_folder'];
  454. }
  455. /**
  456. * Get language matching isocode.
  457. *
  458. * @param string $isocode The language isocode (en, es, fr, zh-TW, etc)
  459. *
  460. * @return mixed English name of the matching language, or false if no active language could be found
  461. */
  462. public static function getLanguageFromIsocode($isocode)
  463. {
  464. $isocode = Database::escape_string($isocode);
  465. $adminLanguagesTable = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  466. // select language - if case several languages match, get the last (more recent) one
  467. $sql = "SELECT english_name
  468. FROM ".$adminLanguagesTable."
  469. WHERE
  470. isocode ='$isocode' AND
  471. available = 1
  472. ORDER BY id
  473. DESC LIMIT 1";
  474. $res = Database::query($sql);
  475. if (Database::num_rows($res) < 1) {
  476. return false;
  477. }
  478. $row = Database::fetch_assoc($res);
  479. return $row['english_name'];
  480. }
  481. /**
  482. * Get best language in browser preferences.
  483. *
  484. * @param string $preferences The browser-configured language preferences (e.g. "en,es;q=0.7;en-us;q=0.3", etc)
  485. *
  486. * @return mixed English name of the matching language, or false if no active language could be found
  487. */
  488. public static function getLanguageFromBrowserPreference($preferences)
  489. {
  490. if (empty($preferences)) {
  491. return false;
  492. }
  493. $preferencesArray = explode(',', $preferences);
  494. if (count($preferencesArray) > 0) {
  495. foreach ($preferencesArray as $pref) {
  496. $s = strpos($pref, ';');
  497. if ($s >= 2) {
  498. $code = substr($pref, 0, $s);
  499. } else {
  500. $code = $pref;
  501. }
  502. $name = self::getLanguageFromIsocode($code);
  503. if ($name !== false) {
  504. return $name;
  505. }
  506. }
  507. }
  508. return false;
  509. }
  510. }