Changed grade_item so that its grademax is count(scale_items) and grademin is 1,...
[moodle-pu.git] / admin / langimport.php
blob194ef2efa97e21220904288a3519adf547eb58ea
1 <?php //$Id$
2 ///This file only manages the installation of 1.6 lang packs.
3 ///in downloads.moodle.org, they are store in separate directory /lang16
4 ///in local server, they are stored in $CFG->dataroot/lang
5 ///This helps to avoid confusion.
7 require_once('../config.php');
8 require_once($CFG->libdir.'/adminlib.php');
10 admin_externalpage_setup('langimport');
12 $mode = optional_param('mode', 0, PARAM_INT); //phase
13 $pack = optional_param('pack', '', PARAM_FILE); //pack to install
14 $displaylang = $pack;
15 $uninstalllang = optional_param('uninstalllang', '', PARAM_FILE);
16 $confirm = optional_param('confirm', 0, PARAM_BOOL);
17 $sitelang = optional_param('sitelangconfig', '', PARAM_FILE);
19 define('INSTALLATION_OF_SELECTED_LANG', 2);
20 define('CHANGE_SITE_LANG', 3);
21 define('DELETION_OF_SELECTED_LANG', 4);
22 define('UPDATE_ALL_LANG', 5);
24 $strlang = get_string('langimport','admin');
26 $strlanguage = get_string("language");
27 $strthislanguage = get_string("thislanguage");
28 $title = $strlang;
30 admin_externalpage_print_header();
32 //reset and diagnose lang cache permissions
33 @unlink($CFG->dataroot.'/cache/languages');
34 if (file_exists($CFG->dataroot.'/cache/languages')) {
35 notify('Language cache can not be deleted, please check permissions in dataroot.');
37 get_list_of_languages(true); //refresh lang cache
39 switch ($mode){
41 case INSTALLATION_OF_SELECTED_LANG: ///installation of selected language pack
43 if (confirm_sesskey()) {
44 @mkdir ($CFG->dataroot.'/temp/'); //make it in case it's a fresh install, it might not be there
45 @mkdir ($CFG->dataroot.'/lang/');
47 require_once($CFG->libdir.'/componentlib.class.php');
48 if ($cd = new component_installer('http://download.moodle.org', 'lang16',
49 $pack.'.zip', 'languages.md5', 'lang')) {
50 $status = $cd->install(); //returns ERROR | UPTODATE | INSTALLED
51 switch ($status) {
53 case ERROR:
54 if ($cd->get_error() == 'remotedownloadnotallowed') {
55 $a = new stdClass();
56 $a->url = 'http://download.moodle.org/lang16/'.$pack.'.zip';
57 $a->dest= $CFG->dataroot.'/lang';
58 error(get_string($cd->get_error(), 'error', $a));
59 } else {
60 error(get_string($cd->get_error(), 'error'));
62 break;
64 case INSTALLED:
65 get_list_of_languages(true); //refresh lang cache
66 redirect('langimport.php', get_string('langpackupdated','admin',$pack), -1);
67 break;
69 case UPTODATE:
70 break;
73 } else {
74 notify('Had an unspecified error with the component installer, sorry.');
77 break;
79 case CHANGE_SITE_LANG: //change site language
81 if (confirm_sesskey()) {
82 $langconfig = get_record('config','name','lang');
83 $langconfig->value = $sitelang;
84 if (!empty($sitelang) && update_record('config',$langconfig)){
85 redirect('langimport.php', get_string('sitelangchanged','admin'));
86 } else {
87 error('Could not update the default site language!');
91 break;
92 case DELETION_OF_SELECTED_LANG: //delete a directory(ies) containing a lang pack completely
94 if (!$confirm && confirm_sesskey()) {
95 notice_yesno(get_string('uninstallconfirm', 'admin', $uninstalllang),
96 'langimport.php?mode=4&amp;uninstalllang='.$uninstalllang.'&amp;confirm=1&amp;sesskey='.sesskey(),
97 'langimport.php');
98 } else if (confirm_sesskey()) {
99 if ($uninstalllang == 'en_utf8') {
100 error ('en_utf8 can not be uninstalled!');
102 $dest1 = $CFG->dataroot.'/lang/'.$uninstalllang;
103 $dest2 = $CFG->dirroot.'/lang/'.$uninstalllang;
104 $rm1 = false;
105 $rm2 = false;
106 if (file_exists($dest1)){
107 $rm1 = remove_dir($dest1);
109 if (file_exists($dest2)){
110 $rm2 = remove_dir($dest2);
112 get_list_of_languages(true); //refresh lang cache
113 //delete the direcotries
114 if ($rm1 or $rm2) {
115 redirect('langimport.php', get_string('langpackremoved','admin'), 3);
116 } else { //nothing deleted, possibly due to permission error
117 error('An error has occurred, language pack is not completely uninstalled, please check file permissions');
120 break;
122 case UPDATE_ALL_LANG: //1 click update for all updatable language packs
124 //0th pull a list from download.moodle.org,
125 //key = langname, value = md5
126 $source = 'http://download.moodle.org/lang16/languages.md5';
127 $md5array = array();
128 $updated = 0; //any packs updated?
129 $alllangs = array_keys(get_list_of_languages(false, true)); //get all available langs
130 $lang16 = array(); //all the Moodle 1.6 unicode lang packs (updated and not updated)
131 $packs = array(); //all the packs that needs updating
134 if (!$availablelangs = proxy_url($source)) {
135 error ('can not read from course');
138 //and build an associative array
139 foreach ($availablelangs as $alang) {
140 $md5array[$alang[0]] = $alang[1];
143 //filtering out non-16 packs
144 foreach ($alllangs as $clang) {
145 $dest1 = $CFG->dataroot.'/lang/'.$clang;
146 $dest2 = $CFG->dirroot.'/lang/'.$clang;
148 if (file_exists($dest1.'/langconfig.php') || file_exists($dest2.'/langconfig.php')){
149 $lang16[] = $clang;
153 //then filter out packs that have the same md5 key
154 foreach ($lang16 as $clang) {
155 if (!is_installed_lang($clang, $md5array[$clang])){
156 $packs[] = $clang;
160 @mkdir ($CFG->dataroot.'/temp/');
161 @mkdir ($CFG->dataroot.'/lang/');
162 foreach ($packs as $pack){ //for each of the remaining in the list, we
163 if ($pack == 'en_utf8') { // no update for en_utf8
164 continue;
166 //1. delete old director(ies)
168 $dest1 = $CFG->dataroot.'/lang/'.$pack;
169 $dest2 = $CFG->dirroot.'/lang/'.$pack;
170 $rm1 = false;
171 $rm2 = false;
172 if (file_exists($dest1)) {
173 $rm1 = remove_dir($dest1);
175 if (file_exists($dest2)) {
176 $rm2 = remove_dir($dest2);
178 if (!($rm1 || $rm2)) {
179 error ('could not delete old directory, update failed');
182 //2. copy & unzip into new
184 require_once($CFG->libdir.'/componentlib.class.php');
185 if ($cd = new component_installer('http://download.moodle.org', 'lang16',
186 $pack.'.zip', 'languages.md5', 'lang')) {
187 $status = $cd->install(); //returns ERROR | UPTODATE | INSTALLED
188 switch ($status) {
190 case ERROR:
191 if ($cd->get_error() == 'remotedownloadnotallowed') {
192 $a = new stdClass();
193 $a->url = 'http://download.moodle.org/lang16/'.$pack.'.zip';
194 $a->dest= $CFG->dataroot.'/lang';
195 error(get_string($cd->get_error(), 'error', $a));
196 } else {
197 error(get_string($cd->get_error(), 'error'));
199 break;
200 case UPTODATE:
201 //Print error string or whatever you want to do
202 break;
203 case INSTALLED:
204 notify(get_string('langpackupdated','admin',$pack), 'notifysuccess');
205 $updated = true;
206 //Print/do whatever you want
207 break;
208 default:
210 } else {
215 if ($updated) {
216 notice(get_string('langupdatecomplete','admin'), 'langimport.php');
217 } else {
218 notice(get_string('nolangupdateneeded','admin'), 'langimport.php');
221 break;
223 default: //display choice mode
225 $source = 'http://download.moodle.org/lang16/languages.md5';
226 $remote = 0; //flag for reading from remote or local
228 if ($availablelangs = proxy_url($source)) {
229 $remote = 1;
230 } else {
231 $availablelangs = get_local_list_of_languages();
234 if ($fp = fopen($source, 'r')){ /// attempt to get the list from Moodle.org.
235 while(!feof ($fp)) {
236 $availablelangs[] = split(',', fgets($fp,1024));
238 $remote = 1; //can read from download.moodle.org
239 } else { /// fopen failed, we find local copy of list.
240 $availablelangs = get_local_list_of_languages();
243 if (!$remote) {
244 print_box_start();
245 print_string('remotelangnotavailable','admin',$CFG->dataroot.'/lang/');
246 print_box_end();
249 print_box_start();
250 echo '<table summary="">';
251 echo '<tr><td align="center" valign="top">';
252 echo '<form id="uninstallform" action="langimport.php?mode=4" method="post">';
253 echo '<fieldset class="invisiblefieldset">';
254 echo '<input name="sesskey" type="hidden" value="'.sesskey().'" />';
255 $installedlangs = get_list_of_languages(false, true);
257 /// display installed langs here
259 echo '<label for="uninstalllang">'.get_string('installedlangs','admin')."</label><br />\n";
260 echo '<select name="uninstalllang" id="uninstalllang" size="15">';
261 foreach ($installedlangs as $clang =>$ilang){
262 echo '<option value="'.$clang.'">'.$ilang.'</option>';
264 echo '</select>';
265 echo '<br /><input type="submit" value="'.get_string('uninstall','admin').'" />';
266 echo '</fieldset>';
267 echo '</form>';
268 echo '<form id="updateform" action="langimport.php?mode=5" method="post">';
269 echo '<div>';
270 echo '<br /><input type="submit" value="'.get_string('updatelangs','admin').'" />';
271 echo '</div>';
272 echo '</form>';
274 /// Display option to change site language
276 /// display to be installed langs here
278 echo '</td><td align="center" valign="top">';
279 //availabe langs table
280 $empty = 1; //something to pring
282 /// if this language pack is not already installed, then we allow installation
284 echo '<form id="installform" method="post" action="langimport.php?mode=2">';
285 echo '<fieldset class="invisiblefieldset">';
286 echo '<input name="sesskey" type="hidden" value="'.sesskey().'" />';
287 echo '<label for="pack">'.get_string('availablelangs','admin')."</label><br />\n";
288 if ($remote) {
289 echo '<select name="pack" id="pack" size="15">';
292 foreach ($availablelangs as $alang) {
293 if (trim($alang[0]) != "en_utf8") {
294 if ($remote){
295 if (substr($alang[0], -5) == '_utf8') { //Remove the _utf8 suffix from the lang to show
296 $shortlang = substr($alang[0], 0, -5);
297 } else {
298 $shortlang = $alang[0];
300 if (!is_installed_lang($alang[0], $alang[1])){ //if not already installed
301 echo '<option value="'.$alang[0].'">'.$alang[2].' ('.$shortlang.')</option>';
303 } else { //print list in local format, and instruction to install
304 echo '<tr><td>'.$alang[2].'</td><td><a href="http://download.moodle.org/lang16/'.$alang[0].'.zip">'.get_string('download','admin').'</a></td></tr>';
306 $empty = 0;
309 if ($remote) {
310 echo '</select>';
311 echo '<br /><input type="submit" value="&larr; '.get_string('install','admin').'" />';
313 echo '</fieldset>';
314 echo '</form>';
316 if ($empty) {
317 echo '<br />';
318 print_string('nolanguagetodownload','admin');
321 //close available langs table
322 echo '</td></tr></table>';
323 print_simple_box_end();
325 break;
327 } //close of main switch
329 admin_externalpage_print_footer();
331 /* returns a list of available language packs from a
332 * local copy shipped with standard moodle distro
333 * this is for site that can't perform fopen
334 * @return array
336 function get_local_list_of_languages() {
337 global $CFG;
338 $source = $CFG->wwwroot.'/lib/languages.md5';
339 $availablelangs = array();
340 if ($fp = fopen($source, 'r')){
341 while(!feof ($fp)) {
342 $availablelangs[] = split(',', fgets($fp,1024));
345 return $availablelangs;
348 /* checks the md5 of the zip file, grabbed from download.moodle.org,
349 * against the md5 of the local language file from last update
350 * @param string $lang
351 * @param string $md5check
352 * @return bool
354 function is_installed_lang($lang, $md5check) {
355 global $CFG;
356 $md5file = $CFG->dataroot.'/lang/'.$lang.'/'.$lang.'.md5';
357 if (file_exists($md5file)){
358 return (file_get_contents($md5file) == $md5check);
360 return false;
363 //returns an array of languages, or false if can not read from source
364 //uses a socket if proxy is set as a config variable
365 function proxy_url($url) {
366 global $CFG;
368 if ($CFG->proxyhost && $CFG->proxyport) {
370 $proxy_fp = fsockopen($CFG->proxyhost, $CFG->proxyport);
371 if (!$proxy_fp) {
372 return false; //failed
374 fputs($proxy_fp, "GET $url HTTP/1.0\r\nHost: $CFG->proxyhost\r\n\r\n");
376 $headers_done = false;
377 while(!feof($proxy_fp)) {
378 $string = fgets($proxy_fp, 1024);
379 if(!$headers_done){
380 // A new line indicates end of HTTP headers
381 $headers_done = ("\r\n" == $string);
382 } else {
383 $availablelangs[] = split(',', $string);
386 fclose($proxy_fp);
388 } else { //proxy not in use
389 if ($fp = fopen($url, 'r')){ /// attempt to get the list from Moodle.org.
390 while(!feof ($fp)) {
391 $availablelangs[] = split(',', fgets($fp,1024));
393 } else { /// fopen failed, return false.
394 return false;
397 return $availablelangs;