function xmldb_questionnaire_upgrade($oldversion = 0) { global $CFG; $result = true; if ($oldversion < 2007120101) { $result &= questionnaire_upgrade_2007120101(); } if ($oldversion < 2007120102) { /// Change enum values to lower case for all tables using them. $enumvals = array('y', 'n'); $table = new XMLDBTable('questionnaire_question'); $field = new XMLDBField('required'); $field->setAttributes(XMLDB_TYPE_CHAR, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, false, null, 'n'); $result &= change_field_enum($table, $field); set_field('questionnaire_question', 'required', 'y', 'required', 'Y'); set_field('questionnaire_question', 'required', 'n', 'required', 'N'); $field->setAttributes(XMLDB_TYPE_CHAR, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, XMLDB_ENUM, array('y', 'n'), 'n'); $result &= change_field_enum($table, $field); $result &= change_field_default($table, $field); unset($field); $field = new XMLDBField('deleted'); $field->setAttributes(XMLDB_TYPE_CHAR, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, false, null, 'n'); $result &= change_field_enum($table, $field); set_field('questionnaire_question', 'deleted', 'y', 'deleted', 'Y'); set_field('questionnaire_question', 'deleted', 'n', 'deleted', 'N'); $field->setAttributes(XMLDB_TYPE_CHAR, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, XMLDB_ENUM, array('y', 'n'), 'n'); $result &= change_field_enum($table, $field); $result &= change_field_default($table, $field); unset($field); $field = new XMLDBField('public'); $field->setAttributes(XMLDB_TYPE_CHAR, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, false, null, 'n'); $result &= change_field_enum($table, $field); set_field('questionnaire_question', 'public', 'y', 'public', 'Y'); set_field('questionnaire_question', 'public', 'n', 'public', 'N'); $field->setAttributes(XMLDB_TYPE_CHAR, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, XMLDB_ENUM, array('y', 'n'), 'y'); $result &= change_field_enum($table, $field); $result &= change_field_default($table, $field); unset($field); unset($table); $table = new XMLDBTable('questionnaire_question_type'); $field = new XMLDBField('has_choices'); $field->setAttributes(XMLDB_TYPE_CHAR, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, false, null, 'n'); $result &= change_field_enum($table, $field); set_field('questionnaire_question_type', 'has_choices', 'y', 'has_choices', 'Y'); set_field('questionnaire_question_type', 'has_choices', 'n', 'has_choices', 'N'); $field->setAttributes(XMLDB_TYPE_CHAR, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, XMLDB_ENUM, array('y', 'n'), 'y'); $result &= change_field_enum($table, $field); $result &= change_field_default($table, $field); unset($field); unset($table); $table = new XMLDBTable('questionnaire_response'); $field = new XMLDBField('complete'); $field->setAttributes(XMLDB_TYPE_CHAR, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, false, null, 'n'); $result &= change_field_enum($table, $field); set_field('questionnaire_response', 'complete', 'y', 'complete', 'Y'); set_field('questionnaire_response', 'complete', 'n', 'complete', 'N'); $field->setAttributes(XMLDB_TYPE_CHAR, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, XMLDB_ENUM, array('y', 'n'), 'n'); $result &= change_field_enum($table, $field); $result &= change_field_default($table, $field); unset($field); unset($table); $table = new XMLDBTable('questionnaire_response_bool'); $field = new XMLDBField('choice_id'); $field->setAttributes(XMLDB_TYPE_CHAR, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, false, null, 'n'); $result &= change_field_enum($table, $field); set_field('questionnaire_response_bool', 'choice_id', 'y', 'choice_id', 'Y'); set_field('questionnaire_response_bool', 'choice_id', 'n', 'choice_id', 'N'); $field->setAttributes(XMLDB_TYPE_CHAR, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, XMLDB_ENUM, array('y', 'n'), 'y'); $result &= change_field_enum($table, $field); $result &= change_field_default($table, $field); unset($field); unset($table); $table = new XMLDBTable('questionnaire_survey'); $field = new XMLDBField('public'); $field->setAttributes(XMLDB_TYPE_CHAR, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, false, null, 'n'); $result &= change_field_enum($table, $field); set_field('questionnaire_survey', 'public', 'y', 'public', 'Y'); set_field('questionnaire_survey', 'public', 'n', 'public', 'N'); $field->setAttributes(XMLDB_TYPE_CHAR, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, XMLDB_ENUM, array('y', 'n'), 'y'); $result &= change_field_enum($table, $field); $result &= change_field_default($table, $field); unset($field); /// Upgrade question_type table with corrected 'response_table' fields. set_field('questionnaire_question_type', 'response_table', 'resp_single', 'response_table', 'response_single'); set_field('questionnaire_question_type', 'response_table', 'resp_multiple', 'response_table', 'response_multiple'); } if ($oldversion < 2008031902) { $table = new XMLDBTable('questionnaire'); $field = new XMLDBField('grade'); $field->setAttributes(XMLDB_TYPE_INTEGER, '10', false, true, false, false, null, 0, 'navigate'); $result = $result && add_field($table, $field); unset($field); unset($table); $table = new XMLDBTable('questionnaire_response'); $field = new XMLDBField('grade'); $field->setAttributes(XMLDB_TYPE_INTEGER, '10', false, true, false, false, null, 0, 'complete'); $result = $result && add_field($table, $field); } if ($oldversion < 2008031904) { $sql = "SELECT q.id, q.resp_eligible, q.resp_view, cm.id as cmid\n FROM {$CFG->prefix}questionnaire q, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m\n WHERE m.name='questionnaire' AND m.id=cm.module AND cm.instance=q.id"; if ($rs = get_recordset_sql($sql)) { $studentroleid = get_field('role', 'id', 'shortname', 'student'); $editteacherroleid = get_field('role', 'id', 'shortname', 'editingteacher'); $teacherroleid = get_field('role', 'id', 'shortname', 'teacher'); $capview = 'mod/questionnaire:view'; $capsubmit = 'mod/questionnaire:submit'; while ($questionnaire = rs_fetch_next_record($rs)) { $context = get_context_instance(CONTEXT_MODULE, $questionnaire->cmid); /// Convert questionnaires with resp_eligible = 'all' so that students & teachers have view and submit if ($questionnaire->resp_eligible == 'all') { assign_capability($capsubmit, CAP_ALLOW, $editteacherroleid, $context->id, true); assign_capability($capsubmit, CAP_ALLOW, $teacherroleid, $context->id, true); /// Convert questionnaires with resp_eligible = 'students' so that just students have view and submit } else { if ($questionnaire->resp_eligible == 'students') { /// This is the default; no changes necessary. /// Convert questionnaires with resp_eligible = 'teachers' so just teachers have view and submit } else { if ($questionnaire->resp_eligible == 'teachers') { assign_capability($capsubmit, CAP_ALLOW, $editteacherroleid, $context->id, true); assign_capability($capsubmit, CAP_ALLOW, $teacherroleid, $context->id, true); assign_capability($capview, CAP_PREVENT, $studentroleid, $context->id, true); assign_capability($capsubmit, CAP_PREVENT, $studentroleid, $context->id, true); } } } } rs_close($rs); } } if ($oldversion < 2008031905) { $table = new XMLDBTable('questionnaire_survey'); $field = new XMLDBField('changed'); $result = $result && drop_field($table, $field); } if ($oldversion < 2008031906) { $table = new XMLDBTable('questionnaire_response_rank'); $field = new XMLDBField('rank'); $field->setAttributes(XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null, null, '0', 'choice_id'); $field->setUnsigned(false); $result &= change_field_unsigned($table, $field); } if ($oldversion < 2008060401) { $table = new XMLDBTable('questionnaire_question'); $field = new XMLDBField('name'); $field->setAttributes(XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, null, 'survey_id'); $field->setNotnull(false); $result &= change_field_notnull($table, $field); } return $result; }
/** * Invoke method, every class will have its own * returns true/false on completion, setting both * errormsg and output as necessary */ function invoke() { parent::invoke(); $result = true; /// Set own core attributes $this->does_generate = ACTION_NONE; //$this->does_generate = ACTION_GENERATE_HTML; /// These are always here global $CFG, $XMLDB; /// Do the job, setting result as needed if (!data_submitted('nomatch')) { ///Basic prevention error('Wrong action call'); } /// Get parameters $dirpath = required_param('dir', PARAM_PATH); $dirpath = $CFG->dirroot . stripslashes_safe($dirpath); $tableparam = strtolower(required_param('table', PARAM_PATH)); $fieldparam = strtolower(required_param('field', PARAM_PATH)); $name = substr(trim(strtolower(optional_param('name', $fieldparam, PARAM_PATH))), 0, 30); $comment = required_param('comment', PARAM_CLEAN); $comment = trim(stripslashes_safe($comment)); $type = required_param('type', PARAM_INT); $length = strtolower(optional_param('length', NULL, PARAM_ALPHANUM)); $decimals = optional_param('decimals', NULL, PARAM_INT); $unsigned = optional_param('unsigned', false, PARAM_BOOL); $notnull = optional_param('notnull', false, PARAM_BOOL); $sequence = optional_param('sequence', false, PARAM_BOOL); $enum = optional_param('enum', false, PARAM_BOOL); $enumvalues = optional_param('enumvalues', 0, PARAM_CLEAN); $enumvalues = trim(stripslashes_safe($enumvalues)); $default = optional_param('default', NULL, PARAM_PATH); $default = trim(stripslashes_safe($default)); $editeddir =& $XMLDB->editeddirs[$dirpath]; $structure =& $editeddir->xml_file->getStructure(); $table =& $structure->getTable($tableparam); $field =& $table->getField($fieldparam); $oldhash = $field->getHash(); $errors = array(); /// To store all the errors found /// Perform some automatic asumptions if ($sequence) { $unsigned = true; $notnull = true; $enum = false; $default = NULL; } if ($type != XMLDB_TYPE_NUMBER && $type != XMLDB_TYPE_FLOAT) { $decimals = NULL; } if ($type != XMLDB_TYPE_CHAR && $type != XMLDB_TYPE_TEXT) { $enum = false; } if ($type == XMLDB_TYPE_BINARY) { $default = NULL; } if (!$enum) { $enumvalues = NULL; } if ($default === '') { $default = NULL; } /// Perform some checks /// Check empty name if (empty($name)) { $errors[] = $this->str['fieldnameempty']; } /// Check incorrect name if ($name == 'changeme') { $errors[] = $this->str['incorrectfieldname']; } /// Check duplicate name if ($fieldparam != $name && $table->getField($name)) { $errors[] = $this->str['duplicatefieldname']; } /// Integer checks if ($type == XMLDB_TYPE_INTEGER) { if (!(is_numeric($length) && !empty($length) && intval($length) == floatval($length) && $length > 0 && $length <= 20)) { $errors[] = $this->str['integerincorrectlength']; } if (!(empty($default) || is_numeric($default) && !empty($default) && intval($default) == floatval($default))) { $errors[] = $this->str['defaultincorrect']; } } /// Number checks if ($type == XMLDB_TYPE_NUMBER) { if (!(is_numeric($length) && !empty($length) && intval($length) == floatval($length) && $length > 0 && $length <= 20)) { $errors[] = $this->str['numberincorrectlength']; } if (!(empty($decimals) || is_numeric($decimals) && !empty($decimals) && intval($decimals) == floatval($decimals) && $decimals >= 0 && $decimals < $length)) { $errors[] = $this->str['numberincorrectdecimals']; } if (!(empty($default) || is_numeric($default) && !empty($default))) { $errors[] = $this->str['defaultincorrect']; } } /// Float checks if ($type == XMLDB_TYPE_FLOAT) { if (!(empty($length) || is_numeric($length) && !empty($length) && intval($length) == floatval($length) && $length > 0 && $length <= 20)) { $errors[] = $this->str['floatincorrectlength']; } if (!(empty($decimals) || is_numeric($decimals) && !empty($decimals) && intval($decimals) == floatval($decimals) && $decimals >= 0 && $decimals < $length)) { $errors[] = $this->str['floatincorrectdecimals']; } if (!(empty($default) || is_numeric($default) && !empty($default))) { $errors[] = $this->str['defaultincorrect']; } } /// Char checks if ($type == XMLDB_TYPE_CHAR) { if (!(is_numeric($length) && !empty($length) && intval($length) == floatval($length) && $length > 0 && $length <= 255)) { $errors[] = $this->str['charincorrectlength']; } if ($default !== NULL && $default !== '') { if (substr($default, 0, 1) == "'" || substr($default, -1, 1) == "'") { $errors[] = $this->str['defaultincorrect']; } } } /// Text checks if ($type == XMLDB_TYPE_TEXT) { if ($length != 'small' && $length != 'medium' && $length != 'big') { $errors[] = $this->str['textincorrectlength']; } if ($default !== NULL && $default !== '') { if (substr($default, 0, 1) == "'" || substr($default, -1, 1) == "'") { $errors[] = $this->str['defaultincorrect']; } } } /// Binary checks if ($type == XMLDB_TYPE_BINARY) { if ($length != 'small' && $length != 'medium' && $length != 'big') { $errors[] = $this->str['binaryincorrectlength']; } } /// Enum checks if ($enum) { $enumerr = false; $enumarr = explode(',', $enumvalues); $maxlength = 0; if ($enumarr) { foreach ($enumarr as $key => $enumelement) { /// Clear some spaces $enumarr[$key] = trim($enumelement); $enumelement = trim($enumelement); /// Calculate needed length $le = strlen(str_replace("'", '', $enumelement)); if ($le > $maxlength) { $maxlength = $le; } /// Skip if under error if ($enumerr) { continue; } /// Look for quoted strings if (substr($enumelement, 0, 1) != "'" || substr($enumelement, -1, 1) != "'") { $enumerr = true; } } } else { $enumerr = true; } if ($enumerr) { $errors[] = $this->str['enumvaluesincorrect']; } else { $enumvalues = $enumarr; } if ($length < $maxlength) { $errors[] = $this->str['wronglengthforenum']; } } if (!empty($errors)) { $tempfield = new XMLDBField($name); $tempfield->setType($type); $tempfield->setLength($length); $tempfield->setDecimals($decimals); $tempfield->setUnsigned($unsigned); $tempfield->setNotNull($notnull); $tempfield->setSequence($sequence); $tempfield->setEnum($enum); $tempfield->setEnumValues($enumvalues); $tempfield->setDefault($default); /// Prepare the output $site = get_site(); $navlinks = array(); $navlinks[] = array('name' => $this->str['administration'], 'link' => '../index.php', 'type' => 'misc'); $navlinks[] = array('name' => 'XMLDB', 'link' => 'index.php', 'type' => 'misc'); $navigation = build_navigation($navlinks); print_header("{$site->shortname}: XMLDB", "{$site->fullname}", $navigation); notice('<p>' . implode(', ', $errors) . '</p> <p>' . $tempfield->readableInfo(), 'index.php?action=edit_field&field=' . $field->getName() . '&table=' . $table->getName() . '&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath))); die; /// re-die :-P } /// Continue if we aren't under errors if (empty($errors)) { /// If there is one name change, do it, changing the prev and next /// atributes of the adjacent fields if ($fieldparam != $name) { $field->setName($name); if ($field->getPrevious()) { $prev =& $table->getField($field->getPrevious()); $prev->setNext($name); $prev->setChanged(true); } if ($field->getNext()) { $next =& $table->getField($field->getNext()); $next->setPrevious($name); $next->setChanged(true); } } /// Set comment $field->setComment($comment); /// Set the rest of fields $field->setType($type); $field->setLength($length); $field->setDecimals($decimals); $field->setUnsigned($unsigned); $field->setNotNull($notnull); $field->setSequence($sequence); $field->setEnum($enum); $field->setEnumValues($enumvalues); $field->setDefault($default); /// If the hash has changed from the old one, change the version /// and mark the structure as changed $field->calculateHash(true); if ($oldhash != $field->getHash()) { $field->setChanged(true); $table->setChanged(true); /// Recalculate the structure hash $structure->calculateHash(true); $structure->setVersion(userdate(time(), '%Y%m%d', 99, false)); /// Mark as changed $structure->setChanged(true); } /// Launch postaction if exists (leave this here!) if ($this->getPostAction() && $result) { return $this->launch($this->getPostAction()); } } /// Return ok if arrived here return $result; }
/** * Invoke method, every class will have its own * returns true/false on completion, setting both * errormsg and output as necessary */ function invoke() { parent::invoke(); $result = true; /// Set own core attributes $this->does_generate = ACTION_NONE; //$this->does_generate = ACTION_GENERATE_HTML; /// These are always here global $CFG, $XMLDB; /// Do the job, setting result as needed /// Get the dir containing the file $dirpath = required_param('dir', PARAM_PATH); $dirpath = $CFG->dirroot . stripslashes_safe($dirpath); /// Get the correct dirs if (!empty($XMLDB->dbdirs)) { $dbdir =& $XMLDB->dbdirs[$dirpath]; } else { return false; } if (!empty($XMLDB->editeddirs)) { $editeddir =& $XMLDB->editeddirs[$dirpath]; $structure =& $editeddir->xml_file->getStructure(); } /// ADD YOUR CODE HERE /// If the changeme table exists, just get it and continue $changeme_exists = false; if ($tables =& $structure->getTables()) { if ($table =& $structure->getTable('changeme')) { $changeme_exists = true; } } if (!$changeme_exists) { /// Lets create the table $field = new XMLDBField('id'); $field->setType(XMLDB_TYPE_INTEGER); $field->setLength(10); $field->setNotNull(true); $field->setUnsigned(true); $field->setSequence(true); $field->setLoaded(true); $field->setChanged(true); $key = new XMLDBKey('primary'); $key->setType(XMLDB_KEY_PRIMARY); $key->setFields(array('id')); $key->setLoaded(true); $key->setChanged(true); $table = new XMLDBTable('changeme'); $table->setComment('Default comment for the table, please edit me'); $table->addField($field); $table->addKey($key); /// Finally, add the whole retroffited table to the structure /// in the place specified $structure->addTable($table); } /// Launch postaction if exists (leave this here!) if ($this->getPostAction() && $result) { return $this->launch($this->getPostAction()); } /// Return ok if arrived here return $result; }