ChoiceStatistics.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <template>
  2. <div class="choice-statistics-panel">
  3. <div v-if="isChoiceQuestion && statistics" class="statistics-content">
  4. <div class="statistics-title">选择题统计</div>
  5. <div class="statistics-summary">
  6. 总提交:{{ statistics.totalSubmissions }} 人
  7. </div>
  8. </div>
  9. <div v-else class="statistics-empty">
  10. <div v-if="!isChoiceQuestion">当前页面不是选择题</div>
  11. <div v-else-if="!statistics">暂无统计数据</div>
  12. </div>
  13. <div class="statistics-chart">
  14. <div class="tool45" v-if="statistics && statistics.tool=='45'">
  15. <div v-for="(item,index) in statistics.titles" :key="index">
  16. <div class="t45_title">
  17. <span>{{ index+1 }}、</span>
  18. <div>{{ item.title }}</div>
  19. </div>
  20. <div class="t45_btn">
  21. <span @click="lookStudent(item.checkList)">查看学生</span>
  22. </div>
  23. <div class="t45_list">
  24. <div class="t45_l_item" v-for="(i,index2) in item.checkList" :key="index+'_'+index2">
  25. <div class="t45_l_i_option">
  26. <span>{{ i.index }}</span>
  27. <span>:</span>
  28. <div>
  29. <div :style="{width:i.proportion}"></div>
  30. </div>
  31. <span>{{ i.proportion }}</span>
  32. </div>
  33. <!-- <div class="t45_l_i_user">
  34. <template v-for="(j,index3) in i.user" :key="index+'_'+index2+'_'+index3">
  35. <div v-if="item.showAllUser || (!item.showAllUser && index3 < 3)">
  36. <span>{{ j }}</span>
  37. </div>
  38. </template>
  39. <span class="t45_o_btn" v-if="i.user.length > 3 && !item.showAllUser" @click="()=>{item.showAllUser = true,$forceUpdate()}">
  40. <svg t="1756278651830" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="7545" width="200" height="200"><path d="M293.546667 253.013333l264.533333 266.666667-264.106667 266.24 70.4 70.826667 334.506667-337.066667-334.933333-337.066667z" p-id="7546"></path></svg>
  41. </span>
  42. <span class="t45_o_btn" v-if="i.user.length > 3 && item.showAllUser" @click="()=>{item.showAllUser = false,$forceUpdate()}">
  43. <svg style="transform: rotate(180deg);" t="1756278651830" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="7545" width="200" height="200"><path d="M293.546667 253.013333l264.533333 266.666667-264.106667 266.24 70.4 70.826667 334.506667-337.066667-334.933333-337.066667z" p-id="7546"></path></svg>
  44. </span>
  45. </div> -->
  46. </div>
  47. </div>
  48. </div>
  49. </div>
  50. </div>
  51. </div>
  52. <Modal :visible="visible" :width="720" :closeButton="true" @update:visible="visible = false">
  53. <div class="userBox">
  54. <div class="ub_item" v-for="(item,index) in userCheckList" :key="index">
  55. <div class="ub_i_options">{{ item.options }}</div>
  56. <div class="ub_i_user">
  57. <span>选择同学:</span>
  58. <div class="ub_i_u_item" v-for="(item2,index2) in item.user" :key="index+'_'+index2">{{ item2 }}</div>
  59. </div>
  60. </div>
  61. </div>
  62. </Modal>
  63. </template>
  64. <script lang="ts" setup>
  65. import { computed, reactive, ref } from 'vue'
  66. import { ElementTypes } from '@/types/slides'
  67. import Modal from '@/components/Modal.vue'
  68. interface Props {
  69. workArray: any[]
  70. elementList: any[]
  71. }
  72. const props = defineProps<Props>()
  73. const visible = ref<boolean>(false)
  74. let userCheckList = reactive<any[]>([])
  75. // 检查当前是否为选择题(toolType为45)
  76. const isChoiceQuestion = computed(() => {
  77. const frame = props.elementList.find(element => element.type === ElementTypes.FRAME)
  78. return frame?.toolType === 45
  79. })
  80. const optionList: Array<string> = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
  81. const lookStudent = (checkList:any) => {
  82. userCheckList = checkList
  83. visible.value = true
  84. }
  85. // 选择题统计信息
  86. const statistics = computed(() => {
  87. if (!isChoiceQuestion.value || !props.workArray || props.workArray.length === 0) return null
  88. // 统计信息结构优化
  89. let titles: Array<{
  90. title: string,
  91. showAllUser: boolean,
  92. checkList: Array<{ options: string, num: number, proportion: string, index :string, user: Array<[]>}>
  93. }> = []
  94. let totalSubmissions = 0
  95. let tool: string = ''
  96. // 只初始化一次题目结构
  97. let isTitleInitialized = false
  98. props.workArray.forEach((work) => {
  99. if (!work.content) return
  100. try {
  101. if (work.atool === '45') {
  102. const content = JSON.parse(decodeURIComponent(work.content))
  103. const testJson = content.testJson
  104. // 初始化题目结构
  105. if (!isTitleInitialized && Array.isArray(testJson)) {
  106. tool = work.atool
  107. titles = testJson.map((item: any) => ({
  108. title: item.teststitle || '',
  109. showAllUser: false,
  110. checkList: Array.isArray(item.checkList)
  111. ? item.checkList.map((i: any) => ({ options: i, num: 0, user: [], proportion: '', index: '' }))
  112. : []
  113. }))
  114. isTitleInitialized = true
  115. }
  116. // 统计每道题的选项被选择次数
  117. if (Array.isArray(titles) && Array.isArray(testJson)) {
  118. testJson.forEach((item: any, index: number) => {
  119. const answer = item.userAnswer
  120. if (typeof answer === 'number') {
  121. if (titles[index] && titles[index].checkList[answer]) {
  122. titles[index].checkList[answer].num += 1
  123. titles[index].checkList[answer].user.push(work.name)
  124. }
  125. }
  126. else if (Array.isArray(answer) && answer.length > 0) {
  127. answer.forEach((i: number) => {
  128. if (titles[index] && titles[index].checkList[i]) {
  129. titles[index].checkList[i].num += 1
  130. titles[index].checkList[i].user.push(work.name)
  131. }
  132. })
  133. }
  134. })
  135. }
  136. totalSubmissions++
  137. }
  138. }
  139. catch (e) {
  140. // eslint-disable-next-line no-console
  141. console.log('解析选择题答案失败:', e, work.content)
  142. }
  143. })
  144. titles.forEach((item: any) => {
  145. const total = item.checkList.reduce((sum: number, cur: any) => sum + cur.num, 0)
  146. item.checkList.forEach((option: any, index :number) => {
  147. option.proportion = total > 0 ? ((option.num / total) * 100).toFixed(1) + '%' : '0.0%'
  148. option.index = optionList[index]
  149. })
  150. })
  151. // 返回结构优化,便于前端展示
  152. return {
  153. totalSubmissions,
  154. titles,
  155. tool
  156. }
  157. })
  158. </script>
  159. <style lang="scss" scoped>
  160. .choice-statistics-panel {
  161. height: 100%;
  162. padding: 16px;
  163. }
  164. .statistics-content {
  165. background: #f8f9fa;
  166. border-radius: 8px;
  167. border: 1px solid #e9ecef;
  168. padding: 16px;
  169. }
  170. .statistics-title {
  171. font-size: 14px;
  172. font-weight: 600;
  173. color: #333;
  174. margin-bottom: 12px;
  175. text-align: center;
  176. }
  177. .statistics-summary {
  178. text-align: center;
  179. color: #666;
  180. font-size: 13px;
  181. margin-bottom: 16px;
  182. padding: 8px;
  183. background: #fff;
  184. border-radius: 6px;
  185. }
  186. .statistics-chart {
  187. display: flex;
  188. flex-direction: column;
  189. gap: 12px;
  190. }
  191. .statistics-item {
  192. display: flex;
  193. align-items: center;
  194. gap: 8px;
  195. }
  196. .option-label {
  197. min-width: 40px;
  198. font-size: 12px;
  199. color: #333;
  200. font-weight: 500;
  201. }
  202. .option-bar {
  203. flex: 1;
  204. height: 8px;
  205. background: #e9ecef;
  206. border-radius: 4px;
  207. overflow: hidden;
  208. }
  209. .option-fill {
  210. height: 100%;
  211. background: linear-gradient(90deg, #1890ff, #40a9ff);
  212. border-radius: 4px;
  213. transition: width 0.3s ease;
  214. }
  215. .option-count {
  216. min-width: 35px;
  217. font-size: 11px;
  218. color: #666;
  219. text-align: right;
  220. }
  221. .statistics-empty {
  222. padding: 40px 20px;
  223. text-align: center;
  224. color: #999;
  225. font-size: 14px;
  226. }
  227. .tool45{
  228. width: 100%;
  229. height: auto;
  230. &>div{
  231. width: 100%;
  232. height: auto;
  233. margin: 10px 0;
  234. background-color: #F8F9FA;
  235. border-radius: 8px;
  236. border: 1px solid #e9ecef;
  237. box-sizing: border-box;
  238. padding: 15px;
  239. position: relative;
  240. padding-bottom: 21px;
  241. .t45_title{
  242. width: 100%;
  243. display: flex;
  244. // align-items: center;
  245. margin-top: 25px;
  246. margin-bottom: 5px;
  247. span{
  248. font-size: 16px;
  249. color: #1890ff;
  250. }
  251. }
  252. .t45_btn{
  253. position: absolute;
  254. right: 15px;
  255. top: 15px;
  256. span{
  257. font-size: 12px;
  258. cursor: pointer;
  259. color: #409EFF;
  260. }
  261. }
  262. }
  263. .t45_list{
  264. width: 100%;
  265. height: auto;
  266. .t45_l_item{
  267. margin-top: 5px;
  268. .t45_l_i_option{
  269. display: flex;
  270. align-items: center;
  271. height: auto;
  272. width: 100%;
  273. justify-content: space-between;
  274. span{
  275. display: flex;
  276. width: 50px;
  277. &:nth-child(1){
  278. width: 20px;
  279. }
  280. &:nth-of-type(2){
  281. width: 10px;
  282. }
  283. }
  284. &>div{
  285. margin: 0 5px;
  286. width: calc(100% - 10px - 20px - 50px - 10px);
  287. height: 15px;
  288. background-color: #FFFFFF;
  289. // box-sizing: border-box;
  290. border: solid 1px #E9ECEF;
  291. border-radius: 4px;
  292. overflow: hidden;
  293. &>div{
  294. height: 100%;
  295. background-color: #3681FC;
  296. }
  297. }
  298. }
  299. .t45_l_i_user{
  300. width: 100%;
  301. height: auto;
  302. display: flex;
  303. align-items: center;
  304. flex-wrap: wrap;
  305. margin-top: 8px;
  306. margin-bottom: 8px;
  307. &>div{
  308. align-items: center;
  309. flex-wrap: wrap;
  310. height: 35px;
  311. span{
  312. width: auto;
  313. height: auto;
  314. padding: 4px 10px;
  315. background-color: #fff;
  316. border: solid 1px #2f80ed;
  317. border-radius: 6px;
  318. margin-right:8px ;
  319. margin-bottom: 5px;
  320. color: #2f80ed;
  321. }
  322. }
  323. &>span{
  324. width: auto;
  325. height: auto;
  326. padding: 4px 10px;
  327. background-color: #fff;
  328. border: solid 1px #E9ECEF;
  329. border-radius: 6px;
  330. margin-right:8px;
  331. cursor: pointer;
  332. transition: .2s;
  333. &:hover{
  334. background-color: #f1f1f1;
  335. }
  336. svg{
  337. width: 15px;
  338. height: 15px;
  339. }
  340. }
  341. }
  342. }
  343. }
  344. }
  345. .userBox{
  346. width: 100%;
  347. height: auto;
  348. .ub_item{
  349. width: 100%;
  350. height: auto;
  351. margin-bottom: 10px;
  352. border-bottom: solid 1px #D8D8D8;
  353. .ub_i_options{
  354. font-size: 20px;
  355. }
  356. .ub_i_user{
  357. display: flex;
  358. margin: 10px 0;
  359. align-items: center;
  360. flex-wrap: wrap;
  361. &>span{
  362. color: #ADADAD;
  363. }
  364. .ub_i_u_item{
  365. padding: 5px 10px;
  366. border: solid 1px #0061FF;
  367. border-radius: 4px;
  368. background-color: #fff;
  369. margin-left: 10px;
  370. margin-top: 10px;
  371. color: #0061FF;
  372. }
  373. }
  374. }
  375. }
  376. </style>