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; }