fix bugs from merging atutor changes.
[acontent.git] / docs / home / editor / edit_content.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 define('TR_INCLUDE_PATH', '../../include/');
14
15 global $associated_forum, $_course_id, $_content_id;
16
17 require(TR_INCLUDE_PATH.'vitals.inc.php');
18 require_once(TR_INCLUDE_PATH.'lib/tinymce.inc.php');
19 require_once(TR_INCLUDE_PATH.'classes/FileUtility.class.php');
20 require_once(TR_INCLUDE_PATH.'classes/DAO/DAO.class.php');
21
22 Utility::authenticate(TR_PRIV_ISAUTHOR);
23
24 $cid = $_content_id;
25 $dao = new DAO();
26
27 if ($_POST) {
28         $do_check = TRUE;
29 } else {
30         $do_check = FALSE;
31 }
32
33 require(TR_INCLUDE_PATH.'../home/editor/editor_tab_functions.inc.php');
34
35 if ($_POST['close'] || $_GET['close']) {
36         if ($_GET['close']) {
37                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
38         } else {
39                 $msg->addFeedback('CLOSED');
40                 if ($cid == 0) {
41                         header('Location: '.TR_BASE_HREF.'home/course/index.php?_course_id='.$_course_id);
42                         exit;
43                 }
44         }
45         
46         if (!isset($_content_id) || $_content_id == 0) {
47                 header('Location: '.TR_BASE_HREF.'home/course/index.php?_course_id='.$_course_id);
48                 exit;
49         }
50         header('Location: '.TR_BASE_HREF.'home/course/content.php?_cid='.$_content_id);
51         exit;
52 }
53         
54 $tabs = get_tabs();     
55 $num_tabs = count($tabs);
56 for ($i=0; $i < $num_tabs; $i++) {
57         if (isset($_POST['button_'.$i]) && ($_POST['button_'.$i] != -1)) { 
58                 $current_tab = $i;
59                 $_POST['current_tab'] = $i;
60                 break;
61         }
62 }
63
64 if (isset($_GET['tab'])) {
65         $current_tab = intval($_GET['tab']);
66 }
67 if (isset($_POST['current_tab'])) {
68         $current_tab = intval($_POST['current_tab']);
69 }
70
71 if (isset($_POST['submit_file'])) {
72         paste_from_file(body_text);
73 } else if (isset($_POST['submit']) && ($_POST['submit'] != 'submit1')) {
74         /* we're saving. redirects if successful. */
75         debug('here');exit;
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         debug('here');
89         save_changes(true, $current_tab);
90 }
91
92 if (!isset($current_tab) && isset($_POST['button_1']) && ($_POST['button_1'] == -1) && !isset($_POST['submit'])) {
93         $current_tab = 1;
94 } else if (!isset($current_tab)) {
95         $current_tab = 0;
96 }
97
98 if ($cid) {
99         $_section[0][0] = _AT('edit_content');
100 } else {
101         $_section[0][0] = _AT('add_content');
102 }
103
104 if($current_tab == 0) {
105     $_custom_head .= '
106     <link rel="stylesheet" type="text/css" href="'.TR_BASE_HREF.'include/jscripts/infusion/framework/fss/css/fss-layout.css" />
107     <link rel="stylesheet" type="text/css" href="'.TR_BASE_HREF.'include/jscripts/infusion/framework/fss/css/fss-text.css" />
108     <script type="text/javascript" src="'.$_base_path.'home/editor/js/edit.js"></script>
109     ';
110 }
111
112 if ($cid) {
113         if (isset($contentManager)) $content_row = $contentManager->getContentPage($cid);
114
115         if (!$content_row || !isset($contentManager)) {
116                 require(TR_INCLUDE_PATH.'header.inc.php');
117                 $msg->printErrors('MISSING_CONTENT');
118                 require (TR_INCLUDE_PATH.'footer.inc.php');
119                 exit;
120         }
121
122         $path   = $contentManager->getContentPath($cid);
123         $content_tests = $contentManager->getContentTestsAssoc($cid);
124
125         if (defined('TR_FORCE_GET_FILE') && TR_FORCE_GET_FILE) {
126                 $course_base_href = 'get.php/';
127         } else {
128                 $course_base_href = 'content/' . $_SESSION['course_id'] . '/';
129         }
130
131         if ($content_row['content_path']) {
132                 $content_base_href .= $content_row['content_path'].'/';
133         }
134 } else {
135         if (defined('TR_FORCE_GET_FILE') && TR_FORCE_GET_FILE) {
136                 $content_base_href = 'get.php/';
137         } else {
138                 $content_base_href = 'content/' . $_SESSION['course_id'] . '/';
139         }
140 }
141 debug($content_base_href);exit;
142 if (($current_tab == 0) || ($current_tab == 2)) {
143     if ($_POST['formatting'] == null){ 
144         // this is a fresh load from just logged in
145             if (isset($_SESSION['prefs']['PREF_CONTENT_EDITOR']) && $_SESSION['prefs']['PREF_CONTENT_EDITOR'] == 0) {
146                         $_POST['formatting'] = 0;
147                 } else {
148                         $_POST['formatting'] = 1;
149                 }
150     }
151 }
152
153 require(TR_INCLUDE_PATH.'header.inc.php');
154
155 if ($current_tab == 0 || $current_tab == 2) 
156 {
157     $simple = true;
158     if ($_POST['complexeditor'] == '1') {
159         $simple = false;
160     }
161     load_editor($simple, false, "none");    
162 }
163
164 //TODO*************BOLOGNA****************REMOVE ME**************/
165 //loading toolbar for insert discussion topic or web link into the content
166 //if ($current_tab == 0){
167 //    if(authenticate(TR_PRIV_CONTENT,TR_PRIV_RETURN)){
168 //        $home_links = get_home_navigation();                        //vengono lette le caratteristiche di ogni modulo attivato nella home page.
169 //        $main_links = get_main_navigation($current_page);           //vengono lette le caratteristiche di ogni modulo attivo nel main navigation
170 //
171 //        $num = count($main_links);                                  //necessario elminare il primo e l'utlimo elemento poichè sono rispettivamente "Home" e "Manage"
172 //        unset($main_links[0]);                                      //"Home" label
173 //        unset($main_links[$num-1]);                                 //"Manage" label
174 //
175 //        $all_tools = $home_links;                                   //$all_tools represent a merge between $home_links and main_links without repetitions.
176 //        $check=false;
177 //        foreach($main_links as $main) {
178 //            foreach($home_links as $home) {
179 //                if($home['title'] == $main['title']) {
180 //                    $check=true;
181 //                    break;
182 //                }
183 //            }
184 //            if(!$check)
185 //                $all_tools[]=$main;
186 //            else
187 //                $check=false;
188 //        }
189 //    }
190 //}
191
192 $pid = intval($_REQUEST['pid']);
193 ?>
194
195 <form action="<?php echo $_SERVER['PHP_SELF']; ?>?_cid=<?php echo $cid; ?>" method="post" name="form" enctype="multipart/form-data">
196 <?php
197
198         if ($cid) {
199                 //$content_row = sql_quote($content_row);
200                 if (isset($_POST['current_tab'])) {
201                         //$changes_made = check_for_changes($content_row);
202                 } else {
203                         $changes_made = array();
204
205                         $_POST['formatting'] = $content_row['formatting'];
206                         $_POST['head'] = $content_row['head'];
207                         $_POST['use_customized_head'] = $content_row['use_customized_head'];
208                         $_POST['title']      = $content_row['title'];
209                         $_POST['body_text']  = $content_row['text'];
210                         $_POST['weblink_text'] = $content_row['text'];
211                         $_POST['keywords']   = $content_row['keywords'];
212                         $_POST['test_message'] = $content_row['test_message'];
213
214 //                      $_POST['day']   = substr($content_row['release_date'], 8, 2);
215 //                      $_POST['month'] = substr($content_row['release_date'], 5, 2);
216 //                      $_POST['year']  = substr($content_row['release_date'], 0, 4);
217 //                      $_POST['hour']  = substr($content_row['release_date'], 11, 2);
218 //                      $_POST['min']= substr($content_row['release_date'], 14, 2);
219
220                         $_POST['ordering'] = $content_row['ordering'];
221 //                      $_POST['related'] = $contentManager->getRelatedContent($cid);
222                         
223                         $_POST['pid'] = $pid = $content_row['content_parent_id'];
224
225 //                      $_POST['related_term'] = $glossary_ids_related;
226                 }
227
228         } else {
229                 $cid = 0;
230                 if (!isset($_POST['current_tab'])) {
231                         $_POST['day']  = date('d');
232                         $_POST['month']  = date('m');
233                         $_POST['year'] = date('Y');
234                         $_POST['hour'] = date('H');
235                         $_POST['min']  = 0;
236
237                         if (isset($_GET['pid'])) {
238                                 $pid = $_POST['pid'] = intval($_GET['pid']);
239                                 $_POST['ordering'] = count($contentManager->getContent($pid))+1;
240                         } else {
241                                 $_POST['pid'] = 0;
242                                 $_POST['ordering'] = count($contentManager->getContent(0))+1;
243                         }
244                 }
245         }
246         
247         echo '<input type="hidden" name="_course_id" value="'.$_course_id.'" />';
248         echo '<input type="hidden" name="_cid" value="'.$cid.'" />';
249         echo '<input type="hidden" name="title" value="'.htmlspecialchars($stripslashes($_POST['title'])).'" />';
250         if ($_REQUEST['sub'] == 1)
251         {
252                 echo '<input type="hidden" name="sub" value="1" />';
253                 echo '<input type="hidden" name="folder_title" value="'.htmlspecialchars($stripslashes($_POST['folder_title'])).'" />';
254         }
255         echo '<input type="submit" name="submit" style="display:none;"/>';
256         if (($current_tab != 0) && (($_current_tab != 2))) {
257         echo '<input type="hidden" name="body_text" value="'.htmlspecialchars($stripslashes($_POST['body_text'])).'" />';
258         echo '<input type="hidden" name="weblink_text" value="'.htmlspecialchars($stripslashes($_POST['weblink_text'])).'" />';
259         echo '<input type="hidden" name="head" value="'.htmlspecialchars($stripslashes($_POST['head'])).'" />';
260                 echo '<input type="hidden" name="use_customized_head" value="'.(($_POST['use_customized_head']=="") ? 0 : $_POST['use_customized_head']).'" />';
261         echo '<input type="hidden" name="displayhead" id="displayhead" value="'.$_POST['displayhead'].'" />';
262         echo '<input type="hidden" name="complexeditor" id="complexeditor" value="'.$_POST['complexeditor'].'" />';
263         echo '<input type="hidden" name="formatting" value="'.$_POST['formatting'].'" />';
264         }
265
266         echo '<input type="hidden" name="ordering" value="'.$_POST['ordering'].'" />';
267         echo '<input type="hidden" name="pid" value="'.$pid.'" />';
268
269 //      echo '<input type="hidden" name="day" value="'.$_POST['day'].'" />';
270 //      echo '<input type="hidden" name="month" value="'.$_POST['month'].'" />';
271 //      echo '<input type="hidden" name="year" value="'.$_POST['year'].'" />';
272 //      echo '<input type="hidden" name="hour" value="'.$_POST['hour'].'" />';
273 //      echo '<input type="hidden" name="minute" value="'.$_POST['minute'].'" />';
274 //      echo '<input type="hidden" name="min" value="'.$_POST['min'].'" />';
275         
276         echo '<input type="hidden" name="alternatives" value="'.$_POST['alternatives'].'" />';
277         
278         echo '<input type="hidden" name="current_tab" value="'.$current_tab.'" />';
279
280 //      if (is_array($_POST['related']) && ($current_tab != 1)) {
281 //              foreach($_POST['related'] as $r_id) {
282 //                      echo '<input type="hidden" name="related[]" value="'.$r_id.'" />';
283 //              }
284 //      }
285         echo '<input type="hidden" name="keywords" value="'.htmlspecialchars(stripslashes($_POST['keywords'])).'" />';
286
287         //content test association
288         echo '<input type="hidden" name="test_message" value="'.$_POST['test_message'].'" />';
289         
290         /* get glossary terms */
291 //      $matches = find_terms(stripslashes($_POST['body_text']));
292 //      $num_terms = count($matches[0]);
293 //      $matches = $matches[0];
294 //      $word = str_replace(array('[?]', '[/?]'), '', $matches);
295 //
296 //      if (is_array($word)) {
297 //              /* update $_POST['glossary_defs'] with any new/changed terms */
298 //              for($i=0; $i<$num_terms; $i++) {
299 //                      $word[$i] = $word[$i];
300 //                      if (!isset($_POST['glossary_defs'][$word[$i]])) {
301 //                              $_POST['glossary_defs'][$word[$i]] = $glossary[$word[$i]];
302 //                      }
303 //              }
304 //      }
305 //
306 //      if (is_array($_POST['glossary_defs']) && ($current_tab != 2)) {
307 //              foreach($_POST['glossary_defs'] as $w => $d) {
308 //                      /* this term still exists in the content */
309 //                      if (!in_array($w, $word)) {
310 //                              unset($_POST['glossary_defs'][$w]);
311 //                              continue;
312 //                      }
313 //                      echo '<input type="hidden" name="glossary_defs['.$w.']" value="'.htmlspecialchars(stripslashes($d)).'" />';
314 //              }
315 //              if (isset($_POST['related_term'])) {
316 //                      foreach($_POST['related_term'] as $w => $d) {
317 //                              echo '<input type="hidden" name="related_term['.$w.']" value="'.$d.'" />';
318 //                      }
319 //              }
320 //      }
321
322         // adapted content
323         $sql = "SELECT pr.primary_resource_id, prt.type_id
324                   FROM ".TABLE_PREFIX."primary_resources pr, ".
325                          TABLE_PREFIX."primary_resources_types prt
326                  WHERE pr.content_id = ".$cid."
327                    AND pr.language_code = '".$_SESSION['lang']."'
328                    AND pr.primary_resource_id = prt.primary_resource_id";
329 //      $all_types_result = mysql_query($sql, $db);
330         $types = $dao->execute($sql);
331         
332         $i = 0;
333         if (is_array($types)) {
334                 foreach ($types as $type) {
335                         $row_alternatives['alt_'.$type['primary_resource_id'].'_'.$type['type_id']] = 1;
336                 }
337         }
338         
339         if ($current_tab != 2 && isset($_POST['use_post_for_alt']))
340         {
341                 echo '<input type="hidden" name="use_post_for_alt" value="1" />';
342                 if (is_array($_POST)) {
343                         foreach ($_POST as $alt_id => $alt_value) {
344                                 if (substr($alt_id, 0 ,4) == 'alt_'){
345                                         echo '<input type="hidden" name="'.$alt_id.'" value="'.$alt_value.'" />';
346                                 }
347                         }
348                 }
349         }
350         
351         //tests
352         if ($current_tab != 3){
353                 // set content associated tests
354                 if (isset($_POST['visited_tests'])) {
355                         echo '<input type="hidden" name="visited_tests" value="1" />'."\n";
356                         if (is_array($_POST['tid'])) {
357                                 foreach ($_POST['tid'] as $i=>$tid){
358                                         echo '<input type="hidden" name="tid['.$i.']" value="'.$tid.'" />';
359                                 }
360                         }
361                 } else {
362                         $i = 0;
363                         if (is_array($content_tests)) {
364                                 foreach ($content_tests as $content_test_row) {
365                                         echo '<input type="hidden" name="tid['.$i++.']" value="'.$content_test_row['test_id'].'" />';
366                                 }
367                         }
368                 }
369                 
370 //              // set pre-tests
371 //              if (is_array($_POST['pre_tid'])) {
372 //                      foreach ($_POST['pre_tid'] as $i=>$pre_tid){
373 //                              echo '<input type="hidden" name="pre_tid['.$i.']" value="'.$pre_tid.'" />';
374 //                      }
375 //              }
376 //              else
377 //              {
378 //                      $i = 0;
379 //                      $sql = 'SELECT * FROM '.TABLE_PREFIX."content_prerequisites WHERE content_id=$cid AND type='".CONTENT_PRE_TEST."'";
380 //                      $pretests_result = mysql_query($sql, $db);
381 //                      while ($pretest_row = mysql_fetch_assoc($pretests_result)) {
382 //                                      echo '<input type="hidden" name="pre_tid['.$i++.']" value="'.$pretest_row['item_id'].'" />';
383 //                      }
384 //              }
385         } 
386         
387         if ($do_check) {
388                 $changes_made = check_for_changes($content_row, $row_alternatives);
389         }
390 ?>
391
392 <div align="center">
393         <?php output_tabs($current_tab, $changes_made); ?>
394 </div>
395
396 <div class="input-form" style="width: 95%;">
397
398         <?php if ($changes_made): ?>
399                 <div class="unsaved">
400                         <span style="color:red;"><?php echo _AT('save_changes_unsaved'); ?></span> 
401                         <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;" /> 
402                         <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"'; } ?> />
403                         <label for="close"><?php echo _AT('close_after_saving'); ?></label>
404                 </div>
405
406         <?php else: ?>
407                 <div class="saved">
408                         <?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>
409                 </div>
410         <?php endif; ?>
411         <?php include('editor_tabs/'.$tabs[$current_tab][1]); ?>
412 </div>
413 </form>
414
415 <?php require(TR_INCLUDE_PATH.'footer.inc.php'); ?>