3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * Functions used by some stages in workshop db/upgrade.php
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
28 * Prepares the inital workshop 2.0 core tables
30 function workshop_upgrade_prepare_20_tables() {
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);
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);
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);
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);
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);
148 * Copies the records from workshop_old into workshop table
152 function workshop_upgrade_module_instances() {
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);
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));
169 * Given a record containing data from 1.9 workshop table, returns object containing data as should be saved in 2.0 workshop table
171 * @param stdClass $old record from 1.9 workshop table
174 function workshop_upgrade_transform_instance(stdClass $old) {
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;
192 $new->useexamples = 0;
194 $new->usepeerassessment = 1;
195 $new->useselfassessment = $old->includeself;
196 switch ($old->gradingstrategy) {
197 case 0: // 'notgraded' - renamed
198 $new->strategy = 'comments';
200 case 1: // 'accumulative'
201 $new->strategy = 'accumulative';
203 case 2: // 'errorbanded' - renamed
204 $new->strategy = 'numerrors';
206 case 3: // 'criterion' - will be migrated into 'rubric'
207 $new->strategy = 'rubric';
210 $new->strategy = 'rubric';
213 if ($old->submissionstart < $old->submissionend) {
214 $new->submissionstart = $old->submissionstart;
215 $new->submissionend = $old->submissionend;
217 if ($old->assessmentstart < $old->assessmentend) {
218 $new->assessmentstart = $old->assessmentstart;
219 $new->assessmentend = $old->assessmentend;
226 * Copies records from workshop_submissions_old into workshop_submissions. Can be called after all workshop module instances
227 * were correctly migrated and new ids are filled in workshop_old
231 function workshop_upgrade_submissions() {
234 upgrade_set_timeout();
236 // list of teachers in every workshop: array of (int)workshopid => array of (int)userid => notused
237 $workshopteachers = array();
239 $rs = $DB->get_recordset_select('workshop_submissions_old', 'newid IS NULL');
240 foreach ($rs as $old) {
241 if (!isset($workshopteachers[$old->workshopid])) {
242 $cm = get_coursemodule_from_instance('workshop', $old->workshopid, 0, false, MUST_EXIST);
243 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
244 $workshopteachers[$old->workshopid] = get_users_by_capability($context, 'mod/workshop:manage', 'u.id');
246 $new = workshop_upgrade_transform_submission($old, $old->workshopid, $workshopteachers[$old->workshopid]);
247 $newid = $DB->insert_record('workshop_submissions', $new, true, true);
248 $DB->set_field('workshop_submissions_old', 'newplugin', 'submissions', array('id' => $old->id));
249 $DB->set_field('workshop_submissions_old', 'newid', $newid, array('id' => $old->id));
255 * Given a record from 1.x workshop_submissions_old, returns data for 2.0 workshop_submissions
257 * @param stdClass $old
258 * @param int $newworkshopid new workshop id
259 * @param array $legacyteachers $userid => notused the list of legacy workshop teachers for the submission's workshop
262 function workshop_upgrade_transform_submission(stdClass $old, $newworkshopid, array $legacyteachers) {
264 $new = new stdclass(); // new submission record to be returned
265 $new->workshopid = $newworkshopid;
267 if (isset($legacyteachers[$old->userid])) {
268 // the author of the submission was teacher = had mod/workshop:manage. this is the only way how we can
269 // recognize the submission should be treated as example submission (ach jo...)
275 $new->authorid = $old->userid;
276 $new->timecreated = $old->timecreated;
277 $new->timemodified = $old->timecreated;
278 $new->title = $old->title;
279 $new->content = $old->description;
280 $new->contentformat = FORMAT_HTML;
281 $new->contenttrust = 0;
288 * Returns the list of new submission instances ids
290 * @return array (int)oldid => (int)newid
292 function workshop_upgrade_submission_id_mappings() {
295 $oldrecords = $DB->get_records('workshop_submissions_old', null, 'id', 'id,newid');
297 foreach ($oldrecords as $oldid => $oldrecord) {
298 if ($oldrecord->id and $oldrecord->newid) {
299 $newids[$oldid] = $oldrecord->newid;
306 * Returns the list of teacherweight values as were set in legacy workshop instances
308 * @return array (int)oldid => (int)teacherweight
310 function workshop_upgrade_legacy_teacher_weights() {
313 $oldrecords = $DB->get_records('workshop_old', null, 'id', 'id,teacherweight');
315 foreach ($oldrecords as $oldid => $oldrecord) {
316 if (is_null($oldrecord->teacherweight)) {
317 $weights[$oldid] = 1;
319 $weights[$oldid] = $oldrecord->teacherweight;
326 * Copies all assessments from workshop_assessments_old to workshop_assessments. Can be called after all
327 * submissions were migrated.
331 function workshop_upgrade_assessments() {
332 global $CFG, $DB, $OUTPUT;
334 upgrade_set_timeout();
336 $newsubmissionids = workshop_upgrade_submission_id_mappings();
337 $teacherweights = workshop_upgrade_legacy_teacher_weights();
339 // list of teachers in every workshop: array of (int)workshopid => array of (int)userid => notused
340 $workshopteachers = array();
342 // get the list of ids of the new example submissions
343 $examplesubmissions = $DB->get_records('workshop_submissions', array('example' => 1), '', 'id');
345 $rs = $DB->get_recordset_select('workshop_assessments_old', 'newid IS NULL');
346 foreach ($rs as $old) {
347 if (!isset($workshopteachers[$old->workshopid])) {
348 $cm = get_coursemodule_from_instance('workshop', $old->workshopid, 0, false, MUST_EXIST);
349 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
350 $workshopteachers[$old->workshopid] = get_users_by_capability($context, 'mod/workshop:manage', 'u.id');
352 $ofexample = isset($examplesubmissions[$newsubmissionids[$old->submissionid]]);
353 $new = workshop_upgrade_transform_assessment($old, $newsubmissionids[$old->submissionid],
354 $workshopteachers[$old->workshopid], $teacherweights[$old->workshopid], $ofexample);
355 $newid = $DB->insert_record('workshop_assessments', $new, true, true);
356 $DB->set_field('workshop_assessments_old', 'newplugin', 'assessments', array('id' => $old->id));
357 $DB->set_field('workshop_assessments_old', 'newid', $newid, array('id' => $old->id));
363 * Given a record from workshop_assessments_old, returns record to be stored in workshop_assessment
365 * @param stdClass $old record from workshop_assessments_old,
366 * @param int $newsubmissionid new submission id
367 * @param array $legacyteachers (int)userid => notused the list of legacy workshop teachers for the submission's workshop
368 * @param int $legacyteacherweight weight of teacher's assessment in legacy workshop
369 * @param bool $ofexample is this the assessment of an example submission?
372 function workshop_upgrade_transform_assessment(stdClass $old, $newsubmissionid, array $legacyteachers, $legacyteacherweight, $ofexample) {
374 require_once($CFG->libdir . '/gradelib.php');
376 $new = new stdclass();
377 $new->submissionid = $newsubmissionid;
378 $new->reviewerid = $old->userid;
381 // this is the assessment of an example submission
382 if (isset($legacyteachers[$old->userid])) {
383 // this is probably the reference assessment of the example submission
390 if (isset($legacyteachers[$old->userid])) {
391 $new->weight = $legacyteacherweight;
397 if ($old->grade < 0) {
398 // in workshop 1.x, this is just allocated assessment that has not been touched yet, having timecreated one year in the future :-/
399 $new->timecreated = time();
401 $new->grade = grade_floatval($old->grade);
402 if ($old->teachergraded) {
403 $new->gradinggradeover = grade_floatval($old->gradinggrade);
405 $new->gradinggrade = grade_floatval($old->gradinggrade);
407 $new->feedbackauthor = $old->generalcomment;
408 $new->feedbackauthorformat = FORMAT_HTML;
409 $new->feedbackreviewer = $old->teachercomment;
410 $new->feedbackreviewerformat = FORMAT_HTML;
411 $new->timecreated = $old->timecreated;
412 $new->timemodified = $old->timegraded;
419 * Returns the list of new assessment ids
421 * @return array (int)oldid => (int)newid
423 function workshop_upgrade_assessment_id_mappings() {
426 $oldrecords = $DB->get_records('workshop_assessments_old', null, 'id', 'id,newid');
428 foreach ($oldrecords as $oldid => $oldrecord) {
429 if ($oldrecord->id and $oldrecord->newid) {
430 $newids[$oldid] = $oldrecord->newid;
437 * Returns the list of new element (dimension) ids
439 * @param string $strategy the name of strategy subplugin that the element was migrated into
440 * @return array (int)workshopid => array (int)elementno => stdclass ->(int)newid {->(string)type} {->(int)maxscore}
442 function workshop_upgrade_element_id_mappings($strategy) {
445 $oldrecords = $DB->get_records('workshop_elements_old', array('newplugin' => $strategy),
446 'workshopid,elementno', 'id,workshopid,elementno,scale,maxscore,newid');
448 foreach ($oldrecords as $old) {
449 if (!isset($newids[$old->workshopid])) {
450 $newids[$old->workshopid] = array();
452 $info = new stdclass();
453 $info->newid = $old->newid;
454 if ($strategy == 'accumulative') {
455 if ($old->scale >= 0 and $old->scale <= 6) {
456 $info->type = 'scale';
458 $info->type = 'value';
461 if ($strategy == 'rubric_levels') {
462 $info->maxscore = $old->maxscore;
464 $newids[$old->workshopid][$old->elementno] = $info;