MDL-71662 mod_h5pactivity: add group filter to attempts report.

This commit is contained in:
Paul Holden 2022-07-28 08:57:11 +01:00
parent 417d1b918b
commit 43bf6c8f00
5 changed files with 83 additions and 16 deletions

View file

@ -332,15 +332,24 @@ class manager {
* *
* @since Moodle 3.11 * @since Moodle 3.11
* @param bool $allpotentialusers if true, the join will return all active users, not only the ones with attempts. * @param bool $allpotentialusers if true, the join will return all active users, not only the ones with attempts.
* @param int|bool $currentgroup False if groups not used, 0 for all groups, group id (int) to filter by specific group
* @return sql_join the active users attempts join * @return sql_join the active users attempts join
*/ */
public function get_active_users_join(bool $allpotentialusers = false): sql_join { public function get_active_users_join(bool $allpotentialusers = false, $currentgroup = false): sql_join {
// Only valid users counts. By default, all users with submit capability are considered potential ones. // Only valid users counts. By default, all users with submit capability are considered potential ones.
$context = $this->get_context(); $context = $this->get_context();
$coursemodule = $this->get_coursemodule();
// Ensure user can view users from all groups.
if ($currentgroup === 0 && $coursemodule->effectivegroupmode == SEPARATEGROUPS
&& !has_capability('moodle/site:accessallgroups', $context)) {
return new sql_join('', '1=2', [], true);
}
// We want to present all potential users. // We want to present all potential users.
$capjoin = get_enrolled_with_capabilities_join($context, '', 'mod/h5pactivity:view'); $capjoin = get_enrolled_with_capabilities_join($context, '', 'mod/h5pactivity:view', $currentgroup);
if ($capjoin->cannotmatchanyrows) { if ($capjoin->cannotmatchanyrows) {
return $capjoin; return $capjoin;
@ -438,9 +447,10 @@ class manager {
* *
* @param int $userid an opional userid to show * @param int $userid an opional userid to show
* @param int $attemptid an optional $attemptid to show * @param int $attemptid an optional $attemptid to show
* @param int|bool $currentgroup False if groups not used, 0 for all groups, group id (int) to filter by specific group
* @return report|null available report (or null if no report available) * @return report|null available report (or null if no report available)
*/ */
public function get_report(int $userid = null, int $attemptid = null): ?report { public function get_report(int $userid = null, int $attemptid = null, $currentgroup = false): ?report {
global $USER; global $USER;
// If tracking is disabled, no reports are available. // If tracking is disabled, no reports are available.
@ -480,7 +490,7 @@ class manager {
} else if ($user) { } else if ($user) {
return new attempts($this, $user); return new attempts($this, $user);
} }
return new participants($this); return new participants($this, $currentgroup);
} }
/** /**

View file

@ -61,8 +61,9 @@ class participants extends table_sql implements report {
* Create a new participants report. * Create a new participants report.
* *
* @param manager $manager h5pactivitymanager object * @param manager $manager h5pactivitymanager object
* @param int|bool $currentgroup False if groups not used, 0 for all groups, group id (int) to filter by specific group
*/ */
public function __construct(manager $manager) { public function __construct(manager $manager, $currentgroup = false) {
parent::__construct('mod_h5pactivity-participants'); parent::__construct('mod_h5pactivity-participants');
$this->manager = $manager; $this->manager = $manager;
$this->scores = $manager->get_users_scaled_score(); $this->scores = $manager->get_users_scaled_score();
@ -83,7 +84,7 @@ class participants extends table_sql implements report {
$this->no_sorting('attempts'); $this->no_sorting('attempts');
$this->pageable(true); $this->pageable(true);
$capjoin = $this->manager->get_active_users_join(true); $capjoin = $this->manager->get_active_users_join(true, $currentgroup);
// Final SQL. // Final SQL.
$this->set_sql( $this->set_sql(

View file

@ -43,9 +43,11 @@ if (empty($id)) {
require_login($course, true, $cm); require_login($course, true, $cm);
$currentgroup = groups_get_activity_group($cm, true);
$manager = manager::create_from_coursemodule($cm); $manager = manager::create_from_coursemodule($cm);
$report = $manager->get_report($userid, $attemptid); $report = $manager->get_report($userid, $attemptid, $currentgroup);
if (!$report) { if (!$report) {
throw new \moodle_exception('permissiondenied'); throw new \moodle_exception('permissiondenied');
} }
@ -126,6 +128,8 @@ $PAGE->set_context($context);
echo $OUTPUT->header(); echo $OUTPUT->header();
groups_print_activity_menu($cm, $PAGE->url);
echo html_writer::start_div('mt-4'); echo html_writer::start_div('mt-4');
echo $report->print(); echo $report->print();
echo html_writer::end_div(); echo html_writer::end_div();

View file

@ -14,14 +14,6 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* mod_h5pactivity manager tests
*
* @package mod_h5pactivity
* @category test
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_h5pactivity\local; namespace mod_h5pactivity\local;
use context_module; use context_module;
@ -31,6 +23,7 @@ use stdClass;
* Manager tests class for mod_h5pactivity. * Manager tests class for mod_h5pactivity.
* *
* @package mod_h5pactivity * @package mod_h5pactivity
* @covers \mod_h5pactivity\local\manager
* @category test * @category test
* @copyright 2020 Ferran Recio <ferran@moodle.com> * @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
@ -627,7 +620,7 @@ class manager_test extends \advanced_testcase {
} }
/** /**
* Test static count_attempts of all active participants. * Test static test_get_active_users_join of all active participants.
* *
* Most method scenarios are tested in test_count_attempts_all so we only * Most method scenarios are tested in test_count_attempts_all so we only
* need to test the with $allpotentialusers true and false. * need to test the with $allpotentialusers true and false.
@ -692,6 +685,58 @@ class manager_test extends \advanced_testcase {
]; ];
} }
/**
* Test active users joins returns appropriate results for groups
*/
public function test_get_active_users_join_groupmode(): void {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course(['groupmode' => SEPARATEGROUPS, 'groupmodeforce' => 1]);
// Teacher/user one in group one.
$teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
$userone = $this->getDataGenerator()->create_and_enrol($course, 'student');
$groupone = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
$this->getDataGenerator()->create_group_member(['groupid' => $groupone->id, 'userid' => $teacher->id]);
$this->getDataGenerator()->create_group_member(['groupid' => $groupone->id, 'userid' => $userone->id]);
// User two in group two.
$usertwo = $this->getDataGenerator()->create_and_enrol($course, 'student');
$grouptwo = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
$this->getDataGenerator()->create_group_member(['groupid' => $grouptwo->id, 'userid' => $usertwo->id]);
$activity = $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course]);
$manager = manager::create_from_instance($activity);
// Admin user can view all participants.
$usersjoin = $manager->get_active_users_join(true, 0);
$users = $DB->get_fieldset_sql("SELECT u.username FROM {user} u {$usersjoin->joins} WHERE {$usersjoin->wheres}",
$usersjoin->params);
$this->assertEqualsCanonicalizing([$teacher->username, $userone->username, $usertwo->username], $users);
// Switch to teacher, who cannot view all participants.
$this->setUser($teacher);
$usersjoin = $manager->get_active_users_join(true, 0);
$users = $DB->get_fieldset_sql("SELECT u.username FROM {user} u {$usersjoin->joins} WHERE {$usersjoin->wheres}",
$usersjoin->params);
$this->assertEmpty($users);
// Teacher can view participants inside group.
$usersjoin = $manager->get_active_users_join(true, $groupone->id);
$users = $DB->get_fieldset_sql("SELECT u.username FROM {user} u {$usersjoin->joins} WHERE {$usersjoin->wheres}",
$usersjoin->params);
$this->assertEqualsCanonicalizing([$teacher->username, $userone->username], $users);
}
/** /**
* Test static count_attempts. * Test static count_attempts.
*/ */

View file

@ -1,6 +1,13 @@
This files describes API changes in /mod/h5pactivity/*, This files describes API changes in /mod/h5pactivity/*,
information provided here is intended especially for developers. information provided here is intended especially for developers.
=== 4.1 ===
* The following methods now accept an optional `$currentgroup` parameter in order to better support groups:
- `\mod_h5pactivity\local\manager::get_active_users_join`
- `\mod_h5pactivity\local\manager::get_report`
- `\mod_h5pactivity\local\report\participants` constructor
=== 3.11 === === 3.11 ===
* Added mod_h5pactivity\local\manager::get_active_users_join method to query all active * Added mod_h5pactivity\local\manager::get_active_users_join method to query all active