Example #1
0
function cluster_profile_update_handler($userdata)
{
    global $DB, $CFG;
    if (!empty($userdata->deleted)) {
        return true;
    }
    // make sure a CM user exists
    pm_moodle_user_to_pm($userdata);
    if (!isset($userdata->id)) {
        return true;
    }
    $cuid = pm_get_crlmuserid($userdata->id);
    if (empty($cuid)) {
        // not a curriculum user -- (guest?)
        return true;
    }
    // the cluster assignments that the plugin wants to exist
    // we figure this out by counting the number of profile fields that the
    // user has that matches the values for the cluster, and comparing that
    // with the number of profile values set for the cluster
    $new_assignments = "(SELECT DISTINCT ? as userid, cp.clusterid\n                         FROM {" . userset_profile::TABLE . "} cp\n                         WHERE (SELECT COUNT(*)\n                                FROM {" . userset_profile::TABLE . "} cp1\n                                JOIN (SELECT i.fieldid, i.data FROM {user_info_data} i\n                                      WHERE i.userid = ?\n                                      UNION\n                                      SELECT uif.id as fieldid, uif.defaultdata as data\n                                      FROM {user_info_field} uif\n                                      LEFT JOIN {user_info_data} i ON i.userid = ? AND uif.id = i.fieldid\n                                      WHERE i.id IS NULL\n                                     ) inf ON inf.fieldid = cp1.fieldid AND inf.data = cp1.value\n                                WHERE cp.clusterid=cp1.clusterid)\n                               = (SELECT COUNT(*) FROM {" . userset_profile::TABLE . "} cp1 WHERE cp.clusterid = cp1.clusterid))";
    $new_assignments_params = array($cuid, $userdata->id, $userdata->id);
    // delete existing assignments that should not be there any more
    if ($CFG->dbfamily == 'postgres') {
        $delete = "DELETE FROM {" . clusterassignment::TABLE . "}\n                   WHERE id IN (\n                       SELECT id FROM {" . clusterassignment::TABLE . "} a\n                       LEFT OUTER JOIN {$new_assignments} b ON a.clusterid = b.clusterid AND a.userid = b.userid\n                       WHERE a.userid = ? AND b.clusterid IS NULL\n                   ) AND plugin='moodleprofile'";
    } else {
        $delete = "DELETE a FROM {" . clusterassignment::TABLE . "} a\n                   LEFT OUTER JOIN {$new_assignments} b ON a.clusterid = b.clusterid AND a.userid = b.userid\n                   WHERE a.userid = ? AND b.clusterid IS NULL AND a.plugin='moodleprofile'";
    }
    $DB->execute($delete, array_merge($new_assignments_params, array($cuid)));
    // add new assignments
    $insert = "INSERT INTO {" . clusterassignment::TABLE . "}\n               (clusterid, userid, plugin)\n               SELECT a.clusterid, a.userid, 'moodleprofile'\n               FROM {$new_assignments} a\n               LEFT OUTER JOIN {" . clusterassignment::TABLE . "} b ON a.clusterid = b.clusterid AND a.userid = b.userid AND b.plugin='moodleprofile'\n               WHERE a.userid = ? AND b.clusterid IS NULL";
    $DB->execute($insert, array_merge($new_assignments_params, array($cuid)));
    clusterassignment::update_enrolments($cuid);
    return true;
}
Example #2
0
 /**
  * Test sync-ing an ELIS User Profile field to a DELETED Moodle User Profile field
  */
 public function test_syncpmuserfieldtodeletedmoodleprofilefield()
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . '/user/profile/definelib.php';
     $this->load_csv_data();
     // Set PM Custom User field(s) to Sync to Moodle.
     $ctxlvl = CONTEXT_ELIS_USER;
     $fields = field::get_for_context_level($ctxlvl);
     foreach ($fields as $field) {
         $fieldobj = new field($field);
         if (!isset($fieldobj->owners['moodle_profile'])) {
             $fieldobj->owners['moodle_profile'] = new stdClass();
         }
         $owner = new field_owner($fieldobj->owners['moodle_profile']);
         $owner->exclude = pm_moodle_profile::sync_from_moodle;
         $owner->save();
         $fieldobj->save();
     }
     // Read a record.
     $src = new user(103, null, array(), false, array());
     $src->reset_custom_field_list();
     // Modify the data.
     $src->firstname = 'Testuser';
     $src->lastname = 'One';
     $src->field_sometext = 'boo';
     $src->field_sometextfrompm = 'bla';
     $src->save();
     // Delete some custom Moodle Profile field(s) to cause old error (pre ELIS-4499).
     $fields = field::get_for_context_level($ctxlvl);
     foreach ($fields as $field) {
         $fieldobj = new field($field);
         if ($moodlefield = $DB->get_record('user_info_field', array('shortname' => $fieldobj->shortname))) {
             profile_delete_field($moodlefield->id);
         }
     }
     // Run the library sync - throws errors not exceptions :(.
     $CFG->mnet_localhost_id = 1;
     // ???
     $mu = cm_get_moodleuser(103);
     try {
         $result = pm_moodle_user_to_pm($mu);
         $this->assertTrue($result);
     } catch (Exception $ex) {
         $this->assertTrue(false, $ex->message);
     }
 }
Example #3
0
 /**
  * Test pm_moodle_user_to_pm function w/o country specified
  */
 public function test_pm_moodle_user_to_pm_no_country()
 {
     global $DB;
     $fields = $this->set_up_custom_fields();
     $mu = $this->set_up_muser($fields['m'], false);
     // no country set
     $hascountry = isset($mu->country);
     $this->assertFalse($hascountry);
     $mu = $DB->get_record('user', array('id' => $mu->id));
     $result = pm_moodle_user_to_pm($mu);
     $this->assertTrue($result);
     // Get ELIS user.
     $cu = $DB->get_record('local_elisprogram_usr', array('username' => $mu->username));
     $this->assertNotEmpty($cu);
     $this->assertEquals($mu->idnumber, $cu->idnumber);
 }