filter.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * Filter iAssign
  4. *
  5. * Release Notes:
  6. * - v 1.2 2014/01/10
  7. * + Remove comment unread function of filter module.
  8. *
  9. * - v 1.1 2013/10/31
  10. * + Fix bugs in message alert in iassign title and remove message alert of the description by cache error.
  11. *
  12. * @author Patricia Alves Rodrigues
  13. * @author Leônidas O. Brandão
  14. * @author Luciano Oliveira Borges
  15. * @version v 1.2 2014/01/10
  16. * @package iassign_filter
  17. * @since 2010/09/27
  18. * @copyright iMatica (<a href="http://www.matematica.br">iMath</a>) - Computer Science Dep. of IME-USP (Brazil)
  19. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  20. * @see moodle_text_filter
  21. */
  22. /**
  23. * Moodle core defines constant MOODLE_INTERNAL which shall be used to make sure that the script is included and not called directly.
  24. */
  25. defined('MOODLE_INTERNAL') || die();
  26. /**
  27. * This class looks for email addresses in Moodle text and
  28. * hides them using the Moodle obfuscate_text function.
  29. * @see moodle_text_filter
  30. */
  31. class filter_iassign_filter extends moodle_text_filter {
  32. /**
  33. * This takes the HTML to be filtered as an argument
  34. * @param string $text The text for filter
  35. * @param array $options An array for option in the filter
  36. * @return string Return the text with filter
  37. */
  38. function filter($text, array $options = array()) {
  39. global $CFG, $DB, $USER, $COURSE;
  40. if(file_exists($CFG->dirroot . '/mod/iassign/version.php')){
  41. $strs = explode("&lt;/ia&gt;", $text); // < (&lt;) - >(&gt;)
  42. if (count($strs) > 1) {
  43. $expressions = array();
  44. foreach ($strs as $str) {
  45. $tmp = explode("&lt;ia", $str); // < (&lt;) - / (&frasl;) >(&gt;)
  46. if (count($tmp) > 1)
  47. $expressions[] = trim($tmp[1]);
  48. } //foreach($strs as $str)
  49. $text = str_replace("&lt;/ia&gt;", "<br/>", $text);
  50. $text = str_replace("&lt;ia", "<br/>", $text);
  51. foreach ($expressions as $expression) {
  52. // default
  53. $toolbar = 'disable'; // enable (habilita a barra de botões) disable (desabilita a barra de botões)
  54. $width = 800;
  55. $height = 600;
  56. $tmp = explode("&gt;", $expression); // >(&gt;)
  57. $params = trim($tmp[0]);
  58. $fileid = trim($tmp[1]);
  59. $params = str_replace("'", "", $params);
  60. $params = str_replace("&quot;", "", $params);
  61. $params = explode(" ", $params);
  62. foreach ($params as $param) {
  63. $tmp1 = explode("=", $param);
  64. if ($tmp1[0] == "toolbar")
  65. $toolbar = $tmp1[1];
  66. if ($tmp1[0] == "width")
  67. $width = $tmp1[1];
  68. if ($tmp1[0] == "height")
  69. $height = $tmp1[1];
  70. } //foreach($params as $param)
  71. $fs = get_file_storage();
  72. $file = $fs->get_file_by_id($fileid);
  73. $ia_content = '';
  74. $filename = '';
  75. $output = '';
  76. if ($file) {
  77. $filename = $file->get_filename();
  78. $ia_content = $file->get_content();
  79. $file_string = utf8_encode($ia_content);
  80. $output = $this->convert_applet($filename, $width, $height, $toolbar, $file_string, $fileid);
  81. } else {
  82. $output = "<strong>" . get_string('filenotfound', 'filter_iassign_filter') . "</strong>";
  83. }
  84. $text = str_replace($expression, $output, $text);
  85. } // foreach($expressions as $expression)
  86. } // if (count($strs)>1)
  87. //TODO Retirar quando atualizar todo os iassign que estão com a tag &lt;ia_uc&gt;
  88. $strs = explode("&lt;ia_uc&gt;", $text);
  89. if (count($strs) > 1) {
  90. $tmp = explode("&lt;/ia_uc&gt;", $strs[1]);
  91. $expression = trim($tmp[0]);
  92. $text = $strs[0];
  93. }
  94. }
  95. return $text;
  96. }
  97. /**
  98. * Convert paramms to html tag for applet
  99. * @param string $file Filename of applet
  100. * @param string $width Width of applet in html page
  101. * @param string $height Height of applet in html page
  102. * @param string $toolbar Enable toolbar of applet
  103. * @param string $file_string The content for applet load
  104. * @param string $fileid Id for identify of applet
  105. * @return string Return html tag with a string
  106. */
  107. function convert_applet($file, $width, $height, $toolbar, $file_string, $fileid) {
  108. global $CFG, $DB;
  109. $tmp = explode(".", $file);
  110. $extension = $tmp[1];
  111. $iassign_ilms = $DB->get_records("iassign_ilm", array('enable' => 1, 'parent' => 0));
  112. foreach($iassign_ilms as $value) {
  113. $extensions = explode(",", $value->extension);
  114. if(in_array($extension, $extensions))
  115. $iassign_ilm = $value;
  116. }
  117. $output = '';
  118. if (empty($iassign_ilm)) {
  119. $output = "<strong>" . $extension . ": " . get_string('extensionnotfound', 'filter_iassign_filter') . "</strong>";
  120. } else {
  121. $lang = substr(current_language(), 0, 2);
  122. $file_class = $iassign_ilm->file_class;
  123. $notSEND = true;
  124. $file_url = array();
  125. $fs = get_file_storage();
  126. $files_jar = explode(",", $iassign_ilm->file_jar);
  127. foreach($files_jar as $fj) {
  128. $file = $fs->get_file_by_id($fj);
  129. if (!$file)
  130. die($OUTPUT->notification ( get_string ( 'error_confirms_jar', 'iassign' ), 'notifyproblem' ));
  131. $url = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename());
  132. array_push($file_url, $url);
  133. }
  134. $enderecoPOST = "";
  135. $iassignid = 0;
  136. $view = -1;
  137. $token = '';
  138. $end_file = $CFG->wwwroot . '/mod/iassign/ilm_security.php?id=' . $fileid . '&token=' . $token . '&view=' . $view;
  139. $output .= "
  140. <applet name='iLM' code='$file_class' archive='".implode(",", $file_url)."' width='$width' height='$height' vspace=10 hspace=10>
  141. ";
  142. if ($toolbar == "disable")
  143. $output .="<param name='SOH_ADD' value='ADD'>";
  144. $output .="
  145. <param name='MA_PARAM_PropositionURL' value='true'/>
  146. <param name='MA_PARAM_notSEND' value='false'/>
  147. <param name='lang' value='$lang'/>
  148. <param name='MA_PARAM_addresPOST' value='$enderecoPOST'> <!-- 2 -->
  149. <param name='MA_PARAM_Proposition' value='" . $end_file . "'>
  150. </applet> ";
  151. }
  152. return $output;
  153. }
  154. }