/**
  * Tests the add_legacy_availability_condition function used in restore.
  */
 public function test_add_legacy_availability_condition()
 {
     // Completion condition tests.
     $rec = (object) array('sourcecmid' => 7, 'requiredcompletion' => 1);
     // No previous availability, show = true.
     $this->assertEquals('{"op":"&","showc":[true],"c":[{"type":"completion","cm":7,"e":1}]}', info::add_legacy_availability_condition(null, $rec, true));
     // No previous availability, show = false.
     $this->assertEquals('{"op":"&","showc":[false],"c":[{"type":"completion","cm":7,"e":1}]}', info::add_legacy_availability_condition(null, $rec, false));
     // Existing availability.
     $before = '{"op":"&","showc":[true],"c":[{"type":"date","d":">=","t":70}]}';
     $this->assertEquals('{"op":"&","showc":[true,true],"c":[' . '{"type":"date","d":">=","t":70},' . '{"type":"completion","cm":7,"e":1}' . ']}', info::add_legacy_availability_condition($before, $rec, true));
     // Grade condition tests.
     $rec = (object) array('gradeitemid' => 3, 'grademin' => 7, 'grademax' => null);
     $this->assertEquals('{"op":"&","showc":[true],"c":[{"type":"grade","id":3,"min":7.00000}]}', info::add_legacy_availability_condition(null, $rec, true));
     $rec->grademax = 8;
     $this->assertEquals('{"op":"&","showc":[true],"c":[{"type":"grade","id":3,"min":7.00000,"max":8.00000}]}', info::add_legacy_availability_condition(null, $rec, true));
     unset($rec->grademax);
     unset($rec->grademin);
     $this->assertEquals('{"op":"&","showc":[true],"c":[{"type":"grade","id":3}]}', info::add_legacy_availability_condition(null, $rec, true));
     // Note: There is no need to test the grade condition with show
     // true/false and existing availability, because this uses the same
     // function.
 }
Beispiel #2
0
 protected function define_execution()
 {
     global $CFG, $DB;
     // Site hasn't availability enabled
     if (empty($CFG->enableavailability)) {
         return;
     }
     // Do both modules and sections.
     foreach (array('module', 'section') as $table) {
         // Get all the availability objects to process.
         $params = array('backupid' => $this->get_restoreid(), 'itemname' => $table . '_availability');
         $rs = $DB->get_recordset('backup_ids_temp', $params, '', 'itemid, info');
         // Process availabilities, creating them if everything matches ok.
         foreach ($rs as $availrec) {
             $allmatchesok = true;
             // Get the complete legacy availability object.
             $availability = backup_controller_dbops::decode_backup_temp_info($availrec->info);
             // Note: This code used to update IDs, but that is now handled by the
             // current code (after restore) instead of this legacy code.
             // Get showavailability option.
             $thingid = $table === 'module' ? $availability->coursemoduleid : $availability->coursesectionid;
             $showrec = restore_dbops::get_backup_ids_record($this->get_restoreid(), $table . '_showavailability', $thingid);
             if (!$showrec) {
                 // Should not happen.
                 throw new coding_exception('No matching showavailability record');
             }
             $show = $showrec->info->showavailability;
             // The $availability object is now in the format used in the old
             // system. Interpret this and convert to new system.
             $currentvalue = $DB->get_field('course_' . $table . 's', 'availability', array('id' => $thingid), MUST_EXIST);
             $newvalue = \core_availability\info::add_legacy_availability_condition($currentvalue, $availability, $show);
             $DB->set_field('course_' . $table . 's', 'availability', $newvalue, array('id' => $thingid));
         }
     }
     $rs->close();
 }