From ba12c4d42b3ffdc33943734cf5b276eabfa0e44b Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Wed, 11 Mar 2015 14:58:52 -0700 Subject: [PATCH] Fixed group mode --- locallib.php | 81 ++++++++++++++++++++++++++++++---------------------- report.php | 5 +++- view.php | 18 ++++++------ 3 files changed, 59 insertions(+), 45 deletions(-) diff --git a/locallib.php b/locallib.php index 1ec7757..324c186 100644 --- a/locallib.php +++ b/locallib.php @@ -402,6 +402,11 @@ function customcert_get_issues($customcertid, $groupmode, $cm, $page, $perpage) // Get the conditional SQL. list($conditionssql, $conditionsparams) = customcert_get_conditional_issues_sql($cm, $groupmode); + // If it is empty then return an empty array. + if (empty($conditionsparams)) { + return array(); + } + // Add the conditional SQL and the customcertid to form all used parameters. $allparams = $conditionsparams + array('customcertid' => $customcertid); @@ -431,6 +436,11 @@ function customcert_get_number_of_issues($customcertid, $cm, $groupmode) { // Get the conditional SQL. list($conditionssql, $conditionsparams) = customcert_get_conditional_issues_sql($cm, $groupmode); + // If it is empty then return 0. + if (empty($conditionsparams)) { + return 0; + } + // Add the conditional SQL and the customcertid to form all used parameters. $allparams = $conditionsparams + array('customcertid' => $customcertid); @@ -453,55 +463,58 @@ function customcert_get_number_of_issues($customcertid, $cm, $groupmode) { * @return array the conditional variables */ function customcert_get_conditional_issues_sql($cm, $groupmode) { - global $CFG, $DB; + global $DB, $USER; // Get all users that can manage this customcert to exclude them from the report. $context = context_module::instance($cm->id); $conditionssql = ''; $conditionsparams = array(); - if ($certmanagers = array_keys(get_users_by_capability($context, 'mod/customcert:manage', 'u.id'))) { - list($sql, $params) = $DB->get_in_or_equal($certmanagers, SQL_PARAMS_NAMED, 'cert'); - $conditionssql .= "AND NOT u.id $sql \n"; - $conditionsparams += $params; - } - $restricttogroup = false; + // Get all users that can manage this certificate to exclude them from the report. + $certmanagers = array_keys(get_users_by_capability($context, 'mod/certificate:manage', 'u.id')); + $certmanagers = array_merge($certmanagers, array_keys(get_admins())); + list($sql, $params) = $DB->get_in_or_equal($certmanagers, SQL_PARAMS_NAMED, 'cert'); + $conditionssql .= "AND NOT u.id $sql \n"; + $conditionsparams += $params; + if ($groupmode) { + $canaccessallgroups = has_capability('moodle/site:accessallgroups', $context); $currentgroup = groups_get_activity_group($cm); + + // If we are viewing all participants and the user does not have access to all groups then return nothing. + if (!$currentgroup && !$canaccessallgroups) { + return array('', array()); + } + if ($currentgroup) { - $restricttogroup = true; + if (!$canaccessallgroups) { + // Guest users do not belong to any groups. + if (isguestuser()) { + return array('', array()); + } + + // Check that the user belongs to the group we are viewing. + $usersgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid); + if ($usersgroups) { + if (!isset($usersgroups[$currentgroup])) { + return array('', array()); + } + } else { // They belong to no group, so return an empty array. + return array('', array()); + } + } + $groupusers = array_keys(groups_get_members($currentgroup, 'u.*')); if (empty($groupusers)) { - return array(); + return array('', array()); } + + list($sql, $params) = $DB->get_in_or_equal($groupusers, SQL_PARAMS_NAMED, 'grp'); + $conditionssql .= "AND u.id $sql "; + $conditionsparams += $params; } } - $restricttogrouping = false; - - // If groupmembersonly used, remove users who are not in any group. - if (!empty($CFG->enablegroupings) and $cm->groupmembersonly) { - if ($groupingusers = groups_get_grouping_members($cm->groupingid, 'u.id', 'u.id')) { - $restricttogrouping = true; - } else { - return array(); - } - } - - if ($restricttogroup || $restricttogrouping) { - if ($restricttogroup) { - $allowedusers = $groupusers; - } else if ($restricttogroup && $restricttogrouping) { - $allowedusers = array_intersect($groupusers, $groupingusers); - } else { - $allowedusers = $groupingusers; - } - - list($sql, $params) = $DB->get_in_or_equal($allowedusers, SQL_PARAMS_NAMED, 'grp'); - $conditionssql .= "AND u.id $sql \n"; - $conditionsparams += $params; - } - return array($conditionssql, $conditionsparams); } diff --git a/report.php b/report.php index f61b1a0..5a4aa6d 100644 --- a/report.php +++ b/report.php @@ -52,7 +52,10 @@ $context = context_module::instance($cm->id); require_capability('mod/customcert:manage', $context); // Get the users who have been issued. -$users = customcert_get_issues($customcert->id, groups_get_activity_groupmode($cm), $cm, $page, $perpage); +if ($groupmode = groups_get_activity_groupmode($cm)) { + groups_get_activity_group($cm, true); +} +$users = customcert_get_issues($customcert->id, $groupmode, $cm, $page, $perpage); if ($download) { customcert_generate_report_file($customcert, $users, $download); diff --git a/view.php b/view.php index c7c2254..2d6f48f 100644 --- a/view.php +++ b/view.php @@ -58,22 +58,19 @@ if ($customcert->requiredtime && !has_capability('mod/certificate:manage', $cont // Check that no action was passed, if so that means we are not outputting to PDF. if (empty($action)) { // Get the current groups mode. - groups_print_activity_menu($cm, $pageurl); - $currentgroup = groups_get_activity_group($cm); - $groupmode = groups_get_activity_groupmode($cm); + if ($groupmode = groups_get_activity_groupmode($cm)) { + groups_get_activity_group($cm, true); + } // Generate the link to the report if there are issues to display. $reportlink = ''; if (has_capability('mod/customcert:manage', $context)) { // Get the total number of issues. $numissues = customcert_get_number_of_issues($customcert->id, $cm, $groupmode); - // If the number of issues is greater than 0 display a link to the report. - if ($numissues > 0) { - $href = new moodle_urL('/mod/customcert/report.php', array('id' => $cm->id)); - $url = html_writer::tag('a', get_string('viewcustomcertissues', 'customcert', $numissues), - array('href' => $href->out())); - $reportlink = html_writer::tag('div', $url, array('class' => 'reportlink')); - } + $href = new moodle_urL('/mod/customcert/report.php', array('id' => $cm->id)); + $url = html_writer::tag('a', get_string('viewcustomcertissues', 'customcert', $numissues), + array('href' => $href->out())); + $reportlink = html_writer::tag('div', $url, array('class' => 'reportlink')); } // Generate the intro content if it exists. @@ -111,6 +108,7 @@ if (empty($action)) { // Output all the page data. echo $OUTPUT->header(); + groups_print_activity_menu($cm, $pageurl); echo $reportlink; echo $intro; echo $issuelist;