/** * This function prepares complete $USER object for manipulation. * Strip long strings and reject some keys. * * @param array $data Complete $USER object with custom profile fields loaded * @return array Cleaned array created from $data */ function mcae_prepare_profile_data($data, $replace_empty = 'EMPTY') { $reject = array('ajax_updatable_user_prefs', 'sesskey', 'preference', 'editing', 'access', 'message_lastpopup', 'enrol'); if (is_array($data) or is_object($data)) { $new_data = array(); foreach ($data as $key => $val) { if (!in_array($key, $reject)) { if (is_array($val) or is_object($val)) { $new_data[$key] = mcae_prepare_profile_data($val, $replace_empty); } else { if ($val === '' or $val === ' ' or $val === NULL) { $str = $val === false ? 'false' : $replace_empty; } else { $str = $val === true ? 'true' : format_string("{$val}"); } $new_data[$key] = substr($str, 0, 100); } } } } else { if ($data === '' or $data === ' ' or $data === NULL) { $str = $data === false ? 'false' : $replace_empty; } else { $str = $data === true ? 'true' : format_string("{$data}"); } $new_data = substr($str, 0, 100); } if (empty($new_data)) { return $replace_empty; } else { return $new_data; } }
/** * Post authentication hook. * This method is called from authenticate_user_login() for all enabled auth plugins. * * @param object $user user object, later used for $USER * @param string $username (with system magic quotes) * @param string $password plain text password (with system magic quotes) */ function user_authenticated_hook(&$user, $username, $password) { global $DB, $SESSION; $context = context_system::instance(); $uid = $user->id; // Ignore users from don't_touch list $ignore = explode(",", $this->config->donttouchusers); if (!empty($ignore) and array_search($username, $ignore) !== false) { $SESSION->mcautoenrolled = TRUE; return true; } // Ignore guests if ($uid < 2) { $SESSION->mcautoenrolled = TRUE; return true; } // ********************** Get COHORTS data $clause = array('contextid' => $context->id); if ($this->config->enableunenrol == 1) { $clause['component'] = self::COMPONENT_NAME; } $cohorts = $DB->get_records('cohort', $clause); $cohorts_list = array(); foreach ($cohorts as $cohort) { $cid = $cohort->id; $cname = format_string($cohort->name); $cohorts_list[$cid] = $cname; } // Get advanced user data profile_load_data($user); profile_load_custom_fields($user); $user_profile_data = mcae_prepare_profile_data($user, $this->config->secondrule_fld); // Additional values for email list($email_username, $email_domain) = explode("@", $user_profile_data['email']); // email root domain $email_domain_array = explode('.', $email_domain); if (count($email_domain_array) > 2) { $email_rootdomain = $email_domain_array[count($email_domain_array) - 2] . '.' . $email_domain_array[count($email_domain_array) - 1]; } else { $email_rootdomain = $email_domain; } $user_profile_data['email'] = array('full' => $user_profile_data['email'], 'username' => $email_username, 'domain' => $email_domain, 'rootdomain' => $email_rootdomain); // Delimiter $delimiter = $this->config->delim; $delim = strtr($delimiter, array('CR+LF' => chr(13) . chr(10), 'CR' => chr(13), 'LF' => chr(10))); // Calculate a cohort names for user $replacements_tpl = $this->config->replace_arr; $replacements = array(); if (!empty($replacements_tpl)) { $replacements_pre = explode($delim, $replacements_tpl); foreach ($replacements_pre as $rap) { list($key, $val) = explode("|", $rap); $replacements[$key] = $val; } } // Generate cohorts array $main_rule = $this->config->mainrule_fld; $templates_tpl = array(); $templates = array(); if (!empty($main_rule)) { $templates_tpl = explode($delim, $main_rule); } else { $SESSION->mcautoenrolled = TRUE; return; //Empty mainrule } // Find %split function foreach ($templates_tpl as $item) { if (preg_match('/(?<full>%split\\((?<fld>\\w*)\\|(?<delim>.{1,5})\\))/', $item, $split_params)) { // Split! $splitted = explode($split_params['delim'], $user_profile_data[$split_params['fld']]); foreach ($splitted as $key => $val) { $user_profile_data[$split_params['fld'] . "_{$key}"] = $val; $templates[] = strtr($item, array("{$split_params['full']}" => "{{ {$split_params['fld']}_{$key} }}")); } } else { $templates[] = $item; } } $processed = array(); // Process templates with Mustache foreach ($templates as $cohort) { $cohortname = $this->mustache->render($cohort, $user_profile_data); $cohortname = !empty($replacements) ? strtr($cohortname, $replacements) : $cohortname; if ($cohortname == '') { continue; // We don't want an empty cohort name } $cid = array_search($cohortname, $cohorts_list); if ($cid !== false) { if (!$DB->record_exists('cohort_members', array('cohortid' => $cid, 'userid' => $user->id))) { cohort_add_member($cid, $user->id); } } else { // Cohort not exist so create a new one $newcohort = new stdClass(); $newcohort->name = $cohortname; $newcohort->description = "created " . date("d-m-Y"); $newcohort->contextid = $context->id; if ($this->config->enableunenrol == 1) { $newcohort->component = "auth_mcae"; } $cid = cohort_add_cohort($newcohort); cohort_add_member($cid, $user->id); // Prevent creation new cohorts with same names $cohorts_list[$cid] = $cohortname; } $processed[] = $cid; } $SESSION->mcautoenrolled = TRUE; //Unenrol user if ($this->config->enableunenrol == 1) { //List of cohorts where this user enrolled $sql = "SELECT c.id AS cid FROM {cohort} c JOIN {cohort_members} cm ON cm.cohortid = c.id WHERE c.component = 'auth_mcae' AND cm.userid = {$uid}"; $enrolledcohorts = $DB->get_records_sql($sql); foreach ($enrolledcohorts as $ec) { if (array_search($ec->cid, $processed) === false) { cohort_remove_member($ec->cid, $uid); } } } }
global $USER; require_once $CFG->dirroot . '/user/profile/lib.php'; require_once $CFG->dirroot . '/auth/mcae/lib.php'; if ($hassiteconfig) { // needs this condition or there is error on login page $ADMIN->add('accounts', new admin_externalpage('cohorttoolmcae', get_string('auth_cohorttoolmcae', 'auth_mcae'), new moodle_url('/auth/mcae/convert.php'))); $ADMIN->add('accounts', new admin_externalpage('cohortviewmcae', get_string('auth_cohortviewmcae', 'auth_mcae'), new moodle_url('/auth/mcae/view.php'))); } if ($ADMIN->fulltree) { $settings->add(new admin_setting_configtextarea('auth_mcae/mainrule_fld', get_string('auth_mainrule_fld', 'auth_mcae'), '', '')); // Profile field helper $fldlist = array(); $usr_helper = $DB->get_record('user', array('id' => 2)); profile_load_data($usr_helper); profile_load_custom_fields($usr_helper); $fldlist = mcae_prepare_profile_data($usr_helper); // Additional values for email list($email_username, $email_domain) = explode("@", $fldlist['email']); // Email root domain $email_domain_array = explode('.', $email_domain); if (count($email_domain_array) > 2) { $email_rootdomain = $email_domain_array[count($email_domain_array) - 2] . '.' . $email_domain_array[count($email_domain_array) - 1]; } else { $email_rootdomain = $email_domain; } $fldlist['email'] = array('full' => $fldlist['email'], 'username' => $email_username, 'domain' => $email_domain, 'rootdomain' => $email_rootdomain); //print_r($fldlist); $help_array = array(); mcae_print_profile_data($fldlist, '', $help_array); //print_r($usr_helper); //print_r($help_array);