openoffice_presentation.class.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 = 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_setting('platform_charset'), $this->original_charset);
  39. $slide_name = api_html_entity_decode($slide_name, ENT_COMPAT, api_get_setting('platform_charset'));
  40. if($this->take_slide_name === true)
  41. {
  42. $slide_name = str_replace('_',' ',$slide_name);
  43. $slide_name = api_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,null,null,api_get_session_id());
  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. $slide_src = api_get_path(REL_COURSE_PATH).$_course['path'].'/document/'.$this->created_dir.'/'.utf8_encode($file_name);
  83. $slide_src = str_replace('//', '/', $slide_src);
  84. fwrite($fp,
  85. '<html>
  86. <head>
  87. </head>
  88. <body>
  89. <img src="'.$slide_src.'" />
  90. </body>
  91. </html>'); // This indentation is to make the generated html files to look well.
  92. fclose($fp);
  93. $document_id = add_document($_course,$this->created_dir.'/'.urlencode($html_file),'file',filesize($this->base_work_dir.$this->created_dir.'/'.$html_file),$slide_name);
  94. if ($document_id){
  95. //put the document in item_property update
  96. api_item_property_update($_course,TOOL_DOCUMENT,$document_id,'DocumentAdded',api_get_user_id(),0,0,null,null,api_get_session_id());
  97. $previous = learnpath::add_item(0, $previous, 'document', $document_id, $slide_name, '');
  98. if($this->first_item == 0){
  99. $this->first_item = $previous;
  100. }
  101. }
  102. // code for text indexing
  103. if (api_get_setting('search_enabled')=='true') {
  104. if (isset($_POST['index_document']) && $_POST['index_document']) {
  105. //Display::display_normal_message(print_r($_POST));
  106. $di = new DokeosIndexer();
  107. isset($_POST['language'])? $lang=Database::escape_string($_POST['language']): $lang = 'english';
  108. $di->connectDb(NULL, NULL, $lang);
  109. $ic_slide = new IndexableChunk();
  110. $ic_slide->addValue("title", $slide_name);
  111. $specific_fields = get_specific_field_list();
  112. $all_specific_terms = '';
  113. foreach ($specific_fields as $specific_field) {
  114. if (isset($_REQUEST[$specific_field['code']])) {
  115. $sterms = trim($_REQUEST[$specific_field['code']]);
  116. $all_specific_terms .= ' '. $sterms;
  117. if (!empty($sterms)) {
  118. $sterms = explode(',', $sterms);
  119. foreach ($sterms as $sterm) {
  120. $ic_slide->addTerm(trim($sterm), $specific_field['code']);
  121. }
  122. }
  123. }
  124. }
  125. $slide_body = $all_specific_terms .' '. $slide_body;
  126. $ic_slide->addValue("content", $slide_body);
  127. /* FIXME: cidReq:lp_id:doc_id al indexar */
  128. // add a comment to say terms separated by commas
  129. $courseid=api_get_course_id();
  130. $ic_slide->addCourseId($courseid);
  131. $ic_slide->addToolId(TOOL_LEARNPATH);
  132. $lp_id = $this->lp_id;
  133. $xapian_data = array(
  134. SE_COURSE_ID => $courseid,
  135. SE_TOOL_ID => TOOL_LEARNPATH,
  136. SE_DATA => array('lp_id' => $lp_id, 'lp_item'=> $previous, 'document_id' => $document_id),
  137. SE_USER => (int)api_get_user_id(),
  138. );
  139. $ic_slide->xapian_data = serialize($xapian_data);
  140. $di->addChunk($ic_slide);
  141. //index and return search engine document id
  142. $did = $di->index();
  143. if ($did) {
  144. // save it to db
  145. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  146. $sql = 'INSERT INTO %s (id, course_code, tool_id, ref_id_high_level, ref_id_second_level, search_did)
  147. VALUES (NULL , \'%s\', \'%s\', %s, %s, %s)';
  148. $sql = sprintf($sql, $tbl_se_ref, api_get_course_id(), TOOL_LEARNPATH, $lp_id, $previous, $did);
  149. Database::query($sql,__FILE__,__LINE__);
  150. }
  151. }
  152. }
  153. }
  154. }
  155. function add_command_parameters(){
  156. if(empty($this->slide_width) || empty($this->slide_height))
  157. list($this->slide_width, $this->slide_height) = explode('x',api_get_setting('service_ppt2lp','size'));
  158. 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"';
  159. }
  160. function set_slide_size($width,$height)
  161. {
  162. $this->slide_width = $width;
  163. $this->slide_height = $height;
  164. }
  165. function add_docs_to_visio ($files=array()){
  166. global $_course;
  167. /* Add Files */
  168. foreach($files as $file){
  169. list($slide_name,$file_name) = explode('||',$file); // '||' is used as separator between slide name (with accents) and file name (without accents)
  170. $slide_name = api_htmlentities($slide_name,ENT_COMPAT,$this->original_charset);
  171. $slide_name = str_replace('&rsquo;','\'',$slide_name);
  172. $slide_name = api_convert_encoding($slide_name, api_get_setting('platform_charset'), $this->original_charset);
  173. $slide_name = api_html_entity_decode($slide_name,ENT_COMPAT,api_get_setting('platform_charset'));
  174. $did = add_document($_course, $this->created_dir.'/'.urlencode($file_name), 'file', filesize($this->base_work_dir.$this->created_dir.'/'.$file_name), $slide_name);
  175. if ($did)
  176. api_item_property_update($_course, TOOL_DOCUMENT, $did, 'DocumentAdded', $_SESSION['_uid'], 0, null,null,null,api_get_session_id());
  177. }
  178. }
  179. }
  180. ?>