ATutor 2.0
[atutor.git] / mods / _core / imsafa / classes / A4aExport.class.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2009                                                                              */
6 /* Inclusive Design Institute                                   */
7 /* http://atutor.ca                                                                                             */
8 /*                                                              */
9 /* This program is free software. You can redistribute it and/or*/
10 /* modify it under the terms of the GNU General Public License  */
11 /* as published by the Free Software Foundation.                                */
12 /****************************************************************/
13 // $Id: A4aExport.class.php 8804 2009-09-29 21:17:00Z hwong $
14
15 require_once(AT_INCLUDE_PATH.'../mods/_core/imsafa/classes/A4a.class.php');
16 //require(AT_INCLUDE_PATH.'classes/zipfile.class.php'); // for zipfile
17
18 /**
19  * Accessforall Export class.
20  * Based on the specification at: 
21  *              http://www.imsglobal.org/accessibility/index.html
22  *
23  * @date        Oct 3rd, 2008
24  * @author      Harris Wong
25  */
26 class A4aExport extends A4a {
27         var $original_files = array();  //store all the primary/original resources in [id]=>[incrementer]=>[resource, language, type]
28 //      var $alternative_files = array();       //secondary files aka. alternative, equivalent
29
30         /**
31          * Get information for this content
32          * @return the xml content
33          */     
34         function getAlternative(){
35                 $resources = parent::getPrimaryResources();
36                 foreach($resources as $rid => $prop){
37                         $resources_types = parent::getPrimaryResourcesTypes($rid);
38                         $temp = array();
39                         $secondary_array = array();
40                         foreach($resources_types as $rtid){
41                                 $sec_resources['secondary_resources'] = parent::getSecondaryResources($rid);
42                                 //determine secondary resource type
43                                 foreach ($sec_resources['secondary_resources'] as $sec_id => $sec_resource){
44                                         $current_sec_file = $sec_resource['resource'];
45                                         $secondary_array['secondary_resources'][] = $current_sec_file ;
46                                         //add to secondary file array if it's not there
47                                         if (!isset($this->original_files[$current_sec_file]) || empty($this->original_files[$current_sec_file]) ){
48                                                 //TODO merge these values i think
49                                                 $this->original_files[$current_sec_file ] = $sec_resource;
50                                                 $this->original_files[$current_sec_file ]['resource_type'][$prop['resource']][] = parent::getSecondaryResourcesTypes($sec_id);
51                                         } else {
52                                                 $this->original_files[$current_sec_file ]['resource_type'][$prop['resource']][] = parent::getSecondaryResourcesTypes($sec_id);
53                                         }
54                                         //add this primary file ref, and the resources type to the secondary file
55                                         $this->original_files[$current_sec_file]['primary_resources'][$prop['resource']] = $rtid;
56 //                                      $this->original_files[$current_sec_file]['primary_resources'][$prop['resource']]['language_code'] = $sec_resource['language_code'];
57                                 }
58                         }
59                         $res_type['resource_type'] = $rtid;     //could be 1+
60                         $temp = array_merge($prop, $res_type, $secondary_array);
61
62                         if (isset($this->original_files[$temp['resource']])){
63                                 //use the existing temp array values, but merge in the secondary_array
64                                 $temp = array_merge($this->original_files[$temp['resource']], $secondary_array);
65                         } 
66                         if(!empty($temp)){
67                                 $this->original_files[$temp['resource']] = $temp;
68         //                      debug($this->original_files['7dolomiti_1a_como1e_como_road1b.jpg'], $rid);
69                         }
70                 }
71                 return $this->original_files;
72         }
73
74         /**
75          * Get all secondary files
76          * @return array of secondary files that is being used in this->content.
77          */
78         function getAllSecondaryFiles(){
79                 global $db;
80                 $secondary_files = array();
81
82                 $sql = "SELECT DISTINCT secondary_resource FROM ".TABLE_PREFIX."primary_resources a LEFT JOIN ".TABLE_PREFIX."secondary_resources s
83                                 ON a.primary_resource_id = s.primary_resource_id WHERE content_id=".$this->cid;
84                 $result = mysql_query ($sql);
85                 if ($result){
86                         while ($row = mysql_fetch_assoc($result)){
87                                 if (!empty($row['secondary_resource'])){
88                                         $secondary_files[] = $row['secondary_resource'];
89                                 }
90                         }
91                 }
92                 return $secondary_files;
93         }
94
95         // Save all the xml into an array. 
96         // key=filename, value=xml content
97         function exportA4a(){
98                 global $savant;
99
100                 $xml_array = array();   //array of xml
101
102                 // Get the alt content first.
103                 $this->getAlternative();
104
105                 // Get original files' xml 
106                 foreach($this->original_files as $id=>$resource){
107                         $orig_access_mode = array();
108                         foreach($resource['resource_type'] as $type_id){
109                                 if (!is_array($type_id)){
110                                         //primary resource will always have just on type
111                                         $orig_access_mode[] = $this->getResourceNameById($type_id);
112                                 } else {
113                                         foreach($type_id as $k=>$type_id2){
114                                                 $orig_access_mode[] = $this->getResourceNameById($type_id2[0]);
115                                         }
116                                 }
117                         }
118                         $savant->assign('relative_path', $this->relative_path); //the template will need the relative path
119                         $savant->assign('orig_access_mode', $orig_access_mode);
120                         $savant->assign('language_code', $resource['language_code']);
121                         $savant->assign('secondary_resources', $resource['secondary_resources']);
122
123                         // If this is an alternative, and it is mapping to 
124                         // 1+ original files.  Each of these mapping requires
125                         // its own xml
126                         if(isset($resource['primary_resources'])){
127                                 foreach($resource['primary_resources'] as $uri=>$pri_resource_types){
128                                         $savant->assign('primary_resource_uri', $uri);
129                                         $savant->assign('primary_resources', $pri_resource_types);
130                                         //A file can be both original and alternative, and each could represent diff language
131                                         //Tried to resolve but the A4a v.2 only accept 1 language
132 //                                      $savant->assign('language_code', $pri_resource_types['language_code']);
133                                         //overwrite orig_access_mode                                    
134                                         $orig_access_mode = array(); //reinitialize
135                                         foreach($resource['resource_type'][$uri] as $type_id){
136                                                 $orig_access_mode[] = $this->getResourceNameById($type_id);
137                                                 $savant->assign('orig_access_mode', $orig_access_mode);
138                                                 $xml_array[$id.' to '.$uri][] = $savant->fetch(AT_INCLUDE_PATH.'../mods/_core/imsafa/classes/A4a.tmpl.php');
139                                         }
140                                         
141                                 }
142                         } else {
143                                 $savant->assign('primary_resource_uri', '');
144                                 $savant->assign('primary_resources', '');
145                                 $xml_array[$id] = $savant->fetch(AT_INCLUDE_PATH.'../mods/_core/imsafa/classes/A4a.tmpl.php');
146                         }
147                 }
148                 return $xml_array;
149         }
150
151         /**
152          * Get resource name by id
153          * @return      array
154          */
155         function getResourceNameById($type_id){
156                 $orig_access_mode = '';
157                 if (is_array($type_id)) {
158                         $type_id = $type_id[0];
159                 }
160                 switch($type_id){
161                         case 1:
162                                 $orig_access_mode = 'auditory';
163                                 break;
164                         case 3:
165                                 $orig_access_mode = 'textual';
166                                 break;
167                         case 2:
168                                 $orig_access_mode = 'sign_language';
169                                 break;
170                         case 4:
171                                 $orig_access_mode = 'visual';
172                                 break;
173                         default:
174                                 $orig_access_mode = '';
175                 }
176                 return $orig_access_mode;
177         }
178
179 }
180
181 ?>