openoffice_text_document.class.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php //$id:$
  2. /**
  3. * Defines the AICC class, which is meant to contain the aicc items (nuclear elements)
  4. * @package dokeos.learnpath.aicc
  5. * @author Yannick Warnier <ywarnier@beeznest.org>
  6. * @license GNU/GPL - See Dokeos license directory for details
  7. */
  8. /**
  9. * Defines the "aicc" child of class "learnpath"
  10. * @package dokeos.learnpath.aicc
  11. */
  12. require_once('openoffice_document.class.php');
  13. class OpenOfficeTextDocument extends OpenofficeDocument {
  14. public $split_steps;
  15. function OpenofficeTextDocument($split_steps=false, $course_code=null, $resource_id=null,$user_id=null) {
  16. $this -> split_steps = $split_steps;
  17. parent::OpenofficeDocument($course_code, $resource_id, $user_id);
  18. }
  19. function make_lp($files=array()){
  20. global $_course;
  21. // we get a content where ||page_break|| indicates where the page is broken
  22. $content = file_get_contents($this->base_work_dir.$this->created_dir.'/'.$this->file_name.'.html');
  23. // set the charset if necessary
  24. $charset = api_get_setting('platform_charset');
  25. if(strcasecmp($charset,'utf-8')!==0)
  26. {
  27. $content = utf8_decode($content);
  28. $content = str_replace('utf-8',$charset,$content);
  29. }
  30. // set the path to pictures to absolute (so that it can be modified in fckeditor)
  31. $content = preg_replace("|src=\"([^\"]*)|i", "src=\"".api_get_path(REL_COURSE_PATH).$_course['path'].'/document'.$this->created_dir."/\\1", $content);
  32. list($header, $body) = explode('<BODY',$content);
  33. $body = '<BODY'.$body;
  34. // remove font-family styles
  35. $header = preg_replace("|font\-family[^;]*;|i", "",$header);
  36. // dokeos styles
  37. $my_style = api_get_setting('stylesheets');
  38. if(empty($my_style)){$my_style = 'default';}
  39. $style_to_import = "<style type=\"text/css\">\r\n";
  40. $style_to_import .= '@import "'.api_get_path(WEB_CODE_PATH).'css/'.$my_style.'/default.css";'."\n";
  41. $style_to_import .= '@import "'.api_get_path(WEB_CODE_PATH).'css/'.$my_style.'/course.css";'."\n";
  42. $style_to_import .= "</style>\r\n";
  43. $header = preg_replace("|</head>|i", "\r\n$style_to_import\r\n\\0",$header);
  44. // line break before and after picture
  45. $header = str_replace('p {','p {clear:both;',$header);
  46. $header = str_replace('absolute','relative',$header);
  47. switch($this->split_steps)
  48. {
  49. case 'per_page' : $this -> dealPerPage($header,$body); break;
  50. case 'per_chapter' : $this -> dealPerChapter($header,$body); break;
  51. }
  52. }
  53. function dealPerChapter($header, $content){
  54. global $_course;
  55. $content = str_replace('||page_break||','',$content);
  56. // get all the h1
  57. preg_match_all("|<h1[^>]*>([^(h1)+]*)</h1>|is",$content,$matches_temp);
  58. //empty the fake chapters
  59. $new_index = 0;
  60. for ($i=0 ; $i<count($matches_temp[0]) ; $i++) {
  61. if(trim($matches_temp[1][$i])!=='')
  62. {
  63. $matches[0][$new_index] = $matches_temp[0][$i];
  64. $matches[1][$new_index] = $matches_temp[1][$i];
  65. $new_index++;
  66. }
  67. }
  68. // add intro item
  69. $intro_content = substr($content, 0, strpos($content, $matches[0][0]));
  70. $items_to_create[get_lang('Introduction')] = $intro_content;
  71. for ($i=0 ; $i<count($matches[0]) ; $i++) {
  72. if(empty($matches[1][$i]))
  73. continue;
  74. $content = strstr($content,$matches[0][$i]);
  75. if($i+1!==count($matches[0]))
  76. {
  77. $chapter_content = substr($content, 0, strpos($content, $matches[0][$i+1]));
  78. }
  79. else
  80. {
  81. $chapter_content = $content;
  82. }
  83. $items_to_create[$matches[1][$i]] = $chapter_content;
  84. }
  85. $i = 0;
  86. foreach($items_to_create as $item_title=>$item_content)
  87. {
  88. $i++;
  89. $page_content = $this->format_page_content($header, $item_content);
  90. $html_file = $this->created_dir.'-'.$i.'.html';
  91. $handle = fopen($this->base_work_dir.$this->created_dir.'/'.$html_file,'w+');
  92. fwrite($handle, $page_content);
  93. fclose($handle);
  94. $document_id = add_document($_course,$this->created_dir.'/'.$html_file,'file',filesize($this->base_work_dir.$this->created_dir.'/'.$html_file),$html_file);
  95. if ($document_id){
  96. //put the document in item_property update
  97. api_item_property_update($_course,TOOL_DOCUMENT,$document_id,'DocumentAdded',$_SESSION['_uid'],0,0);
  98. $infos = pathinfo($this->filepath);
  99. $slide_name = strip_tags(nl2br($item_title));
  100. $slide_name = str_replace(array("\r\n", "\r", "\n"), "", $slide_name);
  101. $slide_name = html_entity_decode($slide_name);
  102. $previous = learnpath::add_item(0, $previous, 'document', $document_id, $slide_name, '');
  103. if($this->first_item == 0){
  104. $this->first_item = $previous;
  105. }
  106. }
  107. }
  108. }
  109. function dealPerPage($header,$body)
  110. {
  111. // split document to pages
  112. $pages = explode('||page_break||',$body);
  113. $first_item = 0;
  114. foreach($pages as $key=>$page_content){ // for every pages, we create a new file
  115. $key +=1;
  116. $page_content = $this->format_page_content($header, $page_content, $this->base_work_dir.$this->created_dir);
  117. $html_file = $this->created_dir.'-'.$key.'.html';
  118. $handle = fopen($this->base_work_dir.$this->created_dir.'/'.$html_file,'w+');
  119. fwrite($handle, $page_content);
  120. fclose($handle);
  121. $document_id = add_document($_course,$this->created_dir.'/'.$html_file,'file',filesize($this->base_work_dir.$this->created_dir.'/'.$html_file),$html_file);
  122. if ($document_id){
  123. //put the document in item_property update
  124. api_item_property_update($_course,TOOL_DOCUMENT,$document_id,'DocumentAdded',$_SESSION['_uid'],0,0);
  125. $infos = pathinfo($this->filepath);
  126. $slide_name = 'Page '.str_repeat('0',2-strlen($key)).$key;
  127. $previous = learnpath::add_item(0, $previous, 'document', $document_id, $slide_name, '');
  128. if($this->first_item == 0){
  129. $this->first_item = $previous;
  130. }
  131. }
  132. }
  133. }
  134. function add_command_parameters(){
  135. return ' -d woogie';
  136. }
  137. function format_page_content($header, $content)
  138. {
  139. // limit the width of the doc
  140. list($max_width, $max_height) = explode('x',api_get_setting('service_ppt2lp','size'));
  141. $content = preg_replace("|<body[^>]*>|i","\\0\r\n<div style=\"width:".$max_width."\">",$content, -1,$count);
  142. if($count < 1)
  143. {
  144. $content = '<body><div style="width:'.$max_width.'">'.$content;
  145. }
  146. $content = preg_replace('|</body>|i','</div>\\0',$content, -1, $count);
  147. if($count < 1)
  148. {
  149. $content = $content.'</div></body>';
  150. }
  151. // add the headers
  152. $content = $header.$content;
  153. // resize all the picture to the max_width-10
  154. preg_match_all("|<img[^src]*src=\"([^\"]*)\"[^>]*>|i",$content,$images);
  155. foreach ($images[1] as $key => $image)
  156. {
  157. // check if the <img tag soon has a width attribute
  158. $defined_width = preg_match("|width=([^\s]*)|i",$images[0][$key], $img_width);
  159. $img_width = $img_width[1];
  160. if(!$defined_width)
  161. {
  162. list($img_width, $img_height, $type) = getimagesize($this->base_work_dir.$this->created_dir.'/'.$image);
  163. $new_width = $max_width-10;
  164. if($img_width > $new_width)
  165. {
  166. $picture_resized = str_ireplace('<img','<img width="'.$new_width.'" ',$images[0][$key]);
  167. $content = str_replace($images[0][$key],$picture_resized,$content);
  168. }
  169. }
  170. else if($img_width > $max_width-10)
  171. {
  172. $picture_resized = str_ireplace('width='.$img_width,'width="'.($max_width-10).'"',$images[0][$key]);
  173. $content = str_replace($images[0][$key],$picture_resized,$content);
  174. }
  175. }
  176. return $content;
  177. }
  178. function add_docs_to_visio (){
  179. }
  180. }
  181. ?>