76fc8df77173f3ce273fa054f99a1616bdaf6601
[acontent.git] / docs / home / editor / editor_tabs / alternatives.inc.php
1 <?php
2 /************************************************************************/
3 /* AContent                                                             */
4 /************************************************************************/
5 /* Copyright (c) 2010                                                   */
6 /* Inclusive Design Institute                                           */
7 /*                                                                      */
8 /* This program is free software. You can redistribute it and/or        */
9 /* modify it under the terms of the GNU General Public License          */
10 /* as published by the Free Software Foundation.                        */
11 /************************************************************************/
12
13 /**
14  * This script creates the interface of "edit content" => "adapted content"
15  */
16
17 if (!defined('TR_INCLUDE_PATH')) { exit; }
18 include_once(TR_INCLUDE_PATH.'classes/DAO/ResourceTypesDAO.class.php');
19 include_once(TR_INCLUDE_PATH.'classes/DAO/DAO.class.php');
20
21 $dao = new DAO();
22 $resourceTypesDAO = new ResourceTypesDAO();
23
24 global $_content_id, $content_row, $msg;
25 $cid = $_content_id;
26
27 if ($cid == 0) {
28         $msg->printErrors('SAVE_BEFORE_PROCEED');
29         require_once(TR_INCLUDE_PATH.'footer.inc.php');
30         exit;
31 }
32 /**
33  * This function returns the preview link of the given file
34  * @param  $file     the file location in "file manager"
35  * @return the relative URL to preview the file
36  */
37 function get_preview_link($file)
38 {
39         global $content_row, $_course_id;
40         
41         if (substr($file, 0 , 7) == 'http://' || substr($file, 0 , 8) == 'https://') {
42                 return $file;
43         } else {
44                 if (defined('TR_FORCE_GET_FILE') && TR_FORCE_GET_FILE) {
45                         $get_file = 'get.php/';
46                 } else {
47                         $get_file = 'content/' . $_course_id . '/';
48                 }
49         }
50         
51         return $get_file.$content_row['content_path'].'/'.$file;
52 }
53
54 /**
55  * When the file name is a remote URL, this function reduces the full URL
56  * @param  $filename
57  * @return the reduced name
58  */
59 function get_display_filename($filename)
60 {
61         if (substr($filename, 0 , 7) == 'http://' || substr($filename, 0 , 8) == 'https://') {
62                 if (substr($filename, 0 , 7) == 'http://') $prefix = 'http://';
63                 if (substr($filename, 0 , 8) == 'https://') $prefix = 'https://';
64                 $name = substr($filename, strrpos($filename, '/'));
65                 $filename = $prefix.'...'.$name;
66         }
67         return $filename;
68 }
69
70 /**
71  * Display alternative table cell
72  * @param $secondary_result   mysql result of all secondary alternatives
73  *        $alternative type   the resource type of the alternative to display. Must be one of the values in resource_types.type_id
74  *        $content_id         used to pass into file_manager/index.php
75  *        $pid                primary resource id
76  *        $td_header_id       the id of the table header cell, to comply with accessibility rule
77  * @return html of the table cell "<td>...</td>"
78  */ 
79 function display_alternative_cell($secondary_resources, $alternative_type, $content_id, $pid, $td_header_id)
80 {
81         global $content_row, $_course_id;
82         
83         $found_alternative = false;
84         
85         echo '    <td headers="'.$td_header_id.'">'."\n";
86         
87         if (is_array($secondary_resources))
88         {
89 //              mysql_data_seek($secondary_result, 0);  // move the mysql result cursor back to the first row
90                 foreach ($secondary_resources as $secondary_resource)
91                 {
92                         if ($secondary_resource['type_id'] == $alternative_type)
93                         {
94                                 echo '    <div id="'.$pid.'_'.$alternative_type.'">'."\n";
95                                 echo '      <a href="'.get_preview_link($secondary_resource['secondary_resource']).'" title="'._AT('new_window').'" target="_new">'.get_display_filename($secondary_resource['secondary_resource']).'</a><br />'."\n";
96                                 echo '      <a href="#" onclick="trans.utility.poptastic(\''.TR_BASE_HREF.'file_manager/index.php?framed=1'. SEP.'popup=1'. SEP.'cp='. $content_row['content_path'].SEP.'_cid='.$content_id.SEP.'pid='.$pid.SEP.'a_type='.$alternative_type.'\');return false;" title="'._AT('new_window').'">'."\n";
97                                 echo '        <img src="'.TR_BASE_HREF.'images/alter.png" border="0" title="'._AT('alter').'" alt="'._AT('alter').'" />'."\n";
98                                 echo '      </a>'."\n";
99                                 echo '      <a href="#" onclick="removeAlternative(\''.$content_row['content_path'].'\', '.$content_id.','.$pid.','.$alternative_type.');return false;">'."\n";
100                                 echo '        <img src="'.TR_BASE_HREF.'images/remove.gif" border="0" title="'._AT('remove').'" alt="'._AT('remove').'" />'."\n";
101                                 echo '      </a>'."\n";
102                                 echo '    </div>'."\n";
103                                 $found_alternative = true;
104                                 break;
105                         }
106                 }
107         }
108         if (!$found_alternative)
109         {
110                 echo '    <div id="'.$pid.'_'.$alternative_type.'">'."\n";
111                 echo '      <input type="button" value="'._AT('add').'" title="'._AT('new_window').'" onclick="trans.utility.poptastic(\''.TR_BASE_HREF.'file_manager/index.php?framed=1'. SEP.'popup=1'. SEP.'cp='. $content_row['content_path'].SEP.'_cid='.$content_id.SEP.'pid='.$pid.SEP.'a_type='.$alternative_type.'\');return false;" />'."\n";
112                 echo '    </div>'."\n";
113         }
114         echo '    </td>'."\n";
115 }
116
117 // Main program
118 require(TR_INCLUDE_PATH.'lib/resources_parser.inc.php');
119
120 if (count($resources)==0)
121 {
122         echo '<p>'. _AT('No_resources'). '</p>';
123 }
124 else
125 {
126         $is_post_indicator_set = false;
127         // get all resource types
128 //      $sql = "SELECT * FROM ".TABLE_PREFIX."resource_types";
129 //      $resource_types_result = mysql_query($sql, $db);
130         $resource_types = $resourceTypesDAO->getAll();
131         
132         echo '<br /><table class="data" rules="all">'."\n";
133         echo '  <thead>'."\n";
134         echo '  <tr>'."\n";
135         echo '    <th rowspan="2" id="header1">'._AT('original_resource').'</th>'."\n";
136         echo '    <th rowspan="2" id="header2">'._AT('resource_type').'</th>'."\n";
137         echo '    <th colspan="4">'._AT('alternatives').'</th>'."\n";
138         echo '  </tr>'."\n";
139         echo '  <tr>'."\n";
140         echo '    <th id="header3">'._AT('text').'</th>'."\n";
141         echo '    <th id="header4">'._AT('audio').'</th>'."\n";
142         echo '    <th id="header5">'._AT('visual').'</th>'."\n";
143         echo '    <th id="header6">'._AT('sign_lang').'</th>'."\n";
144         echo '  </tr>'."\n";
145         echo '  </thead>'."\n";
146         
147         echo '  <tbody>';
148         foreach($resources as $primary_resource)
149         {
150                 // check whether the primary resource is in the table
151                 $sql = "SELECT * FROM ".TABLE_PREFIX."primary_resources 
152                          WHERE content_id = ".$cid."
153                            AND language_code = '".$_SESSION['lang']."'
154                            AND resource='".$primary_resource."'";
155 //              $primary_result = mysql_query($sql, $db);
156                 $existing_primary_resource = $dao->execute($sql);
157
158                 // insert primary resource if it's not in db
159                 if (!is_array($existing_primary_resource))
160 //              if (mysql_num_rows($primary_result) == 0)
161                 {
162                         $sql = "INSERT INTO ".TABLE_PREFIX."primary_resources (content_id, resource, language_code) 
163                                 VALUES (".$cid.", '".$primary_resource."', '".$_SESSION['lang']."')";
164 //                      $result = mysql_query($sql, $db);
165                         $dao->execute($sql);
166                         $primary_resource_id = mysql_insert_id();
167                 }
168                 else
169                 {
170                         // get primary resource id
171 //                      $primary_resource_row = mysql_fetch_assoc($primary_result);
172                         $primary_resource_id = $existing_primary_resource[0]['primary_resource_id'];
173                 }
174                 
175                 $sql = "SELECT prt.type_id, rt.type
176                           FROM ".TABLE_PREFIX."primary_resources pr, ".
177                                  TABLE_PREFIX."primary_resources_types prt, ".
178                                  TABLE_PREFIX."resource_types rt
179                          WHERE pr.content_id = ".$cid."
180                            AND pr.language_code = '".$_SESSION['lang']."'
181                            AND pr.resource='".$primary_resource."'
182                            AND pr.primary_resource_id = prt.primary_resource_id
183                            AND prt.type_id = rt.type_id";
184 //              $primary_type_result = mysql_query($sql, $db);
185                 $primary_types = $dao->execute($sql);
186                 
187                 if (!$is_post_indicator_set)
188                 {
189                         echo '  <input type="hidden" name="use_post_for_alt" value="1" />'."\n";
190                         $is_post_indicator_set = true;
191                 }
192                 
193                 // get secondary resources for the current primary resource
194                 $sql = "SELECT pr.primary_resource_id, sr.secondary_resource, srt.type_id
195                           FROM ".TABLE_PREFIX."primary_resources pr, ".
196                                  TABLE_PREFIX."secondary_resources sr, ".
197                                  TABLE_PREFIX."secondary_resources_types srt
198                          WHERE pr.content_id = ".$cid."
199                            AND pr.language_code = '".$_SESSION['lang']."'
200                            AND pr.resource='".$primary_resource."'
201                            AND pr.primary_resource_id = sr.primary_resource_id
202                            AND sr.secondary_resource_id = srt.secondary_resource_id";
203 //              $secondary_result = mysql_query($sql, $db);
204                 $secondary_resources = $dao->execute($sql);
205                 
206                 echo '  <tr>'."\n";
207
208                 // table cell "original resource"
209                 echo '    <td headers="header1">'."\n";
210                 echo '    <a href="'.get_preview_link($primary_resource).'" title="'._AT('new_window').'" target="_new">'.get_display_filename($primary_resource).'</a>'."\n";
211                 echo '    </td>'."\n";
212
213                 // table cell "original resource type"
214                 echo '    <td headers="header2">'."\n";
215                 
216 //              mysql_data_seek($resource_types_result, 0);  // move the mysql result cursor back to the first row
217 //              while ($resource_type = mysql_fetch_assoc($resource_types_result))
218                 if (is_array($resource_types))
219                 {
220                         foreach ($resource_types as $resource_type) {
221                                 if ($resource_type['type'] == 'sign_language')
222                                         continue;
223                                 else 
224                                 {
225                                         echo '<input type="checkbox" name="alt_'.$primary_resource_id.'_'.$resource_type['type_id'].'" value="1" id="alt_'.$primary_resource_id.'_'.$resource_type['type_id'].'"';
226                                         if ($_POST['use_post_for_alt'])
227                                         {
228                                                 if (isset($_POST['alt_'.$primary_resource_id.'_'.$resource_type['type_id']])) {
229                                                         echo 'checked="checked"';
230                                                 }
231                                         }
232                                         else {
233                                                 if (is_array($primary_types)) {
234                                                         foreach ($primary_types as $primary_resource_type) {
235                                                                 if ($primary_resource_type['type_id'] == $resource_type['type_id']){
236                                                                         echo 'checked="checked"';
237                                                                         break;
238                                                                 }
239                                                         }
240                                                 }
241                                         }
242                                         echo '/>'."\n";
243                                         echo '<label for="alt_'.$primary_resource_id.'_'.$resource_type['type_id'].'">'. _AT($resource_type['type']).'</label><br/>'."\n";      
244                                 }
245                         }
246                 }
247                 echo '    </td>'."\n";
248                 
249                 // table cell "text alternative"
250                 display_alternative_cell($secondary_resources, 3, $cid, $primary_resource_id, "header3");
251                 
252                 // table cell "audio"
253                 display_alternative_cell($secondary_resources, 1, $cid, $primary_resource_id, "header4");
254                 
255                 // table cell "visual"
256                 display_alternative_cell($secondary_resources, 4, $cid, $primary_resource_id, "header5");
257                 
258                 // table cell "sign language"
259                 display_alternative_cell($secondary_resources, 2, $cid, $primary_resource_id, "header6");
260                 
261                 echo '  </tr>'."\n";
262         }
263         echo '  </tbody>'."\n";
264         echo '</table><br /><br />'."\n";
265 }
266 ?>
267
268 <script type="text/javascript">
269 //<!--
270 // This function does:
271 // 1. save the removal into db via ajax
272 // 2. set the according field to "add" button
273 function removeAlternative(contentPath, cid, pid, a_type) 
274 {
275         jQuery.post("<?php echo TR_BASE_HREF; ?>home/editor/remove_alternative.php", 
276                         {"pid":pid, "a_type":a_type}, 
277                         function(data) {});
278
279         var button_html = '      <input type="button" value="<?php echo _AT('add'); ?>" title="<?php echo _AT('new_window'); ?>" onclick="trans.utility.poptastic(\\\'<?php echo TR_BASE_HREF; ?>file_manager/index.php?framed=1<?php echo SEP; ?>popup=1<?php echo SEP; ?>cp='+contentPath+'<?php echo SEP; ?>_cid='+cid+'<?php echo SEP; ?>pid='+pid+'<?php echo SEP; ?>a_type='+a_type+'\\\');return false;" />';
280         eval("document.getElementById(\""+pid+"_"+a_type+"\").innerHTML = '"+button_html+"'");
281 }
282 //-->
283 </script>