From c13ac85db8de26470c6c39a0d41129eb64d8f097 Mon Sep 17 00:00:00 2001 From: sam marshall Date: Thu, 7 Aug 2014 13:39:54 +0100 Subject: [PATCH] MDL-44725 Availability: Replace groupmembersonly - assign (10) Updated code to restrict list of users. Also includes changes to ensure that a cm_info object is available (required for availability checks). There is a tweak to upgradelib to reflect the different fields used. (Note that upgradelib is not used during upgrade, but only when converting assignments from the old assignment module.) --- mod/assign/locallib.php | 21 ++++++++++++--------- mod/assign/tests/locallib_test.php | 20 ++++++++++++++++++-- mod/assign/upgradelib.php | 1 - mod/assign/view.php | 4 ++-- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/mod/assign/locallib.php b/mod/assign/locallib.php index 4629d71e50e..4ae18a1a5b5 100644 --- a/mod/assign/locallib.php +++ b/mod/assign/locallib.php @@ -98,7 +98,7 @@ class assign { /** @var assign_renderer the custom renderer for this module */ private $output; - /** @var stdClass the course module for this assign instance */ + /** @var cm_info the course module for this assign instance */ private $coursemodule; /** @var array cache for things like the coursemodule name or the scale menu - @@ -138,6 +138,9 @@ class assign { /** * Constructor for the base assign class. * + * Note: For $coursemodule you can supply a stdclass if you like, but it + * will be more efficient to supply a cm_info object. + * * @param mixed $coursemodulecontext context|null the course module context * (or the course context if the coursemodule has not been * created yet). @@ -148,9 +151,11 @@ class assign { */ public function __construct($coursemodulecontext, $coursemodule, $course) { $this->context = $coursemodulecontext; - $this->coursemodule = $coursemodule; $this->course = $course; + // Ensure that $this->coursemodule is a cm_info object (or null). + $this->coursemodule = cm_info::create($coursemodule); + // Temporary cache only lives for a single request - used to reduce db lookups. $this->cache = array(); @@ -1182,7 +1187,7 @@ class assign { /** * Get the current course module. * - * @return mixed stdClass|null The course module + * @return cm_info|null The course module or null if not known */ public function get_course_module() { if ($this->coursemodule) { @@ -1193,11 +1198,8 @@ class assign { } if ($this->context->contextlevel == CONTEXT_MODULE) { - $this->coursemodule = get_coursemodule_from_id('assign', - $this->context->instanceid, - 0, - false, - MUST_EXIST); + $modinfo = get_fast_modinfo($this->get_course()); + $this->coursemodule = $modinfo->get_cm($this->context->instanceid); return $this->coursemodule; } return null; @@ -1336,7 +1338,8 @@ class assign { $this->show_only_active_users()); $cm = $this->get_course_module(); - $users = groups_filter_users_by_course_module_visible($cm, $users); + $info = new \core_availability\info_module($cm); + $users = $info->filter_user_list($users); $this->participants[$key] = $users; } diff --git a/mod/assign/tests/locallib_test.php b/mod/assign/tests/locallib_test.php index 1a203d291c7..5feee8840ab 100644 --- a/mod/assign/tests/locallib_test.php +++ b/mod/assign/tests/locallib_test.php @@ -579,6 +579,8 @@ class mod_assign_locallib_testcase extends mod_assign_base_testcase { } public function test_list_participants() { + global $CFG, $DB; + $this->create_extra_users(); $this->setUser($this->editingteachers[0]); $assign = $this->create_instance(array('grade'=>100)); @@ -596,6 +598,18 @@ class mod_assign_locallib_testcase extends mod_assign_base_testcase { set_user_preference('grade_report_showonlyactiveenrol', false); $assign = $this->create_instance(array('grade'=>100)); $this->assertEquals(self::DEFAULT_STUDENT_COUNT + self::EXTRA_STUDENT_COUNT, count($assign->list_participants(null, true))); + + // Turn on availability and a group restriction, and check that it doesn't + // show users who aren't in the group. + $CFG->enableavailability = true; + $specialgroup = $this->getDataGenerator()->create_group( + array('courseid' => $this->course->id)); + $assign = $this->create_instance(array('grade' => 100, + 'availability' => json_encode(\core_availability\tree::get_root_json( + array(\availability_group\condition::get_json($specialgroup->id)))))); + groups_add_member($specialgroup, $this->students[0]); + groups_add_member($specialgroup, $this->students[1]); + $this->assertEquals(2, count($assign->list_participants(null, true))); } public function test_count_teams() { @@ -1173,13 +1187,15 @@ class mod_assign_locallib_testcase extends mod_assign_base_testcase { $this->setAdminUser(); $this->create_extra_users(); - $CFG->enablegroupmembersonly = true; + $CFG->enableavailability = true; $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $this->course->id)); groups_assign_grouping($grouping->id, $this->groups[0]->id); // Force create an assignment with SEPARATEGROUPS. $instance = $this->getDataGenerator()->create_module('assign', array('course'=>$this->course->id), - array('groupmembersonly' => SEPARATEGROUPS, 'groupingid' => $grouping->id)); + array('availability' => json_encode(\core_availability\tree::get_root_json(array( + \availability_grouping\condition::get_json()))), + 'groupingid' => $grouping->id)); $cm = get_coursemodule_from_instance('assign', $instance->id); $context = context_module::instance($cm->id); diff --git a/mod/assign/upgradelib.php b/mod/assign/upgradelib.php index afca47fd8e2..187cb92c6c8 100644 --- a/mod/assign/upgradelib.php +++ b/mod/assign/upgradelib.php @@ -380,7 +380,6 @@ class assign_upgrade_manager { $newcm->indent = $cm->indent; $newcm->groupmode = $cm->groupmode; $newcm->groupingid = $cm->groupingid; - $newcm->groupmembersonly = $cm->groupmembersonly; $newcm->completion = $cm->completion; $newcm->completiongradeitemnumber = $cm->completiongradeitemnumber; $newcm->completionview = $cm->completionview; diff --git a/mod/assign/view.php b/mod/assign/view.php index 9fa7e24395d..62e0b7f16cf 100644 --- a/mod/assign/view.php +++ b/mod/assign/view.php @@ -33,8 +33,8 @@ $urlparams = array('id' => $id, 'useridlistid' => optional_param('action', 0, PARAM_INT)); $url = new moodle_url('/mod/assign/view.php', $urlparams); -$cm = get_coursemodule_from_id('assign', $id, 0, false, MUST_EXIST); -$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); + +list ($course, $cm) = get_course_and_cm_from_cmid($id, 'assign'); require_login($course, true, $cm); $PAGE->set_url($url);