openoffice_text_document.class.php 9.2 KB

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