MDL-7602 - Multianswer question type only works with invalid HTML. Merged from MOODLE...
[moodle.git] / filter / multilang / filter.php
CommitLineData
71c235bc 1<?php //$Id$
2
3///////////////////////////////////////////////////////////////////////////
4// //
5// This program is part of Moodle - Modular Object-Oriented Dynamic //
6// Learning Environment - http://moodle.org //
7// //
9fecfdfb 8// Copyright (C) 2004 Gaetan Frenoy <gaetan@frenoy.net> //
71c235bc 9// Eloy Lafuente <stronk7@moodle.org> //
10// //
11// This program is free software; you can redistribute it and/or modify //
12// it under the terms of the GNU General Public License as published by //
13// the Free Software Foundation; either version 2 of the License, or //
14// (at your option) any later version. //
15// //
16// This program is distributed in the hope that it will be useful, //
17// but WITHOUT ANY WARRANTY; without even the implied warranty of //
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
19// GNU General Public License for more details: //
20// //
21// http://www.gnu.org/copyleft/gpl.html //
22// //
23///////////////////////////////////////////////////////////////////////////
24
25// Given XML multilinguage text, return relevant text according to
26// current language. i.e.=
27// - look for lang sections in the code.
28// - if there exists texts in the currently active language, print them.
29// - else, if there exists texts in the current parent language, print them.
71c235bc 30// - else, print the first language in the text.
9fecfdfb 31// Please note that English texts are not used as default anymore!
71c235bc 32//
9fecfdfb 33// This is an improved version of the original multilang filter by Gaetan Frenoy.
71c235bc 34// It should be 100% compatible with the original one. Some new features are:
35// - Supports a new "short" syntax to make things easier. Simply use:
3bac4f9f 36// <span lang="XX">
71c235bc 37// - Needs less resources and executes faster.
38// - Allows any type of content to be used. No restrictions at all!
39
40function multilang_filter($courseid, $text) {
41
fde4a094 42 // [pj] I don't know about you but I find this new implementation funny :P
9fecfdfb 43 // [skodak] I was laughing while rewriting it ;-)
a630afdc 44 $search = '/(<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.+?<\/(?:lang|span)>\s*)+/is';
fde4a094 45 return preg_replace_callback($search, 'multilang_filter_impl', $text);
46}
71c235bc 47
fde4a094 48function multilang_filter_impl($langblock) {
9fecfdfb 49 $mylang = str_replace('_utf8', '', current_language());
50 static $parentcache;
51 if (!isset($parentcache)) {
52 $parentcache = array();
53 }
54 if (!array_key_exists($mylang, $parentcache)) {
55 $parentlang = str_replace('_utf8', '', get_string('parentlanguage'));
56 $parentcache[$mylang] = $parentlang;
57 } else {
58 $parentlang = $parentcache[$mylang];
71c235bc 59 }
fde4a094 60
a630afdc 61 $searchtosplit = '/<(?:lang|span) lang="([a-zA-Z0-9_-]*)".*?>(.+?)<\/(?:lang|span)>/is';
9fecfdfb 62 preg_match_all($searchtosplit, $langblock[0], $rawlanglist);
63
64 $langlist = array();
65 foreach ($rawlanglist[1] as $index=>$lang) {
66 $lang = str_replace('_utf8', '', str_replace('-','_',strtolower($lang))); // normalize languages
67 $langlist[$lang] = $rawlanglist[2][$index];
71c235bc 68 }
69
9fecfdfb 70 if (array_key_exists($mylang, $langlist)) {
71 return $langlist[$mylang];
72 } else if (array_key_exists($parentlang, $langlist)) {
73 return $langlist[$parentlang];
74 } else {
75 $first = array_shift($langlist);
76 return $first;
77 }
71c235bc 78}
79
80?>