openoffice_text_document.class.php 9.6 KB

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