1. fixed function populate_a4a() to clean up alternatives from db when no resources...
[acontent.git] / docs / home / editor / editor_tab_functions.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 if (!defined('TR_INCLUDE_PATH')) { exit; }
14
15 function in_array_cin($strItem, $arItems)
16 {
17    foreach ($arItems as $key => $strValue)
18    {
19        if (strtoupper($strItem) == strtoupper($strValue))
20        {
21                    return $key;
22        }
23    }
24    return false;
25
26
27
28 function get_tabs() {
29         //these are the _AT(x) variable names and their include file
30         /* tabs[tab_id] = array(tab_name, file_name,                accesskey) */
31         $tabs[0] = array('content',                     'edit.inc.php',          'n');
32         $tabs[1] = array('metadata',                    'properties.inc.php',    'p');
33         $tabs[2] = array('alternative_content', 'alternatives.inc.php',  'l');  
34         $tabs[3] = array('tests',               'tests.inc.php',         't');  
35         
36         return $tabs;
37 }
38
39
40 function output_tabs($current_tab, $changes) {
41         global $_base_path;
42         $tabs = get_tabs();
43         $num_tabs = count($tabs);
44 ?>
45         <table class="etabbed-table" border="0" cellpadding="0" cellspacing="0" width="95%">
46         <tr>            
47                 <?php 
48                 for ($i=0; $i < $num_tabs; $i++): 
49                         if ($current_tab == $i):?>
50                                 <td class="editor_tab_selected">
51                                         <?php if ($changes[$i]): ?>
52                                                 <img src="<?php echo $_base_path; ?>images/changes_bullet.gif" alt="<?php echo _AT('usaved_changes_made'); ?>" height="12" width="15" />
53                                         <?php endif; ?>
54                                         <?php echo _AT($tabs[$i][0]); ?>
55                                 </td>
56                                 <td class="tab-spacer">&nbsp;</td>
57                         <?php else: ?>
58                                 <td class="editor_tab">
59                                         <?php if ($changes[$i]): ?>
60                                                 <img src="<?php echo $_base_path; ?>images/changes_bullet.gif" alt="<?php echo _AT('usaved_changes_made'); ?>" height="12" width="15" />
61                                         <?php endif; ?>
62
63                                         <?php echo '<input type="submit" name="button_'.$i.'" value="'._AT($tabs[$i][0]).'" title="'._AT($tabs[$i][0]).' - alt '.$tabs[$i][2].'" class="editor_buttontab" accesskey="'.$tabs[$i][2].'" onmouseover="this.style.cursor=\'pointer\';" '.$clickEvent.' />'; ?>
64                                 </td>
65                                 <td class="tab-spacer">&nbsp;</td>
66                         <?php endif; ?>
67                 <?php endfor; ?>
68                 <td >&nbsp;</td>
69         </tr>
70         </table>
71 <?php }
72 /**
73  * Strips all tags and encodes special characters in the URL
74  * Returns false if the URL is invalid
75  * 
76  * @param string $url
77  * @return mixed - returns a stripped and encoded URL or false if URL is invalid
78  */
79 function isValidURL($url) {
80     if (substr($url,0,4) === 'http') {
81         return filter_var(filter_var($url, FILTER_SANITIZE_STRING), FILTER_VALIDATE_URL);
82     }
83     return false;
84 }
85
86 /*
87  * Parse the primary resources out of the content and save into db.
88  * Clean up the removed primary resources from db.
89  * @param: $cid: content id
90  * @param: $content
91  * @return: none
92  */
93 function populate_a4a($cid, $content, $formatting){
94         global $my_files, $content_base_href, $contentManager;
95         
96     include_once(TR_INCLUDE_PATH.'classes/A4a/A4a.class.php');
97         include_once(TR_INCLUDE_PATH.'classes/XML/XML_HTMLSax/XML_HTMLSax.php');        /* for XML_HTMLSax */
98         include_once(TR_INCLUDE_PATH.'classes/ContentOutputParser.class.php');  /* for parser */
99         
100         // initialize content_base_href; used in format_content
101         if (!isset($content_base_href)) {
102                 $result = $contentManager->getContentPage($cid);
103                 // return if the cid is not found
104                 if (!($content_row = @mysql_fetch_assoc($result))) return;
105                 $content_base_href = $content_row["content_path"].'/';
106         }
107         
108         $body = ContentUtility::formatContent($content, $formatting);
109     
110         $handler = new ContentOutputParser();
111         $parser = new XML_HTMLSax();
112         $parser->set_object($handler);
113         $parser->set_element_handler('openHandler','closeHandler');
114         
115         $my_files               = array();
116         $parser->parse($body);
117         $my_files = array_unique($my_files);
118         
119         foreach ($my_files as $file) {
120                 /* filter out full urls */
121                 $url_parts = @parse_url($file);
122                 
123                 // file should be relative to content
124                 if ((substr($file, 0, 1) == '/')) {
125                         continue;
126                 }
127                 
128                 // The URL of the movie from youtube.com has been converted above in embed_media().
129                 // For example:  http://www.youtube.com/watch?v=a0ryB0m0MiM is converted to
130                 // http://www.youtube.com/v/a0ryB0m0MiM to make it playable. This creates the problem
131                 // that the parsed-out url (http://www.youtube.com/v/a0ryB0m0MiM) does not match with
132                 // the URL saved in content table (http://www.youtube.com/watch?v=a0ryB0m0MiM).
133                 // The code below is to convert the URL back to original.
134                 $file = ContentUtility::convertYoutubePlayURLToWatchURL($file);
135                 
136                 $resources[] = convertAmp($file);  // converts & to &amp;
137         }
138     
139     $a4a = new A4a($cid);
140     $db_primary_resources = $a4a->getPrimaryResources();
141     
142     // clean up the removed resources
143     foreach ($db_primary_resources  as $primary_rid=>$db_resource){
144         //if this file from our table is not found in the $resource, then it's not used.
145         if(count($resources) == 0 || !in_array($db_resource['resource'], $resources)){
146                 $a4a->deletePrimaryResource($primary_rid);
147         }
148     }
149     
150     if (count($resources) == 0) return;
151
152         // insert the new resources
153     foreach($resources as $primary_resource)
154         {
155                 if (!$a4a->getPrimaryResourceByName($primary_resource)){
156                         $a4a->setPrimaryResource($cid, $primary_resource, $_SESSION['lang']);
157                 }
158         }
159 }
160
161 // save all changes to the DB
162 function save_changes($redir, $current_tab) {
163         global $contentManager, $addslashes, $msg, $_course_id, $_content_id;
164         
165         $_POST['pid']   = intval($_POST['pid']);
166         $_POST['_cid']  = intval($_POST['_cid']);
167         
168         $_POST['alternatives'] = intval($_POST['alternatives']);
169         
170         $_POST['title'] = trim($_POST['title']);
171         $_POST['head']  = trim($_POST['head']);
172         $_POST['use_customized_head']   = isset($_POST['use_customized_head'])?$_POST['use_customized_head']:0;
173         $_POST['body_text']     = trim($_POST['body_text']);
174         $_POST['weblink_text'] = trim($_POST['weblink_text']);
175         $_POST['formatting'] = intval($_POST['formatting']);
176         $_POST['keywords']      = trim($_POST['keywords']);
177         $_POST['test_message'] = trim($_POST['test_message']);
178
179         //if weblink is selected, use it
180         if ($_POST['formatting']==CONTENT_TYPE_WEBLINK) {
181             $url = $_POST['weblink_text'];
182             $validated_url = isValidURL($url);
183         if (!validated_url || $validated_url !== $url) {
184                $msg->addError(array('INVALID_INPUT', _AT('weblink')));
185             } else {
186                     $_POST['body_text'] = $url;
187                     $content_type_pref = CONTENT_TYPE_WEBLINK;
188             }
189         } else {
190                 $content_type_pref = CONTENT_TYPE_CONTENT;
191         }
192
193         /*if (!($release_date = generate_release_date())) {
194                 $msg->addError('BAD_DATE');
195         }*/
196
197 //      if ($_POST['title'] == '') {
198 //              $msg->addError(array('EMPTY_FIELDS', _AT('title')));
199 //      }
200                 
201 //      if (!$msg->containsErrors()) {
202         $orig_body_text = $_POST['body_text'];  // used to populate a4a tables
203 //              $_POST['title']                 = $addslashes($_POST['title']);
204 //              $_POST['body_text']             = $addslashes($_POST['body_text']);
205 //              $_POST['head']                  = $addslashes($_POST['head']);
206 //              $_POST['keywords']              = $addslashes($_POST['keywords']);
207 //              $_POST['test_message']  = $addslashes($_POST['test_message']);          
208
209                 // add or edit content
210                 if ($_POST['_cid']) {
211                         /* editing an existing page */
212                         $err = $contentManager->editContent($_POST['_cid'], $_POST['title'], $_POST['body_text'], 
213                                                             $_POST['keywords'], $_POST['formatting'], 
214                                                             $_POST['head'], $_POST['use_customized_head'], 
215                                                             $_POST['test_message']);
216                         $cid = $_POST['_cid'];
217                 } else {
218                         /* insert new */
219                         $cid = $contentManager->addContent($_course_id,
220                                                                                                   $_POST['pid'],
221                                                                                                   $_POST['ordering'],
222                                                                                                   $_POST['title'],
223                                                                                                   $_POST['body_text'],
224                                                                                                   $_POST['keywords'],
225                                                                                                   $_POST['related'],
226                                                                                                   $_POST['formatting'],
227                                                                                                   $_POST['head'],
228                                                                                                   $_POST['use_customized_head'],
229                                                                                                   $_POST['test_message'],
230                                                                                                   $content_type_pref);
231                         $_POST['_cid']    = $cid;
232                         $_REQUEST['_cid'] = $cid;
233                 }
234         // re-populate a4a tables based on the new content
235                 populate_a4a($cid, $orig_body_text, $_POST['formatting']);
236                 if ($cid == 0) return;
237 //      }
238
239         /* insert glossary terms */
240         /*
241         if (is_array($_POST['glossary_defs']) && ($num_terms = count($_POST['glossary_defs']))) {
242                 global $glossary, $glossary_ids, $msg;
243
244                 foreach($_POST['glossary_defs'] as $w => $d) {
245                         $old_w = $w;
246                         $key = in_array_cin($w, $glossary_ids);
247                         $w = urldecode($w);
248                         $d = $addslashes($d);
249
250                         if (($key !== false) && (($glossary[$old_w] != $d) || isset($_POST['related_term'][$old_w])) ) {
251                                 $w = addslashes($w);
252                                 $related_id = intval($_POST['related_term'][$old_w]);
253                                 $sql = "UPDATE ".TABLE_PREFIX."glossary SET definition='$d', related_word_id=$related_id WHERE word_id=$key AND course_id=$_SESSION[course_id]";
254                                 $result = mysql_query($sql, $db);
255                                 $glossary[$old_w] = $d;
256                         } else if ($key === false && ($d != '')) {
257                                 $w = addslashes($w);
258                                 $related_id = intval($_POST['related_term'][$old_w]);
259                                 $sql = "INSERT INTO ".TABLE_PREFIX."glossary VALUES (NULL, $_SESSION[course_id], '$w', '$d', $related_id)";
260
261                                 $result = mysql_query($sql, $db);
262                                 $glossary[$old_w] = $d;
263                         }
264                 }
265         }*/
266         if (isset($_GET['tab'])) {
267                 $current_tab = intval($_GET['tab']);
268         }
269         if (isset($_POST['current_tab'])) {
270                 $current_tab = intval($_POST['current_tab']);
271         }
272
273         // adapted content: save primary content type
274         if (isset($_POST['use_post_for_alt']))
275         {
276                 include_once(TR_INCLUDE_PATH.'classes/DAO/PrimaryResourcesTypesDAO.class.php');
277                 $primaryResourcesTypesDAO = new PrimaryResourcesTypesDAO();
278                 
279                 // 1. delete old primary content type
280                 $sql = "DELETE FROM ".TABLE_PREFIX."primary_resources_types
281                          WHERE primary_resource_id in 
282                                (SELECT DISTINCT primary_resource_id 
283                                   FROM ".TABLE_PREFIX."primary_resources
284                                  WHERE content_id=".$cid."
285                                    AND language_code='".$_SESSION['lang']."')";
286                 $primaryResourcesTypesDAO->execute($sql);
287                 
288                 // 2. insert the new primary content type
289                 $sql = "SELECT pr.primary_resource_id, rt.type_id
290                           FROM ".TABLE_PREFIX."primary_resources pr, ".
291                                  TABLE_PREFIX."resource_types rt
292                          WHERE pr.content_id = ".$cid."
293                            AND pr.language_code = '".$_SESSION['lang']."'";
294                 $all_types_rows = $primaryResourcesTypesDAO->execute($sql);
295                 
296                 if (is_array($all_types_rows)) {
297                         foreach ($all_types_rows as $type) {
298                                 if (isset($_POST['alt_'.$type['primary_resource_id'].'_'.$type['type_id']]))
299                                 {
300                                         $primaryResourcesTypesDAO->Create($type['primary_resource_id'], $type['type_id']);
301 //                                      $sql = "INSERT INTO ".TABLE_PREFIX."primary_resources_types (primary_resource_id, type_id)
302 //                                              VALUES (".$type['primary_resource_id'].", ".$type['type_id'].")";
303 //                                      $result = mysql_query($sql, $db);
304                                 }
305                         }
306                 }
307         }
308         
309         include_once(TR_INCLUDE_PATH.'classes/DAO/ContentTestsAssocDAO.class.php');
310         $contentTestsAssocDAO = new ContentTestsAssocDAO();
311         $test_rows = $contentTestsAssocDAO->getByContent($_POST['_cid']);
312 //      $sql = 'SELECT * FROM '.TABLE_PREFIX."content_tests_assoc WHERE content_id=$_POST[cid]";
313 //      $result = mysql_query($sql, $db);
314         $db_test_array = array();
315         if (is_array($test_rows)) {
316                 foreach ($test_rows as $row) {
317                         $db_test_array[] = $row['test_id'];
318                 }
319         }
320
321         if (is_array($_POST['tid']) && sizeof($_POST['tid']) > 0){
322                 $toBeDeleted = array_diff($db_test_array, $_POST['tid']);
323                 $toBeAdded = array_diff($_POST['tid'], $db_test_array);
324                 //Delete entries
325                 if (!empty($toBeDeleted)){
326                         $tids = implode(",", $toBeDeleted);
327                         $sql = 'DELETE FROM '. TABLE_PREFIX . "content_tests_assoc WHERE content_id=$_POST[cid] AND test_id IN ($tids)";
328                         $contentTestsAssocDAO->execute($sql);
329                 }
330         
331                 //Add entries
332                 if (!empty($toBeAdded)){
333                         foreach ($toBeAdded as $i => $tid){
334                                 $tid = intval($tid);
335 //                              $sql = 'INSERT INTO '. TABLE_PREFIX . "content_tests_assoc SET content_id=$_POST[cid], test_id=$tid";
336 //                              $result = mysql_query($sql, $db);
337                                 if ($contentTestsAssocDAO->Create($_POST['_cid'], $tid) === false){
338                                         $msg->addError('DB_NOT_UPDATED');
339                                 }
340                         }
341                 }
342         } else {
343                 //All tests has been removed.
344                 $contentTestsAssocDAO->DeleteByContentID($_POST['_cid']);
345 //              $sql = 'DELETE FROM '. TABLE_PREFIX . "content_tests_assoc WHERE content_id=$_POST[cid]";
346 //              $result = mysql_query($sql, $db);
347         }
348         //End Add test
349
350         //TODO*******************BOLOGNA****************REMOVE ME**************/
351 /*
352         if(isset($_SESSION['associated_forum']) && !$msg->containsErrors()){
353                 if($_SESSION['associated_forum']=='none'){
354                         $sql = "DELETE FROM ".TABLE_PREFIX."content_forums_assoc WHERE content_id='$_POST[cid]'";
355                         mysql_query($sql,$db);
356                 } else {
357                         $sql = "DELETE FROM ".TABLE_PREFIX."content_forums_assoc WHERE content_id='$_POST[cid]'";
358                         mysql_query($sql,$db);
359                         $associated_forum = $_SESSION['associated_forum'];
360                         for($i=0; $i<count($associated_forum); $i++){
361                                 $sql="INSERT INTO ".TABLE_PREFIX."content_forums_assoc SET content_id='$_POST[cid]',forum_id='$associated_forum[$i]'";
362                                 mysql_query($sql,$db);
363                         }
364                 }
365                 unset($_SESSION['associated_forum']);
366         }
367 */
368         if (!$msg->containsErrors() && $redir) {
369                 $_SESSION['save_n_close'] = $_POST['save_n_close'];
370                 
371                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
372                 header('Location: '.basename($_SERVER['PHP_SELF']).'?_cid='.$cid.SEP.'close='.$addslashes($_POST['save_n_close']).SEP.'tab='.$addslashes($_POST['current_tab']).SEP.'displayhead='.$addslashes($_POST['displayhead']).SEP.'alternatives='.$addslashes($_POST['alternatives']));
373                 exit;
374         } else {
375                 return;
376         }
377 }
378 /*
379 function generate_release_date($now = false) {
380         if ($now) {
381                 $day  = date('d');
382                 $month= date('m');
383                 $year = date('Y');
384                 $hour = date('H');
385                 $min  = 0;
386         } else {
387                 $day    = intval($_POST['day']);
388                 $month  = intval($_POST['month']);
389                 $year   = intval($_POST['year']);
390                 $hour   = intval($_POST['hour']);
391                 $min    = intval($_POST['min']);
392         }
393
394         if (!checkdate($month, $day, $year)) {
395                 return false;
396         }
397
398         if (strlen($month) == 1){
399                 $month = "0$month";
400         }
401         if (strlen($day) == 1){
402                 $day = "0$day";
403         }
404         if (strlen($hour) == 1){
405                 $hour = "0$hour";
406         }
407         if (strlen($min) == 1){
408                 $min = "0$min";
409         }
410         $release_date = "$year-$month-$day $hour:$min:00";
411         
412         return $release_date;
413 }
414 */
415 function check_for_changes($row, $row_alternatives) {
416         global $contentManager, $cid, $glossary, $glossary_ids_related, $addslashes;
417
418         $changes = array();
419
420         if ($row && strcmp(trim($addslashes($_POST['title'])), addslashes($row['title']))) {
421                 $changes[0] = true;
422         } else if (!$row && $_POST['title']) {
423                 $changes[0] = true;
424         }
425
426         if ($row && strcmp($addslashes(trim($_POST['head'])), trim(addslashes($row['head'])))) {
427                 $changes[0] = true;
428         } else if (!$row && $_POST['head']) {
429                 $changes[0] = true;
430         }
431
432         if ($row && strcmp($addslashes(trim($_POST['body_text'])), trim(addslashes($row['text'])))) {
433                 $changes[0] = true;
434         } else if (!$row && $_POST['body_text']) {
435                 $changes[0] = true;
436         }
437         
438     if ($row && strcmp($addslashes(trim($_POST['weblink_text'])), trim(addslashes($row['text'])))) {
439         $changes[0] = true;
440     } else if (!$row && $_POST['weblink_text']) {
441         $changes[0] = true;
442     }
443
444         /* use customized head: */
445         if ($row && isset($_POST['use_customized_head']) && ($_POST['use_customized_head'] != $row['use_customized_head'])) {
446                 $changes[0] = true;
447         }
448
449         /* formatting: */
450         if ($row && strcmp(trim($_POST['formatting']), $row['formatting'])) {
451                 $changes[0] = true;
452         } else if (!$row && $_POST['formatting']) {
453                 $changes[0] = true;
454         }
455
456         /* release date: */
457 //      if ($row && strcmp(substr(generate_release_date(), 0, -2), substr($row['release_date'], 0, -2))) {
458 //              /* the substr was added because sometimes the release_date in the db has the seconds field set, which we dont use */
459 //              /* so it would show a difference, even though it should actually be the same, so we ignore the seconds with the -2 */
460 //              /* the seconds gets added if the course was created during the installation process. */
461 //              $changes[1] = true;
462 //      } else if (!$row && strcmp(generate_release_date(), generate_release_date(true))) {
463 //              $changes[1] = true;
464 //      }
465
466         /* related content: */
467 //      $row_related = $contentManager->getRelatedContent($cid);
468 //
469 //      if (is_array($_POST['related']) && is_array($row_related)) {
470 //              $sum = array_sum(array_diff($_POST['related'], $row_related));
471 //              $sum += array_sum(array_diff($row_related, $_POST['related']));
472 //              if ($sum > 0) {
473 //                      $changes[1] = true;
474 //              }
475 //      } else if (!is_array($_POST['related']) && !empty($row_related)) {
476 //              $changes[1] = true;
477 //      }
478
479         /* keywords */
480         if ($row && strcmp(trim($_POST['keywords']), $row['keywords'])) {
481                 $changes[1] = true;
482         }  else if (!$row && $_POST['keywords']) {
483                 $changes[1] = true;
484         }
485
486
487         /* glossary */
488 //      if (is_array($_POST['glossary_defs'])) {
489 //              global $glossary_ids;
490 //              foreach ($_POST['glossary_defs'] as $w => $d) {
491 //
492 //                      $key = in_array_cin($w, $glossary_ids);
493 //                      if ($key === false) {
494 //                              /* new term */
495 //                              $changes[2] = true;
496 //                              break;
497 //                      } else if ($cid && ($d &&($d != $glossary[$glossary_ids[$key]]))) {
498 //                              /* changed term */
499 //                              $changes[2] = true;
500 //                              break;
501 //                      }
502 //              }
503 //
504 //              if (is_array($_POST['related_term'])) {
505 //                      foreach($_POST['related_term'] as $term => $r_id) {
506 //                              if ($glossary_ids_related[$term] != $r_id) {
507 //                                      $changes[2] = true;
508 //                                      break;
509 //                              }
510 //                      }
511 //              }
512 //      }
513
514         /* adapted content */
515         if (isset($_POST['use_post_for_alt']))
516         {
517                 foreach ($_POST as $alt_id => $alt_value) {
518                         if (substr($alt_id, 0 ,4) == 'alt_' && $alt_value != $row_alternatives[$alt_id]){
519                                 $changes[2] = true;
520                                 break;
521                         }
522                 }
523         }
524         
525         /* test & survey */     
526         if ($row && isset($_POST['test_message']) && $_POST['test_message'] != $row['test_message']){
527                 $changes[3] = true;
528         }
529         
530         $content_tests = $contentManager->getContentTestsAssoc($cid);
531         
532         if (isset($_POST['visited_tests'])) {
533                 if (!is_array($content_tests) && is_array($_POST['tid'])) {
534                         $changes[3] = true;
535                 }
536                 if (is_array($content_tests)) {
537                         for ($i = 0; $i < count($content_tests); $i++) {
538                                 if ($content_tests[$i]['test_id'] <> $_POST['tid'][$i]) {
539                                         $changes[3] = true;
540                                         break;
541                                 }
542                         }
543                 }
544         }
545
546         return $changes;
547 }
548
549 function paste_from_file() {
550         global $msg;
551         
552         include_once(TR_INCLUDE_PATH.'../home/classes/ContentUtility.class.php');
553         if ($_FILES['uploadedfile_paste']['name'] == '')        {
554                 $msg->addError('FILE_NOT_SELECTED');
555                 return;
556         }
557         if ($_FILES['uploadedfile_paste']['name']
558                 && (($_FILES['uploadedfile_paste']['type'] == 'text/plain')
559                         || ($_FILES['uploadedfile_paste']['type'] == 'text/html')) )
560                 {
561
562                 $path_parts = pathinfo($_FILES['uploadedfile_paste']['name']);
563                 $ext = strtolower($path_parts['extension']);
564
565                 if (in_array($ext, array('html', 'htm'))) {
566                         $_POST['body_text'] = file_get_contents($_FILES['uploadedfile_paste']['tmp_name']);
567
568                         /* get the <title></title> of this page                         */
569
570                         $start_pos      = strpos(strtolower($_POST['body_text']), '<title>');
571                         $end_pos        = strpos(strtolower($_POST['body_text']), '</title>');
572
573                         if (($start_pos !== false) && ($end_pos !== false)) {
574                                 $start_pos += strlen('<title>');
575                                 $_POST['title'] = trim(substr($_POST['body_text'], $start_pos, $end_pos-$start_pos));
576                         }
577                         unset($start_pos);
578                         unset($end_pos);
579
580                         $_POST['head'] = ContentUtility::getHtmlHeadByTag($_POST['body_text'], array("link", "style", "script")); 
581                         if (strlen(trim($_POST['head'])) > 0)   
582                                 $_POST['use_customized_head'] = 1;
583                         else
584                                 $_POST['use_customized_head'] = 0;
585                         
586                         $_POST['body_text'] = ContentUtility::getHtmlBody($_POST['body_text']); 
587
588                         $msg->addFeedback('FILE_PASTED');
589                 } else if ($ext == 'txt') {
590                         $_POST['body_text'] = file_get_contents($_FILES['uploadedfile_paste']['tmp_name']);
591                         //LAW
592 //                      debug($_POST);
593                         $msg->addFeedback('FILE_PASTED');
594
595                 }
596         } else {
597                 $msg->addError('BAD_FILE_TYPE');
598         }
599
600         return;
601 }
602
603 //for accessibility checker
604 function write_temp_file() {
605         global $_POST, $msg;
606
607         if (defined('TR_FORCE_GET_FILE') && TR_FORCE_GET_FILE) {
608                 $content_base = 'get.php/';
609         } else {
610                 $content_base = 'content/' . $_SESSION['course_id'] . '/';
611         }
612
613         if ($_POST['content_path']) {
614                 $content_base .= $_POST['content_path'] . '/';
615         }
616
617         $file_name = $_POST['_cid'].'.html';
618
619         if ($handle = fopen(TR_CONTENT_DIR . $file_name, 'wb+')) {
620 //              $temp_content = '<h2>'.TR_print(stripslashes($_POST['title']), 'content.title').'</h2>';
621 //
622 //              if ($_POST['body_text'] != '') {
623 //                      $temp_content .= format_content(stripslashes($_POST['body_text']), $_POST['formatting'], $_POST['glossary_defs']);
624 //              }
625 //              $temp_title = $_POST['title'];
626 //
627 //              $html_template = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
628 //                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
629 //              <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
630 //              <head>
631 //                      <base href="{BASE_HREF}" />
632 //                      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
633 //                      <title>{TITLE}</title>
634 //                      <meta name="Generator" content="ATutor accessibility checker file - can be deleted">
635 //              </head>
636 //              <body>
637 //              {CONTENT}
638 //              </body>
639 //              </html>';
640 //
641 //              $page_html = str_replace(       array('{BASE_HREF}', '{TITLE}', '{CONTENT}'),
642 //                                                                      array($content_base, $temp_title, $temp_content),
643 //                                                                      $html_template);
644                 
645                 if (!@fwrite($handle, stripslashes($_POST['body_text']))) {
646                         $msg->addError('FILE_NOT_SAVED');       
647            }
648         } else {
649                 $msg->addError('FILE_NOT_SAVED');
650         }
651         $msg->printErrors();
652 }
653 ?>