71fe937e3e35c726f645b41f72d489e391c930b7
[acontent.git] / docs / include / classes / A4a / A4aImport.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
15 /**
16  * Accessforall Import  class.
17  * Based on the specification at: 
18  *              http://www.imsglobal.org/accessibility/index.html
19  *
20  * @date        Oct 9th, 2008
21  * @author      Harris Wong
22  */
23 class A4aImport extends A4a {
24         //Constructor
25         function A4aImport($cid){
26                 parent::A4a($cid);              //call its parent
27         }
28
29         /** 
30          * Import AccessForAll
31          * @param       array   XML items generated by the IMS import
32          */
33         function importA4a($items){
34                 //imported files, keep track of what file path that's been processed. Do not add repeated ones
35                 $imported_files = array();
36                 
37                 //use the items array data and insert it into the database.
38                 foreach ($items as $file_path => $a4a_resources){
39                         foreach ($a4a_resources as $resource){
40                                 //If it has adaptation/alternative, this is a primary file.
41                                 if (isset($resource['hasAdaptation']) && !empty($resource['hasAdaptation'])){
42                                         //we only have one language in the table, [1], [2], etc will be the same
43                                         $pri_lang = $resource['language'][0];   
44
45                                         //insert primary resource
46                                         $primary_id = $this->setPrimaryResource($this->cid, str_replace($this->relative_path, '', $file_path), $pri_lang);
47
48                                         //get primary resource type
49                                         $resources_attrs = $resource['access_stmt_originalAccessMode'];
50                                         
51                                         $attrs = $this->toResourceTypeId($resources_attrs);
52
53                                         //insert primary resource type associations
54                                         foreach ($attrs as $resource_type_id){
55                                                 $this->setPrimaryResourceType($primary_id, $resource_type_id);
56                                         }
57
58                                         //insert secondary resource
59                                         $secondary_resources = $resource['hasAdaptation'];      //uri array
60                                         foreach ($secondary_resources as $secondary_resource){
61                                                 //some paths will reference files above this directory, as a result
62                                                 //we will see ../, but since everything is under 'resources/', the relative_path
63                                                 //we can safely take it out.
64                                                 //@edited Dec 6th, imscc import uses relative paths, ims doesn't.
65                                                 if (substr($secondary_resource, 0, 7) == 'http://' || substr($secondary_resource, 0, 8) == 'https://') {
66                                                         $secondary_resource_with_relative_path = $secondary_resource;
67                                                 } else {
68                                                         $secondary_resource_with_relative_path = $this->relative_path.$secondary_resource;
69                                                 }
70
71                                                 $secondary_files = $items[$secondary_resource_with_relative_path];
72                                                 if (in_array($secondary_resource_with_relative_path, $imported_files)){
73                                                         continue;
74                                                 }
75                                                 $imported_files[] = $secondary_resource_with_relative_path;
76
77                                                 if (empty($secondary_files)){
78                                                     //tweak: if this is empty, then most likely it is an ims import.
79                                                     $secondary_resource = preg_replace('/^\.\.\//', '', $secondary_resource);
80                                                     $secondary_files = $items[$this->relative_path.$secondary_resource];
81                                                 }
82                                                 
83                                                 //check if this secondary file is the adaptation of 
84                                                 // this primary file 
85                                                 foreach($secondary_files as $secondary_file){
86                                                         //isAdaptation is 1-to-1 mapping, save to use [0]
87                                                         if (substr($secondary_file['isAdaptationOf'][0], 0, 7) == 'http://' 
88                                                            || substr($secondary_file['isAdaptationOf'][0], 0, 8) == 'https://') {
89                                                                 $adaption_with_relative_path = $secondary_file['isAdaptationOf'][0];
90                                                         } else {
91                                                                 $adaption_with_relative_path = $this->relative_path.$secondary_file['isAdaptationOf'][0];
92                                                         }
93                                                         
94                                                         if($adaption_with_relative_path == $file_path){
95                                                                 $secondary_lang = $secondary_file['language'][0];
96
97                                                                 //access_stmt_originalAccessMode cause we want the language for the secondary file.
98                                                                 $secondary_attr = $this->toResourceTypeId($secondary_file['access_stmt_originalAccessMode']);
99                                                                 $secondary_id = $this->setSecondaryResource($primary_id, $secondary_resource, $secondary_lang);
100
101                                                                 //insert secondary resource type associations
102                                                                 foreach ($secondary_attr as $secondary_resource_type_id){
103                                                                         $this->setSecondaryResourceType($secondary_id, $secondary_resource_type_id);
104                                                                 }
105                                                                 //break;        //1-to-1 mapping, no need to go further
106                                                         }
107                                                 }
108                                         } //foreach of secondary_resources
109                                         $imported_files = array(); //reset the imported file for the next resource 
110                                 }                               
111                         } //foreach of resources
112                 } //foreach of item array
113         }
114
115         /**
116          * By the given attrs array, decide which resource type it is
117          *      auditory                = type 1
118          *  sign_language       = type 2
119          *      textual                 = type 3
120          *      visual                  = type 4
121          * @param       array
122          * return type id array
123          */
124          function toResourceTypeId($resources_attrs){
125                  $result = array();
126
127                  //if empty
128                  if (empty($resources_attrs)){
129                          return $result;
130                  }
131                 if (is_array($resources_attrs)) {
132                                  if (in_array('auditory', $resources_attrs)){
133                                          $result[] = 1;
134                                  }
135                                  if (in_array('sign_language', $resources_attrs)){
136                                          $result[] = 2;
137                                  }
138                                  if (in_array('textual', $resources_attrs)){
139                                          $result[] = 3;
140                                  }
141                                  if (in_array('visual', $resources_attrs)){
142                                          $result[] = 4;
143                                  }              
144                 } else {
145                         if ($resources_attrs=='auditory'){
146                                 $result[] = 1;
147                         } elseif ($resources_attrs=='sign_language'){
148                                 $result[] = 2;
149                         } elseif ($resources_attrs=='textual'){
150                                 $result[] = 3;
151                         } elseif ($resources_attrs=='visual'){
152                                 $result[] = 4;
153                         }
154                 }
155                 return $result;
156         }
157 }
158
159 ?>