/**
 * Updates an instance of the grouptool in the database
 *
 * Given an object containing all the necessary data,
 * (defined by the form in mod_form.php) this function
 * will update an existing instance with new data.
 *
 * @param stdClass $grouptool An object from the form in mod_form.php
 * @param mod_grouptool_mod_form $mform
 * @return boolean Success/Fail
 */
function grouptool_update_instance(stdClass $grouptool, mod_grouptool_mod_form $mform = null)
{
    global $DB, $CFG;
    $grouptool->timemodified = time();
    $grouptool->id = $grouptool->instance;
    $cmid = $grouptool->coursemodule;
    if (!isset($grouptool->use_size)) {
        $grouptool->use_size = 0;
    }
    if (!isset($grouptool->use_individual)) {
        $grouptool->use_individual = 0;
    }
    if (!isset($grouptool->use_queue)) {
        $queues = $DB->count_records_sql("SELECT COUNT(DISTINCT queues.id)\n                                            FROM {grouptool_agrps} agrps\n                                       LEFT JOIN {grouptool_queued} queues ON queues.agrpid = agrps.id\n                                           WHERE agrps.grouptoolid = ?", array($grouptool->instance));
        if (!empty($queues)) {
            $grouptool->use_queue = 1;
        } else {
            $grouptool->use_queue = 0;
        }
    }
    if (!isset($grouptool->allow_multiple)) {
        $grouptool->allow_multiple = 0;
    }
    $grouptool->grpsize = clean_param($grouptool->grpsize, PARAM_INT);
    $grouptool->choose_min = clean_param($grouptool->choose_min, PARAM_INT);
    $grouptool->choose_max = clean_param($grouptool->choose_max, PARAM_INT);
    // Register students if immediate registration has been turned on!
    if ($grouptool->immediate_reg) {
        require_once $CFG->dirroot . '/mod/grouptool/locallib.php';
        $instance = new mod_grouptool($grouptool->coursemodule, $grouptool);
        $instance->push_registrations();
    }
    require_once $CFG->dirroot . '/calendar/lib.php';
    $event = new stdClass();
    if ($grouptool->allow_reg) {
        $event->name = get_string('registration_period_start', 'grouptool') . ' ' . $grouptool->name;
    } else {
        $event->name = $grouptool->name . ' ' . get_string('availabledate', 'grouptool');
    }
    $event->description = format_module_intro('grouptool', $grouptool, $grouptool->coursemodule);
    if (!empty($grouptool->timeavailable)) {
        $event->timestart = $grouptool->timeavailable;
    } else {
        $grouptool->timecreated = $DB->get_field('grouptool', 'timecreated', array('id' => $grouptool->id));
        $event->timestart = $grouptool->timecreated;
    }
    $event->visible = instance_is_visible('grouptool', $grouptool);
    $event->timeduration = 0;
    if ($event->id = $DB->get_field('event', 'id', array('modulename' => 'grouptool', 'instance' => $grouptool->id, 'eventtype' => 'availablefrom'))) {
        $calendarevent = calendar_event::load($event->id);
        $calendarevent->update($event, false);
    } else {
        $event->courseid = $grouptool->course;
        $event->groupid = 0;
        $event->userid = 0;
        $event->modulename = 'grouptool';
        $event->instance = $grouptool->id;
        /*
         *  For activity module's events, this can be used to set the alternative text of the
         *  event icon. Set it to 'pluginname' unless you have a better string.
         */
        $event->eventtype = 'availablefrom';
        calendar_event::create($event);
    }
    if ($grouptool->timedue != 0) {
        unset($event->id);
        unset($calendarevent);
        if ($grouptool->allow_reg) {
            $event->name = get_string('registration_period_end', 'grouptool') . ' ' . $grouptool->name;
        } else {
            $event->name = $grouptool->name . ' ' . get_string('duedate', 'grouptool');
        }
        $event->timestart = $grouptool->timedue;
        $event->eventtype = 'deadline';
        /*
         *  For activity module's events, this can be used to set the alternative text of the
         *  event icon. Set it to 'pluginname' unless you have a better string.
         */
        if ($event->id = $DB->get_field('event', 'id', array('modulename' => 'grouptool', 'instance' => $grouptool->id, 'eventtype' => 'due'))) {
            $calendarevent = calendar_event::load($event->id);
            $calendarevent->update($event, false);
        } else {
            unset($event->id);
            $event->courseid = $grouptool->course;
            // We've got some permission issues with calendar_event::create() so we work around that!
            $calev = new calendar_event($event);
            $calev->update($event, false);
        }
    } else {
        if ($event->id = $DB->get_field('event', 'id', array('modulename' => 'grouptool', 'instance' => $grouptool->id, 'eventtype' => 'due'))) {
            $calendarevent = calendar_event::load($event->id);
            $calendarevent->delete(true);
        }
    }
    $coursegroups = $DB->get_fieldset_select('groups', 'id', 'courseid = ?', array($grouptool->course));
    foreach ($coursegroups as $groupid) {
        if (!$DB->record_exists('grouptool_agrps', array('grouptoolid' => $grouptool->instance, 'groupid' => $groupid))) {
            $record = new stdClass();
            $record->grouptoolid = $grouptool->instance;
            $record->groupid = $groupid;
            $record->sort_order = 9999999;
            $record->grpsize = $grouptool->grpsize;
            $record->active = 0;
            $DB->insert_record('grouptool_agrps', $record);
        }
    }
    // We have to override the functions fetching of data, because it's not updated yet!
    grouptool_update_queues($grouptool);
    return $DB->update_record('grouptool', $grouptool);
}
 /**
  * Update active group settings for this instance
  *
  * @param stdClass $grouplist List of groups as returned by sortlist-Element
  * @param int $grouptoolid optinoal ID of the instance to update for
  * @return true if successfull
  */
 private function update_active_groups($grouplist, $grouptoolid = null)
 {
     global $DB, $PAGE;
     require_capability('mod/grouptool:create_groups', $this->context);
     if ($grouptoolid == null) {
         $grouptoolid = $this->grouptool->id;
     }
     if (!empty($grouplist) && is_array($grouplist)) {
         $agrpids = $DB->get_records('grouptool_agrps', array('grouptoolid' => $grouptoolid), '', 'groupid, id');
         // Update grouptools additional group-data!
         foreach ($grouplist as $groupid => $groupdata) {
             $dataobj = new stdClass();
             $dataobj->grouptoolid = $grouptoolid;
             $dataobj->groupid = $groupid;
             $dataobj->sort_order = $groupdata['sort_order'];
             if (isset($groupdata['grpsize'])) {
                 $dataobj->grpsize = $groupdata['grpsize'];
             }
             $dataobj->active = $groupdata['active'];
             if (key_exists($groupid, $agrpids)) {
                 $dataobj->id = $agrpids[$groupid]->id;
                 $DB->update_record('grouptool_agrps', $dataobj);
             } else {
                 $dataobj->id = $DB->insert_record('grouptool_agrps', $dataobj, true);
             }
         }
         grouptool_update_queues($this->grouptool);
         /* Trigger event */
         \mod_grouptool\event\agrps_updated::create_convenient($this->cm, $this->grouptool)->trigger();
     }
     return true;
 }