move code up one directory
[atutor.git] / mods / _core / languages / classes / Language.class.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2010                                              */
6 /* Inclusive Design Institute                                           */
7 /* http://atutor.ca                                                     */
8 /* This program is free software. You can redistribute it and/or        */
9 /* modify it under the terms of the GNU General Public License          */
10 /* as published by the Free Software Foundation.                        */
11 /************************************************************************/
12 // $Id$
13
14 define('AT_LANGUAGE_LOCALE_SEP', '-');
15
16 /**
17 * Language
18 * Class for accessing information about a single language.
19 * @access       public
20 * @author       Joel Kronenberg
21 * @see          LanguageManager::getLanguage()
22 * @see          LanguageManager::getMyLanguage()
23 * @package      Language
24 */
25 class Language {
26         // all private
27         var $code;
28         var $characterSet;
29         var $direction;
30         var $regularExpression;
31         var $nativeName;
32         var $englishName;
33         var $status;
34         var $atutor_version;
35
36         var $db;
37
38         // constructor
39         function Language($language_row) {
40                 global $db;
41
42                 $this->db = $db;
43
44                 if (is_array($language_row)) {
45                         $this->code              = $language_row['language_code'];
46                         $this->characterSet      = $language_row['char_set'];
47                         $this->direction         = $language_row['direction'];
48                         $this->regularExpression = $language_row['reg_exp'];
49                         $this->nativeName        = $language_row['native_name'];
50                         $this->englishName       = $language_row['english_name'];
51                         $this->status            = $language_row['status'];
52                         $this->atutor_version    = isset($language_row['version']) ? $language_row['version'] : VERSION;
53
54                 } else if (is_object($language_row)) {
55                         $this->cloneThis($language_row);
56                 }
57         }
58
59         // private
60         // copies the properties from $from to $this Object
61         function cloneThis($from) {
62                 $vars = get_object_vars($from);
63                 foreach ($vars as $key => $value) {
64                         $this->$key = $value;
65                 }
66         }
67
68         // returns whether or not the $search_string matches the regular expression
69         function isMatchHttpAcceptLanguage($search_string) {
70                 return preg_match('/^(' . $this->regularExpression . ')(;q=[0-9]\\.[0-9])?$/', $search_string);
71         }
72
73         // returns boolean whether or not $search_string is in HTTP_USER_AGENT
74         function isMatchHttpUserAgent($search_string) {
75                 return preg_match('/(\(|\[|;[\s])(' . $this->regularExpression . ')(;|\]|\))/', $search_string);
76
77         }
78
79         function getCode() {
80                 return $this->code;
81         }
82
83         function getCharacterSet() {
84                 return $this->characterSet;
85         }
86
87         function getDirection() {
88                 return $this->direction;
89         }
90
91         function getRegularExpression() {
92                 return $this->regularExpression;
93         }
94
95         function getAtutorVersion() {
96                 return $this->atutor_version;
97         }
98
99         function getTranslatedName() {
100                 if ($this->code == $_SESSION['lang']) {
101                         return $this->nativeName;
102                 }
103                 // this code has to be translated:
104                 return _AT('lang_' . str_replace('-', '_', $this->code));
105         }
106
107         function getNativeName() {
108                 return $this->nativeName;
109         }
110
111         function getEnglishName() {
112                 return $this->englishName;
113         }
114
115         function getStatus() {
116                 return $this->status;
117         }
118
119
120         // public
121         function sendContentTypeHeader() {
122                 header('Content-Type: text/html; charset=' . $this->characterSet);
123         }
124
125         // public
126         function saveToSession() {
127                 $_SESSION['lang'] = $this->code;
128         }
129
130         /* 
131          * public
132          * @param       member_id or login for members and admin respectively
133          * @param       1 for admin, 0 for members, all other integers are ignored. 
134          */
135         function saveToPreferences($id, $is_admin) {
136                 global $db;
137                 if ($id) {
138                         if ($is_admin === 0) {
139                                 $sql = "UPDATE ".TABLE_PREFIX."members SET language='".$this->code."', creation_date=creation_date, last_login=last_login WHERE member_id=$id";
140                         } elseif ($is_admin === 1) {
141                                 $sql = "UPDATE ".TABLE_PREFIX."admins SET language='".$this->code."', last_login=last_login WHERE login='$id'";
142                         }
143                         mysql_query($sql,$db);
144                 }
145         }
146
147         // public
148         // returns whether or not this language is right-to-left
149         // possible langues are: arabic, farsi, hebrew, urdo
150         function isRTL() {
151                 if ($this->direction == 'rtl') {
152                         return true;
153                 } // else:
154
155                 return false;
156         }
157
158         // public
159         // can be called staticly
160         function getParentCode($code = '') {
161                 if (!$code && isset($this)) {
162                         $code = $this->code;
163                 }
164                 $peices = explode(AT_LANGUAGE_LOCALE_SEP, $code, 2);
165                 return $peices[0];
166         }
167
168         // public
169         // can be called staticly
170         function getLocale($code = '') {
171                 if (!$code && isset($this)) {
172                         $code = $this->code;
173                 }
174                 $peices = explode(AT_LANGUAGE_LOCALE_SEP, $code, 2);
175                 return $peices[1];
176         }
177
178         
179         // public
180         function getTerm($term) {
181                 $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'";
182
183                 $result = mysql_query($sql, $this->db);
184                 $row = mysql_fetch_assoc($result);
185                 return $row;
186         }
187
188         function getXML($part=FALSE) {
189                 if (!$part) {
190                         $xml = '<?xml version="1.0" encoding="iso-8859-1"?>
191                         <!-- This is an ATutor language pack - http://www.atutor.ca-->
192
193                         <!DOCTYPE language [
194                            <!ELEMENT atutor-version (#PCDATA)>
195                            <!ELEMENT code (#PCDATA)>
196                            <!ELEMENT charset (#PCDATA)>
197                            <!ELEMENT direction (#PCDATA)>
198                            <!ELEMENT reg-exp (#PCDATA)>
199                            <!ELEMENT native-name (#PCDATA)>
200                            <!ELEMENT english-name (#PCDATA)>
201                            <!ELEMENT status (#PCDATA)>
202
203                            <!ATTLIST language code ID #REQUIRED>
204                         ]>';
205                 } 
206
207                 $xml .= '<language code="'.$this->code.'">
208                         <atutor-version>'.VERSION.'</atutor-version>
209                         <charset>'.$this->characterSet.'</charset>
210                         <direction>'.$this->direction.'</direction>
211                         <reg-exp>'.$this->regularExpression.'</reg-exp>
212                         <native-name>'.$this->nativeName.'</native-name>
213                         <english-name>'.$this->englishName.'</english-name>
214                         <status>'.$this->status.'</status>
215                 </language>';
216
217                 return $xml;
218         }
219 }
220 ?>