public function test_course_completion() { global $DB; $this->resetAfterTest(); // Enable avaibility. // If not enabled all conditional fields will be ignored. set_config('enableavailability', 1); // Enable course completion. // If not enabled all completion settings will be ignored. set_config('enablecompletion', COMPLETION_ENABLED); $generator = $this->getDataGenerator(); // Create course with completion tracking enabled. $course = $generator->create_course(['enablecompletion' => 1, 'numsections' => 3], ['createsections' => true]); // Enrol user to completion tracking course. $sturole = $DB->get_record('role', array('shortname' => 'student')); $generator->enrol_user($this->user1->id, $course->id, $sturole->id); // Create page with completion marked on view. $page1 = $generator->create_module('page', array('course' => $course->id, 'name' => 'page1 complete on view'), array('completion' => 2, 'completionview' => 1)); $modinfo = get_fast_modinfo($course); $page1cm = $modinfo->get_cm($page1->cmid); // Create page restricted to only show when first page is viewed. $moduleinfo = (object) []; $moduleinfo->course = $course->id; $moduleinfo->name = 'page2 available after page1 viewed'; $moduleinfo->availability = json_encode(\core_availability\tree::get_root_json([\availability_completion\condition::get_json($page1->cmid, COMPLETION_COMPLETE)], '&')); $page2 = $generator->create_module('page', $moduleinfo); // Make section 2 restricted to only show when first page is viewed. $section = $modinfo->get_section_info(2); $sectionupdate = ['id' => $section->id, 'availability' => json_encode(\core_availability\tree::get_root_json([\availability_completion\condition::get_json($page1->cmid, COMPLETION_COMPLETE)], '&'))]; $DB->update_record('course_sections', $sectionupdate); // Check user1 has expected unavailable section and mod. $this->setUser($this->user1); // Dump cache and reget modinfo. get_fast_modinfo($course, 0, true); $modinfo = get_fast_modinfo($course); $page2cm = $modinfo->get_cm($page2->cmid); list($previouslyunavailablesections, $previouslyunavailablemods) = local::conditionally_unavailable_elements($course); $this->assertContains(2, $previouslyunavailablesections); $this->assertContains($page2cm->id, $previouslyunavailablemods); // View page1 to trigger completion $context = context_module::instance($page1->cmid); page_view($page1, $course, $page1cm, $context); $completion = new completion_info($course); $completiondata = $completion->get_data($page1cm); $this->assertEquals(COMPLETION_COMPLETE, $completiondata->completionstate); get_fast_modinfo($course, 0, true); // Reset modinfo. // Make sure that unavailable sections and mods no longer contain the ones requiring availabililty criteria // satisfying. list($unavailablesections, $unavailablemods) = local::conditionally_unavailable_elements($course); $this->assertNotContains($page2cm->id, $unavailablemods); $this->assertNotContains(2, $unavailablesections); $result = $this->courseservice->course_completion($course->shortname, $previouslyunavailablesections, $previouslyunavailablemods); // Make sure that the second page module (which is now newly available) appears in the list of newly available // module html. $this->assertTrue(isset($result['newlyavailablemodhtml'][$page2->cmid])); // Make sure that the second section (which is now wnely available) appears in the list of newly available // section html. $this->assertTrue(isset($result['newlyavailablesectionhtml'][2])); }
/** * Tests the update_dependency_id() function. */ public function test_update_dependency_id() { $cond = new condition((object) array('cm' => 123, 'e' => COMPLETION_COMPLETE)); $this->assertFalse($cond->update_dependency_id('frogs', 123, 456)); $this->assertFalse($cond->update_dependency_id('course_modules', 12, 34)); $this->assertTrue($cond->update_dependency_id('course_modules', 123, 456)); $after = $cond->save(); $this->assertEquals(456, $after->cm); }
/** * Test a specific type of module. * * @param string $modulename - the module name to test */ private function update_specific_module_test($modulename) { global $DB, $CFG; $this->resetAfterTest(true); $this->setAdminUser(); // Warnings: you'll need to change this line if ever you come to test a module not following Moodle standard. require_once $CFG->dirroot . '/mod/' . $modulename . '/lib.php'; // Enable avaibility. // If not enabled all conditional fields will be ignored. set_config('enableavailability', 1); // Enable course completion. // If not enabled all completion settings will be ignored. set_config('enablecompletion', COMPLETION_ENABLED); // Enable forum RSS feeds. set_config('enablerssfeeds', 1); set_config('forum_enablerssfeeds', 1); $course = $this->getDataGenerator()->create_course(array('numsections' => 1, 'enablecompletion' => COMPLETION_ENABLED), array('createsections' => true)); $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id)); // Create assign module instance for testing gradeitem. $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign'); $params['course'] = $course->id; $instance = $generator->create_instance($params); $assigncm = get_coursemodule_from_instance('assign', $instance->id); // Create the test forum to update. $initvalues = new stdClass(); $initvalues->introformat = FORMAT_HTML; $initvalues->course = $course->id; $forum = self::getDataGenerator()->create_module('forum', $initvalues); // Retrieve course module. $cm = get_coursemodule_from_instance('forum', $forum->id); // Module test values. $moduleinfo = new stdClass(); // Always mandatory generic values to any module. $moduleinfo->coursemodule = $cm->id; $moduleinfo->modulename = $modulename; $moduleinfo->course = $course->id; $moduleinfo->groupingid = $grouping->id; $moduleinfo->visible = true; // Sometimes optional generic values for some modules. $moduleinfo->name = 'My test module'; $moduleinfo->showdescription = 1; // standard boolean require_once $CFG->libdir . '/gradelib.php'; $gradecats = grade_get_categories_menu($moduleinfo->course, false); $gradecatid = current(array_keys($gradecats)); // Retrieve the first key of $gradecats $moduleinfo->gradecat = $gradecatid; $moduleinfo->groupmode = VISIBLEGROUPS; $moduleinfo->cmidnumber = 'idnumber_XXX'; // Completion common to all module. $moduleinfo->completion = COMPLETION_TRACKING_AUTOMATIC; $moduleinfo->completionview = COMPLETION_VIEW_REQUIRED; $moduleinfo->completiongradeitemnumber = 1; $moduleinfo->completionexpected = time() + 7 * 24 * 3600; $moduleinfo->completionunlocked = 1; // Conditional activity. $coursegradeitem = grade_item::fetch_course_item($moduleinfo->course); //the activity will become available only when the user reach some grade into the course itself. $moduleinfo->availability = json_encode(\core_availability\tree::get_root_json(array(\availability_date\condition::get_json('>=', time()), \availability_date\condition::get_json('<', time() + 7 * 24 * 3600), \availability_grade\condition::get_json($coursegradeitem->id, 10, 80), \availability_profile\condition::get_json(false, 'email', 'contains', '@'), \availability_completion\condition::get_json($assigncm->id, COMPLETION_COMPLETE)), '&')); // Grading and Advanced grading. require_once $CFG->dirroot . '/rating/lib.php'; $moduleinfo->assessed = RATING_AGGREGATE_AVERAGE; $moduleinfo->scale = 10; // Note: it could be minus (for specific course scale). It is a signed number. $moduleinfo->assesstimestart = time(); $moduleinfo->assesstimefinish = time() + 7 * 24 * 3600; // RSS. $moduleinfo->rsstype = 2; $moduleinfo->rssarticles = 10; // Optional intro editor (depends of module). $draftid_editor = 0; file_prepare_draft_area($draftid_editor, null, null, null, null); $moduleinfo->introeditor = array('text' => 'This is a module', 'format' => FORMAT_HTML, 'itemid' => $draftid_editor); // Following is the advanced grading method area called 'submissions' for the 'assign' module. if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) { $moduleinfo->grade = 100; } // Plagiarism form values. // No plagiarism plugin installed by default. Use this space to make your own test. // Values specific to the module. $modulesetvalues = $modulename . '_update_set_values'; $this->{$modulesetvalues}($moduleinfo); // Create the module. $result = update_module($moduleinfo); // Retrieve the module info. $dbmodinstance = $DB->get_record($moduleinfo->modulename, array('id' => $result->instance)); $dbcm = get_coursemodule_from_instance($moduleinfo->modulename, $result->instance); // Retrieve the grade item. $gradeitem = $DB->get_record('grade_items', array('courseid' => $moduleinfo->course, 'iteminstance' => $dbmodinstance->id, 'itemmodule' => $moduleinfo->modulename)); // Compare the values common to all module instances. $this->assertEquals($moduleinfo->modulename, $dbcm->modname); $this->assertEquals($moduleinfo->course, $dbcm->course); $this->assertEquals($moduleinfo->groupingid, $dbcm->groupingid); $this->assertEquals($moduleinfo->visible, $dbcm->visible); $this->assertEquals($moduleinfo->completion, $dbcm->completion); $this->assertEquals($moduleinfo->completionview, $dbcm->completionview); $this->assertEquals($moduleinfo->completiongradeitemnumber, $dbcm->completiongradeitemnumber); $this->assertEquals($moduleinfo->completionexpected, $dbcm->completionexpected); $this->assertEquals($moduleinfo->availability, $dbcm->availability); $this->assertEquals($moduleinfo->showdescription, $dbcm->showdescription); $this->assertEquals($moduleinfo->groupmode, $dbcm->groupmode); $this->assertEquals($moduleinfo->cmidnumber, $dbcm->idnumber); $this->assertEquals($moduleinfo->gradecat, $gradeitem->categoryid); // Optional grade testing. if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) { $this->assertEquals($moduleinfo->grade, $dbmodinstance->grade); } // Some optional (but quite common) to some module. $this->assertEquals($moduleinfo->name, $dbmodinstance->name); $this->assertEquals($moduleinfo->intro, $dbmodinstance->intro); $this->assertEquals($moduleinfo->introformat, $dbmodinstance->introformat); // Test specific to the module. $modulerunasserts = $modulename . '_update_run_asserts'; $this->{$modulerunasserts}($moduleinfo, $dbmodinstance); return $moduleinfo; }
/** * Tests completion_value_used static function. */ public function test_completion_value_used() { global $CFG, $DB; $this->resetAfterTest(); // Create course with completion turned on and some sections. $CFG->enablecompletion = true; $CFG->enableavailability = true; $generator = $this->getDataGenerator(); $course = $generator->create_course(array('numsections' => 1, 'enablecompletion' => 1), array('createsections' => true)); availability_completion\condition::wipe_static_cache(); // Create three pages with manual completion. $page1 = $generator->get_plugin_generator('mod_page')->create_instance(array('course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL)); $page2 = $generator->get_plugin_generator('mod_page')->create_instance(array('course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL)); $page3 = $generator->get_plugin_generator('mod_page')->create_instance(array('course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL)); // Set up page3 to depend on page1, and section1 to depend on page2. $DB->set_field('course_modules', 'availability', '{"op":"|","show":true,"c":[' . '{"type":"completion","e":1,"cm":' . $page1->cmid . '}]}', array('id' => $page3->cmid)); $DB->set_field('course_sections', 'availability', '{"op":"|","show":true,"c":[' . '{"type":"completion","e":1,"cm":' . $page2->cmid . '}]}', array('course' => $course->id, 'section' => 1)); // Now check: nothing depends on page3 but something does on the others. $this->assertTrue(availability_completion\condition::completion_value_used($course, $page1->cmid)); $this->assertTrue(availability_completion\condition::completion_value_used($course, $page2->cmid)); $this->assertFalse(availability_completion\condition::completion_value_used($course, $page3->cmid)); }