PageRenderTime 65ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/home/classes/ContentUtility.class.php

https://github.com/harriswong/AContent
PHP | 1049 lines | 668 code | 148 blank | 233 comment | 200 complexity | a5e6b98228777b03a6c8d487fbad73b7 MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, MIT, AGPL-1.0
  1. <?php
  2. /************************************************************************/
  3. /* AContent */
  4. /************************************************************************/
  5. /* Copyright (c) 2010 */
  6. /* Inclusive Design Institute */
  7. /* */
  8. /* This program is free software. You can redistribute it and/or */
  9. /* modify it under the terms of the GNU General Public License */
  10. /* as published by the Free Software Foundation. */
  11. /************************************************************************/
  12. /**
  13. * Content Utility functions
  14. * @access public
  15. * @author Cindy Qi Li
  16. */
  17. if (!defined('TR_INCLUDE_PATH')) exit;
  18. class ContentUtility {
  19. /**
  20. * This function cuts out html body
  21. * @access public
  22. * @param $text html text
  23. * @author Cindy Qi Li
  24. */
  25. public static function getHtmlBody($text) {
  26. /* strip everything before <body> */
  27. $start_pos = strpos(strtolower($text), '<body');
  28. if ($start_pos !== false) {
  29. $start_pos += strlen('<body');
  30. $end_pos = strpos(strtolower($text), '>', $start_pos);
  31. $end_pos += strlen('>');
  32. $text = substr($text, $end_pos);
  33. }
  34. /* strip everything after </body> */
  35. $end_pos = strpos(strtolower($text), '</body>');
  36. if ($end_pos !== false) {
  37. $text = trim(substr($text, 0, $end_pos));
  38. }
  39. return $text;
  40. }
  41. /**
  42. * This function cuts out requested tag information from html head
  43. * @access public
  44. * @param $text html text
  45. * @param $tags a string or an array of requested tags
  46. * @author Cindy Qi Li
  47. */
  48. public static function getHtmlHeadByTag($text, $tags)
  49. {
  50. $head = ContentUtility::getHtmlHead($text);
  51. $rtn_text = "";
  52. if (!is_array($tags) && strlen(trim($tags)) > 0)
  53. {
  54. $tags = array(trim($tags));
  55. }
  56. foreach ($tags as $tag)
  57. {
  58. $tag = strtolower($tag);
  59. /* strip everything before <{tag}> */
  60. $start_pos = stripos($head, '<'.$tag);
  61. $temp_head = $head;
  62. while ($start_pos !== false)
  63. {
  64. $temp_text = substr($temp_head, $start_pos);
  65. /* strip everything after </{tag}> or />*/
  66. $end_pos = stripos($temp_text, '</' . $tag . '>');
  67. if ($end_pos !== false)
  68. {
  69. $end_pos += strlen('</' . $tag . '>');
  70. // add an empty line after each tag information
  71. $rtn_text .= trim(substr($temp_text, 0, $end_pos)) . '
  72. ';
  73. }
  74. else // match /> as ending tag if </tag> is not found
  75. {
  76. $end_pos = stripos($temp_text, '/>');
  77. if($end_pos === false && stripos($temp_text, $tag.'>')===false){
  78. //if /> is not found, then this is not a valid XHTML
  79. //text iff it's not tag>
  80. $end_pos = stripos($temp_text, '>');
  81. $end_pos += strlen('>');
  82. } else {
  83. $end_pos += strlen('/>');
  84. }
  85. // add an empty line after each tag information
  86. $rtn_text .= trim(substr($temp_text, 0, $end_pos)) . '
  87. ';
  88. }
  89. // initialize vars for next round of matching
  90. $temp_head = substr($temp_text, $end_pos);
  91. $start_pos = stripos($temp_head, '<'.$tag);
  92. }
  93. }
  94. return $rtn_text;
  95. }
  96. /**
  97. * This function cuts out html head
  98. * @access private
  99. * @param $text html text
  100. * @author Cindy Qi Li
  101. */
  102. private static function getHtmlHead ($text) {
  103. /* strip everything before <head> */
  104. $start_pos = stripos($text, '<head');
  105. if ($start_pos !== false) {
  106. $start_pos += strlen('<head');
  107. $end_pos = stripos($text, '>', $start_pos);
  108. $end_pos += strlen('>');
  109. $text = substr($text, $end_pos);
  110. }
  111. /* strip everything after </head> */
  112. $end_pos = stripos($text, '</head');
  113. if ($end_pos !== false) {
  114. $text = trim(substr($text, 0, $end_pos));
  115. }
  116. return $text;
  117. }
  118. private static function embedFLV($text) {
  119. global $content_base_href;
  120. $media_replace = array();
  121. $media_matches = array();
  122. $flowplayerholder_class = "atutor.flowplayerholder"; // style class used to play flowplayer medias
  123. $flowplayerholder_def = '$f("*.'.$flowplayerholder_class.'"'; // javascript definition for atutor.flowplayerholder
  124. // .flv
  125. preg_match_all("#\[media[0-9a-z\|]*\]([.\w\d]+[^\s\"]+)\.flv\[/media\]#i",$text,$media_matches[],PREG_SET_ORDER);
  126. if (isset($_SESSION['flash']) && $_SESSION['flash'] == "yes") {
  127. $media_replace[] = "<div>\n".
  128. " <a class=\"".$flowplayerholder_class."\" style=\"display:block;width:##WIDTH##px;height:##HEIGHT##px;\" href=\"##MEDIA1##.flv\"></a>\n".
  129. "</div>\n";
  130. } else {
  131. $media_replace[] = "<div>\n".
  132. " <a href=\"##MEDIA1##.flv\">##MEDIA1##.flv</a>\n".
  133. "</div>\n";
  134. }
  135. // .mp4
  136. preg_match_all("#\[media[0-9a-z\|]*\]([.\w\d]+[^\s\"]+)\.mp4\[/media\]#i",$text,$media_matches[],PREG_SET_ORDER);
  137. if (isset($_SESSION['flash']) && $_SESSION['flash'] == "yes") {
  138. $media_replace[] = "<div>\n".
  139. " <a class=\"".$flowplayerholder_class."\" style=\"display:block;width:##WIDTH##px;height:##HEIGHT##px;\" href=\"##MEDIA1##.mp4\"></a>\n".
  140. "</div>\n";
  141. } else {
  142. $media_replace[] = "<div>\n".
  143. " <a href=\"##MEDIA1##.mp4\">##MEDIA1##.mp4</a>\n".
  144. "</div>\n";
  145. }
  146. // Executing the replace
  147. for ($i=0;$i<count($media_replace);$i++){
  148. foreach($media_matches[$i] as $media)
  149. {
  150. //find width and height for each matched media
  151. if (preg_match("/\[media\|([0-9]*)\|([0-9]*)\]*/", $media[0], $matches))
  152. {
  153. $width = $matches[1];
  154. $height = $matches[2];
  155. }
  156. else
  157. {
  158. $width = 425;
  159. $height = 350;
  160. }
  161. //replace media tags with embedded media for each media tag
  162. $media_input = $media_replace[$i];
  163. $media_input = str_replace("##WIDTH##","$width",$media_input);
  164. $media_input = str_replace("##HEIGHT##","$height",$media_input);
  165. $media_input = str_replace("##MEDIA1##","$media[1]",$media_input);
  166. $media_input = str_replace("##MEDIA2##","$media[2]",$media_input);
  167. $text = str_replace($media[0],$media_input,$text);
  168. }
  169. }
  170. // Include the javascript only if:
  171. // 1. $flowplayerholder_class is used but not defined
  172. // 2. exclude from export common cartridge or content package
  173. if (strpos($text, $flowplayerholder_class)
  174. && !strpos($text, $flowplayerholder_def)
  175. && !strpos($_SERVER['PHP_SELF'], "ims_export.php"))
  176. {
  177. $text .= '<script type="text/javascript">
  178. '.$flowplayerholder_def.', "'.TR_BASE_HREF.'include/jscripts/flowplayer/flowplayer-3.2.4.swf", {
  179. clip: {
  180. autoPlay: false,
  181. baseUrl: \''.TR_BASE_HREF.'get.php/'.$content_base_href.'\'},
  182. plugins: {
  183. controls: {
  184. buttons:true,
  185. play: true,
  186. scrubber: true,
  187. autoHide:false
  188. }
  189. }
  190. });
  191. </script>'."\n";
  192. }
  193. return $text;
  194. }
  195. /*
  196. * This function converts the youtube playable url used in <object> tag (for instance: http://www.youtube.com/v/a0ryB0m0MiM)
  197. * to youtube url that is used to browse (for instance: http://www.youtube.com/watch?v=a0ryB0m0MiM)
  198. * @param: youtube playable URL. For instance, http://www.youtube.com/v/a0ryB0m0MiM
  199. * @return: if the param is a youtube playable url, return the according youtube URL used to browse.
  200. * For instance: http://www.youtube.com/watch?v=a0ryB0m0MiM
  201. * Otherwise, return the original send-in parameter.
  202. */
  203. public function convertYoutubePlayURLToWatchURL($youtube_playURL) {
  204. return preg_replace("/(http:\/\/[a-z0-9\.]*)?youtube.com\/v\/(.*)/",
  205. "\\1youtube.com/watch?v=\\2", $youtube_playURL);
  206. }
  207. /*
  208. * This function converts the youtube url that is used to browse (for instance: http://www.youtube.com/watch?v=a0ryB0m0MiM)
  209. * to youtube playable url used in <object> tag (for instance: http://www.youtube.com/v/a0ryB0m0MiM)
  210. * @param: the youtube URL used to browse.
  211. * For instance: http://www.youtube.com/watch?v=a0ryB0m0MiM
  212. * @return: if the param is a youtube url used to browse, return the according youtube playable URL.
  213. * For instance, http://www.youtube.com/v/a0ryB0m0MiM
  214. * Otherwise, return the original send-in parameter.
  215. */
  216. public function convertYoutubeWatchURLToPlayURL($youtube_watchURL) {
  217. return preg_replace("/(http:\/\/[a-z0-9\.]*)?youtube.com\/watch\?v=(.*)/",
  218. "\\1youtube.com/v/\\2", $youtube_watchURL);
  219. }
  220. public static function embedMedia($text) {
  221. if (preg_match("/\[media(\|[0-9]+\|[0-9]+)?\]*/", $text)==0){
  222. return $text;
  223. }
  224. // remove the spaces in [media] tag, otherwise, the next line converts URL inside [media] into <a> tag
  225. $text = preg_replace("/(\[media\])([\s]*)(.*)(\[\/media\])/", '$1$3$4', $text);
  226. $text = preg_replace("/(\[media\])(.*)([\s]*)(\[\/media\])/U", '$1$2$4', $text);
  227. $media_matches = array();
  228. $media_replace = array();
  229. // First, we search though the text for all different kinds of media defined by media tags and store the results in $media_matches.
  230. // Then the different replacements for the different media tags are stored in $media_replace.
  231. // Lastly, we loop through all $media_matches / $media_replaces. (We choose $media_replace as index because $media_matches is multi-dimensioned.) It is important that for each $media_matches there is a $media_replace with the same index. For each media match we check the width/height, or we use the default value of 425x350. We then replace the height/width/media1/media2 parameter placeholders in $media_replace with the correct ones, before running a str_replace on $text, replacing the given media with its correct replacement.
  232. // youtube videos
  233. preg_match_all("#\[media[0-9a-z\|]*\]http://([a-z0-9\.]*)?youtube.com/watch\?v=(.*)\[/media\]#iU",$text,$media_matches[],PREG_SET_ORDER);
  234. $media_replace[] = '<object width="##WIDTH##" height="##HEIGHT##"><param name="movie" value="http://##MEDIA1##youtube.com/v/##MEDIA2##"></param><embed src="http://##MEDIA1##youtube.com/v/##MEDIA2##" type="application/x-shockwave-flash" width="##WIDTH##" height="##HEIGHT##"></embed></object>';
  235. // .mpg
  236. preg_match_all("#\[media[0-9a-z\|]*\]([.\w\d]+[^\s\"]+).mpg\[/media\]#i",$text,$media_matches[],PREG_SET_ORDER);
  237. $media_replace[] = "<object data=\"##MEDIA1##.mpg\" type=\"video/mpeg\" width=\"##WIDTH##\" height=\"##HEIGHT##\"><param name=\"src\" value=\"##MEDIA1##.mpg\"><param name=\"autoplay\" value=\"false\"><param name=\"autoStart\" value=\"0\"><a href=\"##MEDIA1##.mpg\">##MEDIA1##.mpg</a></object>";
  238. // .avi
  239. preg_match_all("#\[media[0-9a-z\|]*\]([.\w\d]+[^\s\"]+).avi\[/media\]#i",$text,$media_matches[],PREG_SET_ORDER);
  240. $media_replace[] = "<object data=\"##MEDIA1##.avi\" type=\"video/x-msvideo\" width=\"##WIDTH##\" height=\"##HEIGHT##\"><param name=\"src\" value=\"##MEDIA1##.avi\"><param name=\"autoplay\" value=\"false\"><param name=\"autoStart\" value=\"0\"><a href=\"##MEDIA1##.avi\">##MEDIA1##.avi</a></object>";
  241. // .wmv
  242. preg_match_all("#\[media[0-9a-z\|]*\]([.\w\d]+[^\s\"]+).wmv\[/media\]#i",$text,$media_matches[],PREG_SET_ORDER);
  243. $media_replace[] = "<object data=\"##MEDIA1##.wmv\" type=\"video/x-ms-wmv\" width=\"##WIDTH##\" height=\"##HEIGHT##\"><param name=\"src\" value=\"##MEDIA1##.wmv\"><param name=\"autoplay\" value=\"false\"><param name=\"autoStart\" value=\"0\"><a href=\"##MEDIA1##.wmv\">##MEDIA1##.wmv</a></object>";
  244. // .mov
  245. preg_match_all("#\[media[0-9a-z\|]*\]([.\w\d]+[^\s\"]+).mov\[/media\]#i",$text,$media_matches[],PREG_SET_ORDER);
  246. $media_replace[] = "<object classid=\"clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B\" codebase=\"http://www.apple.com/qtactivex/qtplugin.cab\" width=\"##WIDTH##\" height=\"##HEIGHT##\">\n".
  247. " <param name=\"src\" value=\"##MEDIA1##.mov\">\n".
  248. " <param name=\"controller\" value=\"true\">\n".
  249. " <param name=\"autoplay\" value=\"false\">\n".
  250. " <!--[if gte IE 7] > <!-->\n".
  251. " <object type=\"video/quicktime\" data=\"##MEDIA1##.mov\" width=\"##WIDTH##\" height=\"##HEIGHT##\">\n".
  252. " <param name=\"controller\" value=\"true\">\n".
  253. " <param name=\"autoplay\" value=\"false\">\n".
  254. " <a href=\"##MEDIA1##.mov\">##MEDIA1##.mov</a>\n".
  255. " </object>\n".
  256. " <!--<![endif]-->\n".
  257. " <!--[if lt IE 7]>\n".
  258. " <a href=\"##MEDIA1##.mov\">##MEDIA1##.mov</a>\n".
  259. " <![endif]-->\n".
  260. "</object>";
  261. // .swf
  262. preg_match_all("#\[media[0-9a-z\|]*\]([.\w\d]+[^\s\"]+).swf\[/media\]#i",$text,$media_matches[],PREG_SET_ORDER);
  263. $media_replace[] = "<object type=\"application/x-shockwave-flash\" data=\"##MEDIA1##.swf\" width=\"##WIDTH##\" height=\"##HEIGHT##\"> <param name=\"movie\" value=\"##MEDIA1##.swf\"><param name=\"loop\" value=\"false\"><a href=\"##MEDIA1##.swf\">##MEDIA1##.swf</a></object>";
  264. // .mp3
  265. preg_match_all("#\[media[0-9a-z\|]*\]([.\w\d]+[^\s\"]+).mp3\[/media\]#i",$text,$media_matches[],PREG_SET_ORDER);
  266. $media_replace[] = "<object type=\"audio/mpeg\" data=\"##MEDIA1##.mp3\" width=\"##WIDTH##\" height=\"##HEIGHT##\"><param name=\"src\" value=\"##MEDIA1##.mp3\"><param name=\"autoplay\" value=\"false\"><param name=\"autoStart\" value=\"0\"><a href=\"##MEDIA1##.mp3\">##MEDIA1##.mp3</a></object>";
  267. // .wav
  268. preg_match_all("#\[media[0-9a-z\|]*\](.+[^\s\"]+).wav\[/media\]#i",$text,$media_matches[],PREG_SET_ORDER);
  269. $media_replace[] ="<object type=\"audio/x-wav\" data=\"##MEDIA1##.wav\" width=\"##WIDTH##\" height=\"##HEIGHT##\"><param name=\"src\" value=\"##MEDIA1##.wav\"><param name=\"autoplay\" value=\"false\"><param name=\"autoStart\" value=\"0\"><a href=\"##MEDIA1##.wav\">##MEDIA1##.wav</a></object>";
  270. // .ogg
  271. preg_match_all("#\[media[0-9a-z\|]*\](.+[^\s\"]+).ogg\[/media\]#i",$text,$media_matches[],PREG_SET_ORDER);
  272. $media_replace[] ="<object type=\"application/ogg\" data=\"##MEDIA1##.ogg\" width=\"##WIDTH##\" height=\"##HEIGHT##\"><param name=\"src\" value=\"##MEDIA1##.ogg\"><a href=\"##MEDIA1##.ogg\">##MEDIA1##.ogg</a></object>";
  273. // .ogm
  274. preg_match_all("#\[media[0-9a-z\|]*\](.+[^\s\"]+).ogm\[/media\]#i",$text,$media_matches[],PREG_SET_ORDER);
  275. $media_replace[] ="<object type=\"application/ogm\" data=\"##MEDIA1##.ogm\" width=\"##WIDTH##\" height=\"##HEIGHT##\"><param name=\"src\" value=\"##MEDIA1##.ogm\"><a href=\"##MEDIA1##.ogg\">##MEDIA1##.ogm</a></object>";
  276. // .mid
  277. preg_match_all("#\[media[0-9a-z\|]*\](.+[^\s\"]+).mid\[/media\]#i",$text,$media_matches[],PREG_SET_ORDER);
  278. $media_replace[] ="<object type=\"application/x-midi\" data=\"##MEDIA1##.mid\" width=\"##WIDTH##\" height=\"##HEIGHT##\"><param name=\"src\" value=\"##MEDIA1##.mid\"><a href=\"##MEDIA1##.mid\">##MEDIA1##.mid</a></object>";
  279. $text = preg_replace("#\[media[0-9a-z\|]*\](.+[^\s\"]+).mid\[/media\]#i", "<object type=\"application/x-midi\" data=\"\\1.mid\" width=\"".$width."\" height=\"".$height."\"><param name=\"src\" value=\"\\1.mid\"><a href=\"\\1.mid\">\\1.mid</a></object>", $text);
  280. // Executing the replace
  281. for ($i=0;$i<count($media_replace);$i++){
  282. foreach($media_matches[$i] as $media)
  283. {
  284. //find width and height for each matched media
  285. if (preg_match("/\[media\|([0-9]*)\|([0-9]*)\]*/", $media[0], $matches))
  286. {
  287. $width = $matches[1];
  288. $height = $matches[2];
  289. }
  290. else
  291. {
  292. $width = 425;
  293. $height = 350;
  294. }
  295. //replace media tags with embedded media for each media tag
  296. $media_input = $media_replace[$i];
  297. $media_input = str_replace("##WIDTH##","$width",$media_input);
  298. $media_input = str_replace("##HEIGHT##","$height",$media_input);
  299. $media_input = str_replace("##MEDIA1##","$media[1]",$media_input);
  300. $media_input = str_replace("##MEDIA2##","$media[2]",$media_input);
  301. $text = str_replace($media[0],$media_input,$text);
  302. }
  303. }
  304. return $text;
  305. }
  306. public static function makeClickable($text) {
  307. $text = ContentUtility::embedMedia($text);
  308. // convert plain text URL to clickable URL.
  309. // Limited conversion: It doesn't cover the case when the stuff in front of the URL is not a word. For example:
  310. // <p>http://google.ca</p>
  311. // "http://google.ca"
  312. $text = preg_replace('/(^|[\n ])([\w]*?)((?<!(\[media\]))http(s)?:\/\/[\w]+[^ \,\"\n\r\t\)<]*)/is',
  313. '$1$2<a href="$3">$3</a>', $text);
  314. // convert email address to clickable URL that pops up "send email" interface with the address filled in
  315. $text = preg_replace('/(?|<a href="mailto[\s]*:[\s]*([_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]+)*'.'\@'
  316. .'[_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]+)*'.'(\.[a-zA-Z]{1,6})+)">(.*)<\/a>'
  317. .'|((((([_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]+)*'.'\@'
  318. .'[_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]+)*'.'(\.[a-zA-Z]{1,6})+))))))/i',
  319. "<a href=\"mailto:\\1\">\\5</a>",
  320. $text);
  321. // flv conversion needs to come after url conversion (2 lines above) otherwise the url to flowplayer swf file
  322. // in the script for a.flowplayerholder is converted
  323. $text = ContentUtility::embedFLV($text);
  324. return $text;
  325. }
  326. public static function myCodes($text, $html = false) {
  327. global $_base_path;
  328. global $HTTP_USER_AGENT;
  329. if (substr($HTTP_USER_AGENT,0,11) == 'Mozilla/4.7') {
  330. $text = str_replace('[quote]','</p><p class="block">',$text);
  331. $text = str_replace('[/quote]','</p><p>',$text);
  332. $text = str_replace('[reply]','</p><p class="block">',$text);
  333. $text = str_replace('[/reply]','</p><p>',$text);
  334. } else {
  335. $text = str_replace('[quote]','<blockquote>',$text);
  336. $text = str_replace('[/quote]','</blockquote><p>',$text);
  337. $text = str_replace('[reply]','</p><blockquote class="block"><p>',$text);
  338. $text = str_replace('[/reply]','</p></blockquote><p>',$text);
  339. }
  340. $text = str_replace('[b]','<strong>',$text);
  341. $text = str_replace('[/b]','</strong>',$text);
  342. $text = str_replace('[i]','<em>',$text);
  343. $text = str_replace('[/i]','</em>',$text);
  344. $text = str_replace('[u]','<u>',$text);
  345. $text = str_replace('[/u]','</u>',$text);
  346. $text = str_replace('[center]','<center>',$text);
  347. $text = str_replace('[/center]','</center><p>',$text);
  348. /* colours */
  349. $text = str_replace('[blue]','<span style="color: blue;">',$text);
  350. $text = str_replace('[/blue]','</span>',$text);
  351. $text = str_replace('[orange]','<span style="color: orange;">',$text);
  352. $text = str_replace('[/orange]','</span>',$text);
  353. $text = str_replace('[red]','<span style="color: red;">',$text);
  354. $text = str_replace('[/red]','</span>',$text);
  355. $text = str_replace('[purple]','<span style="color: purple;">',$text);
  356. $text = str_replace('[/purple]','</span>',$text);
  357. $text = str_replace('[green]','<span style="color: green;">',$text);
  358. $text = str_replace('[/green]','</span>',$text);
  359. $text = str_replace('[gray]','<span style="color: gray;">',$text);
  360. $text = str_replace('[/gray]','</span>',$text);
  361. $text = str_replace('[op]','<span class="bigspacer"></span> <a href="',$text);
  362. $text = str_replace('[/op]','">'._AT('view_entire_post').'</a>',$text);
  363. $text = str_replace('[head1]','<h2>',$text);
  364. $text = str_replace('[/head1]','</h2>',$text);
  365. $text = str_replace('[head2]','<h3>',$text);
  366. $text = str_replace('[/head2]','</h3>',$text);
  367. $text = str_replace('[cid]',$_base_path.'content.php?_cid='.$_SESSION['s_cid'],$text);
  368. global $sequence_links, $_course_id, $_content_id;
  369. if ($_course_id > 0 && !isset($sequence_links) && $_content_id > 0) {
  370. global $contentManager;
  371. $sequence_links = $contentManager->generateSequenceCrumbs($_content_id);
  372. }
  373. if (isset($sequence_links['previous']) && $sequence_links['previous']['url']) {
  374. $text = str_replace('[pid]', $sequence_links['previous']['url'], $text);
  375. }
  376. if (isset($sequence_links['next']) && $sequence_links['next']['url']) {
  377. $text = str_replace('[nid]', $sequence_links['next']['url'], $text);
  378. }
  379. if (isset($sequence_links['resume']) && $sequence_links['resume']['url']) {
  380. $text = str_replace('[nid]', $sequence_links['resume']['url'], $text);
  381. }
  382. if (isset($sequence_links['first']) && $sequence_links['first']['url']) {
  383. $text = str_replace('[fid]', $sequence_links['first']['url'], $text);
  384. }
  385. /* contributed by Thomas M. Duffey <tduffey at homeboyz.com> */
  386. $html = !$html ? 0 : 1;
  387. // little hack added by greg to add syntax highlighting without using <?php \?\>
  388. $text = str_replace("[code]","[code]<?php",$text);
  389. $text = str_replace("[/code]","?>[/code]",$text);
  390. $text = preg_replace("/\[code\]\s*(.*)\s*\[\\/code\]/Usei", "ContentUtility::highlightCode(ContentUtility::fixQuotes('\\1'), $html)", $text);
  391. // now remove the <?php added above and leave the syntax colour behind.
  392. $text = str_replace("&lt;?php", "", $text);
  393. $text = str_replace("?&gt;", "", $text);
  394. return $text;
  395. }
  396. /* contributed by Thomas M. Duffey <tduffey at homeboyz.com> */
  397. private static function highlightCode($code, $html) {
  398. // XHTMLize PHP highlight_string output until it gets fixed in PHP
  399. static $search = array(
  400. '<br>',
  401. '<font',
  402. '</font>',
  403. 'color="');
  404. static $replace = array(
  405. '<br />',
  406. '<span',
  407. '</span>',
  408. 'style="color:');
  409. if (!$html) {
  410. $code = str_replace('&lt;', '<', $code);
  411. $code = str_replace("\r", '', $code);
  412. }
  413. return str_replace($search, $replace, highlight_string($code, true));
  414. }
  415. /* contributed by Thomas M. Duffey <tduffey at homeboyz.com> */
  416. private static function fixQuotes($text){
  417. return str_replace('\\"', '"', $text);
  418. }
  419. public static function imageReplace($text) {
  420. /* image urls do not require http:// */
  421. // $text = eregi_replace("\[image(\|)?([[:alnum:][:space:]]*)\]" .
  422. // "[:space:]*" .
  423. // "([[:alnum:]#?/&=:\"'_.-]+)" .
  424. // "[:space:]*" .
  425. // "((\[/image\])|(.*\[/image\]))",
  426. // "<img src=\"\\3\" alt=\"\\2\" />",
  427. // $text);
  428. $text = preg_replace("/\[image(\|)?([a-zA-Z0-9\s]*)\]".
  429. "[\s]*".
  430. "([a-zA-Z0-9\#\?\/\&\=\:\\\"\'\_\.\-]+)[\s]*".
  431. "((\[\/image\])|(.*\[\/image\]))/i",
  432. "<img src=\"\\3\" alt=\"\\2\" />",
  433. $text);
  434. return $text;
  435. }
  436. private static function formatFinalOutput($text, $nl2br = true) {
  437. global $_base_path;
  438. $text = str_replace('CONTENT_DIR/', '', $text);
  439. if ($nl2br) {
  440. return nl2br(ContentUtility::imageReplace(ContentUtility::makeClickable(ContentUtility::myCodes(' '.$text, false))));
  441. }
  442. return ContentUtility::imageReplace(ContentUtility::makeClickable(ContentUtility::myCodes(' '.$text, true)));
  443. }
  444. /**
  445. * This function converts the input string into AContent html content string
  446. * @access public
  447. * @param $input: input string
  448. * $html: whether the input is in html
  449. * @return converted AContent html content string
  450. * @author Cindy Qi Li
  451. */
  452. public static function formatContent($input, $html = 0) {
  453. global $_base_path, $_config;
  454. if (!$html) {
  455. $input = str_replace('<', '&lt;', $input);
  456. $input = str_replace('&lt;?php', '<?php', $input); // for bug #2087
  457. } elseif ($html==2) {
  458. $output = '<iframe width="100%" frameborder="0" id="content_frame" marginheight="0" marginwidth="0" src="'.$input.'"></iframe>';
  459. $output .= '<script type="text/javascript">
  460. function resizeIframe() {
  461. var height = document.documentElement.clientHeight;
  462. // not sure how to get this dynamically
  463. height -= 20; /* whatever you set your body bottom margin/padding to be */
  464. document.getElementById(\'content_frame\').style.height = height +"px";
  465. };
  466. document.getElementById(\'content_frame\').onload = resizeIframe;
  467. window.onresize = resizeIframe;
  468. </script>';
  469. return $output;
  470. }
  471. /* Commented by Cindy Qi Li on Jan 12, 2010
  472. * AContent does not support glossary
  473. // do the glossary search and replace:
  474. if (is_array($glossary)) {
  475. foreach ($glossary as $k => $v) {
  476. $k = urldecode($k);
  477. $v = str_replace("\n", '<br />', $v);
  478. $v = str_replace("\r", '', $v);
  479. // escape special characters
  480. $k = preg_quote($k);
  481. $k = str_replace('&lt;', '<', $k);
  482. $k = str_replace('/', '\/', $k);
  483. $original_term = $k;
  484. $term = $original_term;
  485. $term = '(\s*'.$term.'\s*)';
  486. $term = str_replace(' ','((<br \/>)*\s*)', $term);
  487. $def = htmlspecialchars($v, ENT_QUOTES, 'UTF-8');
  488. if ($simple) {
  489. $input = preg_replace
  490. ("/(\[\?\])$term(\[\/\?\])/i",
  491. '<a href="'.$simple.'glossary.html#'.urlencode($original_term).'" target="body" class="at-term">\\2</a>',
  492. $input);
  493. } else {
  494. $input = preg_replace
  495. ("/(\[\?\])$term(\[\/\?\])/i",
  496. '\\2<sup><a class="tooltip" href="'.$_base_path.'mods/_core/glossary/index.php?g_cid='.$_SESSION['s_cid'].htmlentities(SEP).'w='.urlencode($original_term).'#term" title="'.addslashes($original_term).': '.$def.'">?</a></sup>',$input);
  497. }
  498. }
  499. } else if (!$user_glossary) {
  500. $input = str_replace(array('[?]','[/?]'), '', $input);
  501. }
  502. */
  503. $input = str_replace('CONTENT_DIR', '', $input);
  504. if (isset($_config['latex_server']) && $_config['latex_server']) {
  505. // see: http://www.forkosh.com/mimetex.html
  506. $input = preg_replace('/\[tex\](.*?)\[\/tex\]/sie', "'<img src=\"'.\$_config['latex_server'].rawurlencode('$1').'\" align=\"middle\">'", $input);
  507. }
  508. if ($html) {
  509. $x = ContentUtility::formatFinalOutput($input, false);
  510. return $x;
  511. }
  512. // the following has been taken out for this:
  513. // http://atutor.ca/atutor/mantis/view.php?id=4593
  514. // @date Oct 18, 2010
  515. // $output = ContentUtility::formatFinalOutput($input);
  516. $output = $input;
  517. $output = '<p>'.$input.'</p>';
  518. return $output;
  519. }
  520. /**
  521. * This function returns html string of "table of content"
  522. * @access: public
  523. * @param: $content: a string
  524. * @return: a html string of "table of content"
  525. */
  526. public static function getContentTable($content)
  527. {
  528. preg_match_all("/<(h[\d]+)[^>]*>(.*)<\/(\s*)\\1(\s*)>/i", $content, $found_headers, PREG_SET_ORDER);
  529. if (count($found_headers) == 0) return array("", $content);
  530. else
  531. {
  532. $num_of_headers = 0;
  533. for ($i = 0; $i < count($found_headers); $i++)
  534. {
  535. $div_id = "_header_" . $num_of_headers++;
  536. if ($i == 0)
  537. {
  538. $content_table = "<div id=\"toc\">\n<fieldset id=\"toc\"><legend>". _AT("table_of_contents")."</legend>\n";
  539. }
  540. $content = str_replace($found_headers[$i][0], '<div id="'.$div_id.'">'.$found_headers[$i][0].'</div>', $content);
  541. $content_table .= '<a href="'.$_SERVER["REQUEST_URI"].'#'.$div_id.'" class="'.$found_headers[$i][1].'">'. $found_headers[$i][2]."</a>\n";
  542. if ($i == count($found_headers) - 1)
  543. {
  544. $content_table .= "</fieldset></div><br />";
  545. }
  546. }
  547. return array($content_table, $content);
  548. }
  549. }
  550. /**
  551. * This function returns an array of content tools' shortcuts
  552. * @access: public
  553. * @param: $content_row: an array of the current content information
  554. * @return: an array of all the tool short cuts that apply to the current content or content folder
  555. */
  556. public static function getToolShortcuts($content_row)
  557. {
  558. global $_current_user, $_base_href, $contentManager, $_course_id;
  559. if (((!$content_row['content_parent_id'] && ($_SESSION['packaging'] == 'top'))
  560. || ($_SESSION['packaging'] == 'all'))
  561. || (isset($_current_user) && $_current_user->isAuthor($_course_id))) {
  562. $tool_shortcuts[] = array(
  563. 'title' => _AT('export_content_in_cp'),
  564. 'url' => $_base_href . 'home/ims/ims_export.php?_cid='.$content_row['content_id'],
  565. 'icon' => $_base_href . 'themes/'.$_SESSION['prefs']['PREF_THEME'].'/images/export.png');
  566. $tool_shortcuts[] = array(
  567. 'title' => _AT('export_content_in_cc'),
  568. 'url' => $_base_href . 'home/imscc/ims_export.php?_cid='.$content_row['content_id'].SEP.'to_a4a=1',
  569. 'icon' => $_base_href . 'themes/'.$_SESSION['prefs']['PREF_THEME'].'/images/export_cc.png');
  570. }
  571. if (isset($_current_user) && $_current_user->isAuthor($_course_id)) {
  572. if ($content_row['content_type'] == CONTENT_TYPE_CONTENT || $content_row['content_type'] == CONTENT_TYPE_WEBLINK) {
  573. $tool_shortcuts[] = array(
  574. 'title' => _AT('edit_this_page'),
  575. 'url' => $_base_href . 'home/editor/edit_content.php?_cid='.$content_row['content_id'],
  576. 'icon' => $_base_href . 'images/medit.gif');
  577. }
  578. $tool_shortcuts[] = array(
  579. 'title' => _AT('add_sibling_folder'),
  580. 'url' => $_base_href .
  581. 'home/editor/edit_content_folder.php?pid='.$contentManager->_menu_info[$content_row['content_id']]['content_parent_id'].SEP.'_course_id='.$_course_id,
  582. 'icon' => $_base_href . 'images/add_sibling_folder.gif');
  583. if ($content_row['content_type'] == CONTENT_TYPE_FOLDER || $content_row['content_type'] == CONTENT_TYPE_WEBLINK) {
  584. $tool_shortcuts[] = array(
  585. 'title' => _AT('add_sub_folder'),
  586. 'url' => $_base_href .
  587. 'home/editor/edit_content_folder.php?_course_id='.$_course_id.SEP.'pid='.$content_row['content_id'],
  588. 'icon' => $_base_href . 'images/add_sub_folder.gif');
  589. }
  590. $tool_shortcuts[] = array(
  591. 'title' => _AT('add_sibling_page'),
  592. 'url' => $_base_href .
  593. 'home/editor/edit_content.php?pid='.$contentManager->_menu_info[$content_row['content_id']]['content_parent_id'].SEP.'_course_id='.$_course_id,
  594. 'icon' => $_base_href . 'images/add_sibling_page.gif');
  595. if ($content_row['content_type'] == CONTENT_TYPE_CONTENT || $content_row['content_type'] == CONTENT_TYPE_WEBLINK) {
  596. $tool_shortcuts[] = array(
  597. 'title' => _AT('delete_this_page'),
  598. 'url' => $_base_href . 'home/editor/delete_content.php?_cid='.$content_row['content_id'],
  599. 'icon' => $_base_href . 'images/page_delete.gif');
  600. }
  601. else if ($content_row['content_type'] == CONTENT_TYPE_FOLDER) {
  602. $tool_shortcuts[] = array(
  603. 'title' => _AT('add_sub_page'),
  604. 'url' => $_base_href . 'home/editor/edit_content.php?_course_id='.$_course_id.SEP.'pid='.$content_row['content_id'],
  605. 'icon' => $_base_href . 'images/add_sub_page.gif');
  606. $tool_shortcuts[] = array(
  607. 'title' => _AT('delete_this_folder'),
  608. 'url' => $_base_href . 'home/editor/delete_content.php?_cid='.$content_row['content_id'],
  609. 'icon' => $_base_href . 'images/page_delete.gif');
  610. }
  611. }
  612. return $tool_shortcuts;
  613. // if (isset($_current_user) && $_current_user->isAuthor($_course_id)) {
  614. // $shortcuts[] = array('title' => _AT('add_sub_folder'), 'url' => $_base_href . 'home/editor/edit_content_folder.php?_course_id='.$_course_id.'pid='.$cid);
  615. //
  616. //// $shortcuts[] = array('title' => _AT('add_top_page'), 'url' => $_base_href . 'home/editor/edit_content.php?_course_id='.$_course_id, 'icon' => $_base_href . 'images/page_add.gif');
  617. // if ($contentManager->_menu_info[$cid]['content_parent_id']) {
  618. // $shortcuts[] = array('title' => _AT('add_sibling_page'), 'url' => $_base_href .
  619. // 'home/editor/edit_content.php?_course_id='.$_course_id.SEP.'pid='.$contentManager->_menu_info[$cid]['content_parent_id'], 'icon' => $_base_href . 'images/page_add_sibling.gif');
  620. // }
  621. //
  622. // $shortcuts[] = array('title' => _AT('add_sub_page'), 'url' => $_base_href . 'home/editor/edit_content.php?_course_id='.$_course_id.SEP.'pid='.$cid);
  623. // $shortcuts[] = array('title' => _AT('delete_this_folder'), 'url' => $_base_href . 'home/editor/delete_content.php?_cid='.$cid, 'icon' => $_base_href . 'images/page_delete.gif');
  624. // }
  625. }
  626. /**
  627. * replace source object with alternatives according to user's preferences
  628. * @access public
  629. * @param $cid: content id.
  630. * @param $content: the original content page ($content_row['text'], from content.php).
  631. * @param $info_only: when "true", return the array of info (has_text_alternative, has_audio_alternative, has_visual_alternative, has_sign_lang_alternative)
  632. * @param $only_on_secondary_type:
  633. * @return string $content: the content page with the appropriated resources.
  634. * @see $db from include/vitals.inc.php
  635. * @author Cindy Qi Li
  636. */
  637. public static function applyAlternatives($cid, $content, $info_only = false, $only_on_secondary_type = 0){
  638. global $db, $_course_id;
  639. include_once(TR_INCLUDE_PATH.'classes/DAO/DAO.class.php');
  640. $dao = new DAO();
  641. $video_exts = array("mpg", "avi", "wmv", "mov", "swf", "mp4", "flv");
  642. $audio_exts = array("mp3", "wav", "ogg", "mid");
  643. $audio_width = 425;
  644. $audio_height = 27;
  645. $txt_exts = array("txt", "html", "htm");
  646. $image_exts = array("gif", "bmp", "png", "jpg", "jpeg", "png", "tif");
  647. $only_on_secondary_type = intval($only_on_secondary_type);
  648. // intialize the 4 returned values when $info_only is on
  649. if ($info_only)
  650. {
  651. $has_text_alternative = false;
  652. $has_audio_alternative = false;
  653. $has_visual_alternative = false;
  654. $has_sign_lang_alternative = false;
  655. }
  656. if (!$info_only && !$only_on_secondary_type &&
  657. ($_SESSION['prefs']['PREF_USE_ALTERNATIVE_TO_TEXT']==0) &&
  658. ($_SESSION['prefs']['PREF_USE_ALTERNATIVE_TO_AUDIO']==0) &&
  659. ($_SESSION['prefs']['PREF_USE_ALTERNATIVE_TO_VISUAL']==0))
  660. {
  661. //No user's preferences related to content format are declared
  662. if (!$info_only) {
  663. return $content;
  664. } else {
  665. return array($has_text_alternative, $has_audio_alternative, $has_visual_alternative, $has_sign_lang_alternative);
  666. }
  667. }
  668. // get all relations between primary resources and their alternatives
  669. $sql = "SELECT DISTINCT c.content_path, pr.resource, prt.type_id primary_type,
  670. sr.secondary_resource, srt.type_id secondary_type
  671. FROM ".TABLE_PREFIX."primary_resources pr, ".
  672. TABLE_PREFIX."primary_resources_types prt,".
  673. TABLE_PREFIX."secondary_resources sr,".
  674. TABLE_PREFIX."secondary_resources_types srt,".
  675. TABLE_PREFIX."content c
  676. WHERE pr.content_id=".$cid."
  677. AND pr.primary_resource_id = prt.primary_resource_id
  678. AND pr.primary_resource_id = sr.primary_resource_id
  679. AND sr.language_code='".$_SESSION['lang']."'
  680. AND sr.secondary_resource_id = srt.secondary_resource_id
  681. AND pr.content_id = c.content_id";
  682. if ($only_on_secondary_type > 0) {
  683. $sql .= " AND srt.type_id=".$only_on_secondary_type;
  684. }
  685. $sql .= " ORDER BY pr.primary_resource_id, prt.type_id";
  686. $rows = $dao->execute($sql);
  687. if (!is_array($rows)) {
  688. if (!$info_only) {
  689. return $content;
  690. } else {
  691. return array($has_text_alternative, $has_audio_alternative, $has_visual_alternative, $has_sign_lang_alternative);
  692. }
  693. }
  694. $primary_resource_names = array();
  695. foreach ($rows as $row) {
  696. // if the primary resource is defined with multiple resource type,
  697. // the primary resource would be replaced/appended multiple times.
  698. // This is what we want at applying alternatives by default, but
  699. // not when only one secondary type is chosen to apply.
  700. // This fix is to remove the duplicates on the same primary resource.
  701. // A dilemma of this fix is, for example, if the primary resource type
  702. // is "text" and "visual", but
  703. // $_SESSION['prefs']['PREF_ALT_TO_TEXT_APPEND_OR_REPLACE'] == 'replace'
  704. // $_SESSION['prefs']['PREF_ALT_TO_VISUAL_APPEND_OR_REPLACE'] == 'append'
  705. // so, should replace happen or append happen? With this fix, whichever
  706. // the first in the sql return gets preserved in the array and processed.
  707. // The further improvement is requried to keep rows based on the selected
  708. // secondary type (http://www.atutor.ca/atutor/mantis/view.php?id=4598).
  709. if ($only_on_secondary_type > 0) {
  710. if (in_array($row['resource'], $primary_resource_names)) {
  711. continue;
  712. } else {
  713. $primary_resource_names[] = $row['resource'];
  714. }
  715. }
  716. $alternative_rows[] = $row;
  717. $youtube_playURL = ContentUtility::convertYoutubeWatchURLToPlayURL($row['resource']);
  718. if ($row['resource'] <> $youtube_playURL) {
  719. $row['resource'] = $youtube_playURL;
  720. $alternative_rows[] = $row;
  721. }
  722. }
  723. foreach ($alternative_rows as $row)
  724. {
  725. if ($info_only || $only_on_secondary_type ||
  726. ($_SESSION['prefs']['PREF_USE_ALTERNATIVE_TO_TEXT']==1 && $row['primary_type']==3 &&
  727. ($_SESSION['prefs']['PREF_ALT_TO_TEXT']=="audio" && $row['secondary_type']==1 ||
  728. $_SESSION['prefs']['PREF_ALT_TO_TEXT']=="visual" && $row['secondary_type']==4 ||
  729. $_SESSION['prefs']['PREF_ALT_TO_TEXT']=="sign_lang" && $row['secondary_type']==2)) ||
  730. ($_SESSION['prefs']['PREF_USE_ALTERNATIVE_TO_AUDIO']==1 && $row['primary_type']==1 &&
  731. ($_SESSION['prefs']['PREF_ALT_TO_AUDIO']=="visual" && $row['secondary_type']==4 ||
  732. $_SESSION['prefs']['PREF_ALT_TO_AUDIO']=="text" && $row['secondary_type']==3 ||
  733. $_SESSION['prefs']['PREF_ALT_TO_AUDIO']=="sign_lang" && $row['secondary_type']==2)) ||
  734. ($_SESSION['prefs']['PREF_USE_ALTERNATIVE_TO_VISUAL']==1 && $row['primary_type']==4 &&
  735. ($_SESSION['prefs']['PREF_ALT_TO_VISUAL']=="audio" && $row['secondary_type']==1 ||
  736. $_SESSION['prefs']['PREF_ALT_TO_VISUAL']=="text" && $row['secondary_type']==3 ||
  737. $_SESSION['prefs']['PREF_ALT_TO_VISUAL']=="sign_lang" && $row['secondary_type']==2))
  738. )
  739. {
  740. $ext = substr($row['secondary_resource'], strrpos($row['secondary_resource'], '.')+1);
  741. // alternative is video
  742. if (in_array($ext, $video_exts)|| in_array($ext, $audio_exts) ||
  743. preg_match("/http:\/\/.*youtube.com\/watch.*/", $row['secondary_resource'])) {
  744. if (in_array($ext, $audio_exts)) {
  745. // display audio medias in a smaller width/height (425 * 27)
  746. // A hack for now to handle audio media player size
  747. $target = '[media|'.$audio_width.'|'.$audio_height.']'.$row['secondary_resource'].'[/media]';
  748. } else {
  749. // use default media size for video medias
  750. $target = '[media]'.$row['secondary_resource'].'[/media]';
  751. }
  752. }
  753. // a text primary to be replaced by a visual alternative
  754. else if (in_array($ext, $txt_exts))
  755. {
  756. if ($row['content_path'] <> '')
  757. $file_location = $row['content_path'].'/'.$row['secondary_resource'];
  758. else
  759. $file_location = $row['secondary_resource'];
  760. $file = TR_CONTENT_DIR.$_SESSION['course_id'] . '/'.$file_location;
  761. $target = '<br />'.file_get_contents($file);
  762. // check whether html file
  763. if (preg_match('/.*\<html.*\<\/html\>.*/s', $target))
  764. { // is a html file, use iframe to display
  765. // get real path to the text file
  766. if (defined('TR_FORCE_GET_FILE') && TR_FORCE_GET_FILE) {
  767. $course_base_href = 'get.php/';
  768. } else {
  769. $course_base_href = 'content/' . $_SESSION['course_id'] . '/';
  770. }
  771. $file = TR_BASE_HREF . $course_base_href.$file_location;
  772. $target = '<iframe width="100%" frameborder="0" class="autoHeight" scrolling="auto" src="'.$file.'"></iframe>';
  773. }
  774. else
  775. { // is a text file, insert/replace into content
  776. $target = nl2br($target);
  777. }
  778. }
  779. else if (in_array($ext, $image_exts))
  780. $target = '<img border="0" alt="'._AT('alternate_text').'" src="'.$row['secondary_resource'].'"/>';
  781. // otherwise
  782. else
  783. $target = '<p><a href="'.$row['secondary_resource'].'">'.$row['secondary_resource'].'</a></p>';
  784. // replace or append the target alternative to the source
  785. if (($row['primary_type']==3 && $_SESSION['prefs']['PREF_ALT_TO_TEXT_APPEND_OR_REPLACE'] == 'replace') ||
  786. ($row['primary_type']==1 && $_SESSION['prefs']['PREF_ALT_TO_AUDIO_APPEND_OR_REPLACE']=='replace') ||
  787. ($row['primary_type']==4 && $_SESSION['prefs']['PREF_ALT_TO_VISUAL_APPEND_OR_REPLACE']=='replace'))
  788. $pattern_replace_to = '${1}'."\n".$target."\n".'${3}';
  789. else
  790. $pattern_replace_to = '${1}${2}'."<br /><br />\n".$target."\n".'${3}';
  791. // *** Alternative replace/append starts from here ***
  792. $processed = false; // one primary resource is only processed once
  793. // append/replace target alternative to [media]source[/media]
  794. if (!$processed && preg_match("/".preg_quote("[media").".*".preg_quote("]".$row['resource']."[/media]", "/")."/sU", $content))
  795. {
  796. $processed = true;
  797. if (!$info_only) {
  798. $content = preg_replace("/(.*)(".preg_quote("[media").".*".preg_quote("]".$row['resource']."[/media]", "/").")(.*)/sU",
  799. $pattern_replace_to, $content);
  800. } else {
  801. if ($row['secondary_type'] == 1) $has_audio_alternative = true;
  802. if ($row['secondary_type'] == 2) $has_sign_lang_alternative = true;
  803. if ($row['secondary_type'] == 3) $has_text_alternative = true;
  804. if ($row['secondary_type'] == 4) $has_visual_alternative = true;
  805. }
  806. }
  807. // append/replace target alternative to <img ... src="source" ...></a>
  808. if (!$processed && preg_match("/\<img.*src=\"".preg_quote($row['resource'], "/")."\".*\/\>/sU", $content))
  809. {
  810. $processed = true;
  811. if (!$info_only) {
  812. $content = preg_replace("/(.*)(\<img.*src=\"".preg_quote($row['resource'], "/")."\".*\/\>)(.*)/sU",
  813. $pattern_replace_to, $content);
  814. } else {
  815. if ($row['secondary_type'] == 1) $has_audio_alternative = true;
  816. if ($row['secondary_type'] == 2) $has_sign_lang_alternative = true;
  817. if ($row['secondary_type'] == 3) $has_text_alternative = true;
  818. if ($row['secondary_type'] == 4) $has_visual_alternative = true;
  819. }
  820. }
  821. // append/replace target alternative to <object ... source ...></object>
  822. if (!$processed && preg_match("/\<object.*".preg_quote($row['resource'], "/").".*\<\/object\>/sU", $content))
  823. {
  824. $processed = true;
  825. if (!$info_only) {
  826. $content = preg_replace("/(.*)(\<object.*".preg_quote($row['resource'], "/").".*\<\/object\>)(.*)/sU",
  827. $pattern_replace_to, $content);
  828. } else {
  829. if ($row['secondary_type'] == 1) $has_audio_alternative = true;
  830. if ($row['secondary_type'] == 2) $has_sign_lang_alternative = true;
  831. if ($row['secondary_type'] == 3) $has_text_alternative = true;
  832. if ($row['secondary_type'] == 4) $has_visual_alternative = true;
  833. }
  834. }
  835. // append/replace target alternative to <a>...source...</a> or <a ...source...>...</a>
  836. // skip this "if" when the source object has been processed in aboved <img> tag
  837. if (!$processed && preg_match("/\<a.*".preg_quote($row['resource'], "/").".*\<\/a\>/sU", $content))
  838. {
  839. $processed = true;
  840. if (!$info_only) {
  841. $content = preg_replace("/(.*)(\<a.*".preg_quote($row['resource'], "/").".*\<\/a\>)(.*)/sU",
  842. $pattern_replace_to, $content);
  843. } else {
  844. if ($row['secondary_type'] == 1) $has_audio_alternative = true;
  845. if ($row['secondary_type'] == 2) $has_sign_lang_alternative = true;
  846. if ($row['secondary_type'] == 3) $has_text_alternative = true;
  847. if ($row['secondary_type'] == 4) $has_visual_alternative = true;
  848. }
  849. }
  850. // append/replace target alternative to <embed ... source ...>
  851. if (!$processed && preg_match("/\<embed.*".preg_quote($row['resource'], "/").".*\>/sU", $content))
  852. {
  853. $processed = true;
  854. if (!$info_only) {
  855. $content = preg_replace("/(.*)(\<embed.*".preg_quote($row['resource'], "/").".*\>)(.*)/sU",
  856. $pattern_replace_to, $content);
  857. } else {
  858. if ($row['secondary_type'] == 1) $has_audio_alternative = true;
  859. if ($row['secondary_type'] == 2) $has_sign_lang_alternative = true;
  860. if ($row['secondary_type'] == 3) $has_text_alternative = true;
  861. if ($row['secondary_type'] == 4) $has_visual_alternative = true;
  862. }
  863. }
  864. }
  865. }
  866. if (!$info_only) {
  867. return $content;
  868. } else {
  869. return array($has_text_alternative, $has_audio_alternative, $has_visual_alternative, $has_sign_lang_alternative);
  870. }
  871. }
  872. /**
  873. * This function save the last content_id accessed by current user on a course into db and set $_SESSION['s_cid']
  874. * @access: public
  875. * @param: $content_id
  876. * @return: save $content_id, the last visited one of the current user, into db and session
  877. */
  878. public static function saveLastCid($content_id)
  879. {
  880. global $_course_id;
  881. if (!$content_id || !isset($_SESSION['user_id'])) return;
  882. include_once(TR_INCLUDE_PATH.'classes/DAO/UserCoursesDAO.class.php');
  883. $userCoursesDAO = new UserCoursesDAO();
  884. if ($userCoursesDAO->isExist($_SESSION['user_id'], $_course_id))
  885. {
  886. $userCoursesDAO->UpdateLastCid($_SESSION['user_id'], $_course_id, $content_id);
  887. $_SESSION['s_cid'] = $content_id;
  888. }
  889. }
  890. }
  891. ?>