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 = 0;
  15. if ($group_properties) {
  16. $groupdirpath = $group_properties['directory'];
  17. $groupIid = $group_properties['iid'];
  18. }
  19. $group_disk_path = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/document'.$groupdirpath.'/';
  20. $group_web_path = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/document'.$groupdirpath.'/';
  21. //get all group files and folders
  22. $docs_and_folders = DocumentManager::getAllDocumentData(
  23. $course_info,
  24. $groupdirpath,
  25. $groupIid,
  26. null,
  27. $is_allowed_to_edit,
  28. false
  29. );
  30. // get all group filenames
  31. $array_to_search = !empty($docs_and_folders) ? $docs_and_folders : array();
  32. if (count($array_to_search) > 0) {
  33. while (list($key) = each($array_to_search)) {
  34. $all_files[] = basename($array_to_search[$key]['path']);
  35. }
  36. }
  37. //get all svg and png group files
  38. $accepted_extensions = array('.svg', '.png');
  39. if (is_array($all_files) && count($all_files) > 0) {
  40. foreach ($all_files as & $file) {
  41. $slideshow_extension = strrchr($file, '.');
  42. $slideshow_extension = strtolower($slideshow_extension);
  43. if (in_array($slideshow_extension, $accepted_extensions)) {
  44. $png_svg_files[] =$file;
  45. }
  46. }
  47. }
  48. $style = '<style>';
  49. $style .= '@import "'.api_get_path(WEB_CSS_PATH).'base.css";';
  50. $style .= '@import "'.api_get_path(WEB_CSS_PATH).'themes/'.api_get_visual_theme().'/default.css";';
  51. $style .='</style>';
  52. ?>
  53. <!doctype html>
  54. <?php echo api_get_jquery_js(); ?>
  55. <?php echo $style ?>
  56. <body>
  57. <?php
  58. echo '<h2>'.get_lang('Group').': '.$group_properties['name'].'</h2>';
  59. if ((
  60. $group_properties['doc_state'] == 2 &&
  61. ($is_allowed_to_edit || GroupManager :: is_user_in_group($_user['user_id'], $group_properties))) || $group_properties['doc_state'] == 1
  62. ){
  63. if (!empty($png_svg_files)) {
  64. echo '<h3>'.get_lang('Select a picture (SVG, PNG)').'</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('Only accessible from your group'), '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>