move code up one directory
[atutor.git] / mods / _core / editor / edit_content.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2010                                              */
6 /* Inclusive Design Institute                                           */
7 /* http://atutor.ca                                                     */
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 // $Id$
13
14 define('AT_INCLUDE_PATH', '../../../include/');
15
16 global $db, $associated_forum;
17
18 $get_related_glossary = true;
19 require(AT_INCLUDE_PATH.'vitals.inc.php');
20
21 authenticate(AT_PRIV_CONTENT);
22
23 require(AT_INCLUDE_PATH.'lib/tinymce.inc.php');
24 require_once(AT_INCLUDE_PATH.'../mods/_core/file_manager/filemanager.inc.php');
25
26 $cid = intval($_REQUEST['cid']);
27
28 if ($_POST) {
29         $do_check = TRUE;
30 } else {
31         $do_check = FALSE;
32 }
33
34 require(AT_INCLUDE_PATH.'../mods/_core/editor/editor_tab_functions.inc.php');
35
36 if ($_POST['close'] || $_GET['close']) {
37         if ($_GET['close']) {
38                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
39         } else {
40                 $msg->addFeedback('CLOSED');
41                 if ($cid == 0) {
42                         header('Location: '.AT_BASE_HREF.'mods/_core/content/index.php');
43                         exit;
44                 }
45         }
46         
47         if ($_REQUEST['cid'] == 0) {
48                 header('Location: '.AT_BASE_HREF.'mods/_core/content/index.php');
49                 exit;
50         }
51         header('Location: '.$_base_path.url_rewrite('content.php?cid='.intval($_REQUEST['cid'])));
52         exit;
53 }
54         
55 $tabs = get_tabs();     
56 $num_tabs = count($tabs);
57 for ($i=0; $i < $num_tabs; $i++) {
58         if (isset($_POST['button_'.$i]) && ($_POST['button_'.$i] != -1)) { 
59                 $current_tab = $i;
60                 $_POST['current_tab'] = $i;
61                 break;
62         }
63 }
64
65 if (isset($_GET['tab'])) {
66         $current_tab = intval($_GET['tab']);
67 }
68 if (isset($_POST['current_tab'])) {
69         $current_tab = intval($_POST['current_tab']);
70 }
71
72 if (isset($_POST['submit_file'])) {
73         paste_from_file(body_text);
74 } else if (isset($_POST['submit']) && ($_POST['submit'] != 'submit1')) {
75         /* we're saving. redirects if successful. */
76         save_changes(true, $current_tab);
77 }
78
79 if (isset($_POST['submit_file_alt'])) {
80         paste_from_file(body_text_alt);
81 } else if (isset($_POST['submit']) && ($_POST['submit'] != 'submit1')) {
82         /* we're saving. redirects if successful. */
83         save_changes(true, $current_tab);
84 }
85
86 if (isset($_POST['submit'])) {
87         /* we're saving. redirects if successful. */
88         save_changes(true, $current_tab);
89 }
90
91 if (!isset($current_tab) && isset($_POST['button_1']) && ($_POST['button_1'] == -1) && !isset($_POST['submit'])) {
92         $current_tab = 1;
93 } else if (!isset($current_tab)) {
94         $current_tab = 0;
95 }
96
97 if ($cid) {
98         $_section[0][0] = _AT('edit_content');
99 } else {
100         $_section[0][0] = _AT('add_content');
101 }
102
103 if($current_tab == 0) {
104         global $_content_tools;
105         
106         $_custom_head .= '
107     <link rel="stylesheet" type="text/css" href="'.AT_BASE_HREF.'jscripts/infusion/framework/fss/css/fss-layout.css" />
108     <link rel="stylesheet" type="text/css" href="'.AT_BASE_HREF.'jscripts/infusion/framework/fss/css/fss-text.css" />
109     ';
110         $_content_tools = is_array($_content_tools) ? $_content_tools : array();
111         
112         $current_tool_pos = 0;
113         $_content_tools[] = array("id"=>"previewtool", 
114                                   "class"=>"fl-col clickable", 
115                                   "src"=>AT_BASE_HREF."images/preview.png",
116                                   "title"=>_AT('preview').' - '._AT('new_window'),
117                                   "alt"=>_AT('preview').' - '._AT('new_window'),
118                                   "text"=>_AT('preview'), 
119                                   "js"=>AT_BASE_HREF."mods/_core/editor/js/edit.js",
120                                   "position"=>++$current_tool_pos);
121         
122         $_content_tools[] = array("id"=>"accessibilitytool", "class"=>"fl-col", "text"=>_AT('accessibility'), "position"=>++$current_tool_pos);
123         $_content_tools[] = array("id"=>"headtool", "class"=>"fl-col", "text"=>_AT('customized_head'), "position"=>++$current_tool_pos);
124         $_content_tools[] = array("id"=>"pastetool", "class"=>"fl-col", "text"=>_AT('paste'), "position"=>++$current_tool_pos);
125         $_content_tools[] = array("id"=>"filemantool", "class"=>"fl-col", "text"=>_AT('files'), "position"=>++$current_tool_pos);
126
127         foreach ($_content_tools as $tool) {
128         if (isset($tool["js"])) {
129                 $_custom_head .= '<script type="text/javascript" src="'.$tool["js"].'"></script>'."\n";
130         }
131     }
132 }
133
134 if ($cid) {
135         $result = $contentManager->getContentPage($cid);
136
137         if (!($content_row = @mysql_fetch_assoc($result))) {
138                 require(AT_INCLUDE_PATH.'header.inc.php');
139                 $msg->printErrors('PAGE_NOT_FOUND');
140                 require (AT_INCLUDE_PATH.'footer.inc.php');
141                 exit;
142         }
143
144         $path   = $contentManager->getContentPath($cid);
145         $content_test = $contentManager->getContentTestsAssoc($cid);
146
147         if (defined('AT_FORCE_GET_FILE') && AT_FORCE_GET_FILE) {
148                 $course_base_href = 'get.php/';
149         } else {
150                 $course_base_href = 'content/' . $_SESSION['course_id'] . '/';
151         }
152
153         if ($content_row['content_path']) {
154                 $content_base_href .= $content_row['content_path'].'/';
155         }
156 } else {
157         if (defined('AT_FORCE_GET_FILE') && AT_FORCE_GET_FILE) {
158                 $content_base_href = 'get.php/';
159         } else {
160                 $content_base_href = 'content/' . $_SESSION['course_id'] . '/';
161         }
162 }
163
164 if (($current_tab == 0) || ($_current_tab == 3)) {
165     if ($_POST['formatting'] == null){ 
166         // this is a fresh load from just logged in
167             if ($_SESSION['prefs']['PREF_CONTENT_EDITOR'] == 0) { // plain text in text area
168                         $_POST['formatting'] = 0;
169                 } else if ($_SESSION['prefs']['PREF_CONTENT_EDITOR'] == 1) { // html with text area
170                         $_POST['formatting'] = 1;
171             } else if ($_SESSION['prefs']['PREF_CONTENT_EDITOR'] == 2) { // html with visual editor
172                         $_POST['formatting'] = 3;
173                 }
174     }
175 }
176
177 require(AT_INCLUDE_PATH.'header.inc.php');
178
179 if ($current_tab == 0 || $current_tab == 3) 
180 {
181     $simple = true;
182     if ($_POST['complexeditor'] == '1') {
183         $simple = false;
184     }
185     load_editor($simple, false, "none");    
186 }
187
188 // Generate the last content tool elements
189 //loading toolbar for insert discussion topic or web link into the content
190 if ($current_tab == 0){
191     if(authenticate(AT_PRIV_CONTENT,AT_PRIV_RETURN)){
192         $home_links = get_home_navigation();                        //vengono lette le caratteristiche di ogni modulo attivato nella home page.
193         $main_links = get_main_navigation($current_page);           //vengono lette le caratteristiche di ogni modulo attivo nel main navigation
194
195         $num = count($main_links);                                  //necessario elminare il primo e l'utlimo elemento poichè sono rispettivamente "Home" e "Manage"
196         unset($main_links[0]);                                      //"Home" label
197         unset($main_links[$num-1]);                                 //"Manage" label
198
199         $all_tools = $home_links;                                   //$all_tools represent a merge between $home_links and main_links without repetitions.
200         $check=false;
201         foreach($main_links as $main) {
202             foreach($home_links as $home) {
203                 if($home['title'] == $main['title']) {
204                     $check=true;
205                     break;
206                 }
207             }
208             if(!$check && $main['tool_file'] != '' && $main['table'] != '') {
209                 $_content_tools[] = array("class"=>"fl-col clickable tool", 
210                                                           "src"=>$main['img'], 
211                                                           "alt"=>$main['alt'],
212                                                           "title"=>$main['title'],
213                                                           "text"=>$main['title'],
214                                           "position"=>++$current_tool_pos);
215     
216                         if(isset($main['tool_file'])) {
217                                 echo '<!-- TODO LAW note problem here with one tool_file variable for multiple tools -->'."\n";
218                                     echo '  <script type="text/javascript" language="javascript">'."\n";
219                                     echo '  //<!--'."\n";
220                                     echo '  ATutor.mods.editor.tool_file = "'. $main['tool_file'].'";'."\n";
221                                     echo '  //-->'."\n";
222                                     echo '  </script>'."\n";
223                         }
224                 
225             }
226             else {
227                 $check=false;
228             }
229         }
230     }
231 }
232
233 // The customized function to sort multi-dimentional array $_content_tools
234 function compare($x, $y) {
235         if (!isset($x["position"])) return 1;
236         if (!isset($y["position"])) return -1;
237         if ( $x["position"] == $y["position"] )
238                 return 0;
239         else if ( $x["position"] < $y["position"] )
240                 return -1;
241         else
242                 return 1;
243 }
244
245 // Sort $_content_tools. Always append the element with empty "position" to the end of the result array.
246 usort($_content_tools, 'compare');
247
248 $cid = intval($_REQUEST['cid']);
249 $pid = intval($_REQUEST['pid']);
250 ?>
251 <form action="<?php echo $_SERVER['PHP_SELF']; ?>?cid=<?php echo $cid; ?>" method="post" name="form" enctype="multipart/form-data">
252 <?php
253
254         if ($cid) {
255                 //$content_row = sql_quote($content_row);
256                 if (isset($_POST['current_tab'])) {
257                         //$changes_made = check_for_changes($content_row);
258                 } else {
259                         $changes_made = array();
260
261                         $_POST['formatting'] = $content_row['formatting'];
262                         $_POST['head'] = $content_row['head'];
263                         $_POST['use_customized_head'] = $content_row['use_customized_head'];
264                         $_POST['title']      = $content_row['title'];
265                         $_POST['body_text']  = $content_row['text'];
266                         $_POST['weblink_text'] = $content_row['text'];
267                         $_POST['keywords']   = $content_row['keywords'];
268                         $_POST['test_message'] = $content_row['test_message'];
269                         $_POST['allow_test_export'] = $content_row['allow_test_export'];
270
271                         $_POST['day']   = substr($content_row['release_date'], 8, 2);
272                         $_POST['month'] = substr($content_row['release_date'], 5, 2);
273                         $_POST['year']  = substr($content_row['release_date'], 0, 4);
274                         $_POST['hour']  = substr($content_row['release_date'], 11, 2);
275                         $_POST['min']= substr($content_row['release_date'], 14, 2);
276
277                         $_POST['ordering'] = $content_row['ordering'];
278                         $_POST['related'] = $contentManager->getRelatedContent($cid);
279                         
280                         $_POST['pid'] = $pid = $content_row['content_parent_id'];
281
282                         $_POST['related_term'] = $glossary_ids_related;
283                 }
284
285         } else {
286                 $cid = 0;
287                 if (!isset($_POST['current_tab'])) {
288                         $_POST['day']  = date('d');
289                         $_POST['month']  = date('m');
290                         $_POST['year'] = date('Y');
291                         $_POST['hour'] = date('H');
292                         $_POST['min']  = 0;
293
294                         if (isset($_GET['pid'])) {
295                                 $pid = $_POST['pid'] = intval($_GET['pid']);
296                                 $_POST['ordering'] = count($contentManager->getContent($pid))+1;
297                         } else {
298                                 $_POST['pid'] = 0;
299                                 $_POST['ordering'] = count($contentManager->getContent(0))+1;
300                         }
301                 }
302         }
303         
304         echo '<input type="hidden" name="cid" value="'.$cid.'" />';
305         echo '<input type="hidden" name="title" value="'.htmlspecialchars($stripslashes($_POST['title'])).'" />';
306         if ($_REQUEST['sub'] == 1)
307         {
308                 echo '<input type="hidden" name="sub" value="1" />';
309                 echo '<input type="hidden" name="folder_title" value="'.htmlspecialchars($stripslashes($_POST['folder_title'])).'" />';
310         }
311         echo '<input type="submit" name="submit" style="display:none;"/>';
312         if (($current_tab != 0) && (($_current_tab != 3))) {
313         echo '<input type="hidden" name="body_text" value="'.htmlspecialchars($stripslashes($_POST['body_text'])).'" />';
314         echo '<input type="hidden" name="weblink_text" value="'.htmlspecialchars($stripslashes($_POST['weblink_text'])).'" />';
315         echo '<input type="hidden" name="head" value="'.htmlspecialchars($stripslashes($_POST['head'])).'" />';
316                 echo '<input type="hidden" name="use_customized_head" value="'.(($_POST['use_customized_head']=="") ? 0 : $_POST['use_customized_head']).'" />';
317         echo '<input type="hidden" name="displayhead" id="displayhead" value="'.$_POST['displayhead'].'" />';
318         echo '<input type="hidden" name="complexeditor" id="complexeditor" value="'.$_POST['complexeditor'].'" />';
319         echo '<input type="hidden" name="formatting" value="'.$_POST['formatting'].'" />';
320         }
321
322         echo '<input type="hidden" name="ordering" value="'.$_POST['ordering'].'" />';
323         echo '<input type="hidden" name="pid" value="'.$pid.'" />';
324
325         echo '<input type="hidden" name="day" value="'.$_POST['day'].'" />';
326         echo '<input type="hidden" name="month" value="'.$_POST['month'].'" />';
327         echo '<input type="hidden" name="year" value="'.$_POST['year'].'" />';
328         echo '<input type="hidden" name="hour" value="'.$_POST['hour'].'" />';
329         echo '<input type="hidden" name="minute" value="'.$_POST['minute'].'" />';
330         echo '<input type="hidden" name="min" value="'.$_POST['min'].'" />';
331         
332         echo '<input type="hidden" name="alternatives" value="'.$_POST['alternatives'].'" />';
333         
334         echo '<input type="hidden" name="current_tab" value="'.$current_tab.'" />';
335
336         if (is_array($_POST['related']) && ($current_tab != 1)) {
337                 foreach($_POST['related'] as $r_id) {
338                         echo '<input type="hidden" name="related[]" value="'.$r_id.'" />';
339                 }
340         }
341         echo '<input type="hidden" name="keywords" value="'.htmlspecialchars(stripslashes($_POST['keywords'])).'" />';
342
343         //content test association
344         echo '<input type="hidden" name="test_message" value="'.$_POST['test_message'].'" />';
345         
346         /* get glossary terms */
347         $matches = find_terms(stripslashes($_POST['body_text']));
348         $num_terms = count($matches[0]);
349         $matches = $matches[0];
350         $word = str_replace(array('[?]', '[/?]'), '', $matches);
351
352         if (is_array($word)) {
353                 /* update $_POST['glossary_defs'] with any new/changed terms */
354                 for($i=0; $i<$num_terms; $i++) {
355 //                      $word[$i] = htmlentities_utf8($word[$i]);
356                         if (!isset($_POST['glossary_defs'][$word[$i]])) {
357                                 $_POST['glossary_defs'][$word[$i]] = $glossary[$word[$i]];
358                         }
359                 }
360         }
361
362         if (is_array($_POST['glossary_defs']) && ($current_tab != 2)) {
363                 foreach($_POST['glossary_defs'] as $w => $d) {
364
365                         /* this term still exists in the content */
366                         if (!in_array($w, $word)) {
367                                 unset($_POST['glossary_defs'][$w]);
368                                 continue;
369                         }
370                         echo '<input type="hidden" name="glossary_defs['.AT_print($w, 'glossary.word').']" value="'.AT_print($d, 'glossary.definition').'" />';
371                 }
372                 if (isset($_POST['related_term'])) {
373                         foreach($_POST['related_term'] as $w => $d) {
374                                 echo '<input type="hidden" name="related_term['.AT_print($w, 'glossary.word').']" value="'.AT_print($d, 'glossary.definition').'" />';
375                         }
376                 }
377         }
378
379         // adapted content
380         $sql = "SELECT pr.primary_resource_id, prt.type_id
381                   FROM ".TABLE_PREFIX."primary_resources pr, ".
382                          TABLE_PREFIX."primary_resources_types prt
383                  WHERE pr.content_id = ".$cid."
384                    AND pr.language_code = '".$_SESSION['lang']."'
385                    AND pr.primary_resource_id = prt.primary_resource_id";
386         $all_types_result = mysql_query($sql, $db);
387         
388         $i = 0;
389         while ($type = mysql_fetch_assoc($all_types_result)) {
390                 $row_alternatives['alt_'.$type['primary_resource_id'].'_'.$type['type_id']] = 1;
391         }
392         
393         if ($current_tab != 3 && isset($_POST['use_post_for_alt']))
394         {
395                 echo '<input type="hidden" name="use_post_for_alt" value="1" />';
396                 if (is_array($_POST)) {
397                         foreach ($_POST as $alt_id => $alt_value) {
398                                 if (substr($alt_id, 0 ,4) == 'alt_'){
399                                         echo '<input type="hidden" name="'.$alt_id.'" value="'.$alt_value.'" />';
400                                 }
401                         }
402                 }
403         }
404         
405         //tests
406         if ($current_tab != 4){
407                 // set content associated tests
408                 if (is_array($_POST['tid'])) {
409                         foreach ($_POST['tid'] as $i=>$tid){
410                                 echo '<input type="hidden" name="tid['.$i.']" value="'.$tid.'" />';
411                         }
412                 }
413                 else
414                 {
415                         $i = 0;
416                         if ($content_test){
417                                 while ($content_test_row = mysql_fetch_assoc($content_test)){
418                                         echo '<input type="hidden" name="tid['.$i++.']" value="'.$content_test_row['test_id'].'" />';
419                                 }
420                         }
421                 }
422                 
423                 // set pre-tests
424                 if (is_array($_POST['pre_tid'])) {
425                         foreach ($_POST['pre_tid'] as $i=>$pre_tid){
426                                 echo '<input type="hidden" name="pre_tid['.$i.']" value="'.$pre_tid.'" />';
427                         }
428                 }
429                 else
430                 {
431                         $i = 0;
432                         $sql = 'SELECT * FROM '.TABLE_PREFIX."content_prerequisites WHERE content_id=$cid AND type='".CONTENT_PRE_TEST."'";
433                         $pretests_result = mysql_query($sql, $db);
434                         while ($pretest_row = mysql_fetch_assoc($pretests_result)) {
435                                         echo '<input type="hidden" name="pre_tid['.$i++.']" value="'.$pretest_row['item_id'].'" />';
436                         }
437                 }
438         } 
439         if (!isset($_POST['allow_test_export']) && $current_tab != 4) {
440                 //export flag handling.
441                 $sql = "SELECT `allow_test_export` FROM ".TABLE_PREFIX."content WHERE content_id=$_REQUEST[cid]";
442                 $result2 = mysql_query($sql, $db);
443                 if ($result2){
444                         $c_row = mysql_fetch_assoc($result2);
445                 }
446                 if (intval($c_row['allow_test_export'])==1){
447                         echo '<input type="hidden" name="allow_test_export" value="1" />';
448                 } else {
449                         echo '<input type="hidden" name="allow_test_export" value="0" />';
450                 }
451         } else {
452                 echo '<input type="hidden" name="allow_test_export" value="'.intval($_POST['allow_test_export']).'" />';
453         }
454
455         if ($do_check) {
456                 $changes_made = check_for_changes($content_row, $row_alternatives);
457         }
458 ?>
459
460 <div class="editor_wrapper">
461
462 <div>
463         <?php output_tabs($current_tab, $changes_made); ?>
464 </div>
465
466 <span style="clear:both;"/></span>
467 <div class="input-form">
468
469         <?php if ($changes_made): ?>
470                 <div class="unsaved">
471                         <span style="color:red;"><?php echo _AT('save_changes_unsaved'); ?></span> 
472                         <input type="submit" name="submit" value="<?php echo _AT('save'); ?>" title="<?php echo _AT('save_changes'); ?> alt-s" accesskey="s" style="border: 1px solid red;" /> 
473                         <input type="submit" name="close" class="button green" value="<?php echo _AT('close'); ?>" />  <input type="checkbox" id="close" name="save_n_close" value="1" <?php if ($_SESSION['save_n_close']) { echo 'checked="checked"'; } ?> />
474                         <label for="close"><?php echo _AT('close_after_saving'); ?></label>
475                 </div>
476
477         <?php else: ?>
478                 <div class="saved">
479                         <?php //if ($cid) { echo _AT('save_changes_saved'); } ?> <input type="submit" name="submit" value="<?php echo _AT('save'); ?>" title="<?php echo _AT('save_changes'); ?> alt-s" accesskey="s" class="button"/> <input type="submit" name="close" value="<?php echo _AT('close'); ?>"  class="button"/> <input type="checkbox" style="border:0px;" id="close" name="save_n_close" value="1" <?php if ($_SESSION['save_n_close']) { echo 'checked="checked"'; } ?>/><label for="close"><?php echo _AT('close_after_saving'); ?></label>
480                 </div>
481         <?php endif; ?>
482         <?php include('editor_tabs/'.$tabs[$current_tab][1]); ?>
483 </div></div>
484 </form>
485
486 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>