fix_mimetex.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. exit;
  4. require_once __DIR__ . '/../../main/inc/global.inc.php';
  5. $sql = 'SELECT iid, id, question, c_id
  6. FROM c_quiz_question
  7. ORDER BY iid';
  8. $result = Database::query($sql);
  9. $data = Database::store_result($result);
  10. $counter = 1;
  11. $total = count($data);
  12. echo 'Exercises to check: '.$total.PHP_EOL;
  13. foreach ($data as $row) {
  14. $id = $row['id'];
  15. $courseId = $row['c_id'];
  16. $content = fixText($row['question']);
  17. $params = [
  18. 'question' => $content
  19. ];
  20. Database::update('c_quiz_question', $params, ['id = ? and c_id = ?' => [$id, $courseId]]);
  21. $sql = "SELECT iid, answer, comment FROM c_quiz_answer WHERE question_id = $id and c_id = $courseId";
  22. $result = Database::query($sql);
  23. $items = Database::store_result($result, 'ASSOC');
  24. foreach ($items as $item) {
  25. $id = $item['iid'];
  26. $answer = fixText($item['answer']);
  27. $comment = fixText($item['comment']);
  28. $params = [
  29. 'answer' => $answer,
  30. 'comment' => $comment,
  31. ];
  32. Database::update('c_quiz_answer', $params, ['iid = ? ' => $id]);
  33. }
  34. }
  35. function fixText($content)
  36. {
  37. $debug = false;
  38. if (strpos($content, 'mimetex.cgi') !== false) {
  39. $content = preg_replace('/[\r\n]+/', '', $content);
  40. //$debug = true;
  41. if ($debug) {
  42. var_dump($content);
  43. }
  44. }
  45. $matches = [];
  46. if (preg_match_all('/<img alt="(.*?)" (.*?)mimetex.cgi\? (.*?)\/>/', $content, $matches)) {
  47. $count = count($matches[0]);
  48. for ($i = 0; $i <= $count; $i++) {
  49. if (isset($matches[0]) && isset($matches[0][$i])) {
  50. $value = "<span class=\"math-tex\">`".$matches[1][$i]."`</span>";
  51. $content = str_replace($matches[0][$i], $value, $content);
  52. }
  53. }
  54. }
  55. $matches = [];
  56. /*<img (.*)mimetex.cgi\?(.*) title="(.*)?" alt="(.*)?" \/>
  57. <img (.*)?mimetex.cgi\? (.*)? title="(.*)?" alt="(.*)?" \/>*/
  58. // if (preg_match_all('/<img (.*)?mimetex.cgi\? (.*)? title="(.*)?" alt="(.*)?" \/>/', $content, $matches)) {
  59. if (preg_match_all('/<img (.*?)mimetex.cgi\?(.*?) title="(.*?)" alt="(.*?)"/', $content, $matches)) {
  60. $count = count($matches[0]);
  61. for ($i = 0; $i <= $count; $i++) {
  62. if (isset($matches[0]) && isset($matches[0][$i])) {
  63. $value = "<span class=\"math-tex\">`".$matches[3][$i]."`</span>";
  64. $content = str_replace($matches[0][$i], $value, $content);
  65. }
  66. }
  67. }
  68. $matches = [];
  69. if (preg_match_all('/<img (.*?)mimetex.cgi\? (.*?) alt="(.*?)" title="(.*?)" \/>/', $content, $matches)) {
  70. $count = count($matches[0]);
  71. for ($i = 0; $i <= $count; $i++) {
  72. if (isset($matches[0]) && isset($matches[0][$i])) {
  73. $value = "<span class=\"math-tex\">`".$matches[3][$i]."`</span>";
  74. $content = str_replace($matches[0][$i], $value, $content);
  75. }
  76. }
  77. }
  78. $matches = [];
  79. if (preg_match_all('/<img (.*?)mimetex.cgi\?(.*?)title="(.*?)" alt="(.*?)"/', $content, $matches)) {
  80. $count = count($matches[0]);
  81. for ($i = 0; $i <= $count; $i++) {
  82. if (isset($matches[0]) && isset($matches[0][$i])) {
  83. $value = "<span class=\"math-tex\">`".$matches[3][$i]."`</span>";
  84. $content = str_replace($matches[0][$i], $value, $content);
  85. }
  86. }
  87. }
  88. $matches = [];
  89. if (preg_match_all('/<img (.*?)mimetex.cgi\? (.*?) title="(.*?)" alt="(.*?)"\/>/', $content, $matches)) {
  90. $count = count($matches[0]);
  91. for ($i = 0; $i <= $count; $i++) {
  92. if (isset($matches[0]) && isset($matches[0][$i])) {
  93. $value = "<span class=\"math-tex\">`".$matches[3][$i]."`</span>";
  94. $content = str_replace($matches[0][$i], $value, $content);
  95. }
  96. }
  97. }
  98. $matches = [];
  99. if (preg_match_all('/<img title="(.*?)" (.*?)mimetex.cgi\? (.*?) \/>/', $content, $matches)) {
  100. $count = count($matches[0]);
  101. for ($i = 0; $i <= $count; $i++) {
  102. if (isset($matches[0]) && isset($matches[0][$i])) {
  103. $value = "<span class=\"math-tex\">`".$matches[3][$i]."`</span>";
  104. $content = str_replace($matches[0][$i], $value, $content);
  105. }
  106. }
  107. }
  108. if ($debug) {
  109. var_dump($content);
  110. }
  111. return $content;
  112. }