PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/php/main/newscorm/openoffice_text.class.php

https://bitbucket.org/frchico/chamilo_openshift
PHP | 327 lines | 275 code | 16 blank | 36 comment | 5 complexity | dd0f0752bd09e2df868db36552f4e0e1 MD5 | raw file
  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. global $_course;
  44. // We get a content where ||page_break|| indicates where the page is broken.
  45. if (!file_exists($this->base_work_dir.'/'.$this->created_dir.'/'.$this->file_name.'.html')) { return false; }
  46. $content = file_get_contents($this->base_work_dir.'/'.$this->created_dir.'/'.$this->file_name.'.html');
  47. unlink($this->base_work_dir.'/'.$this->file_path);
  48. unlink($this->base_work_dir.'/'.$this->created_dir.'/'.$this->file_name.'.html');
  49. // The file is utf8 encoded and it seems to make problems with special quotes.
  50. // Then we htmlentities that, we replace these quotes and html_entity_decode that in good charset.
  51. $charset = api_get_system_encoding();
  52. $content = api_htmlentities($content, ENT_COMPAT, $this->original_charset);
  53. $content = str_replace('&rsquo;', '\'', $content);
  54. $content = api_convert_encoding($content, $charset, $this->original_charset);
  55. $content = str_replace($this->original_charset, $charset, $content);
  56. $content = api_html_entity_decode($content, ENT_COMPAT, $charset);
  57. // Set the path to pictures to absolute (so that it can be modified in fckeditor).
  58. $content = preg_replace("|src=\"([^\"]*)|i", "src=\"".api_get_path(REL_COURSE_PATH).$_course['path'].'/document'.$this->created_dir."/\\1", $content);
  59. list($header, $body) = explode('<BODY', $content);
  60. $body = '<BODY'.$body;
  61. // Remove font-family styles.
  62. $header = preg_replace("|font\-family[^;]*;|i", '', $header);
  63. // Chamilo styles.
  64. $my_style = api_get_setting('stylesheets');
  65. if (empty($my_style)) { $my_style = 'chamilo'; }
  66. $style_to_import = "<style type=\"text/css\">\r\n";
  67. $style_to_import .= '@import "'.api_get_path(WEB_CODE_PATH).'css/'.$my_style.'/default.css";'."\n";
  68. $style_to_import .= "</style>\r\n";
  69. $header = preg_replace("|</head>|i", "\r\n$style_to_import\r\n\\0", $header);
  70. // Line break before and after picture.
  71. $header = str_replace('p {', 'p {clear:both;', $header);
  72. $header = str_replace('absolute', 'relative', $header);
  73. switch ($this->split_steps) {
  74. case 'per_page': $this -> dealPerPage($header, $body); break;
  75. case 'per_chapter': $this -> dealPerChapter($header, $body); break;
  76. }
  77. }
  78. /**
  79. * Manages chapter splitting
  80. * @param string Chapter header
  81. * @param string Content
  82. * @return void
  83. */
  84. function dealPerChapter($header, $content) {
  85. global $_course;
  86. $content = str_replace('||page_break||', '', $content);
  87. // Get all the h1.
  88. preg_match_all("|<h1[^>]*>([^(h1)+]*)</h1>|is", $content, $matches_temp);
  89. // Empty the fake chapters.
  90. $new_index = 0;
  91. for ($i = 0; $i < count($matches_temp[0]); $i++) {
  92. if (trim($matches_temp[1][$i]) !== '') {
  93. $matches[0][$new_index] = $matches_temp[0][$i];
  94. $matches[1][$new_index] = $matches_temp[1][$i];
  95. $new_index++;
  96. }
  97. }
  98. // Add intro item.
  99. $intro_content = substr($content, 0, strpos($content, $matches[0][0]));
  100. $items_to_create[get_lang('Introduction')] = $intro_content;
  101. for ($i = 0; $i < count($matches[0]); $i++) {
  102. if (empty($matches[1][$i]))
  103. continue;
  104. $content = strstr($content,$matches[0][$i]);
  105. if ($i + 1 !== count($matches[0])) {
  106. $chapter_content = substr($content, 0, strpos($content, $matches[0][$i + 1]));
  107. } else {
  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. $i++;
  115. $page_content = $this->format_page_content($header, $item_content);
  116. $html_file = $this->created_dir.'-'.$i.'.html';
  117. $handle = fopen($this->base_work_dir.$this->created_dir.'/'.$html_file, 'w+');
  118. fwrite($handle, $page_content);
  119. fclose($handle);
  120. $document_id = add_document($_course, $this->created_dir.'/'.$html_file, 'file', filesize($this->base_work_dir.$this->created_dir.'/'.$html_file), $html_file);
  121. if ($document_id){
  122. // Put the document in item_property update.
  123. api_item_property_update($_course, TOOL_DOCUMENT, $document_id, 'DocumentAdded', $_SESSION['_uid'], 0, 0, null, null, api_get_session_id());
  124. $infos = pathinfo($this->filepath);
  125. $slide_name = strip_tags(nl2br($item_title));
  126. $slide_name = str_replace(array("\r\n", "\r", "\n"), '', $slide_name);
  127. $slide_name = html_entity_decode($slide_name);
  128. $previous = learnpath::add_item(0, $previous, 'document', $document_id, $slide_name, '');
  129. if ($this->first_item == 0) {
  130. $this->first_item = $previous;
  131. }
  132. }
  133. }
  134. }
  135. /**
  136. * Manages page splitting
  137. * @param string Page header
  138. * @param string Page body
  139. * @return void
  140. */
  141. function dealPerPage($header, $body) {
  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. $slide_name = '';
  156. if ($document_id) {
  157. // Put the document in item_property update.
  158. api_item_property_update($_course, TOOL_DOCUMENT, $document_id, 'DocumentAdded', $_SESSION['_uid'], 0, 0, null, null, api_get_session_id());
  159. $infos = pathinfo($this->filepath);
  160. $slide_name = 'Page '.str_repeat('0', 2 - strlen($key)).$key;
  161. $previous = learnpath::add_item(0, $previous, 'document', $document_id, $slide_name, '');
  162. if ($this->first_item == 0) {
  163. $this->first_item = $previous;
  164. }
  165. // Code for text indexing.
  166. if (api_get_setting('search_enabled') == 'true') {
  167. if (isset($_POST['index_document']) && $_POST['index_document']) {
  168. //Display::display_normal_message(print_r($_POST));
  169. $di = new ChamiloIndexer();
  170. isset($_POST['language']) ? $lang = Database::escape_string($_POST['language']) : $lang = 'english';
  171. $di->connectDb(NULL, NULL, $lang);
  172. $ic_slide = new IndexableChunk();
  173. $ic_slide->addValue('title', $slide_name);
  174. $specific_fields = get_specific_field_list();
  175. $all_specific_terms = '';
  176. foreach ($specific_fields as $specific_field) {
  177. if (isset($_REQUEST[$specific_field['code']])) {
  178. $sterms = trim($_REQUEST[$specific_field['code']]);
  179. $all_specific_terms .= ' '. $sterms;
  180. if (!empty($sterms)) {
  181. $sterms = explode(',', $sterms);
  182. foreach ($sterms as $sterm) {
  183. $ic_slide->addTerm(trim($sterm), $specific_field['code']);
  184. }
  185. }
  186. }
  187. }
  188. $page_content = $all_specific_terms .' '. $page_content;
  189. $ic_slide->addValue('content', $page_content);
  190. // Add a comment to say terms separated by commas.
  191. $courseid=api_get_course_id();
  192. $ic_slide->addCourseId($courseid);
  193. $ic_slide->addToolId(TOOL_LEARNPATH);
  194. $lp_id = $this->lp_id;
  195. $xapian_data = array(
  196. SE_COURSE_ID => $courseid,
  197. SE_TOOL_ID => TOOL_LEARNPATH,
  198. SE_DATA => array('lp_id' => $lp_id, 'lp_item'=> $previous, 'document_id' => $document_id),
  199. SE_USER => (int)api_get_user_id(),
  200. );
  201. $ic_slide->xapian_data = serialize($xapian_data);
  202. $di->addChunk($ic_slide);
  203. // Index and return search engine document id.
  204. $did = $di->index();
  205. if ($did) {
  206. // Save it to db.
  207. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  208. $sql = 'INSERT INTO %s (id, course_code, tool_id, ref_id_high_level, ref_id_second_level, search_did)
  209. VALUES (NULL , \'%s\', \'%s\', %s, %s, %s)';
  210. $sql = sprintf($sql, $tbl_se_ref, api_get_course_id(), TOOL_LEARNPATH, $lp_id, $previous, $did);
  211. Database::query($sql);
  212. }
  213. }
  214. }
  215. }
  216. }
  217. }
  218. /**
  219. * Returns additional Java command parameters
  220. * @return string The additional parameters to be used in the Java call
  221. */
  222. function add_command_parameters(){
  223. return ' -d woogie "'.$this->base_work_dir.'/'.$this->file_path.'" "'.$this->base_work_dir.$this->created_dir.'/'.$this->file_name.'.html"';
  224. }
  225. /**
  226. * Formats a page content by reorganising the HTML code a little
  227. * @param string Page header
  228. * @param string Page content
  229. * @return string Formatted page content
  230. */
  231. function format_page_content($header, $content) {
  232. // Limit the width of the doc.
  233. list($max_width, $max_height) = explode('x',api_get_setting('service_ppt2lp','size'));
  234. $content = preg_replace("|<body[^>]*>|i", "\\0\r\n<div style=\"width:".$max_width."\">", $content, -1, $count);
  235. if ($count < 1) {
  236. $content = '<body><div style="width:'.$max_width.'">'.$content;
  237. }
  238. $content = preg_replace('|</body>|i', '</div>\\0', $content, -1, $count);
  239. if ($count < 1) {
  240. $content = $content.'</div></body>';
  241. }
  242. // Add the headers.
  243. $content = $header.$content;
  244. // Resize all the picture to the max_width-10
  245. preg_match_all("|<img[^src]*src=\"([^\"]*)\"[^>]*>|i", $content, $images);
  246. foreach ($images[1] as $key => $image) {
  247. // Check if the <img tag soon has a width attribute.
  248. $defined_width = preg_match("|width=([^\s]*)|i", $images[0][$key], $img_width);
  249. $img_width = $img_width[1];
  250. if (!$defined_width) {
  251. list($img_width, $img_height, $type) = getimagesize($this->base_work_dir.$this->created_dir.'/'.$image);
  252. $new_width = $max_width - 10;
  253. if ($img_width > $new_width) {
  254. $picture_resized = str_ireplace('<img', '<img width="'.$new_width.'" ', $images[0][$key]);
  255. $content = str_replace($images[0][$key], $picture_resized, $content);
  256. }
  257. } elseif ($img_width > $max_width - 10) {
  258. $picture_resized = str_ireplace('width='.$img_width, 'width="'.($max_width-10).'"', $images[0][$key]);
  259. $content = str_replace($images[0][$key], $picture_resized, $content);
  260. }
  261. }
  262. return $content;
  263. }
  264. /**
  265. * Add documents to the visioconference (to be implemented)
  266. */
  267. function add_docs_to_visio() {
  268. }
  269. }