Skip to content

Commit 5a575a5

Browse files
Merge pull request #30 from prismathic/develop
Merge changes
2 parents 6ff2ce4 + cf1ab0d commit 5a575a5

21 files changed

Lines changed: 909 additions & 148555 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ Homestead.json
1010
Homestead.yaml
1111
npm-debug.log
1212
yarn-error.log
13+
1_START_CBT.bat
14+
2_START_CBT.bat
15+
START_CBT.sh

app/Http/Controllers/AdminController.php

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,18 @@
77
use App\Subject;
88
use App\Classes;
99
use App\Exam;
10-
use App\Mark;
10+
use App\Imports\StudentsImport;
1111
use App\Question;
1212
use App\Option;
1313
use App\User;
14-
use Gate;
15-
use DB;
16-
use Auth;
1714
use Illuminate\Support\Arr;
18-
use Illuminate\Support\Facades\Auth as FacadesAuth;
15+
use Illuminate\Support\Facades\Auth;
16+
use Illuminate\Support\Facades\DB;
17+
use Illuminate\Support\Facades\Gate;
1918
use Illuminate\Support\Facades\Hash;
20-
use PDF;
21-
use Illuminate\Support\Facades\Log;
19+
use Barryvdh\DomPDF\PDF;
2220
use Illuminate\Support\Facades\Session;
23-
use Illuminate\Support\Str;
21+
use Maatwebsite\Excel\Facades\Excel;
2422

2523
class AdminController extends Controller
2624
{
@@ -115,9 +113,9 @@ public function restoreStudent($id) {
115113
return compact('student','message');
116114
}
117115

118-
public function generateStudentCode($class_id) {
116+
protected function generateStudentCode($class_id) {
119117
$characters = 'ABCDEFGHJKLMNPQRSTUVWXYZ';
120-
$code = mt_rand(5111, 9999) . $characters[rand(0, strlen($characters) - 1)] . $characters[rand(0, strlen($characters) - 1)];
118+
$code = mt_rand(2111, 9999) . $characters[rand(0, strlen($characters) - 1)] . $characters[rand(0, strlen($characters) - 1)];
121119

122120
$check = User::where('class_id',$class_id)->where('code', $code)->first();
123121

@@ -150,6 +148,28 @@ public function addStudent(Request $request) {
150148
return compact('student','message');
151149
}
152150

151+
public function addMultipleStudents(Request $request) {
152+
$class_id = $request->class_id;
153+
154+
try {
155+
Excel::import(new StudentsImport($class_id), $request->students);
156+
157+
$students = User::where('class_id',$class_id)->get();
158+
$message = "Student details Imported successfully!";
159+
$success = true;
160+
161+
return compact('success','students','message');
162+
163+
} catch (\Maatwebsite\Excel\Validators\ValidationException $e) {
164+
$failures = $e->failures();
165+
166+
$success = false;
167+
$message = $failures[0]->errors();
168+
169+
return compact('success','message');
170+
}
171+
}
172+
153173
public function getAllQuestions($subject,$class_id) {
154174
$subject = Subject::where('alias',$subject)->firstOrFail();
155175
$current_class = $subject->classes()->where('class_id', $class_id)->firstOrFail();

app/Http/Controllers/StudentController.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44

55
use App\Exam;
66
use Illuminate\Http\Request;
7-
use Auth;
87
use App\Question;
98
use App\Subject;
109
use App\Option;
1110
use App\Score;
12-
use Illuminate\Support\Facades\Log;
11+
use Illuminate\Support\Facades\Auth;
1312
use Illuminate\Support\Facades\Session;
1413

1514
class StudentController extends Controller
@@ -30,7 +29,7 @@ public function index() {
3029
return view('home',compact('exams'));
3130
}
3231

33-
public function getExamQuestions(\Request $request, $subject) {
32+
public function getExamQuestions(Request $request, $subject) {
3433
$subject = Subject::where('alias',$subject)->first();
3534
$user = Auth::user();
3635
$class_id = $user->class_id;
@@ -43,7 +42,7 @@ public function getExamQuestions(\Request $request, $subject) {
4342
$hours = $exam->hours;
4443
$minutes = $exam->minutes;
4544
$subject_id = $subject->id;
46-
$seed = Auth::user()->code;
45+
$seed = substr(Auth::user()->code,0,4);
4746

4847
$questions = Question::where('exam_id',$exam->id)->with('options:id,question_id,body')->inRandomOrder($seed)->get();
4948

@@ -60,7 +59,7 @@ public function submitExam(Request $request) {
6059
$exam_id = Session::get('exam_id');
6160

6261
//first mark all the answers
63-
$seed = Auth::user()->code;
62+
$seed = substr(Auth::user()->code,0,4);
6463

6564
$questions = Question::where('exam_id',$exam_id)->with('options')->inRandomOrder($seed)->get();
6665

app/Imports/StudentsImport.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace App\Imports;
4+
5+
use App\User;
6+
use Illuminate\Validation\Rule;
7+
use Maatwebsite\Excel\Concerns\ToModel;
8+
use Maatwebsite\Excel\Concerns\WithHeadingRow;
9+
use Maatwebsite\Excel\Concerns\WithValidation;
10+
11+
class StudentsImport implements ToModel, WithHeadingRow, WithValidation
12+
{
13+
private $class_id;
14+
15+
public function __construct(int $class_id)
16+
{
17+
$this->class_id = $class_id;
18+
}
19+
/**
20+
* @param array $row
21+
*
22+
* @return \Illuminate\Database\Eloquent\Model|null
23+
*/
24+
public function model(array $row)
25+
{
26+
return new User([
27+
'lastname' => $row['surname'],
28+
'firstname' => $row['first_name'],
29+
'class_id' => $this->class_id,
30+
'code' => $this->generateStudentCode()
31+
]);
32+
}
33+
34+
public function rules(): array
35+
{
36+
return array(
37+
'surname' => ['required' , 'string', 'min:2', 'regex:/^(?=.*?[a-zA-Z0-9])/'],
38+
'first_name' => 'required|string|min:2|regex:/^(?=.*?[a-zA-Z0-9])/'
39+
);
40+
}
41+
42+
/**
43+
* Custom Validation Messages
44+
* @return array
45+
*/
46+
public function customValidationMessages(): array {
47+
return array(
48+
'surname.regex' => 'Invalid :attribute provided: :input',
49+
'surname.min' => 'Invalid :attribute provided: :input',
50+
'surname.required' => 'Empty cell in :attribute field',
51+
'surname.string' => 'Invalid :attribute provided: :input',
52+
'first_name.regex' => 'Invalid :attribute provided: :input',
53+
'first_name.min' => 'Invalid :attribute provided: :input',
54+
'first_name.required' => 'Empty cell in :attribute field',
55+
'first_name.string' => 'Invalid :attribute provided: :input'
56+
);
57+
}
58+
59+
private function generateStudentCode()
60+
{
61+
$characters = 'ABCDEFGHJKLMNPQRSTUVWXYZ';
62+
$code = mt_rand(2111, 9999) . $characters[rand(0, strlen($characters) - 1)] . $characters[rand(0, strlen($characters) - 1)];
63+
64+
$check = User::where('class_id',$this->class_id)->where('code', $code)->first();
65+
66+
//recursively check whether another student exists in that class with the same code
67+
return ($check) ? $this->generateStudentCode() : $code;
68+
}
69+
}

app/Providers/AuthServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function boot()
3838
if ($admin->isSuperAdmin()) return true;
3939

4040
$subject = $admin->subjects()->where('subject_id', $subject_id)->first();
41-
$check = $subject->classes()->where('class_id',$class_id)->first();
41+
$check = $subject ? $subject->classes()->where('class_id',$class_id)->first() : false;
4242

4343
return $check ? true : false;
4444
});

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"fideloper/proxy": "^4.0",
1515
"laravel/framework": "^7.0",
1616
"laravel/tinker": "^2.0",
17-
"laravel/ui": "2.0"
17+
"laravel/ui": "2.0",
18+
"maatwebsite/excel": "^3.1"
1819
},
1920
"require-dev": {
2021
"barryvdh/laravel-debugbar": "^3.4",

0 commit comments

Comments
 (0)