public IActionResult CreateStudentQuestion(int code1)
        {
            List <quests_of_test> qs = (from x in db.quests_of_test

                                        where x.test_code == code1
                                        select x).OrderBy(x => Guid.NewGuid()).ToList();

            var rd   = new Random();
            var list = db.students.ToList();

            foreach (var item in qs)
            {
                var StudentTest = new student_test_detail();
                StudentTest.id_question = item.id_question;
                StudentTest.test_code   = code1;
                StudentTest.id_student  = 1;
                question q      = db.questions.SingleOrDefault(x => x.id_question == item.id_question);
                string[] answer = { q.answer_a, q.answer_b, q.answer_c, q.answer_d };
                answer = ShuffleArray.Randomize(answer);
                StudentTest.answer_a = answer[0];
                StudentTest.answer_b = answer[1];
                StudentTest.answer_c = answer[2];
                StudentTest.answer_d = answer[3];
                db.student_test_detail.Add(StudentTest);
                db.SaveChanges();
            }
            return(RedirectToAction("DoingTest", new { test_code = code1 }));
        }
Beispiel #2
0
        public ActionResult Upload(FormCollection formCollection)
        {
            var questionList = new List <question>();                                // tạo 1 danh sách có phần tử có kiểu question

            if (Request != null)
            {
                HttpPostedFileBase file = Request.Files["UploadedFile"];
                if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName))
                {
                    string fileName        = file.FileName;
                    string fileContentType = file.ContentType;
                    byte[] fileBytes       = new byte[file.ContentLength];
                    var    data            = file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength));
                    ExcelPackage.LicenseContext = LicenseContext.NonCommercial; // cấp phép để sử dụng
                    using (var package = new ExcelPackage(file.InputStream))
                    {
                        // tạo 1 trang tính Excel
                        var currentSheet = package.Workbook.Worksheets;
                        var workSheet    = currentSheet.First();
                        var noOfCol      = workSheet.Dimension.End.Column;
                        var noOfRow      = workSheet.Dimension.End.Row;
                        for (int rowIterator = 2; rowIterator <= noOfRow; rowIterator++) // tạo 1 vòng lặp để lưu dữ liệu từng dòng của Excel
                        {
                            var question = new question();                               //tạo 1 biến có kiểu question
                            // lấy dữ liệu từ excel lưu vào biến vừa tạo(1 biến lưu 1 dữ liệu của 1 dòng Excel)
                            question.id_question    = Convert.ToInt32(workSheet.Cells[rowIterator, 1].Value);
                            question.id_subject     = Convert.ToInt32(workSheet.Cells[rowIterator, 2].Value);
                            question.unit           = Convert.ToInt32(workSheet.Cells[rowIterator, 3].Value);
                            question.img_content    = workSheet.Cells[rowIterator, 4].Value.ToString();
                            question.content        = workSheet.Cells[rowIterator, 5].Value.ToString();
                            question.answer_a       = workSheet.Cells[rowIterator, 6].Value.ToString();
                            question.answer_b       = workSheet.Cells[rowIterator, 7].Value.ToString();
                            question.answer_c       = workSheet.Cells[rowIterator, 8].Value.ToString();
                            question.answer_d       = workSheet.Cells[rowIterator, 9].Value.ToString();
                            question.correct_answer = workSheet.Cells[rowIterator, 10].Value.ToString();
                            questionList.Add(question);                                 // lưu biến vào danh sách đã tạo ở trên
                        }
                    }
                }
            }
            using (trac_nghiem_onlineEntities trac_nghiem_onlineEntities = new trac_nghiem_onlineEntities()) // gọi đến CSDL
            {
                foreach (var item in questionList)                                                           // tạo vào lặp để xử lý dữ liệu trong danh sách
                {
                    trac_nghiem_onlineEntities.questions.Add(item);                                          // thêm phần tử của danh sách vào bảng
                }
                trac_nghiem_onlineEntities.SaveChanges();                                                    // lưu thay đổi
            }
            return(RedirectToAction("QuestionManager"));                                                     // trả về View quản lý câu hỏi
        }