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