/**
 * 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 $workshopplus An object from the form in mod_form.php
 * @return bool success
 */
function workshopplus_update_instance(stdclass $workshopplus)
{
    global $CFG, $DB;
    require_once dirname(__FILE__) . '/locallib.php';
    $workshopplus->timemodified = time();
    $workshopplus->id = $workshopplus->instance;
    $workshopplus->useexamples = (int) (!empty($workshopplus->useexamples));
    $workshopplus->usepeerassessment = 1;
    $workshopplus->useselfassessment = (int) (!empty($workshopplus->useselfassessment));
    $workshopplus->latesubmissions = (int) (!empty($workshopplus->latesubmissions));
    $workshopplus->phaseswitchassessment = (int) (!empty($workshopplus->phaseswitchassessment));
    // todo - if the grading strategy is being changed, we may want to replace all aggregated peer grades with nulls
    $DB->update_record('workshopplus', $workshopplus);
    $context = context_module::instance($workshopplus->coursemodule);
    // process the custom wysiwyg editors
    if ($draftitemid = $workshopplus->instructauthorseditor['itemid']) {
        $workshopplus->instructauthors = file_save_draft_area_files($draftitemid, $context->id, 'mod_workshopplus', 'instructauthors', 0, workshopplus::instruction_editors_options($context), $workshopplus->instructauthorseditor['text']);
        $workshopplus->instructauthorsformat = $workshopplus->instructauthorseditor['format'];
    }
    if ($draftitemid = $workshopplus->instructreviewerseditor['itemid']) {
        $workshopplus->instructreviewers = file_save_draft_area_files($draftitemid, $context->id, 'mod_workshopplus', 'instructreviewers', 0, workshopplus::instruction_editors_options($context), $workshopplus->instructreviewerseditor['text']);
        $workshopplus->instructreviewersformat = $workshopplus->instructreviewerseditor['format'];
    }
    if ($draftitemid = $workshopplus->conclusioneditor['itemid']) {
        $workshopplus->conclusion = file_save_draft_area_files($draftitemid, $context->id, 'mod_workshopplus', 'conclusion', 0, workshopplus::instruction_editors_options($context), $workshopplus->conclusioneditor['text']);
        $workshopplus->conclusionformat = $workshopplus->conclusioneditor['format'];
    }
    // re-save the record with the replaced URLs in editor fields
    $DB->update_record('workshopplus', $workshopplus);
    // update gradebook items
    workshopplus_grade_item_update($workshopplus);
    workshopplus_grade_item_category_update($workshopplus);
    // update calendar events
    workshopplus_calendar_update($workshopplus, $workshopplus->coursemodule);
    return true;
}
/**
 * Performs upgrade of the database structure and data
 *
 * workshopplus supports upgrades from version 1.9.0 and higher only. During 1.9 > 2.0 upgrade,
 * there are significant database changes.
 *
 * @param int $oldversion the version we are upgrading from
 * @return bool result
 */
function xmldb_workshopplus_upgrade($oldversion)
{
    global $CFG, $DB, $OUTPUT;
    $dbman = $DB->get_manager();
    // Moodle v2.2.0 release upgrade line
    if ($oldversion < 2012033100) {
        // add the field 'phaseswitchassessment' to the 'workshopplus' table
        $table = new xmldb_table('workshopplus');
        $field = new xmldb_field('phaseswitchassessment', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'assessmentend');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        upgrade_mod_savepoint(true, 2012033100, 'workshopplus');
    }
    /**
     * Remove all workshopplus calendar events
     */
    if ($oldversion < 2012041700) {
        require_once $CFG->dirroot . '/calendar/lib.php';
        $events = $DB->get_records('event', array('modulename' => 'workshopplus'));
        foreach ($events as $event) {
            $event = calendar_event::load($event);
            $event->delete();
        }
        upgrade_mod_savepoint(true, 2012041700, 'workshopplus');
    }
    /**
     * Recreate all workshopplus calendar events
     */
    if ($oldversion < 2012041701) {
        require_once dirname(dirname(__FILE__)) . '/lib.php';
        $sql = "SELECT w.id, w.course, w.name, w.intro, w.introformat, w.submissionstart,\n                       w.submissionend, w.assessmentstart, w.assessmentend,\n                       cm.id AS cmid\n                  FROM {workshopplus} w\n                  JOIN {modules} m ON m.name = 'workshopplus'\n                  JOIN {course_modules} cm ON (cm.module = m.id AND cm.course = w.course AND cm.instance = w.id)";
        $rs = $DB->get_recordset_sql($sql);
        foreach ($rs as $workshopplus) {
            $cmid = $workshopplus->cmid;
            unset($workshopplus->cmid);
            workshopplus_calendar_update($workshopplus, $cmid);
        }
        $rs->close();
        upgrade_mod_savepoint(true, 2012041701, 'workshopplus');
    }
    // Moodle v2.3.0 release upgrade line
    /**
     * Add new fields conclusion and conclusionformat
     */
    if ($oldversion < 2012102400) {
        $table = new xmldb_table('workshopplus');
        $field = new xmldb_field('conclusion', XMLDB_TYPE_TEXT, null, null, null, null, null, 'phaseswitchassessment');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        $field = new xmldb_field('conclusionformat', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '1', 'conclusion');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        upgrade_mod_savepoint(true, 2012102400, 'workshopplus');
    }
    // Moodle v2.4.0 release upgrade line
    // Put any upgrade step following this
    /**
     * Add overall feedback related fields into the workshopplus table.
     */
    if ($oldversion < 2013032500) {
        $table = new xmldb_table('workshopplus');
        $field = new xmldb_field('overallfeedbackmode', XMLDB_TYPE_INTEGER, '3', null, null, null, '1', 'conclusionformat');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        $field = new xmldb_field('overallfeedbackfiles', XMLDB_TYPE_INTEGER, '3', null, null, null, '0', 'overallfeedbackmode');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        $field = new xmldb_field('overallfeedbackmaxbytes', XMLDB_TYPE_INTEGER, '10', null, null, null, '100000', 'overallfeedbackfiles');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        upgrade_mod_savepoint(true, 2013032500, 'workshopplus');
    }
    /**
     * Add feedbackauthorattachment field into the workshopplus_assessments table.
     */
    if ($oldversion < 2013032501) {
        $table = new xmldb_table('workshopplus_assessments');
        $field = new xmldb_field('feedbackauthorattachment', XMLDB_TYPE_INTEGER, '3', null, null, null, '0', 'feedbackauthorformat');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        upgrade_mod_savepoint(true, 2013032501, 'workshopplus');
    }
    // Moodle v2.5.0 release upgrade line.
    // Put any upgrade step following this.
    // Moodle v2.6.0 release upgrade line.
    // Put any upgrade step following this.
    return true;
}