MDL-27734 Fixed weights of the example submission assessments
[moodle.git] / mod / workshop / db / upgradelib.php
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
18 /**
19  * Functions used by some stages in workshop db/upgrade.php
20  *
21  * @package    mod
22  * @subpackage workshop
23  * @copyright  2009 David Mudrak <david.mudrak@gmail.com>
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
27 /**
28  * Prepares the inital workshop 2.0 core tables
29  */
30 function workshop_upgrade_prepare_20_tables() {
31     global $CFG, $DB;
33     $dbman = $DB->get_manager();
35     if (!$dbman->table_exists('workshop')) {
36         $table = new xmldb_table('workshop');
37         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
38         $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
39         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
40         $table->add_field('intro', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
41         $table->add_field('introformat', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
42         $table->add_field('instructauthors', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
43         $table->add_field('instructauthorsformat', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
44         $table->add_field('instructreviewers', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
45         $table->add_field('instructreviewersformat', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
46         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
47         $table->add_field('phase', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, null, null, '0');
48         $table->add_field('useexamples', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, '0');
49         $table->add_field('usepeerassessment', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, '0');
50         $table->add_field('useselfassessment', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, '0');
51         $table->add_field('grade', XMLDB_TYPE_NUMBER, '10, 5', XMLDB_UNSIGNED, null, null, '80');
52         $table->add_field('gradinggrade', XMLDB_TYPE_NUMBER, '10, 5', XMLDB_UNSIGNED, null, null, '20');
53         $table->add_field('strategy', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null);
54         $table->add_field('gradedecimals', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, null, null, '0');
55         $table->add_field('nattachments', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, null, null, '0');
56         $table->add_field('latesubmissions', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, '0');
57         $table->add_field('maxbytes', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '100000');
58         $table->add_field('examplesmode', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, null, null, '0');
59         $table->add_field('submissionstart', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
60         $table->add_field('submissionend', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
61         $table->add_field('assessmentstart', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
62         $table->add_field('assessmentend', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
63         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
64         $table->add_key('course_fk', XMLDB_KEY_FOREIGN, array('course'), 'course', array('id'));
65         $dbman->create_table($table);
66     }
68     if (!$dbman->table_exists('workshop_submissions')) {
69         $table = new xmldb_table('workshop_submissions');
70         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
71         $table->add_field('workshopid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
72         $table->add_field('example', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, '0');
73         $table->add_field('authorid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
74         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
75         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
76         $table->add_field('title', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
77         $table->add_field('content', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
78         $table->add_field('contentformat', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
79         $table->add_field('contenttrust', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
80         $table->add_field('attachment', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, '0');
81         $table->add_field('grade', XMLDB_TYPE_NUMBER, '10, 5', XMLDB_UNSIGNED, null, null, null);
82         $table->add_field('gradeover', XMLDB_TYPE_NUMBER, '10, 5', XMLDB_UNSIGNED, null, null, null);
83         $table->add_field('gradeoverby', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
84         $table->add_field('feedbackauthor', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
85         $table->add_field('feedbackauthorformat', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, null, null, '0');
86         $table->add_field('timegraded', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
87         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
88         $table->add_key('workshop_fk', XMLDB_KEY_FOREIGN, array('workshopid'), 'workshop', array('id'));
89         $table->add_key('overriddenby_fk', XMLDB_KEY_FOREIGN, array('gradeoverby'), 'user', array('id'));
90         $table->add_key('author_fk', XMLDB_KEY_FOREIGN, array('authorid'), 'user', array('id'));
91         $dbman->create_table($table);
92     }
94     if (!$dbman->table_exists('workshop_assessments')) {
95         $table = new xmldb_table('workshop_assessments');
96         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
97         $table->add_field('submissionid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
98         $table->add_field('reviewerid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
99         $table->add_field('weight', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
100         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
101         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
102         $table->add_field('grade', XMLDB_TYPE_NUMBER, '10, 5', XMLDB_UNSIGNED, null, null, null);
103         $table->add_field('gradinggrade', XMLDB_TYPE_NUMBER, '10, 5', XMLDB_UNSIGNED, null, null, null);
104         $table->add_field('gradinggradeover', XMLDB_TYPE_NUMBER, '10, 5', XMLDB_UNSIGNED, null, null, null);
105         $table->add_field('gradinggradeoverby', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
106         $table->add_field('feedbackauthor', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
107         $table->add_field('feedbackauthorformat', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, null, null, '0');
108         $table->add_field('feedbackreviewer', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
109         $table->add_field('feedbackreviewerformat', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, null, null, '0');
110         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
111         $table->add_key('submission_fk', XMLDB_KEY_FOREIGN, array('submissionid'), 'workshop_submissions', array('id'));
112         $table->add_key('overriddenby_fk', XMLDB_KEY_FOREIGN, array('gradinggradeoverby'), 'user', array('id'));
113         $table->add_key('reviewer_fk', XMLDB_KEY_FOREIGN, array('reviewerid'), 'user', array('id'));
114         $dbman->create_table($table);
115     }
117     if (!$dbman->table_exists('workshop_grades')) {
118         $table = new xmldb_table('workshop_grades');
119         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
120         $table->add_field('assessmentid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
121         $table->add_field('strategy', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null);
122         $table->add_field('dimensionid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
123         $table->add_field('grade', XMLDB_TYPE_NUMBER, '10, 5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
124         $table->add_field('peercomment', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
125         $table->add_field('peercommentformat', XMLDB_TYPE_INTEGER, '3', null, null, null, '0');
126         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
127         $table->add_key('assessment_fk', XMLDB_KEY_FOREIGN, array('assessmentid'), 'workshop_assessments', array('id'));
128         $table->add_key('formfield_uk', XMLDB_KEY_UNIQUE, array('assessmentid', 'strategy', 'dimensionid'));
129         $dbman->create_table($table);
130     }
132     if (!$dbman->table_exists('workshop_aggregations')) {
133         $table = new xmldb_table('workshop_aggregations');
134         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
135         $table->add_field('workshopid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
136         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
137         $table->add_field('gradinggrade', XMLDB_TYPE_NUMBER, '10, 5', XMLDB_UNSIGNED, null, null, null);
138         $table->add_field('timegraded', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
139         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
140         $table->add_key('workshop_fk', XMLDB_KEY_FOREIGN, array('workshopid'), 'workshop', array('id'));
141         $table->add_key('user_fk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
142         $table->add_key('workshopuser', XMLDB_KEY_UNIQUE, array('workshopid', 'userid'));
143         $dbman->create_table($table);
144     }
147 /**
148  * Copies the records from workshop_old into workshop table
149  *
150  * @return void
151  */
152 function workshop_upgrade_module_instances() {
153     global $CFG, $DB;
155     upgrade_set_timeout();
156     $moduleid = $DB->get_field('modules', 'id', array('name' => 'workshop'), MUST_EXIST);
157     $rs = $DB->get_recordset_select('workshop_old', 'newid IS NULL', null, 'id');
158     foreach ($rs as $old) {
159         $new = workshop_upgrade_transform_instance($old);
160         $new->id = $old->id;
161         $DB->import_record('workshop', $new);
162         $DB->set_field('workshop_old', 'newplugin', 'workshop', array('id' => $old->id));
163         $DB->set_field('workshop_old', 'newid', $new->id, array('id' => $old->id));
164     }
165     $rs->close();
168 /**
169  * Given a record containing data from 1.9 workshop table, returns object containing data as should be saved in 2.0 workshop table
170  *
171  * @param stdClass $old record from 1.9 workshop table
172  * @return stdClass
173  */
174 function workshop_upgrade_transform_instance(stdClass $old) {
175     global $CFG;
176     require_once(dirname(dirname(__FILE__)) . '/locallib.php');
178     $new                = new stdClass();
179     $new->course        = $old->course;
180     $new->name          = $old->name;
181     $new->intro         = $old->description;
182     $new->introformat   = $old->format;
183     $new->nattachments  = $old->nattachments;
184     $new->maxbytes      = $old->maxbytes;
185     $new->grade         = $old->grade;
186     $new->gradinggrade  = $old->gradinggrade;
187     $new->phase         = workshop::PHASE_CLOSED;
188     $new->timemodified  = time();
189     if ($old->ntassessments > 0) {
190         $new->useexamples = 1;
191     } else {
192         $new->useexamples = 0;
193     }
194     $new->usepeerassessment = 1;
195     $new->useselfassessment = $old->includeself;
196     switch ($old->gradingstrategy) {
197     case 0: // 'notgraded' - renamed
198         $new->strategy = 'comments';
199         break;
200     case 1: // 'accumulative'
201         $new->strategy = 'accumulative';
202         break;
203     case 2: // 'errorbanded' - renamed
204         $new->strategy = 'numerrors';
205         break;
206     case 3: // 'criterion' - will be migrated into 'rubric'
207         $new->strategy = 'rubric';
208         break;
209     case 4: // 'rubric'
210         $new->strategy = 'rubric';
211         break;
212     }
214     return $new;
217 /**
218  * Copies records from workshop_submissions_old into workshop_submissions. Can be called after all workshop module instances
219  * were correctly migrated and new ids are filled in workshop_old
220  *
221  * @return void
222  */
223 function workshop_upgrade_submissions() {
224     global $CFG, $DB;
226     upgrade_set_timeout();
228     // list of teachers in every workshop: array of (int)workshopid => array of (int)userid => notused
229     $workshopteachers = array();
231     $rs = $DB->get_recordset_select('workshop_submissions_old', 'newid IS NULL');
232     foreach ($rs as $old) {
233         if (!isset($workshopteachers[$old->workshopid])) {
234             $cm = get_coursemodule_from_instance('workshop', $old->workshopid, 0, false, MUST_EXIST);
235             $context = get_context_instance(CONTEXT_MODULE, $cm->id);
236             $workshopteachers[$old->workshopid] = get_users_by_capability($context, 'mod/workshop:manage', 'u.id');
237         }
238         $new = workshop_upgrade_transform_submission($old, $old->workshopid, $workshopteachers[$old->workshopid]);
239         $newid = $DB->insert_record('workshop_submissions', $new, true, true);
240         $DB->set_field('workshop_submissions_old', 'newplugin', 'submissions', array('id' => $old->id));
241         $DB->set_field('workshop_submissions_old', 'newid', $newid, array('id' => $old->id));
242     }
243     $rs->close();
246 /**
247  * Given a record from 1.x workshop_submissions_old, returns data for 2.0 workshop_submissions
248  *
249  * @param stdClass $old
250  * @param int $newworkshopid new workshop id
251  * @param array $legacyteachers $userid => notused the list of legacy workshop teachers for the submission's workshop
252  * @return stdClass
253  */
254 function workshop_upgrade_transform_submission(stdClass $old, $newworkshopid, array $legacyteachers) {
256     $new                = new stdclass(); // new submission record to be returned
257     $new->workshopid    = $newworkshopid;
259     if (isset($legacyteachers[$old->userid])) {
260         // the author of the submission was teacher = had mod/workshop:manage. this is the only way how we can
261         // recognize the submission should be treated as example submission (ach jo...)
262         $new->example   = 1;
263     } else {
264         $new->example   = 0;
265     }
267     $new->authorid      = $old->userid;
268     $new->timecreated   = $old->timecreated;
269     $new->timemodified  = $old->timecreated;
270     $new->title         = $old->title;
271     $new->content       = $old->description;
272     $new->contentformat = FORMAT_HTML;
273     $new->contenttrust  = 0;
274     $new->published     = 0;
276     return $new;
279 /**
280  * Returns the list of new submission instances ids
281  *
282  * @return array (int)oldid => (int)newid
283  */
284 function workshop_upgrade_submission_id_mappings() {
285     global $DB;
287     $oldrecords = $DB->get_records('workshop_submissions_old', null, 'id', 'id,newid');
288     $newids = array();
289     foreach ($oldrecords as $oldid => $oldrecord) {
290         if ($oldrecord->id and $oldrecord->newid) {
291             $newids[$oldid] = $oldrecord->newid;
292         }
293     }
294     return $newids;
297 /**
298  * Returns the list of teacherweight values as were set in legacy workshop instances
299  *
300  * @return array (int)oldid => (int)teacherweight
301  */
302 function workshop_upgrade_legacy_teacher_weights() {
303     global $DB;
305     $oldrecords = $DB->get_records('workshop_old', null, 'id', 'id,teacherweight');
306     $weights = array();
307     foreach ($oldrecords as $oldid => $oldrecord) {
308         if (is_null($oldrecord->teacherweight)) {
309             $weights[$oldid] = 1;
310         } else {
311             $weights[$oldid] = $oldrecord->teacherweight;
312         }
313     }
314     return $weights;
317 /**
318  * Copies all assessments from workshop_assessments_old to workshop_assessments. Can be called after all
319  * submissions were migrated.
320  *
321  * @return void
322  */
323 function workshop_upgrade_assessments() {
324     global $CFG, $DB, $OUTPUT;
326     upgrade_set_timeout();
328     $newsubmissionids   = workshop_upgrade_submission_id_mappings();
329     $teacherweights     = workshop_upgrade_legacy_teacher_weights();
331     // list of teachers in every workshop: array of (int)workshopid => array of (int)userid => notused
332     $workshopteachers   = array();
334     // get the list of ids of the new example submissions
335     $examplesubmissions = $DB->get_records('workshop_submissions', array('example' => 1), '', 'id');
337     $rs = $DB->get_recordset_select('workshop_assessments_old', 'newid IS NULL');
338     foreach ($rs as $old) {
339         if (!isset($workshopteachers[$old->workshopid])) {
340             $cm = get_coursemodule_from_instance('workshop', $old->workshopid, 0, false, MUST_EXIST);
341             $context = get_context_instance(CONTEXT_MODULE, $cm->id);
342             $workshopteachers[$old->workshopid] = get_users_by_capability($context, 'mod/workshop:manage', 'u.id');
343         }
344         $ofexample = isset($examplesubmissions[$newsubmissionids[$old->submissionid]]);
345         $new = workshop_upgrade_transform_assessment($old, $newsubmissionids[$old->submissionid],
346                                                      $workshopteachers[$old->workshopid], $teacherweights[$old->workshopid], $ofexample);
347         $newid = $DB->insert_record('workshop_assessments', $new, true, true);
348         $DB->set_field('workshop_assessments_old', 'newplugin', 'assessments', array('id' => $old->id));
349         $DB->set_field('workshop_assessments_old', 'newid', $newid, array('id' => $old->id));
350     }
351     $rs->close();
354 /**
355  * Given a record from workshop_assessments_old, returns record to be stored in workshop_assessment
356  *
357  * @param stdClass $old                 record from workshop_assessments_old,
358  * @param int      $newsubmissionid     new submission id
359  * @param array    $legacyteachers      (int)userid => notused the list of legacy workshop teachers for the submission's workshop
360  * @param int      $legacyteacherweight weight of teacher's assessment in legacy workshop
361  * @param bool     $ofexample           is this the assessment of an example submission?
362  * @return stdClass
363  */
364 function workshop_upgrade_transform_assessment(stdClass $old, $newsubmissionid, array $legacyteachers, $legacyteacherweight, $ofexample) {
365     global $CFG;
366     require_once($CFG->libdir . '/gradelib.php');
368     $new                            = new stdclass();
369     $new->submissionid              = $newsubmissionid;
370     $new->reviewerid                = $old->userid;
372     if ($ofexample) {
373         // this is the assessment of an example submission
374         if (isset($legacyteachers[$old->userid])) {
375             // this is probably the reference assessment of the example submission
376             $new->weight            = 1;
377         } else {
378             $new->weight            = 0;
379         }
381     } else {
382         if (isset($legacyteachers[$old->userid])) {
383             $new->weight            = $legacyteacherweight;
384         } else {
385             $new->weight            = 1;
386         }
387     }
389     if ($old->grade < 0) {
390         // in workshop 1.x, this is just allocated assessment that has not been touched yet, having timecreated one year in the future :-/
391         $new->timecreated           = time();
392     } else {
393         $new->grade                 = grade_floatval($old->grade);
394         if ($old->teachergraded) {
395             $new->gradinggradeover  = grade_floatval($old->gradinggrade);
396         } else {
397             $new->gradinggrade      = grade_floatval($old->gradinggrade);
398         }
399         $new->feedbackauthor        = $old->generalcomment;
400         $new->feedbackauthorformat  = FORMAT_HTML;
401         $new->feedbackreviewer      = $old->teachercomment;
402         $new->feedbackreviewerformat = FORMAT_HTML;
403         $new->timecreated           = $old->timecreated;
404         $new->timemodified          = $old->timegraded;
405     }
407     return $new;
410 /**
411  * Returns the list of new assessment ids
412  *
413  * @return array (int)oldid => (int)newid
414  */
415 function workshop_upgrade_assessment_id_mappings() {
416     global $DB;
418     $oldrecords = $DB->get_records('workshop_assessments_old', null, 'id', 'id,newid');
419     $newids = array();
420     foreach ($oldrecords as $oldid => $oldrecord) {
421         if ($oldrecord->id and $oldrecord->newid) {
422             $newids[$oldid] = $oldrecord->newid;
423         }
424     }
425     return $newids;
428 /**
429  * Returns the list of new element (dimension) ids
430  *
431  * @param string $strategy the name of strategy subplugin that the element was migrated into
432  * @return array (int)workshopid => array (int)elementno => stdclass ->(int)newid {->(string)type} {->(int)maxscore}
433  */
434 function workshop_upgrade_element_id_mappings($strategy) {
435     global $DB;
437     $oldrecords = $DB->get_records('workshop_elements_old', array('newplugin' => $strategy),
438                                    'workshopid,elementno', 'id,workshopid,elementno,scale,maxscore,newid');
439     $newids = array();
440     foreach ($oldrecords as $old) {
441         if (!isset($newids[$old->workshopid])) {
442             $newids[$old->workshopid] = array();
443         }
444         $info = new stdclass();
445         $info->newid = $old->newid;
446         if ($strategy == 'accumulative') {
447             if ($old->scale >= 0 and $old->scale <= 6) {
448                 $info->type = 'scale';
449             } else {
450                 $info->type = 'value';
451             }
452         }
453         if ($strategy == 'rubric_levels') {
454             $info->maxscore = $old->maxscore;
455         }
456         $newids[$old->workshopid][$old->elementno] = $info;
457     }
458     return $newids;