Exemplo n.º 1
0
/**
 * Migrate a single Moodle user to the Program Management system.  Will
 * only do this for users who have an idnumber set.
 *
 * @param object $mu Moodle user object
 * @return boolean Whether user was synchronized or not
 */
function pm_moodle_user_to_pm($mu)
{
    global $CFG, $DB;
    require_once $CFG->dirroot . '/lib/moodlelib.php';
    require_once elis::lib('data/customfield.class.php');
    require_once elispm::lib('data/user.class.php');
    require_once elispm::lib('data/usermoodle.class.php');
    require_once elis::lib('data/data_filter.class.php');
    require_once $CFG->dirroot . '/user/profile/lib.php';
    require_once elis::lib('lib.php');
    if (!isset($mu->id)) {
        return true;
    }
    // re-fetch, in case this is from a stale event
    $mu = $DB->get_record('user', array('id' => $mu->id));
    if (user_not_fully_set_up($mu) || !$mu->confirmed) {
        // Prevent the sync if a bare-bones user record is being created by create_user_record
        // or Moodle user has not yet been confirmed.
        return true;
    }
    //not going to be concerned with city or password for now
    if (empty($mu->idnumber) && elis::$config->local_elisprogram->auto_assign_user_idnumber) {
        //make sure the current user's username does not match up with some other user's
        //idnumber (necessary since usernames and idnumbers aren't bound to one another)
        if (!$DB->record_exists('user', array('idnumber' => $mu->username))) {
            $mu->idnumber = $mu->username;
            $DB->update_record('user', $mu);
        }
    }
    // skip user if no ID number set
    if (empty($mu->idnumber)) {
        return true;
    }
    // track whether we're syncing an idnumber change over to the PM system
    $idnumber_updated = false;
    // track whether an associated Moodle user is linked to the current PM user
    $moodle_user_exists = false;
    // determine if the user is already noted as having been associated to a PM user
    // this will join to Moodle user and PM user table to ensure data correctness
    $filters = array();
    $filters[] = new join_filter('muserid', 'user', 'id');
    $filters[] = new join_filter('cuserid', user::TABLE, 'id');
    $filters[] = new field_filter('muserid', $mu->id);
    if ($um = usermoodle::find($filters)) {
        if ($um->valid()) {
            $um = $um->current();
            //signal that an associated user already exists
            $moodle_user_exists = true;
            // determine if the Moodle user idnumber was updated
            if ($um->idnumber != $mu->idnumber) {
                //signal that the idnumber was synced over
                $idnumber_updated = true;
                // update the PM user with the new idnumber
                $cmuser = new user();
                $cmuser->id = $um->cuserid;
                $cmuser->idnumber = $mu->idnumber;
                $cmuser->save();
                // update the association table with the new idnumber
                $um->idnumber = $mu->idnumber;
                $um->save();
            }
        }
    }
    // find the linked PM user
    //filter for the basic condition on the Moodle user id
    $condition_filter = new field_filter('id', $mu->id);
    //filter for joining the association table
    $association_filter = new join_filter('muserid', 'user', 'id', $condition_filter);
    //outermost filter
    $filter = new join_filter('id', usermoodle::TABLE, 'cuserid', $association_filter);
    $cu = user::find($filter);
    if ($cu->valid()) {
        $cu = $cu->current();
    } else {
        // if a user with the same username but different idnumber exists,
        // we can't sync over because it will violate PM user uniqueness
        // constraints
        $cu = user::find(new field_filter('username', $mu->username));
        if ($cu->valid()) {
            return true;
        }
        // if no such PM user exists, create a new one
        $cu = new user();
        $cu->transfercredits = 0;
        $cu->timecreated = time();
    }
    // synchronize standard fields
    $cu->username = $mu->username;
    $cu->password = $mu->password;
    // only need to update the idnumber if it wasn't handled above
    if (!$idnumber_updated) {
        $cu->idnumber = $mu->idnumber;
    }
    $cu->firstname = $mu->firstname;
    $cu->lastname = $mu->lastname;
    $cu->email = $mu->email;
    $cu->address = $mu->address;
    $cu->city = $mu->city;
    $cu->country = $mu->country;
    if (!empty($mu->phone1)) {
        $cu->phone = $mu->phone1;
    }
    if (!empty($mu->phone2)) {
        $cu->phone2 = $mu->phone2;
    }
    if (!empty($mu->lang)) {
        $cu->language = $mu->lang;
    }
    $cu->timemodified = time();
    // synchronize custom profile fields
    profile_load_data($mu);
    fix_moodle_profile_fields($mu);
    $fields = field::get_for_context_level(CONTEXT_ELIS_USER);
    $fields = $fields ? $fields : array();
    require_once elis::plugin_file('elisfields_moodleprofile', 'custom_fields.php');
    foreach ($fields as $field) {
        $field = new field($field);
        if (!moodle_profile_can_sync($field->shortname)) {
            continue;
        }
        if (isset($field->owners['moodle_profile']) && isset($mu->{"profile_field_{$field->shortname}"})) {
            // check if should sync user profile field settings
            if ($field->owners['moodle_profile']->exclude == pm_moodle_profile::sync_from_moodle) {
                sync_profile_field_settings_from_moodle($field);
            }
            $fieldname = "field_{$field->shortname}";
            $cu->{$fieldname} = $mu->{"profile_field_{$field->shortname}"};
        }
    }
    //specifically tell the user save not to use the local_elisprogram_usr_mdl for syncing
    //because the record hasn't been inserted yet (see below)
    try {
        $cu->save(false);
    } catch (Exception $ex) {
        if (in_cron()) {
            mtrace(get_string('record_not_created_reason', 'local_elisprogram', array('message' => $ex->getMessage() . " [{$mu->id}]")));
            return false;
        } else {
            throw new Exception($ex->getMessage());
        }
    }
    // if no user association record exists, create one
    if (!$moodle_user_exists) {
        $um = new usermoodle();
        $um->cuserid = $cu->id;
        $um->muserid = $mu->id;
        $um->idnumber = $mu->idnumber;
        $um->save();
    }
    return true;
}
Exemplo n.º 2
0
 /**
  * Function to synchronize the curriculum data with the Moodle data.
  *
  * @param boolean $tomoodle Optional direction to synchronize the data.
  * @param boolean $strict_match Whether we should use the association table rather
  *                               than just check idnumbers when comparing to Moodle users
  *
  */
 function synchronize_moodle_user($tomoodle = true, $createnew = false, $strict_match = true)
 {
     global $CFG;
     require_once $CFG->dirroot . '/admin/tool/uploaduser/locallib.php';
     require_once elispm::lib('data/usermoodle.class.php');
     require_once elis::lib('lib.php');
     static $mu_loop_detect = array();
     // Create a new Moodle user record to update with.
     if (!($muser = $this->get_moodleuser($strict_match)) && !$createnew) {
         return false;
     }
     $muserid = $muser ? $muser->id : false;
     if ($tomoodle) {
         // map PM user fields to Moodle user fields
         $mdlfieldmap = array('idnumber' => 'idnumber', 'username' => 'username', 'firstname' => 'firstname', 'lastname' => 'lastname', 'email' => 'email', 'address' => 'address', 'city' => 'city', 'country' => 'country', 'language' => 'lang');
         // determine if the user is already noted as having been associated to a PM user
         if ($um = usermoodle::find(new field_filter('cuserid', $this->id))) {
             if ($um->valid()) {
                 $um = $um->current();
                 // determine if the PM user idnumber was updated
                 if ($um->idnumber != $this->idnumber) {
                     // update the Moodle user with the new idnumber
                     $muser = new stdClass();
                     $muser->id = $um->muserid;
                     $muser->idnumber = $this->idnumber;
                     $this->_db->update_record('user', $muser);
                     // update the association table with the new idnumber
                     $um->idnumber = $this->idnumber;
                     $um->save();
                 }
             }
         }
         //try to update the idnumber of a matching Moodle user that
         //doesn't have an idnumber set yet
         $exists_params = array('username' => $this->username, 'mnethostid' => $CFG->mnet_localhost_id);
         if ($moodle_user = $this->_db->get_record('user', $exists_params)) {
             if (empty($moodle_user->idnumber)) {
                 //potentially old data, so set the idnumber
                 $moodle_user->idnumber = $this->idnumber;
                 $this->_db->update_record('user', $moodle_user);
                 $muserid = $moodle_user->id;
             } else {
                 if ($this->idnumber != $moodle_user->idnumber) {
                     //the username points to a pre-existing Moodle user
                     //with a non-matching idnumber, so something horrible
                     //happened
                     return;
                 }
             }
         }
         if ($createnew && !$muserid) {
             /// Add a new user
             $record = new stdClass();
             foreach ($mdlfieldmap as $pmfield => $mdlfield) {
                 if (isset($this->{$pmfield})) {
                     $record->{$mdlfield} = $this->{$pmfield};
                 }
             }
             $record->password = $this->password === null ? '' : $this->password;
             $record->confirmed = 1;
             $record->mnethostid = $CFG->mnet_localhost_id;
             $record->timemodified = time();
             $record->id = $this->_db->insert_record('user', $record);
         } else {
             if ($muserid) {
                 /// Update an existing user
                 $record = new stdClass();
                 $record->id = $muserid;
                 foreach ($mdlfieldmap as $pmfield => $mdlfield) {
                     if (isset($this->{$pmfield})) {
                         $record->{$mdlfield} = $this->{$pmfield};
                     }
                 }
                 if (!empty($this->password)) {
                     $record->password = $this->password;
                 }
                 $record->timemodified = time();
                 $this->_db->update_record('user', $record);
             } else {
                 return true;
             }
         }
         // avoid update loops
         if (isset($mu_loop_detect[$this->id])) {
             return $record->id;
         }
         $mu_loop_detect[$this->id] = true;
         // synchronize profile fields
         $origrec = clone $record;
         profile_load_data($origrec);
         fix_moodle_profile_fields($origrec);
         $fields = field::get_for_context_level(CONTEXT_ELIS_USER);
         $mfields = $this->_db->get_records('user_info_field', array(), '', 'shortname');
         $fields = $fields ? $fields : array();
         $changed = false;
         require_once elis::plugin_file('elisfields_moodleprofile', 'custom_fields.php');
         foreach ($fields as $field) {
             $field = new field($field);
             if (!moodle_profile_can_sync($field->shortname)) {
                 continue;
             }
             if (isset($field->owners['moodle_profile']) && $field->owners['moodle_profile']->exclude == pm_moodle_profile::sync_to_moodle && isset($mfields[$field->shortname])) {
                 $shortname = $field->shortname;
                 $fieldname = "field_{$shortname}";
                 $mfieldname = "profile_{$fieldname}";
                 $mfieldvalue = isset($origrec->{$mfieldname}) ? $origrec->{$mfieldname} : null;
                 if ($mfieldvalue != $this->{$fieldname}) {
                     $record->{$mfieldname} = $this->{$fieldname};
                     $changed = true;
                     sync_profile_field_settings_to_moodle($field);
                 }
             }
         }
         $record = uu_pre_process_custom_profile_data($record);
         profile_save_data($record);
         if ($muserid) {
             if ($changed) {
                 events_trigger('user_updated', $record);
             }
         } else {
             // if no user association record exists, create one
             $um = new usermoodle();
             $um->cuserid = $this->id;
             $um->muserid = $record->id;
             $um->idnumber = $this->idnumber;
             $um->save();
             events_trigger('user_created', $record);
         }
         unset($mu_loop_detect[$this->id]);
         return $record->id;
     }
 }
 /**
  * Test make_from_moodle_field function.
  *
  * @dataProvider dataprovider_make_from_moodle_field
  * @param object $mfielddata Data to create the initial moodle profile with.
  * @param array $expectedfielddata Expected data created for the field.
  * @param array $expectedmanualownerdata Expected field_owner data created.
  */
 public function test_make_from_moodle_field($mfielddata, $expectedfielddata, $expectedmanualownerdata)
 {
     require_once elis::file('eliscore/lib/data/customfield.class.php');
     require_once elis::file('eliscore/fields/moodleprofile/custom_fields.php');
     global $DB;
     $mfieldid = $DB->insert_record('user_info_field', $mfielddata);
     $fieldcat = new field_category();
     $fieldcat->name = 'Moodle Fields';
     $fieldcat->save();
     $efield = field::make_from_moodle_field($mfieldid, $fieldcat, pm_moodle_profile::sync_from_moodle);
     $efieldrec = $DB->get_record(field::TABLE, array('shortname' => $efield->shortname));
     unset($efieldrec->id);
     unset($efieldrec->sortorder);
     $expectedfielddata['categoryid'] = (string) $fieldcat->id;
     $this->assertEquals($expectedfielddata, (array) $efieldrec);
     $manualowner = $DB->get_record(field_owner::TABLE, array('fieldid' => $efield->id, 'plugin' => 'manual'));
     ksort($expectedmanualownerdata);
     $actualparams = unserialize($manualowner->params);
     ksort($actualparams);
     $this->assertEquals($expectedmanualownerdata, $actualparams);
     $this->assertTrue(moodle_profile_can_sync($efield->shortname));
     if ($mfielddata['defaultdata'] != '') {
         switch ($efield->datatype) {
             case 'bool':
                 $fielddatatype = 'int';
                 break;
             case 'datetime':
                 $fielddatatype = 'int';
                 break;
             case 'char':
                 $fielddatatype = 'char';
                 break;
             default:
                 $fielddatatype = 'text';
         }
         $fielddataclass = 'field_data_' . $fielddatatype;
         $defaultdata = $DB->get_record($fielddataclass::TABLE, array('fieldid' => $efield->id));
         $this->assertNotEmpty($defaultdata);
         $this->assertEquals($mfielddata['defaultdata'], $defaultdata->data);
     }
 }
Exemplo n.º 4
0
/**
 * sync_profile_field_from_moodle function synchronizes ELIS custom user field from corresponding Moodle field if possible.
 * also syncs relevant field settings
 *
 * @param  object $field  the field object to sync
 * @return mixed          void or true (may throw DB exceptions)
 * @uses   $DB
 */
function sync_profile_field_from_moodle($field)
{
    global $DB;
    if (!isset($field->owners['moodle_profile']) || $field->owners['moodle_profile']->exclude == pm_moodle_profile::sync_to_moodle) {
        // not owned by the Moodle plugin, or set to sync to Moodle
        return true;
    }
    // check if sync is possible with current field settings
    if (!moodle_profile_can_sync($field->shortname)) {
        return true;
    }
    // Sync field settings first, since they could prevent field data sync
    sync_profile_field_settings_from_moodle($field);
    $dest = $field->data_table();
    $src = 'user_info_data';
    $mfieldid = $DB->get_field('user_info_field', 'id', array('shortname' => $field->shortname));
    $joins = 'JOIN {' . user::TABLE . '} cu ON usr.idnumber = cu.idnumber
              JOIN {context} ctx ON ctx.instanceid = cu.id AND ctx.contextlevel = ' . CONTEXT_ELIS_USER . '
              JOIN {' . $src . '} src ON src.userid = usr.id AND src.fieldid = ' . $mfieldid;
    // insert field values that don't already exist
    $sql = 'INSERT INTO {' . $dest . '}
            (contextid, fieldid, data)
            SELECT ctx.id AS contextid, ' . $field->id . ' AS fieldid, src.data
              FROM {user} usr
                   ' . $joins . '
         LEFT JOIN {' . $dest . '} dest ON dest.contextid = ctx.id AND dest.fieldid = ?
             WHERE dest.id IS NULL';
    $DB->execute($sql, array($field->id));
    // update already-existing values
    $sql = 'UPDATE {' . $dest . '} dest
              JOIN {user} usr
                   ' . $joins . '
               SET dest.data = src.data
             WHERE dest.fieldid = ?
               AND dest.contextid = ctx.id';
    $DB->execute($sql, array($field->id));
}
Exemplo n.º 5
0
 /**
  * Validate that Moodle & ELIS custom user field settings are synced.
  * @param array $moodlefielddata Array of moodle field params
  * @param array $elisfielddata Array of elis field, context & owner params
  * @param bool  $cansync True if Moodle & ELIS field should be sync-able
  * @param array $moodlefieldexpect Array of expected Moodle field values
  * @param array $elisfieldexpect Array of expected ELIS field/owner values
  * @uses $DB
  * @dataProvider customfieldsync_dataprovider
  */
 public function test_custom_user_field_settings_synced($moodlefielddata, $elisfielddata, $cansync, $moodlefieldexpect, $elisfieldexpect)
 {
     global $DB;
     // Create Moodle profile field with data.
     if (!empty($moodlefielddata)) {
         $DB->insert_record('user_info_field', (object) $moodlefielddata);
     }
     // Create ELIS field, context & owner data.
     $field = $this->build_elis_field_data($elisfielddata);
     $this->assertTrue(moodle_profile_can_sync($field->shortname) == $cansync);
     sync_profile_field_settings_to_moodle($field);
     sync_profile_field_settings_from_moodle($field);
     if (!empty($moodlefieldexpect)) {
         $mdlfield = $DB->get_record('user_info_field', array('shortname' => $field->shortname));
         foreach ($moodlefieldexpect as $key => $val) {
             $this->assertEquals($val, $mdlfield->{$key});
         }
     }
     $this->assert_elis_field_data($field, $elisfieldexpect);
 }