Well, as a training, before entering into the Nwiki refactoring project, my project leader asked me for repairing an nwiki bug: http://moodle.org/mod/forum/discuss.php?d=107332
This bug is located (into the module directory of nwiki) into db/upgrade.php. The bug is simply understandable: From the previous version of Nwiki to the new version of Nwiki the "wiki" table changed a bit. One of these changes was modifying the type of 1 field from String to Integer (the evaluation field). The previous developer programmed the type change inside out (with a DML function called change_type_field()). However there is a problem. If the table/field is empty, there isn't any problem. But if the evaluation field was filled previously with an string value, the conversion from string to integer cannot be done, and the upgrade fails.
My task is repair this undesired behavior. The way to do this was:
- Create a new auxiliar field for the evaluation field in the wiki table, in order to store there the integer value of the string.
- Once this auxiliar field is filled for all table tuples, erase the evaluation field and create a new one with type integer. Once this is done, dump all auxiliar field data to this new evaluation field and erase the auxiliar field.
Having DML and DDL Moodle libraries (a libraries used to update-query-manipulate moodle database) it shouldn't be too much difficult. Actually, it isn't. However I needed to use some special update SQL instruction with a WHERE clause, and I've not found any Moodle function to do such a thing, so I've done it with a function called execute_sql(SQL sentence), where you put as parameter an SQL query, and this query will be executed to the database.
What's the matter? All DBMS does not have the same SQL syntax. Functions provided by Moodle DML libraries solves this problem, but as I said before, I haven't found any upgrade function to use with a WHERE clause.
I hope Pigui will give me some assistance. But all my work will be thrown away with Nwiki refactoring, so Pigui will not spend too much time on this. And for me, I've seen I'm capable of doing it and repair bugs with work. I think I'm ready to do whatever task could be assigned to me.
This afternoon all scolarship holders will have a meeting to discuss the methodology we should follow for developing nwiki to fit with the new Moodle 2.0 architecture (probably available in early 2009). This afternoon I'm going to write a summary of the main clues of my meeting.
I post the code I made in order to repair the bug (it is incomplete and fails, but the main idea is clearly visible):
$table = new XMLDBTable('wiki');
$field = new XMLDBField('evaluation');
$field->setAttributes(XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'studentdiscussion');
$auxfield = new XMLDBField('evaluation2');
$auxfield->setAttributes(XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'studentdiscussion');
add_field($table, $auxfield);
/*Substitution*/
$evaluation_values = get_records('wiki','','','','id,evaluation');
foreach ($evaluation_values as $key => $single_value) {
//TODO: let's substitute the evaluation string for the integer value.
//To pigui: Falten el valor dels strings
if ($single_value->evaluation == '') {
$a = 0;
}
else if ($single_value->evaluation == '') {
$a = 1;
}
else {
$a = 2;
}
$quer = 'UPDATE '.$CFG->prefix.'wiki SET evaluation2 = '.$a.' WHERE id = '.$single_value->id;
execute_sql($quer);
}
//Now, drop table evaluation, and recreate it as an Integer field
drop_field($table,$field);
add_field($table, $field);
//Dump all evaluation2 field into new evaluation field
$quer = 'UPDATE '.$CFG->prefix.'wiki SET evaluation = evaluation2';
execute_sql($quer);
//Drop aux field
drop_field($table,$auxfield);