|
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_rubric_upgrade_legacy() { 00034 00035 if (!workshopform_rubric_upgrade_legacy_needed()) { 00036 return; 00037 } 00038 workshopform_rubric_upgrade_legacy_criterion(); 00039 workshopform_rubric_upgrade_legacy_rubric(); 00040 } 00041 00045 function workshopform_rubric_upgrade_legacy_criterion() { 00046 global $CFG, $DB, $OUTPUT; 00047 require_once($CFG->dirroot . '/mod/workshop/db/upgradelib.php'); 00048 00049 // get the list of all legacy workshops using this grading strategy 00050 if ($legacyworkshops = $DB->get_records('workshop_old', array('gradingstrategy' => 3), 'course,id', 'id')) { 00051 echo $OUTPUT->notification('Copying criterion assessment form elements', 'notifysuccess'); 00052 $legacyworkshops = array_keys($legacyworkshops); 00053 // get the list of all form elements 00054 list($workshopids, $params) = $DB->get_in_or_equal($legacyworkshops, SQL_PARAMS_NAMED); 00055 $sql = "SELECT * 00056 FROM {workshop_elements_old} 00057 WHERE workshopid $workshopids 00058 AND newid IS NULL"; 00059 $rs = $DB->get_recordset_sql($sql, $params); 00060 $newdimensionids = array(); // (int)workshopid => (int)dimensionid 00061 foreach ($rs as $old) { 00062 // create rubric criterion and the configuration if necessary 00063 if (!isset($newdimensionids[$old->workshopid])) { 00064 if (!$DB->record_exists('workshopform_rubric', array('workshopid' => $old->workshopid, 'sort' => 1))) { 00065 $newdimension = new stdclass(); 00066 $newdimension->workshopid = $old->workshopid; 00067 $newdimension->sort = 1; 00068 $newdimension->description = trim(get_string('dimensionnumber', 'workshopform_rubric', '')); 00069 $newdimension->descriptionformat = FORMAT_HTML; 00070 $newdimensionids[$old->workshopid] = $DB->insert_record('workshopform_rubric', $newdimension); 00071 } else { 00072 $newdimensionids[$old->workshopid] = $DB->get_field('workshopform_rubric', 'id', 00073 array('workshopid' => $old->workshopid, 'sort' => 1)); 00074 } 00075 if (!$DB->record_exists('workshopform_rubric_config', array('workshopid' => $old->workshopid))) { 00076 $newconfig = new stdclass(); 00077 $newconfig->workshopid = $old->workshopid; 00078 $newconfig->layout = 'list'; 00079 $DB->insert_record('workshopform_rubric_config', $newconfig); 00080 } 00081 } 00082 // process the information about the criterion levels 00083 if (trim($old->description)) { 00084 $new = workshopform_rubric_upgrade_criterion_level($old, $newdimensionids[$old->workshopid]); 00085 $newid = $DB->insert_record('workshopform_rubric_levels', $new); 00086 $DB->set_field('workshop_elements_old', 'newplugin', 'rubric_levels', array('id' => $old->id)); 00087 $DB->set_field('workshop_elements_old', 'newid', $newid, array('id' => $old->id)); 00088 } 00089 } 00090 $rs->close(); 00091 00092 // reload the mappings - this must be reloaded to that we can run this during recovery 00093 $newelementids = workshop_upgrade_element_id_mappings('rubric_levels'); 00094 00095 // migrate all grades for these elements (i.e. the values that reviewers put into forms) 00096 echo $OUTPUT->notification('Copying criterion assessment form grades', 'notifysuccess'); 00097 $sql = "SELECT * 00098 FROM {workshop_grades_old} 00099 WHERE workshopid $workshopids 00100 AND elementno = 0 00101 AND newid IS NULL"; 00102 $rs = $DB->get_recordset_sql($sql, $params); 00103 $newassessmentids = workshop_upgrade_assessment_id_mappings(); 00104 $newdimensionids = $DB->get_records('workshopform_rubric_levels', array(), '', 'id,dimensionid'); 00105 foreach ($rs as $old) { 00106 if (!isset($newassessmentids[$old->assessmentid])) { 00107 // orphaned grade - the assessment was removed but the grade remained 00108 continue; 00109 } 00110 if (!isset($newelementids[$old->workshopid]) or !isset($newelementids[$old->workshopid][$old->elementno])) { 00111 // orphaned grade - the assessment form element has been removed after the grade was recorded 00112 continue; 00113 } 00114 $newlevelid = $newelementids[$old->workshopid][$old->elementno]->newid; 00115 $new = new stdclass(); 00116 $new->assessmentid = $newassessmentids[$old->assessmentid]; 00117 $new->strategy = 'rubric'; 00118 $new->dimensionid = $newdimensionids[$newlevelid]->dimensionid; 00119 $new->grade = $newelementids[$old->workshopid][$old->grade]->maxscore; 00120 $newid = $DB->insert_record('workshop_grades', $new); 00121 $DB->set_field('workshop_grades_old', 'newplugin', 'rubric', array('id' => $old->id)); 00122 $DB->set_field('workshop_grades_old', 'newid', $newid, array('id' => $old->id)); 00123 } 00124 $rs->close(); 00125 } 00126 } 00127 00131 function workshopform_rubric_upgrade_legacy_rubric() { 00132 global $CFG, $DB, $OUTPUT; 00133 require_once($CFG->dirroot . '/mod/workshop/db/upgradelib.php'); 00134 00135 // get the list of all legacy workshops using this grading strategy 00136 if ($legacyworkshops = $DB->get_records('workshop_old', array('gradingstrategy' => 4), 'course,id', 'id')) { 00137 echo $OUTPUT->notification('Copying rubric assessment form elements', 'notifysuccess'); 00138 $legacyworkshops = array_keys($legacyworkshops); 00139 // get the list of all form elements and rubrics 00140 list($workshopids, $params) = $DB->get_in_or_equal($legacyworkshops, SQL_PARAMS_NAMED); 00141 $sql = "SELECT e.id AS eid, e.workshopid AS workshopid, e.elementno AS esort, e.description AS edesc, e.weight AS eweight, 00142 r.id AS rid, r.rubricno AS rgrade, r.description AS rdesc 00143 FROM {workshop_elements_old} e 00144 LEFT JOIN {workshop_rubrics_old} r ON (r.elementno = e.elementno AND r.workshopid = e.workshopid) 00145 WHERE e.workshopid $workshopids 00146 AND e.newid IS NULL 00147 AND r.newid IS NULL 00148 ORDER BY e.workshopid, e.elementno, r.rubricno"; 00149 $rs = $DB->get_recordset_sql($sql, $params); 00150 $newdimensionids = array(); // (int)workshopid => (int)elementno => (int)dimensionid 00151 $newlevelids = array(); // (int)oldrubricid => (int)newlevelid 00152 $prevelement = null; 00153 foreach ($rs as $old) { 00154 // create rubric criterion and the configuration if necessary 00155 if (!isset($newdimensionids[$old->workshopid]) or !isset($newdimensionids[$old->workshopid][$old->esort])) { 00156 if (!$DB->record_exists('workshopform_rubric', array('workshopid' => $old->workshopid, 'sort' => $old->esort))) { 00157 $newdimension = new stdclass(); 00158 $newdimension->workshopid = $old->workshopid; 00159 $newdimension->sort = $old->esort; 00160 $newdimension->description = $old->edesc; 00161 $newdimension->descriptionformat = FORMAT_HTML; 00162 $newdimensionids[$old->workshopid][$old->esort] = $DB->insert_record('workshopform_rubric', $newdimension); 00163 } else { 00164 $newdimensionids[$old->workshopid][$old->esort] = $DB->get_field('workshopform_rubric', 'id', 00165 array('workshopid' => $old->workshopid, 'sort' => $old->esort)); 00166 } 00167 if (!$DB->record_exists('workshopform_rubric_config', array('workshopid' => $old->workshopid))) { 00168 $newconfig = new stdclass(); 00169 $newconfig->workshopid = $old->workshopid; 00170 $newconfig->layout = 'grid'; 00171 $DB->insert_record('workshopform_rubric_config', $newconfig); 00172 } 00173 } 00174 // process the information about the criterion levels 00175 if (trim($old->rdesc)) { 00176 $new = workshopform_rubric_upgrade_rubric_level($old, $newdimensionids[$old->workshopid][$old->esort]); 00177 $newid = $DB->insert_record('workshopform_rubric_levels', $new); 00178 $DB->set_field('workshop_rubrics_old', 'newplugin', 'rubric_levels', array('id' => $old->rid)); 00179 $DB->set_field('workshop_rubrics_old', 'newid', $newid, array('id' => $old->rid)); 00180 } 00181 // mark the whole element as processed if the last level was processed 00182 if ($old->rgrade == 4) { 00183 $DB->set_field('workshop_elements_old', 'newplugin', 'rubric', array('id' => $old->eid)); 00184 $DB->set_field('workshop_elements_old', 'newid', $newdimensionids[$old->workshopid][$old->esort], array('id' => $old->eid)); 00185 } 00186 } 00187 $rs->close(); 00188 00189 // reload the mappings - this must be reloaded so that we can run this during recovery 00190 $newelementids = workshop_upgrade_element_id_mappings('rubric'); 00191 00192 // load the legacy element weights and multiply the new max grade by it 00193 echo $OUTPUT->notification('Recalculating rubric assessment form element weights', 'notifysuccess'); 00194 $oldweights = $DB->get_records('workshop_elements_old', array('newplugin' => 'rubric'), '', 'id,workshopid,elementno,weight'); 00195 $newweights = array(); 00196 foreach ($oldweights as $eid => $element) { 00197 $newweights[$newelementids[$element->workshopid][$element->elementno]->newid] = workshopform_rubric_upgrade_weight($element->weight); 00198 } 00199 unset($oldweights); 00200 unset($element); 00201 00202 // migrate all grades for these elements (i.e. the values that reviewers put into forms) 00203 echo $OUTPUT->notification('Copying rubric assessment form grades', 'notifysuccess'); 00204 $sql = "SELECT * 00205 FROM {workshop_grades_old} 00206 WHERE workshopid $workshopids 00207 AND newid IS NULL"; 00208 $rs = $DB->get_recordset_sql($sql, $params); 00209 $newassessmentids = workshop_upgrade_assessment_id_mappings(); 00210 foreach ($rs as $old) { 00211 if (!isset($newelementids[$old->workshopid]) or !isset($newelementids[$old->workshopid][$old->elementno])) { 00212 // orphaned grade - the assessment form element has been removed after the grade was recorded 00213 continue; 00214 } 00215 $new = new stdclass(); 00216 $new->assessmentid = $newassessmentids[$old->assessmentid]; 00217 $new->strategy = 'rubric'; 00218 $new->dimensionid = $newelementids[$old->workshopid][$old->elementno]->newid; 00219 $new->grade = $old->grade * $newweights[$new->dimensionid]; 00220 $new->peercomment = $old->feedback; 00221 $new->peercommentformat = FORMAT_HTML; 00222 $newid = $DB->insert_record('workshop_grades', $new); 00223 $DB->set_field('workshop_grades_old', 'newplugin', 'rubric', array('id' => $old->id)); 00224 $DB->set_field('workshop_grades_old', 'newid', $newid, array('id' => $old->id)); 00225 } 00226 $rs->close(); 00227 } 00228 } 00229 00239 function workshopform_rubric_upgrade_criterion_level(stdclass $old, $newdimensionid) { 00240 $new = new stdclass(); 00241 $new->dimensionid = $newdimensionid; 00242 $new->grade = $old->maxscore; 00243 $new->definition = $old->description; 00244 $new->definitionformat = FORMAT_HTML; 00245 return $new; 00246 } 00247 00257 function workshopform_rubric_upgrade_rubric_level(stdclass $old, $newdimensionid) { 00258 $new = new stdclass(); 00259 $new->dimensionid = $newdimensionid; 00260 $new->grade = $old->rgrade * workshopform_rubric_upgrade_weight($old->eweight); 00261 $new->definition = $old->rdesc; 00262 $new->definitionformat = FORMAT_HTML; 00263 return $new; 00264 } 00265 00271 function workshopform_rubric_upgrade_legacy_needed() { 00272 global $CFG, $DB; 00273 00274 $dbman = $DB->get_manager(); 00275 if (!($dbman->table_exists('workshop_elements_old') and $dbman->table_exists('workshop_grades_old'))) { 00276 return false; 00277 } 00278 return true; 00279 } 00280 00292 function workshopform_rubric_upgrade_weight($oldweight) { 00293 00294 switch ($oldweight) { 00295 case 8: $weight = 1; break; 00296 case 9: $weight = 2; break; 00297 case 10: $weight = 3; break; 00298 case 11: $weight = 4; break; 00299 case 12: $weight = 6; break; 00300 case 13: $weight = 8; break; 00301 case 14: $weight = 16; break; 00302 default: $weight = 0; 00303 } 00304 return $weight; 00305 }