Example #1
0
 protected function process_module($data)
 {
     global $CFG, $DB;
     $data = (object) $data;
     $oldid = $data->id;
     $this->task->set_old_moduleversion($data->version);
     $data->course = $this->task->get_courseid();
     $data->module = $DB->get_field('modules', 'id', array('name' => $data->modulename));
     // Map section (first try by course_section mapping match. Useful in course and section restores)
     $data->section = $this->get_mappingid('course_section', $data->sectionid);
     if (!$data->section) {
         // mapping failed, try to get section by sectionnumber matching
         $params = array('course' => $this->get_courseid(), 'section' => $data->sectionnumber);
         $data->section = $DB->get_field('course_sections', 'id', $params);
     }
     if (!$data->section) {
         // sectionnumber failed, try to get first section in course
         $params = array('course' => $this->get_courseid());
         $data->section = $DB->get_field('course_sections', 'MIN(id)', $params);
     }
     if (!$data->section) {
         // no sections in course, create section 0 and 1 and assign module to 1
         $sectionrec = array('course' => $this->get_courseid(), 'section' => 0);
         $DB->insert_record('course_sections', $sectionrec);
         // section 0
         $sectionrec = array('course' => $this->get_courseid(), 'section' => 1);
         $data->section = $DB->insert_record('course_sections', $sectionrec);
         // section 1
     }
     $data->groupingid = $this->get_mappingid('grouping', $data->groupingid);
     // grouping
     if (!grade_verify_idnumber($data->idnumber, $this->get_courseid())) {
         // idnumber uniqueness
         $data->idnumber = '';
     }
     if (empty($CFG->enablecompletion)) {
         // completion
         $data->completion = 0;
         $data->completiongradeitemnumber = null;
         $data->completionview = 0;
         $data->completionexpected = 0;
     } else {
         $data->completionexpected = $this->apply_date_offset($data->completionexpected);
     }
     if (empty($CFG->enableavailability)) {
         $data->availability = null;
     }
     // Backups that did not include showdescription, set it to default 0
     // (this is not totally necessary as it has a db default, but just to
     // be explicit).
     if (!isset($data->showdescription)) {
         $data->showdescription = 0;
     }
     $data->instance = 0;
     // Set to 0 for now, going to create it soon (next step)
     if (empty($data->availability)) {
         // If there are legacy availablility data fields (and no new format data),
         // convert the old fields.
         $data->availability = \core_availability\info::convert_legacy_fields($data, false);
     } else {
         if (!empty($data->groupmembersonly)) {
             // There is current availability data, but it still has groupmembersonly
             // as well (2.7 backups), convert just that part.
             require_once $CFG->dirroot . '/lib/db/upgradelib.php';
             $data->availability = upgrade_group_members_only($data->groupingid, $data->availability);
         }
     }
     // course_module record ready, insert it
     $newitemid = $DB->insert_record('course_modules', $data);
     // save mapping
     $this->set_mapping('course_module', $oldid, $newitemid);
     // set the new course_module id in the task
     $this->task->set_moduleid($newitemid);
     // we can now create the context safely
     $ctxid = context_module::instance($newitemid)->id;
     // set the new context id in the task
     $this->task->set_contextid($ctxid);
     // update sequence field in course_section
     if ($sequence = $DB->get_field('course_sections', 'sequence', array('id' => $data->section))) {
         $sequence .= ',' . $newitemid;
     } else {
         $sequence = $newitemid;
     }
     $DB->set_field('course_sections', 'sequence', $sequence, array('id' => $data->section));
     // If there is the legacy showavailability data, store this for later use.
     // (This data is not present when restoring 'new' backups.)
     if (isset($data->showavailability)) {
         // Cache the showavailability flag using the backup_ids data field.
         restore_dbops::set_backup_ids_record($this->get_restoreid(), 'module_showavailability', $newitemid, 0, null, (object) array('showavailability' => $data->showavailability));
     }
 }
 /**
  * Tests the convert_legacy_fields function used in restore.
  */
 public function test_convert_legacy_fields()
 {
     // Check with no availability conditions first.
     $rec = (object) array('availablefrom' => 0, 'availableuntil' => 0, 'groupingid' => 7, 'showavailability' => 1);
     $this->assertNull(info::convert_legacy_fields($rec, false));
     // Check same list for a section.
     $this->assertEquals('{"op":"&","showc":[false],"c":[{"type":"grouping","id":7}]}', info::convert_legacy_fields($rec, true));
     // Check groupmembersonly with grouping.
     $rec->groupmembersonly = 1;
     $this->assertEquals('{"op":"&","showc":[false],"c":[{"type":"grouping","id":7}]}', info::convert_legacy_fields($rec, false));
     // Check groupmembersonly without grouping.
     $rec->groupingid = 0;
     $this->assertEquals('{"op":"&","showc":[false],"c":[{"type":"group"}]}', info::convert_legacy_fields($rec, false));
     // Check start date.
     $rec->groupmembersonly = 0;
     $rec->availablefrom = 123;
     $this->assertEquals('{"op":"&","showc":[true],"c":[{"type":"date","d":">=","t":123}]}', info::convert_legacy_fields($rec, false));
     // Start date with show = false.
     $rec->showavailability = 0;
     $this->assertEquals('{"op":"&","showc":[false],"c":[{"type":"date","d":">=","t":123}]}', info::convert_legacy_fields($rec, false));
     // End date.
     $rec->showavailability = 1;
     $rec->availablefrom = 0;
     $rec->availableuntil = 456;
     $this->assertEquals('{"op":"&","showc":[false],"c":[{"type":"date","d":"<","t":456}]}', info::convert_legacy_fields($rec, false));
     // All together now.
     $rec->groupingid = 7;
     $rec->groupmembersonly = 1;
     $rec->availablefrom = 123;
     $this->assertEquals('{"op":"&","showc":[false,true,false],"c":[' . '{"type":"grouping","id":7},' . '{"type":"date","d":">=","t":123},' . '{"type":"date","d":"<","t":456}' . ']}', info::convert_legacy_fields($rec, false));
     $this->assertEquals('{"op":"&","showc":[false,true,false],"c":[' . '{"type":"grouping","id":7},' . '{"type":"date","d":">=","t":123},' . '{"type":"date","d":"<","t":456}' . ']}', info::convert_legacy_fields($rec, false, true));
 }
 protected function process_module($data)
 {
     global $CFG, $DB;
     $data = (object) $data;
     $oldid = $data->id;
     $this->task->set_old_moduleversion($data->version);
     // Get the current course module data.
     $newitemid = $this->task->get_moduleid();
     $params = array('id' => $newitemid);
     $cmdata = $DB->get_record('course_modules', $params, '*', MUST_EXIST);
     // Group mode and Grouping.
     $cmdata->groupmode = $data->groupmode;
     $cmdata->groupingid = $this->get_mappingid('grouping', $data->groupingid);
     // Idnumber uniqueness.
     if (!grade_verify_idnumber($data->idnumber, $this->get_courseid())) {
         $data->idnumber = '';
     }
     $cmdata->idnumber = $data->idnumber;
     // Completion.
     if (!empty($CFG->enablecompletion)) {
         $cmdata->completion = $data->completion;
         $cmdata->completiongradeitemnumber = $data->completiongradeitemnumber;
         $cmdata->completionview = $data->completionview;
         $cmdata->completionexpected = $this->apply_date_offset($data->completionexpected);
     }
     // Availability.
     if (empty($CFG->enableavailability)) {
         $data->availability = null;
     }
     if (empty($data->availability)) {
         // If there are legacy availablility data fields (and no new format data),
         // convert the old fields.
         $data->availability = \core_availability\info::convert_legacy_fields($data, false);
     } else {
         if (!empty($data->groupmembersonly)) {
             // There is current availability data, but it still has groupmembersonly
             // as well (2.7 backups), convert just that part.
             require_once $CFG->dirroot . '/lib/db/upgradelib.php';
             $data->availability = upgrade_group_members_only($data->groupingid, $data->availability);
         }
     }
     $cmdata->availability = $data->availability;
     // Backups that did not include showdescription, set it to default 0
     // (this is not totally necessary as it has a db default, but just to
     // be explicit).
     if (!isset($data->showdescription)) {
         $data->showdescription = 0;
     }
     $cmdata->showdescription = $data->showdescription;
     // Course_module record ready, update it.
     $DB->update_record('course_modules', $cmdata);
     // Save mapping.
     $this->set_mapping('course_module', $oldid, $newitemid);
     // Set the new course_module id in the task.
     $this->task->set_moduleid($newitemid);
     // We can now create the context safely.
     $ctxid = context_module::instance($newitemid)->id;
     // Set the new context id in the task.
     $this->task->set_contextid($ctxid);
     // If there is the legacy showavailability data, store this for later use.
     // (This data is not present when restoring 'new' backups.)
     if (isset($cmdata->showavailability)) {
         // Cache the showavailability flag using the backup_ids data field.
         restore_dbops::set_backup_ids_record($this->get_restoreid(), 'module_showavailability', $newitemid, 0, null, (object) array('showavailability' => $cmdata->showavailability));
     }
 }