openoffice_presentation.class.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 presentations (.ppt, .sxi, .odp, .pptx) 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 "OpenofficePresentation" child of class "OpenofficeDocument"
  13. */
  14. require_once 'openoffice_document.class.php';
  15. if (api_get_setting('search_enabled') == 'true') {
  16. require_once api_get_path(LIBRARY_PATH).'search/DokeosIndexer.class.php';
  17. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  18. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  19. }
  20. class OpenofficePresentation extends OpenofficeDocument {
  21. public $take_slide_name;
  22. public function __construct($take_slide_name = false, $course_code = null, $resource_id = null, $user_id = null) {
  23. $this -> take_slide_name = $take_slide_name;
  24. parent::__construct($course_code, $resource_id, $user_id);
  25. }
  26. public function make_lp($files = array()) {
  27. global $_course;
  28. $previous = 0;
  29. $i = 0;
  30. if (!is_dir($this->base_work_dir.$this->created_dir))
  31. return false;
  32. foreach ($files as $file) {
  33. list($slide_name, $file_name, $slide_body) = explode('||', $file); // '||' is used as separator between fields: slide name (with accents) || file name (without accents) || all slide text (to be indexed).
  34. // Filename is utf8 encoded, but when we decode, some chars are not translated (like quote &rsquo;).
  35. // so we remove these chars by translating it in htmlentities and the reconvert it in want charset.
  36. $slide_name = api_htmlentities($slide_name, ENT_COMPAT, $this->original_charset);
  37. $slide_name = str_replace('&rsquo;', '\'', $slide_name);
  38. $slide_name = api_convert_encoding($slide_name, api_get_system_encoding(), $this->original_charset);
  39. $slide_name = api_html_entity_decode($slide_name, ENT_COMPAT, api_get_system_encoding());
  40. if ($this->take_slide_name === true) {
  41. $slide_name = str_replace('_', ' ', $slide_name);
  42. $slide_name = api_ucfirst($slide_name);
  43. } else {
  44. $slide_name = 'slide'.str_repeat('0', 2 - strlen($i)).$i;
  45. }
  46. $i++;
  47. // Add the png to documents.
  48. $document_id = add_document($_course, $this->created_dir.'/'.urlencode($file_name), 'file', filesize($this->base_work_dir.$this->created_dir.'/'.$file_name), $slide_name);
  49. api_item_property_update($_course, TOOL_DOCUMENT, $document_id, 'DocumentAdded', api_get_user_id(), 0, 0, null, null, api_get_session_id());
  50. // Generating the thumbnail.
  51. $image = $this->base_work_dir.$this->created_dir .'/'. $file_name;
  52. $pattern = '/(\w+)\.png$/';
  53. $replacement = '${1}_thumb.png';
  54. $thumb_name = preg_replace($pattern, $replacement, $file_name);
  55. // Calculate thumbnail size.
  56. $image_size = api_getimagesize($image);
  57. $width = $image_size['width'];
  58. $height = $image_size['height'];
  59. $thumb_width = 300;
  60. $thumb_height = floor($height * ($thumb_width / $width));
  61. $my_new_image = new Image($image);
  62. $my_new_image->resize($thumb_width, $thumb_height);
  63. $my_new_image->send_image($this->base_work_dir.$this->created_dir .'/'. $thumb_name, -1, 'png');
  64. // Adding the thumbnail to documents.
  65. $document_id_thumb = add_document($_course, $this->created_dir.'/'.urlencode($thumb_name), 'file', filesize($this->base_work_dir.$this->created_dir.'/'.$thumb_name), $slide_name);
  66. api_item_property_update($_course, TOOL_THUMBNAIL, $document_id_thumb, 'DocumentAdded', api_get_user_id(), 0, 0);
  67. // Create an html file.
  68. $html_file = $file_name.'.html';
  69. $fp = fopen($this->base_work_dir.$this->created_dir.'/'.$html_file, 'w+');
  70. $slide_src = api_get_path(REL_COURSE_PATH).$_course['path'].'/document/'.$this->created_dir.'/'.utf8_encode($file_name);
  71. $slide_src = str_replace('//', '/', $slide_src);
  72. fwrite($fp,
  73. '<html>
  74. <head>
  75. </head>
  76. <body>
  77. <img src="'.$slide_src.'" />
  78. </body>
  79. </html>'); // This indentation is to make the generated html files to look well.
  80. fclose($fp);
  81. $document_id = add_document($_course,$this->created_dir.'/'.urlencode($html_file), 'file', filesize($this->base_work_dir.$this->created_dir.'/'.$html_file), $slide_name);
  82. if ($document_id) {
  83. // Put the document in item_property update.
  84. api_item_property_update($_course, TOOL_DOCUMENT, $document_id, 'DocumentAdded', api_get_user_id(), 0, 0, null, null, api_get_session_id());
  85. $previous = $this->add_item(0, $previous, 'document', $document_id, $slide_name, '');
  86. if ($this->first_item == 0) {
  87. $this->first_item = $previous;
  88. }
  89. }
  90. // Code for text indexing.
  91. if (api_get_setting('search_enabled') == 'true') {
  92. if (isset($_POST['index_document']) && $_POST['index_document']) {
  93. //Display::display_normal_message(print_r($_POST));
  94. $di = new DokeosIndexer();
  95. isset($_POST['language']) ? $lang = Database::escape_string($_POST['language']) : $lang = 'english';
  96. $di->connectDb(NULL, NULL, $lang);
  97. $ic_slide = new IndexableChunk();
  98. $ic_slide->addValue('title', $slide_name);
  99. $specific_fields = get_specific_field_list();
  100. $all_specific_terms = '';
  101. foreach ($specific_fields as $specific_field) {
  102. if (isset($_REQUEST[$specific_field['code']])) {
  103. $sterms = trim($_REQUEST[$specific_field['code']]);
  104. $all_specific_terms .= ' '. $sterms;
  105. if (!empty($sterms)) {
  106. $sterms = explode(',', $sterms);
  107. foreach ($sterms as $sterm) {
  108. $ic_slide->addTerm(trim($sterm), $specific_field['code']);
  109. }
  110. }
  111. }
  112. }
  113. $slide_body = $all_specific_terms .' '. $slide_body;
  114. $ic_slide->addValue('content', $slide_body);
  115. /* FIXME: cidReq:lp_id:doc_id al indexar */
  116. // Add a comment to say terms separated by commas.
  117. $courseid = api_get_course_id();
  118. $ic_slide->addCourseId($courseid);
  119. $ic_slide->addToolId(TOOL_LEARNPATH);
  120. $lp_id = $this->lp_id;
  121. $xapian_data = array(
  122. SE_COURSE_ID => $courseid,
  123. SE_TOOL_ID => TOOL_LEARNPATH,
  124. SE_DATA => array('lp_id' => $lp_id, 'lp_item' => $previous, 'document_id' => $document_id),
  125. SE_USER => (int)api_get_user_id(),
  126. );
  127. $ic_slide->xapian_data = serialize($xapian_data);
  128. $di->addChunk($ic_slide);
  129. // Index and return search engine document id.
  130. $did = $di->index();
  131. if ($did) {
  132. // Save it to db.
  133. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  134. $sql = 'INSERT INTO %s (id, course_code, tool_id, ref_id_high_level, ref_id_second_level, search_did)
  135. VALUES (NULL , \'%s\', \'%s\', %s, %s, %s)';
  136. $sql = sprintf($sql, $tbl_se_ref, api_get_course_id(), TOOL_LEARNPATH, $lp_id, $previous, $did);
  137. Database::query($sql);
  138. }
  139. }
  140. }
  141. }
  142. }
  143. function add_command_parameters() {
  144. if (empty($this->slide_width) || empty($this->slide_height))
  145. list($this->slide_width, $this->slide_height) = explode('x', api_get_setting('service_ppt2lp', 'size'));
  146. return ' -w '.$this->slide_width.' -h '.$this->slide_height.' -d oogie "'.$this->base_work_dir.'/'.$this->file_path.'" "'.$this->base_work_dir.$this->created_dir.'.html"';
  147. }
  148. function set_slide_size($width, $height) {
  149. $this->slide_width = $width;
  150. $this->slide_height = $height;
  151. }
  152. function add_docs_to_visio ($files = array()) {
  153. global $_course;
  154. /* Add Files */
  155. foreach ($files as $file) {
  156. list($slide_name,$file_name) = explode('||',$file); // '||' is used as separator between slide name (with accents) and file name (without accents).
  157. $slide_name = api_htmlentities($slide_name, ENT_COMPAT, $this->original_charset);
  158. $slide_name = str_replace('&rsquo;', '\'', $slide_name);
  159. $slide_name = api_convert_encoding($slide_name, api_get_system_encoding(), $this->original_charset);
  160. $slide_name = api_html_entity_decode($slide_name, ENT_COMPAT, api_get_system_encoding());
  161. $did = add_document($_course, $this->created_dir.'/'.urlencode($file_name), 'file', filesize($this->base_work_dir.$this->created_dir.'/'.$file_name), $slide_name);
  162. if ($did)
  163. api_item_property_update($_course, TOOL_DOCUMENT, $did, 'DocumentAdded', $_SESSION['_uid'], 0, null, null, null, api_get_session_id());
  164. }
  165. }
  166. }