StudentController.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. using IES.ExamLibrary.Models;
  2. using IES.ExamServer.DI;
  3. using IES.ExamServer.Filters;
  4. using IES.ExamServer.Helper;
  5. using IES.ExamServer.Helpers;
  6. using IES.ExamServer.Models;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Microsoft.Extensions.Caching.Memory;
  9. using Microsoft.Extensions.Hosting;
  10. using Microsoft.VisualBasic;
  11. using System;
  12. using System.Text.Json.Nodes;
  13. using System.Threading.Tasks;
  14. namespace IES.ExamServer.Controllers
  15. {
  16. [ApiController]
  17. [Route("student")]
  18. public class StudentController : BaseController
  19. {
  20. private readonly IConfiguration _configuration;
  21. private readonly IHttpClientFactory _httpClientFactory;
  22. private readonly IMemoryCache _memoryCache;
  23. private readonly ILogger<IndexController> _logger;
  24. private readonly CenterServiceConnectionService _connectionService;
  25. private readonly LiteDBFactory _liteDBFactory;
  26. private readonly DataQueue _dataQueue;
  27. public StudentController(ILogger<IndexController> logger, IConfiguration configuration, IHttpClientFactory httpClientFactory,
  28. IMemoryCache memoryCache, CenterServiceConnectionService connectionService, LiteDBFactory liteDBFactory, DataQueue dataQueue)
  29. {
  30. _logger = logger;
  31. _configuration=configuration;
  32. _httpClientFactory=httpClientFactory;
  33. _memoryCache=memoryCache;
  34. _connectionService=connectionService;
  35. _liteDBFactory=liteDBFactory;
  36. _dataQueue=dataQueue;
  37. }
  38. /// <summary>
  39. /// 学生提交音乐作答
  40. /// </summary>
  41. /// <param name="json"></param>
  42. /// <returns></returns>
  43. [HttpPost("submit-music-ai-result")]
  44. [AuthToken("student")]
  45. public IActionResult SubmitMusicAIResult(JsonNode json)
  46. {
  47. int finished = int.Parse($"{json["finished"]}");
  48. string evaluationId = $"{json["evaluationId"]}";
  49. string taskId = $"{json["taskId"]}";
  50. string questionId = $"{json["questionId"]}";
  51. long costTime = int.Parse($"{json["costTime"]}");
  52. string settingId = $"{json["settingId"]}";
  53. var token = GetAuthTokenInfo();
  54. EvaluationRoundSetting? setting = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationRoundSetting>().FindOne(x => x.id!.Equals(settingId) && evaluationId.Equals(x.evaluationId) && x.activate==1);
  55. EvaluationClient? evaluationClient = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationClient>().FindOne(x => evaluationId.Equals(x.id));
  56. if (evaluationClient!=null && setting!=null && setting.activate==1)
  57. {
  58. string resultId = ShaHashHelper.GetSHA1(evaluationId+_connectionService?.serverDevice?.school?.id+token.id);
  59. EvaluationStudentResult studentResult = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationStudentResult>()
  60. .FindOne(x => resultId.Equals(x.id) && token.id.Equals(x.studentId) && evaluationId.Equals(x.evaluationId));
  61. if (studentResult!=null)
  62. {
  63. long now = DateTimeOffset.Now.ToUnixTimeMilliseconds();
  64. //判断开始时间
  65. if ((setting.startline>0 && setting.startline>now)|| evaluationClient.stime>now)
  66. {
  67. //未到开始时间
  68. return Ok(new { msg = "未到开始时间。", code = 1 });
  69. }
  70. //判断截止时间
  71. long deadline;
  72. if (setting.countdownType==2)
  73. {
  74. deadline= studentResult.startTime+setting.countdown;
  75. }
  76. else
  77. {
  78. deadline= setting.startline+setting.countdown;
  79. }
  80. deadline+= 10*60*1000;//漂移10分钟,允许学生延迟提交,但是前端页面显示的截止时间是准的,时间一到,可自动提交。
  81. //已过截止时间
  82. //if ((deadline>0&&deadline<now)|| evaluationClient.etime<now)
  83. //{
  84. // return Ok(new { msg = "已过截止时间。", code = 2 });
  85. //}
  86. if (!string.IsNullOrWhiteSpace(questionId) && !string.IsNullOrWhiteSpace(taskId) && costTime>0)
  87. {
  88. var result = new EvaluationMusicAIResult()
  89. {
  90. id= ShaHashHelper.GetSHA1($"{evaluationId}{questionId}{taskId}{token.id}"),
  91. taskId = taskId,
  92. questionId = questionId,
  93. costTime = costTime,
  94. finished = finished,
  95. submitTime = now,
  96. questionName=!string.IsNullOrWhiteSpace(evaluationClient?.music?.questionName) ? evaluationClient.music.questionName : null,
  97. evaluationId=evaluationId,
  98. createTime=now,
  99. pushed=0//强制重新推送
  100. };
  101. studentResult.musicAIResult= result;
  102. if (_connectionService!.centerIsConnected)
  103. {
  104. result.pushed=1;
  105. studentResult.musicAIResult.pushed=1;
  106. }
  107. _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationMusicAIResult>().Upsert(result);
  108. _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationStudentResult>().Upsert(studentResult);
  109. return Ok(new { code = 200, studentResult = studentResult, msg = "提交成功!" });
  110. }
  111. else
  112. {
  113. return Ok(new { msg = "提交失败,请检查参数。", code = 3 });
  114. }
  115. }
  116. else
  117. {
  118. return Ok(new { msg = "未找到该学生的作答信息。", code = 4 });
  119. }
  120. }
  121. else {
  122. return Ok(new { msg = "未匹配到正则开考的评测。", code = 5 });
  123. }
  124. }
  125. /// <summary>
  126. /// 学生提交科目作答
  127. /// </summary>
  128. /// <param name="json"></param>
  129. /// <returns></returns>
  130. [HttpPost("submit-subject-result")]
  131. [AuthToken("student")]
  132. public async Task<IActionResult> SubmitSubjectResult(JsonNode json)
  133. {
  134. List<List<string>>? answers = json["answer"]?.ToObject<List<List<string>>>();
  135. string evaluationId = $"{json["evaluationId"]}";
  136. string examId = $"{json["examId"]}";
  137. string subjectId = $"{json["subjectId"]}";
  138. string paperId = $"{json["paperId"]}";
  139. long costTime = int.Parse($"{json["costTime"]}");
  140. string settingId = $"{json["settingId"]}";
  141. var token = GetAuthTokenInfo();
  142. EvaluationRoundSetting? setting = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationRoundSetting>().FindOne(x => x.id!.Equals(settingId) && evaluationId.Equals(x.evaluationId) && x.activate==1);
  143. EvaluationClient? evaluationClient = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationClient>().FindOne(x=>evaluationId.Equals(x.id));
  144. if (evaluationClient!=null && setting!=null && setting.activate==1)
  145. {
  146. string resultId = ShaHashHelper.GetSHA1(evaluationId+_connectionService?.serverDevice?.school?.id+token.id);
  147. EvaluationStudentResult studentResult = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationStudentResult>()
  148. .FindOne(x => resultId.Equals(x.id) && token.id.Equals(x.studentId) && evaluationId.Equals(x.evaluationId));
  149. if (studentResult!=null)
  150. {
  151. long now = DateTimeOffset.Now.ToUnixTimeMilliseconds();
  152. //判断开始时间
  153. if ((setting.startline>0 && setting.startline>now)|| evaluationClient.stime>now)
  154. {
  155. //未到开始时间
  156. return Ok(new { msg = "未到开始时间。", code = 1 });
  157. }
  158. //判断截止时间
  159. long deadline;
  160. if (setting.countdownType==2)
  161. {
  162. deadline= studentResult.startTime+setting.countdown;
  163. }
  164. else
  165. {
  166. deadline= setting.startline+setting.countdown;
  167. }
  168. deadline+= 10*60*1000;//漂移10分钟,允许学生延迟提交,但是前端页面显示的截止时间是准的,时间一到,可自动提交。
  169. //已过截止时间
  170. //if ((deadline>0&&deadline<now)|| evaluationClient.etime<now)
  171. //{
  172. // return Ok(new { msg = "已过截止时间。", code = 2 });
  173. //}
  174. if (!string.IsNullOrWhiteSpace(subjectId) && !string.IsNullOrWhiteSpace(examId) && !string.IsNullOrWhiteSpace(paperId))
  175. {
  176. if (answers!.IsNotEmpty())
  177. {
  178. var subjectExams= evaluationClient.subjects.FindAll(x => examId.Equals(x.examId)&& subjectId.Equals(x.subjectId));
  179. var papers= subjectExams.FirstOrDefault()?.papers.FindAll(x => paperId.Equals(x.paperId));
  180. if (papers.IsNotEmpty())
  181. {
  182. if (answers!.Count()!=papers!.First()?.questionCount)
  183. {
  184. return Ok(new { msg = "提交的答案数量与试卷题目数量不匹配。", code = 5 });
  185. }
  186. }
  187. string subjectResultId = ShaHashHelper.GetSHA1(evaluationClient.id+examId+subjectId+token.id);
  188. var subjectResult = studentResult.subjectResults.Where(x => subjectResultId.Equals(x.id) && subjectId.Equals(x.subjectId)
  189. && examId.Equals(x.examId) && paperId.Equals(x.paperId)).FirstOrDefault();
  190. EvaluationSubjectResult result = new EvaluationSubjectResult()
  191. {
  192. id = subjectResultId,
  193. evaluationId = evaluationId,
  194. examId = examId,
  195. examName = subjectResult?.examName,
  196. subjectId = subjectId,
  197. subjectName = subjectResult?.subjectName,
  198. paperId =paperId,
  199. paperName = subjectResult?.paperName,
  200. createTime=now,
  201. finished=1,
  202. costTime=costTime,
  203. submitTime=now,
  204. answers=answers,
  205. questionCount=papers.IsNotEmpty() ? papers!.First().questionCount : 0,
  206. pushed=0//强制重新推送
  207. };
  208. if (subjectResult!=null)
  209. {
  210. subjectResult.answers=answers;
  211. subjectResult.finished=1;
  212. subjectResult.costTime=costTime;
  213. subjectResult.submitTime=now;
  214. subjectResult.pushed=0;//强制重新推送
  215. }
  216. else
  217. {
  218. studentResult.subjectResults.Add(result);
  219. }
  220. if (_connectionService!.centerIsConnected)
  221. {
  222. result.pushed=1;
  223. if (subjectResult!=null)
  224. {
  225. subjectResult!.pushed=1;
  226. }
  227. await _dataQueue.TryAddAsync(new SubjectPushData(result, studentResult));
  228. }
  229. _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationSubjectResult>().Upsert(result);
  230. _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationStudentResult>().Upsert(studentResult);
  231. }
  232. }
  233. return Ok(new { code = 200, studentResult = studentResult, msg = "提交成功!" });
  234. }
  235. else {
  236. return Ok(new { msg = "未找到该学生的作答信息。", code = 4});
  237. }
  238. }
  239. else
  240. {
  241. return Ok(new { msg = "未匹配到正则开考的评测。", code = 3 });
  242. }
  243. }
  244. /// <summary>
  245. /// 获取学生当前考试的作答信息。
  246. /// </summary>
  247. /// <param name="json"></param>
  248. /// <returns></returns>
  249. [HttpPost("load-evaluation-result")]
  250. [AuthToken("student")]
  251. public IActionResult LoadEvaluationResult(JsonNode json)
  252. {
  253. string evaluationId = $"{json["evaluationId"]}";
  254. var token = GetAuthTokenInfo();//6af32bbd-144e-4366-8bc0-61ba4c85677c
  255. string resultId = ShaHashHelper.GetSHA1(evaluationId+_connectionService?.serverDevice?.school?.id+token.id);
  256. string? scoolId = _connectionService?.serverDevice?.school?.id;
  257. EvaluationStudentResult studentResult = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationStudentResult>()
  258. .FindOne(x=> resultId.Equals(x.id) && !string.IsNullOrWhiteSpace(x.schoolId) && x.schoolId.Equals(scoolId)&& token.id.Equals(x.studentId) && evaluationId.Equals(x.evaluationId));
  259. if (studentResult!=null)
  260. {
  261. //标记开始作答
  262. if (studentResult.finished<1)
  263. {
  264. studentResult.finished=1;
  265. }
  266. if (studentResult.startTime<=0)
  267. {
  268. studentResult.startTime=DateTimeOffset.Now.ToUnixTimeMilliseconds();
  269. }
  270. _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationStudentResult>().Update(studentResult);
  271. return Ok(new { code = 200, studentResult = studentResult });
  272. }
  273. else {
  274. return Ok(new { msg = "未找到该学生的作答信息。", code = 404});
  275. }
  276. }
  277. /// <summary>
  278. /// 登录
  279. /// </summary>
  280. /// <param name="json"></param>
  281. /// <returns></returns>
  282. [HttpPost("login")]
  283. public IActionResult Login(JsonNode json)
  284. {
  285. string studentId = $"{json["studentId"]}";
  286. string studentName = $"{json["studentName"]}";
  287. string evaluationId = $"{json["evaluationId"]}";
  288. string settingId = $"{json["settingId"]}";
  289. EvaluationRoundSetting? setting = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationRoundSetting>().FindOne(x => x.id!.Equals(settingId) && evaluationId.Equals(x.evaluationId) && x.activate==1);
  290. EvaluationClient? evaluationClient = null;
  291. if (setting!=null)
  292. {
  293. evaluationClient = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationClient>().FindOne(x => x.id!.Equals(setting.evaluationId));
  294. if (evaluationClient!=null)
  295. {
  296. //检查是否在作答时间内
  297. (code, msg)= CheckActivate(evaluationClient, setting);
  298. if (code==200)
  299. {
  300. long time = DateTimeOffset.Now.ToUnixTimeMilliseconds();
  301. _memoryCache.TryGetValue(Constant._KeyServerDevice, out ServerDevice? server);
  302. School? school = null;
  303. ///获取名单文件,解析信息,应该在设置开考轮次的时候进行解析,并放在数据库中,并且在此时看是否是需要 分配试卷,还是在登录的时候获取试卷。
  304. if (server!=null)
  305. {
  306. school = server.school;
  307. }
  308. EvaluationMember? member = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationMember>().FindOne(x => studentId.Equals(x.id)&& studentName.Equals(x.name));
  309. if (member!=null)
  310. {
  311. if (evaluationId.Equals(member.evaluationId))
  312. {
  313. string x_auth_token = JwtAuthExtension.CreateAuthToken("www.teammodel.cn", studentId, studentName, picture: string.Empty, ExamConstant.JwtSecretKey, ExamConstant.ScopeStudent, 8, schoolID: school?.id, new string[] { "student" },expire: 1, year: member.year);
  314. return Ok(new { code = 200, x_auth_token = x_auth_token });
  315. }
  316. else
  317. {
  318. return Ok(new { msg = "当前考试未添加该学生!", code = 7 });
  319. }
  320. }
  321. else
  322. {
  323. return Ok(new { msg = "学者账号和姓名不匹配!", code = 1 });
  324. }
  325. }
  326. else {
  327. return Ok(new { msg = msg, code = code });
  328. }
  329. }
  330. else {
  331. return Ok(new { msg = "未找到考试设置。", code = 6 });
  332. }
  333. }
  334. else {
  335. return Ok(new { msg = "未找到考试设置。", code = 2 });
  336. }
  337. }
  338. /// <summary>
  339. /// 学生端获取激活的考试
  340. /// </summary>
  341. /// <param name="json"></param>
  342. /// <returns></returns>
  343. [HttpPost("get-activate-evaluation")]
  344. public IActionResult GetActivateEvaluation(JsonNode json)
  345. {
  346. // _connectionService.serverDevice.school.id?
  347. try
  348. {
  349. IEnumerable<EvaluationRoundSetting>? settings = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationRoundSetting>().Find(x =>x.activate==1 );
  350. if (settings != null && settings.Count() > 0)
  351. {
  352. if (settings.Count()>1)
  353. {
  354. msg="有多个正在开考的评测,请监考教师重新设置开考评测。";
  355. code=2;
  356. }
  357. else
  358. {
  359. EvaluationRoundSetting? setting = settings.First(); ;
  360. EvaluationClient evaluationClient = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationClient>().FindOne(x => x.id ==setting.evaluationId);
  361. if (evaluationClient!=null)
  362. {
  363. (code ,msg)= CheckActivate(evaluationClient, setting);
  364. var anonymousObject = new Dictionary<string, object?>();
  365. var properties = evaluationClient.GetType().GetProperties();
  366. foreach (var property in properties)
  367. {
  368. if (!property.Name.Equals("shortCode") && !property.Name.Equals("openCode"))
  369. {
  370. anonymousObject[property.Name] = property.GetValue(evaluationClient);
  371. }
  372. }
  373. return Ok(new { evaluationClient = anonymousObject, code = code, msg = msg, setting });
  374. }
  375. else
  376. {
  377. msg="未找到评测信息。";
  378. code=6;
  379. }
  380. //foreach (var subject in evaluationClient.subjects)
  381. //{
  382. // foreach (var paper in subject.papers)
  383. // {
  384. // paper.blob=$"package/{evaluationClient.id}/papers/{paper.paperId}";
  385. // }
  386. //}
  387. }
  388. }
  389. else
  390. {
  391. msg="暂无正在开考的评测";
  392. code=1;
  393. }
  394. }
  395. catch (Exception ex) { }
  396. return Ok(new { msg ,code});
  397. }
  398. private (int code, string msg) CheckActivate(EvaluationClient evaluationClient, EvaluationRoundSetting setting)
  399. {
  400. code = 200;
  401. if (evaluationClient.scope!.Equals("school"))
  402. {
  403. if (!evaluationClient!.ownerId!.Equals($"{_connectionService.serverDevice?.school?.id}"))
  404. {
  405. msg="授权学校与评测归属学校不一致。";
  406. code=3;
  407. }
  408. }
  409. //当前时间
  410. long now = DateTimeOffset.Now.ToUnixTimeMilliseconds();
  411. //if (setting.countdownType>0)
  412. //{
  413. //}
  414. //else
  415. //{
  416. //}
  417. if ((setting.startline>0 &&setting.startline>now) || evaluationClient.stime>now)
  418. {
  419. msg="评测暂未开始。";
  420. code=4;
  421. }
  422. if ((setting.deadline>0 && setting.deadline<now)|| evaluationClient.etime<now)
  423. {
  424. msg="评测已经结束。";
  425. code=5;
  426. }
  427. return (code, msg);
  428. }
  429. }
  430. }