Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 into dev
  • Loading branch information
Gus-Amorim committed Oct 24, 2024
1 parent 40e9844 commit 3a07fad
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 210 deletions.
2 changes: 1 addition & 1 deletion classes/form/oneroster_csv_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected function definition() {
// Submit button.
$this->add_action_buttons(
true,
'Upload'
get_string('upload', 'enrol_oneroster')
);
}
}
80 changes: 38 additions & 42 deletions classes/local/csv_client.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,21 @@ class csv_client implements client_interface {
use root_oneroster_client;
use versioned_client;

// Define constants for the base paths and types
const basepath_orgs = 'orgs';
const basepath_schools = 'schools';
const type_terms = 'terms';
const type_classes = 'classes';
const type_enrollments = 'enrollments';
const basepath_users = 'users';
// Define constants for the base paths and types.
const BASEPATH_ORGS = 'orgs';
const BASEPATH_SCHOOLS = 'schools';
const TYPE_TERMS = 'terms';
const TYPE_CLASSES = 'classes';
const TYPE_ENROLLMENTS = 'enrollments';
const BASEPATH_USERS = 'users';
private $org_id;

// Define constants for keys
const academic_sessions_key = 'academicSessions';
const orgs_key = 'orgs';
const classes_key = 'classes';
const enrollments_key = 'enrollments';
const users_key = 'users';
const periods_key = 'periods';
const subjects_key = 'subjects';
const subject_codes_key = 'subjectCodes';
const grades_key = 'grades';
// Define constants for keys.
const ACADEMIC_SESSIONS_KEY = 'academicSessions';
const PERIODS_KEY = 'periods';
const SUBJECTS_KEYS = 'subjects';
const SUBJECT_CODES_KEY = 'subjectCodes';
const GRADES_KEY = 'grades';

/**
* Authenticate the client. This is a no-op for the CSV client.
Expand Down Expand Up @@ -112,37 +108,37 @@ public function set_org_id($org_id) {
*/
public function execute(command $command, ?filter $filter = null): stdClass {
$url = $command->get_url('');
// Split the URL into tokens using '/' as the delimiter (eg. /schools/org-sch-222-456/terms)
// Split the URL into tokens using '/' as the delimiter (eg. /schools/org-sch-222-456/terms).
$tokens = explode('/', $url);
// The second token represents the base path ('users', 'orgs', 'schools')
// The second token represents the base path ('users', 'orgs', 'schools').
$basepath = $tokens[1];
// The third token represents the Organisation ID
// The third token represents the Organisation ID.
$param = $tokens[2] ?? '';
// The fourth token represents the type of data to fetch ('terms', 'classes', 'enrollments')
// The fourth token represents the type of data to fetch ('terms', 'classes', 'enrollments').
$type = $tokens[3] ?? '';
// Get the organisation ID
// Get the organisation ID.
$org_id = $this->org_id ?? null;

if ($org_id == null) {
throw new \Exception('Organization ID is not set.');
}

switch ($basepath) {
case self::basepath_orgs:
// The endpoint getAllOrgs is called to fetch all organisations
case self::BASEPATH_ORGS:
// The endpoint getAllOrgs is called to fetch all organisations.
if ($param === $org_id || $param === '') {
$orgdata = $this->data[self::orgs_key];
$orgdata = $this->data[self::BASEPATH_ORGS];
$keys = array_map(function($orgs) { return $orgs['sourcedId']; }, $orgdata);
// Combine keys and organization data into an associative array
// Combine keys and organization data into an associative array.
$mapped_data = array_combine($keys, $orgdata);
if (isset($mapped_data[$org_id])) {
$org = (object) $mapped_data[$org_id];
// If status and dateLastModified are not set, set them to active and the current date
// If status and dateLastModified are not set, set them to active and the current date.
if ($org->status == null && $org->dateLastModified == null) {
$org->status = 'active';
$org->dateLastModified = date('Y-m-d');
}
// To ensure compatibility with v1.0, set the status to 'tobedeleted' if it is 'inactive'
// To ensure compatibility with v1.0, set the status to 'tobedeleted' if it is 'inactive'.
if ($org->status === 'inactive') {
$org->status = 'tobedeleted';
}
Expand All @@ -157,10 +153,10 @@ public function execute(command $command, ?filter $filter = null): stdClass {
];
}

case self::basepath_schools:
// The endpoint getTermsForSchool is called to fetch a list of classes in a term
if ($type === self::type_terms) {
$academicsessiondata = $this->data[self::academic_sessions_key];
case self::BASEPATH_SCHOOLS:
// The endpoint getTermsForSchool is called to fetch a list of classes in a term.
if ($type === self::TYPE_TERMS) {
$academicsessiondata = $this->data[self::ACADEMIC_SESSIONS_KEY];
$keys = array_map(function ($schools) { return $schools['sourcedId']; }, $academicsessiondata);
$mapped_data = array_combine($keys, $academicsessiondata);
$academicSession = [];
Expand All @@ -182,9 +178,9 @@ public function execute(command $command, ?filter $filter = null): stdClass {
];
}

if ($type === self::type_classes) {
// The endpoint getClassesForSchool is called to fetch all students for a class
$classdata = $this->data[self::classes_key];
if ($type === self::TYPE_CLASSES) {
// The endpoint getClassesForSchool is called to fetch all students for a class.
$classdata = $this->data[self::TYPE_CLASSES];
$keys = array_map(function($schools) { return $schools['sourcedId']; }, $classdata);
$mapped_data = array_combine($keys, $classdata);
$classes = [];
Expand Down Expand Up @@ -212,7 +208,7 @@ public function execute(command $command, ?filter $filter = null): stdClass {
$class->periods = [];
}

$objs = [self::periods_key, self::subjects_key, self::subject_codes_key, self::grades_key];
$objs = [self::PERIODS_KEY, self::SUBJECTS_KEYS, self::SUBJECT_CODES_KEY, self::GRADES_KEY];

foreach ($objs as $obj) {
if (!empty($class->$obj)) {
Expand All @@ -239,9 +235,9 @@ public function execute(command $command, ?filter $filter = null): stdClass {
];
}

if ($type === self::type_enrollments) {
// The endpoint getEnrollmentsForSchool is called to fetch all enrollments in a school
$enrollmentdata = $this->data[self::enrollments_key];
if ($type === self::TYPE_ENROLLMENTS) {
// The endpoint getEnrollmentsForSchool is called to fetch all enrollments in a school.
$enrollmentdata = $this->data[self::TYPE_ENROLLMENTS];
$keys = array_map(function($schools) { return $schools['sourcedId']; }, $enrollmentdata);
$mapped_data = array_combine($keys, $enrollmentdata);
$enrollments = [];
Expand All @@ -266,9 +262,9 @@ public function execute(command $command, ?filter $filter = null): stdClass {
];
}

case self::basepath_users:
// The endpoint getAllUsers is called to fetch all users in a school
$usersData = $this->data[self::users_key];
case self::BASEPATH_USERS:
// The endpoint getAllUsers is called to fetch all users in a school.
$usersData = $this->data[self::BASEPATH_USERS];
$keys = array_map(function($user) {return $user['sourcedId']; }, $usersData);
$mapped_data = array_combine($keys, $usersData);
$users = [];
Expand Down
18 changes: 9 additions & 9 deletions classes/local/csv_client_const_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
namespace enrol_oneroster\classes\local;

/**
* Class csv_client_const_helper
* Class csv_client_const_helper.
*
* This class contains constants that are used throughout the OneRoster CSV client.
*
Expand All @@ -25,7 +25,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class csv_client_const_helper {
// Individual header constants
// Individual header constants.
const HEADER_SOURCEDID = 'sourcedId';
const HEADER_STATUS = 'status';
const HEADER_DATE_LAST_MODIFIED = 'dateLastModified';
Expand Down Expand Up @@ -65,15 +65,15 @@ class csv_client_const_helper {
const HEADER_AGENT_SOURCEDIDS = 'agentSourcedIds';
const HEADER_PASSWORD = 'password';

// CSV file names
// CSV file names.
const FILE_MANIFEST = 'manifest.csv';
const FILE_ACADEMIC_SESSIONS = 'academicSessions.csv';
const FILE_CLASSES = 'classes.csv';
const FILE_ENROLLMENTS = 'enrollments.csv';
const FILE_ORGS = 'orgs.csv';
const FILE_USERS = 'users.csv';

// Define constants for the types
// Define constants for the types.
const DATATYPE_NULL = 'null';
const DATATYPE_GUID = 'guid';
const DATATYPE_INT = 'int';
Expand All @@ -98,7 +98,7 @@ class csv_client_const_helper {
const DATATYPE_PASSWORD = 'password';
const DATATYPE_STRING = 'string';

// Valid Enum values
// Valid Enum values.
const valid_class_types = ['homeroom', 'scheduled'];
const valid_roles = ['administrator', 'proctor', 'student', 'teacher'];
const valid_primary_values = ['true', 'false'];
Expand All @@ -107,14 +107,14 @@ class csv_client_const_helper {
'administrator', 'aide', 'guardian', 'parent', 'proctor',
'relative', 'student', 'teacher'
];
// Valid grade codes from the Common Education Data Standards (CEDS)
// Reference: https://ceds.ed.gov/CEDSElementDetails.aspx?TermId=7100
// Valid grade codes from the Common Education Data Standards (CEDS).
// Reference: https://ceds.ed.gov/CEDSElementDetails.aspx?TermId=7100.
const valid_grade_codes = [
'IT', 'PR', 'PK', 'TK', 'KG', '01', '02', '03', '04', '05', '06',
'07', '08', '09', '10', '11', '12', '13', 'PS', 'UG', 'Other'
];

// Header constants for each file
// Header constants for each file.
const HEADER_ACADEMIC_SESSIONS = [
self::HEADER_SOURCEDID, self::HEADER_STATUS, self::HEADER_DATE_LAST_MODIFIED, self::HEADER_TITLE,
self::HEADER_TYPE, self::HEADER_START_DATE, self::HEADER_END_DATE, self::HEADER_PARENT_SOURCEDID, self::HEADER_SCHOOL_YEAR
Expand All @@ -140,7 +140,7 @@ class csv_client_const_helper {
self::HEADER_EMAIL, self::HEADER_SMS, self::HEADER_PHONE, self::HEADER_AGENT_SOURCEDIDS, self::HEADER_GRADES, self::HEADER_PASSWORD
];

// Required files and their headers
// Required files and their headers.
const REQUIRED_FILES = [
self::FILE_ACADEMIC_SESSIONS => self::HEADER_ACADEMIC_SESSIONS,
self::FILE_CLASSES => self::HEADER_CLASSES,
Expand Down
Loading

0 comments on commit 3a07fad

Please sign in to comment.