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