You are on page 1of 13

Managing Moodle with Laravel Nova | by Dennis Sauve | DevOps in... https://medium.com/devops-in-the-trenches/managing-moodle-with-...

Managing Moodle with Laravel


Nova
Dennis Sauve Follow
Mar 12, 2019 · 6 min read

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.

This migration included moving not only e-commerce, but the


learning management system and tools as well. Moodle appeared
to be a viable candidate for handling our courses and learning

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-...

tools. I was excited to build our platform on open-source tools, and


facing the challenges that follow such an endeavor.

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.

Moodle’s administration and reporting tools are terrible, or non-


existent.

I wanted to know, at a glance, how many new or existing users had


bought courses. I wanted a comprehensive overview of users and
courses within 3 clicks of the dashboard.

So I built it.

Laravel Nova relies on resources to pull data, and resources are


based on models. These models can be as simple as a reference to a
database table. Leveraging that, we can create models from the
necessary tables that will provide meaningful insight to what users
are doing.

I wanted a comprehensive overview… 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-...

What you need:


A working knowledge of Laravel and Laravel Nova

A good understanding of Moodle’s database and how the tables


relate to one another.

Access to your Moodle Database, and to have that connection


configured within Laravel’s config/database.php file.

Patience :)

1. Con0iguring your Moodle connection


In your config/database.php file, create a Moodle database
connection, it should look something like this.

'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-...

variables, please see this tutorial.

2. Creating a Model from a Moodle Table


We’re going to take a look at creating a MoodleAccount model.
This will reference Moodle’s user table, which is mdl_user

Option 1:

The first thing to do is create the actual model. You can do this by
running a php artisan command

php artisan make:model MoodleAccount

I like to group my models, so I created a Moodle directory in my

Models directory. You’ll need to do the same, then move


MoodleAccount.php into that directory. This is optional in the
future, but necessary for this tutorial.

Option 2:

You could just create the app\Models\Moodle directory and create


MoodleAccount.php within.

Next:

You’ll want to copy/paste the following into your


MoodleAccount.php file.

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;

class MoodleAccount extends Model


{
// Choose the database connection to use
protected $connection = 'moodle';

// Choose the table to reference


protected $table = 'mdl_user';

// Set the primary id key reference


protected $primaryKey = 'id';
}

This is fairly straightforward, so long as you’ve configured your


Moodle database connection. Now that we’ve created the
MoodleAccount model, it’s time to create the Laravel Nova resource
that will reference it.

3. Creating a Laravel Nova resource from a model


We need to start by creating a Nova resource file. The artisan
command is

php artisan nova:resource MoodleAccount

The code for the resource is

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;

class MoodleAccount extends Resource


{
/**
* The model the resource corresponds to.
*
* @var string
*/
public static $model = 'App\Models\Moodle
\MoodleAccount';
public static $group = 'Moodle';

/**
* 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 [];
}

public function create()


{
return false;
}
}

Oof, that’s a big wall of code.

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-...

Let’s break it down a little…

Model and Group

public static $model = 'App\Models\Moodle\MoodleAccount';


public static $group = 'Moodle';

This is pretty easy. The $model variable tells Nova what Laravel
Model this Nova Resource relates to. Every Nova Resource relates to
Laravel Model.

The $group variable is optional. This variable will separate the


MoodleAccount resource away from the rest of the resources show
on the Nova dashboard, into its’ own grouping. This is optional, but
certainly nice to have when managing more than a dozen
resources.

Title and Label

public static $title = 'id';


public static function label() { return "Moodle Accounts"; }

Again, straightforward enough. $title tells Nova how what field


to use to represent each Resource object. This can be any field, but
for this example, we’re using id.

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

public static $search = [


'username',
'email',
'lastname',
'firstname',
];

The $search variable tells Nova which fields to apply the search
bar argument to.

Fields

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(),

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-...

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;
}),
];
}

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:

Applying Sortable to a field will allow the user to click on the


column headers and sort the results on that column.

Function values (the second argument in the make() function)


allow for you to create computed fields. This is helpful when
inferring values from other data types (see the
Boolean::make(‘Active’, function()…) field)

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.

public function create()


{

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;
}

This is just a basic function telling Nova not to allow anyone to


create a new record of this resource. This is mildly important
because Moodle handles Account creation, course enrollments, etc.
and has logic and actions that happen behind the scenes.
Attempting to create users manually in the Moodle database will
cause nothing but headache and frustration down the road, and
may result in your database being unusable.

For external user creation tools, I suggest building tools based on


the Moodle Web Services API.

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-...

I built resources in Laravel Nova because it’s the system we were,


and for the moment are, using to manage user’s purchases and
enrollments between three different systems.

Since I began writing this article, our company is investigating


using other tools (like metabase) to glean user information and
Laravel Moodle Development PHP Nova
perform data analysis. This is a disappointment to me, having
invested a significant amount of time learning the ins and outs of
Laravel, Nova, and Moodle — though I am excited about easier to
Discover Medium
configure, Make
out of the box Medium
reporting yours
and analysisBecome
tools. a member
Welcome to a place where Follow all the topics you care Get unlimited access to the
words matter. On Medium, about, and we’ll deliver the best stories on Medium —
smart voices and original best stories for you to your and support writers while
ideas take center stage - with homepage and inbox. Explore you’re at it. Just $5/month.
no ads in sight. Watch Upgrade

About Help Legal

13 de 13 20/07/20, 17:05

You might also like