openoffice_text.class.php 14 KB

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