made a copy
[atutor.git] / include / classes / Language / Language.class.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2008 by Greg Gay, Joel Kronenberg & Heidi Hazelton*/
6 /* Adaptive Technology Resource Centre / University of Toronto                  */
7 /* http://atutor.ca                                                                                                             */
8 /*                                                                                                                                              */
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 /************************************************************************/
13 // $Id$
14
15 define('AT_LANGUAGE_LOCALE_SEP', '-');
16
17 /**
18 * Language
19 * Class for accessing information about a single language.
20 * @access       public
21 * @author       Joel Kronenberg
22 * @see          LanguageManager::getLanguage()
23 * @see          LanguageManager::getMyLanguage()
24 * @package      Language
25 */
26 class Language {
27         // all private
28         var $code;
29         var $characterSet;
30         var $direction;
31         var $regularExpression;
32         var $nativeName;
33         var $englishName;
34         var $status;
35         var $atutor_version;
36
37         var $db;
38
39         // constructor
40         function Language($language_row) {
41                 global $db;
42
43                 $this->db = $db;
44
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;
54
55                 } else if (is_object($language_row)) {
56                         $this->cloneThis($language_row);
57                 }
58         }
59
60         // private
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) {
65                         $this->$key = $value;
66                 }
67         }
68
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);
72         }
73
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);
77
78         }
79
80         function getCode() {
81                 return $this->code;
82         }
83
84         function getCharacterSet() {
85                 return $this->characterSet;
86         }
87
88         function getDirection() {
89                 return $this->direction;
90         }
91
92         function getRegularExpression() {
93                 return $this->regularExpression;
94         }
95
96         function getAtutorVersion() {
97                 return $this->atutor_version;
98         }
99
100         function getTranslatedName() {
101                 if ($this->code == $_SESSION['lang']) {
102                         return $this->nativeName;
103                 }
104                 // this code has to be translated:
105                 return _AT('lang_' . str_replace('-', '_', $this->code));
106         }
107
108         function getNativeName() {
109                 return $this->nativeName;
110         }
111
112         function getEnglishName() {
113                 return $this->englishName;
114         }
115
116         function getStatus() {
117                 return $this->status;
118         }
119
120
121         // public
122         function sendContentTypeHeader() {
123                 header('Content-Type: text/html; charset=' . $this->characterSet);
124         }
125
126         // public
127         function saveToSession() {
128                 $_SESSION['lang'] = $this->code;
129         }
130
131         /* 
132          * public
133          * @param       member_id or login for members and admin respectively
134          * @param       1 for admin, 0 for members, all other integers are ignored. 
135          */
136         function saveToPreferences($id, $is_admin) {
137                 global $db;
138                 if ($id) {
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'";
143                         }
144                         mysql_query($sql,$db);
145                 }
146         }
147
148         // public
149         // returns whether or not this language is right-to-left
150         // possible langues are: arabic, farsi, hebrew, urdo
151         function isRTL() {
152                 if ($this->direction == 'rtl') {
153                         return true;
154                 } // else:
155
156                 return false;
157         }
158
159         // public
160         // can be called staticly
161         function getParentCode($code = '') {
162                 if (!$code && isset($this)) {
163                         $code = $this->code;
164                 }
165                 $peices = explode(AT_LANGUAGE_LOCALE_SEP, $code, 2);
166                 return $peices[0];
167         }
168
169         // public
170         // can be called staticly
171         function getLocale($code = '') {
172                 if (!$code && isset($this)) {
173                         $code = $this->code;
174                 }
175                 $peices = explode(AT_LANGUAGE_LOCALE_SEP, $code, 2);
176                 return $peices[1];
177         }
178
179         
180         // public
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'";
183
184                 $result = mysql_query($sql, $this->db);
185                 $row = mysql_fetch_assoc($result);
186                 return $row;
187         }
188
189         function getXML($part=FALSE) {
190                 if (!$part) {
191                         $xml = '<?xml version="1.0" encoding="iso-8859-1"?>
192                         <!-- This is an ATutor language pack - http://www.atutor.ca-->
193
194                         <!DOCTYPE language [
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)>
203
204                            <!ATTLIST language code ID #REQUIRED>
205                         ]>';
206                 } 
207
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>
216                 </language>';
217
218                 return $xml;
219         }
220 }
221 ?>