Fixed group mode

This commit is contained in:
Mark Nelson 2015-03-11 14:58:52 -07:00
parent 6821122c30
commit ba12c4d42b
3 changed files with 59 additions and 45 deletions

View file

@ -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);
}

View file

@ -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);

View file

@ -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;