/** * Update group and membership info in underlying 'course-' group (group type 4) * * Method is called anytime after a course is saved * * @param $course - course object */ public function onAfterStoreCourse($course) { // Get a new group object $group = new \Hubzero\User\Group(); // If the course doesn't have a group id set, then we need to create a new group if (!$course->get('group_id')) { // Set some group info $group->set('cn', 'course-' . $course->cn); $group->create(); $group->set('type', 4); // group type 4 = course // Set the new group gidNumber as the group_id in the course and update $course->set('group_id', $group->get('gidNumber')); $course->update(); } else { $group->read($course->get('group_id')); } // Set the group description (in case it's been changed) $group->set('description', $course->get('description')); // Get all of the course members that are not yet group members (i.e. they need to be added to the group) $add = array_diff($course->get('members'), $group->get('members')); foreach ($add as $a) { $group->add('members', $a); } // Get all of the group members that are not members of the course (i.e. they need to be removed from the group) $remove = array_diff($group->get('members'), $course->get('members')); foreach ($remove as $r) { $group->remove('members', $r); } // Finally, update the group $group->update(); }
/** * Create a group * * @apiMethod POST * @apiUri /groups * @apiParameter { * "name": "cn", * "description": "Group alias that appears in the url for group. Only lowercase alphanumeric chars allowed.", * "type": "string", * "required": true, * "default": null * } * @apiParameter { * "name": "title", * "description": "Group title", * "type": "string", * "required": true, * "default": null * } * @apiParameter { * "name": "tags", * "description": "Group tags", * "type": "string (comma separated)", * "required": false, * "default": null * } * @apiParameter { * "name": "public_description", * "description": "Group public description", * "type": "string", * "required": false, * "default": null * } * @apiParameter { * "name": "private_description", * "description": "Group private description", * "type": "string", * "required": false, * "default": null * } * @apiParameter { * "name": "join_policy", * "description": "Membership join policy", * "type": "string", * "required": true, * "default": "open", * "allowedValues": "open, restricted, invite_only, closed" * } * @apiParameter { * "name": "discoverability", * "description": "Is the group shown in hub searches/listings.", * "type": "string", * "required": true, * "default": "visible", * "allowedValues": "visible, hidden" * } * @return void */ public function createTask() { $this->requiresAuthentication(); $cn = Request::getWord('cn', ''); $title = Request::getVar('title', ''); $tags = Request::getVar('tags', ''); $publicDesc = Request::getVar('public_description', ''); $privateDesc = Request::getVar('private_description', ''); $joinPolicy = strtolower(Request::getWord('join_policy', 'open')); $discoverability = Request::getWord('discoverability', 'visible'); // var to hold errors $errors = array(); // check for required fields (cn & title) if ($cn == '') { $errors[] = array('field' => 'cn', 'message' => Lang::txt('Group cn cannot be empty.')); } if ($title == '') { $errors[] = array('field' => 'title', 'message' => Lang::txt('Group title cannot be empty.')); } // check to make sure cn is valid & isnt taken if (!\Hubzero\Utility\Validate::group($cn, false)) { $errors[] = array('field' => 'cn', 'message' => Lang::txt('COM_GROUPS_SAVE_ERROR_INVALID_ID')); } if (\Hubzero\User\Group::exists($cn, false)) { $errors[] = array('field' => 'cn', 'message' => Lang::txt('COM_GROUPS_SAVE_ERROR_ID_TAKEN')); } // valid join policy $policies = array(0 => 'open', 1 => 'restricted', 2 => 'invite_only', 3 => 'closed'); // make sure we have a valid policy if (!in_array($joinPolicy, $policies)) { $errors[] = array('field' => 'join_policy', 'message' => Lang::txt('Group "join_policy" value must be one of the following: %s', implode(', ', $policies))); } // valid discoverabilities $discoverabilities = array(0 => 'visible', 1 => 'hidden'); // make sure we have a valid discoverability if (!in_array($discoverability, $discoverabilities)) { $errors[] = array('field' => 'discoverability', 'message' => Lang::txt('Group "discoverability" value must be one of the following: %s', implode(', ', $discoverabilities))); } // check for errors at this point if (!empty($errors)) { throw new Exception(Lang::txt('Validation Failed') . ': ' . implode("\n", $errors), 422); } // make sure we have a public desc of none was entered if ($publicDesc == '') { $publicDesc = $title; } // map the join policy & discoverability values to their int value $joinPolicy = array_search($joinPolicy, $policies); $discoverability = array_search($discoverability, $discoverabilities); // bind all our fields to the group object $group = new \Hubzero\User\Group(); $group->set('cn', $cn); $group->set('type', 1); $group->set('published', 1); $group->set('approved', \App::get('component')->params('com_groups')->get('auto_approve', 1)); $group->set('description', $title); $group->set('public_desc', $publicDesc); $group->set('private_desc', $privateDesc); $group->set('join_policy', $joinPolicy); $group->set('discoverability', $discoverability); $group->set('created', with(new Date('now'))->toSql()); $group->set('created_by', User::get('id')); $group->add('managers', array(User::get('id'))); $group->add('members', array(User::get('id'))); if (!$group->create() || !$group->update()) { throw new Exception(Lang::txt('Failed to create group.'), 500); } $this->send($group); }