Beispiel #1
0
 /**
  * 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 $USER;
     $values = $tour->get_filter_values(self::get_filter_name());
     if (empty($values)) {
         // There are no values configured.
         // No values means all.
         return true;
     }
     if (is_siteadmin()) {
         return true;
     }
     // Presence within the array is sufficient. Ignore any value.
     $values = array_flip($values);
     $cache = \cache::make_from_params(\cache_store::MODE_REQUEST, 'tool_usertours', 'filter_role');
     $cachekey = "{$USER->id}_{$context->id}";
     $userroles = $cache->get($cachekey);
     if ($userroles === false) {
         $userroles = get_user_roles_with_special($context);
         $cache->set($cachekey, $userroles);
     }
     foreach ($userroles as $role) {
         if (isset($values[$role->roleid])) {
             return true;
         }
     }
     return false;
 }
Beispiel #2
0
 /**
  * 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 $USER;
     $values = $tour->get_filter_values(self::get_filter_name());
     if (empty($values)) {
         // There are no values configured.
         // No values means all.
         return true;
     }
     // Presence within the array is sufficient. Ignore any value.
     $values = array_flip($values);
     if (isset($values[self::ROLE_SITEADMIN]) && is_siteadmin()) {
         // This tour has been restricted to a role including site admin, and this user is a site admin.
         return true;
     }
     // Use a request cache to save on DB queries.
     // We may be checking multiple tours and they'll all be for the same userid, and contextid
     $cache = \cache::make_from_params(\cache_store::MODE_REQUEST, 'tool_usertours', 'filter_role');
     // Get all of the roles used in this context, including special roles such as user, and frontpageuser.
     $cachekey = "{$USER->id}_{$context->id}";
     $userroles = $cache->get($cachekey);
     if ($userroles === false) {
         $userroles = get_user_roles_with_special($context);
         $cache->set($cachekey, $userroles);
     }
     // Some special roles do not include the shortname.
     // Therefore we must fetch all roles too. Thankfully these don't actually change based on context.
     // They do require a DB call, so let's cache it.
     $cachekey = "allroles";
     $allroles = $cache->get($cachekey);
     if ($allroles === false) {
         $allroles = get_all_roles();
         $cache->set($cachekey, $allroles);
     }
     // Now we can check whether any of the user roles are in the list of allowed roles for this filter.
     foreach ($userroles as $role) {
         $shortname = $allroles[$role->roleid]->shortname;
         if (isset($values[$shortname])) {
             return true;
         }
     }
     return false;
 }
Beispiel #3
0
 /**
  * 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 $PAGE;
     $values = $tour->get_filter_values('theme');
     if (empty($values)) {
         // There are no values configured.
         // No values means all.
         return true;
     }
     // Presence within the array is sufficient. Ignore any value.
     $values = array_flip($values);
     return isset($values[$PAGE->theme->name]);
 }
Beispiel #4
0
 /**
  * A helper to create an empty tour.
  *
  * @param   stdClass    $tourconfig     The configuration for the new tour
  * @param   bool        $persist        Whether to persist the data
  * @return  \tool_usertours\tour
  */
 public function helper_create_tour(\stdClass $tourconfig = null, $persist = true)
 {
     $minvalues = ['id' => null, 'pathmatch' => '/my/%', 'enabled' => true, 'name' => '', 'description' => '', 'configdata' => ''];
     if ($tourconfig === null) {
         $tourconfig = new \stdClass();
     }
     foreach ($minvalues as $key => $value) {
         if (!isset($tourconfig->{$key})) {
             $tourconfig->{$key} = $value;
         }
     }
     $tour = \tool_usertours\tour::load_from_record($tourconfig, true);
     if ($persist) {
         $tour->persist(true);
     }
     return $tour;
 }
 /**
  * Test that a disabled tour should never be shown to users.
  */
 public function test_should_show_for_user_disabled()
 {
     $tour = new \tool_usertours\tour();
     $tour->set_enabled(false);
     $this->assertFalse($tour->should_show_for_user());
 }
Beispiel #6
0
 /**
  * Make sure all of the default tours that are shipped with Moodle are created
  * and up to date with the latest version.
  */
 public static function update_shipped_tours()
 {
     global $DB, $CFG;
     // A list of tours that are shipped with Moodle. They are in
     // the format filename => version. The version value needs to
     // be increased if the tour has been updated.
     $shippedtours = ['boost_administrator.json' => 1, 'boost_course_view.json' => 1];
     $existingtourrecords = $DB->get_recordset('tool_usertours_tours');
     // Get all of the existing shipped tours and check if they need to be
     // updated.
     foreach ($existingtourrecords as $tourrecord) {
         $tour = tour::load_from_record($tourrecord);
         if (!empty($tour->get_config(self::CONFIG_SHIPPED_TOUR))) {
             $filename = $tour->get_config(self::CONFIG_SHIPPED_FILENAME);
             $version = $tour->get_config(self::CONFIG_SHIPPED_VERSION);
             // If we know about this tour (otherwise leave it as is).
             if (isset($shippedtours[$filename])) {
                 // And the version in the DB is an older version.
                 if ($version < $shippedtours[$filename]) {
                     // Remove the old version because it's been updated
                     // and needs to be recreated.
                     $tour->remove();
                 } else {
                     // The tour has not been updated so we don't need to
                     // do anything with it.
                     unset($shippedtours[$filename]);
                 }
             }
         }
     }
     $existingtourrecords->close();
     foreach ($shippedtours as $filename => $version) {
         $filepath = $CFG->dirroot . '/admin/tool/usertours/tours/' . $filename;
         $tourjson = file_get_contents($filepath);
         $tour = self::import_tour_from_json($tourjson);
         // Set some additional config data to record that this tour was
         // added as a shipped tour.
         $tour->set_config(self::CONFIG_SHIPPED_TOUR, true);
         $tour->set_config(self::CONFIG_SHIPPED_FILENAME, $filename);
         $tour->set_config(self::CONFIG_SHIPPED_VERSION, $version);
         if (defined('BEHAT_SITE_RUNNING') || defined('PHPUNIT_TEST') && PHPUNIT_TEST) {
             // Disable this tour if this is behat or phpunit.
             $tour->set_enabled(false);
         }
         $tour->persist();
     }
 }
Beispiel #7
0
 /**
  * Format the current row's actions column.
  *
  * @param   tour    $tour       The tour for this row.
  * @return  string
  */
 protected function col_actions(tour $tour)
 {
     $actions = [];
     if ($tour->is_first_tour()) {
         $actions[] = helper::get_filler_icon();
     } else {
         $actions[] = helper::format_icon_link($tour->get_moveup_link(), 't/up', get_string('movetourup', 'tool_usertours'));
     }
     if ($tour->is_last_tour($this->tourcount)) {
         $actions[] = helper::get_filler_icon();
     } else {
         $actions[] = helper::format_icon_link($tour->get_movedown_link(), 't/down', get_string('movetourdown', 'tool_usertours'));
     }
     $actions[] = helper::format_icon_link($tour->get_view_link(), 't/viewdetails', get_string('view'));
     $actions[] = helper::format_icon_link($tour->get_edit_link(), 't/edit', get_string('edit'));
     $actions[] = helper::format_icon_link($tour->get_export_link(), 't/export', get_string('exporttour', 'tool_usertours'), 'tool_usertours');
     $actions[] = helper::format_icon_link($tour->get_delete_link(), 't/delete', get_string('delete'), null, ['data-action' => 'delete', 'data-id' => $tour->get_id()]);
     return implode('&nbsp;', $actions);
 }
Beispiel #8
0
 /**
  * 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};
     foreach ($data->{$key} as $value) {
         if ($value === static::ANYVALUE) {
             $newvalue = [];
             break;
         }
     }
     $tour->set_filter_values($filtername, $newvalue);
 }
Beispiel #9
0
 /**
  * Mark the specified tour as completed for the current user.
  *
  * @param   int     $tourid     The ID of the tour.
  * @param   int     $context    The Context ID of the current page.
  * @param   string  $pageurl    The path of the current page.
  * @param   int     $stepid     The step id
  * @param   int     $stepindex  The step index
  * @return  array               As described in complete_tour_returns
  */
 public static function complete_tour($tourid, $context, $pageurl, $stepid, $stepindex)
 {
     $params = self::validate_parameters(self::complete_tour_parameters(), ['tourid' => $tourid, 'context' => $context, 'pageurl' => $pageurl, 'stepid' => $stepid, 'stepindex' => $stepindex]);
     $context = \context_helper::instance_by_id($params['context']);
     self::validate_context($context);
     $tour = tourinstance::instance($params['tourid']);
     $tour->mark_user_completed();
     \tool_usertours\event\tour_ended::create(['contextid' => $context->id, 'objectid' => $params['tourid'], 'other' => ['pageurl' => $params['pageurl'], 'stepid' => $params['stepid'], 'stepindex' => $params['stepindex']]])->trigger();
     return [];
 }
Beispiel #10
0
 /**
  * Move a tour up or down.
  *
  * @param   int     $id     The tour to move.
  */
 protected function move_tour($id)
 {
     require_sesskey();
     $direction = required_param('direction', PARAM_INT);
     $tour = tour::instance($id);
     $currentsortorder = $tour->get_sortorder();
     $targetsortorder = $currentsortorder + $direction;
     $swapwith = helper::get_tour_from_sortorder($targetsortorder);
     // Set the sort order to something out of the way.
     $tour->set_sortorder(-1);
     $tour->persist();
     // Swap the two sort orders.
     $swapwith->set_sortorder($currentsortorder);
     $swapwith->persist();
     $tour->set_sortorder($targetsortorder);
     $tour->persist();
     redirect(helper::get_list_tour_link());
 }
Beispiel #11
0
 /**
  * Create the edit tour form.
  *
  * @param   tour        $tour       The tour being editted.
  */
 public function __construct(\tool_usertours\tour $tour)
 {
     $this->tour = $tour;
     parent::__construct($tour->get_edit_link());
 }