|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00033 function workshopform_accumulative_upgrade_legacy() { 00034 global $CFG, $DB, $OUTPUT; 00035 require_once($CFG->dirroot . '/mod/workshop/db/upgradelib.php'); 00036 00037 if (!workshopform_accumulative_upgrade_legacy_needed()) { 00038 return; 00039 } 00040 00041 // get the list of all legacy workshops using this grading strategy 00042 if ($legacyworkshops = $DB->get_records('workshop_old', array('gradingstrategy' => 1), 'course,id', 'id')) { 00043 echo $OUTPUT->notification('Copying assessment forms elements', 'notifysuccess'); 00044 $legacyworkshops = array_keys($legacyworkshops); 00045 // get the list of all form elements 00046 list($workshopids, $params) = $DB->get_in_or_equal($legacyworkshops, SQL_PARAMS_NAMED); 00047 $sql = "SELECT * 00048 FROM {workshop_elements_old} 00049 WHERE workshopid $workshopids 00050 AND newid IS NULL"; 00051 $rs = $DB->get_recordset_sql($sql, $params); 00052 // prepare system (global) scales to replace the legacy in-built ones 00053 $newscaleids = workshopform_accumulative_upgrade_scales(); 00054 foreach ($rs as $old) { 00055 $new = workshopform_accumulative_upgrade_element($old, $newscaleids, $old->workshopid); 00056 $newid = $DB->insert_record('workshopform_accumulative', $new); 00057 $DB->set_field('workshop_elements_old', 'newplugin', 'accumulative', array('id' => $old->id)); 00058 $DB->set_field('workshop_elements_old', 'newid', $newid, array('id' => $old->id)); 00059 } 00060 $rs->close(); 00061 00062 // now we need to reload the legacy ids. Although we have them in $newelements after the first run, we must 00063 // refetch them from DB so that this function can be called during recovery 00064 $newelementids = workshop_upgrade_element_id_mappings('accumulative'); 00065 00066 // migrate all grades for these elements (i.e. the values that reviewers put into forms) 00067 echo $OUTPUT->notification('Copying assessment form grades', 'notifysuccess'); 00068 $sql = "SELECT * 00069 FROM {workshop_grades_old} 00070 WHERE workshopid $workshopids 00071 AND newid IS NULL"; 00072 $rs = $DB->get_recordset_sql($sql, $params); 00073 $newassessmentids = workshop_upgrade_assessment_id_mappings(); 00074 foreach ($rs as $old) { 00075 if (!isset($newassessmentids[$old->assessmentid])) { 00076 // orphaned grade - the assessment was removed but the grade remained 00077 continue; 00078 } 00079 if (!isset($newelementids[$old->workshopid]) or !isset($newelementids[$old->workshopid][$old->elementno])) { 00080 // orphaned grade - the assessment form element has been removed after the grade was recorded 00081 continue; 00082 } 00083 $new = workshopform_accumulative_upgrade_grade($old, $newassessmentids[$old->assessmentid], 00084 $newelementids[$old->workshopid][$old->elementno]); 00085 $newid = $DB->insert_record('workshop_grades', $new); 00086 $DB->set_field('workshop_grades_old', 'newplugin', 'accumulative', array('id' => $old->id)); 00087 $DB->set_field('workshop_grades_old', 'newid', $newid, array('id' => $old->id)); 00088 } 00089 $rs->close(); 00090 } 00091 } 00092 00101 function workshopform_accumulative_upgrade_element(stdclass $old, array $newscaleids, $newworkshopid) { 00102 $new = new stdclass(); 00103 $new->workshopid = $newworkshopid; 00104 $new->sort = $old->elementno; 00105 $new->description = $old->description; 00106 $new->descriptionformat = FORMAT_HTML; 00107 // calculate new grade/scale of the element 00108 if ($old->scale >= 0 and $old->scale <= 6 and isset($newscaleids[$old->scale])) { 00109 $new->grade = -$newscaleids[$old->scale]; 00110 } elseif ($old->scale == 7) { 00111 $new->grade = 10; 00112 } elseif ($old->scale == 8) { 00113 $new->grade = 20; 00114 } elseif ($old->scale == 9) { 00115 $new->grade = 100; 00116 } else { 00117 $new->grade = 0; // something is wrong 00118 } 00119 // calculate new weight of the element. Negative weights are not supported any more and 00120 // are replaced with weight = 0. Legacy workshop did not store the raw weight but the index 00121 // in the array of weights (see $WORKSHOP_EWEIGHTS in workshop 1.x) 00122 // workshop 2.0 uses integer weights only (0-16) so all previous weights are multiplied by 4. 00123 switch ($old->weight) { 00124 case 8: $new->weight = 1; break; 00125 case 9: $new->weight = 2; break; 00126 case 10: $new->weight = 3; break; 00127 case 11: $new->weight = 4; break; 00128 case 12: $new->weight = 6; break; 00129 case 13: $new->weight = 8; break; 00130 case 14: $new->weight = 16; break; 00131 default: $new->weight = 0; 00132 } 00133 return $new; 00134 } 00135 00144 function workshopform_accumulative_upgrade_grade(stdclass $old, $newassessmentid, stdclass $newdimensioninfo) { 00145 $new = new stdclass(); 00146 $new->assessmentid = $newassessmentid; 00147 $new->strategy = 'accumulative'; 00148 $new->dimensionid = $newdimensioninfo->newid; 00149 if ($newdimensioninfo->type == 'value') { 00150 $new->grade = $old->grade; 00151 } elseif ($newdimensioninfo->type == 'scale') { 00152 // in workshop 1.x, scale items are numbered starting from 0 but Moodle in-built scales start numbering from 1 00153 $new->grade = $old->grade + 1; 00154 } 00155 $new->peercomment = $old->feedback; 00156 $new->peercommentformat = FORMAT_HTML; 00157 00158 return $new; 00159 } 00160 00166 function workshopform_accumulative_upgrade_legacy_needed() { 00167 global $CFG, $DB; 00168 00169 $dbman = $DB->get_manager(); 00170 if (!($dbman->table_exists('workshop_elements_old') and $dbman->table_exists('workshop_grades_old'))) { 00171 return false; 00172 } 00173 return true; 00174 } 00175 00193 function workshopform_accumulative_upgrade_scales() { 00194 global $DB, $CFG, $USER; 00195 require_once($CFG->libdir . '/gradelib.php'); 00196 00197 $sql = 'SELECT DISTINCT scale 00198 FROM {workshop_elements_old} 00199 WHERE newplugin IS NULL'; 00200 $oldscales = $DB->get_records_sql($sql); 00201 $newscales = array(); 00202 foreach($oldscales as $oldscale => $whatever) { 00203 switch ($oldscale) { 00204 case 0: 00205 $data = new stdclass(); 00206 $data->courseid = 0; 00207 $data->userid = $USER->id; 00208 $data->name = get_string('scalename0', 'workshopform_accumulative'); 00209 $data->scale = implode(',', array(get_string('no'), get_string('yes'))); 00210 $data->description = ''; 00211 $data->descriptionformat = FORMAT_HTML; 00212 00213 $scale = new grade_scale(); 00214 grade_scale::set_properties($scale, $data); 00215 $newscales[0] = $scale->insert('mod/workshop'); 00216 break; 00217 case 1: 00218 $data = new stdclass(); 00219 $data->courseid = 0; 00220 $data->userid = $USER->id; 00221 $data->name = get_string('scalename1', 'workshopform_accumulative'); 00222 $data->scale = implode(',', array(get_string('absent', 'workshopform_accumulative'), 00223 get_string('present', 'workshopform_accumulative'))); 00224 $data->description = ''; 00225 $data->descriptionformat = FORMAT_HTML; 00226 00227 $scale = new grade_scale(); 00228 grade_scale::set_properties($scale, $data); 00229 $newscales[1] = $scale->insert('mod/workshop'); 00230 break; 00231 case 2: 00232 $data = new stdclass(); 00233 $data->courseid = 0; 00234 $data->userid = $USER->id; 00235 $data->name = get_string('scalename2', 'workshopform_accumulative'); 00236 $data->scale = implode(',', array(get_string('incorrect', 'workshopform_accumulative'), 00237 get_string('correct', 'workshopform_accumulative'))); 00238 $data->description = ''; 00239 $data->descriptionformat = FORMAT_HTML; 00240 00241 $scale = new grade_scale(); 00242 grade_scale::set_properties($scale, $data); 00243 $newscales[2] = $scale->insert('mod/workshop'); 00244 break; 00245 case 3: 00246 $data = new stdclass(); 00247 $data->courseid = 0; 00248 $data->userid = $USER->id; 00249 $data->name = get_string('scalename3', 'workshopform_accumulative'); 00250 $data->scale = implode(',', array('* ' . get_string('poor', 'workshopform_accumulative'), 00251 '**', 00252 '*** ' . get_string('good', 'workshopform_accumulative'))); 00253 $data->description = ''; 00254 $data->descriptionformat = FORMAT_HTML; 00255 00256 $scale = new grade_scale(); 00257 grade_scale::set_properties($scale, $data); 00258 $newscales[3] = $scale->insert('mod/workshop'); 00259 break; 00260 case 4: 00261 $data = new stdclass(); 00262 $data->courseid = 0; 00263 $data->userid = $USER->id; 00264 $data->name = get_string('scalename4', 'workshopform_accumulative'); 00265 $data->scale = implode(',', array('* ' . get_string('verypoor', 'workshopform_accumulative'), 00266 '**', 00267 '***', 00268 '**** ' . get_string('excellent', 'workshopform_accumulative'))); 00269 $data->description = ''; 00270 $data->descriptionformat = FORMAT_HTML; 00271 00272 $scale = new grade_scale(); 00273 grade_scale::set_properties($scale, $data); 00274 $newscales[4] = $scale->insert('mod/workshop'); 00275 break; 00276 case 5: 00277 $data = new stdclass(); 00278 $data->courseid = 0; 00279 $data->userid = $USER->id; 00280 $data->name = get_string('scalename5', 'workshopform_accumulative'); 00281 $data->scale = implode(',', array('* ' . get_string('verypoor', 'workshopform_accumulative'), 00282 '**', 00283 '***', 00284 '****', 00285 '***** ' . get_string('excellent', 'workshopform_accumulative'))); 00286 $data->description = ''; 00287 $data->descriptionformat = FORMAT_HTML; 00288 00289 $scale = new grade_scale(); 00290 grade_scale::set_properties($scale, $data); 00291 $newscales[5] = $scale->insert('mod/workshop'); 00292 break; 00293 case 6: 00294 $data = new stdclass(); 00295 $data->courseid = 0; 00296 $data->userid = $USER->id; 00297 $data->name = get_string('scalename6', 'workshopform_accumulative'); 00298 $data->scale = implode(',', array('* ' . get_string('verypoor', 'workshopform_accumulative'), 00299 '**', 00300 '***', 00301 '****', 00302 '*****', 00303 '******', 00304 '******* ' . get_string('excellent', 'workshopform_accumulative'))); 00305 $data->description = ''; 00306 $data->descriptionformat = FORMAT_HTML; 00307 00308 $scale = new grade_scale(); 00309 grade_scale::set_properties($scale, $data); 00310 $newscales[6] = $scale->insert('mod/workshop'); 00311 break; 00312 } 00313 } 00314 00315 return $newscales; 00316 }