diff --git a/admin/tool/usertours/classes/local/filter/category.php b/admin/tool/usertours/classes/local/filter/category.php new file mode 100644 index 0000000000000..5f7ffc5a24c3a --- /dev/null +++ b/admin/tool/usertours/classes/local/filter/category.php @@ -0,0 +1,98 @@ +. + +/** + * Category filter. + * + * @package tool_usertours + * @copyright 2017 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace tool_usertours\local\filter; + +defined('MOODLE_INTERNAL') || die(); + +use tool_usertours\tour; +use context; + +/** + * Category filter. + * + * @copyright 2017 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class category extends base { + /** + * The name of the filter. + * + * @return string + */ + public static function get_filter_name() { + return 'category'; + } + + /** + * Retrieve the list of available filter options. + * + * @return array An array whose keys are the valid options + * And whose values are the values to display + */ + public static function get_filter_options() { + $options = \coursecat::make_categories_list(); + return $options; + } + + /** + * Check whether the filter matches the specified tour and/or context. + * + * @param tour $tour The tour to check + * @param context $context The context to check + * @return boolean + */ + public static function filter_matches(tour $tour, context $context) { + $values = $tour->get_filter_values(self::get_filter_name()); + if (empty($values) || empty($values[0])) { + // There are no values configured, meaning all. + return true; + } + if ($context->contextlevel < CONTEXT_COURSECAT) { + return false; + } + return self::check_contexts($context, $values); + } + + /** + * Recursive function allows checking of parent categories. + * + * @param context $context + * @param array $values + * @return boolean + */ + private static function check_contexts(context $context, $values) { + if ($context->contextlevel > CONTEXT_COURSECAT) { + return self::check_contexts($context->get_parent_context(), $values); + } else if ($context->contextlevel == CONTEXT_COURSECAT) { + if (in_array($context->instanceid, $values)) { + return true; + } else { + return self::check_contexts($context->get_parent_context(), $values); + } + } else { + return false; + } + } +} diff --git a/admin/tool/usertours/classes/local/filter/course.php b/admin/tool/usertours/classes/local/filter/course.php new file mode 100644 index 0000000000000..32fb21ddb5dae --- /dev/null +++ b/admin/tool/usertours/classes/local/filter/course.php @@ -0,0 +1,117 @@ +. + +/** + * Course filter. + * + * @package tool_usertours + * @copyright 2017 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace tool_usertours\local\filter; + +defined('MOODLE_INTERNAL') || die(); + +use tool_usertours\tour; +use context; + +/** + * Course filter. + * + * @copyright 2017 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class course extends base { + /** + * The name of the filter. + * + * @return string + */ + public static function get_filter_name() { + return 'course'; + } + + /** + * Overrides the base add form element with a course selector. + * + * @param \MoodleQuickForm $mform + */ + public static function add_filter_to_form(\MoodleQuickForm &$mform) { + $options = ['multiple' => true]; + + $filtername = self::get_filter_name(); + $key = "filter_{$filtername}"; + + $mform->addElement('course', $key, get_string($key, 'tool_usertours'), $options); + $mform->setDefault($key, '0'); + $mform->addHelpButton($key, $key, 'tool_usertours'); + } + + /** + * Check whether the filter matches the specified tour and/or context. + * + * @param tour $tour The tour to check + * @param context $context The context to check + * @return boolean + */ + public static function filter_matches(tour $tour, context $context) { + global $COURSE; + $values = $tour->get_filter_values(self::get_filter_name()); + if (empty($values) || empty($values[0])) { + // There are no values configured, meaning all. + return true; + } + if (empty($COURSE->id)) { + return false; + } + return in_array($COURSE->id, $values); + } + + /** + * Overrides the base prepare the filter values for the form with an integer value. + * + * @param tour $tour The tour to prepare values from + * @param stdClass $data The data value + * @return stdClass + */ + public static function prepare_filter_values_for_form(tour $tour, \stdClass $data) { + $filtername = static::get_filter_name(); + $key = "filter_{$filtername}"; + $values = $tour->get_filter_values($filtername); + if (empty($values)) { + $values = 0; + } + $data->$key = $values; + return $data; + } + + /** + * Overrides the base save the filter values from the form to the tour. + * + * @param tour $tour The tour to save values to + * @param stdClass $data The data submitted in the form + */ + public static function save_filter_values_from_form(tour $tour, \stdClass $data) { + $filtername = static::get_filter_name(); + $key = "filter_{$filtername}"; + $newvalue = $data->$key; + if (empty($data->$key)) { + $newvalue = []; + } + $tour->set_filter_values($filtername, $newvalue); + } +} diff --git a/admin/tool/usertours/classes/local/filter/courseformat.php b/admin/tool/usertours/classes/local/filter/courseformat.php new file mode 100644 index 0000000000000..95e2b7c861328 --- /dev/null +++ b/admin/tool/usertours/classes/local/filter/courseformat.php @@ -0,0 +1,82 @@ +. + +/** + * Course format filter. + * + * @package tool_usertours + * @copyright 2017 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace tool_usertours\local\filter; + +defined('MOODLE_INTERNAL') || die(); + +use tool_usertours\tour; +use context; + +/** + * Course format filter. + * + * @copyright 2017 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class courseformat extends base { + /** + * The name of the filter. + * + * @return string + */ + public static function get_filter_name() { + return 'courseformat'; + } + + /** + * Retrieve the list of available filter options. + * + * @return array An array whose keys are the valid options + * And whose values are the values to display + */ + public static function get_filter_options() { + $options = []; + $courseformats = get_sorted_course_formats(true); + foreach ($courseformats as $courseformat) { + $options[$courseformat] = get_string('pluginname', "format_$courseformat"); + } + return $options; + } + + /** + * Check whether the filter matches the specified tour and/or context. + * + * @param tour $tour The tour to check + * @param context $context The context to check + * @return boolean + */ + public static function filter_matches(tour $tour, context $context) { + global $COURSE; + $values = $tour->get_filter_values('courseformat'); + if (empty($values)) { + // There are no values configured, meaning all. + return true; + } + if (empty($COURSE->format)) { + return false; + } + return in_array($COURSE->format, $values); + } +} diff --git a/admin/tool/usertours/lang/en/tool_usertours.php b/admin/tool/usertours/lang/en/tool_usertours.php index 9842e41c4b363..5d8dd8ad17930 100644 --- a/admin/tool/usertours/lang/en/tool_usertours.php +++ b/admin/tool/usertours/lang/en/tool_usertours.php @@ -52,6 +52,12 @@ $string['event_tour_ended'] = 'Tour ended'; $string['event_step_shown'] = 'Step shown'; $string['exporttour'] = 'Export tour'; +$string['filter_category'] = 'Category'; +$string['filter_category_help'] = 'Show the tour on a page that is associated with a course in the selected category.'; +$string['filter_course'] = 'Courses'; +$string['filter_course_help'] = 'Show the tour on a page that is associated with the selected course.'; +$string['filter_courseformat'] = 'Course format'; +$string['filter_courseformat_help'] = 'Show the tour on a page that is associated with a course using the selected course format.'; $string['filter_header'] = 'Tour filters'; $string['filter_help'] = 'Select the conditions under which the tour will be shown. All of the filters must match for a tour to be shown to a user.'; $string['filter_theme'] = 'Theme'; diff --git a/admin/tool/usertours/tests/behat/tour_filter.feature b/admin/tool/usertours/tests/behat/tour_filter.feature index 46f226a9c06a8..fdcd28d38b7b4 100644 --- a/admin/tool/usertours/tests/behat/tour_filter.feature +++ b/admin/tool/usertours/tests/behat/tour_filter.feature @@ -57,3 +57,103 @@ Feature: Apply tour filters to a tour And I log in as "teacher1" And I am on "Course 1" course homepage And I should see "Welcome to your course tour." + + @javascript + Scenario: Add tour for a specific category and its subcategory + Given the following "categories" exist: + | name | category | idnumber | + | MainCat | 0 | CAT1 | + | SubCat | CAT1 | CAT2 | + And the following "courses" exist: + | fullname | shortname | category | + | Course 1 | C1 | CAT1 | + | Course 2 | C2 | CAT2 | + And the following "users" exist: + | username | + | student1 | + And the following "course enrolments" exist: + | user | course | role | + | student1 | C1 | student | + | student1 | C2 | student | + And I log in as "admin" + And I add a new user tour with: + | Name | First tour | + | Description | My first tour | + | Apply to URL match | /course/view.php% | + | Tour is enabled | 1 | + | Category | MainCat | + And I add steps to the "First tour" tour: + | targettype | Title | Content | + | Display in middle of page | Welcome | Welcome to your course tour.| + And I log out + And I log in as "student1" + When I am on "Course 1" course homepage + And I wait until the page is ready + Then I should see "Welcome to your course tour." + When I am on "Course 2" course homepage + And I wait until the page is ready + Then I should see "Welcome to your course tour." + + @javascript + Scenario: Add tour for a specific courseformat + Given the following "courses" exist: + | fullname | shortname | format | + | Course 1 | C1 | topics | + | Course 2 | C2 | weeks | + And the following "users" exist: + | username | + | student1 | + And the following "course enrolments" exist: + | user | course | role | + | student1 | C1 | student | + | student1 | C2 | student | + And I log in as "admin" + And I add a new user tour with: + | Name | First tour | + | Description | My first tour | + | Apply to URL match | /course/view.php% | + | Tour is enabled | 1 | + | Course format | Weekly format | + And I add steps to the "First tour" tour: + | targettype | Title | Content | + | Display in middle of page | Welcome | Welcome to your course tour.| + And I log out + And I log in as "student1" + When I am on "Course 1" course homepage + And I wait until the page is ready + Then I should not see "Welcome to your course tour." + When I am on "Course 2" course homepage + And I wait until the page is ready + Then I should see "Welcome to your course tour." + + @javascript + Scenario: Add tour for a specific course + Given the following "courses" exist: + | fullname | shortname | format | + | Course 1 | C1 | topics | + | Course 2 | C2 | weeks | + And the following "users" exist: + | username | + | student1 | + And the following "course enrolments" exist: + | user | course | role | + | student1 | C1 | student | + | student1 | C2 | student | + And I log in as "admin" + And I add a new user tour with: + | Name | First tour | + | Description | My first tour | + | Apply to URL match | /course/view.php% | + | Tour is enabled | 1 | + | Courses | C1 | + And I add steps to the "First tour" tour: + | targettype | Title | Content | + | Display in middle of page | Welcome | Welcome to your course tour.| + And I log out + And I log in as "student1" + When I am on "Course 1" course homepage + And I wait until the page is ready + Then I should see "Welcome to your course tour." + When I am on "Course 2" course homepage + And I wait until the page is ready + Then I should not see "Welcome to your course tour."