Download the PHP package networkrailbusinesssystems/laravel-moodle without Composer
On this page you can find all versions of the php package networkrailbusinesssystems/laravel-moodle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download networkrailbusinesssystems/laravel-moodle
More information about networkrailbusinesssystems/laravel-moodle
Files in networkrailbusinesssystems/laravel-moodle
Package laravel-moodle
Short Description A Laravel package to authenticate against Moodle and retrieve course data
License
Informations about the package laravel-moodle
LaravelMoodle
A Laravel package to authenticate with Moodle and retrieve course information.
Installation
The package should auto register the service providers.
Migrations
The package includes a migration to make the password column nullable and adds a username field to the users table.
Configuration
Optionally, publish the config file. This is required if you want to customise which fields are synchronised from Moodle to your user model.
Update your auth.php config file, providers > users > drivers to moodle
Update the .env file with the MOODLE_BASE_URL
with the url of your Moodle installation and MOODLE_ADMIN_TOKEN
with a token for an existing user that has permission to search existing users.
User Model
When a user logs in for the first time their email, username and id are set from Moodle in the user model. If you want to customise the fields that are synchronised then publish the laravel-moodle
config file and update the sync_attributes array.
Any fields that you synchronise will need to added to the protected $fillable
array in your User model.
If you want to use a different model to App\User
then update the user_model
in the laravel-moodle
config file.
Credentials
The package expects the credentials from the LoginController to be an array containing username
and password
, but if you want to use username
instead then add MOODLE_LOGIN_ATTRIBUTE=username
to your .env file.
Users set as a Site Administrator in Moodle will not be able to login using LaravelMoodle until they manually generate a token for their account as per step #5 below.
Middleware
The package contains a middleware that you can use to check if the user has a moodle token in their session. If they don't it will log the user out from their Laravel session and redirect the user ot the login page.
To use globally list the NetworkRailBusinessSystems\LaravelMoodle\Middleware\MoodleToken::class
in the $middleware
property of app/Http/Kernel.php
.
To use on specific routes add 'laravel-moodle' => \NetworkRailBusinessSystems\LaravelMoodle\Middleware\MoodleToken::class
to the $routeMiddleware
in app/Http/Kernel.php
and then add to your route.
Moodle Configuration
In order to access data from Moodle, it needs to be configured first as the web service features are disabled by default.
- Enable Web Services for mobile devices
- Site Administration > Mobile app > Mobile settings
- Enable web services for mobile devices - yes
- Save changes
- Enable Web Services (yes, again)
- Site Administration > Advanced features
- Enable Web Services - yes
- Save changes
- Create new webservice
- Site Administration > Plugins > Web services > External Services
- Add Custom Service or use an existing custom service
- Name: Web Service
- Short name: web_service
- Enabled: true
- Authorised users only: false
- Can download files: true
- Save changes
- Enable the following functions in the new Moodle web service
- Site Administration > Plugins > Web services > External Services
- On the newly created Web Service, click functions and add the following
- core_badges_get_user_badges
- core_calendar_get_calendar_monthly_view
- core_completion_get_activities_completion_status
- core_completion_get_course_completion_status
- core_course_get_categories
- core_course_get_contents
- core_course_get_course_module
- core_course_get_courses
- core_course_get_courses_by_field
- core_course_search_courses
- core_enrol_get_enrolled_users
- core_user_get_users
- enrol_manual_enrol_users
- enrol_manual_unenrol_users
- enrol_self_enrol_user
- gradereport_overview_get_course_grades
- mod_assign_get_assignments
- mod_assign_get_submissions
- mod_assign_get_submission_status
- mod_assign_save_submission
- mod_page_get_pages_by_courses
- mod_page_view_page
- mod_resource_get_resources_by_courses
- mod_resource_view_resource
- mod_book_get_books_by_courses
- mod_scorm_get_scorm_scoes
- mod_scorm_get_scorms_by_courses
- Create a token for the admin user (used in the Laravel .env file)
- Site Administration > Plugins > Web services > Manage tokens
- Click Add
- Search for the admin user you want to use
- Select your Web Service
- Save changes
- Allow users to create tokens for the new Web Service so they can create a token when they log in
- Site Administration > Users > Define Roles
- Edit Authenticated user
- Capabilities > Create a web service token > Allow
- Save
Endpoints
Use the LaravelMoodle facade to access the web service data.
The package uses Spatie Data Transfer Objects to format the response into objects.
Get Courses
Returns a collection of courses.
To filter the list of courses pass in the term and the field. you can use id, ids, shortname and category id.
Get Course By Id
Once you know the course ID (int) you can get a specific course.
Search Courses
Pass in your search term as the first parameter (string).
Optionally pass in page number (integer) and per page (integer).
You can also limit the search to only enrolled courses by specifying 1 as the 4th parameter.
Get Course Contents By Id
Once you know the course Id you can get the contents of a specific course.
Get Course Module By Id
Once you know the module id from the course contents you can get more details about the module.
Get Course Pages
Once you know the course id you can get the pages for the course.
Get Course Page
Get a specific course page by module id.
Get Course Scorms
Once you know the course id you can get the scorms for the course.
Get Course Scorm
Get a specific course scorm by module id.
Get Course Resources
Once you know the course id you can get the resources.
Get Course Resource
Get a specific course resource by module id.
Get Course Completion
You can get the course completion status by passing in the user id and the course id.
Get Course Activities Completion
You can get details of a course's activities completion by passing in the user id and the course id.
Get Course Assignments
Get the assignments for a specific course by passing in the course id.
Get Course Assignment
Get a specific course assignment by the module id.
Get Assignment Submission Status
Provides information on the previous attempts for an assignment. User id is optional as it defaults to the current user.
Save Course Assignment
Submit the online text for a specific assignment. Returns true on success.
If there is an issue submitting then an array of warnings will be returned, with the error details in the item and message.
Get User Grades
Get the grades for a user. Default to 0 for the current logged in user.
Get Course Grade for User
Get the grade for a user for a specific course. Default to 0 for the current logged in user. Returns null for grade if course not found.
Search Users
Search for users. Default search field is username if not provided.
Enrol User On A Course
You can enrol a user onto a course by specifying the user id then the course id. By default they have the student role, but you can specify the role id.
The user performing the call must be an Admin or Manager in Moodle or a teacher in the course.
There is a null
response from Moodle when successful, but enrolUserOnCourse returns true
when successful and MoodleException if it fails.
Unenrol User On A Course
You can unenrol a user on a course. Role id is not required. It will default to the student role.
Get Enrolled Users For A Course
You can get a collection of users enrolled on a course. These are Moodle users and not Laravel user models.
Get Categories
Get all categories.
Search Categories
Search for categories. Defaults to searching name if second parameter isn't passed. The search term is an exact match only.
View Page Event
Trigger the view page event for activity auto completion. Pass in the page id. Returns true if successful and MoodleException if unsuccessful.
HasDates Trait
The package has a HasDates trait that can be added to FlexibleDataTransferObjects to convert the timestamp from Moodle into a Carbon instance to allow easier formatting for presentation.