2 class hotpot_xml_template_default
{
3 function read_template($filename, $tag='temporary') {
4 // create the file path to the template
5 $filepath = $this->parent
->template_dirpath
.DIRECTORY_SEPARATOR
.$filename;
6 // try and open the template file
7 if (!file_exists($filepath) ||
!is_readable($filepath)) {
8 $msg = 'Could not open the '.$this->parent
->template_dir
.' template file "'.$filepath.'"';
9 error($msg, $this->parent
->course_homeurl
);
11 // read in the template and close the file
12 $this->$tag = file_get_contents($filepath);
13 // expand the blocks and strings in the template
14 $this->expand_blocks($tag);
15 $this->expand_strings($tag);
16 if ($tag=='temporary') {
17 $template = $this->$tag;
22 function expand_blocks($tag) {
24 // [1] the full block name (including optional leading 'str' or 'incl')
25 // [2] leading 'incl' or 'str', if any
26 // [3] the real block name ([1] without [2])
27 $search = '/\[\/((incl|str)?((?:\w|\.)+))\]/';
28 preg_match_all($search, $this->$tag, $names);
29 $i_max = count($names[0]);
30 for ($i=0; $i<$i_max; $i++
) {
31 $method = $this->parent
->template_dir
.'_expand_'.str_replace('.', '', $names[3][$i]);
32 if (method_exists($this, $method)) {
33 eval('$value=$this->'.$method.'();');
34 $search = '/\['.$names[1][$i].'\](.*?)\[\/'.$names[1][$i].'\]/s';
35 preg_match_all($search, $this->$tag, $blocks);
36 $ii_max = count($blocks[0]);
37 for ($ii=0; $ii<$ii_max; $ii++
) {
38 $replace = empty($value) ?
'' : $blocks[1][$ii];
39 $this->$tag = str_replace($blocks[0][$ii], $replace, $this->$tag);
42 $msg = 'Template block expand method not found: "'.$method.'"';
43 error($msg, $this->parent
->course_homeurl
);
47 function expand_strings($tag, $search='') {
49 // default $search $pattern
50 $search = '/\[(?:bool|int|str)(\\w+)\]/';
52 preg_match_all($search, $this->$tag, $matches);
53 $i_max = count($matches[0]);
54 for ($i=0; $i<$i_max; $i++
) {
55 $method = $this->parent
->template_dir
.'_expand_'.$matches[1][$i];
56 if (method_exists($this, $method)) {
57 eval('$replace=$this->'.$method.'();');
58 $this->$tag = str_replace($matches[0][$i], $replace, $this->$tag);
62 function bool_value($tags, $more_tags="[0]['#']") {
63 $value = $this->parent
->xml_value($tags, $more_tags);
64 return empty($value) ?
'false' : 'true';
66 function int_value($tags, $more_tags="[0]['#']") {
67 return intval($this->parent
->xml_value($tags, $more_tags));
69 function js_value($tags, $more_tags="[0]['#']", $convert_to_unicode=false) {
70 return $this->js_safe($this->parent
->xml_value($tags, $more_tags), $convert_to_unicode);
72 function js_safe($str, $convert_to_unicode=false) {
73 // encode a string for javascript
74 // decode "<" and ">" - not necesary as it was done by xml_value()
75 // $str = strtr($str, array('<' => '<', '>' => '>'));
76 // escape single quotes and backslashes
77 $str = strtr($str, array("'"=>"\\'", '\\'=>'\\\\'));
78 // convert newlines (win = "\r\n", mac="\r", linix/unix="\n")
79 $nl = '\\n'; // javascript newline
80 $str = strtr($str, array("\r\n"=>$nl, "\r"=>$nl, "\n"=>$nl));
81 // convert (hex and decimal) html entities to unicode, if required
82 if ($convert_to_unicode) {
83 $str = preg_replace('|&#x([0-9A-F]+);|i', '\\u\\1', $str);
84 $str = preg_replace('|&#(\d+);|e', "'\\u'.sprintf('%04X', '\\1')", $str);
88 function get_halfway_color($x, $y) {
89 // returns the $color that is half way between $x and $y
90 $color = $x; // default
91 $rgb = '/^\#?([0-9a-f])([0-9a-f])([0-9a-f])$/i';
92 $rrggbb = '/^\#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i';
94 preg_match($rgb, $x, $x_matches) ||
95 preg_match($rrggbb, $x, $x_matches)
97 preg_match($rgb, $y, $y_matches) ||
98 preg_match($rrggbb, $y, $y_matches)
101 for ($i=1; $i<=3; $i++
) {
102 $x_dec = hexdec($x_matches[$i]);
103 $y_dec = hexdec($y_matches[$i]);
104 $color .= sprintf('%02x', min($x_dec, $y_dec) +
abs($x_dec-$y_dec)/2);