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