Commit 0edbc6fd authored by Cecilia Vela Gurovic's avatar Cecilia Vela Gurovic
Browse files

Bug 1667799: Added function to find group information by id

Replaced most calls to DB to request group
basic information, for get_group_by_id() method
in group library

behatnotneeded

Change-Id: Ie03520a6c7a364ead63ccedaff07118a5928348e
parent 4a9e6f29
......@@ -69,7 +69,7 @@ if ($institution = param_alphanum('institution', null)) {
}
else if ($groupid = param_alphanum('group', null)) {
$blogs->group = $groupid;
$group = get_record('group', 'id', $groupid, 'deleted', 0);
$group = get_group_by_id($groupid);
define('TITLE', $group->name);
}
else {
......
......@@ -252,7 +252,7 @@ class ArtefactTypeBlog extends ArtefactType {
}
}
else if (!empty($this->group)) {
$group = get_record('group', 'id', $this->group, 'deleted', 0);
$group = get_group_by_id($this->group);
$USER->reset_grouproles();
if (!isset($USER->grouproles[$this->group])) {
throw new AccessDeniedException(get_string('youarenotamemberof', 'artefact.blog', $group->name));
......@@ -793,7 +793,7 @@ class ArtefactTypeBlogPost extends ArtefactType {
}
}
else if (!empty($this->group)) {
$group = get_record('group', 'id', $this->group, 'deleted', 0);
$group = get_group_by_id($this->group);
$USER->reset_grouproles();
if (!isset($USER->grouproles[$this->group])) {
throw new AccessDeniedException(get_string('youarenotamemberof', 'artefact.blog', $group->name));
......
......@@ -42,7 +42,7 @@ if ($institutionname = param_alphanum('institution', null)) {
}
else if ($groupid = param_alphanum('group', null)) {
require_once('group.php');
$group = get_record('group', 'id', $groupid, 'deleted', 0);
$group = get_group_by_id($groupid);
$USER->reset_grouproles();
if (!isset($USER->grouproles[$group->id])) {
throw new AccessDeniedException(get_string('youarenotamemberof', 'artefact.blog', $group->name));
......
......@@ -45,7 +45,7 @@ if (!$blogpost) {
$focuselement = 'title';
$attachments = array();
if ($blogobj->get('group')) {
$group = get_record('group', 'id', $blogobj->get('group'), 'deleted', 0);
$group = get_group_by_id($blogobj->get('group'));
define('TITLE', $group->name);
define('SUBSECTIONHEADING', $pagetitle);
}
......@@ -58,7 +58,7 @@ else {
$blogpostobj->check_permission(true);
$pagetitle = get_string('editblogpost', 'artefact.blog');
if ($blogpostobj->get('group')) {
$group = get_record('group', 'id', $blogpostobj->get('group'), 'deleted', 0);
$group = get_group_by_id($blogpostobj->get('group'));
define('SUBSECTIONHEADING', $pagetitle);
define('TITLE', $group->name);
}
......
......@@ -51,7 +51,7 @@ if ($institution = param_alphanum('institution', null)) {
}
}
else if ($groupid = param_alphanum('group', null)) {
$group = get_record('group', 'id', $groupid, 'deleted', 0);
$group = get_group_by_id($groupid);
$title = get_string('groupblogs', 'artefact.blog', $group->name);
}
else if ($id) {
......@@ -59,7 +59,7 @@ else if ($id) {
$institution = $institutionname = $blogobj->get('institution');
$groupid = $blogobj->get('group');
if ($groupid) {
$group = get_record('group', 'id', $groupid, 'deleted', 0);
$group = get_group_by_id($groupid);
}
$title = get_string('viewbloggroup', 'artefact.blog', $blogobj->get('title'));
if ($institution && $institution != 'mahara') {
......
......@@ -426,7 +426,7 @@ class PluginArtefactFile extends PluginArtefact {
}
}
else if ($group !== null) {
$groupdata = get_record('group', 'id', $group);
$groupdata = get_group_by_id($group, true);
$groupdata->quotausedpercent = empty($groupdata->quota) ? 0 : (($groupdata->quotaused + $filesize ) / $groupdata->quota) * 100;
$overlimit = false;
......
......@@ -59,7 +59,7 @@ class PluginBlocktypeGroupMembers extends MaharaCoreBlocktype {
// If the group has hidden membership, display nothing
$usergroups = $USER->get('grouproles');
$group = defined('GROUP') && $groupid == GROUP ? group_current_group() : get_record('group', 'id', $groupid);
$group = defined('GROUP') && $groupid == GROUP ? group_current_group() : get_group_by_id($groupid, true);
if ($group->hidemembersfrommembers === '1' && (!isset($usergroups[$groupid]) || $usergroups[$groupid] != 'admin')) {
return '';
}
......
......@@ -23,7 +23,7 @@ $groupid = param_integer('id');
$return = param_alphanum('return', null);
// Check the group exists
if (!get_record('group', 'id', $groupid)) {
if (!get_group_by_id($groupid, true)) {
throw new GroupNotFoundException(get_string('groupnotfound', 'group', $groupid));
}
......
......@@ -15,7 +15,7 @@ require(dirname(dirname(__FILE__)) . '/init.php');
require_once(get_config('libroot') . 'group.php');
$id = param_integer('id');
$group = get_record('group', 'id', $id);
$group = get_group_by_id($id, true);
$group = group_get_groupinfo_data($group);
......
......@@ -57,7 +57,7 @@ $feedtype = param_alpha('type'); //g = group, f = forum, t = topic
$id = param_integer('id');
if ($feedtype == 'g') {
if (!$group = get_record('group', 'id', $id, 'deleted', 0)) {
if (!$group = get_group_by_id($id)) {
generate_feed(error_feed(), error_post(get_string('groupnotfound', 'group', $id)));
exit();
}
......
......@@ -43,7 +43,7 @@ if (!$topic) {
define('GROUP', $topic->groupid);
$group = get_record('group', 'id', $topic->groupid);
$group = get_group_by_id($topic->groupid, true);
$publicgroup = $group->public;
$ineditwindow = group_within_edit_window($group);
$feedlink = get_config('wwwroot') . 'interaction/forum/atom.php?type=t&id=' . $topic->id;
......
......@@ -52,7 +52,7 @@ define('GROUP', $forum->groupid);
$membership = user_can_access_forum((int)$forumid);
$admin = (bool)($membership & INTERACTION_FORUM_ADMIN);
$moderator = (bool)($membership & INTERACTION_FORUM_MOD);
$group = get_record('group', 'id', $forum->groupid);
$group = get_group_by_id($forum->groupid, true);
$publicgroup = $group->public;
if (!$membership && !$publicgroup) {
throw new GroupAccessDeniedException(get_string('cantviewforums', 'interaction.forum'));
......
......@@ -186,7 +186,7 @@ function group_role_can_edit_views($group, $role) {
*/
function group_within_edit_window($group, $admin_always=true) {
if (is_numeric($group)) {
$group = get_record('group', 'id', $group);
$group = get_group_by_id($group, true);
}
if ($admin_always && group_user_access($group->id) == 'admin') {
......@@ -774,7 +774,7 @@ function group_delete($groupid, $shortname=null, $institution=null, $notifymembe
}
else {
$groupid = group_param_groupid($groupid);
$group = get_record('group', 'id', $groupid);
$group = get_group_by_id($groupid, true);
}
db_begin();
......@@ -912,7 +912,7 @@ function group_user_can_leave($group, $userid=null) {
$userid = optional_userid($userid);
if (is_numeric($group)) {
if (!$group = get_record('group', 'id', $group, 'deleted', 0)) {
if (!$group = get_group_by_id($group)) {
return false;
}
}
......@@ -993,7 +993,7 @@ function group_update_members($groupid, $members, $lines_done = 0, $num_lines =
$groupid = group_param_groupid($groupid);
if (!$group = get_record('group', 'id', $groupid, 'deleted', 0)) {
if (!$group = get_group_by_id($groupid)) {
throw new NotFoundException("group_update_members: group not found: $groupid");
}
......@@ -1290,7 +1290,7 @@ function joingroup_submit(Pieform $form, $values) {
$next = $values['returnto'];
}
else {
$next = group_homepage_url(get_record('group', 'id', $values['group']));
$next = group_homepage_url(get_group_by_id($values['group'], true));
}
redirect($next);
}
......@@ -1307,7 +1307,7 @@ function group_invite_submit(Pieform $form, $values) {
$next = $values['returnto'];
}
else {
$next = group_homepage_url(get_record('group', 'id', $values['group']));
$next = group_homepage_url(get_group_by_id($values['group'], true));
}
redirect($next);
}
......@@ -2527,7 +2527,7 @@ function group_quota_allowed($groupid, $bytes) {
if (!is_numeric($bytes) || $bytes < 0) {
throw new InvalidArgumentException('parameter must be a positive integer to add to the quota');
}
if (!$group = get_record('group', 'id', $groupid, 'deleted', 0)) {
if (!$group = get_group_by_id($groupid)) {
throw new GroupNotFoundException(get_string('groupnotfound', 'group', $groupid));
}
if ($group->quotaused + $bytes > $group->quota) {
......@@ -2541,7 +2541,7 @@ function group_quota_add($groupid, $bytes) {
if (!group_quota_allowed($groupid, $bytes)) {
throw new QuotaExceededException('Adding ' . $bytes . ' bytes would exceed the group\'s quota');
}
if (!$group = get_record('group', 'id', $groupid, 'deleted', 0)) {
if (!$group = get_group_by_id($groupid)) {
throw new GroupNotFoundException(get_string('groupnotfound', 'group', $groupid));
}
$newquota = $group->quotaused + $bytes;
......@@ -2555,7 +2555,7 @@ function group_quota_remove($groupid, $bytes) {
if (!is_numeric($bytes) || $bytes < 0) {
throw new InvalidArgumentException('parameter must be a positive integer to add to the quota');
}
if (!$group = get_record('group', 'id', $groupid, 'deleted', 0)) {
if (!$group = get_group_by_id($groupid)) {
throw new GroupNotFoundException(get_string('groupnotfound', 'group', $groupid));
}
$newquota = max(0, $group->quotaused - $bytes);
......@@ -2745,7 +2745,7 @@ function group_get_membership_file_data($group_id, $file_format = 'csv', $mimety
return $data;
}
$group = get_record('group', 'id', $group_id);
$group = get_group_by_id($group_id, true);
if (!$group) {
return $data;
......@@ -2968,3 +2968,30 @@ function new_group_name($name) {
}
return $title . $ext;
}
/**
*
* @param integer $groupid ID of the group to check
* @param boolean $includedeleted Whether to include deleted groups in result
* @param boolean $getrole Whether to return the role in group of logged in user
* @param boolean $canedit Whether to return if the user has create/edit permission in group
*/
function get_group_by_id($groupid, $includedeleted = false, $getrole = false, $canedit = false) {
$groupid = group_param_groupid($groupid);
if ($includedeleted) {
$group = get_record('group', 'id', $groupid);
}
else {
$group = get_record('group', 'id', $groupid, 'deleted', 0);
}
if (empty($group)) {
return false;
}
if ($getrole || $canedit) {
$group->role = group_user_access($group->id);
}
if ($canedit) {
$group->canedit = $group->role && group_role_can_edit_views($group, $group->role);
}
return $group;
}
......@@ -852,7 +852,7 @@ class View {
public function get_group_object() {
if (!isset($this->groupobj)) {
$this->groupobj = get_record('group', 'id', $this->get('group'));
$this->groupobj = get_group_by_id($this->get('group'), true);
}
return $this->groupobj;
}
......@@ -3238,7 +3238,7 @@ class View {
$formcontrols .= '<input type="hidden" name="' . hsc($elementname) . '_onpage[]" value="' . hsc($artefact->id) . '" class="artefactid-onpage">';
}
if (!empty($artefact->group)) {
$groupobj = get_record('group', 'id', $artefact->group);
$groupobj = get_group_by_id($artefact->group, true);
$artefact->groupname = $groupobj->name;
$artefact->groupurl = get_config('wwwroot') . 'group/view.php?id=' . $groupobj->id;
}
......@@ -5765,7 +5765,7 @@ class View {
else {
if ($this->get('group')) {
if ($this->get('type') == 'grouphomepage') {
$redirecturl = group_homepage_url(get_record('group', 'id', $this->get('group')));
$redirecturl = group_homepage_url(get_group_by_id($this->get('group'), true));
}
else {
$redirecturl = '/view/groupviews.php?group='.$this->get('group');
......
......@@ -378,7 +378,7 @@ function invite_submit(Pieform $form, $values) {
function addmember_submit(Pieform $form, $values) {
global $USER, $SESSION, $userid;
$group = get_record('group', 'id', $values['group']);
$group = get_group_by_id($values['group'], true);
$ctitle = $group->name;
$adduser = get_record('usr', 'id', $userid);
......
......@@ -89,7 +89,7 @@ else if ($view->get('type') == 'dashboard') {
else if ($view->get('type') == 'grouphomepage') {
$title = get_string('grouphomepage', 'view');
if ($view->get('template') != View::SITE_TEMPLATE) {
$groupurl = group_homepage_url(get_record('group', 'id', $view->get('group')), false);
$groupurl = group_homepage_url(get_group_by_id($view->get('group'), true), false);
}
define('TITLE', $title);
}
......
......@@ -79,7 +79,7 @@ if ($studentid !== '') {
// have their own clean profiles or group homepages.
if ($urlallowed = get_config('cleanurls') && $view->get('type') == 'portfolio' && !$institution) {
if ($group) {
$groupdata = get_record('group', 'id', $group);
$groupdata = get_group_by_id($group, true);
if ($urlallowed = !is_null($groupdata->urlid) && strlen($groupdata->urlid)) {
$cleanurlbase = group_homepage_url($groupdata) . '/';
}
......
......@@ -123,7 +123,7 @@ $submittedgroup = (int)$view->get('submittedgroup');
if ($USER->is_logged_in() && $submittedgroup && group_user_can_assess_submitted_views($submittedgroup, $USER->get('id'))) {
// The user is a tutor of the group that this view has
// been submitted to, and is entitled to release the view
$submittedgroup = get_record('group', 'id', $submittedgroup);
$submittedgroup = get_group_by_id($submittedgroup, true);
// If the view is part of a submitted collection, the whole
// collection must be released at once.
$releasecollection = !empty($collection) && $collection->get('submittedgroup') == $submittedgroup->id;
......
......@@ -273,7 +273,7 @@ class mahara_group_external extends external_api {
foreach ($params['groups'] as $group) {
// Make sure that the group doesn't already exist
if (!empty($group['id'])) {
if (!$dbgroup = get_record('group', 'id', $group['id'], 'deleted', 0)) {
if (!$dbgroup = get_group_by_id($group['id'])) {
throw new WebserviceInvalidParameterException('delete_groups | ' . get_string('groupnotexist', 'auth.webservice', $group['id']));
}
}
......@@ -381,7 +381,7 @@ class mahara_group_external extends external_api {
foreach ($params['groups'] as $group) {
// Make sure that the group doesn't already exist
if (!empty($group['id'])) {
if (!$dbgroup = get_record('group', 'id', $group['id'], 'deleted', 0)) {
if (!$dbgroup = get_group_by_id($group['id'])) {
throw new WebserviceInvalidParameterException('update_groups | ' . get_string('groupnotexist', 'auth.webservice', $group['id']));
}
}
......@@ -562,7 +562,7 @@ class mahara_group_external extends external_api {
foreach ($params['groups'] as $group) {
// Make sure that the group doesn't already exist
if (!empty($group['id'])) {
if (!$dbgroup = get_record('group', 'id', $group['id'], 'deleted', 0)) {
if (!$dbgroup = get_group_by_id($group['id'])) {
throw new WebserviceInvalidParameterException('update_group_members | ' . get_string('groupnotexist', 'auth.webservice', $group['id']));
}
}
......@@ -721,7 +721,7 @@ class mahara_group_external extends external_api {
foreach ($params['groups'] as $group) {
// Make sure that the group doesn't already exist
if (!empty($group['id'])) {
if (!$dbgroup = get_record('group', 'id', $group['id'], 'deleted', 0)) {
if (!$dbgroup = get_groups_by_id($group['id'])) {
throw new WebserviceInvalidParameterException('get_groups_by_id | ' . get_string('groupnotexist', 'auth.webservice', $group['id']));
}
}
......
......@@ -233,7 +233,7 @@ class WebServiceGroupTest extends WebServiceTestBase {
//search for them => TESTS they don't exists
foreach (array($dbgroup1, $dbgroup2) as $group) {
$group = get_record('group', 'id', $group->id, 'deleted', 0);
$group = get_group_by_id($group->id);
$this->assertTrue(empty($group));
}
}
......@@ -326,10 +326,10 @@ class WebServiceGroupTest extends WebServiceTestBase {
$params = array('groups' => $groups);
$client->call($function, $params);
$dbgroup1 = get_record('group', 'id', $groupid1);
$dbgroup1 = get_group_by_id($groupid1, true);
$dbgroupmembers1 = get_records_array('group_member', 'group', $dbgroup1->id);
$dbgroup2 = get_record('group', 'id', $groupid2);
$dbgroup2 = get_group_by_id($groupid2, true);
$dbgroupmembers2 = get_records_array('group_member', 'group', $dbgroup2->id);
// temporary hack untl group changes are sorted XXX
......@@ -441,10 +441,10 @@ class WebServiceGroupTest extends WebServiceTestBase {
$params = array('groups' => $groups);
$client->call($function, $params);
$dbgroup1 = get_record('group', 'id', $groupid1);
$dbgroup1 = get_group_by_id($groupid1, true);
$dbgroupmembers1 = get_records_array('group_member', 'group', $dbgroup1->id);
$dbgroup2 = get_record('group', 'id', $groupid2);
$dbgroup2 = get_group_by_id($groupid2, true);
$dbgroupmembers2 = get_records_array('group_member', 'group', $dbgroup2->id);
//compare DB group with the test data
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment