move code up one directory
[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$
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             if (empty($resources_types)){
39                 //if there are no resource types, then don't use it.
40                 continue;
41             }
42
43                         $temp = array();
44                         $secondary_array = array();
45                         foreach($resources_types as $rtid){
46                                 $sec_resources['secondary_resources'] = parent::getSecondaryResources($rid);
47                                 //determine secondary resource type
48                                 foreach ($sec_resources['secondary_resources'] as $sec_id => $sec_resource){
49                                         $current_sec_file = $sec_resource['resource'];
50                                         $secondary_array['secondary_resources'][] = $current_sec_file ;
51                                         //add to secondary file array if it's not there
52                                         if (!isset($this->original_files[$current_sec_file]) || empty($this->original_files[$current_sec_file]) ){
53                                                 //TODO merge these values i think
54                                                 $this->original_files[$current_sec_file ] = $sec_resource;
55                                                 $this->original_files[$current_sec_file ]['resource_type'][$prop['resource']][] = parent::getSecondaryResourcesTypes($sec_id);
56                                         } else {
57                                                 $this->original_files[$current_sec_file ]['resource_type'][$prop['resource']][] = parent::getSecondaryResourcesTypes($sec_id);
58                                         }
59                                         //add this primary file ref, and the resources type to the secondary file
60                                         $this->original_files[$current_sec_file]['primary_resources'][$prop['resource']] = $rtid;
61 //                                      $this->original_files[$current_sec_file]['primary_resources'][$prop['resource']]['language_code'] = $sec_resource['language_code'];
62                                 }
63                         }
64                         $res_type['resource_type'] = $rtid;     //could be 1+
65                         $temp = array_merge($prop, $res_type, $secondary_array);
66
67                         if (isset($this->original_files[$temp['resource']])){
68                                 //use the existing temp array values, but merge in the secondary_array
69                                 $temp = array_merge($this->original_files[$temp['resource']], $secondary_array);
70                         } 
71                         if(!empty($temp)){
72                                 $this->original_files[$temp['resource']] = $temp;
73                         }
74                 }
75                 return $this->original_files;
76         }
77
78         /**
79          * Get all secondary files
80          * @return array of secondary files that is being used in this->content.
81          */
82         function getAllSecondaryFiles(){
83                 global $db;
84                 $secondary_files = array();
85
86                 $sql = "SELECT DISTINCT secondary_resource FROM ".TABLE_PREFIX."primary_resources a LEFT JOIN ".TABLE_PREFIX."secondary_resources s
87                                 ON a.primary_resource_id = s.primary_resource_id WHERE content_id=".$this->cid;
88                 $result = mysql_query ($sql);
89                 if ($result){
90                         while ($row = mysql_fetch_assoc($result)){
91                                 if (!empty($row['secondary_resource'])){
92                                         $secondary_files[] = $row['secondary_resource'];
93                                 }
94                         }
95                 }
96                 return $secondary_files;
97         }
98
99         // Save all the xml into an array. 
100         // key=filename, value=xml content
101         function exportA4a(){
102                 global $savant;
103
104                 $xml_array = array();   //array of xml
105
106                 // Get the alt content first.
107                 $this->getAlternative();
108
109                 // Get original files' xml 
110                 foreach($this->original_files as $id=>$resource){
111                         $orig_access_mode = array();
112                         foreach($resource['resource_type'] as $type_id){
113                                 if (!is_array($type_id)){
114                                         //primary resource will always have just on type
115                                         $orig_access_mode[] = $this->getResourceNameById($type_id);
116                                 } else {
117                                         foreach($type_id as $k=>$type_id2){
118                                                 $orig_access_mode[] = $this->getResourceNameById($type_id2[0]);
119                                         }
120                                 }
121                         }
122                         $savant->assign('relative_path', $this->relative_path); //the template will need the relative path
123                         $savant->assign('orig_access_mode', $orig_access_mode);
124                         $savant->assign('language_code', $resource['language_code']);
125                         $savant->assign('secondary_resources', $resource['secondary_resources']);
126
127                         // If this is an alternative, and it is mapping to 
128                         // 1+ original files.  Each of these mapping requires
129                         // its own xml
130                         if(isset($resource['primary_resources'])){
131                                 foreach($resource['primary_resources'] as $uri=>$pri_resource_types){
132                                         $savant->assign('primary_resource_uri', $uri);
133                                         $savant->assign('primary_resources', $pri_resource_types);
134                                         //A file can be both original and alternative, and each could represent diff language
135                                         //Tried to resolve but the A4a v.2 only accept 1 language
136 //                                      $savant->assign('language_code', $pri_resource_types['language_code']);
137                                         //overwrite orig_access_mode                                    
138                                         $orig_access_mode = array(); //reinitialize
139                                         foreach($resource['resource_type'][$uri] as $type_id){
140                                                 $orig_access_mode[] = $this->getResourceNameById($type_id);
141                                                 $savant->assign('orig_access_mode', $orig_access_mode);
142                                                 $xml_array[$id.' to '.$uri][] = $savant->fetch(AT_INCLUDE_PATH.'../mods/_core/imsafa/classes/A4a.tmpl.php');
143                                         }
144                                         
145                                 }
146                         } else {
147                                 $savant->assign('primary_resource_uri', '');
148                                 $savant->assign('primary_resources', '');
149                                 $xml_array[$id] = $savant->fetch(AT_INCLUDE_PATH.'../mods/_core/imsafa/classes/A4a.tmpl.php');
150                         }
151                 }
152                 return $xml_array;
153         }
154
155         /**
156          * Get resource name by id
157          * @return      array
158          */
159         function getResourceNameById($type_id){
160                 $orig_access_mode = '';
161                 if (is_array($type_id)) {
162                         $type_id = $type_id[0];
163                 }
164                 switch($type_id){
165                         case 1:
166                                 $orig_access_mode = 'auditory';
167                                 break;
168                         case 3:
169                                 $orig_access_mode = 'textual';
170                                 break;
171                         case 2:
172                                 $orig_access_mode = 'sign_language';
173                                 break;
174                         case 4:
175                                 $orig_access_mode = 'visual';
176                                 break;
177                         default:
178                                 $orig_access_mode = '';
179                 }
180                 return $orig_access_mode;
181         }
182
183 }
184
185 ?>