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