PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/hotpot/template/default.php

https://bitbucket.org/ceu/moodle_demo
PHP | 113 lines | 99 code | 0 blank | 14 comment | 13 complexity | 323b347b537f934c2b686e97db043dca MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, LGPL-2.1
  1. <?PHP
  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 &quot;'.$filepath.'&quot;';
  9. error($msg, $this->parent->course_homeurl);
  10. }
  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;
  18. $this->$tag = '';
  19. return $template;
  20. }
  21. }
  22. function expand_blocks($tag) {
  23. // get block $names
  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);
  40. }
  41. } else {
  42. $msg = 'Template block expand method not found: &quot;'.$method.'&quot;';
  43. error($msg, $this->parent->course_homeurl);
  44. }
  45. }
  46. }
  47. function expand_strings($tag, $search='') {
  48. if (empty($search)) {
  49. // default $search $pattern
  50. $search = '/\[(?:bool|int|str)(\\w+)\]/';
  51. }
  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);
  59. }
  60. }
  61. }
  62. function bool_value($tags, $more_tags="[0]['#']") {
  63. $value = $this->parent->xml_value($tags, $more_tags);
  64. return empty($value) ? 'false' : 'true';
  65. }
  66. function int_value($tags, $more_tags="[0]['#']") {
  67. return intval($this->parent->xml_value($tags, $more_tags));
  68. }
  69. function js_value($tags, $more_tags="[0]['#']", $convert_to_unicode=true) {
  70. return $this->js_safe($this->parent->xml_value($tags, $more_tags), $convert_to_unicode);
  71. }
  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('&#x003C;' => '<', '&#x003E;' => '>'));
  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_callback('/&#(\d+);/', array(&$this, 'js_safe_callback'), $str);
  85. }
  86. return $str;
  87. }
  88. function js_safe_callback(&$matches) {
  89. return '\\u'.sprintf('%04X', $matches[1]);
  90. }
  91. function get_halfway_color($x, $y) {
  92. // returns the $color that is half way between $x and $y
  93. $color = $x; // default
  94. $rgb = '/^\#?([0-9a-f])([0-9a-f])([0-9a-f])$/i';
  95. $rrggbb = '/^\#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i';
  96. if ((
  97. preg_match($rgb, $x, $x_matches) ||
  98. preg_match($rrggbb, $x, $x_matches)
  99. ) && (
  100. preg_match($rgb, $y, $y_matches) ||
  101. preg_match($rrggbb, $y, $y_matches)
  102. )) {
  103. $color = '#';
  104. for ($i=1; $i<=3; $i++) {
  105. $x_dec = hexdec($x_matches[$i]);
  106. $y_dec = hexdec($y_matches[$i]);
  107. $color .= sprintf('%02x', min($x_dec, $y_dec) + abs($x_dec-$y_dec)/2);
  108. }
  109. }
  110. return $color;
  111. }
  112. }
  113. ?>