changed git call from https to git readonly
[atutor.git] / mods / atutor_opencaps / include / classes / ATOC_ProjectManager.php
1 <?php\r
2 /****************************************************************/\r
3 /* Atutor-OpenCaps Module                                               \r
4 /****************************************************************/\r
5 /* Copyright (c) 2010                           \r
6 /* Written by Antonio Gamba                                             \r
7 /* Adaptive Technology Resource Centre / University of Toronto\r
8 /*\r
9 /* This program is free software. You can redistribute it and/or\r
10 /* modify it under the terms of the GNU General Public License\r
11 /* as published by the Free Software Foundation.\r
12 /****************************************************************/\r
13 \r
14 class ATOCProjectManager\r
15 {\r
16         /*\r
17         private $id;\r
18         private $login;\r
19         private $courseId;\r
20         private $name;\r
21         private $mediaFile;\r
22         private $captionFile;\r
23         private $date;\r
24         */\r
25         \r
26         /**\r
27          * @desc Load Projects\r
28          * @param String $theLogin\r
29          * @param int $theCourseId\r
30          * @param int $theId\r
31          * @return Array $myProjects\r
32          */\r
33         public function _loadProjects($theLogin, $theCourseId, $theId)\r
34         {\r
35                 //echo '<h3>START method: _loadProjects()</h3>';\r
36 \r
37                 global $db;\r
38                 \r
39                 $myProjects = array();\r
40                 $myGetMediaObj = new OcJsonFileProject();\r
41                 \r
42                 $sql = "SELECT * FROM ".TABLE_PREFIX."atopencaps_mod WHERE login = '".$theLogin."' AND courseId = ".$theCourseId;\r
43                 $sqlWhere = '';\r
44 \r
45                 if($theId!=0)\r
46                 {\r
47                         $sqlWhere .= ' AND id = '.$theId;\r
48                 } \r
49                 \r
50                 $sqlOrder = ' ORDER BY id DESC';\r
51                 $sql .= $sqlWhere.$sqlOrder;\r
52                         //echo "<p>".$sql.'</p>';\r
53 \r
54                 $result = mysql_query($sql, $db);\r
55                 $num_affected = mysql_affected_rows($db);\r
56                 \r
57                 if ($ocAtSettings['debugMode'] && $num_affected==0)\r
58                 {\r
59                         $ocAtSettings['messages'] = '<p>_loadProjects(): no data to load </p>';\r
60                 }\r
61                 \r
62                 // get all project data into an array. \r
63                 $myCounter = 0;\r
64                 while ($row = @mysql_fetch_assoc($result)) \r
65                 {\r
66                         //$existing_accounts[$row['public_field']];\r
67                         $myProjects[$myCounter]['id'] = $row['id'];\r
68                         $myProjects[$myCounter]['login'] = $row['login'];\r
69                         $myProjects[$myCounter]['courseId'] = $row['courseId'];\r
70                         $myProjects[$myCounter]['name'] = $row['name'];\r
71                         $myProjects[$myCounter]['mediaFile'] = $row['mediaFile'];\r
72                         $myProjects[$myCounter]['captionFile'] = $row['captionFile'];\r
73                         $myProjects[$myCounter]['timeStamp'] = $row['timeStamp'];\r
74                         $myProjects[$myCounter]['width'] = $row['width'];\r
75                         $myProjects[$myCounter]['height'] = $row['height'];\r
76                         \r
77                         $myCounter++;\r
78                         //echo "<BR/>ID: ".$row['id'];\r
79                 } // end while\r
80 \r
81                 return $myProjects;\r
82 \r
83         } // _loadProjects()\r
84 \r
85 \r
86         /**\r
87          * @desc _createCcFile\r
88          * @param String $theMediaFile\r
89          * @return String $theCaptionFile\r
90          */\r
91         public function _createCcFile($theMediaFile)\r
92         {\r
93                 global $ocAtSettings; //\r
94 \r
95                 $theNewFileName = '';\r
96                 $bol=false;\r
97                 $theCaptionFile = '';\r
98                 $theCaptionFilePath = '';\r
99                 $theCaptionFilePathFull = '';\r
100                 $i = 0;\r
101                 \r
102                 while (!$bol)\r
103                 {\r
104                         $ccNameTemp = explode('.',$theMediaFile);\r
105                         //print_r($ccNameTemp);\r
106                         if ($i==0)\r
107                         {\r
108                                 $theCaptionFile = $ccNameTemp[0].'.'.$ocAtSettings['defaultCcExt'];\r
109                         } else {\r
110                                 $theCaptionFile = $ccNameTemp[0].'_'.$i.'.'.$ocAtSettings['defaultCcExt'];\r
111                         }\r
112 \r
113                         // fix path\r
114                         $theCaptionFilePath = str_replace('/',$ocAtSettings['dirSep'],$theCaptionFile);\r
115         \r
116                         $theCaptionFilePathFull = AT_CONTENT_DIR.$_SESSION['course_id'].$ocAtSettings['dirSep'].$theCaptionFilePath;\r
117                         \r
118                         if (!file_exists($theCaptionFilePathFull))\r
119                         {\r
120                                 $bol=true;\r
121                         }\r
122                         $i++;\r
123                         \r
124                 } // end while\r
125         \r
126                 file_put_contents($theCaptionFilePathFull, '');\r
127                 return $theCaptionFile;\r
128 \r
129         } // end createNewCcFile\r
130         \r
131 \r
132         /**\r
133          * @desc Set Active Project\r
134          * @param int $theId\r
135          * @param string $theLogin\r
136          * @param int $theCourseId\r
137          * @param string $theSessionId\r
138          */\r
139         public function _setActiveProject($theId, $theLogin, $theCourseId, $theSessionId)\r
140         {\r
141                 global $db, $ocAtSettings; // load AT db object\r
142                 \r
143                 // split id \r
144                 $temId = explode('-',$theId);\r
145                 \r
146                 if($theId!=0 && $theLogin!='' && $theCourseId!=0 && $theSessionId!='')\r
147                 {\r
148                         $sql = "UPDATE ".TABLE_PREFIX."atopencaps_mod SET sessionId='$theSessionId'";\r
149                         $sql .= " WHERE id = $theId AND login='$theLogin' AND courseId=$theCourseId";\r
150                         $result = mysql_query($sql, $db);\r
151                         $num_affected = mysql_affected_rows($db);\r
152                                 //echo '<br/>'.$sql;\r
153                         $result = null;\r
154                 }\r
155                 \r
156         } // end _setActiveProject\r
157         \r
158         \r
159         /**\r
160          * @desc Add/Edit/Delete captioning Project.\r
161          * @param int $theId\r
162          * @param string $theLogin\r
163          * @param int $theCourseId\r
164          * @param string $theName\r
165          * @param string $theMediaFile\r
166          * @param string $theCaptionFile\r
167          * @param int $theWidth\r
168          * @param int $theHeight\r
169          * @param int $theAction\r
170          */\r
171         public function _addEditProject($theId, $theLogin, $theCourseId, $theName, $theMediaFile, $theCaptionFile, $theWidth, $theHeight, $theAction='')\r
172         {\r
173 \r
174                 global $db, $ocAtSettings; // load AT db object\r
175                 \r
176                 if ($theAction=="deleteProject")\r
177                 {\r
178                         $sql = "DELETE FROM ".TABLE_PREFIX."atopencaps_mod";\r
179                         $sql .= " WHERE id = $theId AND login='$theLogin'";\r
180                 \r
181                 } else if ($theId != 0)\r
182                 {\r
183                         // UPDATE\r
184                         $sql = "UPDATE ".TABLE_PREFIX."atopencaps_mod SET name='$theName', mediaFile='$theMediaFile', captionFile='$theCaptionFile', width='".$theWidth."', height='".$theHeight."'";\r
185                         $sql .= " WHERE id = $theId AND login='$theLogin'";\r
186 \r
187                 } else {\r
188                         \r
189                         // create caption file if does not exist\r
190                         if($theCaptionFile=='')\r
191                         {\r
192                                 $theCaptionFile = $this->_createCcFile($theMediaFile);\r
193                         }\r
194 \r
195                         // INSERT\r
196                         $sql = "INSERT INTO ".TABLE_PREFIX."atopencaps_mod (`login`, `courseId`, `name`, `mediaFile`, `captionFile`, `width`, `height`) VALUES";\r
197                         $sql .= "('".$theLogin."', ".$theCourseId.", '".$theName."', '".$theMediaFile."', '".$theCaptionFile."', '".$theWidth."', '".$theHeight."')";\r
198                                 \r
199                 }\r
200                 \r
201                         //echo $sql;\r
202                 // run SQL \r
203                 $result = mysql_query($sql, $db);\r
204                 $num_affected = mysql_affected_rows($db);\r
205                 $result = null;\r
206                 \r
207         } // end addProject()\r
208         \r
209         /**\r
210          * @desc _getProjecDataJson\r
211          * @param unknown_type $theId\r
212          * @param unknown_type $theBaseUrl\r
213          * @return unknown\r
214          */\r
215         public function _getProjecDataJson($theId, $theBaseUrl)\r
216         \r
217         {\r
218                 global $db, $ocAtSettings;\r
219                 \r
220                 // split id \r
221                 $temId = explode('-',$theId);\r
222         \r
223                 $myProjects = array();\r
224                 $myGetMediaObj = new OcJsonFileProject();\r
225                 \r
226                 $sql = "SELECT * FROM ".TABLE_PREFIX."atopencaps_mod WHERE id = $temId[0] AND sessionId ='$temId[1]'";\r
227         \r
228                 $result = mysql_query($sql, $db);\r
229                 $num_affected = mysql_affected_rows($db);\r
230                 \r
231                 // debug\r
232                 if ($ocAtSettings['debugMode'] && $num_affected==0)\r
233                 {\r
234                         echo "<p>_getProjecDataJson() function reported that: there are no Captioning Projects matching the criteria \r
235                         <br/>SQL: ".$sql.'</p>';\r
236                 }\r
237                 \r
238                 // get all project data into an array. \r
239                 $myCounter = 0;\r
240                 while ($row = mysql_fetch_assoc($result)) \r
241                 {\r
242                                 // build object to JSON\r
243                                 $myGetMediaObj->setVars('id',$theId);\r
244                                 $myGetMediaObj->setVars('login',$row['login']);\r
245                                 $myGetMediaObj->setVars('title',$row['name']);\r
246                                 \r
247                                 $rex1 = '/(http:|https:)/i';\r
248                                 \r
249                                 if (preg_match($rex1, $row['mediaFile']))\r
250                                 {\r
251                                         $myGetMediaObj->setVars('mediaFile',''.$row['mediaFile']);\r
252                                         $myGetMediaObj->setVars('captionFile',$theBaseUrl.'content/'.$row['courseId'].'/'.$row['captionFile']);\r
253                                         \r
254                                 } else if ($ocAtSettings['contentUrlType']== 0) {\r
255                                         $myGetMediaObj->setVars('mediaFile',$theBaseUrl.'get.php/'.''.$row['mediaFile']);\r
256                                         $myGetMediaObj->setVars('captionFile',$theBaseUrl.'get.php/'.''.$row['captionFile']);\r
257                                         \r
258                                 } else if ($ocAtSettings['contentUrlType']== 1) {\r
259                                         $myGetMediaObj->setVars('mediaFile',$theBaseUrl.'content/'.$row['courseId'].'/'.$row['mediaFile']);\r
260                                         $myGetMediaObj->setVars('captionFile',$theBaseUrl.'content/'.$row['courseId'].'/'.$row['captionFile']);\r
261                                         \r
262                                 }\r
263                                 \r
264                                 $myGetMediaObj->setVars('timeStamp',$row['timeStamp']);\r
265                                 $myGetMediaObj->setVars('returnFormat',$ocAtSettings['ccReturnFormat']);\r
266                                 \r
267                 } // end while\r
268                 \r
269                 $jsonFile = json_encode($myGetMediaObj);\r
270                 \r
271                 return $jsonFile;\r
272 \r
273 \r
274         } // _getProjecDataJason()\r
275         \r
276         \r
277         /**\r
278          * @desc _saveCaptionData\r
279          * @param String $theId\r
280          * @param String $theCcData\r
281          * @param int $theWidth\r
282          * @param int $theHeight\r
283          */\r
284         public function _saveCaptionData($theId,$theCcData,$theWidth,$theHeight)\r
285         {\r
286                 global $db, $ocAtSettings;\r
287                 \r
288                 $temId = explode('-',$theId);\r
289                 \r
290                 $sql = "SELECT * FROM ".TABLE_PREFIX."atopencaps_mod WHERE id = $temId[0] AND sessionId ='$temId[1]'";\r
291                 \r
292                 $result = mysql_query($sql, $db);\r
293                 $num_affected = mysql_affected_rows($db);\r
294                 \r
295                 $row = mysql_fetch_assoc($result);\r
296 \r
297                 if ($row['captionFile']!='')\r
298                 {\r
299                         // fix path\r
300                         $theCaptionFilePath = str_replace('/',$ocAtSettings['dirSep'],$row['captionFile']);\r
301                         \r
302                         $theCaptionFilePathFull = AT_CONTENT_DIR.$row['courseId'].$ocAtSettings['dirSep'].$theCaptionFilePath;\r
303                         \r
304                         file_put_contents($theCaptionFilePathFull, $theCcData);\r
305 \r
306                         // update metadata\r
307                         if ($theWidth!='' && $theHeight!='')\r
308                         {\r
309                                 $sqlUpdateMetadata = "UPDATE ".TABLE_PREFIX."atopencaps_mod SET width='".$theWidth."', height='".$theHeight."'";\r
310                                 $sqlUpdateMetadata .=  " WHERE id = $temId[0] AND sessionId ='$temId[1]'";\r
311                                 //$sqlUpdateMetadata .=  " AND login='$theLogin'";\r
312                                 //echo '<br/>'.$sqlUpdateMetadata;\r
313                                 $result1 = mysql_query($sqlUpdateMetadata, $db);\r
314                                 $result1 = null;\r
315                         }\r
316                         $result = null;\r
317                 \r
318                         //return  '<h3>Data Saved in : '.$row['captionFile'].'</h3>';\r
319                         return '<p>Data Saved in : '.$theCaptionFilePathFull.'</p>';\r
320                 }\r
321 \r
322         } // _saveCaptionData()\r
323         \r
324         \r
325 } // end class\r
326 ?>