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