changed git call from https to git readonly
[atutor.git] / mods / atutor_opencaps / opencaps / conversion_service / include / classes / core_ConversionManager_class.php
1 <?php\r
2 /**\r
3  * Name: ConversionManager class\r
4  * Author: Antonio Gamba Bari - antonio.gambabari@utoronto.ca. ARTC, University of Toronto.\r
5  * Last Update: July 22, 2009\r
6  * \r
7  * Description: This class performs all the conversion functions\r
8  * 1. Start a conversion\r
9  * 2. Retrieve a caption file\r
10  * 3. Load all caption formats inc files\r
11  * 3. Autodetect caption format\r
12  * 4. Build a Collection of Captions\r
13  * 5. Call export method from the proper CaptionFormat sub-class \r
14  */\r
15 \r
16 class ConversionManager\r
17 {\r
18         private $ccStringTarget; // Caption Target file as String\r
19         private $ccSourceFileName; // File Name of the Source caption \r
20         private $ccTargetExt; // File Extension of the caption file  \r
21         private $ccTypeSource; // Class name of the source caption format\r
22         private $ccTypeTarget; // Class name of the target caption format\r
23         private $ccFormats = Array(); // array containing all the class names of the supported caption formats\r
24         private $ccTargetUrl; // relative path to target caption file\r
25         public $ccImportedCollection; // The target CaptionCollection Object\r
26         \r
27         \r
28         /**\r
29          * Class Constructor: Receives a caption string, parameters and returns the target caption as string\r
30          *\r
31          * @param String $theCaption The Caption File as String\r
32          * @param String $theTarget The target Type = Class name of target sub-class\r
33          * @param String $theFileName The name of the source caption file \r
34          */\r
35         public function __construct($theCaption,$theTarget,$theFileName)\r
36         {\r
37                 global $rosettaCCSettings;\r
38                 /*\r
39                  * start conversion \r
40                  */\r
41                 // assign parameters to class members \r
42                 $this->ccTypeTarget = $theTarget;\r
43                 $this->ccSourceFileName = $theFileName;\r
44                 $this->ccTypeSource = ''; // initialize $ccTypeSource = ''\r
45                 \r
46                 // load caption formats \r
47                 $this->ccFormats = CcUtilVital::ccFormatsLoad();\r
48 \r
49                 /*\r
50                  * run detection function on imported caption string\r
51                  * Note: the type of the source caption is not a parametter. This means that for stability \r
52                  * purposes the conversion tool MUST identify the format. Otherwhise, many unexpected and \r
53                  * incorrect conversions may occur   \r
54                  */ \r
55                 $this->ccTypeSource = $this->_ccAutoDetect($theCaption);                \r
56                         \r
57                 // proceed only if caption format is detected  \r
58                 if($this->ccTypeSource!='')\r
59                 {\r
60                                 //echo '<br/><br/> Caption format Detected: <b>'.$this->ccTypeSource.'</b>';\r
61 \r
62                         /*\r
63                          * Invoques import function in caption format subclass\r
64                          * Note: here we know the name of the Caption format class from the $this->ccTypeSource (the value assigned by the autodetection function) \r
65                          */\r
66                         $ccSourceObj = new $this->ccTypeSource();\r
67                         \r
68                         // get a CaptionCollection Object \r
69                         $this->ccImportedCollection = $ccSourceObj->importCC($theCaption); \r
70                         \r
71                                 //echo '<br/><br/>Uploaded Caption file was loaded into a CaptionCollection';\r
72                         \r
73                         // Initialize exported collection string\r
74                         $this->ccStringTarget = '';\r
75                         \r
76                         // run export function\r
77                         $this->ccStringTarget = $this->_ccExport();\r
78                         \r
79                         // print target format in a form for debuging purposes only\r
80                                 //echo '<form><textarea name="textarea" cols="120" rows="10">'. $this->ccStringTarget . '</textarea></form>';\r
81                         \r
82                         //$this->_ccExport();\r
83                         \r
84                         // verify if the Target format has been provided \r
85                         if ($this->ccStringTarget!='')\r
86                         {\r
87                                 // Save collection in DB as serialized string\r
88                                         //DbTools::collectionInBdSave($this->ccImportedCollection, $theFileName);\r
89                                 \r
90                                 // FINALLY !! if all goes right save exported caption into a file \r
91                                 $theConvertedFile = $this->_saveExported();\r
92                                 // display player test option\r
93                                         //PlayerTools::displayPlayerOp($theConvertedFile);\r
94                                 \r
95                                 // force downlaod: only when no comments or debuging mode \r
96                                                 //TxtFileTools::downloadFile($theConvertedFile);\r
97                                 \r
98                                 // set the relative path of the target caption file\r
99                                 $this->ccTargetUrl = $theConvertedFile;\r
100 \r
101                         }\r
102 \r
103                         \r
104                 } else {\r
105                         echo 'The format of source Caption was not recognized.';\r
106                 };\r
107                 \r
108                 // return $ccSourceFormat\r
109                                 \r
110         }// end __construct() \r
111 \r
112         public function getCcTargetUrl()\r
113         {\r
114                 return $this->ccTargetUrl;              \r
115         }\r
116         \r
117         /**\r
118          * Gets the file name without extension of a file\r
119          *\r
120          * @param String $theFilename Name of the file\r
121          * @return String $targetFileName\r
122          */\r
123         private function _buildExportFileName()\r
124         {\r
125                 $targetFileName = substr($this->ccSourceFileName, 0, -3);\r
126                 return $targetFileName;\r
127         }\r
128         \r
129         private function _saveExported()\r
130         {\r
131                         //global $rosettaCCSettings;\r
132                 // build the proper name to save converted caption\r
133                 $theCCname = $this->_buildExportFileName().$this->ccTargetExt;\r
134                 \r
135                         //$fullFileUrl = $rosettaCCSettings['uploadDir'].'/'.$theCCname;\r
136                  \r
137                 // save exported collection in a file \r
138                 TxtFileTools::stringToFile($theCCname,$this->ccStringTarget);\r
139 \r
140                 // return the generated file name\r
141                 return $theCCname;\r
142                 \r
143                 // download the generated caption file \r
144                 //CcUtil::downloadFile($fullFileUrl);\r
145                                   \r
146         }\r
147 \r
148         /**\r
149          * Gets a CaptionCollection object and return the target caption file as string\r
150          *\r
151          * @return unknown\r
152          */\r
153         private function _ccExport()\r
154         {\r
155                 $ccTargetString = '';\r
156 \r
157                 //echo '<br/><br/>...creating an object of the target format: '.$this->ccTypeTarget;\r
158                 //echo '<br/><br/>I AM a '.$ccObjExp->getName().' Object';\r
159 \r
160                 if ($this->ccTypeTarget!='0')\r
161                 {\r
162                         // create an instance of the target Caption Format\r
163                         $ccObjExp = new $this->ccTypeTarget();\r
164                         \r
165                         // get the extension of the target Caption\r
166                         $this->ccTargetExt = $ccObjExp->getFileExtension();\r
167                         //echo '<br/>The extension of the Target Caption is: '. $this->ccTargetExt; \r
168                         \r
169                         // call export method in caption format instance\r
170                         $ccTargetString = $ccObjExp->exportCC($this->ccImportedCollection);\r
171 \r
172                 } // end if\r
173                 \r
174                 //echo '<br/>Target Collection: <br/>'.$ccTargetString;\r
175                 return $ccTargetString;\r
176         } // end _ccExport()\r
177         \r
178         /**\r
179          * Import caption as a sting and return a Caption Collection\r
180          * @param String $theCaption Caption file as a String    \r
181          * @param String $theCcFormatClassName The class name of the caption source\r
182          * @return CaptionCollection $importedCollection Return a Caption Collection Object  \r
183          */\r
184         private function _ccImport($theCaption,$theCcFormatClassName)\r
185         {\r
186                 // Create an instance of the source Caption Format\r
187                 $myCcSourceObj = new $theCcFormatClassName();\r
188                 \r
189                 // import the caption into a CaptionCollection Object\r
190                 return $myCcSourceObj->importCC($theCaption);\r
191                 \r
192         } // _ccImport() end\r
193 \r
194         /**\r
195          * Auto detect format of a caption string using the unique pattern provided by each caption format \r
196          * @param String $theCaption Caption file as a String    \r
197          * @return String $formatfound Return detected caption format or '' if not found  \r
198          */\r
199         private function _ccAutoDetect($theCaption)\r
200         {\r
201                 //echo '<br/>Total Formats to Auto-detect: '.count($this->ccFormats);\r
202                 \r
203                 $formatfound = '';\r
204                 \r
205                 // instanciate each caption format sub-class and call getIdPattern() \r
206                 foreach ($this->ccFormats as $ccId)\r
207                 { \r
208                         // testing here a polymorphic behaviour \r
209                                 //echo '<br/>.....Detecting format: '.$ccId;\r
210                         $theCcIdPattern = '';\r
211                         $ccObj = new $ccId(); // Create an instance of a caption format\r
212                         $theCcIdPattern = $ccObj->getIdPattern(); // get the pattern identifying the caption format\r
213                         //echo '<br/>***** '.$ccId.' = '.$theCcIdPattern;\r
214                         \r
215                         // look for the pattern in the caption string\r
216                         if (preg_match($theCcIdPattern,$theCaption)==1)\r
217                         {\r
218                                         //echo '<br/>!! Caption Format Dettected!! ***** '.$ccId.' = '.$theCcIdPattern; \r
219                                         //echo '<br/>Caption Format Dettected: <b>'.$ccId.'</b>';\r
220                                 \r
221                                 // set the detected format\r
222                                 $formatfound = $ccId;\r
223                                 \r
224                                 return $formatfound;\r
225                                 break; // stop detecting caption format\r
226                         }\r
227 \r
228                 } // foreach end\r
229 \r
230                 return $formatfound;\r
231                 \r
232         } // _ccAutoDetect() end\r
233         \r
234         public function getCaptionFormats()\r
235         {\r
236                 return $this->ccFormats;\r
237         }\r
238         \r
239         public function getImportedCollection()\r
240         {\r
241                 return $this->ccImportedCollection;\r
242         }\r
243 \r
244         \r
245 } // class end \r
246 ?>