Skip to content

Commit

Permalink
No code needed for single courses
Browse files Browse the repository at this point in the history
  • Loading branch information
kreut committed Aug 29, 2024
1 parent 4fd2e19 commit 14261dd
Show file tree
Hide file tree
Showing 7 changed files with 343 additions and 226 deletions.
88 changes: 77 additions & 11 deletions app/Enrollment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App;

use App\Exceptions\Handler;
use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
Expand All @@ -11,18 +13,82 @@ class Enrollment extends Model

protected $guarded = [];

public function removeAllRelatedEnrollmentInformation(User $user,
int $course_id,
AssignToUser $assignToUser,
Assignment $assignment,
Submission $submission,
SubmissionFile $submissionFile,
Score $score,
Extension $extension,

public function completeEnrollmentDetails($user_id, Section $section, $course_id, $actual_student)
{
$assignToUser = new AssignToUser();
$section_id = $section->id;
$this->user_id = $user_id;
$this->section_id = $section_id;
$this->course_id = $course_id;
$this->save();
$course = Course::find($course_id);
if (!$course ->shown){
$course->shown = 1;
$course->save();
}
$assignments = $section->course->assignments;
$assignToUser->assignToUserForAssignments($assignments, $user_id, $section->id);

if ($actual_student) {
$data_shops_enrollment = DB::table('data_shops_enrollments')
->where('course_id', $course_id)
->first();
try {
if (!$data_shops_enrollment) {
$course_info = DB::table('courses')
->join('users', 'courses.user_id', '=', 'users.id')
->join('schools', 'courses.school_id', '=', 'schools.id')
->select('term',
'courses.name AS course_name',
'schools.name AS school_name',
DB::raw('CONCAT(first_name, " " , last_name) AS instructor_name'))
->where('courses.id', $course_id)
->first();

$data = ['course_id' => $course_id,
'course_name' => $course_info->course_name,
'school_name' => $course_info->school_name,
'term' => $course_info->term,
'instructor_name' => $course_info->instructor_name,
'number_of_enrolled_students' => 1,
'created_at' => now(),
'updated_at' => now()];
DB::table('data_shops_enrollments')->insert($data);
} else {
DB::table('data_shops_enrollments')->where(['course_id' => $course_id])
->update(['number_of_enrolled_students' => $data_shops_enrollment->number_of_enrolled_students + 1,
'updated_at' => now()]);
}
} catch (Exception $e) {
$h = new Handler(app());
$h->report($e);
}
}

$notification_exists = DB::table('notifications')->where('user_id', $user_id)->first();
if ($actual_student && !$notification_exists) {
$notification_data = [
'user_id' => $user_id,
'hours_until_due' => 24,
'created_at' => now(),
'updated_at' => now()];
DB::table('notifications')->insert($notification_data);
}
}

public function removeAllRelatedEnrollmentInformation(User $user,
int $course_id,
AssignToUser $assignToUser,
Assignment $assignment,
Submission $submission,
SubmissionFile $submissionFile,
Score $score,
Extension $extension,
LtiGradePassback $ltiGradePassback,
Seed $seed,
ExtraCredit $extraCredit,
Section $section)
Seed $seed,
ExtraCredit $extraCredit,
Section $section)
{
$assignments_to_remove_ids = [];
$assign_to_timings_to_remove_ids = [];
Expand Down
56 changes: 2 additions & 54 deletions app/Http/Controllers/EnrollmentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -508,15 +508,13 @@ function index(Request $request, Enrollment $enrollment)
* @param StoreEnrollment $request
* @param Enrollment $enrollment
* @param Section $Section
* @param AssignToUser $assignToUser
* @return array|Application|ResponseFactory|Response|string
* @throws Exception
*/
public
function store(StoreEnrollment $request,
Enrollment $enrollment,
Section $Section,
AssignToUser $assignToUser)
Section $Section)
{
$response['type'] = 'error';
$authorized = Gate::inspect('store', $enrollment);
Expand Down Expand Up @@ -578,60 +576,10 @@ function store(StoreEnrollment $request,
$response['type'] = 'error';
$response['message'] = "You are already enrolled in <strong>$course_section_name</strong>.";
} else {

$enrollment->user_id = $request->user()->id;
$enrollment->section_id = $section_id;
$enrollment->course_id = $course_id;
$enrollment->save();
$assignments = $section->course->assignments;
$assignToUser->assignToUserForAssignments($assignments, $enrollment->user_id, $section->id);
$actual_student = !($request->user()->fake_student || $request->user()->formative_student || $request->user()->testing_student);
if ($actual_student) {
$data_shops_enrollment = DB::table('data_shops_enrollments')
->where('course_id', $course_id)
->first();
try {
if (!$data_shops_enrollment) {
$course_info = DB::table('courses')
->join('users', 'courses.user_id', '=', 'users.id')
->join('schools', 'courses.school_id', '=', 'schools.id')
->select('term',
'courses.name AS course_name',
'schools.name AS school_name',
DB::raw('CONCAT(first_name, " " , last_name) AS instructor_name'))
->where('courses.id', $course_id)
->first();

$data = ['course_id' => $course_id,
'course_name' => $course_info->course_name,
'school_name' => $course_info->school_name,
'term' => $course_info->term,
'instructor_name' => $course_info->instructor_name,
'number_of_enrolled_students' => 1,
'created_at' => now(),
'updated_at' => now()];
DB::table('data_shops_enrollments')->insert($data);
} else {
DB::table('data_shops_enrollments')->where(['course_id' => $course_id])
->update(['number_of_enrolled_students' => $data_shops_enrollment->number_of_enrolled_students + 1,
'updated_at' => now()]);
}
} catch (Exception $e) {
$h = new Handler(app());
$h->report($e);
}
}

$enrollment->completeEnrollmentDetails($request->user()->id, $section, $course_id, $actual_student);

$notification_exists = DB::table('notifications')->where('user_id', $request->user()->id)->first();
if ($actual_student && !$notification_exists) {
$notification_data = [
'user_id' => $request->user()->id,
'hours_until_due' => 24,
'created_at' => now(),
'updated_at' => now()];
DB::table('notifications')->insert($notification_data);
}
$response['type'] = 'success';
$response['message'] = "You are now enrolled in <strong>$course_section_name</strong>.";
DB::commit();
Expand Down
49 changes: 43 additions & 6 deletions app/Http/Controllers/LTIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\LtiGradePassback;
use App\LtiLaunch;
use App\LtiToken;
use App\Section;
use App\User;
use Exception;
use Illuminate\Contracts\Foundation\Application;
Expand All @@ -21,6 +22,7 @@
use Overrides\IMSGlobal\LTI;
use App\Custom\LTIDatabase;
use App\Assignment;
use Overrides\IMSGlobal\LTI\LTI_Names_Roles_Provisioning_Service;
use stdClass;


Expand Down Expand Up @@ -143,8 +145,8 @@ public function authenticationResponse(Assignment $assignment,

if ($launch->is_deep_link_launch()) {
//this configures the Deep Link
$is_moodle = strpos($launch->get_launch_data()['iss'],'moodle') !== false;
if ($is_moodle){
$is_moodle = strpos($launch->get_launch_data()['iss'], 'moodle') !== false;
if ($is_moodle) {
//moodle needs the custom params to be some sort of object (even though it's really empty). As a hack I'm just sending them
$resource = LTI\LTI_Deep_Link_Resource::new()
->set_url($url)
Expand All @@ -169,17 +171,27 @@ public function authenticationResponse(Assignment $assignment,
echo "We can't seem to get this user's email. Typically this happens if you're trying to connect in Student View mode or if you neglected to set the Privacy Level to Public when configuring this tool.";
exit;
}

$lti_user = $user->where('email', $email)->first();
$sub = $launch->get_launch_data()['sub'];
$lti_user = $user->where('email', $sub)->first();
if (!$lti_user) {
$lti_user = $user->where('email', $email)->first();
}
if (!$lti_user) {
$lti_user = User::create([
'first_name' => $launch->get_launch_data()['given_name'],
'last_name' => $launch->get_launch_data()['family_name'],
'email' => $email,
'role' => 3,
'time_zone' => 'America/Los_Angeles',
'sub' => $sub,
'email_verified_at' => now(),
]);
} else {
///eventually I shouldn't need the following code since they'll all be new
if (!$lti_user->sub) {
$lti_user->lms_user_id = $sub;
$lti_user->save();
}
}
DB::table('users')->where('instructor_user_id', $lti_user->id)->update(['instructor_user_id' => null]);
session()->forget('original_user_id');
Expand Down Expand Up @@ -254,6 +266,29 @@ public function authenticationResponse(Assignment $assignment,
->where('assignment_id', $linked_assignment->id)
->update(['launch_id' => $launch_id]);
}
$courses = DB::table('assignments')
->join('courses', 'courses.id', '=', 'assignments.course_id')
->join('sections', 'courses.id', '=', 'sections.course_id')
->join('users','courses.user_id','=','users.id')
->where('assignments.id', $linked_assignment->id)
->select('courses.id AS course_id','users.time_zone AS instructor_time_zone')
->get();
if (count($courses) === 1) {
$course = $courses[0];
$course_id = $course->course_id;
$Section = new Section();
$section = $Section->where('course_id', $course_id)->first();
if ($section->course->enrollments->isNotEmpty()) {
$enrolled_user_ids = $section->course->enrollments->pluck('user_id')->toArray();
if (!in_array($lti_user->id, $enrolled_user_ids)) {
$lti_user->time_zone = $course->instructor_time_zone;
$lti_user->student_id = $sub;
$lti_user->save();
$enrollment = new Enrollment();
$enrollment->completeEnrollmentDetails($lti_user->id, $section, $course_id, !$lti_user->fake_student);
}
}
}
}
return $lms_launch_in_new_window ?
redirect("/launch-in-new-window/$lti_token_id/init/$linked_assignment->id")
Expand Down Expand Up @@ -369,7 +404,8 @@ function jsonConfig($id)
* @return array
* @throws Exception
*/
public function getTokenByLtiTokenId(Request $request, LtiToken $LtiToken)
public
function getTokenByLtiTokenId(Request $request, LtiToken $LtiToken)
{

$response['type'] = 'error';
Expand All @@ -390,7 +426,8 @@ public function getTokenByLtiTokenId(Request $request, LtiToken $LtiToken)

}

public function refreshToken()
public
function refreshToken()
{
$response['type'] = 'error';
try {
Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/SectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public function index(Course $course): array
$response['sections'] = $course->sections;
$response['course_start_date'] = $course->start_date;
$response['course_end_date'] = $course->end_date;
$response['is_lms'] = (bool) $course->lms;
$response['type'] = 'success';

} catch (Exception $e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class UpdateLmsUserIdToUsers extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('lms_user_id')->nullable()->after('testing_student')->nullable();
});
Schema::table('users', function (Blueprint $table) {
$table->index('lms_user_id');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('lms_user_id');
});
}
}
Loading

0 comments on commit 14261dd

Please sign in to comment.