2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2002-2008 by Greg Gay, Joel Kronenberg & Heidi Hazelton*/
6 /* Adaptive Technology Resource Centre / University of Toronto */
9 /* This program is free software. You can redistribute it and/or */
10 /* modify it under the terms of the GNU General Public License */
11 /* as published by the Free Software Foundation. */
12 /************************************************************************/
15 define('AT_LANGUAGE_LOCALE_SEP', '-');
19 * Class for accessing information about a single language.
21 * @author Joel Kronenberg
22 * @see LanguageManager::getLanguage()
23 * @see LanguageManager::getMyLanguage()
31 var $regularExpression;
40 function Language($language_row) {
45 if (is_array($language_row)) {
46 $this->code = $language_row['language_code'];
47 $this->characterSet = $language_row['char_set'];
48 $this->direction = $language_row['direction'];
49 $this->regularExpression = $language_row['reg_exp'];
50 $this->nativeName = $language_row['native_name'];
51 $this->englishName = $language_row['english_name'];
52 $this->status = $language_row['status'];
53 $this->atutor_version = isset($language_row['version']) ? $language_row['version'] : VERSION;
55 } else if (is_object($language_row)) {
56 $this->cloneThis($language_row);
61 // copies the properties from $from to $this Object
62 function cloneThis($from) {
63 $vars = get_object_vars($from);
64 foreach ($vars as $key => $value) {
69 // returns whether or not the $search_string matches the regular expression
70 function isMatchHttpAcceptLanguage($search_string) {
71 return preg_match('/^(' . $this->regularExpression . ')(;q=[0-9]\\.[0-9])?$/', $search_string);
74 // returns boolean whether or not $search_string is in HTTP_USER_AGENT
75 function isMatchHttpUserAgent($search_string) {
76 return preg_match('/(\(|\[|;[\s])(' . $this->regularExpression . ')(;|\]|\))/', $search_string);
84 function getCharacterSet() {
85 return $this->characterSet;
88 function getDirection() {
89 return $this->direction;
92 function getRegularExpression() {
93 return $this->regularExpression;
96 function getAtutorVersion() {
97 return $this->atutor_version;
100 function getTranslatedName() {
101 if ($this->code == $_SESSION['lang']) {
102 return $this->nativeName;
104 // this code has to be translated:
105 return _AT('lang_' . str_replace('-', '_', $this->code));
108 function getNativeName() {
109 return $this->nativeName;
112 function getEnglishName() {
113 return $this->englishName;
116 function getStatus() {
117 return $this->status;
122 function sendContentTypeHeader() {
123 header('Content-Type: text/html; charset=' . $this->characterSet);
127 function saveToSession() {
128 $_SESSION['lang'] = $this->code;
133 * @param member_id or login for members and admin respectively
134 * @param 1 for admin, 0 for members, all other integers are ignored.
136 function saveToPreferences($id, $is_admin) {
139 if ($is_admin === 0) {
140 $sql = "UPDATE ".TABLE_PREFIX."members SET language='".$this->code."', creation_date=creation_date, last_login=last_login WHERE member_id=$id";
141 } elseif ($is_admin === 1) {
142 $sql = "UPDATE ".TABLE_PREFIX."admins SET language='".$this->code."', last_login=last_login WHERE login='$id'";
144 mysql_query($sql,$db);
149 // returns whether or not this language is right-to-left
150 // possible langues are: arabic, farsi, hebrew, urdo
152 if ($this->direction == 'rtl') {
160 // can be called staticly
161 function getParentCode($code = '') {
162 if (!$code && isset($this)) {
165 $peices = explode(AT_LANGUAGE_LOCALE_SEP, $code, 2);
170 // can be called staticly
171 function getLocale($code = '') {
172 if (!$code && isset($this)) {
175 $peices = explode(AT_LANGUAGE_LOCALE_SEP, $code, 2);
181 function getTerm($term) {
182 $sql = "SELECT *, UNIX_TIMESTAMP(L.revised_date) AS revised_date_unix FROM ".TABLE_PREFIX."language_text L WHERE L.language_code='".$this->getCode()."' AND L.variable='_template' AND L.term='$term'";
184 $result = mysql_query($sql, $this->db);
185 $row = mysql_fetch_assoc($result);
189 function getXML($part=FALSE) {
191 $xml = '<?xml version="1.0" encoding="iso-8859-1"?>
192 <!-- This is an ATutor language pack - http://www.atutor.ca-->
195 <!ELEMENT atutor-version (#PCDATA)>
196 <!ELEMENT code (#PCDATA)>
197 <!ELEMENT charset (#PCDATA)>
198 <!ELEMENT direction (#PCDATA)>
199 <!ELEMENT reg-exp (#PCDATA)>
200 <!ELEMENT native-name (#PCDATA)>
201 <!ELEMENT english-name (#PCDATA)>
202 <!ELEMENT status (#PCDATA)>
204 <!ATTLIST language code ID #REQUIRED>
208 $xml .= '<language code="'.$this->code.'">
209 <atutor-version>'.VERSION.'</atutor-version>
210 <charset>'.$this->characterSet.'</charset>
211 <direction>'.$this->direction.'</direction>
212 <reg-exp>'.$this->regularExpression.'</reg-exp>
213 <native-name>'.$this->nativeName.'</native-name>
214 <english-name>'.$this->englishName.'</english-name>
215 <status>'.$this->status.'</status>