tagging as ATutor 1.5.4-release
[atutor.git] / include / classes / Language / Language.class.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2004 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    = $language_row['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 eregi('^(' . $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 eregi('(\(|\[|;[[:space:]])(' . $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         // public
132         function saveToPreferences($id) {
133                 global $db;
134                 if ($id) {
135                         $sql = "UPDATE ".TABLE_PREFIX."members SET language='".$this->code."', creation_date=creation_date, last_login=last_login WHERE member_id=$id";
136                         mysql_query($sql,$db);
137                 }
138         }
139
140         // public
141         // returns whether or not this language is right-to-left
142         // possible langues are: arabic, farsi, hebrew, urdo
143         function isRTL() {
144                 if ($this->direction == 'rtl') {
145                         return true;
146                 } // else:
147
148                 return false;
149         }
150
151         // public
152         // can be called staticly
153         function getParentCode($code = '') {
154                 if (!$code && isset($this)) {
155                         $code = $this->code;
156                 }
157                 $peices = explode(AT_LANGUAGE_LOCALE_SEP, $code, 2);
158                 return $peices[0];
159         }
160
161         // public
162         // can be called staticly
163         function getLocale($code = '') {
164                 if (!$code && isset($this)) {
165                         $code = $this->code;
166                 }
167                 $peices = explode(AT_LANGUAGE_LOCALE_SEP, $code, 2);
168                 return $peices[1];
169         }
170
171         
172         // public
173         function getTerm($term) {
174                 $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'";
175
176                 $result = mysql_query($sql, $this->db);
177                 $row = mysql_fetch_assoc($result);
178                 return $row;
179         }
180
181         function getXML($part=FALSE) {
182                 if (!$part) {
183                         $xml = '<?xml version="1.0" encoding="iso-8859-1"?>
184                         <!-- This is an ATutor language pack - http://www.atutor.ca-->
185
186                         <!DOCTYPE language [
187                            <!ELEMENT atutor-version (#PCDATA)>
188                            <!ELEMENT code (#PCDATA)>
189                            <!ELEMENT charset (#PCDATA)>
190                            <!ELEMENT direction (#PCDATA)>
191                            <!ELEMENT reg-exp (#PCDATA)>
192                            <!ELEMENT native-name (#PCDATA)>
193                            <!ELEMENT english-name (#PCDATA)>
194                            <!ELEMENT status (#PCDATA)>
195
196                            <!ATTLIST language code ID #REQUIRED>
197                         ]>';
198                 } 
199
200                 $xml .= '<language code="'.$this->code.'">
201                         <atutor-version>'.VERSION.'</atutor-version>
202                         <charset>'.$this->characterSet.'</charset>
203                         <direction>'.$this->direction.'</direction>
204                         <reg-exp>'.$this->regularExpression.'</reg-exp>
205                         <native-name>'.$this->nativeName.'</native-name>
206                         <english-name>'.$this->englishName.'</english-name>
207                         <status>'.$this->status.'</status>
208                 </language>';
209
210                 return $xml;
211         }
212 }
213 ?>