/**
* Cleans up all the groups and groupings in a course (it does this best-effort
* i.e. if one deletion fails, it still attempts further deletions).
* IMPORTANT: Note that if the groups and groupings are used by other courses
* somehow, they will still be deleted - it doesn't protect against this.
* @param int $courseid The id of the course
* @return boolean True if the clean up was successful, false otherwise.
*/
function groups_cleanup_groups($courseid)
{
$success = true;
// Delete all the groupings
$groupings = groups_get_groupings($courseid);
if ($groupings != false) {
foreach ($groupings as $groupingid) {
$groupingdeleted = groups_delete_grouping($groupingid);
if (!$groupingdeleted) {
$success = false;
}
}
}
// Delete all the groups
$groupids = groups_get_groups($courseid);
if ($groupids != false) {
foreach ($groupids as $groupid) {
$groupdeleted = groups_delete_group($groupid);
if (!$groupdeleted) {
$success = false;
}
}
}
return $success;
}
/**
* Returns an array of group objects that the user is a member of
* in the given course. If userid isn't specified, then return a
* list of all groups in the course.
*
* @uses $CFG
* @param int $courseid The id of the course in question.
* @param int $userid The id of the user in question as found in the 'user'
* table 'id' field.
* @return object
*/
function get_groups($courseid, $userid = 0)
{
if ($userid) {
$groupids = groups_get_groups_for_user($userid, $courseid);
} else {
$groupids = groups_get_groups($courseid);
}
return groups_groupids_to_groups($groupids, $courseid, $alldata = true);
}
/**
* Builds the select box to choose the group to attach the BuddyDrive Item to
* @param int $user_id the user id
* @param int $selected the group id in case of edit form
* @param string $name the name of the select box
* @uses bp_loggedin_user_id() to get current user id
* @uses groups_get_groups() to list the groups of the user
* @uses groups_get_groupmeta() to check group enabled BuddyDrive
* @uses selected() to eventually activate a group
* @return string the select box
*/
function buddydrive_get_select_user_group($user_id = false, $selected = false, $name = false)
{
if (empty($user_id)) {
$user_id = bp_loggedin_user_id();
}
$name = !empty($name) ? ' name="' . $name . '"' : false;
$output = __('No group available for BuddyDrive', 'buddydrive');
if (!bp_is_active('groups')) {
return $output;
}
$user_groups = groups_get_groups(array('user_id' => $user_id, 'show_hidden' => true, 'per_page' => false));
$buddydrive_groups = false;
// checking for available buddydrive groups
if (!empty($user_groups['groups'])) {
foreach ($user_groups['groups'] as $group) {
if (1 == groups_get_groupmeta($group->id, '_buddydrive_enabled')) {
$buddydrive_groups[] = array('group_id' => $group->id, 'group_name' => $group->name);
}
}
}
// building the select box
if (!empty($buddydrive_groups) && is_array($buddydrive_groups)) {
$output = '<select id="buddygroup"' . $name . '>';
foreach ($buddydrive_groups as $buddydrive_group) {
$output .= '<option value="' . $buddydrive_group['group_id'] . '" ' . selected($selected, $buddydrive_group['group_id'], false) . '>' . $buddydrive_group['group_name'] . '</option>';
}
$output .= '</select>';
}
return apply_filters('buddydrive_get_select_user_group', $output);
}
/**
* This function will empty a course of USER data as much as
/// possible. It will retain the activities and the structure
/// of the course.
*
* @uses $USER
* @uses $SESSION
* @uses $CFG
* @param object $data an object containing all the boolean settings and courseid
* @param bool $showfeedback if false then do it all silently
* @return bool
* @todo Finish documenting this function
*/
function reset_course_userdata($data, $showfeedback = true)
{
global $CFG, $USER, $SESSION;
$result = true;
$strdeleted = get_string('deleted');
// Look in every instance of every module for data to delete
if ($allmods = get_records('modules')) {
foreach ($allmods as $mod) {
$modname = $mod->name;
$modfile = $CFG->dirroot . '/mod/' . $modname . '/lib.php';
$moddeleteuserdata = $modname . '_delete_userdata';
// Function to delete user data
if (file_exists($modfile)) {
@(include_once $modfile);
if (function_exists($moddeleteuserdata)) {
$moddeleteuserdata($data, $showfeedback);
}
}
}
} else {
error('No modules are installed!');
}
// Delete other stuff
$coursecontext = get_context_instance(CONTEXT_COURSE, $data->courseid);
if (!empty($data->reset_students) or !empty($data->reset_teachers)) {
$teachers = array_keys(get_users_by_capability($coursecontext, 'moodle/course:update'));
$participants = array_keys(get_users_by_capability($coursecontext, 'moodle/course:view'));
$students = array_diff($participants, $teachers);
if (!empty($data->reset_students)) {
foreach ($students as $studentid) {
role_unassign(0, $studentid, 0, $coursecontext->id);
}
if ($showfeedback) {
notify($strdeleted . ' ' . get_string('students'), 'notifysuccess');
}
/// Delete group members (but keep the groups) TODO:check.
if ($groupids = groups_get_groups($data->courseid)) {
foreach ($groupids as $groupid) {
if (groups_remove_all_group_members($groupid)) {
if ($showfeedback) {
notify($strdeleted . ' groups_members', 'notifysuccess');
}
} else {
$result = false;
}
}
}
}
if (!empty($data->reset_teachers)) {
foreach ($teachers as $teacherid) {
role_unassign(0, $teacherid, 0, $coursecontext->id);
}
if ($showfeedback) {
notify($strdeleted . ' ' . get_string('teachers'), 'notifysuccess');
}
}
}
if (!empty($data->reset_groups)) {
if ($groupids = groups_get_groups($data->courseid)) {
foreach ($groupids as $groupid) {
if (groups_delete_group($groupid)) {
if ($showfeedback) {
notify($strdeleted . ' groups', 'notifysuccess');
}
} else {
$result = false;
}
}
}
}
if (!empty($data->reset_events)) {
if (delete_records('event', 'courseid', $data->courseid)) {
if ($showfeedback) {
notify($strdeleted . ' event', 'notifysuccess');
}
} else {
$result = false;
}
}
if (!empty($data->reset_logs)) {
if (delete_records('log', 'course', $data->courseid)) {
if ($showfeedback) {
notify($strdeleted . ' log', 'notifysuccess');
}
} else {
$result = false;
}
//.........这里部分代码省略.........
/**
* Process the repair tool
*
* In case something went wrong with group activities (visibility, component...),
* this tool should repair the problematic activities.
*
* @package WP Idea Stream
* @subpackage buddypress/activity
*
* @since 2.0.0
*
* @global $wpdb
* @uses bp_is_active() to check if a component is active
* @uses wp_list_pluck() to pluck a certain field out of each object in a list.
* @uses bp_activity_get() to get all ideastream activities
* @uses wp_filter_object_list() to filter a list of objects, based on a set of key => value arguments.
* @uses wp_parse_id_list() to sanitize a list of ids
* @uses get_post_meta() to get the group id the idea is attached to
* @uses groups_get_groups() to get the needed groups
* @uses WP_Idea_Stream_Activity::bulk_edit_activity() to update the activities (component/item_id/visibility)
* @return array the result of the repair operation.
*/
public function repair_activities()
{
global $wpdb;
$buddypress = buddypress();
$blog_id = get_current_blog_id();
// Description of this tool, displayed to the user
$statement = __('Making sure IdeaStream activities are consistent: %s', 'wp-idea-stream');
// Default to failure text
$result = __('No activity needs to be repaired.', 'wp-idea-stream');
// Default to unrepaired
$repair = 0;
if (!bp_is_active('groups')) {
return;
}
$ideastream_types = wp_list_pluck($this->activity_actions, 'type');
// Get all activities
$ideastream_activities = bp_activity_get(array('filter' => array('action' => $ideastream_types), 'show_hidden' => true, 'spam' => 'all', 'per_page' => false));
if (is_array($ideastream_activities['activities'])) {
$idea_comments = array();
$idea_posts = array();
$to_repair = array();
$attached_component = array('comment' => array($buddypress->groups->id => array(), $buddypress->blogs->id => array()), 'post' => array($buddypress->groups->id => array(), $buddypress->blogs->id => array()));
foreach ($ideastream_activities['activities'] as $activity) {
if (false !== strpos($activity->type, 'comment')) {
$idea_comments[$activity->id] = $activity->secondary_item_id;
$attached_component['comment'][$activity->component][] = $activity->id;
} else {
$idea_posts[$activity->id] = $activity->secondary_item_id;
$attached_component['post'][$activity->component][] = $activity->id;
}
}
// Gets the comment activities to repair
if (!empty($idea_comments)) {
// I don't think get_comments() allow us to get comments
// using a list of comment ids..
$in = implode(',', wp_parse_id_list($idea_comments));
$sql = array('select' => "SELECT c.comment_ID, m.meta_value as group_id", 'from' => "FROM {$wpdb->comments} c LEFT JOIN {$wpdb->postmeta} m", 'on' => "ON (c.comment_post_ID = m.post_id )", 'where' => $wpdb->prepare("WHERE comment_ID IN ({$in}) AND m.meta_key = %s", '_ideastream_group_id'));
$idea_comments_check = $wpdb->get_results(join(' ', $sql), OBJECT_K);
foreach ($idea_comments as $activity_comment_id => $comment_secondary_id) {
if (!empty($idea_comments_check[$comment_secondary_id]) && !in_array($activity_comment_id, $attached_component['comment'][$buddypress->groups->id])) {
$to_repair['groups'][$idea_comments_check[$comment_secondary_id]->group_id][] = $activity_comment_id;
} else {
if (empty($idea_comments_check[$comment_secondary_id]) && in_array($activity_comment_id, $attached_component['comment'][$buddypress->groups->id])) {
$to_repair['blogs'][] = $activity_comment_id;
}
}
}
}
// Gets the idea activities to repair
if (!empty($idea_posts)) {
add_filter('wp_idea_stream_ideas_get_status', 'wp_idea_stream_ideas_get_all_status', 10, 1);
// Get user's ideas posted in the group
$idea_posts_check = wp_idea_stream_ideas_get_ideas(array('per_page' => -1, 'include' => $idea_posts, 'meta_query' => array(array('key' => '_ideastream_group_id', 'compare' => 'EXIST'))));
$idea_posts_check = wp_list_pluck($idea_posts_check['ideas'], 'ID');
remove_filter('wp_idea_stream_ideas_get_status', 'wp_idea_stream_ideas_get_all_status', 10, 1);
foreach ($idea_posts as $activity_post_id => $post_secondary_id) {
if (in_array($post_secondary_id, $idea_posts_check) && !in_array($activity_post_id, $attached_component['post'][$buddypress->groups->id])) {
$group_id = get_post_meta($post_secondary_id, '_ideastream_group_id', true);
if (!empty($group_id)) {
$to_repair['groups'][$group_id][] = $activity_post_id;
}
} else {
if (!in_array($post_secondary_id, $idea_posts_check) && in_array($activity_post_id, $attached_component['post'][$buddypress->groups->id])) {
$to_repair['blogs'][] = $activity_post_id;
}
}
}
}
}
if (!empty($to_repair['groups'])) {
// Get the groups to have their status
$groups = groups_get_groups(array('show_hidden' => true, 'populate_extras' => false, 'include' => join(',', array_keys($to_repair['groups'])), 'per_page' => false));
if (!empty($groups['groups'])) {
$public_groups = wp_filter_object_list($groups['groups'], array('status' => 'public'), 'and', 'id');
foreach ($to_repair['groups'] as $item_id => $repair_activities) {
$hide_sitewide = 0;
if (!in_array($item_id, $public_groups)) {
$hide_sitewide = 1;
//.........这里部分代码省略.........
function format_value($value, $post_id, $field)
{
// bail early if no value
if (empty($value)) {
return $value;
}
// force value to array
$value = acf_get_array($value);
// convert values to int
$value = array_map('intval', $value);
// load groups if needed
if ($field['return_format'] == 'object') {
// get groups
$value = groups_get_groups(array('include' => $value));
}
// convert back from array if neccessary
if (!$field['multiple']) {
$value = array_shift($value);
}
// return
return $value;
}
请发表评论