/** * 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; }
/** * 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(); } }
/** * Import the provided tour JSON. * * @param string $json The tour configuration. * @return tour */ public static function import_tour_from_json($json) { $tourconfig = json_decode($json); // We do not use this yet - we may do in the future. unset($tourconfig->version); $steps = $tourconfig->steps; unset($tourconfig->steps); $tourconfig->id = null; $tourconfig->sortorder = null; $tour = tour::load_from_record($tourconfig, true); $tour->persist(true); // Ensure that steps are orderered by their sortorder. \core_collator::asort_objects_by_property($steps, 'sortorder', \core_collator::SORT_NUMERIC); foreach ($steps as $stepconfig) { $stepconfig->id = null; $stepconfig->tourid = $tour->get_id(); $step = step::load_from_record($stepconfig, true); $step->persist(true); } return $tour; }