Skip to content

Instantly share code, notes, and snippets.

@rec4i
Created March 1, 2022 19:24
Show Gist options
  • Save rec4i/47cdd3007f8f396f9e51698d461ffb97 to your computer and use it in GitHub Desktop.
Save rec4i/47cdd3007f8f396f9e51698d461ffb97 to your computer and use it in GitHub Desktop.
Exam-System-Exam-Results
using Microsoft.AspNetCore.Mvc;
using WebApi.Authorization;
using WebApi.Entities;
using Kaynak_Kod.Entities.Questions;
using Kaynak_Kod.Services.Exams;
using Kaynak_Kod.Entities.Exams;
namespace Kaynak_Kod.Controllers.ExamControllers
{
[ApiController]
[Route("api/[controller]")]
public class ExamResultController : ControllerBase
{
private IExamResultService _ExamResultService;
public ExamResultController(IExamResultService ExamService)
{
_ExamResultService = ExamService;
}
[Authorize(Role.Admin)]
[HttpPost("Get_All_Exam_List_With_Pagination")]
public IActionResult Get_All_Exam_List_With_Pagination(pagenation_request x)
{
var rd = _ExamResultService.Get_All_Exam_List_With_Pagination(x);
return Ok(rd);
}
[Authorize(Role.Admin)]
[HttpPost("Get_By_Exam_Guid_Exam_Resutls")]
public IActionResult Get_By_Exam_Guid_Exam_Resutls(Exam x)
{
var rd = _ExamResultService.Get_By_Exam_Guid_Exam_Resutls(x);
return Ok(rd);
}
}
}
@{
Layout = "~/Views/Shared/Anasayfa_Layout.cshtml";
}
<link href="https://unpkg.com/bootstrap-table@1.19.1/dist/bootstrap-table.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/tableexport.jquery.plugin@1.10.21/tableExport.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/tableexport.jquery.plugin@1.10.21/libs/jsPDF/jspdf.min.js"></script>
<script
src="https://cdn.jsdelivr.net/npm/tableexport.jquery.plugin@1.10.21/libs/jsPDF-AutoTable/jspdf.plugin.autotable.js"></script>
<script src="https://unpkg.com/bootstrap-table@1.19.1/dist/bootstrap-table.min.js"></script>
<script src="https://unpkg.com/bootstrap-table@1.19.1/dist/extensions/export/bootstrap-table-export.min.js"></script>
<script>
function ajaxRequest(params) {
$.ajax({
url: "/api/ExamResult/Get_All_Exam_List_With_Pagination",
type: 'POST',
async: false,
dataType: "json",
data: '{' +
'"offset":"' + params.data.offset + '",' +
'"limit":"' + params.data.limit + '",' +
'"search":"' + params.data.search + '"' +
'}',
contentType: "application/json; charset=utf-8",
success: function (data) {
var temp = data
var rows = []
console.log(data)
for (i = 0; i < temp.rows.length; i++) {
var x = {
'Start_Date': temp.rows[i].exam_Strat_Date.replaceAll('T', ' '),
'End_Date': temp.rows[i].exam_End_Date.replaceAll('T', ' '),
'Question_Per_Sercond': temp.rows[i].exam_Question_Per_Second,
'Number_Of_Question': temp.rows[i].exam_Number_Of_Questions,
'Subs': temp.rows[i].subs,
'Show_Results': '<button class="btn btn-warning btn-sm" value="' + temp.rows[i].id + '" id="Show_Resluts" style="word-break: keep-all;"><i class="fa fa-file-text "></i> Show Result</button>',
'Exam_Link': '<button class="btn btn-primary btn-sm" value="' + temp.rows[i].exam_Connection_Id + '" id="Exam_Link" style="word-break: keep-all;"><i class="fa fa-copy "></i> Copy Link</button>',
'Delete': '<button class="btn btn-danger btn-sm" value="' + temp.rows[i].id + '" id="Delete" style="word-break: keep-all;"><i class="fa fa-trash "></i> Delete</button>'
}
rows.push(x)
}
temp.rows = rows
params.success(temp)
},
error: function () {
alert('Talep esnasında sorun oluştu.Yeniden deneyin');
}
});
}
</script>
<script>
$(document).ready(function () {
Create_Exams_Table($('#Exams_Table'), 0, 0)
function Create_Exams_Table($el, cells, rows) {
$el.bootstrapTable('destroy');
var i; var j; var row
var columns = [];
var data__ = [];
columns.push({
field: 'Start_Date',
title: 'Strat Date',
sortable: true
})
columns.push({
field: 'End_Date',
title: 'End Date',
sortable: true
})
columns.push({
field: 'Question_Per_Sercond',
title: 'Question Per Second',
sortable: true
})
columns.push({
field: 'Number_Of_Question',
title: 'Number Of Question',
sortable: true
})
columns.push({
field: 'Subs',
title: 'Subs',
sortable: true
})
columns.push({
field: 'Exam_Link',
title: 'Exam Link',
sortable: true
})
columns.push({
field: 'Delete',
title: 'Delete',
sortable: true
})
columns.push({
field: 'Show_Results',
title: 'Show Results',
sortable: true
})
$el.bootstrapTable({
sidePagination: "server",
pagination: true,
ajax: "ajaxRequest",
clickToSelect: true,
columns: columns,
data: data__,
exportTypes: ['json', 'xml', 'csv', 'txt', 'sql', 'excel', 'pdf'],
exportOptions: {
fileName: function () {
const monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
const dateObj = new Date();
const month = monthNames[dateObj.getMonth()];
const day = String(dateObj.getDate()).padStart(2, '0');
const year = dateObj.getFullYear();
const output = day + '-' + month + '-' + year;
return 'Exams ' + output
}
},
onPostBody: function () {
$('button[id=Exam_Link]').click(function () {
const url = new URL(window.location.href);
var Connetion_Link = url.origin + "/ExamScreen?Exam_Guid=" + $(this).attr('value')
navigator.clipboard.writeText(Connetion_Link);
Swal.fire({
icon: 'success',
title: 'Link successfully copied',
})
})
$('button[id=Delete]').click(function () {
console.log($(this).attr('value'))
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.isConfirmed) {
var x = {
id: $(this).attr('value')
}
$.ajax({
url: 'api/Exam/Exam_Delete',
type: 'POST',
async: false,
dataType: "json",
data: JSON.stringify(x),
contentType: "application/json; charset=utf-8",
success: function (data) {
Swal.fire(
'Deleted!',
'success'
)
Create_Question_Table($('#Questions_Table'), 0, 0)
},
error: function (xhr, status, error) {
if (xhr.status == 200) {
}
else {
Swal.fire({
icon: 'error',
title: 'Talep Esnasında Bir Hata Oluştur',
text: '' + xhr.status + ' ' + error.Message,
})
}
}
})
}
})
})
$('button[id=Show_Resluts]').click(function () {
$('#Exam_Results_modal').modal('show')
Birimleri_Doldur_Tablo($('#Exam_Results_Table'), 0, 0, $(this).attr('value'))
})
}
})
}
function Birimleri_Doldur_Tablo($el, cells, rows, Exam_ID) {
$el.bootstrapTable('destroy');
var i; var j; var row
var columns = [];
var data__ = [];
columns.push({
field: 'User_Name',
title: 'User Name',
sortable: true
})
columns.push({
field: 'True_Answers',
title: 'True Answers',
sortable: true
})
columns.push({
field: 'False_Answers',
title: 'False Answers',
sortable: true
})
columns.push({
field: 'Sucsess_Rate',
title: 'Sucsess Rate',
sortable: true
})
var x = {
id: Exam_ID
}
$.ajax({
url: 'api/ExamResult/Get_By_Exam_Guid_Exam_Resutls',
type: 'POST',
async: false,
data: JSON.stringify(x),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
var temp = data
console.log(temp)
for (i = 0; i < temp.length; i++) {
row = {}
row['User_Name'] = temp[i].user_Name
row['True_Answers'] = temp[i].answer_Rate.true_Answer
row['False_Answers'] = temp[i].answer_Rate.false_Answer
row['Sucsess_Rate'] = temp[i].succses_Rate.toFixed(2)
data__.push(row)
}
},
error: function () {
alert('Talep esnasında sorun oluştu.Yeniden deneyin');
}
});
$el.bootstrapTable({
columns: columns,
data: data__,
exportTypes: ['json', 'xml', 'csv', 'txt', 'sql', 'excel', 'pdf'],
exportOptions: {
fileName: function () {
const monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
const dateObj = new Date();
const month = monthNames[dateObj.getMonth()];
const day = String(dateObj.getDate()).padStart(2, '0');
const year = dateObj.getFullYear();
const output = day + '-' + month + '-' + year;
return 'Exams Results ' + output
}
},
})
$el.bootstrapTable('hideColumn', 'ID')
}
})
</script>
<div class="modal fade" id="Exam_Results_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle"
aria-hidden="true">
<div class="modal-dialog modal-dialog-scrollable modal-xl" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="Exam_Results_modal">Exam Results</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-12">
<table id="Exam_Results_Table" data-toolbar="#toolbar" data-show-toggle="true"
data-show-columns="true" data-show-export="true"></table>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-body">
<table id="Exams_Table" data-toolbar="#toolbar" data-show-toggle="true" data-show-columns="true"
data-show-export="true"></table>
</div>
</div>
</div>
</div>
using System;
using System.Collections.Generic;
using System.Linq;
using Kaynak_Kod.Entities.Exams;
using Kaynak_Kod.Entities.ExamScreen;
using Kaynak_Kod.Entities.Questions;
using Microsoft.Extensions.Options;
using WebApi.Authorization;
using WebApi.Helpers;
namespace Kaynak_Kod.Services.Exams
{
public interface IExamResultService
{
pagination_Request_Result<Exam_List> Get_All_Exam_List_With_Pagination(pagenation_request x);
List<User_Answers_Sucsess_Rate> Get_By_Exam_Guid_Exam_Resutls(Exam x);
}
public class ExamResultService : IExamResultService
{
private DataContext _context;
private IJwtUtils _jwtUtils;
private readonly AppSettings _appSettings;
public ExamResultService(
DataContext context,
IJwtUtils jwtUtils,
IOptions<AppSettings> appSettings)
{
_context = context;
_jwtUtils = jwtUtils;
_appSettings = appSettings.Value;
}
public pagination_Request_Result<Exam_List> Get_All_Exam_List_With_Pagination(pagenation_request y)
{
var temp_ = _context.exams;
var temp = (from x in _context.exams
select new
{
x.Exam_Connection_Id,
x.Exam_End_Date,
x.Exam_Number_Of_Questions,
x.Exam_Question_Per_Second,
x.Exam_Strat_Date,
x.Id,
}
).Skip(Convert.ToInt32(y.offset)).Take(Convert.ToInt32(y.limit)).ToList();
IEnumerable<Exam_List> rd = temp.Select(o => new Exam_List
{
Exam_Connection_Id = o.Exam_Connection_Id,
Exam_End_Date = o.Exam_End_Date,
Exam_Number_Of_Questions = o.Exam_Number_Of_Questions,
Exam_Question_Per_Second = o.Exam_Question_Per_Second,
Exam_Strat_Date = o.Exam_Strat_Date,
Id = o.Id,
Subs = Get_Subs_Number(o.Exam_Connection_Id)
});
var Result = new pagination_Request_Result<Exam_List>
{
rows = rd.ToList(),
totalNotFiltered = temp_.Count(),
total = temp.Count()
};
return Result;
}
public int Get_Subs_Number(string x)
{
int z = (from ü in _context.user_Informations
where ü.Exam_Guid == x
select ü
).Count();
return z;
}
public List<User_Answers_Sucsess_Rate> Get_By_Exam_Guid_Exam_Resutls(Exam x)
{
var Exam_Informationm=(from y in _context.exams
where y.Id==x.Id
select y
).FirstOrDefault();
var exam_Users = (from y in _context.user_Informations
where y.Exam_Guid == Exam_Informationm.Exam_Connection_Id
select new
{
y.Exam_Guid,
y.Id,
y.Guid,
y.User_Name
}
);
IEnumerable<User_Information> temp_rd= exam_Users.Select(o=> new User_Information{
Exam_Guid=o.Exam_Guid,
Id=o.Id,
Guid = o.Guid,
User_Name=o.User_Name
});
List<User_Answers_Sucsess_Rate> rd = new List<User_Answers_Sucsess_Rate>();
foreach (var item in temp_rd)
{
User_Answers_Sucsess_Rate result = new User_Answers_Sucsess_Rate
{
Answer_Rate = new Number_of_True_And_False_Answers
{
True_Answer = Answer_Is_True(item.Guid, item.Exam_Guid).True_Answer,
False_Answer = Answer_Is_False(item.Guid, item.Exam_Guid)
},
User_Name = item.User_Name,
User_Guid = item.Guid,
Succses_Rate = Calculate_Sucsess_rate(Answer_Is_True(item.Guid, item.Exam_Guid).True_Answer, Answer_Is_False(item.Guid, item.Exam_Guid), item.Exam_Guid)
};
rd.Add(result);
}
return rd.ToList();
}
public decimal Calculate_Sucsess_rate(int True_Answers_, int False_Answers, string Exam_Guid)
{
int Exam_Number_Of_Question = (from x in _context.exams
where x.Exam_Connection_Id == Exam_Guid
select x
).FirstOrDefault().Exam_Number_Of_Questions;
decimal Result = 100*(Convert.ToDecimal(True_Answers_) / Convert.ToDecimal(Exam_Number_Of_Question));
return Result;
}
public Number_of_True_And_False_Answers Answer_Is_True(string User_Guid, string Exam_Guid)
{
var temp = (from x in _context.User_Answers
join Answer_ in _context.answers
on x.Answer_Id equals Answer_.Id
where x.User_Guid == User_Guid && x.Exam_Guid == Exam_Guid && Answer_.Is_True == 1
select
new
{
Answer_.Answer_Text,
Answer_.Is_True,
}
);
IEnumerable<User_Answers> rd = temp.Select(o => new User_Answers
{
Answer_Id = o.Is_True,
});
var rd_ = new Number_of_True_And_False_Answers
{
True_Answer = rd.ToList().Count()
};
return rd_;
}
public int Answer_Is_False(string User_Guid, string Exam_Guid)
{
var temp = (from x in _context.User_Answers
join Answer_ in _context.answers
on x.Answer_Id equals Answer_.Id
where x.User_Guid == User_Guid && x.Exam_Guid == Exam_Guid && Answer_.Is_True == 0
select
new
{
Answer_.Answer_Text,
Answer_.Is_True,
}
);
IEnumerable<User_Answers> rd = temp.Select(o => new User_Answers
{
Answer_Id = o.Is_True,
});
return rd.ToList().Count();
}
}
}
@rec4i
Copy link
Author

rec4i commented Mar 1, 2022

image

*Sınavları ve sonuçlarını listelemek için kullandığım ekran

image

*Sınava giren kullanıcıların sonuçlarının listelendiği ekran

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment