openoffice_presentation.class.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php //$id:$
  2. /**
  3. * Defines the OpenofficeDocument class, which is meant as a conversion
  4. * tool from Office presentations (.ppt, .sxi, .odp, .pptx) to
  5. * learning paths
  6. * @package dokeos.learnpath
  7. * @author Eric Marguin <eric.marguin@dokeos.com>
  8. * @license GNU/GPL - See Dokeos license directory for details
  9. */
  10. /**
  11. * Defines the "OpenofficePresentation" child of class "OpenofficeDocument"
  12. * @package dokeos.learnpath.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. function OpenofficePresentation($take_slide_name=false, $course_code=null, $resource_id=null,$user_id=null) {
  23. $this -> take_slide_name = $take_slide_name;
  24. parent::OpenofficeDocument($course_code, $resource_id, $user_id);
  25. }
  26. 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 = htmlentities($slide_name,ENT_COMPAT,$this->original_charset);
  37. $slide_name = str_replace('&rsquo;','\'',$slide_name);
  38. $slide_name = mb_convert_encoding($slide_name, api_get_setting('platform_charset'), $this->original_charset);
  39. $slide_name = html_entity_decode($slide_name);
  40. if($this->take_slide_name === true)
  41. {
  42. $slide_name = str_replace('_',' ',$slide_name);
  43. $slide_name = ucfirst($slide_name);
  44. }
  45. else
  46. {
  47. $slide_name = 'slide'.str_repeat('0',2-strlen($i)).$i;
  48. }
  49. $i++;
  50. // add the png to documents
  51. $document_id = add_document($_course,$this->created_dir.'/'.urlencode($file_name),'file',filesize($this->base_work_dir.$this->created_dir.'/'.$file_name),$slide_name);
  52. api_item_property_update($_course,TOOL_DOCUMENT,$document_id,'DocumentAdded',api_get_user_id(),0,0);
  53. // Generating the thumbnail
  54. $image = $this->base_work_dir.$this->created_dir .'/'. $file_name;
  55. $pattern = '/(\w+)\.png$/';
  56. $replacement = '${1}_thumb.png';
  57. $thumb_name = preg_replace($pattern, $replacement, $file_name);
  58. // calculate thumbnail size
  59. list($width, $height) = getimagesize($image);
  60. $thumb_width = 300;
  61. $thumb_height = floor( $height * ($thumb_width / $width ) );
  62. // load
  63. $thumb = imagecreatetruecolor($thumb_width, $thumb_height);
  64. $source = imagecreatefrompng($image);
  65. // resize
  66. imagecopyresized($thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
  67. // output
  68. imagepng($thumb, $this->base_work_dir.$this->created_dir .'/'. $thumb_name);
  69. /*
  70. // new resizing method usign imagemagick
  71. $im = new Imagick( $image );
  72. $im->thumbnailImage($thumb_width, $thumb_height);
  73. //file_put_contents($this->base_work_dir.$this->created_dir .'/'. $thumb_name,$im);
  74. $im->writeImage($this->base_work_dir.$this->created_dir .'/'. $thumb_name);
  75. */
  76. // adding the thumbnail to documents
  77. $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);
  78. api_item_property_update($_course, TOOL_THUMBNAIL, $document_id_thumb,'DocumentAdded',api_get_user_id(),0,0);
  79. // create an html file
  80. $html_file = $file_name.'.html';
  81. $fp = fopen($this->base_work_dir.$this->created_dir.'/'.$html_file, 'w+');
  82. fwrite($fp,
  83. '<html>
  84. <head></head>
  85. <body>
  86. <img src="'.api_get_path(REL_COURSE_PATH).$_course['path'].'/document/'.$this->created_dir.'/'.utf8_encode($file_name).'" />
  87. </body>
  88. </html>');
  89. fclose($fp);
  90. $document_id = add_document($_course,$this->created_dir.'/'.urlencode($html_file),'file',filesize($this->base_work_dir.$this->created_dir.'/'.$html_file),$slide_name);
  91. if ($document_id){
  92. //put the document in item_property update
  93. api_item_property_update($_course,TOOL_DOCUMENT,$document_id,'DocumentAdded',api_get_user_id(),0,0);
  94. $previous = learnpath::add_item(0, $previous, 'document', $document_id, $slide_name, '');
  95. if($this->first_item == 0){
  96. $this->first_item = $previous;
  97. }
  98. }
  99. // code for text indexing
  100. if (api_get_setting('search_enabled')=='true') {
  101. if (isset($_POST['index_document']) && $_POST['index_document']) {
  102. //Display::display_normal_message(print_r($_POST));
  103. $di = new DokeosIndexer();
  104. isset($_POST['language'])? $lang=Database::escape_string($_POST['language']): $lang = 'english';
  105. $di->connectDb(NULL, NULL, $lang);
  106. $ic_slide = new IndexableChunk();
  107. $ic_slide->addValue("title", $slide_name);
  108. $specific_fields = get_specific_field_list();
  109. $all_specific_terms = '';
  110. foreach ($specific_fields as $specific_field) {
  111. if (isset($_REQUEST[$specific_field['code']])) {
  112. $sterms = trim($_REQUEST[$specific_field['code']]);
  113. $all_specific_terms .= ' '. $sterms;
  114. if (!empty($sterms)) {
  115. $sterms = explode(',', $sterms);
  116. foreach ($sterms as $sterm) {
  117. $ic_slide->addTerm(trim($sterm), $specific_field['code']);
  118. }
  119. }
  120. }
  121. }
  122. $slide_body = $all_specific_terms .' '. $slide_body;
  123. $ic_slide->addValue("content", $slide_body);
  124. /* FIXME: cidReq:lp_id:doc_id al indexar */
  125. // add a comment to say terms separated by commas
  126. $courseid=api_get_course_id();
  127. $ic_slide->addCourseId($courseid);
  128. $ic_slide->addToolId(TOOL_LEARNPATH);
  129. $lp_id = $this->lp_id;
  130. $xapian_data = array(
  131. SE_COURSE_ID => $courseid,
  132. SE_TOOL_ID => TOOL_LEARNPATH,
  133. SE_DATA => array('lp_id' => $lp_id, 'lp_item'=> $previous, 'document_id' => $document_id),
  134. SE_USER => (int)api_get_user_id(),
  135. );
  136. $ic_slide->xapian_data = serialize($xapian_data);
  137. $di->addChunk($ic_slide);
  138. //index and return search engine document id
  139. $did = $di->index();
  140. if ($did) {
  141. // save it to db
  142. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  143. $sql = 'INSERT INTO %s (id, course_code, tool_id, ref_id_high_level, ref_id_second_level, search_did)
  144. VALUES (NULL , \'%s\', \'%s\', %s, %s, %s)';
  145. $sql = sprintf($sql, $tbl_se_ref, api_get_course_id(), TOOL_LEARNPATH, $lp_id, $previous, $did);
  146. api_sql_query($sql,__FILE__,__LINE__);
  147. }
  148. }
  149. }
  150. }
  151. }
  152. function add_command_parameters(){
  153. if(empty($this->slide_width) || empty($this->slide_height))
  154. list($this->slide_width, $this->slide_height) = explode('x',api_get_setting('service_ppt2lp','size'));
  155. 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"';
  156. }
  157. function set_slide_size($width,$height)
  158. {
  159. $this->slide_width = $width;
  160. $this->slide_height = $height;
  161. }
  162. function add_docs_to_visio ($files=array()){
  163. global $_course;
  164. /* Add Files */
  165. foreach($files as $file){
  166. list($slide_name,$file_name) = explode('||',$file); // '||' is used as separator between slide name (with accents) and file name (without accents)
  167. $slide_name = htmlentities($slide_name,ENT_COMPAT,$this->original_charset);
  168. $slide_name = str_replace('&rsquo;','\'',$slide_name);
  169. $slide_name = mb_convert_encoding($slide_name, api_get_setting('platform_charset'), $this->original_charset);
  170. $slide_name = html_entity_decode($slide_name);
  171. $did = add_document($_course, $this->created_dir.'/'.urlencode($file_name), 'file', filesize($this->base_work_dir.$this->created_dir.'/'.$file_name), $slide_name);
  172. if ($did)
  173. api_item_property_update($_course, TOOL_DOCUMENT, $did, 'DocumentAdded', $_SESSION['_uid'], 0, NULL);
  174. }
  175. }
  176. }
  177. ?>