public function test_gallery_access_permissions()
 {
     $options = array('colltype' => 'contributed', 'course' => $this->course->id);
     $record = $this->getDataGenerator()->create_module('mediagallery', $options);
     $collection = new \mod_mediagallery\collection($record);
     // Contributed mode. Users can see each others galleries.
     self::setUser($this->students[0]);
     $record = array('name' => 'Test gallery', 'instanceid' => $collection->id);
     $gallery = self::getDataGenerator()->get_plugin_generator('mod_mediagallery')->create_gallery($record);
     $mygals = $collection->get_my_galleries();
     $this->assertTrue(isset($mygals[$gallery->id]));
     self::setUser($this->students[1]);
     $mygals = $collection->get_my_galleries();
     $this->assertFalse(isset($mygals[$gallery->id]));
     // Assignment mode. Users cannot see each others galleries.
     // Peer assessment mode. Users can see each others galleries.
     // Instructor mode. Users can see all galleries.
     // Contributable flag on a gallery in contributed mode.
     $generator = self::getDataGenerator()->get_plugin_generator('mod_mediagallery');
     $teaid = $this->teachers[0]->id;
     $stuid1 = $this->students[0]->id;
     $stuid2 = $this->students[1]->id;
     $record = array('name' => 'Test gallery', 'instanceid' => $collection->id, 'contributable' => 1, 'userid' => $this->students[0]->id);
     $gallery = $generator->create_gallery($record);
     $this->assertTrue($gallery->user_can_edit($stuid1));
     $this->assertFalse($gallery->user_can_edit($stuid2));
     $this->assertTrue($gallery->user_can_contribute($stuid1));
     $this->assertTrue($gallery->user_can_contribute($stuid2));
     $record = array('galleryid' => $gallery->id, 'userid' => $stuid1);
     $item1 = $generator->create_item($record);
     $record = array('galleryid' => $gallery->id, 'userid' => $stuid2);
     $item2 = $generator->create_item($record);
     // Stuid1 owns the gallery and the item. So stuid2 can do nothing to
     // it.
     $this->assertTrue($item1->user_can_edit($stuid1));
     $this->assertTrue($item1->user_can_remove($stuid1));
     $this->assertTrue($item1->user_can_remove($teaid));
     $this->assertFalse($item1->user_can_edit($stuid2));
     $this->assertFalse($item1->user_can_edit($teaid));
     $this->assertFalse($item1->user_can_remove($stuid2));
     // Stuid2 owns the item, but not the gallery. So only stuid2 and teacher
     // can remove it.
     $this->assertTrue($item2->user_can_edit($stuid2));
     $this->assertTrue($item2->user_can_remove($stuid2));
     $this->assertTrue($item2->user_can_remove($teaid));
     $this->assertFalse($item2->user_can_edit($stuid1));
     $this->assertFalse($item2->user_can_edit($teaid));
     $this->assertFalse($item2->user_can_remove($stuid1));
     // Flag off.
     $record = array('name' => 'Test gallery', 'instanceid' => $collection->id, 'contributable' => 0, 'userid' => $this->students[0]->id);
     $gallery = $generator->create_gallery($record);
     $this->assertTrue($gallery->user_can_edit($stuid1));
     $this->assertFalse($gallery->user_can_edit($stuid2));
     $this->assertTrue($gallery->user_can_contribute($stuid1));
     $this->assertFalse($gallery->user_can_contribute($stuid2));
 }
 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));
 }
$id = optional_param('id', 0, PARAM_INT);
// A gallery id.
$m = optional_param('m', 0, PARAM_INT);
// The mediagallery id.
$g = optional_param('g', $id, PARAM_INT);
// A gallery 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) {
/**
 * Execute mediagallery upgrade from the given old version
 *
 * @param int $oldversion
 * @return bool
 */
function xmldb_mediagallery_upgrade($oldversion)
{
    global $CFG, $DB;
    $dbman = $DB->get_manager();
    if ($oldversion < 2014010300) {
        $dbman->install_one_table_from_xmldb_file($CFG->dirroot . '/mod/mediagallery/db/install.xml', 'mediagallery_userfeedback');
        upgrade_mod_savepoint(true, 2014010300, 'mediagallery');
    }
    if ($oldversion < 2014010400) {
        $table = new xmldb_table('mediagallery_gallery');
        $field = new xmldb_field('groupid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'userid');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        upgrade_mod_savepoint(true, 2014010400, 'mediagallery');
    }
    if ($oldversion < 2014010800) {
        require_once "{$CFG->dirroot}/mod/mediagallery/locallib.php";
        if ($records = $DB->get_recordset('mediagallery_item')) {
            foreach ($records as $record) {
                $item = new \mod_mediagallery\item($record);
                $item->generate_thumbnail();
            }
        }
        upgrade_mod_savepoint(true, 2014010800, 'mediagallery');
    }
    if ($oldversion < 2014011400) {
        $table = new xmldb_table('mediagallery_gallery');
        $field = new xmldb_field('visibleinstructor', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, false, 0, 0);
        $dbman->change_field_precision($table, $field);
        $field = new xmldb_field('visibleother', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, false, 0, 0);
        $dbman->change_field_precision($table, $field);
        upgrade_mod_savepoint(true, 2014011400, 'mediagallery');
    }
    if ($oldversion < 2014021800) {
        $table = new xmldb_table('mediagallery_item');
        $field = new xmldb_field('timecreated');
        $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'externalurl');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        $field = new xmldb_field('userid');
        $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'galleryid');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        upgrade_mod_savepoint(true, 2014021800, 'mediagallery');
    }
    if ($oldversion < 2014022500) {
        $table = new xmldb_table('mediagallery');
        $field = new xmldb_field('maxbytes');
        $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'readonlyto');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        $field = new xmldb_field('maxitems');
        $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'maxbytes');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        $field = new xmldb_field('maxgalleries');
        $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1', 'maxitems');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        upgrade_mod_savepoint(true, 2014022500, 'mediagallery');
    }
    if ($oldversion < 2014022701) {
        $table = new xmldb_table('mediagallery');
        $field = new xmldb_field('allowcomments');
        $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1', 'maxgalleries');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        $field = new xmldb_field('allowlikes');
        $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1', 'allowcomments');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        upgrade_mod_savepoint(true, 2014022701, 'mediagallery');
    }
    if ($oldversion < 2014042801) {
        $table = new xmldb_table('mediagallery');
        $field = new xmldb_field('gallerytype', XMLDB_TYPE_CHAR, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, '');
        $dbman->change_field_precision($table, $field);
        $table = new xmldb_table('mediagallery_gallery');
        $field = new xmldb_field('gallerytype');
        $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1', 'exportable');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Set type based on collection type.
        $sql = "SELECT mg.id, mc.gallerytype\n                FROM {mediagallery_gallery} mg\n                JOIN {mediagallery} mc ON (mc.id = mg.instanceid)";
        $galleries = $DB->get_recordset_sql($sql);
        foreach ($galleries as $gallery) {
            $DB->update_record('mediagallery_gallery', $gallery);
        }
        upgrade_mod_savepoint(true, 2014042801, 'mediagallery');
    }
    if ($oldversion < 2014102000) {
        $table = new xmldb_table('mediagallery');
        $field = new xmldb_field('objectid');
        $field->set_attributes(XMLDB_TYPE_CHAR, '36', null, null, null, null, 'allowlikes');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        $table = new xmldb_table('mediagallery_gallery');
        $field = new xmldb_field('objectid');
        $field->set_attributes(XMLDB_TYPE_CHAR, '36', null, null, null, null, 'thumbnail');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        $table = new xmldb_table('mediagallery_item');
        $field = new xmldb_field('objectid');
        $field->set_attributes(XMLDB_TYPE_CHAR, '36', null, null, null, null, 'timecreated');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        upgrade_mod_savepoint(true, 2014102000, 'mediagallery');
    }
    if ($oldversion < 2014111400) {
        $table = new xmldb_table('mediagallery');
        $field = new xmldb_field('galleryfocus');
        $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1', 'captionposition');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Migrate existing defaults into new focus.
        $field = new xmldb_field('gallerytype');
        if ($dbman->field_exists($table, $field)) {
            $rs = $DB->get_recordset('mediagallery');
            foreach ($rs as $record) {
                $types = explode(',', $record->gallerytype);
                // "1" means default to image focus.
                $focus = !empty($types) ? $types[0] : 1;
                if (empty($focus)) {
                    $focus = 1;
                }
                $record->galleryfocus = $focus;
                $DB->update_record('mediagallery', $record);
            }
            $dbman->drop_field($table, $field);
        }
        // Just need to rename the field for this one.
        $table = new xmldb_table('mediagallery_gallery');
        $field = new xmldb_field('gallerytype');
        $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1', 'exportable');
        if ($dbman->field_exists($table, $field)) {
            $dbman->rename_field($table, $field, 'galleryfocus');
        }
        upgrade_mod_savepoint(true, 2014111400, 'mediagallery');
    }
    if ($oldversion < 2014111801) {
        // Check these are added again - out of order version bumps means they
        // might've been missed.
        $table = new xmldb_table('mediagallery');
        $field = new xmldb_field('objectid');
        $field->set_attributes(XMLDB_TYPE_CHAR, '36', null, null, null, null, 'allowlikes');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        $table = new xmldb_table('mediagallery_gallery');
        $field = new xmldb_field('objectid');
        $field->set_attributes(XMLDB_TYPE_CHAR, '36', null, null, null, null, 'thumbnail');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        $table = new xmldb_table('mediagallery_item');
        $field = new xmldb_field('objectid');
        $field->set_attributes(XMLDB_TYPE_CHAR, '36', null, null, null, null, 'timecreated');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        $table = new xmldb_table('mediagallery_gallery');
        $field = new xmldb_field('mode');
        $field->set_attributes(XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, 'standard', 'objectid');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        upgrade_mod_savepoint(true, 2014111801, 'mediagallery');
    }
    if ($oldversion < 2014112400) {
        $table = new xmldb_table('mediagallery');
        $field = new xmldb_field('colltype');
        $field->set_attributes(XMLDB_TYPE_CHAR, '12', null, XMLDB_NOTNULL, null, 'peerreviewed', 'timemodified');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        upgrade_mod_savepoint(true, 2014112400, 'mediagallery');
    }
    if ($oldversion < 2014112401) {
        $table = new xmldb_table('mediagallery');
        $field = new xmldb_field('source');
        $field->set_attributes(XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, 'moodle', 'objectid');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        $table = new xmldb_table('mediagallery_gallery');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        $table = new xmldb_table('mediagallery_item');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        upgrade_mod_savepoint(true, 2014112401, 'mediagallery');
    }
    if ($oldversion < 2014112402) {
        $table = new xmldb_table('mediagallery_item');
        $field = new xmldb_field('collection');
        $field->set_attributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'publisher');
        if ($dbman->field_exists($table, $field)) {
            $dbman->rename_field($table, $field, 'reference');
        }
        upgrade_mod_savepoint(true, 2014112402, 'mediagallery');
    }
    if ($oldversion < 2014112500) {
        $table = new xmldb_table('mediagallery_item');
        $field = new xmldb_field('broadcaster');
        $field->set_attributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'publisher');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        upgrade_mod_savepoint(true, 2014112500, 'mediagallery');
    }
    if ($oldversion < 2015011600) {
        $table = new xmldb_table('mediagallery_item');
        $fields = array();
        $fields = array(new xmldb_field('extpath', XMLDB_TYPE_TEXT, null, null, null, null, null, 'objectid'), new xmldb_field('theme_id', XMLDB_TYPE_CHAR, '36', null, null, null, null, 'extpath'), new xmldb_field('copyright_video_id', XMLDB_TYPE_CHAR, '36', null, null, null, null, 'theme_id'));
        foreach ($fields as $field) {
            if (!$dbman->field_exists($table, $field)) {
                $dbman->add_field($table, $field);
            }
        }
        upgrade_mod_savepoint(true, 2015011600, 'mediagallery');
    }
    if ($oldversion < 2015020300) {
        $table = new xmldb_table('mediagallery');
        $field = new xmldb_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'name');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        upgrade_mod_savepoint(true, 2015020300, 'mediagallery');
    }
    if ($oldversion < 2015020302) {
        $table = new xmldb_table('mediagallery');
        $field = new xmldb_field('creator', XMLDB_TYPE_CHAR, '100', null, null, null, null, 'source');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        upgrade_mod_savepoint(true, 2015020302, 'mediagallery');
    }
    if ($oldversion < 2015020400) {
        $table = new xmldb_table('mediagallery_gallery');
        $field = new xmldb_field('creator', XMLDB_TYPE_CHAR, '100', null, null, null, null, 'source');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        $table = new xmldb_table('mediagallery_item');
        $field = new xmldb_field('creator', XMLDB_TYPE_CHAR, '100', null, null, null, null, 'source');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        upgrade_mod_savepoint(true, 2015020400, 'mediagallery');
    }
    if ($oldversion < 2015020401) {
        $table = new xmldb_table('mediagallery_item');
        $field = new xmldb_field('processing_status', XMLDB_TYPE_CHAR, '20', null, null, null, null, 'source');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        upgrade_mod_savepoint(true, 2015020401, 'mediagallery');
    }
    if ($oldversion < 2015020601) {
        $table = new xmldb_table('mediagallery');
        $field = new xmldb_field('mode', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, 'standard', 'objectid');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Any collections that have an objectid are in theBox.
        $sql = "UPDATE {mediagallery}\n                SET mode = 'thebox'\n                WHERE objectid IS NOT NULL AND objectid != ''";
        $DB->execute($sql);
        upgrade_mod_savepoint(true, 2015020601, 'mediagallery');
    }
    if ($oldversion < 2015021000) {
        $tables = array(new xmldb_table('mediagallery'), new xmldb_table('mediagallery_gallery'), new xmldb_table('mediagallery_item'));
        $field = new xmldb_field('agents', XMLDB_TYPE_TEXT, null, null, null, null, null, 'creator');
        foreach ($tables as $table) {
            if (!$dbman->field_exists($table, $field)) {
                $dbman->add_field($table, $field);
            }
        }
        upgrade_mod_savepoint(true, 2015021000, 'mediagallery');
    }
    if ($oldversion < 2015021101) {
        // Populate the userid field based on the original user that added the module.
        $rs = $DB->get_recordset('mediagallery');
        foreach ($rs as $record) {
            if (!empty($record->userid)) {
                continue;
            }
            $collection = new \mod_mediagallery\collection($record);
            if ($record->userid = $collection->get_userid_from_logs()) {
                $DB->update_record('mediagallery', $record);
            }
        }
        upgrade_mod_savepoint(true, 2015021101, 'mediagallery');
    }
    if ($oldversion < 2015021102) {
        // Rename the copyright field to match change in API.
        $table = new xmldb_table('mediagallery_item');
        $field = new xmldb_field('copyright_video_id', XMLDB_TYPE_CHAR, '36', null, null, null, null, 'theme_id');
        // Launch rename field summary
        if ($dbman->field_exists($table, $field)) {
            $dbman->rename_field($table, $field, 'copyright_id');
        }
        upgrade_mod_savepoint(true, 2015021102, 'mediagallery');
    }
    if ($oldversion < 2015082600) {
        // Rename the copyright field to match change in API.
        $table = new xmldb_table('mediagallery_gallery');
        $field = new xmldb_field('contributable', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'mode');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        upgrade_mod_savepoint(true, 2015082600, 'mediagallery');
    }
    return true;
}
                    break;
                case 1:
                    // instantiate
                    $galleries = $mediagallery->get_visible_galleries();
                    $gallery = reset($galleries);
                    $options['action'] = 'viewgallery';
                    $options['viewcontrols'] = 'item';
                    break;
                default:
                    // more than one, delete others
                    print_error('toomany', 'mod_mediagallery');
            }
        }
    } else {
        if ($m) {
            $mediagallery = new \mod_mediagallery\collection($m);
            $course = $DB->get_record('course', array('id' => $mediagallery->course), '*', MUST_EXIST);
            $cm = get_coursemodule_from_instance('mediagallery', $mediagallery->id, $course->id, false, MUST_EXIST);
        } else {
            print_error('missingparameter');
        }
    }
}
$context = context_module::instance($cm->id);
// Request update from theBox (does nothing if synced within the past hour).
if (!$gallery) {
    $mediagallery->sync($forcesync);
}
if ($mediagallery->was_deleted()) {
    $coursecontext = $context->get_course_context();
    $pageurl = new moodle_url('/mod/mediagallery/view.php');
 public function set_data($data)
 {
     if (!empty($data->id)) {
         $collection = new \mod_mediagallery\collection($data);
         $data->tags = $collection->get_tags();
         if ($collection->count_galleries() && $collection->is_assessable()) {
             $this->_form->hardFreeze('colltype');
         }
     }
     parent::set_data($data);
 }
/**
 * Removes an instance of the mediagallery from the database
 *
 * Given an ID of an instance of this module,
 * this function will permanently delete the instance
 * and any data that depends on it.
 *
 * @param int $id Id of the module instance
 * @return boolean Success/Failure
 */
function mediagallery_delete_instance($id)
{
    global $DB;
    if (!($mediagallery = $DB->get_record('mediagallery', array('id' => $id)))) {
        return false;
    }
    $collection = new \mod_mediagallery\collection($mediagallery);
    $collection->delete();
    return true;
}