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