/**
  * Validate that deleting a userset deletes all appropriate associations
  */
 public function test_delete_elis_userset_deletes_associations()
 {
     global $CFG, $DB;
     // Entities.
     require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php';
     require_once elispm::lib('data/userset.class.php');
     require_once elispm::lib('data/user.class.php');
     require_once elispm::lib('data/curriculum.class.php');
     require_once elispm::lib('data/track.class.php');
     require_once elis::lib('data/customfield.class.php');
     // Associations.
     require_once elispm::lib('data/clusterassignment.class.php');
     require_once elispm::lib('data/clustercurriculum.class.php');
     require_once elispm::lib('data/clustertrack.class.php');
     require_once elispm::file('enrol/userset/moodleprofile/userset_profile.class.php');
     // For context level access.
     require_once elispm::file('accesslib.php');
     $origfieldcount = $DB->count_records(field::TABLE);
     // Set up user set.
     $userset = new userset(array('name' => 'testusersetname'));
     $userset->save();
     // Set up other entities and associations.
     // Cluster enrolment.
     $user = new user(array('idnumber' => 'testuseridnumber', 'username' => 'testuserusername', 'firstname' => 'testuserfirstname', 'lastname' => 'testuserlastname', 'email' => '*****@*****.**', 'country' => 'CA'));
     $user->save();
     $clusterassignment = new clusterassignment(array('clusterid' => $userset->id, 'userid' => $user->id));
     $clusterassignment->save();
     // Cluster-curriculum assignment.
     $curriculum = new curriculum(array('idnumber' => 'testcurriculumidnumber'));
     $curriculum->save();
     $clustercurriculum = new clustercurriculum(array('clusterid' => $userset->id, 'curriculumid' => $curriculum->id));
     $clustercurriculum->save();
     // Cluster-track assignment.
     $track = new track(array('curid' => $curriculum->id, 'idnumber' => 'testtrackidnumber'));
     $track->save();
     $clustertrack = new clustertrack(array('clusterid' => $userset->id, 'trackid' => $track->id));
     $clustertrack->save();
     // Custom field.
     $field = new field(array('name' => 'testfieldname', 'categoryid' => 9999));
     $field->save();
     $context = \local_elisprogram\context\userset::instance($userset->id);
     $data = new field_data_int(array('contextid' => $context->id, 'fieldid' => $field->id, 'data' => 1));
     $data->save();
     // Cluster profile criteria.
     $clusterprofile = new userset_profile(array('clusterid' => $userset->id, 'fieldid' => $field->id, 'value' => 0));
     $clusterprofile->save();
     // Validate setup.
     $this->assertEquals(1, $DB->count_records(userset::TABLE));
     $this->assertEquals(1, $DB->count_records(user::TABLE));
     $this->assertEquals(1, $DB->count_records(clusterassignment::TABLE));
     $this->assertEquals(1, $DB->count_records(curriculum::TABLE));
     $this->assertEquals(1, $DB->count_records(clustercurriculum::TABLE));
     $this->assertEquals(1, $DB->count_records(track::TABLE));
     $this->assertEquals(1, $DB->count_records(clustertrack::TABLE));
     $this->assertEquals(1 + $origfieldcount, $DB->count_records(field::TABLE));
     $this->assertEquals(1, $DB->count_records(field_data_int::TABLE));
     $this->assertEquals(1, $DB->count_records(userset_profile::TABLE));
     // Run the import.
     $data = array('action' => 'delete');
     $this->run_core_userset_import($data, true);
     // Validation.
     $this->assertEquals(0, $DB->count_records(userset::TABLE));
     $this->assertEquals(1, $DB->count_records(user::TABLE));
     $this->assertEquals(0, $DB->count_records(clusterassignment::TABLE));
     $this->assertEquals(1, $DB->count_records(curriculum::TABLE));
     $this->assertEquals(0, $DB->count_records(clustercurriculum::TABLE));
     $this->assertEquals(1, $DB->count_records(track::TABLE));
     $this->assertEquals(0, $DB->count_records(clustertrack::TABLE));
     $this->assertEquals(1 + $origfieldcount, $DB->count_records(field::TABLE));
     $this->assertEquals(0, $DB->count_records(field_data_int::TABLE));
     $this->assertEquals(0, $DB->count_records(userset_profile::TABLE));
 }
 /**
  * Set up data that is needed for testing
  *
  * @param boolean $setcustomfielddata Specify whether the userset's custom fields should be set
  * @param boolean $assignuser Specify whether the user should be directly assigned to the user set
  * @param boolean $setautoassociatefields Specity whether we should set up fields that allow userset autoassociation
  */
 private function set_up_required_data($setcustomfielddata = true, $assignuser = true, $setautoassociatefields = false)
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php';
     require_once elis::lib('data/customfield.class.php');
     require_once elispm::file('accesslib.php');
     require_once elispm::file('enrol/userset/moodleprofile/userset_profile.class.php');
     require_once elispm::lib('data/clusterassignment.class.php');
     require_once elispm::lib('data/user.class.php');
     require_once elispm::lib('data/usermoodle.class.php');
     require_once elispm::lib('data/userset.class.php');
     $fieldcategoryid = $DB->get_field(field_category::TABLE, 'id', array('name' => 'Associated Group'));
     $this->assertNotEquals(false, $fieldcategoryid);
     $fieldcategory = new field_category($fieldcategoryid);
     $fieldcategory->load();
     // Set up the test user.
     $user = new user(array('idnumber' => 'testuseridnumber', 'username' => 'testuserusername', 'firstname' => 'testuserfirstname', 'lastname' => 'testuserlastname', 'email' => '*****@*****.**', 'country' => 'CA'));
     $user->save();
     $user->synchronize_moodle_user();
     // We need a system-level role assignment.
     $roleid = create_role('systemrole', 'systemrole', 'systemrole');
     $userid = $DB->get_field('user', 'id', array('username' => 'testuserusername'));
     $context = context_system::instance();
     role_assign($roleid, $userid, $context->id);
     // Set up the userset.
     $userset = new userset();
     $usersetdata = array('name' => 'testusersetname');
     if ($setcustomfielddata) {
         $usersetdata['field_userset_group'] = 1;
         $usersetdata['field_userset_groupings'] = 1;
     }
     $userset->set_from_data((object) $usersetdata);
     $userset->save();
     if ($setautoassociatefields) {
         // Set up a file we can use to auto-associate users to a userset.
         $field = new field(array('categoryid' => $fieldcategory->id, 'shortname' => 'autoassociate', 'name' => 'autoassociate', 'datatype' => 'bool'));
         $field->save();
         // Ensure manual field owner exists for syncing.
         field_owner::ensure_field_owner_exists($field, 'manual');
         $ownerid = $DB->get_field(field_owner::TABLE, 'id', array('fieldid' => $field->id, 'plugin' => 'manual'));
         $owner = new field_owner($ownerid);
         $owner->param_control = 'checkbox';
         $owner->save();
         field_owner::ensure_field_owner_exists($field, 'moodle_profile');
         $DB->execute("UPDATE {" . field_owner::TABLE . "} SET exclude = ?", array(pm_moodle_profile::sync_to_moodle));
         $fieldcontextlevel = new field_contextlevel(array('fieldid' => $field->id, 'contextlevel' => CONTEXT_ELIS_USER));
         $fieldcontextlevel->save();
         // The associated Moodle user profile field.
         require_once $CFG->dirroot . '/user/profile/definelib.php';
         require_once $CFG->dirroot . '/user/profile/field/checkbox/define.class.php';
         $profiledefinecheckbox = new profile_define_checkbox();
         $data = new stdClass();
         $data->datatype = 'checkbox';
         $data->categoryid = 99999;
         $data->shortname = 'autoassociate';
         $data->name = 'autoassociate';
         $profiledefinecheckbox->define_save($data);
         $mfield = $DB->get_record('user_info_field', array('shortname' => 'autoassociate'));
         // The "cluster-profile" association.
         $usersetprofile = new userset_profile(array('clusterid' => $userset->id, 'fieldid' => $mfield->id, 'value' => 1));
         $usersetprofile->save();
     }
     if ($assignuser) {
         // Assign the user to the user set.
         $clusterassignment = new clusterassignment(array('clusterid' => $userset->id, 'userid' => $user->id, 'plugin' => 'manual'));
         $clusterassignment->save();
     }
 }
Beispiel #3
0
function userset_moodleprofile_update($cluster)
{
    global $CFG, $DB;
    // get the "old" (existing) profile field assignment values
    $old = userset_profile::find(new field_filter('clusterid', $cluster->id), array(), 0, 2)->to_array();
    // get the "new" (submitted) profile field assignment values
    $new = array();
    for ($i = 1; $i <= 2; $i++) {
        $newfield = optional_param("profile_field{$i}", 0, PARAM_INT);
        if ($newfield) {
            /// convert checkbox values from 'on' and '' to 1 and 0
            $new[$newfield] = optional_param("profile_value{$i}", '', PARAM_TEXT);
            if ('checkbox' == $DB->get_field('user_info_field', 'datatype', array('id' => $newfield))) {
                $new[$newfield] = empty($new[$newfield]) ? 0 : 1;
            }
        }
    }
    $updated = false;
    // Have we updated the profile field associated with this cluster?
    // Compare old values against new values
    foreach ($old as $field) {
        if (!isset($new[$field->id])) {
            // old field is no longer a field
            $field->delete();
            unset($old[$field->id]);
            $updated = true;
        } else {
            if ($new[$field->id] != $field->value) {
                // value has changed
                $field->value = $new[$field->id];
                $field->save();
                $updated = true;
            }
        }
    }
    // check for added fields
    $added = array_diff_key($new, $old);
    foreach ($added as $fieldid => $value) {
        $record = new userset_profile();
        $record->clusterid = $cluster->id;
        $record->fieldid = $fieldid;
        $record->value = $value;
        $record->save();
        $updated = true;
    }
    if ($updated) {
        // re-assign users:
        // remove previous cluster assignments
        clusterassignment::delete_records(array(new field_filter('clusterid', $cluster->id), new field_filter('plugin', 'moodleprofile')));
        // create new cluster assignments
        $join = '';
        $join_params = array();
        $where_clauses = array();
        $where_params = array();
        $i = 1;
        foreach ($new as $fieldid => $value) {
            // check if the desired field value is equal to the field's default
            // value -- if so, we need to include users that don't have an
            // associated entry in user_info_data
            $defaultvalue = $DB->get_field('user_info_field', 'defaultdata', array('id' => $fieldid));
            $isdefault = $value == $defaultvalue;
            $join .= ($isdefault ? ' LEFT' : ' INNER') . " JOIN {user_info_data} inf{$i} ON mu.id = inf{$i}.userid AND inf{$i}.fieldid = ?";
            $join_params[] = $fieldid;
            $where = "(inf{$i}.data = ?";
            // if desired field is the default
            if ($isdefault) {
                $where .= " OR inf{$i}.userid IS NULL";
            }
            $where .= ')';
            $where_clauses[] = $where;
            $where_params[] = $value;
            $i++;
        }
        //use the clauses to construct a where condition
        $where_clause = implode(' AND ', $where_clauses);
        if (!empty($join) && !empty($where)) {
            $sql = "INSERT INTO {" . clusterassignment::TABLE . "}\n                    (clusterid, userid, plugin)\n                    SELECT ?, cu.id, 'moodleprofile'\n                    FROM {" . user::TABLE . "} cu\n                    INNER JOIN {user} mu ON mu.idnumber = cu.idnumber\n                    {$join}\n                    WHERE {$where_clause}";
            $params = array_merge(array($cluster->id), $join_params, $where_params);
            $DB->execute($sql, $params);
        }
    }
    clusterassignment::update_enrolments(0, $cluster->id);
}
 /**
  * Set up necessary data
  *
  * @param int $numfields The number of custom fields used in auto-association
  */
 private function init_required_data($numfields = 1)
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php';
     require_once elis::file('eliscore/fields/moodleprofile/custom_fields.php');
     require_once elis::lib('data/customfield.class.php');
     require_once elispm::file('accesslib.php');
     require_once elispm::lib('data/userset.class.php');
     require_once $CFG->dirroot . '/user/profile/definelib.php';
     require_once $CFG->dirroot . '/user/profile/field/checkbox/define.class.php';
     // Set up the category only once.
     $fieldcategory = new field_category(array('name' => 'testcategoryname'));
     $fieldcategory->save();
     // Ste up the target userset only once.
     $userset = new userset(array('name' => 'testusersetname'));
     $userset->save();
     for ($i = 1; $i <= $numfields; $i++) {
         // Custom field.
         $field = new field(array('categoryid' => $fieldcategory->id, 'shortname' => 'testfieldshortname' . $i, 'name' => 'testfieldname' . $i, 'datatype' => 'bool'));
         $field->save();
         // Ensure manual field owner exists for syncing.
         field_owner::ensure_field_owner_exists($field, 'manual');
         $ownerid = $DB->get_field(field_owner::TABLE, 'id', array('fieldid' => $field->id, 'plugin' => 'manual'));
         $owner = new field_owner($ownerid);
         $owner->param_control = 'checkbox';
         $owner->save();
         // Ensure moodle profile field owner exists.
         field_owner::ensure_field_owner_exists($field, 'moodle_profile');
         $DB->execute("UPDATE {" . field_owner::TABLE . "} SET exclude = ?", array(pm_moodle_profile::sync_to_moodle));
         // Field context level assocation.
         $fieldcontextlevel = new field_contextlevel(array('fieldid' => $field->id, 'contextlevel' => CONTEXT_ELIS_USER));
         $fieldcontextlevel->save();
         // The associated Moodle user profile field.
         $profiledefinecheckbox = new profile_define_checkbox();
         $data = new stdClass();
         $data->datatype = 'checkbox';
         $data->categoryid = 99999;
         $data->shortname = 'testfieldshortname' . $i;
         $data->name = 'testfieldname' . $i;
         $profiledefinecheckbox->define_save($data);
         $mfield = $DB->get_record('user_info_field', array('shortname' => 'testfieldshortname' . $i));
         // The "cluster-profile" association.
         $usersetprofile = new userset_profile(array('clusterid' => $userset->id, 'fieldid' => $mfield->id, 'value' => 1));
         $usersetprofile->save();
     }
 }
 /**
  * Set up data that is needed for testing
  */
 private function set_up_required_data($assignusertouserset = true, $assigncoursetoclass = true, $assigntracktoclass = true, $initclusterprofile = false, $initusersetfielddata = true)
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php';
     require_once $CFG->dirroot . '/course/lib.php';
     require_once $CFG->dirroot . '/lib/enrollib.php';
     require_once elis::lib('data/customfield.class.php');
     require_once elispm::file('accesslib.php');
     require_once elispm::lib('data/classmoodlecourse.class.php');
     require_once elispm::lib('data/clusterassignment.class.php');
     require_once elispm::lib('data/clustertrack.class.php');
     require_once elispm::lib('data/course.class.php');
     require_once elispm::lib('data/curriculum.class.php');
     require_once elispm::lib('data/pmclass.class.php');
     require_once elispm::lib('data/track.class.php');
     require_once elispm::lib('data/user.class.php');
     require_once elispm::lib('data/userset.class.php');
     require_once elispm::lib('data/usertrack.class.php');
     $fieldcategoryid = $DB->get_field(field_category::TABLE, 'id', array('name' => 'Associated Group'));
     $this->assertNotEquals(false, $fieldcategoryid);
     $fieldcategory = new field_category($fieldcategoryid);
     $fieldcategory->load();
     // Set up the test user.
     $user = new user(array('idnumber' => 'testuseridnumber', 'username' => 'testuserusername', 'firstname' => 'testuserfirstname', 'lastname' => 'testuserlastname', 'email' => '*****@*****.**', 'country' => 'CA'));
     $user->save();
     // Set up the test course description and class instance.
     $course = new course(array('name' => 'testcoursename', 'idnumber' => 'testcourseidnumber', 'syllabus' => ''));
     $course->save();
     $pmclass = new pmclass(array('courseid' => $course->id, 'idnumber' => 'testclassidnumber'));
     $pmclass->save();
     $category = new stdClass();
     $category->name = 'testcategoryname';
     $category->id = $DB->insert_record('course_categories', $category);
     // Set up the test Moodle course.
     set_config('enrol_plugins_enabled', 'manual');
     set_config('defaultenrol', 1, 'enrol_manual');
     set_config('status', ENROL_INSTANCE_ENABLED, 'enrol_manual');
     $course = new stdClass();
     $course->category = $category->id;
     $course->shortname = 'testcourseshortname';
     $course->fullname = 'testcoursefullname';
     $course = create_course($course);
     if ($assigncoursetoclass) {
         // Assign the Moodle course to a class instance.
         $classmoodlecourse = new classmoodlecourse(array('classid' => $pmclass->id, 'moodlecourseid' => $course->id));
         $classmoodlecourse->save();
     }
     // Set up the test program and track.
     $curriculum = new curriculum(array('idnumber' => 'testcurriculumidnumber'));
     $curriculum->save();
     $track = new track(array('curid' => $curriculum->id, 'idnumber' => 'testtrackidnumber'));
     $track->save();
     if ($assigntracktoclass) {
         // Assign the test track to the test class instance.
         $trackassignment = new trackassignment(array('trackid' => $track->id, 'classid' => $pmclass->id, 'autoenrol' => 1));
         $trackassignment->save();
     }
     // Set up the test userset.
     $userset = new userset();
     $usersetdata = array('name' => 'testusersetname');
     if ($initusersetfielddata) {
         $usersetdata['field_userset_group'] = 1;
         $usersetdata['field_userset_groupings'] = 1;
     }
     $userset->set_from_data((object) $usersetdata);
     $userset->save();
     // Assign the test user to the test track.
     $usertrack = new usertrack(array('userid' => $user->id, 'trackid' => $track->id));
     $usertrack->save();
     $clustertrack = new clustertrack(array('clusterid' => $userset->id, 'trackid' => $track->id));
     $clustertrack->save();
     if ($assignusertouserset) {
         // Assign the test user to the test userset.
         $clusterassignment = new clusterassignment(array('userid' => $user->id, 'clusterid' => $userset->id, 'plugin' => 'manual'));
         $clusterassignment->save();
     }
     if ($initclusterprofile) {
         // Set up a file we can use to auto-associate users to a userset.
         $field = new field(array('categoryid' => $fieldcategory->id, 'shortname' => 'autoassociate', 'name' => 'autoassociate', 'datatype' => 'bool'));
         $field->save();
         // Ensure manual field owner exists for syncing.
         field_owner::ensure_field_owner_exists($field, 'manual');
         $ownerid = $DB->get_field(field_owner::TABLE, 'id', array('fieldid' => $field->id, 'plugin' => 'manual'));
         $owner = new field_owner($ownerid);
         $owner->param_control = 'checkbox';
         $owner->save();
         field_owner::ensure_field_owner_exists($field, 'moodle_profile');
         $DB->execute("UPDATE {" . field_owner::TABLE . "} SET exclude = ?", array(pm_moodle_profile::sync_to_moodle));
         $fieldcontextlevel = new field_contextlevel(array('fieldid' => $field->id, 'contextlevel' => CONTEXT_ELIS_USER));
         $fieldcontextlevel->save();
         // The associated Moodle user profile field.
         require_once $CFG->dirroot . '/user/profile/definelib.php';
         require_once $CFG->dirroot . '/user/profile/field/checkbox/define.class.php';
         $profiledefinecheckbox = new profile_define_checkbox();
         $data = new stdClass();
         $data->datatype = 'checkbox';
         $data->categoryid = 99999;
         $data->shortname = 'autoassociate';
         $data->name = 'autoassociate';
         $profiledefinecheckbox->define_save($data);
         $mfield = $DB->get_record('user_info_field', array('shortname' => 'autoassociate'));
         // The "cluster-profile" association.
         $usersetprofile = new userset_profile(array('clusterid' => $userset->id, 'fieldid' => $mfield->id, 'value' => true));
         $usersetprofile->save();
     }
     // Enrol the user in the Moodle course.
     $mdluserid = $DB->get_field('user', 'id', array('username' => 'testuserusername'));
     $roleid = create_role('testrole', 'testrole', 'testrole');
     enrol_try_internal_enrol($course->id, $mdluserid, $roleid);
     // Set up the necessary config data.
     set_config('userset_groups', 1, 'elisprogram_usetgroups');
     set_config('siteguest', '');
     // Validate setup.
     $this->assertEquals(0, $DB->count_records('groups'));
 }