Skip to content

Commit

Permalink
Issue 3 and 6: Remove dependency of custom plugin local_organisations
Browse files Browse the repository at this point in the history
  • Loading branch information
sumaiyamannan committed Jan 30, 2025
1 parent 450d9cb commit 5ab53a5
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 104 deletions.
32 changes: 16 additions & 16 deletions classes/form/edit_school_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,23 @@ public function definition() {
}
$select->setSelected($selected);
}
if (helper::check_local_organisations()) {
// Get system level cohorts.
global $DB;
$records = $DB->get_records('cohort', ['visible' => 1, 'contextid' => 1], 'name');
$cohorts[0] = 'None';
foreach ($records as $record) {
$cohorts[$record->id] = $record->name;
}
$select = $mform->addElement('select', 'cohortid', get_string('cohortid', 'tool_smsimport'), $cohorts);
if (!empty($school->cohortid)) {
$mform->updateElementAttr('cohortid', ['disabled' => 'disabled']);
$mform->addElement('checkbox', 'unlink', get_string('unlink', 'tool_smsimport'));
$mform->addHelpButton('unlink', 'unlink', 'tool_smsimport');
}
$select->setSelected($school->cohortid);
$mform->addHelpButton('cohortid', 'cohortid', 'tool_smsimport');

// Get system level cohorts.
global $DB;
$records = $DB->get_records('cohort', ['visible' => 1, 'contextid' => 1], 'name');
$cohorts[0] = 'None';
foreach ($records as $record) {
$cohorts[$record->id] = $record->name;
}
$select = $mform->addElement('select', 'cohortid', get_string('cohortid', 'tool_smsimport'), $cohorts);
if (!empty($school->cohortid)) {
$mform->updateElementAttr('cohortid', ['disabled' => 'disabled']);
$mform->addElement('checkbox', 'unlink', get_string('unlink', 'tool_smsimport'));
$mform->addHelpButton('unlink', 'unlink', 'tool_smsimport');
}
$select->setSelected($school->cohortid);
$mform->addHelpButton('cohortid', 'cohortid', 'tool_smsimport');


$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
Expand Down
132 changes: 44 additions & 88 deletions classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@

namespace tool_smsimport;

use local_organisations\persistent\school;
use stdClass;

defined('MOODLE_INTERNAL') || die();

require_once($CFG->libdir . '/filelib.php');
require_once("{$CFG->dirroot}/local/organisations/locallib.php");
require_once("{$CFG->dirroot}/cohort/lib.php");
require_once("{$CFG->dirroot}/user/lib.php");
require_once("{$CFG->dirroot}/user/profile/lib.php");
Expand Down Expand Up @@ -114,21 +112,6 @@ public static function get_sms_schools($params) {
return $record;
}

/**
* Check if local_organisations is installed.
*
* @return boolean
*/
public static function check_local_organisations() {
$pluginman = \core_plugin_manager::instance();
$plugins = $pluginman->get_installed_plugins('local');
if (array_key_exists('organisations', $plugins)) {
return true;
} else {
return false;
}
}

/**
* Edit SMS school details.
*
Expand Down Expand Up @@ -198,7 +181,6 @@ public static function save_sms_school_details($data, $action = null, $groups =
$data->timemodified = time();
$result = new stdClass();
$courseid = get_config('tool_smsimport', 'smscourse');
$localorg = self::check_local_organisations();
// Log record.
$logrecord = new stdClass();
$logrecord->schoolno = $data->schoolno;
Expand All @@ -208,19 +190,11 @@ public static function save_sms_school_details($data, $action = null, $groups =
$logaction = get_string('logcreate', 'tool_smsimport');
$cohortid = $data->cohortid;
$schoolname = $data->name;
// Plugin local_organisations school update.
$data->organisationname = $schoolname;
$data->organisationcode = $data->schoolno;
$data->organisation_school_type = school::STANDARD;
if ($cohortid == 0) {
// Create new school and cohort.
$result->message[] = get_string('notifyschoolcreate', 'tool_smsimport', $schoolname);
if ($action == 'edit') {
if ($localorg) {
$cohortid = local_organisations_create_organisation($data, 'tool_smsimport');
} else {
$cohortid = cohort_add_cohort($data);
}
$cohortid = cohort_add_cohort($data);
$data->cohortid = $cohortid;
$result->message[] = get_string('notifyschoolcreated', 'tool_smsimport', $cohortid);
}
Expand Down Expand Up @@ -298,7 +272,7 @@ public static function save_sms_school_details($data, $action = null, $groups =
$logaction = get_string('logupdate', 'tool_smsimport');
} else {
// If the SMS school is linked to existing school.
if ($localorg && $records = local_organisations_get_organisation_groups($cohortid, $courseid)) {
if ($records = get_cohort_groups($cohortid, $courseid)) {
$ngroupname = str_replace(' ', '', $groupname);
// Check if the existing school's group match the SMS school group.
$logaction = get_string('logcreate', 'tool_smsimport');
Expand Down Expand Up @@ -356,9 +330,6 @@ public static function save_sms_school_details($data, $action = null, $groups =
if ($action == 'edit') {
// Link groups to local organisation schools.
self::save_sms_school_groups($data->id, $groupid);
if ($localorg) {
local_organisations_store_organisation_group($cohortid, $groupid);
}
$data->groupid = $groupid;
// Log event.
if ($logaction) {
Expand Down Expand Up @@ -427,6 +398,28 @@ public static function save_cohort_course($cohortid, $cohortname, $courseid) {
return true;
}

/**
* Get groups to cohort-group linking table.
*
* @param int $cohortid
* @param int $groupid
* @return array of groups
*/
function get_cohort_groups($cohortid, $courseid) {
global $DB;
$sql = "SELECT g.*, og.orggroupname FROM {groups} g
JOIN {tool_smsimport_school_groups} og ON g.id = og.groupid
WHERE g.courseid = :courseid
AND og.cohortid = :cohortid
AND g.name not ilike '%cohort%'";
$params = array('courseid' => $courseid, 'cohortid' => $cohortid);

$groups = $DB->get_records_sql($sql, $params);

return $groups;

}


/**
* Link and unlink a group to a SMS school.
Expand Down Expand Up @@ -474,23 +467,19 @@ public static function delete_sms_school_groups($schoolid, $groupid) {
*/
public static function is_teacher($userid, $courseid) {
global $DB;
if (self::check_local_organisations()) {
$isteacher = false;
$context = \context_course::instance($courseid);
// Get roles for the course.
$roles = $DB->get_records_sql("SELECT DISTINCT(ra.id), r.id AS role, r.shortname
FROM {role_assignments} ra, {role} r
WHERE userid = ?
AND contextid = ?
AND r.id = ra.roleid", [$userid, $context->id]);
foreach ($roles as $role) {
if ($role->shortname == 'teacher') {
$isteacher = true;
break;
}
$isteacher = false;
$context = \context_course::instance($courseid);
// Get roles for the course.
$roles = $DB->get_records_sql("SELECT DISTINCT(ra.id), r.id AS role, r.shortname
FROM {role_assignments} ra, {role} r
WHERE userid = ?
AND contextid = ?
AND r.id = ra.roleid", [$userid, $context->id]);
foreach ($roles as $role) {
if ($role->shortname == 'teacher') {
$isteacher = true;
break;
}
} else {
$isteacher = true;
}
return $isteacher;
}
Expand Down Expand Up @@ -663,7 +652,6 @@ public static function transfer_user_school($school, $groupid, $usernsn,
$logrecord->other = '';
$cohortid = $info['cohortid'];
$userid = $info['userid'];
$localorg = self::check_local_organisations();
// If student exists in other schools then deal with transfer-in/transfer-out.
$otherrecords = $DB->get_records_select('cohort_members', 'cohortid != :cohortid AND userid = :userid',
['cohortid' => $cohortid, 'userid' => $userid], '', '*');
Expand All @@ -677,11 +665,6 @@ public static function transfer_user_school($school, $groupid, $usernsn,
$info['transferin'] = $school->schoolno;
$info['transferout'] = $oldschool->schoolno;
if (empty($oldschool)) {
if ($localorg) {
$orgschool = school::from_cohort_id($oldcohortid);
$oldschool->transferout = $orgschool->get('transferout');
$oldschool->cohortid = $oldcohortid;
}
$oldschool->schoolno = 0;
$info['transferout'] = $oldschool->schoolno." ({$oldcohortid})";
}
Expand Down Expand Up @@ -765,24 +748,17 @@ public static function import_school_users($school, $smsusers, $logsource = 'cro
$updateuser = 0;
$syncerror = '';
$usersparsed = new stdClass();
$localorg = self::check_local_organisations();
$groups = self::get_sms_school_groups($school->id, 'schoolid');
// Get SMS school config.
$cohortid = $school->cohortid;
if ($logsource == 'cron') {
// The users are coming from an external API.
$authtype = 'webservice';
$linebreak = "\n";
$groups = self::get_sms_school_groups($school->id, 'schoolid');

} else {
if ($localorg) {
$authtype = 'nologin';
$linebreak = "<br>";
$orggroups = local_organisations_get_organisation_groups($cohortid, $courseid);
foreach ($orggroups as $orggroup) {
$orggroupname = str_replace($school->name, '', $orggroup->orggroupname);
$groups[$orggroup->id] = $orggroupname;
}
}
$authtype = 'login';
$linebreak = "\n";
}
// Log record.
$logrecord = new stdClass();
Expand Down Expand Up @@ -991,7 +967,6 @@ public static function find_groupidnumber($groupname, $school) {
* @return string translated data value
*/
public static function sms_data_mapping($name, $value) {
$localorg = self::check_local_organisations();
$finalvalue = '';
$error = '';
$customfield = profile_get_custom_field_data_by_shortname($name);
Expand All @@ -1002,26 +977,10 @@ public static function sms_data_mapping($name, $value) {
switch ($name) {
// Format e.g. : Australian.
case "ethnicity":
if ($localorg) {
if (empty($value)) {
$finalvalue = 'Unknown';
} else {
$sitevalue = explode("\n", $customfield->param1);
$multiet = explode(",", $value);
/* If there are multiple ethnicities, take the first one and ignore the rest.
as this profile field currently only supports taking a single value.
*/
$value = $multiet[0];
if (!$finalvalue = local_organisations_convert_ethnicity_to_category($value)) {
$et = explode("/", $value);
foreach ($et as $ethnicity) {
if (!$finalvalue = local_organisations_convert_ethnicity_to_category($ethnicity)) {
$finalvalue = 'Unknown';
$error = 1;
}
}
}
}
if (empty($value)) {
$finalvalue = 'Unknown';
} else {
$finalvalue = $value;
}
break;

Expand Down Expand Up @@ -1535,9 +1494,6 @@ public static function parse_data($data, $options, $school) {
$newgroupdata->courseid = $courseid;
$newgroupdata->name = $groupname;
$groupid = groups_create_group($newgroupdata);
if (self::check_local_organisations()) {
local_organisations_store_organisation_group($school->cohortid, $groupid);
}
}
}
$user->$label = trim($value);
Expand Down

0 comments on commit 5ab53a5

Please sign in to comment.