groups.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /* Integrate svg-edit libraries with Chamilo default documents
  3. * @author Juan Carlos Raña Trabado
  4. * @since 25/september/2010
  5. */
  6. require_once '../../../../../../inc/global.inc.php';
  7. //Add security from Chamilo
  8. api_protect_course_script();
  9. api_block_anonymous_users();
  10. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  11. $course_info = api_get_course_info();
  12. $groupId = api_get_group_id();
  13. $group_properties = GroupManager::get_group_properties($groupId);
  14. $groupIid = isset($group_properties['iid']) ? $group_properties['iid'] : 0;
  15. $groupdirpath = $group_properties['directory'];
  16. $group_disk_path = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/document'.$groupdirpath.'/';
  17. $group_web_path = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/document'.$groupdirpath.'/';
  18. //get all group files and folders
  19. $docs_and_folders = DocumentManager::getAllDocumentData(
  20. $course_info,
  21. $groupdirpath,
  22. $groupIid,
  23. null,
  24. $is_allowed_to_edit,
  25. false
  26. );
  27. //get all group filenames
  28. $array_to_search = !empty($docs_and_folders) ? $docs_and_folders : [];
  29. $all_files = [];
  30. if (count($array_to_search) > 0) {
  31. foreach ($array_to_search as $key => $value) {
  32. $all_files[] = basename($array_to_search[$key]['path']);
  33. }
  34. }
  35. //get all svg and png group files
  36. $accepted_extensions = array('.svg', '.png');
  37. $png_svg_files = [];
  38. if (is_array($all_files) && count($all_files) > 0) {
  39. foreach ($all_files as & $file) {
  40. $slideshow_extension = strrchr($file, '.');
  41. $slideshow_extension = strtolower($slideshow_extension);
  42. if (in_array($slideshow_extension, $accepted_extensions)) {
  43. $png_svg_files[] =$file;
  44. }
  45. }
  46. }
  47. $style = '<style>';
  48. $style .= '@import "'.api_get_path(WEB_CSS_PATH).'base.css";';
  49. $style .= '@import "'.api_get_path(WEB_CSS_PATH).'themes/'.api_get_visual_theme().'/default.css";';
  50. $style .='</style>';
  51. ?>
  52. <!doctype html>
  53. <?php echo api_get_jquery_js(); ?>
  54. <?php echo $style ?>
  55. <body>
  56. <?php
  57. echo '<h2>'.get_lang('GroupSingle').': '.$group_properties['name'].'</h2>';
  58. if ((
  59. $group_properties['doc_state'] == 2 &&
  60. ($is_allowed_to_edit || GroupManager :: is_user_in_group($_user['user_id'], $group_properties))) ||
  61. $group_properties['doc_state'] == 1
  62. ){
  63. if (!empty($png_svg_files)) {
  64. echo '<h3>'.get_lang('SelectSVGEditImage').'</h3>';
  65. echo '<ul>';
  66. foreach($png_svg_files as $filename) {
  67. $image = $group_disk_path.$filename;
  68. if (strpos($filename, "svg")){
  69. $new_sizes['width'] = 60;
  70. $new_sizes['height'] = 60;
  71. } else {
  72. $new_sizes = api_resize_image($image, 60, 60);
  73. }
  74. echo '<li style="display:inline; padding:8px;">';
  75. echo '<a href = "'.$group_web_path.$filename.'" alt="'.$filename.'" title="'.$filename.'">';
  76. echo '<img src = "'.$group_web_path.$filename.'" width = "'.$new_sizes['width'].'" height="'.$new_sizes['height'].'" border="0"></a></li>';
  77. }
  78. echo '</ul>';
  79. }
  80. } else {
  81. echo Display::return_message(get_lang('OnlyAccessFromYourGroup'), 'warning');
  82. }
  83. ?>
  84. </body>
  85. <script>
  86. $('a').click(function() {
  87. var href = this.href;
  88. // Convert Non-SVG images to data URL first
  89. // (this could also have been done server-side by the library)
  90. if(this.href.indexOf('.svg') === -1) {
  91. var meta_str = JSON.stringify({
  92. name: $(this).text(),
  93. id: href
  94. });
  95. window.top.postMessage(meta_str, "*");
  96. var img = new Image();
  97. img.onload = function() {
  98. var canvas = document.createElement("canvas");
  99. canvas.width = this.width;
  100. canvas.height = this.height;
  101. // load the raster image into the canvas
  102. canvas.getContext("2d").drawImage(this,0,0);
  103. // retrieve the data: URL
  104. try {
  105. var dataurl = canvas.toDataURL();
  106. } catch(err) {
  107. // This fails in Firefox with file:// URLs :(
  108. alert("Data URL conversion failed: " + err);
  109. var dataurl = "";
  110. }
  111. window.top.postMessage('|' + href + '|' + dataurl, "*");
  112. }
  113. img.src = href;
  114. } else {
  115. // Send metadata (also indicates file is about to be sent)
  116. var meta_str = JSON.stringify({
  117. name: $(this).text(),
  118. id: href
  119. });
  120. window.top.postMessage(meta_str, "*");
  121. // Do ajax request for image's href value
  122. $.get(href, function(data) {
  123. data = '|' + href + '|' + data;
  124. // This is where the magic happens!
  125. window.top.postMessage(data, "*");
  126. }, 'html'); // 'html' is necessary to keep returned data as a string
  127. }
  128. return false;
  129. });
  130. </script>