made a copy
[atutor.git] / include / html / editor_tabs / alternatives.inc.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2008 by Greg Gay & Joel Kronenberg        */
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: alternatives.inc.php 7208 2008-07-04 16:07:24Z silvia $
14
15 if (!defined('AT_INCLUDE_PATH')) { exit; }
16 require(AT_INCLUDE_PATH.'lib/alternatives_functions.inc.php');
17
18 global $db;
19 /*
20 Edited by Greg Oct 19
21 Commented out radios for full page and file alternatives
22 for now
23 */
24
25 /* Edited by Silvia Oct 24 
26 Added a control in order to force 
27 */
28 if ($cid == 0){
29         echo '<div class="row_alternatives" id="radio_alt">';
30         echo '<p>';
31         echo $msg->printInfos(SAVE_CONTENT);
32         echo '</p>';
33         echo '</div>';
34 }
35 else{
36
37 ?>
38
39 <div class="row_alternatives" id="radio_alt">
40         <input type="hidden" name="alternatives" value="1" id="single_resources" onclick="openIt(1)" />
41
42
43 <!--    <input type="radio" name="alternatives" value="1" id="single_resources" onclick="openIt(1)" <?php if (($_POST['alternatives'] != 2) || ($_GET['alternatives'] != 2)) { echo 'checked="checked"';} ?> />
44         <label for="single_resources"><?php echo _AT('define_alternatives_to_single_resources');  ?></label>
45         <br/>
46         <input type="radio" name="alternatives" value="2" id="whole_page" onclick="openIt(2)" <?php if (($_POST['alternatives'] == 2) || ($_GET['alternatives'] == 2)) { echo 'checked="checked"'; } ?> />
47         <label for="whole_page"><?php echo _AT('define_alternatives_to_the_whole_page');  ?></label>
48 <br/><br/> -->
49 <?php echo '<input class="button" type="submit" name="save_types_and_language" value="'._AT('save_types_and_language').'" class="button"/>'; ?>
50
51 <div class="row_alternatives" id="nontextual_div" style="display: <?php if (($_POST['alternatives'] == 2) || ($_GET['alternatives'] == 2)) echo 'none'; else echo 'block';?>;">
52   <div class="column_primary">
53                 
54 <?php 
55         require(AT_INCLUDE_PATH.'html/resources_parser.inc.php');
56
57         $n=count($resources);
58                                 
59         if ($n==0)
60         {
61                 echo '<p>'. _AT('No_resources'). '</p>';
62         }
63         else 
64         {
65                 $sql    = "SELECT * FROM ".TABLE_PREFIX."primary_resources WHERE content_id=".$cid." order by primary_resource_id";
66                 $result = mysql_query($sql, $db);
67                 
68                 if (mysql_num_rows($result) > 0) 
69                 {
70                         $j=0;
71                         while ($row = mysql_fetch_assoc($result)) 
72                         {
73                                 $whole_resource = $stripslashes(htmlspecialchars($row['resource']));
74                                 $body = $stripslashes(htmlspecialchars($_POST['body_text']));
75                                 if (trim($whole_resource) == trim($body))
76                                         continue;
77                                 else 
78                                 {
79                                         $resources_db[$j]=$row['resource'];
80                                         $j++;
81                                 }
82                         }
83                 }
84
85                 $m=count($resources_db);
86                 for ($i=0; $i < $n; $i++)
87                 {
88                         for($j=0; $j < $m; $j++)
89                         {
90                                 if (trim($resources[$i])==trim($resources_db[$j]))
91                                         $present[$i]=true;
92                         }
93                         
94                         if ($present[$i]==false) 
95                         {
96                                 $sql_sel= "SELECT * FROM ".TABLE_PREFIX."primary_resources WHERE content_id=".$cid." and resource='$resources[$i]'";
97                                 $sel    = mysql_query($sql_sel, $db);
98                                 if (mysql_num_rows($sel) > 0)
99                                         continue;
100                                 else
101                                 {
102                                         $sql_ins= "INSERT INTO ".TABLE_PREFIX."primary_resources VALUES (NULL, $cid, '$resources[$i]', NULL)";
103                                         $r              = mysql_query($sql_ins, $db);
104                                 }
105                         }
106                 }
107                 
108                 $sql    = "SELECT * FROM ".TABLE_PREFIX."primary_resources WHERE content_id=".$cid." order by primary_resource_id";
109                 $result = mysql_query($sql, $db);
110                 while ($row = mysql_fetch_assoc($result)) 
111                 {
112                         $present=false; 
113                         
114                         for ($i=0; $i < $n; $i++)
115                         {
116                                 $cid_wholepage = $cid.'_wholepage';
117                                 if ($row['resource'] ==$cid_wholepage)
118                                 {
119                                         $present=true;
120                                         continue;
121                                 }
122                                 else 
123                                 {
124                                         if (trim($resources[$i])==trim($row['resource'])) 
125                                         {
126                                                 $present=true;  
127 ?>
128   <div class="resource_box">
129     <p>
130       <input type="radio" name="resources" value="<?php echo $row['primary_resource_id']?>" id="<?php echo 'primary_'.$row['primary_resource_id']?>"/>
131       <label class="primary" for="<?php echo 'primary_'.$row['primary_resource_id']?>"><?php link_name_resource($row['resource']);?></label>
132     </p>
133 <?php 
134                                                 checkbox_types($row[primary_resource_id], 'primary', 'non_textual');
135                                                                 
136                                                 $languages = $languageManager->getAvailableLanguages();
137                                                 echo '<label for="lang_'.$row[primary_resource_id].'_primary">'._AT('primary_resource_language').'</label><br />';
138                                                 echo '<select name="lang_'.$row[primary_resource_id].'_primary" id="lang_'.$row[primary_resource_id].'_primary">';
139                                                         
140                                                 foreach ($languages as $codes)
141                                                 {
142                                                         $language = current($codes);
143                                                         $lang_code = $language->getCode();
144                                                         $lang_native_name = $language->getNativeName();
145                                                         $lang_english_name = $language->getEnglishName()
146 ?>
147       <option value="<?php echo $lang_code ?>"
148     <?php if($lang_code == $row[language_code]) echo ' selected="selected"'?>><?php echo $lang_english_name . ' - '. $lang_native_name ?></option>
149 <?php
150                                                 }
151 ?>
152     </select>
153 <?php
154                                                 $sql_alt        = "SELECT * FROM ".TABLE_PREFIX."secondary_resources WHERE primary_resource_id=".$row[primary_resource_id]." order by secondary_resource_id";
155                                                 $result_alt     = mysql_query($sql_alt, $db);
156                                                 
157                                                 if (mysql_num_rows($result_alt) > 0) 
158                                                 {
159 ?>
160     <h2 class="alternatives_to"><?php echo _AT('alternatives_to').' '.$row['resource'];?></h2>
161 <?php
162                                                         while ($alternative = mysql_fetch_assoc($result_alt))
163                                                         {
164 ?>
165     <div class="alternative_box">
166       <p><?php link_name_resource($alternative['secondary_resource']);?></p>
167 <?php 
168                                                                 checkbox_types($alternative['secondary_resource_id'], 'secondary', 'non_textual');
169                                                                 $languages = $languageManager->getAvailableLanguages();
170                                                                 echo '<label for="lang_'.$alternative['secondary_resource_id'].'">'._AT('secondary_resource_language').'</label><br />';
171                                                                 echo '<select name="lang_'.$alternative['secondary_resource_id'].'_secondary" id="lang_'.$alternative['secondary_resource_id'].'">';
172                                                                 
173                                                                 foreach ($languages as $codes)
174                                                                 {
175                                                                         $language = current($codes);
176                                                                         $lang_code = $language->getCode();
177                                                                         $lang_native_name = $language->getNativeName();
178                                                                         $lang_english_name = $language->getEnglishName();
179                                                                         
180                                                                         echo '<option value="'.$lang_code.'"';
181                                                                         if ($lang_code == $alternative['language_code']) echo ' selected="selected"';
182                                                                         echo '>';
183                                                                         echo $lang_english_name . ' - '. $lang_native_name; 
184                                                                         echo '</option>';
185                                                                 } // end of foreach
186 ?>
187       </select>
188             <p><?php delete_alternative($alternative, $cid, $current_tab); ?></p>
189     </div>
190 <?php
191                                                         } // end of while
192                                                 } // end of if
193 ?>
194         </div>
195 <?php   
196                                         } // end of if
197                                 } // end of else
198                         } // end of for
199                         
200                         if ($present==false)
201                         {
202                                 $res=addslashes($row['resource']);
203                                 $sql_sel        = "SELECT primary_resource_id FROM ".TABLE_PREFIX."primary_resources WHERE content_id=".$cid." and resource='".$res."'";
204                                 $result_sel = mysql_query($sql_sel, $db);
205
206                                 while ($id = mysql_fetch_assoc($result_sel))
207                                 {
208                                         $sql_del        = "DELETE FROM ".TABLE_PREFIX."primary_resources WHERE primary_resource_id='".$id[primary_resource_id]."'";
209                                         $result_del = mysql_query($sql_del, $db);
210                                         $sql_del        = "DELETE FROM ".TABLE_PREFIX."primary_resources_types WHERE primary_resource_id=".$id."'";
211                                         $result_del = mysql_query($sql_del, $db);
212                                 }
213                         } // end of if ($present == false)
214                 } // end of while
215         } // end of else
216 ?>
217     </div>
218         
219     <div class="column_equivalent">
220 <?php 
221         require(AT_INCLUDE_PATH.'html/filemanager_display_alternatives.inc.php');
222 ?>
223           </div>
224         </div>
225         
226         <div class="row_alternatives" id="textual_div" style="display: <?php if (($_POST['alternatives'] == 2) || ($_GET['alternatives'] == 2)) echo 'block'; else echo 'none';?>;">
227                 <div class="row">
228 <?php
229         $alternatives=2;
230         if ($changes_made)
231                 $body_ins = $_POST['body_text'];
232         else 
233         {
234                 $sql = "SELECT * FROM ".TABLE_PREFIX."content WHERE content_id='$cid'";
235                 $result = mysql_query($sql, $db);
236
237                 while ($row = mysql_fetch_assoc($result)) 
238                         $body_ins = addslashes($row['text']);
239         }
240         
241         $cid_wholepage = $cid.'_wholepage';
242         $sql    = "SELECT * FROM ".TABLE_PREFIX."primary_resources WHERE content_id='$cid' and resource='$cid_wholepage'";
243   $result       = mysql_query($sql, $db);
244                             
245         if (mysql_num_rows($result) > 0) 
246         {
247                 // Modified by Cindy Li on Oct 2, 2008, replaced while loop with single fetch.
248                 // while ($row = mysql_fetch_assoc($result)) {
249                 // $content_id = $row[primary_resource_id];
250                 // }
251                 $row = mysql_fetch_assoc($result);
252                 $content_id = $row[primary_resource_id];
253         }
254         else 
255         {
256                 $sql_ins = "INSERT INTO ".TABLE_PREFIX."primary_resources VALUES (NULL, $cid, '$cid_wholepage', 'en')";
257                 $r               = mysql_query($sql_ins, $db);
258                 $sql_sel = "SELECT * FROM ".TABLE_PREFIX."primary_resources WHERE content_id='$cid' and resource='$cid_wholepage'";
259         $result  = mysql_query($sql_sel, $db);
260         
261         while ($row = mysql_fetch_assoc($result))
262                         $content_id = $row[primary_resource_id];
263
264                 //Modified by Silvia on Oct 10, 2008
265                 //The whole resource page is inserted in the DB always and only as a textual resource
266                 $sql_sel = "SELECT type_id FROM ".TABLE_PREFIX."resource_types WHERE type='textual'";
267                 $result  = mysql_query($sql_sel, $db);  
268                 $row     = mysql_fetch_assoc($result);
269                 $sql_ins = "INSERT INTO ".TABLE_PREFIX."primary_resources_types VALUES ($content_id, $row[type_id])"; 
270                 $result  = mysql_query($sql_ins, $db);
271         }
272         
273         //Modified by Silvia on Oct 10, 2008
274         //In order to remove the checkboxes to declare whole page types 
275         //(it is recorded only and always as a textual resource)
276         //checkbox_types($content_id, 'primary', 'textual');
277                 
278         $languages = $languageManager->getAvailableLanguages();
279         echo '<label for="lang_'.$content_id.'">'._AT('primary_resource_language').'</label><br />';
280         // Modified by Cindy Li on Oct 2, 2008
281         // Variable name is defined as "lang_1" here, but "editor/edit_content.php" saves on var "lang_1_primary" for 
282         // primary resource language. 
283         // echo '<select name="lang_'.$content_id.'" id="lang_'.$content_id.'">';
284         echo '<select name="lang_'.$content_id.'_primary" id="lang_'.$content_id.'">';
285
286         foreach ($languages as $codes)
287         {
288                 $language = current($codes);
289                 $lang_code = $language->getCode();
290                 $lang_native_name = $language->getNativeName();
291                 $lang_english_name = $language->getEnglishName()
292 ?>
293                         <option value="<?php echo $lang_code ?>"
294 <?php if($lang_code == $row[language_code]) echo ' selected="selected"'?>><?php echo $lang_english_name . ' - '. $lang_native_name ?></option>
295 <?php
296         }
297 ?>
298                 </select>
299         </div>
300         
301         <div class="row">
302 <?php
303 $sql_alt        = "SELECT * FROM ".TABLE_PREFIX."secondary_resources WHERE primary_resource_id=".$content_id." order by secondary_resource_id";
304 $result_alt     = mysql_query($sql_alt, $db);
305
306 if (mysql_num_rows($result_alt) > 0) 
307 {
308         while ($alternative = mysql_fetch_assoc($result_alt))
309         {
310         checkbox_types($alternative['secondary_resource_id'], 'secondary', 'non_textual');
311           $languages = $languageManager->getAvailableLanguages();
312                 echo '<label for="lang_'.$alternative['secondary_resource_id'].'">'._AT('secondary_resource_language').'</label><br />';
313                 echo '<select name="lang_'.$alternative['secondary_resource_id'].'_secondary" id="lang_'.$alternative['secondary_resource_id'].'">';
314                 
315                 foreach ($languages as $codes)
316                 {
317                         $language = current($codes);
318                         $lang_code = $language->getCode();
319                         $lang_native_name = $language->getNativeName();
320                         $lang_english_name = $language->getEnglishName();
321                         echo '<option value="'.$lang_code.'"';
322                         if ($lang_code == $alternative['language_code']) 
323                                 echo ' selected="selected"';
324                         echo '>';
325                         echo $lang_english_name . ' - '. $lang_native_name; 
326                         echo '</option>';
327                 }
328 ?>
329                 </select>
330                 <p><?php //delete_alternative($alternative, $cid, $current_tab); ?></p>
331 <?php
332         } // end of while
333 } // end of if
334 ?>
335         </div>
336         
337         <div class="row">
338                 <?php echo _AT('formatting'); ?><br />
339                 <input type="radio" name="formatting_alt" value="0" id="text_alt_radio" <?php if ($_POST['formatting_alt'] == 0) { echo 'checked="checked"'; } ?> onclick="javascript: document.form.setvisualbutton_alt.disabled=true;" />
340                 <label for="text_alt_radio"><?php echo _AT('plain_text'); ?></label>
341                 <input type="radio" name="formatting_alt" value="1" id="html_alt_radio" <?php if ($_POST['formatting_alt'] == 1 || $_POST['setvisual_alt']) { echo 'checked="checked"'; } ?> onclick="javascript: document.form.setvisualbutton_alt.disabled=false;"/>
342                 <label for="html_alt_radio"><?php echo _AT('html'); ?></label>
343                 <input type="hidden" name="setvisual_alt" value="<?php if ($_POST['setvisual_alt']==1 || $_REQUEST['setvisual_alt']==1 || $_GET['setvisual_alt']==1) echo '1'; else echo '0'; ?>" />
344                 <input type="hidden" name="settext_alt" value="<?php if ($_POST['settext_alt']==1 || $_REQUEST['settext_alt']==1 || $_GET['settext_alt']==1) echo '1'; else echo '0'; ?>" />
345                 <input type="button" name="setvisualbutton_alt" value="<?php echo _AT('switch_visual'); ?>" onclick="switch_body_editor()" class="button" />
346
347                 <script type="text/javascript" language="javascript">
348                 //<!--
349                         document.write(" <a href=\"#\" onclick=\"window.open('<?php echo AT_BASE_HREF; ?>tools/filemanager/index.php?framed=1<?php echo SEP; ?>popup=1<?php echo SEP; ?>tab=5<?php echo SEP; ?>cp=<?php echo $content_row['content_path']; ?>','newWin1','menubar=0,scrollbars=1,resizable=1,width=640,height=490'); return false;\"><?php echo _AT('open_file_manager'); ?> </a>");
350                 //-->
351                 </script>
352                 <noscript>
353                         <a href="<?php echo AT_BASE_HREF; ?>tools/filemanager/index.php?framed=1"><?php echo _AT('open_file_manager'); ?></a>
354                 </noscript>                     
355         </div>
356         <div class="row">
357 <?php 
358
359 // kludge #1548
360 if (trim($_POST['body_text']) == '<br />') 
361 {
362         $_POST['body_text'] = '';
363 }
364 if ($do_check) 
365 {
366         $_POST['body_text'] = $stripslashes($_POST['body_text']);
367 }
368
369 $sql_alt        = "SELECT * FROM ".TABLE_PREFIX."secondary_resources WHERE primary_resource_id=".$content_id." order by secondary_resource_id";
370 $result_alt     = mysql_query($sql_alt, $db);
371 if (mysql_num_rows($result_alt) > 0) 
372 {
373         while ($alternative = mysql_fetch_assoc($result_alt))
374         {
375                 echo '<label for="body_text_alt">'._AT(secondary_resource_body).'</label><br />';
376                 echo '<textarea name="body_text_alt" id="body_text_alt" cols="" rows="20">'.$alternative['secondary_resource'].'</textarea>';
377         }
378 }
379 else
380 {
381         echo '<label for="body_text_alt">'._AT(secondary_resource_body).'</label><br />';
382         echo '<textarea name="body_text_alt" id="body_text_alt" cols="" rows="20">'.htmlspecialchars($_POST['body_text']).'</textarea>';
383 }
384 ?>
385
386         </div>
387         
388         <div class="row">
389                 <?php require(AT_INCLUDE_PATH.'html/editor_tabs/content_code_picker.inc.php'); ?>
390         </div>
391
392         <div class="row">
393                 <strong><?php echo _AT('or'); ?></strong> <?php echo _AT('paste_file'); ?><br />
394                 <input type="file" name="uploadedfile_paste" class="formfield" size="20" /> <input type="submit" name="submit_file_alt" value="<?php echo _AT('upload'); ?>" /><br />
395                 <small class="spacer">&middot;<?php echo _AT('html_only'); ?><br />
396                 &middot;<?php echo _AT('edit_after_upload'); ?></small>
397         </div>
398          
399 </div>  
400
401 <script type="text/javascript" language="javascript">
402 //<!--
403 function on_load()
404 {
405         if (document.getElementById("text_alt_radio").checked)
406                 document.form.setvisualbutton_alt.disabled = true;
407                 
408         if (document.form.setvisual_alt.value==1)
409         {
410                 tinyMCE.execCommand('mceAddControl', false, 'body_text_alt');
411                 document.form.formatting_alt[0].disabled = "disabled";
412                 document.form.setvisualbutton_alt.value = "<?php echo _AT('switch_text'); ?>";
413         }
414         else
415         {
416                 document.form.setvisualbutton_alt.value = "<?php echo _AT('switch_visual'); ?>";
417         }
418 }
419         
420 // switch between text, visual editor for "body text"
421 function switch_body_editor()
422 {
423         if (document.form.setvisualbutton_alt.value=="<?php echo _AT('switch_visual'); ?>")
424         {
425                 tinyMCE.execCommand('mceAddControl', false, 'body_text_alt');
426                 document.form.setvisual_alt.value=1;
427                 document.form.settext_alt.value=0;
428                 document.form.formatting_alt[0].disabled = "disabled";
429                 document.form.setvisualbutton_alt.value = "<?php echo _AT('switch_text'); ?>";
430         }
431         else
432         {
433                 tinyMCE.execCommand('mceRemoveControl', false, 'body_text_alt');
434                 document.form.setvisual_alt.value=0;
435                 document.form.settext_alt.value=1;
436                 document.form.formatting_alt[0].disabled = "";
437                 document.form.setvisualbutton_alt.value = "<?php echo _AT('switch_visual'); ?>";
438         }
439 }
440         
441 function openIt(x)
442 {
443         if (x=='1')
444         {
445                 document.getElementById('textual_div').style.display = "none";
446                 document.getElementById('nontextual_div').style.display = "block";
447         }
448         else 
449         {
450                 document.getElementById('nontextual_div').style.display = "none";
451         document.getElementById('textual_div').style.display = "block";
452         }
453 }
454 //-->
455 </script> 
456 <?php
457 }
458 ?>