CAT-SOOP is a flexible, programmable learning management system based on the Python programming language. https://catsoop.mit.edu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

140 lines
4.9 KiB

  1. # This file is part of CAT-SOOP
  2. # Copyright (c) 2011-2020 by The CAT-SOOP Developers <catsoop-dev@mit.edu>
  3. #
  4. # This program is free software: you can redistribute it and/or modify it under
  5. # the terms of the GNU Affero General Public License as published by the Free
  6. # Software Foundation, either version 3 of the License, or (at your option) any
  7. # later version.
  8. #
  9. # This program is distributed in the hope that it will be useful, but WITHOUT
  10. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  12. # details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. expression, _ = csm_tutor.question("expression")
  17. defaults = dict(expression["defaults"])
  18. defaults["csq_expressions"] = [("$x = ~$", ["2", "3"]), ("$y = ~$", ["sqrt(2)"])]
  19. defaults["csq_combine_results"] = lambda results: sum(any(i) for i in results) / len(
  20. results
  21. )
  22. total_points = expression["total_points"]
  23. def get_parsed_reps(submissions, **info):
  24. parser = expression["_get_parser"](info)
  25. funcs = dict(expression["default_funcs"])
  26. funcs.update(info.get("csq_funcs", {}))
  27. parsed = []
  28. for (ix, (prompt, solutions)) in enumerate(info["csq_expressions"]):
  29. osub = sub = submissions.get("__%s_%04d" % (info["csq_name"], ix), "")
  30. fprompt = csm_language.source_transform_string(info, prompt)
  31. try:
  32. sub = parser.parse(sub)
  33. parsed.append(
  34. "%s<br/><displaymath>%s</displaymath>"
  35. % (fprompt, expression["tree2tex"](info, funcs, sub)[0])
  36. )
  37. except:
  38. parsed.append(
  39. '%s<br/><center><font color="red">Error: could not parse your expression <code>%s</code></font></center>'
  40. % (fprompt, repr(osub))
  41. )
  42. msg = '<div class="question">Your expressions were parsed as:<hr/>'
  43. msg += "<hr />".join(parsed)
  44. return msg + "</div>"
  45. checktext = "Check Syntax"
  46. def handle_check(submissions, **info):
  47. return get_parsed_reps(submissions, **info)
  48. def handle_submission(submissions, **info):
  49. results = []
  50. parsed = []
  51. for (ix, (prompt, solutions)) in enumerate(info["csq_expressions"]):
  52. sub = submissions.get("__%s_%04d" % (info["csq_name"], ix), "")
  53. # check each solution and save the results
  54. this_question = []
  55. if not isinstance(solutions, list):
  56. solutions = [solutions]
  57. for soln in solutions:
  58. spoof = dict(info)
  59. spoof["csq_soln"] = [soln]
  60. this_question.append(
  61. expression["handle_submission"]({info["csq_name"]: sub}, **spoof).get(
  62. "score", 0.0
  63. )
  64. )
  65. results.append(this_question)
  66. msg = get_parsed_reps(submissions, **info)
  67. score = info["csq_combine_results"](results)
  68. if isinstance(score, (list, tuple)):
  69. score, extra_msg = score
  70. msg = "%s<hr/>%s" % (msg, extra_msg)
  71. return {"score": score, "msg": msg}
  72. def escape(s):
  73. return s.replace("&", "&amp;").replace('"', "&quot;")
  74. def render_html(submissions, **info):
  75. submissions = submissions or {}
  76. out = '<table border="0">'
  77. for (ix, (prompt, _)) in enumerate(info["csq_expressions"]):
  78. qbox_name = "__%s_%04d" % (info["csq_name"], ix)
  79. out += '<tr><td align="right">'
  80. out += csm_language.source_transform_string(info, prompt)
  81. out += "</td><td>"
  82. out += '<input type="text"'
  83. if info.get("csq_size", None) is not None:
  84. out += ' size="%s"' % info["csq_size"]
  85. out += ' value="%s"' % escape(submissions.get(qbox_name, ""))
  86. out += ' name="%s"' % qbox_name
  87. out += ' id="%s"' % qbox_name
  88. out += " /></td></tr>"
  89. return out + "</table>"
  90. def answer_display(**info):
  91. custom_answer = info.get("csq_custom_answer_display", None)
  92. if custom_answer is not None:
  93. return custom_answer
  94. parser = expression["_get_parser"](info)
  95. out = ""
  96. funcs = dict(expression["default_funcs"])
  97. funcs.update(info.get("csq_funcs", {}))
  98. parsed = []
  99. for (ix, (prompt, solutions)) in enumerate(info["csq_expressions"]):
  100. fprompt = csm_language.source_transform_string(info, prompt)
  101. if not isinstance(solutions, list):
  102. solutions = [solutions]
  103. for soln in solutions:
  104. try:
  105. soln = parser.parse(soln)
  106. parsed.append(
  107. "%s<br/><displaymath>%s</displaymath>"
  108. % (fprompt, expression["tree2tex"](info, funcs, soln)[0])
  109. )
  110. except:
  111. parsed.append(
  112. '%s<br/><center><font color="red">Error: could not parse expression <code>%s</code></font></center>'
  113. % (fprompt, repr(soln))
  114. )
  115. return "<hr/>".join(parsed)