openoffice_text_document.class.php 15 KB

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