StudentController.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. namespace IES.ExamServer.Controllers
  14. {
  15. [ApiController]
  16. [Route("student")]
  17. public class StudentController : BaseController
  18. {
  19. private readonly IConfiguration _configuration;
  20. private readonly IHttpClientFactory _httpClientFactory;
  21. private readonly IMemoryCache _memoryCache;
  22. private readonly ILogger<IndexController> _logger;
  23. private readonly CenterServiceConnectionService _connectionService;
  24. private readonly LiteDBFactory _liteDBFactory;
  25. public StudentController(ILogger<IndexController> logger, IConfiguration configuration, IHttpClientFactory httpClientFactory,
  26. IMemoryCache memoryCache, CenterServiceConnectionService connectionService, LiteDBFactory liteDBFactory)
  27. {
  28. _logger = logger;
  29. _configuration=configuration;
  30. _httpClientFactory=httpClientFactory;
  31. _memoryCache=memoryCache;
  32. _connectionService=connectionService;
  33. _liteDBFactory=liteDBFactory;
  34. }
  35. /// <summary>
  36. /// 学生提交科目作答
  37. /// </summary>
  38. /// <param name="json"></param>
  39. /// <returns></returns>
  40. [HttpPost("answer-subject-result")]
  41. [AuthToken("student")]
  42. public IActionResult AnswerSubjectResult(JsonNode json)
  43. {
  44. List<List<string>>? answers = json["answer"]?.ToObject<List<List<string>>>();
  45. string evaluationId = $"{json["evaluationId"]}";
  46. string examId = $"{json["examId"]}";
  47. string subjectId = $"{json["subjectId"]}";
  48. string paperId = $"{json["paperId"]}";
  49. int costTime = int.Parse($"{json["costTime"]}");
  50. string settingId = $"{json["settingId"]}";
  51. var token = GetAuthTokenInfo();
  52. EvaluationRoundSetting? setting = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationRoundSetting>().FindOne(x => x.id!.Equals(settingId) && evaluationId.Equals(x.evaluationId) && x.activate==1);
  53. EvaluationClient? evaluationClient = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationClient>().FindOne(x=>evaluationId.Equals(x.id));
  54. if (evaluationClient!=null && setting!=null && setting.activate==1)
  55. {
  56. string resultId = ShaHashHelper.GetSHA1(evaluationId+_connectionService?.serverDevice?.school?.id+token.id);
  57. EvaluationStudentResult studentResult = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationStudentResult>()
  58. .FindOne(x => resultId.Equals(x.id) && token.id.Equals(x.studentId) && evaluationId.Equals(x.evaluationId));
  59. if (studentResult!=null)
  60. {
  61. long now = DateTimeOffset.Now.ToUnixTimeMilliseconds();
  62. //判断开始时间
  63. if ((setting.startline>0 && setting.startline>now)|| evaluationClient.stime>now)
  64. {
  65. //未到开始时间
  66. return Ok(new { msg = "未到开始时间。", code = 1 });
  67. }
  68. //判断截止时间
  69. long deadline;
  70. if (setting.countdownType==2)
  71. {
  72. deadline= studentResult.startTime+setting.countdown;
  73. }
  74. else
  75. {
  76. deadline= setting.startline+setting.countdown;
  77. }
  78. deadline+= 10*60*1000;//漂移10分钟,允许学生延迟提交,但是前端页面显示的截止时间是准的,时间一到,可自动提交。
  79. if ((deadline>0&&deadline<now)|| evaluationClient.etime<now)
  80. {
  81. //已过截止时间
  82. return Ok(new { msg = "已过截止时间。", code = 2 });
  83. }
  84. if (!string.IsNullOrWhiteSpace(subjectId) && !string.IsNullOrWhiteSpace(examId) && !string.IsNullOrWhiteSpace(paperId)&& costTime>0)
  85. {
  86. if (answers!.IsNotEmpty())
  87. {
  88. var subjectExams= evaluationClient.subjects.FindAll(x => examId.Equals(x.examId)&& subjectId.Equals(x.subjectId));
  89. var papers= subjectExams.FirstOrDefault()?.papers.FindAll(x => paperId.Equals(x.paperId));
  90. if (papers.IsNotEmpty())
  91. {
  92. if (answers!.Count()!=papers!.First()?.questionCount)
  93. {
  94. return Ok(new { msg = "提交的答案数量与试卷题目数量不匹配。", code = 5 });
  95. }
  96. }
  97. string subjectResultId = ShaHashHelper.GetSHA1(evaluationClient.id+examId+subjectId+token.id);
  98. var subjectResult = studentResult.subjectResults.Where(x => subjectResultId.Equals(x.id) && subjectId.Equals(x.subjectId)
  99. && examId.Equals(x.examId) && paperId.Equals(x.paperId)).FirstOrDefault();
  100. if (subjectResult!=null)
  101. {
  102. subjectResult.answers=answers;
  103. subjectResult.finished=1;
  104. subjectResult.costTime=costTime;
  105. subjectResult.submitTime=now;
  106. subjectResult.pushed=0;//强制重新推送
  107. }
  108. EvaluationSubjectResult result = new EvaluationSubjectResult()
  109. {
  110. id = subjectResultId,
  111. evaluationId = evaluationId,
  112. examId = examId,
  113. examName = subjectResult?.examName,
  114. subjectId = subjectId,
  115. subjectName = subjectResult?.subjectName,
  116. paperId =paperId,
  117. paperName = subjectResult?.paperName,
  118. createTime=now,
  119. finished=1,
  120. costTime=costTime,
  121. submitTime=now,
  122. answers=answers,
  123. questionCount=papers.IsNotEmpty()? papers!.First().questionCount : 0,
  124. pushed=0//强制重新推送
  125. };
  126. _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationSubjectResult>().Upsert(result);
  127. if (_connectionService!.centerIsConnected)
  128. {
  129. }
  130. }
  131. }
  132. return Ok(new { code = 200, studentResult = studentResult, msg = "提交成功!" });
  133. }
  134. else {
  135. return Ok(new { msg = "未找到该学生的作答信息。", code = 4});
  136. }
  137. }
  138. else
  139. {
  140. return Ok(new { msg = "未匹配到正则开考的评测。", code = 3 });
  141. }
  142. }
  143. /// <summary>
  144. /// 获取学生当前考试的作答信息。
  145. /// </summary>
  146. /// <param name="json"></param>
  147. /// <returns></returns>
  148. [HttpPost("load-evaluation-result")]
  149. [AuthToken("student")]
  150. public IActionResult LoadEvaluationResult(JsonNode json)
  151. {
  152. string evaluationId = $"{json["evaluationId"]}";
  153. var token = GetAuthTokenInfo();//6af32bbd-144e-4366-8bc0-61ba4c85677c
  154. string resultId = ShaHashHelper.GetSHA1(evaluationId+_connectionService?.serverDevice?.school?.id+token.id);
  155. string? scoolId = _connectionService?.serverDevice?.school?.id;
  156. EvaluationStudentResult studentResult = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationStudentResult>()
  157. .FindOne(x=> resultId.Equals(x.id) && !string.IsNullOrWhiteSpace(x.schoolId) && x.schoolId.Equals(scoolId)&& token.id.Equals(x.studentId) && evaluationId.Equals(x.evaluationId));
  158. if (studentResult!=null)
  159. {
  160. //标记开始作答
  161. if (studentResult.finished<1)
  162. {
  163. studentResult.finished=1;
  164. }
  165. if (studentResult.startTime<=0)
  166. {
  167. studentResult.startTime=DateTimeOffset.Now.ToUnixTimeMilliseconds();
  168. }
  169. _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationStudentResult>().Update(studentResult);
  170. return Ok(new { code = 200, studentResult = studentResult });
  171. }
  172. else {
  173. return Ok(new { msg = "未找到该学生的作答信息。", code = 404});
  174. }
  175. }
  176. /// <summary>
  177. /// 登录
  178. /// </summary>
  179. /// <param name="json"></param>
  180. /// <returns></returns>
  181. [HttpPost("login")]
  182. public IActionResult Login(JsonNode json)
  183. {
  184. string studentId = $"{json["studentId"]}";
  185. string studentName = $"{json["studentName"]}";
  186. string evaluationId = $"{json["evaluationId"]}";
  187. string settingId = $"{json["settingId"]}";
  188. EvaluationRoundSetting? setting = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationRoundSetting>().FindOne(x => x.id!.Equals(settingId) && evaluationId.Equals(x.evaluationId) && x.activate==1);
  189. EvaluationClient? evaluationClient = null;
  190. if (setting!=null)
  191. {
  192. evaluationClient = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationClient>().FindOne(x => x.id!.Equals(setting.evaluationId));
  193. if (evaluationClient!=null)
  194. {
  195. //检查是否在作答时间内
  196. (code, msg)= CheckActivate(evaluationClient, setting);
  197. if (code==200)
  198. {
  199. long time = DateTimeOffset.Now.ToUnixTimeMilliseconds();
  200. _memoryCache.TryGetValue(Constant._KeyServerDevice, out ServerDevice? server);
  201. School? school = null;
  202. ///获取名单文件,解析信息,应该在设置开考轮次的时候进行解析,并放在数据库中,并且在此时看是否是需要 分配试卷,还是在登录的时候获取试卷。
  203. if (server!=null)
  204. {
  205. school = server.school;
  206. }
  207. EvaluationMember? member = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationMember>().FindOne(x => studentId.Equals(x.id)&& studentName.Equals(x.name));
  208. if (member!=null)
  209. {
  210. if (evaluationId.Equals(member.evaluationId))
  211. {
  212. 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);
  213. return Ok(new { code = 200, x_auth_token = x_auth_token });
  214. }
  215. else
  216. {
  217. return Ok(new { msg = "当前考试未添加该学生!", code = 7 });
  218. }
  219. }
  220. else
  221. {
  222. return Ok(new { msg = "学者账号和姓名不匹配!", code = 1 });
  223. }
  224. }
  225. else {
  226. return Ok(new { msg = msg, code = code });
  227. }
  228. }
  229. else {
  230. return Ok(new { msg = "未找到考试设置。", code = 6 });
  231. }
  232. }
  233. else {
  234. return Ok(new { msg = "未找到考试设置。", code = 2 });
  235. }
  236. }
  237. /// <summary>
  238. /// 学生端获取激活的考试
  239. /// </summary>
  240. /// <param name="json"></param>
  241. /// <returns></returns>
  242. [HttpPost("get-activate-evaluation")]
  243. public IActionResult GetActivateEvaluation(JsonNode json)
  244. {
  245. // _connectionService.serverDevice.school.id?
  246. try
  247. {
  248. IEnumerable<EvaluationRoundSetting>? settings = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationRoundSetting>().Find(x =>x.activate==1 );
  249. if (settings != null && settings.Count() > 0)
  250. {
  251. if (settings.Count()>1)
  252. {
  253. msg="有多个正在开考的评测,请监考教师重新设置开考评测。";
  254. code=2;
  255. }
  256. else
  257. {
  258. EvaluationRoundSetting? setting = settings.First(); ;
  259. EvaluationClient evaluationClient = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationClient>().FindOne(x => x.id ==setting.evaluationId);
  260. if (evaluationClient!=null)
  261. {
  262. (code ,msg)= CheckActivate(evaluationClient, setting);
  263. var anonymousObject = new Dictionary<string, object?>();
  264. var properties = evaluationClient.GetType().GetProperties();
  265. foreach (var property in properties)
  266. {
  267. if (!property.Name.Equals("shortCode") && !property.Name.Equals("openCode"))
  268. {
  269. anonymousObject[property.Name] = property.GetValue(evaluationClient);
  270. }
  271. }
  272. return Ok(new { evaluationClient = anonymousObject, code = code, msg = msg, setting });
  273. }
  274. else
  275. {
  276. msg="未找到评测信息。";
  277. code=6;
  278. }
  279. //foreach (var subject in evaluationClient.subjects)
  280. //{
  281. // foreach (var paper in subject.papers)
  282. // {
  283. // paper.blob=$"package/{evaluationClient.id}/papers/{paper.paperId}";
  284. // }
  285. //}
  286. }
  287. }
  288. else
  289. {
  290. msg="暂无正在开考的评测";
  291. code=1;
  292. }
  293. }
  294. catch (Exception ex) { }
  295. return Ok(new { msg ,code});
  296. }
  297. private (int code, string msg) CheckActivate(EvaluationClient evaluationClient, EvaluationRoundSetting setting)
  298. {
  299. code = 200;
  300. if (evaluationClient.scope!.Equals("school"))
  301. {
  302. if (!evaluationClient!.ownerId!.Equals($"{_connectionService.serverDevice?.school?.id}"))
  303. {
  304. msg="授权学校与评测归属学校不一致。";
  305. code=3;
  306. }
  307. }
  308. //当前时间
  309. long now = DateTimeOffset.Now.ToUnixTimeMilliseconds();
  310. //if (setting.countdownType>0)
  311. //{
  312. //}
  313. //else
  314. //{
  315. //}
  316. if ((setting.startline>0 &&setting.startline>now) || evaluationClient.stime>now)
  317. {
  318. msg="评测暂未开始。";
  319. code=4;
  320. }
  321. if ((setting.deadline>0 && setting.deadline<now)|| evaluationClient.etime<now)
  322. {
  323. msg="评测已经结束。";
  324. code=5;
  325. }
  326. return (code, msg);
  327. }
  328. }
  329. }