/** * Test that you can delete and promote user subsets */ public function test_deletingrecordcanpromoteusersubsets() { $this->load_csv_data(); accesslib_clear_all_caches(true); // Make sure all the contexts are created, so that we can find the children. $contextclass = \local_eliscore\context\helper::get_class_for_level(CONTEXT_ELIS_USERSET); for ($i = 1; $i <= 4; $i++) { $clustercontextinstance = $contextclass::instance($i); } // Delete a record. $src = new userset(2, null, array(), false, array()); $src->deletesubs = false; $src->delete(); // Read it back. $result = new moodle_recordset_phpunit_datatable(userset::TABLE, userset::find(null, array(), 0, 0)); $dataset = new PHPUnit_Extensions_Database_DataSet_CsvDataSet(); $dataset->addTable(userset::TABLE, elispm::file('tests/fixtures/userset_promote_test_result.csv')); $this->assertTablesEqual($dataset->getTable(userset::TABLE), $result); }
/** * Perform the necessary actions required to "delete" a cluster from the system. * * @param none * @return bool True on success, False otherwise. */ function delete() { require_once elis::lib('data/data_filter.class.php'); if ($this->deletesimple) { //core delete method, not including recursion (entered once for each //individual userset being delete) //clean make the delete cascade into association records $filter = new field_filter('clusterid', $this->id); clustercurriculum::delete_records($filter, $this->_db); clustertrack::delete_records($filter, $this->_db); clusterassignment::delete_records($filter, $this->_db); //cluster plugin cleanup $plugins = get_plugin_list(self::ENROL_PLUGIN_TYPE); foreach ($plugins as $plugin => $plugindir) { require_once elis::plugin_file(self::ENROL_PLUGIN_TYPE . '_' . $plugin, 'lib.php'); call_user_func('cluster_' . $plugin . '_delete_for_cluster', $this->id); } //delete the userset record parent::delete(); //delete this cluster's context //get a new context instance, $contextclass = \local_eliscore\context\helper::get_class_for_level(CONTEXT_ELIS_USERSET); $userset_context = $contextclass::instance($this->id); $userset_context->delete(); events_trigger('cluster_deleted', $this->id); return; } $result = true; $children = array(); $delete_ids = array(); $promote_ids = array(); /// Figure out all the sub-clusters $cluster_context_instance = \local_elisprogram\context\userset::instance($this->id); $instance_id = $cluster_context_instance->id; $instance_path = $cluster_context_instance->path; $children = userset::find(new join_filter('id', 'context', 'instanceid', new AND_filter(array(new field_filter('path', "{$instance_path}/%", field_filter::LIKE), new field_filter('contextlevel', CONTEXT_ELIS_USERSET)))), array('depth' => 'ASC'), 0, 0, $this->_db); $children = $children->to_array(); if ($this->deletesubs) { $todelete = $children; $todelete[] = $this; // The specified cluster always gets deleted } else { $todelete = array($this); } foreach ($todelete as $userset) { //delete without recursion $userset->deletesimple = true; $userset->delete(); } if (!$this->deletesubs && !empty($children)) { foreach ($children as $child) { $lower_depth = $child->depth - 1; if (userset::exists(new field_filter('id', $child->parent))) { /// A parent found so lets lower the depth $child->depth = 0; } else { /// Parent not found so this cluster will be top-level $child->parent = 0; $child->depth = 1; } $child->save(); $sql = "UPDATE {context}\n SET depth=0, path=NULL\n WHERE contextlevel=? AND instanceid=?"; $this->_db->execute($sql, array(CONTEXT_ELIS_USERSET, $child->id)); } \local_eliscore\context\helper::build_all_paths(false, array(CONTEXT_ELIS_USERSET)); // Re-build the context table for all sub-clusters } return $result; }