Ejemplo n.º 1
0
 /**
  * replace source object with alternatives according to user's preferences
  * @access	public
  * @param	$cid: 				content id.
  * @param	$content:	 		the original content page ($content_row['text'], from content.php).
  * @param    $info_only:         when "true", return the array of info (has_text_alternative, has_audio_alternative, has_visual_alternative, has_sign_lang_alternative)
  * @param    $only_on_secondary_type: 
  * @return	string				$content: the content page with the appropriated resources.
  * @see		$db			        from include/vitals.inc.php
  * @author	Cindy Qi Li
  */
 public static function applyAlternatives($cid, $content, $info_only = false, $only_on_secondary_type = 0)
 {
     global $db, $_course_id;
     include_once TR_INCLUDE_PATH . 'classes/DAO/DAO.class.php';
     $dao = new DAO();
     $video_exts = array("mpg", "avi", "wmv", "mov", "swf", "mp4", "flv");
     $audio_exts = array("mp3", "wav", "ogg", "mid");
     $audio_width = 425;
     $audio_height = 27;
     $txt_exts = array("txt", "html", "htm");
     $image_exts = array("gif", "bmp", "png", "jpg", "jpeg", "png", "tif");
     $only_on_secondary_type = intval($only_on_secondary_type);
     // intialize the 4 returned values when $info_only is on
     if ($info_only) {
         $has_text_alternative = false;
         $has_audio_alternative = false;
         $has_visual_alternative = false;
         $has_sign_lang_alternative = false;
     }
     if (!$info_only && !$only_on_secondary_type && $_SESSION['prefs']['PREF_USE_ALTERNATIVE_TO_TEXT'] == 0 && $_SESSION['prefs']['PREF_USE_ALTERNATIVE_TO_AUDIO'] == 0 && $_SESSION['prefs']['PREF_USE_ALTERNATIVE_TO_VISUAL'] == 0) {
         //No user's preferences related to content format are declared
         if (!$info_only) {
             return $content;
         } else {
             return array($has_text_alternative, $has_audio_alternative, $has_visual_alternative, $has_sign_lang_alternative);
         }
     }
     // get all relations between primary resources and their alternatives
     $sql = "SELECT DISTINCT c.content_path, pr.resource,  prt.type_id primary_type,\r\n                       sr.secondary_resource, srt.type_id secondary_type\r\n\t\t          FROM " . TABLE_PREFIX . "primary_resources pr, " . TABLE_PREFIX . "primary_resources_types prt," . TABLE_PREFIX . "secondary_resources sr," . TABLE_PREFIX . "secondary_resources_types srt," . TABLE_PREFIX . "content c\r\n\t\t         WHERE pr.content_id=" . $cid . "\r\n\t\t\t       AND pr.primary_resource_id = prt.primary_resource_id\r\n\t\t\t       AND pr.primary_resource_id = sr.primary_resource_id\r\n\t\t\t       AND sr.language_code='" . $_SESSION['lang'] . "'\r\n\t\t\t       AND sr.secondary_resource_id = srt.secondary_resource_id\r\n\t\t           AND pr.content_id = c.content_id";
     if ($only_on_secondary_type > 0) {
         $sql .= " AND srt.type_id=" . $only_on_secondary_type;
     }
     $sql .= " ORDER BY pr.primary_resource_id, prt.type_id";
     $rows = $dao->execute($sql);
     if (!is_array($rows)) {
         if (!$info_only) {
             return $content;
         } else {
             return array($has_text_alternative, $has_audio_alternative, $has_visual_alternative, $has_sign_lang_alternative);
         }
     }
     $primary_resource_names = array();
     foreach ($rows as $row) {
         // if the primary resource is defined with multiple resource type,
         // the primary resource would be replaced/appended multiple times.
         // This is what we want at applying alternatives by default, but
         // not when only one secondary type is chosen to apply.
         // This fix is to remove the duplicates on the same primary resource.
         // A dilemma of this fix is, for example, if the primary resource type
         // is "text" and "visual", but
         // $_SESSION['prefs']['PREF_ALT_TO_TEXT_APPEND_OR_REPLACE'] == 'replace'
         // $_SESSION['prefs']['PREF_ALT_TO_VISUAL_APPEND_OR_REPLACE'] == 'append'
         // so, should replace happen or append happen? With this fix, whichever
         // the first in the sql return gets preserved in the array and processed.
         // The further improvement is requried to keep rows based on the selected
         // secondary type (http://www.atutor.ca/atutor/mantis/view.php?id=4598).
         if ($only_on_secondary_type > 0) {
             if (in_array($row['resource'], $primary_resource_names)) {
                 continue;
             } else {
                 $primary_resource_names[] = $row['resource'];
             }
         }
         $alternative_rows[] = $row;
         $youtube_playURL = ContentUtility::convertYoutubeWatchURLToPlayURL($row['resource']);
         if ($row['resource'] != $youtube_playURL) {
             $row['resource'] = $youtube_playURL;
             $alternative_rows[] = $row;
         }
     }
     foreach ($alternative_rows as $row) {
         if ($info_only || $only_on_secondary_type || $_SESSION['prefs']['PREF_USE_ALTERNATIVE_TO_TEXT'] == 1 && $row['primary_type'] == 3 && ($_SESSION['prefs']['PREF_ALT_TO_TEXT'] == "audio" && $row['secondary_type'] == 1 || $_SESSION['prefs']['PREF_ALT_TO_TEXT'] == "visual" && $row['secondary_type'] == 4 || $_SESSION['prefs']['PREF_ALT_TO_TEXT'] == "sign_lang" && $row['secondary_type'] == 2) || $_SESSION['prefs']['PREF_USE_ALTERNATIVE_TO_AUDIO'] == 1 && $row['primary_type'] == 1 && ($_SESSION['prefs']['PREF_ALT_TO_AUDIO'] == "visual" && $row['secondary_type'] == 4 || $_SESSION['prefs']['PREF_ALT_TO_AUDIO'] == "text" && $row['secondary_type'] == 3 || $_SESSION['prefs']['PREF_ALT_TO_AUDIO'] == "sign_lang" && $row['secondary_type'] == 2) || $_SESSION['prefs']['PREF_USE_ALTERNATIVE_TO_VISUAL'] == 1 && $row['primary_type'] == 4 && ($_SESSION['prefs']['PREF_ALT_TO_VISUAL'] == "audio" && $row['secondary_type'] == 1 || $_SESSION['prefs']['PREF_ALT_TO_VISUAL'] == "text" && $row['secondary_type'] == 3 || $_SESSION['prefs']['PREF_ALT_TO_VISUAL'] == "sign_lang" && $row['secondary_type'] == 2)) {
             $ext = substr($row['secondary_resource'], strrpos($row['secondary_resource'], '.') + 1);
             // alternative is video
             if (in_array($ext, $video_exts) || in_array($ext, $audio_exts) || preg_match("/http:\\/\\/.*youtube.com\\/watch.*/", $row['secondary_resource'])) {
                 if (in_array($ext, $audio_exts)) {
                     // display audio medias in a smaller width/height (425 * 27)
                     // A hack for now to handle audio media player size
                     $target = '[media|' . $audio_width . '|' . $audio_height . ']' . $row['secondary_resource'] . '[/media]';
                 } else {
                     // use default media size for video medias
                     $target = '[media]' . $row['secondary_resource'] . '[/media]';
                 }
             } else {
                 if (in_array($ext, $txt_exts)) {
                     if ($row['content_path'] != '') {
                         $file_location = $row['content_path'] . '/' . $row['secondary_resource'];
                     } else {
                         $file_location = $row['secondary_resource'];
                     }
                     $file = TR_CONTENT_DIR . $_SESSION['course_id'] . '/' . $file_location;
                     $target = '<br />' . file_get_contents($file);
                     // check whether html file
                     if (preg_match('/.*\\<html.*\\<\\/html\\>.*/s', $target)) {
                         // is a html file, use iframe to display
                         // get real path to the text file
                         if (defined('TR_FORCE_GET_FILE') && TR_FORCE_GET_FILE) {
                             $course_base_href = 'get.php/';
                         } else {
                             $course_base_href = 'content/' . $_SESSION['course_id'] . '/';
                         }
                         $file = TR_BASE_HREF . $course_base_href . $file_location;
                         $target = '<iframe width="100%" frameborder="0" class="autoHeight" scrolling="auto" src="' . $file . '"></iframe>';
                     } else {
                         // is a text file, insert/replace into content
                         $target = nl2br($target);
                     }
                 } else {
                     if (in_array($ext, $image_exts)) {
                         $target = '<img border="0" alt="' . _AT('alternate_text') . '" src="' . $row['secondary_resource'] . '"/>';
                     } else {
                         $target = '<p><a href="' . $row['secondary_resource'] . '">' . $row['secondary_resource'] . '</a></p>';
                     }
                 }
             }
             // replace or append the target alternative to the source
             if ($row['primary_type'] == 3 && $_SESSION['prefs']['PREF_ALT_TO_TEXT_APPEND_OR_REPLACE'] == 'replace' || $row['primary_type'] == 1 && $_SESSION['prefs']['PREF_ALT_TO_AUDIO_APPEND_OR_REPLACE'] == 'replace' || $row['primary_type'] == 4 && $_SESSION['prefs']['PREF_ALT_TO_VISUAL_APPEND_OR_REPLACE'] == 'replace') {
                 $pattern_replace_to = '${1}' . "\n" . $target . "\n" . '${3}';
             } else {
                 $pattern_replace_to = '${1}${2}' . "<br /><br />\n" . $target . "\n" . '${3}';
             }
             // *** Alternative replace/append starts from here ***
             $processed = false;
             // one primary resource is only processed once
             // append/replace target alternative to [media]source[/media]
             if (!$processed && preg_match("/" . preg_quote("[media") . ".*" . preg_quote("]" . $row['resource'] . "[/media]", "/") . "/sU", $content)) {
                 $processed = true;
                 if (!$info_only) {
                     $content = preg_replace("/(.*)(" . preg_quote("[media") . ".*" . preg_quote("]" . $row['resource'] . "[/media]", "/") . ")(.*)/sU", $pattern_replace_to, $content);
                 } else {
                     if ($row['secondary_type'] == 1) {
                         $has_audio_alternative = true;
                     }
                     if ($row['secondary_type'] == 2) {
                         $has_sign_lang_alternative = true;
                     }
                     if ($row['secondary_type'] == 3) {
                         $has_text_alternative = true;
                     }
                     if ($row['secondary_type'] == 4) {
                         $has_visual_alternative = true;
                     }
                 }
             }
             // append/replace target alternative to <img ... src="source" ...></a>
             if (!$processed && preg_match("/\\<img.*src=\"" . preg_quote($row['resource'], "/") . "\".*\\/\\>/sU", $content)) {
                 $processed = true;
                 if (!$info_only) {
                     $content = preg_replace("/(.*)(\\<img.*src=\"" . preg_quote($row['resource'], "/") . "\".*\\/\\>)(.*)/sU", $pattern_replace_to, $content);
                 } else {
                     if ($row['secondary_type'] == 1) {
                         $has_audio_alternative = true;
                     }
                     if ($row['secondary_type'] == 2) {
                         $has_sign_lang_alternative = true;
                     }
                     if ($row['secondary_type'] == 3) {
                         $has_text_alternative = true;
                     }
                     if ($row['secondary_type'] == 4) {
                         $has_visual_alternative = true;
                     }
                 }
             }
             // append/replace target alternative to <object ... source ...></object>
             if (!$processed && preg_match("/\\<object.*" . preg_quote($row['resource'], "/") . ".*\\<\\/object\\>/sU", $content)) {
                 $processed = true;
                 if (!$info_only) {
                     $content = preg_replace("/(.*)(\\<object.*" . preg_quote($row['resource'], "/") . ".*\\<\\/object\\>)(.*)/sU", $pattern_replace_to, $content);
                 } else {
                     if ($row['secondary_type'] == 1) {
                         $has_audio_alternative = true;
                     }
                     if ($row['secondary_type'] == 2) {
                         $has_sign_lang_alternative = true;
                     }
                     if ($row['secondary_type'] == 3) {
                         $has_text_alternative = true;
                     }
                     if ($row['secondary_type'] == 4) {
                         $has_visual_alternative = true;
                     }
                 }
             }
             // append/replace target alternative to <a>...source...</a> or <a ...source...>...</a>
             // skip this "if" when the source object has been processed in aboved <img> tag
             if (!$processed && preg_match("/\\<a.*" . preg_quote($row['resource'], "/") . ".*\\<\\/a\\>/sU", $content)) {
                 $processed = true;
                 if (!$info_only) {
                     $content = preg_replace("/(.*)(\\<a.*" . preg_quote($row['resource'], "/") . ".*\\<\\/a\\>)(.*)/sU", $pattern_replace_to, $content);
                 } else {
                     if ($row['secondary_type'] == 1) {
                         $has_audio_alternative = true;
                     }
                     if ($row['secondary_type'] == 2) {
                         $has_sign_lang_alternative = true;
                     }
                     if ($row['secondary_type'] == 3) {
                         $has_text_alternative = true;
                     }
                     if ($row['secondary_type'] == 4) {
                         $has_visual_alternative = true;
                     }
                 }
             }
             // append/replace target alternative to <embed ... source ...>
             if (!$processed && preg_match("/\\<embed.*" . preg_quote($row['resource'], "/") . ".*\\>/sU", $content)) {
                 $processed = true;
                 if (!$info_only) {
                     $content = preg_replace("/(.*)(\\<embed.*" . preg_quote($row['resource'], "/") . ".*\\>)(.*)/sU", $pattern_replace_to, $content);
                 } else {
                     if ($row['secondary_type'] == 1) {
                         $has_audio_alternative = true;
                     }
                     if ($row['secondary_type'] == 2) {
                         $has_sign_lang_alternative = true;
                     }
                     if ($row['secondary_type'] == 3) {
                         $has_text_alternative = true;
                     }
                     if ($row['secondary_type'] == 4) {
                         $has_visual_alternative = true;
                     }
                 }
             }
         }
     }
     if (!$info_only) {
         return $content;
     } else {
         return array($has_text_alternative, $has_audio_alternative, $has_visual_alternative, $has_sign_lang_alternative);
     }
 }