asset_scratch_renderer.class.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * Scratch renderer.
  4. *
  5. * @see http://scratch.mit.edu/projects/
  6. *
  7. * @copyright (c) 2012 University of Geneva
  8. * @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
  9. * @author Laurent Opprecht
  10. */
  11. class AssetScratchRenderer extends AssetRenderer
  12. {
  13. /**
  14. *
  15. * @param HttpResource $asset
  16. */
  17. public function accept($asset)
  18. {
  19. return $asset->url_match('http://scratch.mit.edu/projects/');
  20. }
  21. /**
  22. *
  23. * @param HttpResource $asset
  24. */
  25. public function render($asset)
  26. {
  27. if (!$this->accept($asset))
  28. {
  29. return;
  30. }
  31. $matches = array();
  32. $pattern = "#http:\/\/scratch.mit.edu\/projects\/(\w+)/(\d*)\/?#ims";
  33. preg_match($pattern, $asset->url(), $matches);
  34. $url = $matches[0];
  35. $author = $matches[1];
  36. $project_id = $matches[2];
  37. $project_url = "../../static/projects/$author/$project_id.sb";
  38. $image_url = "http://scratch.mit.edu/static/projects/$author/{$project_id}_med.png";
  39. $thumb_url = "http://scratch.mit.edu/static/projects/$author/{$project_id}_sm.png";
  40. $height = 387;
  41. $width = 482;
  42. if (function_exists('get_string'))
  43. {
  44. $no_java = get_string('no_java', 'artefact.extresource');
  45. }
  46. else
  47. {
  48. $no_java = 'Java is not installed on your computer. You must install java first.';
  49. }
  50. $embed = <<<EOT
  51. <object type="application/x-java-applet" width="$width" height="$height" style="display:block" id="ProjectApplet">
  52. <param name="codebase" value="http://scratch.mit.edu/static/misc">
  53. <param name="archive" value="ScratchApplet.jar">
  54. <param name="code" value="ScratchApplet">
  55. <param name="project" value="$project_url">
  56. <pre>$no_java</pre>
  57. <img alt="" src="$image_url">
  58. </object>
  59. EOT;
  60. $result = array();
  61. $result[self::EMBED_SNIPPET] = $embed;
  62. $result[self::THUMBNAIL] = $thumb_url;
  63. return $result;
  64. }
  65. }