move code up one directory
[atutor.git] / mods / _core / imsafa / classes / A4a.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
16 /**
17  * Accessforall General class.
18  * Based on the specification at: 
19  *              http://www.imsglobal.org/accessibility/index.html
20  *
21  * @date        Oct 3rd, 2008
22  * @author      Harris Wong
23  */
24 class A4a {
25         //variables
26         var $cid = 0;                                           //content id
27         var $resource_types = array();          //resource types hash mapping
28         var $relative_path = '';                        //relative path to the file 
29
30         //Constructor
31         function A4a($cid){
32                 $this->cid = intval($cid);
33         }
34
35
36         // Return resources type hash mapping.
37         function getResourcesType($type_id=0){
38                 global $db;
39
40                 $type_id = intval($type_id);
41
42                 //if this is the first time calling this function, grab the list from db
43                 if (empty($resource_types)){
44                         $sql = 'SELECT * FROM '.TABLE_PREFIX.'resource_types';
45                         $result = mysql_query($sql, $db);
46                         while ($row = mysql_fetch_assoc($result)){
47                                 $this->resource_types[$row['type_id']] = $row['type'];
48                         }
49                 }
50
51                 if (!empty($this->resource_types[$type_id])){
52                         return $this->resource_types[$type_id];         
53                 }
54                 return $this->resource_types;
55         }
56
57         
58         // Get primary resources
59         function getPrimaryResources(){
60                 global $db;
61
62                 $pri_resources = array(); // cid=>[resource, language code]
63                 $sql = 'SELECT * FROM '.TABLE_PREFIX.'primary_resources WHERE content_id='.$this->cid.' ORDER BY primary_resource_id';
64                 $result = mysql_query($sql, $db);
65                 if (mysql_numrows($result) > 0){
66                         while ($row = mysql_fetch_assoc($result)){
67                                 $pri_resources[$row['primary_resource_id']]['resource'] = $row['resource'];
68                                 if ($row['language_code'] != ''){
69                                         $pri_resources[$row['primary_resource_id']]['language_code'] = $row['language_code'];
70                                 }
71                         }
72                 }
73                 return $pri_resources;
74         }
75
76
77         // Get primary resources by resource name
78         function getPrimaryResourceByName($primary_resource){
79                 global $db;
80
81                 $sql = "SELECT * FROM ".TABLE_PREFIX."primary_resources 
82                         WHERE content_id=".$this->cid."
83                           AND language_code = '".$_SESSION['lang']."'
84                           AND resource='".$primary_resource."'";
85                 $result = mysql_query($sql, $db);
86                 if (mysql_numrows($result) > 0){
87                         return mysql_fetch_assoc($result);
88                 } else {
89                         return false;
90                 }
91         }
92
93
94         // Get primary resources types
95         function getPrimaryResourcesTypes($pri_resource_id=0){
96                 global $db;
97
98                 $pri_resource_id = intval($pri_resource_id);
99
100                 //quit if id not specified
101                 if ($pri_resource_id == 0) {
102                         return array();
103                 }
104
105                 $pri_resources_types = array(); // cid=>[type id]+
106                 $sql = 'SELECT * FROM '.TABLE_PREFIX.'primary_resources_types WHERE primary_resource_id='.$pri_resource_id;
107                 $result = mysql_query($sql, $db);
108                 if ($result){
109                         while ($row = mysql_fetch_assoc($result)){
110                                 $pri_resources_types[$pri_resource_id][] = $row['type_id'];
111                         }
112                 }
113                 return $pri_resources_types;
114         }
115
116
117         // Get secondary resources 
118         function getSecondaryResources($pri_resource_id=0){
119                 global $db;
120
121                 $pri_resource_id = intval($pri_resource_id);
122
123                 //quit if id not specified
124                 if ($pri_resource_id == 0) {
125                         return array();
126                 }
127
128                 $sec_resources = array(); // cid=>[resource, language code]
129                 $sql = 'SELECT * FROM '.TABLE_PREFIX.'secondary_resources WHERE primary_resource_id='.$pri_resource_id;
130                 $result = mysql_query($sql, $db);
131                 if ($result){
132                         while ($row = mysql_fetch_assoc($result)){
133                                 $sec_resources[$row['secondary_resource_id']]['resource'] = $row['secondary_resource'];
134                                 if ($row['language_code'] != ''){
135                                         $sec_resources[$row['secondary_resource_id']]['language_code'] = $row['language_code'];
136                                 }
137                         }
138                 }
139                 return $sec_resources;
140         }
141
142
143         // Get secondary resources types
144         function getSecondaryResourcesTypes($sec_resource_id=0){
145                 global $db;
146
147                 $sec_resource_id = intval($sec_resource_id);
148
149                 //quit if id not specified
150                 if ($sec_resource_id == 0) {
151                         return array();
152                 }
153
154                 $sec_resources_types = array(); // cid=>[type id]+
155                 $sql = 'SELECT * FROM '.TABLE_PREFIX.'secondary_resources_types WHERE secondary_resource_id='.$sec_resource_id;
156                 $result = mysql_query($sql, $db);
157                 if ($result){
158                         while ($row = mysql_fetch_assoc($result)){
159                                 $sec_resources_types[] = $row['type_id'];
160                         }
161                 }
162                 return $sec_resources_types;
163         }
164
165
166         // Insert primary resources into the db
167         // @return primary resource id.
168         function setPrimaryResource($content_id, $file_name, $lang){
169                 global $addslashes, $db; 
170
171                 $content_id = intval($content_id);
172                 $file_name = $addslashes(convert_amp($file_name));
173                 $lang = $addslashes($lang);
174
175                 $sql = "INSERT INTO ".TABLE_PREFIX."primary_resources SET content_id=$content_id, resource='$file_name', language_code='$lang'";
176                 $result = mysql_query($sql, $db);
177                 if ($result){
178                         return mysql_insert_id();
179                 }
180                 return false;
181         }
182
183         // Insert primary resource type
184         function setPrimaryResourceType($primary_resource_id, $type_id){
185                 global $db; 
186
187                 $primary_resource_id= intval($primary_resource_id);
188                 $type_id = intval($type_id);
189
190                 $sql = "INSERT INTO ".TABLE_PREFIX."primary_resources_types SET primary_resource_id=$primary_resource_id, type_id=$type_id";
191                 $result = mysql_query($sql, $db);
192         }
193
194         // Insert secondary resource
195         // @return secondary resource id
196         function setSecondaryResource($primary_resource_id, $file_name, $lang){
197                 global $addslashes, $db; 
198
199                 $primary_resource_id = intval($primary_resource_id);
200                 $file_name = $addslashes(convert_amp($file_name));
201                 $lang = $addslashes($lang);
202
203                 $sql = "INSERT INTO ".TABLE_PREFIX."secondary_resources SET primary_resource_id=$primary_resource_id, secondary_resource='$file_name', language_code='$lang'";
204                 $result = mysql_query($sql, $db);
205                 if ($result){
206                         return mysql_insert_id();
207                 }
208                 return false;
209         }
210
211         // Insert secondary resource
212         function setSecondaryResourceType($secondary_resource, $type_id){
213                 global $db;
214
215                 $secondary_resource = intval($secondary_resource);
216                 $type_id = intval($type_id);
217
218                 $sql = "INSERT INTO ".TABLE_PREFIX."secondary_resources_types SET secondary_resource_id=$secondary_resource, type_id=$type_id";
219                 $result = mysql_query($sql, $db);
220         }
221
222         
223         // Set the relative path to all files
224         function setRelativePath($path){
225                 $this->relative_path = $path . '/';
226         }
227
228
229     /**
230      * Delete this primary resource and all its associated secondary resources
231      * @param   int     primary resournce id
232      */
233     function deletePrimaryResource($primary_rid){
234         global $db;
235         // Delete all secondary a4a
236         $sql = 'DELETE c, d FROM '.TABLE_PREFIX.'secondary_resources c LEFT JOIN '.TABLE_PREFIX."secondary_resources_types d ON c.secondary_resource_id=d.secondary_resource_id WHERE primary_resource_id=$primary_rid";
237         $result = mysql_query($sql, $db);
238         
239         // If successful, remove all primary resources
240         if ($result){
241             $sql = 'DELETE a, b FROM '.TABLE_PREFIX.'primary_resources a LEFT JOIN '.TABLE_PREFIX."primary_resources_types b ON a.primary_resource_id=b.primary_resource_id WHERE a.primary_resource_id=$primary_rid";
242             mysql_query($sql, $db);
243         }
244     }
245
246         // Delete all materials associated with this content
247         function deleteA4a(){
248                 global $db; 
249
250                 $pri_resource_ids = array();
251
252                 // Get all primary resources ID out that're associated with this content
253                 $sql = 'SELECT a.primary_resource_id FROM '.TABLE_PREFIX.'primary_resources a LEFT JOIN '.TABLE_PREFIX.'primary_resources_types b ON a.primary_resource_id=b.primary_resource_id WHERE content_id='.$this->cid;
254                 $result = mysql_query($sql);
255
256                 while($row=mysql_fetch_assoc($result)){
257                         $pri_resource_ids[] = $row['primary_resource_id'];
258                 }
259
260                 //If the are primary resources found
261                 if (!empty($pri_resource_ids)){
262                         $glued_pri_ids = implode(",", $pri_resource_ids);
263
264                         // Delete all secondary a4a
265                         $sql = 'DELETE c, d FROM '.TABLE_PREFIX.'secondary_resources c LEFT JOIN '.TABLE_PREFIX.'secondary_resources_types d ON c.secondary_resource_id=d.secondary_resource_id WHERE primary_resource_id IN ('.$glued_pri_ids.')';
266                         $result = mysql_query($sql);
267
268                         // If successful, remove all primary resources
269                         if ($result){
270                                 $sql = 'DELETE a, b FROM '.TABLE_PREFIX.'primary_resources a LEFT JOIN '.TABLE_PREFIX.'primary_resources_types b ON a.primary_resource_id=b.primary_resource_id WHERE content_id='.$this->cid;
271                                 mysql_query($sql);
272                         }
273                 }
274         }
275 }
276
277 ?>