Search code examples
phpjoomlajoomla2.5joomla-extensions

Joomla store data on session


I'm modifying a joomla component (jQuarks) in order to achieve the wanted behaviour from the module in order to do online quiz/exams. Basically, I want to be able to use the session variable to store the following items:

  • UserId
  • QuizSessionID (not the Joomla Session per se, since this is a jQarks "session")
  • The quiz start time
  • Last question answered page (or id, doesn't really matter, but since I'm using pages it's the best option)
  • All the previously answered questions (ie, the question ID and the Answer ID)

In order to achieve this I'm using JFactory::getSession() but I'm having some doubts.

The first one is: Is the session variable the best way to store this data? If not, how or where can I store this info? Whenever the user clicks to navigate to the next page, I want to store the answer given and add it to the previously given answers. Won't this make me have to constantly destroy and recreate the session? If, by any chance a user needs to refresh the page, the current component behavior is to create a new quizSession thus reseting everything and storing nothing. Will the Joomla Session variable be enough to force the behavior I intend? (resuming the quizSession that te user started)

If this is the wrong approach what should I use? I'm not really versed developing for Joomla and haven't touched PHP in at least 3 years...

Best Regards


Solution

  • I advice you not to use JSession, but try User state. It is widely used in Joomla for storing pagiantion, filters vars across pages. You can find info about it here.

    The only concern I have is a page refresh. The state will not be reset on refresh and I doubt that it is possible to do with Joomla API...

    And one more thing - you do not need to store UserId in the session, because you can anytime retrieve it using this simple piece of code:

    $userId = JFactory::getUser()->id;
    

    More info here.