Professional Documents
Culture Documents
In
March of 2018, our goal was simple. We were going to
migrate our Continuing Education website, built in classic
ASP, to newer frameworks and platforms, one of which was
Moodle.
1 de 13 20/07/20, 17:05
Managing Moodle with Laravel Nova | by Dennis Sauve | DevOps in... https://medium.com/devops-in-the-trenches/managing-moodle-with-...
Fast forward a few months, and our content editors are putting the
finishing touches on our courses, we have configured external
database authentication through Laravel, and everything looks
okay. There was only one problem.
So I built it.
2 de 13 20/07/20, 17:05
Managing Moodle with Laravel Nova | by Dennis Sauve | DevOps in... https://medium.com/devops-in-the-trenches/managing-moodle-with-...
Patience :)
'moodle' => [
'driver' => 'pgsql',
'host' => env('DB_MOODLE_HOST', '127.0.0.1'),
'port' => env('DB_MOODLE_PORT', '5432'),
'database' => env('DB_MOODLE_DATABASE',
'forge'),
'username' => env('DB_MOODLE_USERNAME',
'forge'),
'password' => env('DB_MOODLE_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
This is necessary for the next step of creating a model based off of a
Moodle table. If you’re unsure about Laravel environment
3 de 13 20/07/20, 17:05
Managing Moodle with Laravel Nova | by Dennis Sauve | DevOps in... https://medium.com/devops-in-the-trenches/managing-moodle-with-...
Option 1:
The first thing to do is create the actual model. You can do this by
running a php artisan command
Option 2:
Next:
4 de 13 20/07/20, 17:05
Managing Moodle with Laravel Nova | by Dennis Sauve | DevOps in... https://medium.com/devops-in-the-trenches/managing-moodle-with-...
namespace App\Models\Moodle;
use Illuminate\Database\Eloquent\Model;
namespace App\Nova\Resources\Moodle;
5 de 13 20/07/20, 17:05
Managing Moodle with Laravel Nova | by Dennis Sauve | DevOps in... https://medium.com/devops-in-the-trenches/managing-moodle-with-...
use App\Nova\Resources\Resource;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\HasMany;
use Laravel\Nova\Fields\Boolean;
use Laravel\Nova\Panel;
use Illuminate\Http\Request;
use Laravel\Nova\Http\Requests\NovaRequest;
/**
* The single value that should be used to represent the
resource when being displayed.
*
* @var string
*/
public static $title = 'id';
/**
* Get the displayable label of the resource.
*
* @return string
*/
public static function label() { return "Moodle
Accounts"; }
/**
* The columns that should be searched.
*
* @var array
*/
public static $search = [
'username',
'email',
'lastname',
6 de 13 20/07/20, 17:05
Managing Moodle with Laravel Nova | by Dennis Sauve | DevOps in... https://medium.com/devops-in-the-trenches/managing-moodle-with-...
'firstname',
];
/**
* Get the fields displayed by the resource.
*
* The HasMany is for the courses that the MoodleAccount
is enrolled in.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function fields(Request $request)
{
return [
ID::make('ID', 'id')
->sortable()
->hideFromIndex(),
Text::make('Username', 'username')
->sortable(),
Text::make('Authorization Type', 'auth'),
Text::make('Last Name', 'lastname')
->sortable(),
Text::make('First Name', 'firstname')
->sortable(),
Text::make('Email Address', 'email')
->sortable(),
Text::make('Created On', function(){
return date('M d Y H:i',
$this->timecreated);
}),
Text::make('Last Access', function(){
return date('M d Y H:i', $this->lastaccess);
})
->hideFromIndex(),
Boolean::make('Active', function(){
return !$this->deleted;
}),
];
}
/**
* Get the cards available for the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function cards(Request $request)
7 de 13 20/07/20, 17:05
Managing Moodle with Laravel Nova | by Dennis Sauve | DevOps in... https://medium.com/devops-in-the-trenches/managing-moodle-with-...
{
return [];
}
/**
* Get the filters available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function filters(Request $request)
{
return [];
}
/**
* Get the lenses available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function lenses(Request $request)
{
return [];
}
/**
* Get the actions available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function actions(Request $request)
{
return [];
}
8 de 13 20/07/20, 17:05
Managing Moodle with Laravel Nova | by Dennis Sauve | DevOps in... https://medium.com/devops-in-the-trenches/managing-moodle-with-...
This is pretty easy. The $model variable tells Nova what Laravel
Model this Nova Resource relates to. Every Nova Resource relates to
Laravel Model.
9 de 13 20/07/20, 17:05
Managing Moodle with Laravel Nova | by Dennis Sauve | DevOps in... https://medium.com/devops-in-the-trenches/managing-moodle-with-...
The label() function tells Nova how to display the label for this
resource. This label will be how the resource appears in the Nova
sidebar.
Search
The $search variable tells Nova which fields to apply the search
bar argument to.
Fields
10 de 13 20/07/20, 17:05
Managing Moodle with Laravel Nova | by Dennis Sauve | DevOps in... https://medium.com/devops-in-the-trenches/managing-moodle-with-...
This looks like a lot, but it’s fairly straightforward. Every item in
the returned array is some type of field that will be displayed for
each resource item. A few key points:
Create
The rest of the functions are detailed in Nova docs and don’t have
much bearing on what is discussed here, with the exception of the
create() function.
11 de 13 20/07/20, 17:05
Managing Moodle with Laravel Nova | by Dennis Sauve | DevOps in... https://medium.com/devops-in-the-trenches/managing-moodle-with-...
return false;
}
Summary
So now you’ve built a Laravel Nova resource for Moodle Accounts.
You can create more - I have created Courses and
CourseEnrollment Models and Resources. Just these three Models
and Resources allow me to see user information, and what courses
they’re enrolled in from a single page!
You can use the examples above as a template to build out Models
in Laravel and Resources in Nova to monitor more Moodle
information.
Final Thoughts
12 de 13 20/07/20, 17:05
Managing Moodle with Laravel Nova | by Dennis Sauve | DevOps in... https://medium.com/devops-in-the-trenches/managing-moodle-with-...
13 de 13 20/07/20, 17:05