groups.php 4.1 KB

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