openoffice_text_document.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Defines the OpenOfficeDocument class, which is meant as a conversion
  5. * tool from Office text documents (.doc, .sxw, .odt, .docx) to
  6. * learning paths
  7. * @package chamilo.learnpath
  8. * @author Eric Marguin <eric.marguin@dokeos.com>
  9. * @license GNU/GPL
  10. */
  11. /**
  12. * Defines the "OpenOfficeTextDocument" child of class "learnpath"
  13. * @package chamilo.learnpath.openofficedocument
  14. */
  15. require_once 'openoffice_document.class.php';
  16. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  17. require_once api_get_path(LIBRARY_PATH).'search/DokeosIndexer.class.php';
  18. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  19. class OpenOfficeTextDocument extends OpenofficeDocument {
  20. public $split_steps;
  21. /**
  22. * Class constructor. Calls the parent class and initialises the local attribute split_steps
  23. * @param boolean Whether to split steps (true) or make one large page (false)
  24. * @param string Course code
  25. * @param integer Resource ID
  26. * @param integer Creator user id
  27. * @return void
  28. */
  29. public function __construct($split_steps = false, $course_code = null, $resource_id = null, $user_id = null) {
  30. $this -> split_steps = $split_steps;
  31. parent::__construct($course_code, $resource_id, $user_id);
  32. }
  33. /**
  34. * Gets html pages and compose them into a learning path
  35. * @param array The files that will compose the generated learning path. Unused so far.
  36. * @return boolean False if file does not exit. Nothing otherwise.
  37. */
  38. public function make_lp($files = array()) {
  39. global $_course;
  40. // We get a content where ||page_break|| indicates where the page is broken.
  41. if (!file_exists($this->base_work_dir.'/'.$this->created_dir.'/'.$this->file_name.'.html')) { return false; }
  42. $content = file_get_contents($this->base_work_dir.'/'.$this->created_dir.'/'.$this->file_name.'.html');
  43. unlink($this->base_work_dir.'/'.$this->file_path);
  44. unlink($this->base_work_dir.'/'.$this->created_dir.'/'.$this->file_name.'.html');
  45. // The file is utf8 encoded and it seems to make problems with special quotes.
  46. // then we htmlentities that, we replace these quotes and html_entity_decode that in good charset.
  47. $charset = api_get_system_encoding();
  48. $content = api_htmlentities($content, ENT_COMPAT, $this->original_charset);
  49. $content = str_replace('&rsquo;', '\'', $content);
  50. $content = api_convert_encoding($content, $charset, $this->original_charset);
  51. $content = str_replace($this->original_charset, $charset, $content);
  52. $content = api_html_entity_decode($content, ENT_COMPAT, $charset);
  53. // Set the path to pictures to absolute (so that it can be modified in fckeditor).
  54. $content = preg_replace("|src=\"([^\"]*)|i", "src=\"".api_get_path(REL_COURSE_PATH).$_course['path'].'/document'.$this->created_dir."/\\1", $content);
  55. list($header, $body) = explode('<BODY', $content);
  56. $body = '<BODY'.$body;
  57. // Remove font-family styles.
  58. $header = preg_replace("|font\-family[^;]*;|i", '', $header);
  59. // Chamilo styles.
  60. $my_style = api_get_setting('stylesheets');
  61. if (empty($my_style)) { $my_style = 'chamilo'; }
  62. $style_to_import = "<style type=\"text/css\">\r\n";
  63. $style_to_import .= '@import "'.api_get_path(WEB_CODE_PATH).'css/'.$my_style.'/default.css";'."\n";
  64. $style_to_import .= '@import "'.api_get_path(WEB_CODE_PATH).'css/'.$my_style.'/course.css";'."\n";
  65. $style_to_import .= "</style>\r\n";
  66. $header = preg_replace("|</head>|i", "\r\n$style_to_import\r\n\\0", $header);
  67. // Line break before and after picture.
  68. $header = str_replace('p {', 'p {clear:both;', $header);
  69. $header = str_replace('absolute', 'relative', $header);
  70. switch ($this->split_steps) {
  71. case 'per_page': $this -> dealPerPage($header, $body); break;
  72. case 'per_chapter': $this -> dealPerChapter($header, $body); break;
  73. }
  74. }
  75. /**
  76. * Manages chapter splitting
  77. * @param string Chapter header
  78. * @param string Content
  79. * @return void
  80. */
  81. function dealPerChapter($header, $content) {
  82. global $_course;
  83. $content = str_replace('||page_break||', '', $content);
  84. // Get all the h1.
  85. preg_match_all("|<h1[^>]*>([^(h1)+]*)</h1>|is", $content, $matches_temp);
  86. // Empty the fake chapters.
  87. $new_index = 0;
  88. for ($i = 0; $i < count($matches_temp[0]); $i++) {
  89. if (trim($matches_temp[1][$i]) !== '') {
  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 = api_substr($content, 0, api_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 = api_strstr($content, $matches[0][$i]);
  102. if ($i + 1 !== count($matches[0])) {
  103. $chapter_content = api_substr($content, 0, api_strpos($content, $matches[0][$i + 1]));
  104. } else {
  105. $chapter_content = $content;
  106. }
  107. $items_to_create[$matches[1][$i]] = $chapter_content;
  108. }
  109. $i = 0;
  110. foreach ($items_to_create as $item_title => $item_content) {
  111. $i++;
  112. $page_content = $this->format_page_content($header, $item_content);
  113. $html_file = $this->created_dir.'-'.$i.'.html';
  114. $handle = fopen($this->base_work_dir.$this->created_dir.'/'.$html_file, 'w+');
  115. fwrite($handle, $page_content);
  116. fclose($handle);
  117. $document_id = add_document($_course, $this->created_dir.'/'.$html_file, 'file', filesize($this->base_work_dir.$this->created_dir.'/'.$html_file), $html_file);
  118. if ($document_id) {
  119. // Put the document in item_property update.
  120. api_item_property_update($_course, TOOL_DOCUMENT, $document_id, 'DocumentAdded', $_SESSION['_uid'], 0, 0, null, null, api_get_session_id());
  121. $infos = pathinfo($this->filepath);
  122. $slide_name = strip_tags(nl2br($item_title));
  123. $slide_name = str_replace(array("\r\n", "\r", "\n"), '', $slide_name);
  124. $slide_name = api_html_entity_decode($slide_name, ENT_COMPAT, api_get_system_encoding());
  125. $previous = learnpath::add_item(0, $previous, 'document', $document_id, $slide_name, '');
  126. if ($this->first_item == 0) {
  127. $this->first_item = $previous;
  128. }
  129. }
  130. }
  131. }
  132. /**
  133. * Manages page splitting
  134. * @param string Page header
  135. * @param string Page body
  136. * @return void
  137. */
  138. function dealPerPage($header, $body) {
  139. global $_course;
  140. // Split document to pages.
  141. $pages = explode('||page_break||', $body);
  142. $first_item = 0;
  143. foreach($pages as $key => $page_content) {
  144. // For every pages, we create a new file.
  145. $key += 1;
  146. $page_content = $this->format_page_content($header, $page_content, $this->base_work_dir.$this->created_dir);
  147. $html_file = $this->created_dir.'-'.$key.'.html';
  148. $handle = fopen($this->base_work_dir.$this->created_dir.'/'.$html_file, 'w+');
  149. fwrite($handle, $page_content);
  150. fclose($handle);
  151. $document_id = add_document($_course, $this->created_dir.$html_file, 'file', filesize($this->base_work_dir.$this->created_dir.$html_file), $html_file);
  152. $slide_name = '';
  153. if ($document_id) {
  154. // Put the document in item_property update.
  155. api_item_property_update($_course, TOOL_DOCUMENT, $document_id, 'DocumentAdded', $_SESSION['_uid'], 0, 0, null, null, api_get_session_id());
  156. $infos = pathinfo($this->filepath);
  157. $slide_name = 'Page '.str_repeat('0', 2 - strlen($key)).$key;
  158. $previous = learnpath::add_item(0, $previous, 'document', $document_id, $slide_name, '');
  159. if ($this->first_item == 0) {
  160. $this->first_item = $previous;
  161. }
  162. // Code for text indexing.
  163. if (isset($_POST['index_document']) && $_POST['index_document']) {
  164. //Display::display_normal_message(print_r($_POST));
  165. $di = new DokeosIndexer();
  166. isset($_POST['language']) ? $lang = Database::escape_string($_POST['language']) : $lang = 'english';
  167. $di->connectDb(NULL, NULL, $lang);
  168. $ic_slide = new IndexableChunk();
  169. $ic_slide->addValue('title', $slide_name);
  170. $specific_fields = get_specific_field_list();
  171. $all_specific_terms = '';
  172. foreach ($specific_fields as $specific_field) {
  173. if (isset($_REQUEST[$specific_field['code']])) {
  174. $sterms = trim($_REQUEST[$specific_field['code']]);
  175. $all_specific_terms .= ' '. $sterms;
  176. if (!empty($sterms)) {
  177. $sterms = explode(',', $sterms);
  178. foreach ($sterms as $sterm) {
  179. $ic_slide->addTerm(trim($sterm), $specific_field['code']);
  180. }
  181. }
  182. }
  183. }
  184. $page_content = $all_specific_terms .' '. $page_content;
  185. $ic_slide->addValue('content', $page_content);
  186. // Add a comment to say terms separated by commas.
  187. $courseid = api_get_course_id();
  188. $ic_slide->addCourseId($courseid);
  189. $ic_slide->addToolId(TOOL_LEARNPATH);
  190. $lp_id = $this->lp_id;
  191. $xapian_data = array(
  192. SE_COURSE_ID => $courseid,
  193. SE_TOOL_ID => TOOL_LEARNPATH,
  194. SE_DATA => array('lp_id' => $lp_id, 'lp_item' => $previous, 'document_id' => $document_id),
  195. SE_USER => (int)api_get_user_id(),
  196. );
  197. $ic_slide->xapian_data = serialize($xapian_data);
  198. $di->addChunk($ic_slide);
  199. // Index and return search engine document id.
  200. $did = $di->index();
  201. if ($did) {
  202. // Save it to db.
  203. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  204. $sql = 'INSERT INTO %s (id, course_code, tool_id, ref_id_high_level, ref_id_second_level, search_did)
  205. VALUES (NULL , \'%s\', \'%s\', %s, %s, %s)';
  206. $sql = sprintf($sql, $tbl_se_ref, api_get_course_id(), TOOL_LEARNPATH, $lp_id, $previous, $did);
  207. Database::query($sql);
  208. }
  209. }
  210. }
  211. }
  212. }
  213. /**
  214. * Returns additional Java command parameters
  215. * @return string The additional parameters to be used in the Java call
  216. */
  217. function add_command_parameters() {
  218. return ' -d woogie "'.$this->base_work_dir.'/'.$this->file_path.'" "'.$this->base_work_dir.$this->created_dir.'/'.$this->file_name.'.html"';
  219. }
  220. /**
  221. * Formats a page content by reorganising the HTML code a little
  222. * @param string Page header
  223. * @param string Page content
  224. * @return string Formatted page content
  225. */
  226. function format_page_content($header, $content) {
  227. // Limit the width of the doc.
  228. list($max_width, $max_height) = explode('x', api_get_setting('service_ppt2lp', 'size'));
  229. $content = preg_replace("|<body[^>]*>|i", "\\0\r\n<div style=\"width:".$max_width."\">", $content, -1, $count);
  230. if ($count < 1) {
  231. $content = '<body><div style="width:'.$max_width.'">'.$content;
  232. }
  233. $content = preg_replace('|</body>|i','</div>\\0', $content, -1, $count);
  234. if ($count < 1) {
  235. $content = $content.'</div></body>';
  236. }
  237. // Add the headers.
  238. $content = $header.$content;
  239. // Resize all the picture to the max_width-10
  240. preg_match_all("|<img[^src]*src=\"([^\"]*)\"[^>]*>|i", $content, $images);
  241. foreach ($images[1] as $key => $image) {
  242. // Check if the <img tag soon has a width attribute.
  243. $defined_width = preg_match("|width=([^\s]*)|i", $images[0][$key], $img_width);
  244. $img_width = $img_width[1];
  245. if (!$defined_width) {
  246. $image_size = api_getimagesize($this->base_work_dir.$this->created_dir.'/'.$image);
  247. $img_width = $image_size['width'];
  248. $img_height = $image_size['height'];
  249. $new_width = $max_width - 10;
  250. if ($img_width > $new_width) {
  251. $picture_resized = str_ireplace('<img', '<img width="'.$new_width.'" ', $images[0][$key]);
  252. $content = str_replace($images[0][$key], $picture_resized, $content);
  253. }
  254. } elseif ($img_width > $max_width - 10) {
  255. $picture_resized = str_ireplace('width='.$img_width, 'width="'.($max_width-10).'"', $images[0][$key]);
  256. $content = str_replace($images[0][$key], $picture_resized, $content);
  257. }
  258. }
  259. return $content;
  260. }
  261. /**
  262. * Add documents to the visioconference (to be implemented)
  263. */
  264. function add_docs_to_visio() {
  265. }
  266. }