/**
  * Tests that all section options are copied when the course format is changed.
  * None of the data is copied.
  *
  * It is a future enhancement to copy;
  * 1. Only the relevant options.
  * 2. Only the data associated with relevant options.
  */
 public function test_course_format_options_restore_new_format()
 {
     global $DB, $CFG;
     $this->resetAfterTest(true);
     $this->setAdminUser();
     $CFG->enableavailability = true;
     $CFG->enablecompletion = true;
     // Create a course with some availability data set.
     $generator = $this->getDataGenerator();
     $course = $generator->create_course(array('format' => 'test_cs2_options', 'numsections' => 3, 'enablecompletion' => COMPLETION_ENABLED), array('createsections' => true));
     $newcourse = $generator->create_course(array('format' => 'test_cs_options', 'numsections' => 3, 'enablecompletion' => COMPLETION_ENABLED), array('createsections' => true));
     $courseobject = format_base::instance($course->id);
     $section = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1), '*', MUST_EXIST);
     $data = array('id' => $section->id, 'numdaystocomplete' => 2, 'secondparameter' => 8);
     $courseobject->update_section_format_options($data);
     // Backup and restore it.
     $this->backup_and_restore($course, $newcourse);
     $newcourseobject = format_base::instance($newcourse->id);
     $sectionoptions = $newcourseobject->get_format_options(1);
     $this->assertArrayHasKey('numdaystocomplete', $sectionoptions);
     $this->assertArrayHasKey('secondparameter', $sectionoptions);
     $this->assertEquals(0, $sectionoptions['numdaystocomplete']);
     $this->assertEquals(0, $sectionoptions['secondparameter']);
 }
Esempio n. 2
0
/**
 * Returns an instance of format class (extending format_base) for given course
 *
 * @param int|stdClass $courseorid either course id or
 *     an object that has the property 'format' and may contain property 'id'
 * @return format_base
 */
function course_get_format($courseorid)
{
    return format_base::instance($courseorid);
}
 /**
  * Tests that all section options are copied when the course format is changed.
  * None of the data is copied.
  *
  * It is a future enhancement to copy;
  * 1. Only the relevant options.
  * 2. Only the data associated with relevant options.
  */
 public function test_course_format_options_restore_new_format()
 {
     global $DB, $CFG;
     $this->resetAfterTest(true);
     $this->setAdminUser();
     // Create a source course using the test_cs2_options format.
     $generator = $this->getDataGenerator();
     $course = $generator->create_course(array('format' => 'test_cs2_options', 'numsections' => 3, 'enablecompletion' => COMPLETION_ENABLED), array('createsections' => true));
     // Create a target course using test_cs_options format.
     $newcourse = $generator->create_course(array('format' => 'test_cs_options', 'numsections' => 3, 'enablecompletion' => COMPLETION_ENABLED), array('createsections' => true));
     // Set section 2 to have both options, and a name.
     $courseobject = format_base::instance($course->id);
     $section = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 2), '*', MUST_EXIST);
     $data = array('id' => $section->id, 'numdaystocomplete' => 2, 'secondparameter' => 8);
     $courseobject->update_section_format_options($data);
     $DB->set_field('course_sections', 'name', 'Frogs', array('id' => $section->id));
     // Backup and restore to the new course using 'add to existing' so it
     // keeps the current (test_cs_options) format.
     $this->backup_and_restore($course, $newcourse, backup::TARGET_EXISTING_ADDING);
     // Check that the section contains the options suitable for the new
     // format and that even the one with the same name as from the old format
     // has NOT been set.
     $newcourseobject = format_base::instance($newcourse->id);
     $sectionoptions = $newcourseobject->get_format_options(2);
     $this->assertArrayHasKey('numdaystocomplete', $sectionoptions);
     $this->assertArrayNotHasKey('secondparameter', $sectionoptions);
     $this->assertEquals(0, $sectionoptions['numdaystocomplete']);
     // However, the name should have been changed, as this does not depend
     // on the format.
     $modinfo = get_fast_modinfo($newcourse->id);
     $section = $modinfo->get_section_info(2);
     $this->assertEquals('Frogs', $section->name);
 }