fix_mimetex.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. $content = fixText($item['answer']);
  27. $params = [
  28. 'answer' => $content
  29. ];
  30. Database::update('c_quiz_answer', $params, ['iid = ? ' => $id]);
  31. }
  32. }
  33. function fixText($content)
  34. {
  35. $matches = [];
  36. if (preg_match_all('/<img alt="(.*)?" (.*)?mimetex.cgi\? (.*)?\/>/', $content, $matches)) {
  37. $count = count($matches[0]);
  38. for ($i = 0; $i <= $count; $i++) {
  39. if (isset($matches[0]) && isset($matches[0][$i])) {
  40. $value = "<span class=\"math-tex\">`".$matches[1][$i]."`</span>";
  41. $content = str_replace($matches[0][$i], $value, $content);
  42. }
  43. }
  44. }
  45. return $content;
  46. }