Skip to content

Commit

Permalink
MDL-75695 mod_h5pactivity: add group filter to attempts report.
Browse files Browse the repository at this point in the history
This is a backport from MDL-71662.
  • Loading branch information
paulholden authored and ilyatregubov committed Sep 8, 2022
1 parent ad5bc5b commit e48460c
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 16 deletions.
18 changes: 14 additions & 4 deletions mod/h5pactivity/classes/local/manager.php
Expand Up @@ -332,15 +332,24 @@ public function count_attempts(int $userid = null): int {
*
* @since Moodle 3.9.7
* @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
*/
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.
$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.
$capjoin = get_enrolled_with_capabilities_join($context, '', 'mod/h5pactivity:view');
$capjoin = get_enrolled_with_capabilities_join($context, '', 'mod/h5pactivity:view', $currentgroup);

if ($capjoin->cannotmatchanyrows) {
return $capjoin;
Expand Down Expand Up @@ -438,9 +447,10 @@ public function get_grader(): grader {
*
* @param int $userid an opional userid 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)
*/
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;

// If tracking is disabled, no reports are available.
Expand Down Expand Up @@ -480,7 +490,7 @@ public function get_report(int $userid = null, int $attemptid = null): ?report {
} else if ($user) {
return new attempts($this, $user);
}
return new participants($this);
return new participants($this, $currentgroup);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions mod/h5pactivity/classes/local/report/participants.php
Expand Up @@ -61,8 +61,9 @@ class participants extends table_sql implements report {
* Create a new participants report.
*
* @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');
$this->manager = $manager;
$this->scores = $manager->get_users_scaled_score();
Expand All @@ -83,7 +84,7 @@ public function __construct(manager $manager) {
$this->no_sorting('attempts');
$this->pageable(true);

$capjoin = $this->manager->get_active_users_join(true);
$capjoin = $this->manager->get_active_users_join(true, $currentgroup);

// Final SQL.
$this->set_sql(
Expand Down
6 changes: 5 additions & 1 deletion mod/h5pactivity/report.php
Expand Up @@ -43,9 +43,11 @@

require_login($course, true, $cm);

$currentgroup = groups_get_activity_group($cm, true);

$manager = manager::create_from_coursemodule($cm);

$report = $manager->get_report($userid, $attemptid);
$report = $manager->get_report($userid, $attemptid, $currentgroup);
if (!$report) {
print_error('permissiondenied');
}
Expand Down Expand Up @@ -125,6 +127,8 @@

echo $OUTPUT->header();

groups_print_activity_menu($cm, $PAGE->url);

echo $report->print();

echo $OUTPUT->footer();
63 changes: 54 additions & 9 deletions mod/h5pactivity/tests/local/manager_test.php
Expand Up @@ -14,14 +14,6 @@
// You should have received a copy of the GNU General Public License
// 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;
use context_module;
Expand All @@ -31,6 +23,7 @@
* Manager tests class for mod_h5pactivity.
*
* @package mod_h5pactivity
* @covers \mod_h5pactivity\local\manager
* @category test
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
Expand Down Expand Up @@ -627,7 +620,7 @@ public function count_attempts_all_data(): array {
}

/**
* 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
* need to test the with $allpotentialusers true and false.
Expand Down Expand Up @@ -692,6 +685,58 @@ public function get_active_users_join_data(): array {
];
}

/**
* 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.
*/
Expand Down
7 changes: 7 additions & 0 deletions mod/h5pactivity/upgrade.txt
@@ -1,6 +1,13 @@
This files describes API changes in /mod/h5pactivity/*,
information provided here is intended especially for developers.

=== 3.9.17 ===

* 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.9.7 ===

* Added mod_h5pactivity\local\manager::get_active_users_join method to query all active
Expand Down

0 comments on commit e48460c

Please sign in to comment.