/** * Tests constructing and using condition as part of tree. */ public function test_in_tree() { global $USER, $CFG; $this->resetAfterTest(); $this->setAdminUser(); // Create course with completion turned on and a Page. $CFG->enablecompletion = true; $CFG->enableavailability = true; $generator = $this->getDataGenerator(); $course = $generator->create_course(array('enablecompletion' => 1)); $page = $generator->get_plugin_generator('mod_page')->create_instance(array('course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL)); $modinfo = get_fast_modinfo($course); $cm = $modinfo->get_cm($page->cmid); $info = new \core_availability\mock_info($course, $USER->id); $structure = (object) array('op' => '|', 'show' => true, 'c' => array((object) array('type' => 'completion', 'cm' => (int) $cm->id, 'e' => COMPLETION_COMPLETE))); $tree = new \core_availability\tree($structure); // Initial check (user has not completed activity). $result = $tree->check_available(false, $info, true, $USER->id); $this->assertFalse($result->is_available()); // Mark activity complete. $completion = new completion_info($course); $completion->update_state($cm, COMPLETION_COMPLETE); // Now it's true! $result = $tree->check_available(false, $info, true, $USER->id); $this->assertTrue($result->is_available()); }
/** * Tests constructing and using date condition as part of tree. */ public function test_in_tree() { global $SITE, $USER, $CFG; $this->resetAfterTest(); $this->setAdminUser(); // Set server timezone for test. (Important as otherwise the timezone // could be anything - this is modified by other unit tests, too.) $this->setTimezone('UTC'); // SEt user to GMT+5. $USER->timezone = 5; // Construct tree with date condition. $time = strtotime('2014-02-18 14:20:00 GMT'); $structure = (object) array('op' => '|', 'show' => true, 'c' => array((object) array('type' => 'date', 'd' => '>=', 't' => $time))); $tree = new \core_availability\tree($structure); $info = new \core_availability\mock_info(); // Check if available (when not available). condition::set_current_time_for_test($time - 1); $information = ''; $result = $tree->check_available(false, $info, true, $USER->id); $this->assertFalse($result->is_available()); $information = $tree->get_result_information($info, $result); // Note: PM is normally upper-case, but an issue with PHP on Mac means // that on that platform, it is reported lower-case. $this->assertRegExp('~from.*18 February 2014, 7:20 (PM|pm)~', $information); // Check if available (when available). condition::set_current_time_for_test($time); $result = $tree->check_available(false, $info, true, $USER->id); $this->assertTrue($result->is_available()); $information = $tree->get_result_information($info, $result); $this->assertEquals('', $information); }
/** * Tests constructing and using date condition as part of tree. */ public function test_in_tree() { global $USER; $this->setAdminUser(); $info = new \core_availability\mock_info(); $structure = (object) array('op' => '|', 'show' => true, 'c' => array((object) array('type' => 'profile', 'op' => condition::OP_IS_EQUAL_TO, 'cf' => 'frogtype', 'v' => 'tree'))); $tree = new \core_availability\tree($structure); // Initial check (user does not have custom field). $result = $tree->check_available(false, $info, true, $USER->id); $this->assertFalse($result->is_available()); // Set field. $this->set_field($USER->id, 'tree'); // Now it's true! $result = $tree->check_available(false, $info, true, $USER->id); $this->assertTrue($result->is_available()); }
function content_unlock_satisfies_conditions($conditions, $courseid, $userid) { global $DB; if (isset($conditions)) { $tree = new \core_availability\tree(json_decode($conditions)); $course = $DB->get_record('course', array('id' => $courseid)); $info = new content_unlock_conditions_info($course); $result = $tree->check_available(false, $info, true, $userid); return $result->is_available(); } return true; }
/** * Shortcut function to check availability and also get information. * * @param stdClass $structure Tree structure * @param \core_availability\info $info Location info * @param int $userid User id */ protected function get_available_results($structure, \core_availability\info $info, $userid) { $tree = new \core_availability\tree($structure); $result = $tree->check_available(false, $info, true, $userid); return array($result->is_available(), $tree->get_result_information($info, $result)); }