-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
07ad009
commit a0c083f
Showing
3 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class AcademyCourse extends Model { | ||
protected $table = 'academy_course'; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class AcademyCourseEnrollment extends Model { | ||
public static $STATUS_NOT_ENROLLED = 0; | ||
public static $STATUS_ENROLLED = 1; | ||
public static $STATUS_COMPLETED = 2; | ||
public static $STATUS_EXEMPT = 3; | ||
|
||
protected $table = 'academy_course_enrollment'; | ||
public function course() | ||
{ | ||
return $this->belongsTo(AcademyCourse::class, 'id', 'academy_course_id'); | ||
} | ||
|
||
public function user() | ||
{ | ||
return $this->belongsTo(User::class, 'cid', 'cid'); | ||
} | ||
} | ||
|
111 changes: 111 additions & 0 deletions
111
app/Console/Commands/PopulateAcademyCourseEnrollments.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Classes\VATUSAMoodle; | ||
use App\Facility; | ||
use App\Models\AcademyCourse; | ||
use App\Models\AcademyCourseEnrollment; | ||
use App\User; | ||
use Carbon\Carbon; | ||
use Illuminate\Console\Command; | ||
|
||
class PopulateAcademyCourseEnrollments extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'moodle:populate_enrollments'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Update AcademyCourseEnrollments'; | ||
|
||
/** @var \App\Classes\VATUSAMoodle instance */ | ||
private $moodle; | ||
|
||
/** | ||
* Create a new command instance. | ||
* | ||
* @param \App\Classes\VATUSAMoodle $moodle | ||
*/ | ||
public function __construct(VATUSAMoodle $moodle) | ||
{ | ||
parent::__construct(); | ||
$this->moodle = $moodle; | ||
} | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return mixed | ||
* @throws \Exception | ||
*/ | ||
public function handle() | ||
{ | ||
$academy_courses = AcademyCourse::orderBy("list_order", "ASC")->get(); | ||
foreach (User::where('flag_homecontroller', 1)->where('rating', '>', 0)->get() as $user) { | ||
try { | ||
$uid = $this->moodle->getUserId($user->cid); | ||
} catch (Exception $e) { | ||
$uid = -1; | ||
} | ||
$enrollments = AcademyCourseEnrollment::where('cid', $user->cid)->get(); | ||
$enrollment_map = []; | ||
foreach ($enrollments as $enrollment) { | ||
$enrollment_map[$enrollment->academy_course_id] = $enrollment; | ||
} | ||
foreach ($academy_courses as $academy_course) { | ||
if (!array_key_exists($academy_course->id, $enrollment_map)) { | ||
|
||
$e = new AcademyCourseEnrollment(); | ||
$e->cid = $user->cid; | ||
$e->academy_course_id = $academy_course->id; | ||
$e->assignment_timestamp = null; | ||
$e->passed_timestamp = null; | ||
$e->status = AcademyCourseEnrollment::$STATUS_NOT_ENROLLED; | ||
$e->save(); | ||
$enrollment_map[$academy_course->id] = $e; | ||
} | ||
} | ||
|
||
foreach ($enrollments as $e) { | ||
$hasChange = false; | ||
if ($e->status < AcademyCourseEnrollment::$STATUS_ENROLLED) { | ||
$assignmentDate = $this->moodle->getUserEnrolmentTimestamp($uid, $academy_course->moodle_enrol_id); | ||
$assignmentTimestamp = $assignmentDate ? | ||
Carbon::createFromTimestampUTC($assignmentDate)->format('Y-m-d H:i') : null; | ||
|
||
if ($assignmentTimestamp) { | ||
$e->assignment_timestamp = $assignmentTimestamp; | ||
$e->status = AcademyCourseEnrollment::$STATUS_ENROLLED; | ||
$hasChange = true; | ||
} | ||
} | ||
|
||
if ($e->status == AcademyCourseEnrollment::$STATUS_ENROLLED) { | ||
$attempts = $this->moodle->getQuizAttempts($e->course->moodle_quiz_id, null, $uid); | ||
foreach($attempts as $attempt) { | ||
if (round($attempt['grade']) > $e->course->passing_percent) { | ||
// Passed | ||
$finishTimestamp = | ||
Carbon::createFromTimestampUTC($attempt['timefinish'])->format('Y-m-d H:i'); | ||
$e->passed_timestamp = $finishTimestamp; | ||
$e->status = AcademyCourseEnrollment::$STATUS_COMPLETED; | ||
$hasChange = true; | ||
} | ||
} | ||
} | ||
|
||
if ($hasChange) { | ||
$e->save(); | ||
} | ||
} | ||
} | ||
} | ||
} |