PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/php/util/fix_failed_fill_in_the_blanks_questions.php

https://bitbucket.org/chamilo/chamilo-migration-dev/
PHP | 79 lines | 49 code | 30 blank | 0 comment | 1 complexity | 6dc4a1f77b0ede1f3158ecb033cf7799 MD5 | raw file
  1. <?php
  2. namespace migration;
  3. use repository\RepositoryDataManager;
  4. use repository\content_object\fill_in_blanks_question\FillInBlanksQuestion;
  5. ini_set("memory_limit", "-1");
  6. set_time_limit(0);
  7. require_once dirname(__FILE__) . '/../../../common/global.inc.php';
  8. $dbhost = '';
  9. $dbuser = '';
  10. $dbpass = '';
  11. $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
  12. $query = "SELECT rco.*, fib.*, mir.old_id, mir.reference_table_name
  13. FROM `repository_content_object` as rco
  14. JOIN `repository_fill_in_blanks_question` AS fib on rco.id=fib.id
  15. JOIN migration_id_reference AS mir ON fib.id = mir.new_id
  16. WHERE answer_text = 'error: non-standard fill in the blanks answer [error]'";
  17. $dm = RepositoryDataManager :: get_instance();
  18. $questions = $dm->retrieve_object_set($query, FillInBlanksQuestion :: get_table_name());
  19. echo $questions->size() . '<br /><br />';
  20. while($question = $questions->next_result())
  21. {
  22. $db_name = $question->get_optional_property('reference_table_name');
  23. $db_name = explode('.', $db_name);
  24. $db_name = $db_name[0];
  25. $dokeos_id = $question->get_optional_property('old_id');
  26. mysql_select_db($db_name);
  27. $result = mysql_query('SELECT answer from quiz_answer
  28. where question_id=' . $dokeos_id);
  29. if (!$result) {
  30. die('Invalid query: ' . mysql_error());
  31. }
  32. $row = mysql_fetch_assoc($result);
  33. $answer_text = $row['answer'];
  34. $split = explode('::', $answer_text);
  35. $scores = explode(',', $split[1]);
  36. $answer_text = $split[0];
  37. $pattern = '/\[[^\[\]]*\]/';
  38. $answers = preg_match_all($pattern, $answer_text, $matches);
  39. foreach ($matches[0] as $i => $answer)
  40. {
  41. $score = $scores[$i] ? $scores[$i] : 0;
  42. $answer = (substr($answer, 1, -1));
  43. $new_answer = '[' . $answer . '=' . $score . ']';
  44. $answer_text = str_replace('[' . $answer . ']', $new_answer, $answer_text);
  45. }
  46. $question->set_optional_property(FillInBlanksQuestion::PROPERTY_ANSWER_TEXT, $answer_text);
  47. $question->update();
  48. }
  49. ?>