/** * Delete a record, plus all associated custom fields. */ public function delete() { $this->_load_context(); $filter = new field_filter('contextid', $this->_context->id); field_data_int::delete_records($filter); field_data_num::delete_records($filter); field_data_char::delete_records($filter); field_data_text::delete_records($filter); parent::delete(); }
/** * 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)); }