. namespace mod_adaptivequiz\local\report\users_attempts\user_preferences; /** * Provides methods for storing and fetching of the report user preferences from the storage. * * Operates on user session as well to avoid unnecessary database queries. * * @package mod_adaptivequiz * @copyright 2022 Vitaly Potenko * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ final class user_preferences_repository { /** * @var string Name of the preference. */ private const PREFERENCE_NAME = 'adaptivequiz_users_attempts_report'; /** * Stores the given preferences object in available storages. * * @param user_preferences $prefs */ public static function save(user_preferences $prefs): void { global $SESSION; $prefsarr = $prefs->as_array(); $SESSION->flextableextra[self::PREFERENCE_NAME] = $prefsarr; set_user_preference(self::PREFERENCE_NAME, json_encode($prefsarr)); } /** * Instantiates user preferences from available storages or default. * * @return user_preferences */ public static function get(): user_preferences { global $SESSION; if (!empty($SESSION->flextableextra[self::PREFERENCE_NAME])) { return user_preferences::from_array($SESSION->flextableextra[self::PREFERENCE_NAME]); } if (!$json = get_user_preferences(self::PREFERENCE_NAME)) { return user_preferences::defaults(); } return user_preferences::from_array(json_decode($json, true)); } }