Dear All,
I have been pondering on how to solve the persistence problem of my Course Format - Collapsed Topics.
The format uses Javascript to manipulate the CSS controlling the page to decide if a section is shown or not. What I would like to do is have some per user persistence without using the database (because it would be clogged up with data that would not be used 90% of the time).
There are two solutions:
1 - Have a session per user with php variables understanding if a topic is currently visable which then writes the appropriate html / css start point on page refresh and have Javascript RPC php from the client end when the user presses the toggle.
2 - Keep everything client side with Javascript variables and cookies.
With solution 1 - I would like help with Moodle sessions and how to use XML RPC's (I have programmed tradional RPC's in the past) - I also notice that XML RPC's are turned on within php for Moodle.
With solution 2 - I would need to know that it is possible for Javasript to rewrite the html as it is first loaded by the browser without any user interaction by reading the contents of a cookie.
What do you think? What are good examples?
Many thanks,
Gareth
In reply to Gareth J Barnard
Re: XML RPC and Sessions vs Javascript and Cookies
by Skylar Saveland -
Solution 2 is definitely possible. You can do anything you want with the html once it is loaded. Might be a little tricky to make it smooth in all browsers as you hide() things right after they enter the DOM.
In reply to Skylar Saveland
Re: XML RPC and Sessions vs Javascript and Cookies
by Gareth J Barnard -
Dear Skylar,
Thanks. Is there anyway I can manipulate the HTML as it is being entered into the DOM? The old idea of self modifying code.
Cheers,
Gareth
Thanks. Is there anyway I can manipulate the HTML as it is being entered into the DOM? The old idea of self modifying code.
Cheers,
Gareth
Hi Gareth,
The DOM (http://en.wikipedia.org/wiki/Document_Object_Model) is the standard representation of the HTML code in the browser.
Moodle uses YUI (Yahoo! UI Library) as its JavaScript library which provides lots of functions to manipulate the DOM:
http://developer.yahoo.com/yui/dom/
There are also features for managing cookies:
http://developer.yahoo.com/yui/cookie/
You might also find some useful information in the Moodle Docs, see Development:YUI and Javascript FAQ.
hth
Frank
The DOM (http://en.wikipedia.org/wiki/Document_Object_Model) is the standard representation of the HTML code in the browser.
Moodle uses YUI (Yahoo! UI Library) as its JavaScript library which provides lots of functions to manipulate the DOM:
http://developer.yahoo.com/yui/dom/
There are also features for managing cookies:
http://developer.yahoo.com/yui/cookie/
You might also find some useful information in the Moodle Docs, see Development:YUI and Javascript FAQ.
hth
Frank
You can use the YUI onDOMReady event to get something to happen as soon as possible before the page renders. If you add x.style.display = 'none', or perhaps x.className += 'hidden-section'; or something, that will work fine. Check out the docs for the event utility.
Dear Frank and Matt,
Thank you
- I will have a look.
Cheers,
Gareth
Thank you

Cheers,
Gareth
Just use the database! That is want databases are for: storing data and retrieving it efficiently. And if you store it in cookies instead, then Moodle will behave weirdly for people who log in from different computers.
In Moodle we have the user_preference table to store things like this, which you access using the get/set_user_preference functions. In Moodle 2.0 I wrote some code to make that possible to do from JavaScript code. Look at the function print_collapsible_region_start() function in http://cvs.moodle.org/moodle/lib/weblib.php?view=markup, and the functions it uses, to see a good way to implement this sort of thing. You should be able to steal most of that code and use it in your Moodle 1.9 plugin.
In Moodle we have the user_preference table to store things like this, which you access using the get/set_user_preference functions. In Moodle 2.0 I wrote some code to make that possible to do from JavaScript code. Look at the function print_collapsible_region_start() function in http://cvs.moodle.org/moodle/lib/weblib.php?view=markup, and the functions it uses, to see a good way to implement this sort of thing. You should be able to steal most of that code and use it in your Moodle 1.9 plugin.
Dear Tim,
Thanks. I have looked at markup code and have attempted to figure it out. However, is there a way for the client side Javascript to call the server side Javasript get/set_user_preference functions?
This is because of the nature of the implementation of the page where only client side Javascript is manipulating the page through the DOM and the server has no idea of the state of each topic - toggled or not.
I see your point with the database instead of cookies etc. But I wanted to eleminate the performance overhead on the server of lots of XML RPC calls happening to inform the server of a change in the state of a topic when the issue is a cosmetic entity at the client end. However if the XML RPC solution is efficient, then I will have a bash - help!
Thanks,
Gareth
Thanks. I have looked at markup code and have attempted to figure it out. However, is there a way for the client side Javascript to call the server side Javasript get/set_user_preference functions?
This is because of the nature of the implementation of the page where only client side Javascript is manipulating the page through the DOM and the server has no idea of the state of each topic - toggled or not.
I see your point with the database instead of cookies etc. But I wanted to eleminate the performance overhead on the server of lots of XML RPC calls happening to inform the server of a change in the state of a topic when the issue is a cosmetic entity at the client end. However if the XML RPC solution is efficient, then I will have a bash - help!
Thanks,
Gareth
That's what all this Ajax stuff is for surely? Look at the Yui library that comes with Moodle. The Connection Manager I would think....
http://developer.yahoo.com/yui/connection/
http://developer.yahoo.com/yui/connection/
Dear Howard,
Um, thanks, but I am having difficulty understanding 'how' to actually implement a request within JavaScript will set and get user preference data. All I actually want to do is persist one boolean per topic per user. I have a little JavaScript experience and am currenlty reading a book to learn. However, I am an experienced programmer: Java, C++, C etc. so know what I am doing from a Software Engineering point of view.
Please would you be so kind as to point me towards some documentation and examples that demonstrate how AJAX can be used at the client end to make requests and recieve information back from a web server running Moodle and what the requests need to be from a Moodle point of view.
Many thanks,
Gareth
Um, thanks, but I am having difficulty understanding 'how' to actually implement a request within JavaScript will set and get user preference data. All I actually want to do is persist one boolean per topic per user. I have a little JavaScript experience and am currenlty reading a book to learn. However, I am an experienced programmer: Java, C++, C etc. so know what I am doing from a Software Engineering point of view.
Please would you be so kind as to point me towards some documentation and examples that demonstrate how AJAX can be used at the client end to make requests and recieve information back from a web server running Moodle and what the requests need to be from a Moodle point of view.
Many thanks,
Gareth
Ah, I see. If your code (JavaScript) is purely client-side, then just storing all your data in a cookie would let you do an entirely client-side solution. That is probably the simplest solution.
Dear Tim,
Thanks
Gareth
Thanks

Gareth