function cluster_manual_deassign_user($clusterid, $userid) { global $CURMAN, $CFG; if (!is_numeric($clusterid) || !is_numeric($userid) || $clusterid <= 0 || $userid <= 0) { return false; } clusterassignment::delete_records(array(new field_filter('userid', $userid), new field_filter('clusterid', $clusterid), new field_filter('plugin', 'manual'))); return true; }
public function delete() { global $CFG; $muser = $this->get_moodleuser(); if (empty($muser) || !is_primary_admin($muser->id)) { // delete associated data require_once elis::lib('data/data_filter.class.php'); $filter = new field_filter('userid', $this->id); curriculumstudent::delete_records($filter, $this->_db); student::delete_records($filter, $this->_db); student_grade::delete_records($filter, $this->_db); waitlist::delete_records($filter, $this->_db); instructor::delete_records($filter, $this->_db); usertrack::delete_records($filter, $this->_db); clusterassignment::delete_records($filter, $this->_db); //delete association to Moodle user, if applicable require_once elispm::lib('data/usermoodle.class.php'); $filter = new field_filter('cuserid', $this->id); usermoodle::delete_records($filter, $this->_db); // Delete Moodle user. if (!empty($muser)) { delete_user($muser); } parent::delete(); $context = \local_elisprogram\context\user::instance($this->id); $context->delete(); } }
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); }
/** * 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; }