public function test_user_can_add_children()
 {
     $options = array('colltype' => 'contributed', 'course' => $this->course->id, 'groupmode' => VISIBLEGROUPS, 'maxgalleries' => 1);
     $record = $this->getDataGenerator()->create_module('mediagallery', $options);
     $collection = new \mod_mediagallery\collection($record);
     $group1 = $this->getDataGenerator()->create_group(array('courseid' => $this->course->id));
     $group2 = $this->getDataGenerator()->create_group(array('courseid' => $this->course->id));
     $this->getDataGenerator()->create_group_member((object) array('groupid' => $group1, 'userid' => $this->students[0]->id));
     $this->getDataGenerator()->create_group_member((object) array('groupid' => $group2, 'userid' => $this->students[0]->id));
     $this->getDataGenerator()->create_group_member((object) array('groupid' => $group2, 'userid' => $this->students[1]->id));
     $this->assertTrue($collection->user_can_add_children($this->students[0]->id));
     $this->assertTrue($collection->user_can_add_children($this->students[1]->id));
     $generator = self::getDataGenerator()->get_plugin_generator('mod_mediagallery');
     $record = array('name' => 'Test gallery G1', 'instanceid' => $collection->id, 'contributable' => 1, 'userid' => $this->students[0]->id, 'groupid' => $group1->id);
     $generator->create_gallery($record);
     // Limit is one per group, as both are in group2 which has no gallery yet, they should both still be able to add.
     $this->assertTrue($collection->user_can_add_children($this->students[0]->id));
     $this->assertTrue($collection->user_can_add_children($this->students[1]->id));
     $record = array('name' => 'Test gallery G2', 'instanceid' => $collection->id, 'contributable' => 1, 'userid' => $this->students[0]->id, 'groupid' => $group2->id);
     $generator->create_gallery($record);
     // Now that group2 has a gallery, neither should be able to add a new gallery.
     $this->assertFalse($collection->user_can_add_children($this->students[0]->id));
     $this->assertFalse($collection->user_can_add_children($this->students[1]->id));
     // Now test with a non-groupmode collection.
     $options = array('colltype' => 'contributed', 'course' => $this->course->id, 'groupmode' => NOGROUPS, 'maxgalleries' => 1);
     $record = $this->getDataGenerator()->create_module('mediagallery', $options);
     $collection = new \mod_mediagallery\collection($record);
     $this->assertTrue($collection->user_can_add_children($this->students[0]->id));
     $this->assertTrue($collection->user_can_add_children($this->students[1]->id));
     $generator = self::getDataGenerator()->get_plugin_generator('mod_mediagallery');
     $record = array('name' => 'Test gallery G1', 'instanceid' => $collection->id, 'contributable' => 1, 'userid' => $this->students[0]->id);
     $generator->create_gallery($record);
     // Limit is one per user, not in groupmode so groups don't matter.
     $this->assertFalse($collection->user_can_add_children($this->students[0]->id));
     $this->assertTrue($collection->user_can_add_children($this->students[1]->id));
 }
if (!$m && !$g) {
    print_error('missingparameter');
}
$gallery = false;
if ($g) {
    $gallery = new \mod_mediagallery\gallery($g);
    $m = $gallery->instanceid;
}
$mediagallery = $DB->get_record('mediagallery', array('id' => $m), '*', MUST_EXIST);
$mediagallery = new \mod_mediagallery\collection($mediagallery);
$course = $DB->get_record('course', array('id' => $mediagallery->course), '*', MUST_EXIST);
$cm = get_coursemodule_from_instance('mediagallery', $mediagallery->id, $course->id, false, MUST_EXIST);
require_login($course, true, $cm);
$context = context_module::instance($cm->id);
$maxgalleries = $mediagallery->maxgalleries;
if (!$gallery && !$mediagallery->user_can_add_children()) {
    print_error('errortoomanygalleries', 'mediagallery', '', $maxgalleries);
}
$pageurl = new moodle_url('/mod/mediagallery/gallery.php', array('m' => $mediagallery->id));
$PAGE->set_url($pageurl);
$PAGE->set_title(format_string($mediagallery->name));
$PAGE->set_heading(format_string($course->fullname));
$PAGE->set_context($context);
$groupmode = groups_get_activity_groupmode($cm);
if (has_capability('moodle/site:accessallgroups', $context) && $groupmode != NOGROUPS) {
    $groupmode = 'aag';
    $groups = groups_get_all_groups($cm->course, null, $cm->groupingid);
} else {
    $groups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid);
}
$tags = \mod_mediagallery\gallery::get_tags_possible();