From 0c441b0344aa4c978b6460deaba1ab94fa9c6345 Mon Sep 17 00:00:00 2001 From: b0ink <40929320+b0ink@users.noreply.github.com> Date: Fri, 7 Nov 2025 16:37:27 +1100 Subject: [PATCH] chore: init moodle api queries --- app/helpers/moodle_integration.rb | 103 ++++++++++++++++++++++++++++++ config/application.rb | 4 ++ 2 files changed, 107 insertions(+) create mode 100644 app/helpers/moodle_integration.rb diff --git a/app/helpers/moodle_integration.rb b/app/helpers/moodle_integration.rb new file mode 100644 index 000000000..e8d01649f --- /dev/null +++ b/app/helpers/moodle_integration.rb @@ -0,0 +1,103 @@ +require 'oauth2' +require 'net/http' +require 'uri' +require 'json' + +# Class to load moodle features +# +class MoodleIntegration + def self.enabled? + Doubtfire::Application.config.d2l_enabled && + Doubtfire::Application.config.d2l_client_id.present? && + Doubtfire::Application.config.d2l_client_secret.present? && + Doubtfire::Application.config.d2l_redirect_uri.present? + end + + def self.moodle_api_key + Doubtfire::Application.config.moodle_api_key + end + + def self.moodle_api_url + Doubtfire::Application.config.moodle_api_url + end + + def self.save_user_extension + uri = URI("#{moodle_api_url}/webservice/rest/server.php") + uri.query = URI.encode_www_form({ + wstoken: moodle_api_key, + wsfunction: 'mod_assign_save_user_extensions', + moodlewsrestformat: 'json', + assignmentid: 1, + 'userids[0]' => 3, + 'dates[0]' => 1_763_793_200 + }) + + res = Net::HTTP.get_response(uri) + + # puts res.code + # puts res.body + # puts res.message + + JSON.parse(res.body) + end + + def self.get_context_users(course_id) + uri = URI("#{moodle_api_url}/webservice/rest/server.php") + uri.query = URI.encode_www_form({ + wstoken: moodle_api_key, + wsfunction: 'core_enrol_get_enrolled_users', + moodlewsrestformat: 'json', + courseid: course_id + }) + + res = Net::HTTP.get_response(uri) + + uri.query = URI.encode_www_form({ + wstoken: moodle_api_key, + wsfunction: 'mod_assign_get_assignments', + moodlewsrestformat: 'json', + courseid: course_id + }) + + res = Net::HTTP.get_response(uri) + + # puts res.code + # puts res.body + # puts res.message + + JSON.parse(res.body) + + # Example output: + # [{ "id" => 2, + # "username" => "user", + # "firstname" => "Admin", + # "lastname" => "User", + # "fullname" => "Admin User", + # "email" => "user@example.com", + # "department" => "", + # "firstaccess" => 1762488076, + # "lastaccess" => 1762491307, + # "lastcourseaccess" => 1762491351, + # TODO: we can use the role here to import the user a tutor + # "roles" => [{ "roleid" => 3, "name" => "", "shortname" => "editingteacher", "sortorder" => 0 }], + # "enrolledcourses" => [{ "id" => 2, "fullname" => "FIT1045 Introduction to Programming", "shortname" => "FIT1045" }] }, + # { "id" => 3, + # "username" => "student_1", + # "firstname" => "Example", + # "lastname" => "Student", + # "fullname" => "Example Student", + # "email" => "student@example.com", + # "department" => "", + # "firstaccess" => 0, + # "lastaccess" => 0, + # "lastcourseaccess" => 0, + # "description" => "", + # "descriptionformat" => 1, + # TODO: we can use the role here to import the user and enrol them as a student + # "roles" => [{ "roleid" => 5, "name" => "", "shortname" => "student", "sortorder" => 0 }], + # "enrolledcourses" => [{ "id" => 2, "fullname" => "FIT1045 Introduction to Programming", "shortname" => "FIT1045" }] }] + + # TODO: loop through each user, create the user and enrol them into the linked course + # TODO: The unit details will probably need a field to set the course_id + end +end diff --git a/config/application.rb b/config/application.rb index 2b54558e5..fe5b3fe11 100644 --- a/config/application.rb +++ b/config/application.rb @@ -94,6 +94,10 @@ def self.fetch_boolean_env(name) # LTI.js will send signed JWT tokens using this secret config.lti_api_secret = ENV.fetch('LTI_SHARED_API_SECRET', nil) + # ==> Moodle settings + config.moodle_api_url = ENV.fetch('DF_MOODLE_API_URL', nil) + config.moodle_api_key = ENV.fetch('DF_MOODLE_API_KEY', nil) + # ==> Institution settings # Institution YAML and ENV (override) config load config.institution = YAML.load_file(Rails.root.join('config/institution.yml').to_s).with_indifferent_access