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.
 
 
 

86 lines
2.6 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. import collections.abc
  17. defaults = {
  18. "csq_soln": "",
  19. "csq_check_function": lambda sub, soln: sub.strip() == soln.strip(),
  20. "csq_npoints": 1,
  21. "csq_msg_function": lambda sub, soln: "",
  22. "csq_show_check": False,
  23. }
  24. def escape(s):
  25. return s.replace("&", "&amp;").replace('"', "&quot;")
  26. def total_points(**info):
  27. return info["csq_npoints"]
  28. def handle_submission(submissions, **info):
  29. check = info["csq_check_function"]
  30. sub = submissions[info["csq_name"]]
  31. soln = info["csq_soln"]
  32. check_result = check(sub, soln)
  33. if isinstance(check_result, collections.abc.Mapping):
  34. score = check_result["score"]
  35. msg = check_result["msg"]
  36. elif isinstance(check_result, collections.abc.Sequence):
  37. score, msg = check_result
  38. else:
  39. score = check_result
  40. mfunc = info["csq_msg_function"]
  41. try:
  42. msg = mfunc(sub, soln)
  43. except:
  44. try:
  45. msg = mfunc(sub)
  46. except:
  47. msg = ""
  48. percent = float(score)
  49. if info["csq_show_check"]:
  50. if percent == 1.0:
  51. response = '<img src="%s" />' % info["cs_check_image"]
  52. elif percent == 0.0:
  53. response = '<img src="%s" />' % info["cs_cross_image"]
  54. else:
  55. response = ""
  56. else:
  57. response = ""
  58. response += msg
  59. return {"score": percent, "msg": response}
  60. def render_html(last_log, **info):
  61. if last_log is None:
  62. last_log = {}
  63. out = '<input type="text"'
  64. if info.get("csq_size", None) is not None:
  65. out += ' size="%s"' % info["csq_size"]
  66. out += ' value="%s"' % escape(last_log.get(info["csq_name"], ""))
  67. out += ' name="%s"' % info["csq_name"]
  68. out += ' id="%s"' % info["csq_name"]
  69. return out + " />"
  70. def answer_display(**info):
  71. out = "Solution: %s" % (info["csq_soln"])
  72. return out