dokeos-learnpath-openofficedocument
[ class tree: dokeos-learnpath-openofficedocument ] [ index: dokeos-learnpath-openofficedocument ] [ all elements ]

Source for file openoffice_text_document.class.php

Documentation is available at openoffice_text_document.class.php

  1. <?php //$id:$
  2. /**
  3.  * Defines the OpenOfficeDocument class, which is meant as a conversion
  4.  * tool from Office text documents (.doc, .sxw, .odt, .docx) to
  5.  * learning paths
  6.  * @package dokeos.learnpath.openofficedocument
  7.  * @author  Eric Marguin <eric.marguin@dokeos.com>
  8.  * @license GNU/GPL - See Doke
  9. /**
  10.  *  Defines the "OpenOfficeTextDocument" child of class "learnpath"
  11.  * @package dokeos.learnpath.openofficedocument
  12.  */
  13. require_once('openoffice_document.class.php');
  14.  
  15.     
  16.  
  17.     public $split_steps;
  18.     
  19.     /**
  20.      * Class constructor. Calls the parent class and initialises the local attribute split_steps
  21.      * @param    boolean    Whether to split steps (true) or make one large page (false)
  22.      * @param    string    Course code
  23.      * @param    integer    Resource ID
  24.      * @param    integer Creator user id
  25.      * @return    void 
  26.      */
  27.     function OpenofficeTextDocument($split_steps=false$course_code=null$resource_id=null,$user_id=null)
  28.     {
  29.         
  30.         $this -> split_steps = $split_steps;
  31.         parent::OpenofficeDocument($course_code$resource_id$user_id);
  32.         
  33.     }
  34.     
  35.     /**
  36.      * Gets html pages and compose them into a learning path
  37.      * @param    array    The files that will compose the generated learning path. Unused so far.
  38.      * @return    boolean    False if file does not exit. Nothing otherwise.
  39.      */
  40.     function make_lp($files=array())
  41.     {
  42.         
  43.         global $_course;
  44.         // we get a content where ||page_break|| indicates where the page is broken
  45.         if(!file_exists($this->base_work_dir.'/'.$this->created_dir.'/'.$this->file_name.'.html')){return false;}
  46.         $content file_get_contents($this->base_work_dir.'/'.$this->created_dir.'/'.$this->file_name.'.html');    
  47.         
  48.         
  49.         unlink($this->base_work_dir.'/'.$this->file_path);
  50.         unlink($this->base_work_dir.'/'.$this->created_dir.'/'.$this->file_name.'.html');
  51.         
  52.         // the file is utf8 encoded and it seems to make problems with special quotes. 
  53.         // then we htmlentities that, we replace these quotes and html_entity_decode that in good charset
  54.         $charset api_get_setting('platform_charset');
  55.         $content htmlentities($content,ENT_COMPAT,$this->original_charset)
  56.         $content str_replace('&rsquo;','\'',$content);
  57.         $content mb_convert_encoding($content$charset$this->original_charset);
  58.         $content str_replace($this->original_charset,$charset,$content);
  59.         $content html_entity_decode($content);
  60.         
  61.         // set the path to pictures to absolute (so that it can be modified in fckeditor)
  62.         $content preg_replace("|src=\"([^\"]*)|i""src=\"".api_get_path(REL_COURSE_PATH).$_course['path'].'/document'.$this->created_dir."/\\1"$content);
  63.         
  64.         list($header$bodyexplode('<BODY',$content);
  65.  
  66.         $body '<BODY'.$body;        
  67.  
  68.         // remove font-family styles
  69.         $header preg_replace("|font\-family[^;]*;|i""",$header);
  70.         
  71.         // dokeos styles
  72.         $my_style api_get_setting('stylesheets');
  73.         if(empty($my_style)){$my_style 'dokeos_classic';}
  74.         $style_to_import "<style type=\"text/css\">\r\n";
  75.         $style_to_import .= '@import "'.api_get_path(WEB_CODE_PATH).'css/'.$my_style.'/default.css";'."\n";
  76.         $style_to_import .= '@import "'.api_get_path(WEB_CODE_PATH).'css/'.$my_style.'/course.css";'."\n";
  77.         $style_to_import .= "</style>\r\n";        
  78.         $header preg_replace("|</head>|i""\r\n$style_to_import\r\n\\0",$header);
  79.         
  80.         // line break before and after picture
  81.         $header str_replace('p {','p {clear:both;',$header);
  82.         
  83.         $header str_replace('absolute','relative',$header);
  84.         
  85.         switch($this->split_steps)
  86.         {
  87.             case 'per_page' $this -> dealPerPage($header,$body)break;
  88.             case 'per_chapter' $this -> dealPerChapter($header,$body)break;
  89.         }
  90.         
  91.     }
  92.     
  93.     /**
  94.      * Manages chapter splitting
  95.      * @param    string    Chapter header
  96.      * @param    string    Content
  97.      * @return    void 
  98.      */
  99.     function dealPerChapter($header$content)
  100.     {
  101.         
  102.         global $_course;
  103.         
  104.         $content str_replace('||page_break||','',$content);
  105.         
  106.         // get all the h1
  107.         preg_match_all("|<h1[^>]*>([^(h1)+]*)</h1>|is",$content,$matches_temp);
  108.         
  109.         
  110.         //empty the fake chapters
  111.         $new_index 0;
  112.         for ($i=$i<count($matches_temp[0]$i++{
  113.  
  114.             if(trim($matches_temp[1][$i])!=='')
  115.             {
  116.                 $matches[0][$new_index$matches_temp[0][$i];
  117.                 $matches[1][$new_index$matches_temp[1][$i];
  118.                 $new_index++;
  119.             }
  120.             
  121.         }
  122.         
  123.         // add intro item
  124.         $intro_content substr($content0strpos($content$matches[0][0]));
  125.         $items_to_create[get_lang('Introduction')$intro_content;
  126.  
  127.         
  128.         for ($i=$i<count($matches[0]$i++{
  129.             
  130.             if(empty($matches[1][$i]))
  131.                 continue;
  132.             
  133.             $content strstr($content,$matches[0][$i]);
  134.             if($i+1!==count($matches[0]))
  135.             {
  136.                 $chapter_content substr($content0strpos($content$matches[0][$i+1]));
  137.             }
  138.             else
  139.             {
  140.                 $chapter_content $content;
  141.             }
  142.             $items_to_create[$matches[1][$i]] $chapter_content;
  143.             
  144.         }
  145.         
  146.         $i 0;
  147.         foreach($items_to_create as $item_title=>$item_content)
  148.         {
  149.             $i++;
  150.             $page_content $this->format_page_content($header$item_content);
  151.             
  152.             $html_file $this->created_dir.'-'.$i.'.html';
  153.             $handle fopen($this->base_work_dir.$this->created_dir.'/'.$html_file,'w+');
  154.             fwrite($handle$page_content);
  155.             fclose($handle);
  156.             
  157.             $document_id add_document($_course,$this->created_dir.'/'.$html_file,'file',filesize($this->base_work_dir.$this->created_dir.'/'.$html_file),$html_file);
  158.         
  159.             if ($document_id){    
  160.                             
  161.                 //put the document in item_property update
  162.                 api_item_property_update($_course,TOOL_DOCUMENT,$document_id,'DocumentAdded',$_SESSION['_uid'],0,0);
  163.                 
  164.                 $infos pathinfo($this->filepath);
  165.                 $slide_name strip_tags(nl2br($item_title));
  166.                 $slide_name str_replace(array("\r\n""\r""\n")""$slide_name);
  167.                 $slide_name html_entity_decode($slide_name);
  168.                 $previous learnpath::add_item(0$previous'document'$document_id$slide_name'');
  169.                 if($this->first_item == 0){
  170.                     $this->first_item = $previous;
  171.                 }
  172.             }
  173.         }
  174.         
  175.     }
  176.     
  177.     /**
  178.      * Manages page splitting
  179.      * @param    string    Page header
  180.      * @param    string    Page body
  181.      * @return    void 
  182.      */
  183.     function dealPerPage($header,$body)
  184.     {
  185.         global $_course;
  186.         // split document to pages
  187.         $pages explode('||page_break||',$body);        
  188.         
  189.         $first_item 0;
  190.         
  191.         foreach($pages as $key=>$page_content)
  192.         // for every pages, we create a new file
  193.             
  194.             $key +=1;
  195.             
  196.             $page_content $this->format_page_content($header$page_content$this->base_work_dir.$this->created_dir);
  197.             $html_file $this->created_dir.'-'.$key.'.html';
  198.             $handle fopen($this->base_work_dir.$this->created_dir.'/'.$html_file,'w+');
  199.             fwrite($handle$page_content);
  200.             fclose($handle);
  201.             
  202.             $document_id add_document($_course,$this->created_dir.$html_file,'file',filesize($this->base_work_dir.$this->created_dir.$html_file),$html_file);
  203.         
  204.             if ($document_id){    
  205.                             
  206.                 //put the document in item_property update
  207.                 api_item_property_update($_course,TOOL_DOCUMENT,$document_id,'DocumentAdded',$_SESSION['_uid'],0,0);
  208.                 
  209.                 $infos pathinfo($this->filepath);
  210.                 $slide_name 'Page '.str_repeat('0',2-strlen($key)).$key;
  211.                 $previous learnpath::add_item(0$previous'document'$document_id$slide_name'');
  212.                 if($this->first_item == 0){
  213.                     $this->first_item = $previous;
  214.                 }
  215.             }
  216.         }
  217.             
  218.     }
  219.     
  220.     /**
  221.      * Returns additional Java command parameters
  222.      * @return    string    The additional parameters to be used in the Java call
  223.      */
  224.     function add_command_parameters(){
  225.         return ' -d woogie "'.$this->base_work_dir.'/'.$this->file_path.'"  "'.$this->base_work_dir.$this->created_dir.'/'.$this->file_name.'.html"';
  226.     }
  227.     
  228.     /**
  229.      * Formats a page content by reorganising the HTML code a little
  230.      * @param    string    Page header
  231.      * @param    string    Page content
  232.      * @return    string    Formatted page content
  233.      */
  234.     function format_page_content($header$content)
  235.     {
  236.         
  237.         
  238.         // limit the width of the doc
  239.         list($max_width$max_heightexplode('x',api_get_setting('service_ppt2lp','size'));
  240.         
  241.         $content preg_replace("|<body[^>]*>|i","\\0\r\n<div style=\"width:".$max_width."\">",$content-1,$count);
  242.         if($count 1)
  243.         {
  244.             $content '<body><div style="width:'.$max_width.'">'.$content;
  245.         }
  246.         
  247.         $content preg_replace('|</body>|i','</div>\\0',$content-1$count);
  248.         if($count 1)
  249.         {
  250.             $content $content.'</div></body>';
  251.         }
  252.         
  253.         // add the headers
  254.         $content $header.$content;
  255.         
  256.         
  257.         // resize all the picture to the max_width-10
  258.         preg_match_all("|<img[^src]*src=\"([^\"]*)\"[^>]*>|i",$content,$images);
  259.         
  260.         foreach ($images[1as $key => $image)
  261.         {
  262.             // check if the <img tag soon has a width attribute
  263.             $defined_width preg_match("|width=([^\s]*)|i",$images[0][$key]$img_width);
  264.             $img_width $img_width[1];
  265.             if(!$defined_width)
  266.             {
  267.             
  268.                 list($img_width$img_height$typegetimagesize($this->base_work_dir.$this->created_dir.'/'.$image);
  269.                 
  270.                 $new_width $max_width-10;
  271.                 if($img_width $new_width)
  272.                 {
  273.                     $picture_resized str_ireplace('<img','<img width="'.$new_width.'" ',$images[0][$key]);
  274.                     $content str_replace($images[0][$key],$picture_resized,$content);
  275.                 }
  276.                 
  277.             }
  278.             else if($img_width $max_width-10)
  279.             {
  280.                 $picture_resized str_ireplace('width='.$img_width,'width="'.($max_width-10).'"',$images[0][$key]);
  281.                 $content str_replace($images[0][$key],$picture_resized,$content);
  282.             }
  283.         }
  284.         
  285.         
  286.         return $content;
  287.         
  288.     }
  289.     /**
  290.      * Add documents to the visioconference (to be implemented)
  291.      */
  292.     function add_docs_to_visio (){
  293.               
  294.     }
  295. }
  296. ?>

Documentation generated on Thu, 12 Jun 2008 14:09:40 -0500 by phpDocumentor 1.4.1