made a copy
[atutor.git] / include / classes / A4a / A4a.class.php
1 <?php
2 /********************************************************************/
3 /* ATutor                                                                                                                       */
4 /********************************************************************/
5 /* Copyright (c) 2002-2008 by Greg Gay, Cindy Qi Li, & Harris Wong      */
6 /* Adaptive Technology Resource Centre / University of Toronto          */
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: A4a.class.php 8077 2008-10-23 18:32:31Z hwong $
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;
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 types
78         function getPrimaryResourcesTypes($pri_resource_id=0){
79                 global $db;
80
81                 $pri_resource_id = intval($pri_resource_id);
82
83                 //quit if id not specified
84                 if ($pri_resource_id == 0) {
85                         return array();
86                 }
87
88                 $pri_resources_types = array(); // cid=>[type id]+
89                 $sql = 'SELECT * FROM '.TABLE_PREFIX.'primary_resources_types WHERE primary_resource_id='.$pri_resource_id;
90                 $result = mysql_query($sql, $db);
91                 if ($result){
92                         while ($row = mysql_fetch_assoc($result)){
93                                 $pri_resources_types[$pri_resource_id][] = $row['type_id'];
94                         }
95                 }
96                 return $pri_resources_types;
97         }
98
99
100         // Get secondary resources 
101         function getSecondaryResources($pri_resource_id=0){
102                 global $db;
103
104                 $pri_resource_id = intval($pri_resource_id);
105
106                 //quit if id not specified
107                 if ($pri_resource_id == 0) {
108                         return array();
109                 }
110
111                 $sec_resources = array(); // cid=>[resource, language code]
112                 $sql = 'SELECT * FROM '.TABLE_PREFIX.'secondary_resources WHERE primary_resource_id='.$pri_resource_id;
113                 $result = mysql_query($sql, $db);
114                 if ($result){
115                         while ($row = mysql_fetch_assoc($result)){
116                                 $sec_resources[$row['secondary_resource_id']]['resource'] = $row['secondary_resource'];
117                                 if ($row['language_code'] != ''){
118                                         $sec_resources[$row['secondary_resource_id']]['language_code'] = $row['language_code'];
119                                 }
120                         }
121                 }
122                 return $sec_resources;
123         }
124
125
126         // Get secondary resources types
127         function getSecondaryResourcesTypes($sec_resource_id=0){
128                 global $db;
129
130                 $sec_resource_id = intval($sec_resource_id);
131
132                 //quit if id not specified
133                 if ($sec_resource_id == 0) {
134                         return array();
135                 }
136
137                 $sec_resources_types = array(); // cid=>[type id]+
138                 $sql = 'SELECT * FROM '.TABLE_PREFIX.'secondary_resources_types WHERE secondary_resource_id='.$sec_resource_id;
139                 $result = mysql_query($sql, $db);
140                 if ($result){
141                         while ($row = mysql_fetch_assoc($result)){
142                                 $sec_resources_types[] = $row['type_id'];
143                         }
144                 }
145                 return $sec_resources_types;
146         }
147
148
149         // Insert primary resources into the db
150         // @return primary resource id.
151         function setPrimaryResource($content_id, $file_name, $lang){
152                 global $addslashes, $db; 
153
154                 $content_id = intval($content_id);
155                 $file_name = $addslashes($file_name);
156                 $lang = $addslashes($lang);
157
158                 $sql = "INSERT INTO ".TABLE_PREFIX."primary_resources SET content_id=$content_id, resource='$file_name', language_code='$lang'";
159                 $result = mysql_query($sql, $db);
160                 if ($result){
161                         return mysql_insert_id();
162                 }
163                 return false;
164         }
165
166         // Insert primary resource type
167         function setPrimaryResourceType($primary_resource_id, $type_id){
168                 global $db; 
169
170                 $primary_resource_id= intval($primary_resource_id);
171                 $type_id = intval($type_id);
172
173                 $sql = "INSERT INTO ".TABLE_PREFIX."primary_resources_types SET primary_resource_id=$primary_resource_id, type_id=$type_id";
174                 $result = mysql_query($sql, $db);
175         }
176
177         // Insert secondary resource
178         // @return secondary resource id
179         function setSecondaryResource($primary_resource_id, $file_name, $lang){
180                 global $addslashes, $db; 
181
182                 $primary_resource_id = intval($primary_resource_id);
183                 $file_name = $addslashes($file_name);
184                 $lang = $addslashes($lang);
185
186                 $sql = "INSERT INTO ".TABLE_PREFIX."secondary_resources SET primary_resource_id=$primary_resource_id, secondary_resource='$file_name', language_code='$lang'";
187                 $result = mysql_query($sql, $db);
188                 if ($result){
189                         return mysql_insert_id();
190                 }
191                 return false;
192         }
193
194         // Insert secondary resource
195         function setSecondaryResourceType($secondary_resource, $type_id){
196                 global $db;
197
198                 $secondary_resource = intval($secondary_resource);
199                 $type_id = intval($type_id);
200
201                 $sql = "INSERT INTO ".TABLE_PREFIX."secondary_resources_types SET secondary_resource_id=$secondary_resource, type_id=$type_id";
202                 $result = mysql_query($sql, $db);
203         }
204
205         
206         // Set the relative path to all files
207         function setRelativePath($path){
208                 $this->relative_path = $path . '/';
209         }
210
211
212         // Delete all materials associated with this content
213         function deleteA4a(){
214                 global $db; 
215
216                 $pri_resource_ids = array();
217
218                 // Get all primary resources ID out that're associated with this content
219                 $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;
220                 $result = mysql_query($sql);
221
222                 while($row=mysql_fetch_assoc($result)){
223                         $pri_resource_ids[] = $row['primary_resource_id'];
224                 }
225
226                 //If the are primary resources found
227                 if (!empty($pri_resource_ids)){
228                         $glued_pri_ids = implode(",", $pri_resource_ids);
229
230                         // Delete all secondary a4a
231                         $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.')';
232                         $result = mysql_query($sql);
233
234                         // If successful, remove all primary resources
235                         if ($result){
236                                 $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;
237                                 mysql_query($sql);
238                         }
239                 }
240         }
241 }
242
243 ?>