sub_language.class.php 17 KB

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