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. SubjectPushData subjectPushData = new SubjectPushData(result, studentResult);
  221. if (_connectionService!.centerIsConnected)
  222. {
  223. result.pushed=1;
  224. if (subjectResult!=null)
  225. {
  226. subjectResult!.pushed=1;
  227. }
  228. await _dataQueue.TryAddAsync(subjectPushData);
  229. }
  230. _liteDBFactory.GetLiteDatabase().GetCollection<SubjectPushData>().Upsert(subjectPushData);
  231. _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationStudentResult>().Upsert(studentResult);
  232. }
  233. }
  234. return Ok(new { code = 200, studentResult = studentResult, msg = "提交成功!" });
  235. }
  236. else {
  237. return Ok(new { msg = "未找到该学生的作答信息。", code = 4});
  238. }
  239. }
  240. else
  241. {
  242. return Ok(new { msg = "未匹配到正则开考的评测。", code = 3 });
  243. }
  244. }
  245. /// <summary>
  246. /// 获取学生当前考试的作答信息。
  247. /// </summary>
  248. /// <param name="json"></param>
  249. /// <returns></returns>
  250. [HttpPost("load-evaluation-result")]
  251. [AuthToken("student")]
  252. public IActionResult LoadEvaluationResult(JsonNode json)
  253. {
  254. string evaluationId = $"{json["evaluationId"]}";
  255. var token = GetAuthTokenInfo();//6af32bbd-144e-4366-8bc0-61ba4c85677c
  256. string resultId = ShaHashHelper.GetSHA1(evaluationId+_connectionService?.serverDevice?.school?.id+token.id);
  257. string? scoolId = _connectionService?.serverDevice?.school?.id;
  258. EvaluationStudentResult studentResult = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationStudentResult>()
  259. .FindOne(x=> resultId.Equals(x.id) && !string.IsNullOrWhiteSpace(x.schoolId) && x.schoolId.Equals(scoolId)&& token.id.Equals(x.studentId) && evaluationId.Equals(x.evaluationId));
  260. if (studentResult!=null)
  261. {
  262. //标记开始作答
  263. if (studentResult.finished<1)
  264. {
  265. studentResult.finished=1;
  266. }
  267. if (studentResult.startTime<=0)
  268. {
  269. studentResult.startTime=DateTimeOffset.Now.ToUnixTimeMilliseconds();
  270. }
  271. _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationStudentResult>().Update(studentResult);
  272. return Ok(new { code = 200, studentResult = studentResult });
  273. }
  274. else {
  275. return Ok(new { msg = "未找到该学生的作答信息。", code = 404});
  276. }
  277. }
  278. /// <summary>
  279. /// 登录
  280. /// </summary>
  281. /// <param name="json"></param>
  282. /// <returns></returns>
  283. [HttpPost("login")]
  284. public IActionResult Login(JsonNode json)
  285. {
  286. string studentId = $"{json["studentId"]}";
  287. string studentName = $"{json["studentName"]}";
  288. string evaluationId = $"{json["evaluationId"]}";
  289. string settingId = $"{json["settingId"]}";
  290. EvaluationRoundSetting? setting = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationRoundSetting>().FindOne(x => x.id!.Equals(settingId) && evaluationId.Equals(x.evaluationId) && x.activate==1);
  291. EvaluationClient? evaluationClient = null;
  292. if (setting!=null)
  293. {
  294. evaluationClient = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationClient>().FindOne(x => x.id!.Equals(setting.evaluationId));
  295. if (evaluationClient!=null)
  296. {
  297. //检查是否在作答时间内
  298. (code, msg)= CheckActivate(evaluationClient, setting);
  299. if (code==200)
  300. {
  301. long time = DateTimeOffset.Now.ToUnixTimeMilliseconds();
  302. _memoryCache.TryGetValue(Constant._KeyServerDevice, out ServerDevice? server);
  303. School? school = null;
  304. ///获取名单文件,解析信息,应该在设置开考轮次的时候进行解析,并放在数据库中,并且在此时看是否是需要 分配试卷,还是在登录的时候获取试卷。
  305. if (server!=null)
  306. {
  307. school = server.school;
  308. }
  309. EvaluationMember? member = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationMember>().FindOne(x => studentId.Equals(x.id)&& studentName.Equals(x.name));
  310. if (member!=null)
  311. {
  312. if (evaluationId.Equals(member.evaluationId))
  313. {
  314. 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);
  315. return Ok(new { code = 200, x_auth_token = x_auth_token });
  316. }
  317. else
  318. {
  319. return Ok(new { msg = "当前考试未添加该学生!", code = 7 });
  320. }
  321. }
  322. else
  323. {
  324. return Ok(new { msg = "学者账号和姓名不匹配!", code = 1 });
  325. }
  326. }
  327. else {
  328. return Ok(new { msg = msg, code = code });
  329. }
  330. }
  331. else {
  332. return Ok(new { msg = "未找到考试设置。", code = 6 });
  333. }
  334. }
  335. else {
  336. return Ok(new { msg = "未找到考试设置。", code = 2 });
  337. }
  338. }
  339. /// <summary>
  340. /// 学生端获取激活的考试
  341. /// </summary>
  342. /// <param name="json"></param>
  343. /// <returns></returns>
  344. [HttpPost("get-activate-evaluation")]
  345. public IActionResult GetActivateEvaluation(JsonNode json)
  346. {
  347. // _connectionService.serverDevice.school.id?
  348. try
  349. {
  350. IEnumerable<EvaluationRoundSetting>? settings = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationRoundSetting>().Find(x =>x.activate==1 );
  351. if (settings != null && settings.Count() > 0)
  352. {
  353. if (settings.Count()>1)
  354. {
  355. msg="有多个正在开考的评测,请监考教师重新设置开考评测。";
  356. code=2;
  357. }
  358. else
  359. {
  360. EvaluationRoundSetting? setting = settings.First(); ;
  361. EvaluationClient evaluationClient = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationClient>().FindOne(x => x.id ==setting.evaluationId);
  362. if (evaluationClient!=null)
  363. {
  364. (code ,msg)= CheckActivate(evaluationClient, setting);
  365. var anonymousObject = new Dictionary<string, object?>();
  366. var properties = evaluationClient.GetType().GetProperties();
  367. foreach (var property in properties)
  368. {
  369. if (!property.Name.Equals("shortCode") && !property.Name.Equals("openCode"))
  370. {
  371. anonymousObject[property.Name] = property.GetValue(evaluationClient);
  372. }
  373. }
  374. return Ok(new { evaluationClient = anonymousObject, code = code, msg = msg, setting });
  375. }
  376. else
  377. {
  378. msg="未找到评测信息。";
  379. code=6;
  380. }
  381. //foreach (var subject in evaluationClient.subjects)
  382. //{
  383. // foreach (var paper in subject.papers)
  384. // {
  385. // paper.blob=$"package/{evaluationClient.id}/papers/{paper.paperId}";
  386. // }
  387. //}
  388. }
  389. }
  390. else
  391. {
  392. msg="暂无正在开考的评测";
  393. code=1;
  394. }
  395. }
  396. catch (Exception ex) { }
  397. return Ok(new { msg ,code});
  398. }
  399. private (int code, string msg) CheckActivate(EvaluationClient evaluationClient, EvaluationRoundSetting setting)
  400. {
  401. code = 200;
  402. if (evaluationClient.scope!.Equals("school"))
  403. {
  404. if (!evaluationClient!.ownerId!.Equals($"{_connectionService.serverDevice?.school?.id}"))
  405. {
  406. msg="授权学校与评测归属学校不一致。";
  407. code=3;
  408. }
  409. }
  410. //当前时间
  411. long now = DateTimeOffset.Now.ToUnixTimeMilliseconds();
  412. //if (setting.countdownType>0)
  413. //{
  414. //}
  415. //else
  416. //{
  417. //}
  418. if ((setting.startline>0 &&setting.startline>now) || evaluationClient.stime>now)
  419. {
  420. msg="评测暂未开始。";
  421. code=4;
  422. }
  423. if ((setting.deadline>0 && setting.deadline<now)|| evaluationClient.etime<now)
  424. {
  425. msg="评测已经结束。";
  426. code=5;
  427. }
  428. return (code, msg);
  429. }
  430. }
  431. }