/** * Form definition. */ public function definition() { $mform = $this->_form; // ID of existing tour. $mform->addElement('hidden', 'id'); $mform->setType('id', PARAM_INT); // Name of the tour. $mform->addElement('text', 'name', get_string('name', 'tool_usertours')); $mform->addRule('name', get_string('required'), 'required', null, 'client'); $mform->setType('name', PARAM_TEXT); // Admin-only descriptions. $mform->addElement('textarea', 'description', get_string('description', 'tool_usertours')); $mform->setType('description', PARAM_RAW); // Application. $mform->addElement('text', 'pathmatch', get_string('pathmatch', 'tool_usertours')); $mform->setType('pathmatch', PARAM_RAW); $mform->addHelpButton('pathmatch', 'pathmatch', 'tool_usertours'); $mform->addElement('checkbox', 'enabled', get_string('tourisenabled', 'tool_usertours')); // Configuration. $this->tour->add_config_to_form($mform); // Filters. $mform->addElement('header', 'filters', get_string('filter_header', 'tool_usertours')); $mform->addElement('static', 'filterhelp', '', get_string('filter_help', 'tool_usertours')); foreach (helper::get_all_filters() as $filterclass) { $filterclass::add_filter_to_form($mform); } $this->add_action_buttons(); }
/** * Test the filter_matches function. * * @dataProvider filter_matches_provider * @param array $filtervalues The filter values * @param string $currenttheme The name of the current theme * @param boolean $expected Whether the tour is expected to match */ public function test_filter_matches($filtervalues, $currenttheme, $expected) { global $PAGE; $filtername = \tool_usertours\local\filter\theme::class; // Note: No need to persist this tour. $tour = new \tool_usertours\tour(); if ($filtervalues !== null) { $tour->set_filter_values('theme', $filtervalues); } $PAGE->theme->name = $currenttheme; // Note: The theme filter does not use the context. $this->assertEquals($expected, $filtername::filter_matches($tour, \context_system::instance())); }
/** * Test the filter_matches function when one user has multiple roles. */ public function test_filter_matches_multiple_role_one_user() { $context = \context_course::instance($this->course->id); $roles = [$this->roles['student']]; $this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $this->roles['teacher']); // Note: No need to persist this tour. $tour = new \tool_usertours\tour(); $tour->set_filter_values('role', $roles); // Note: The role filter does not use the context. foreach ($this->testroles as $role) { $this->setUser($this->{$role}); if ($role === 'student') { $this->assertTrue(\tool_usertours\local\filter\role::filter_matches($tour, $context)); } else { $this->assertFalse(\tool_usertours\local\filter\role::filter_matches($tour, $context)); } } // The admin should always be able to view too. $this->setAdminUser(); $this->assertTrue(\tool_usertours\local\filter\role::filter_matches($tour, $context)); }
/** * Test the filter_matches function when an admin user has multiple roles. */ public function test_filter_matches_multiple_role_admin_user() { global $USER; $context = \context_course::instance($this->course->id); $roles = [\tool_usertours\local\filter\role::ROLE_SITEADMIN]; $this->setAdminUser(); $this->getDataGenerator()->enrol_user($USER->id, $this->course->id, $this->roles['student']); // Note: No need to persist this tour. $tour = new \tool_usertours\tour(); $tour->set_filter_values('role', $roles); // The admin can view this one because it's only aimed at them. $this->assertTrue(\tool_usertours\local\filter\role::filter_matches($tour, $context)); }