Coverage for tasks/isaaqcommon.py: 40%

1"""

2 camcops_server/tasks/isaaqcommon.py

3

4===============================================================================

5

6 Copyright (C) 2012, University of Cambridge, Department of Psychiatry.

7 Created by Rudolf Cardinal (rnc1001@cam.ac.uk).

8

9 This file is part of CamCOPS.

10

11 CamCOPS is free software: you can redistribute it and/or modify

12 it under the terms of the GNU General Public License as published by

13 the Free Software Foundation, either version 3 of the License, or

14 (at your option) any later version.

15

16 CamCOPS is distributed in the hope that it will be useful,

17 but WITHOUT ANY WARRANTY; without even the implied warranty of

18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

19 GNU General Public License for more details.

20

21 You should have received a copy of the GNU General Public License

22 along with CamCOPS. If not, see <https://www.gnu.org/licenses/>.

23

24===============================================================================

25

26** Common functionality for Internet Severity and Activities Addiction

27 Questionnaire (ISAAQ) tasks.**

28

29"""

30

31 from typing import Optional

32

33 from camcops_server.cc_modules.cc_constants import CssClass

34 from camcops_server.cc_modules.cc_html import tr_qa

35 from camcops_server.cc_modules.cc_request import CamcopsRequest

36 from camcops_server.cc_modules.cc_task import Task, TaskHasPatientMixin

37

38

39# noinspection PyAbstractClass

40 class IsaaqCommon(TaskHasPatientMixin, Task): # type: ignore[misc]

41 __abstract__ = True

42

43 ALL_FIELD_NAMES: list[str] = []

44

45 def is_complete(self) -> bool:

46 # noinspection PyUnresolvedReferences

47 if self.any_fields_none(self.ALL_FIELD_NAMES):

48 return False

49

50 return True

51

52 def get_task_html(self, req: CamcopsRequest) -> str:

53 rows = self.get_task_html_rows(req)

54

55 html = """

56 <div class="{CssClass.SUMMARY}">

57 <table class="{CssClass.SUMMARY}">

58 {tr_is_complete}

59 </table>

60 </div>

61 <table class="{CssClass.TASKDETAIL}">

62 {rows}

63 </table>

64 <div class="{CssClass.FOOTNOTES}">

65 </div>

66 """.format(

67 CssClass=CssClass,

68 tr_is_complete=self.get_is_complete_tr(req),

69 rows=rows,

70 )

71 return html

72

73 def get_task_html_rows(self, req: CamcopsRequest) -> str:

74 raise NotImplementedError

75

76 def get_task_html_rows_for_range(

77 self, req: CamcopsRequest, prefix: str, first_q: int, last_q: int

78 ) -> str:

79 rows = ""

80 for q_num in range(first_q, last_q + 1):

81 field = prefix + str(q_num)

82 question_cell = f"{q_num}. {self.xstring(req, field)}"

83

84 rows += tr_qa(

85 question_cell, self.get_answer_cell(req, prefix, q_num)

86 )

87

88 return rows

89

90 def get_answer_cell(

91 self, req: CamcopsRequest, prefix: str, q_num: int

92 ) -> Optional[str]:

93 q_field = prefix + str(q_num)

94

95 score = getattr(self, q_field)

96 if score is None:

97 return score

98

99 meaning = self.get_score_meaning(req, score)

100

101 answer_cell = f"{score} [{meaning}]"

102

103 return answer_cell

104

105 def get_score_meaning(self, req: CamcopsRequest, score: int) -> str:

106 return self.wxstring(req, f"freq_option_{score}")