XML for making editing forms

XML for making editing forms

by Tim Hunt -
Number of replies: 50
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
There has been some discussion about changing how editing forms are produced in Moodle. I think this has mainly been in the accessibility forum so far.

At the moment, every bit of Moodle makes its own forms for editing by outputting HTML, so there is loads of very similar code all over the place. So if, for example, you wanted to improve the accessibility of all Moodle forms, you would have to go and hack HTML all over the place.

Obviously the accessibility people don't want to do that. Also, perhaps, as developers we would like to stop copying and pasting so many similar bits of HTML, and be able to make our forms much more easily.

So the suggestion is to have a simplified format for describing the form you want at a high level, and then have a library to generate all the repetative HTML.

One approach that is being talked about is XForms, which managers like becuase it is a 'standard'. However, in my opinion, it is an unnecessarily complicated standard that looks like it was designed by a committee (becuase it was).

The other approach, and we (mainly sam) have started doing this at the OU, would be to make up our own Moodle-specific form syntax. Here is a simple example:
<editform langfile='resourcepage'>
     <item type='text' name='name'/>
     <item type='html' name='description'/>
     <visible/>
</editform>
And to display the form you would do something like:
$xf = xml_form::load_file('editresourcepage.xml'); // This gets the form def from a file. You can also get it from a string.
$xf->set_init_html_editor(true);
$xf->set_hidden('moduleid',0); // adds a hidden field
$form = new stdClass; // To hold the initial value for each form field.
$form->name = 'A name';
// ... initialise the other $form fields too ...
$xf->show(basename(__FILE__), $form);
That would generate a typical create/edit module form. The item type says what sort of control to print (text box, dropdown, html editor*, etc.). The text to use as a label is looked up in the lang file resourcepage.php as field_name, and the help ? icon links to resourcepage/name.html. You can override both these using extra attributes, such as help='' to omit help icon altogether.

<visible/> generates a standard visible to student option by calling print_visible_setting.

Basically, the whole point is to minimise the amount you have to type to get your form. This is just about generating the HTML. You still have to do the required_param, optional_param stuff to process the results, like now.


Here is a more complex example:
<editform langfile='block_newsfeed'>
<item type='text' name='location'/>
<item type='text' name='name'/>
<item type='text' name='pres'/>
<item type='html' name='summary'/>
<item type='dropdown' name='type'>
<option value='internal'>type_internal</option>
<option value='external'>type_external</option>
</item>
<group requiredname="type" requiredvalue="external">
<item type='text' name='url'/>
</group>
<group requiredname="type" requiredvalue="internal">
<item type='date' name='startdate'/>
<item type='dropdown' name='public'>
<option value='1'>access_public</option>
<option value='0'>access_private</option>
</item>
<item type='text' name='defaultauthid'/>
<item type='multitext' name='optionalauthids'/>
<item type='users' name='posters'/>
<item type='users' name='approvers'/>
<xhtml><![CDATA[
<a href="editincludes.php?newsfeedid=<?php print $form->newsfeedid ?>">Includes</a>
]]>
</xhtml>
</group>
</editform>

Things to note are:

  1. An example of how you do a dropdown. Again, option names like type_internal are looked up in the lang file automatically.
  2. The <group> stuff shows or hides different bits at the end for the form depending on the dropdown setting.
  3. More interesting types. For example type="users". This now has some AJAX stuff to check the username in the database as you type.
  4. You can include arbitrary HTML (and PHP) using the <xhtml> tag. So if the library cannot generate the HTML you want, you can just add it.

This library is still evolving as we find new things we need it to do and implement them. The question is, is this a sensible approach, and should we be thinking about tidying it up and contributing it back to the community? Or is the community going to adopt an alternative approach, as we should stop developing this.

As I say, my colleague sam did most of the work, and I have just used this, and found it really made my life easier.

Tim.

P.S. sorry this is such a long post.
Average of ratings: -
In reply to Tim Hunt

Re: XML for making editing forms

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
That does look pretty nice, but to me even XML seems to be too complicated.   PHP developers can most efficiently use PHP to create data structures, we don't need to translate them to XML.

The approach I've been planning for a long time (and hoping to include in the upcoming accessibility work) is to use a library to convert PHP data structures into HTML.

There are several of these, but the most likely contender is HTML Quickform, because it's part of PEAR, is mature, extensible, accessible, and supports client and server-side validation, multi-page forms etc. It's more standard than most, and widely used among PHP developers.
In reply to Martin Dougiamas

Re: XML for making editing forms

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
OK, I'll take a look at that. I agree with you that XML is not the nicest format to type.

Whatever we use, I think it is worth customising it for Moodle, to do things like automatically looking up strings in the appropriate lang file and automatically generate appropriate moodle-style help links; and to have moodle-specific data types like username.

It would certainly be good to use the same set of logical field types (text, int, html, username, date, ...) for both specifying the type of control in the form (textbox, dropdown, ...) and the type of validation that is done when the form is submitted. That might mean re-implementing require/optional_param as wrappers round the new form library during the transition.

The correct optimization here is to make it as easy as possible for Moodle developers.



P.S. Hey! the rich-text editor doesn't work in Safari. That sucks! No wonder we want to replace it.
In reply to Tim Hunt

Re: XML for making editing forms

by Martín Langhoff -
MartinD beat me to it, so +1 on his comments. I would add that while Sam is up there with Larry Wall and Guido Van Rossum (adding some XML too!), keeping Moodle simple to work with means not using application-specific mini-languages.

sad

I've been looking both a QuickForms and at the XForms stuff, I think we can have a good chat about this at the MoodleMoot UK. I agree that they are a good thing, but I am concerned that they assume an MVC-style application, which Moodle is not. Maybe we want to turn Moodle towards MVC... I'm wary though as some of the flexibility in the modules/blocks/filters scheme is there exactly because it is _not_ MVC. And I don't think we can lose that.

So I am trying to think how to keep Moodle's flexible approach and yet have more structure around forms creation, and possibly other bits of HTML too.

One possible structured but non-MVC model is what the CGI.pm Perl Module has. Give me some beer when we're out in MK and ask me about it wink

BTW: TinyMCE does work on Safari, is anyone making a move to replace HTMLArea with TinyMCE? Yeah!
In reply to Martín Langhoff

Re: XML for making editing forms

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
TinyMCE is already in 1.6 actually, just disabled.  Janne and I were fully planning to replace HTMLarea in 1.6 but just could not get the performance to anything comparable without major work.  "Tiny" MCE is huge and slow by default.

For Quickform, I think we just need to tackle low-hanging fruit first, like the forms for adding new activity modules, then move on to other areas one at a time.  We don't have to use all the features in Quickform, but just moving to a consistent renderer will be a great first step.
In reply to Martín Langhoff

Re: XML for making editing forms

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
I don't understand why MVC should restrict anything. I have written plenty of Java swing apps which ore of less force you to do it the MVC way, and have never hit any inherent limitations.

And actually, with a generous interpretation, Moodle is quite MVC already. For example, in a module:

The Model is the database tables.

The view layer is in the index.php and view.php files, etc.

And the controller layer in in the lib.php file, etc.

Of course, it is not really MVC, because the layers are not very well encapsulated and they need to know too many of the inner workings of each other.
In reply to Martin Dougiamas

Re: XML for making editing forms

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
I think switching to HTML Quickform would be a big change, and having read through all the docs and looked at some of the examples, I don't the benefits would be all that great. Particularly, not all that great for Moodle. Some of my concerns:
  • I think it would be quite hard to make an output renderer that used lang string from our language file setup.
  • The client side validation does nothing until they click the submit button, and then it pops up an alert. That is a really sucky interface for client-side validation. The state of the art is much better.
  • There is no support for lists of things. For example in the quiz: http://demo.moodle.com/question/question.php?category=5&qtype=shortanswer. You have a number of groups of elements (answer, score, feedback). At the moment is hardcoded to 10 groups. What I would like is to start with a few, and if you fill all those in, more groups magically appear by JavaScript, so you can have an arbitrary number. This would also be good for lists of usernames, which is a simpler example, since it is only a list of single fields, not groups.
  • At the same time, HTML QuickForm is too flexible. If we want moodle forms to look consistently good, then QuickForm give developers the power to make a horrible mess.
  • The QuickForm syntax is worse than XML. I would much rather type Sam's XML format than something like:
      $form = new HTML_QuickForm('frmTest', 'get');
      $form->addElement('header', 'MyHeader', 'Edit username');

      $form->addElement('hidden', 'user_id');
      $salutations = array("0"=>"Mr",
                           "1"=>"Miss",
                           "2"=>"Mrs",
                           "3"=>"Dr",
                           "4"=>"Sir");
      $form->addElement('select', 'sal_id', 'Address me as:', $salutations);
      $form->addElement('text', 'firstname', 'First name:');
      $form->addElement('text', 'lastname', 'Last name:');

      $form->addElement('reset', 'btnClear', 'Clear');
      $form->addElement('submit', 'btnSubmit', 'Submit');

      $form->setDefaults($user); // User object has fields user_id, sal_id, etc.

      $form->display();
So, to make HTML quick form do exactly what we want would require so much customisation, I think it would be less work to roll our own moodle-specific solution.

Especially because we would not need to start froms cratch. We could start from sam's code which generates the right sort of output to replicate most of Moodle forms, and just change the input format it uses to something we think will be easiest for developers to use. That's actually an easy change becuase it is just changing the one function that loops through the XML structure and calls functions to output each bit is a swich statement. Looping over a different data structure - no big deal.


In reply to Tim Hunt

Re: XML for making editing forms

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Actually, the key thing, before we argue about possible solutions, is to agree what we are trying to achieve. Which I think boils down to two questions:

How do we want Moodle forms to look in future?

Or: Do we want Moodle forms to look significantly different in future to how they look today?

I think the answer is we want the to go on looking pretty much the same. Maybe tweak them a bit if we think that makes the look a bit better. And certainly improve the underlying HTML from the accessiblity point of view. But what we have got currenly looks pretty good and is familiar, so I say don't change it.

What syntax do we (developers) want to type make our forms?

I think we want to minimise keystrokes, as long as it is not at the expense of clarity. I think sam's XML format is pretty good in that regard. However, if someone can come up with something better - great.

And I don't understand Martin's apparrent lothing of XML. Sure some XML formats you would never want to type by hand. But XML formats that are designed for hand typing are fine. After all, most of us are happy to hand-type HTML. Well most of HTML, like simple marked up text. We don't want to type the horrible bits like tables and complex forms. Which is where this all started ...
In reply to Tim Hunt

Re: XML for making editing forms

by Martín Langhoff -

I think the answer is we want the to go on looking pretty much the same.

+1 -- and with the developer still in control of each specific form look and feel

What syntax do we (developers) want to type make our forms?

One that is familiar. You don't only learn about HTML, you also learn about the bugs and compromises in HTML parser implementations. Custom XML means we also have to learn about the XML parser bugs and compromises, and how those interact with the (X)HTML parsers out there.

In short, we are talking very leaky abstractions here as Joel would say. http://www.joelonsoftware.com/articles/LeakyAbstractions.html

The same is true about something like Quickforms. I favour something really thin and simple that helps us in conformance, and in a switch to something more advanced like xforms later.

We don't want to type the horrible bits like tables and complex forms.

But we can't really hide too much of that if we want to control how our forms look & feel sad

I don't think Quickforms is the right answer either as it adds yet another templating mechanism, it is just a place to start looking. Perl's CGI.pm still looks better to me...

In reply to Martín Langhoff

Re: XML for making editing forms

by Thanh Le -

In response to Martin...

For the Forms work being suggested, I dont think the asbtraction metaphor and the "leaky abstraction" concept applies...

With the Form issues discussed here we should not be trying to take away the ability of the developer to do what he has done in the past or want to do in the future....

The aim is to provide the same features (and definitely more) but encourage a clean separation of data from processing from presentation.  That is separate the key components of Forms processingl; not abstract the existing Form functions.

Currently Moodle does data, processing and presetntation in one (or more) hard-coded PHP files.

The aim is to do the same but separate out the elements; i.e. data in XML (maybe?), processing in PHP (or XSLT and XForm syntax?) and presentation in XSLT ideally (but PHP also valid; i.e. through QuickForms).

If in making this separation, we make past effort impossible or the development onerous, then we have failed in our solution design effort...

Thanh.

In reply to Thanh Le

Re: XML for making editing forms

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Thanh, can you shows us what the XForms representation of one of the typical Moodle forms might look like. Perhaps the first few fields in the "Adding a new forum" form. Thanks.
In reply to Tim Hunt

Re: XML for making editing forms

by Thanh Le -

I'll see if I can find time today to look at one example as requested... otherwise within the coming days.

Thanh.

In reply to Tim Hunt

Re: XML for making editing forms

by Thanh Le -

Hi,

My portfolio project has kickstarted this whole forms debate and from this work I have looked seriously at XForms and PHP form modules such as QuickForms for the last 2 months.

In response to Tim's question....

I agree that QuickForm is not the immediate answer.  Conceptually as an abstraction layer from Form control to Form Presentation (in HTML), QuickForm is very useful.  But its current implementation of writing PHP code syntax is very tiredsome indeed.  So if QuickForm is to be mentioned, I would garner from it only it's conceptual role not its actual implementation.

I think XML syntax is the way forward.  Sam's XML syntax is perfectly good.  But its one syntax amongst many that any one could make up.  that's why I favour XForms.  XForms is just XML syntax; but at least this work is supported by W3C.  Some may mock and joke at "design by committee" but who brought you HTML?  Who brough you CSS1, CSS2, and CSS3?  Who has defined our Accessibility requirements that we now work tiredless to comply with?  Dont dimiss standards because they take a while to develop.  They take a while to develop because they need to be good and stable and ussable.

XForm can match Sam's XML, but XForm has a cleaner separation of data model from Form control through a "binding" layer that can include data validation.  This tri-part separation keeps data separate from Form control and keeps logical procssing separate from data and Form control.  This gives great flexibility for developing complex applications and allowing them to change with least impact.  I dont think Sam's XML has gone for the tri-part XML separation.  Its a subtle addition but powerful all the same.

Finally XForms syntax is richer in features and supports the desribing of:

1) language to describe validation and XML Schema for data model validation;

2) events; i.e. trigger action in reponse to something

3) Multi-form paging and routing; i.e. create Form wizards

4) Etc.

These features are optional for usage but at least the ability to build more complex forms are there.

So XML is my preferred syntax.  Given the choice of any XML syntax, lets pick a standard with pontential.

Thanh.

 

In reply to Thanh Le

Re: XML for making editing forms

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
who brought you HTML?

Tim Berner's Lee.

Then it was pulled in conflicting directions my Netscape and Microsoft.

The W3C came together as a committee to pull together the parts and clean up the mess.

But still, the single underlying concept of a single mind is at the heart of it. And TBL was helped by building on a lot of established good practise developed in the SGML community.

Who brough you CSS1, CSS2, and CSS3?

Didn't actually know the history of where this originated. Wikipaedia suggests that agan the core ideas and unerlying unifying concepts came from Håkon Lie and Bert Bos.

Then a committee gave us CSS2, which was so big and complicated that no-one ever implemented it, and they eventually had the wisdom to scale it back to CSS2.1

Who brought us JavaScript?

Brendan Eich. Another successful standard that was initially the concept of a single mind.

Who brough us Java?

James Gosling, with help from one or two others.

Ruby?

Yukihiro Matsumoto.


I am only really confident of my facts on one design by committe standard:

IMS QTI

Designed my committee. Very complex. No one has got to even a nearly complete implementation. Certainly there are not two interroperable ones yet. And the only peole who talk about using it are managers, not technical people.


I'll refrain from citing examples because I don't know any others well enough (but I could have thought of many more single-mind-originated standards that are popular: Perl (but where is the design-by-committee Perl 6?), C, ...).


Anyway, my thesis is that good standards have to have a consitent underlying concept that holds them together and makes them easier to understand. Therefore, in practice, they have to come from one, or exceptionally a few, fery bright brains. This brain needs to have the benefit of an established body of knowledge to build on. And they need peer review of their ideas, but they don't other people trying to shove completely different ideas into the standard.

Once version 1 of the standard is written, and it has been adopted by enough different people who understand the underlying concept, then it is then safe to hand it over to a committee to maintain it.


My thesis is all very well for standards that you did not know you needed until they were there (HTML, JavaScript, C, etc.) they arise spontaneously from someone scratching thier own itch, and are then adopted when people realise how good they are, and you never hear about the failures.

It is not much good if are a group of different (competing) companies who all agree that you would mutually benefit from having a standard. If you can find and agree on a pet genius, will they be able to deliver on demand? And what will you do if they come up with a concept that seems to favour your competitors more than you?
In reply to Tim Hunt

Re: XML for making editing forms

by Thanh Le -

I think my point has been missed...

I'm not defending "design by committee" or saying any great idea cannot come from one person or come at us by surprise.

I'm arguing that a body as big and as influential as W3c has spent time and energy on XForms effort, and a group of people (commercial and personal) have expended "thinking" and anlaysis to get us to XForms v1 second edition.  my point is why waste their effort?  We can start from scratch and make up own models, our own XML, own own everything and I believe we will end up with the same solution.

If the acutal proposal (XForm) fails, we should not have lost anyting if we design our solutions right.  In this case, we are not blindly saying we must conform to XForms (for example), but doing it because its ideas are solid and its solution is good.  If XForms (for example) dies, then you stand on the merit of the solution done and ignore the alternative unless there is strong reason to.  To change simply because of a temporary wind change is so no-no! 

We need to start somewhere and the point is starting with somehting that has a good basis as opposed to starting from scratch and reinvent the wheel because we can.

I can go with any solution and any specification, if the merit is good.  But I would favour one that has gone through a serious anylasis and have group effort/support that have many disparate views such that any merits, flaws and inconsistencies are at least highlighted now as opposed to later.

This is just my "philosophical" viewpoint... key message is not reinvent the wheel unecessarily.

 

 

In reply to Thanh Le

Philosophy

by sam marshall -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
I haven't said anything in this thread yet so first to put one comment in the wrong place... I do object to the bizarre anti-XML preconceptions that have been mentioned. Nowadays the question for a given situation shouldn't be 'why use XML', it should be 'why not use XML' (i.e. if you need to represent structured data you should be using XML unless there's a really good reason otherwise). We know that XML works reliably and quickly, all developers know the basics so it's instantly familiar (at least when using a simple human-readable format), and it generally does the job... however... perhaps there are some issues. For example, my code - which is pretty crappy and would need refactoring before community use anyway - uses the PHP5 DOM functions. PHP4 support and the need for extensions or libraries could be a limitation on XML usage (but frankly I don't think it ought to be, I'm certain it's possible to reliably use XML in PHP4, even if it means putting something else in lib/).

I absolutely agree that if you are simply developing forms that store data (with no other preconceptions) then XForms is a good way to go. That is the situation for the portfolio Thanh is developing which allows users to create custom forms that store arbitrary information about themselves and does not have any particular integration to Moodle - for example, the labels on those forms would be typed by the users and would not come from Moodle language files. It might also be appropriate for other similar situations such as perhaps the database module's form editing, etc.

But the philosophy behind my design is different and, I thought, rather easier to embed into Moodle. It's a slot-in replacement to handle Moodle's existing forms for things like module settings, and to decimate the existing redundancy there. You do not need to change any code other than the place where the forms are actually printed. The forms look the same and it automates the language file support, which is one of the key redundancies in the existing system (the other one being all the html table guff, which is obviously gone). Importantly, it's also very lightweight on top of the existing model; if a particular form contains some kind of control that isn't supported in the XML, you don't have to implement it as a new item type, you can simply do

<xhtml><[CDATA[
// existing code pretty much e.g.
<input type='file' name='upload' />
<?php print_string('uploadinfo'); ?>
]]></xhtml>

So it's easy to gradually transition from existing forms to the new behaviour.

The question is whether an approach like this (fixing the critical problem with the current system - redundancy, corresponding inability to make overall changes - and adding a few features, but leaving it working fundamentally the same way), or a more fundamental change such as adopting the XForms model, is appropriate at this point.

I think in-house we're going to argue about it tomorrow. smile

--sam
In reply to sam marshall

Re: Philosophy

by Thanh Le -

Thanks for your post and input Sam.

I agree with your perspective and the role your development has for Moodle now in comparison to XForms and for my portfolio projects.  Nothing to add other than 100% agree.

I would favour and support Sam's work as an intermediate bridge for existing Moodle forms in general and offer XForms (or similiar thing) for more specific FORMS applications such as the database form module.

Maybe the XForms (or similiar thing) will never make it into core; but the choice and its avialability should be seen as a bonus.

Thanh.

 

In reply to sam marshall

Re: Philosophy

by David Scotson -

What an interesting thread! Is it really true that Moodle is the only PHP project trying to create web forms that are:

  • consistent
  • valid XHTML
  • accessible
  • CSS styleable
  • AJAX'ble (e.g. allowing the user create 1 or 10 entries without a reload)
  • integrating with both client- and server-side validation
  • translatable (including i8n/l10n things like number and date formats, rtl text etc.)
  • integratable with WYSIWYG HTML editors
  • easy to create (both by programmers making 'specs' by hand in PHP or XML and in code e.g. for database module)

and so writing one (from scratch) only for Moodle makes sense?

Also, I'm not terribly familiar with XForms but how does this all relate to WHATWG's Web Form 2.0 stuff and related specs?

And also, on the topic of irrational XML-bashing and while I recoginize it's not a great argument, but aren't all the cool kids moving away from XML as format to be written, read or edited by humans? (Personally I much preferred the PHP example given above, and thought that most of it would only need to be typed once and stored as a variable, e.g. various salutations)

In reply to David Scotson

Re: Philosophy

by Thanh Le -

WHATG is a spawned off project from XForms.  The WHATWG proposal and XFORM share similiar goals and general architecture.  I would say the former is more hooked to using Web 2.0 technology and are being immediately pragmatic whereas XForms/W3C is attempting to be more pure and abstract in concept and leaving implementation to others.

So they are complimentary.

I would focus on the common goals of both and then find a solution that is simple to deploy whether that will be XForms, WHATWG's work, hybrid of both, or our own.

 

In reply to David Scotson

Re: Philosophy

by sam marshall -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
Nobody serious is moving away from XML, no. (I'm sure there are quotes from a few clowns who are, but they can be safely ignored, however eminent they may appear.) XML is very easy to human-edit compared with the 17,000 different programming/templating languages, each with their own different syntaxes for escaping etc., and is also very easy to machine-read/write/manipulate in every programming language. That doesn't mean that every XML syntax is human-readable or that XML is the only good solution, but it does mean that it should be the default position for representing structured data.

As for the 'cool kids', they're always to be avoided.

The WHATWG do sometimes appear to fall perilously close to the 'cool kids' group in their intention to do something any possible way other than the right one... but they do have some justification for their pragmatic approach. As I understood it (haven't reread recently), their plans mainly involve adding extra components to HTML forms such as a rich-text editor - obviously this in particular is definitely worth watching in future - and extra control attributes that let you make things like basic required fields without needing Javascript. I don't think they're making an attempt to actually solve any fundamental problems with the model, as XForms does.

Here's the relevant section of the document you linked that explains the relationship with XForms.

--sam


In reply to sam marshall

Re: Philosophy

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Have you got shares in The XML Corporation, Sam? tongueout
In reply to Howard Miller

Re: Philosophy

by sam marshall -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
XML's a free, published standard, so no. ;p

Sorry if I'm coming across as a bit bombastic on this (and everything else smile) but it's like.. everywhere else in the world XML is a no-brainer, so I don't understand why it's a problem here. smile

--sam
In reply to sam marshall

Re: Philosophy

by David Scotson -

To be fair XML has a lot of different uses, for many of which it is indeed the no-brainer solution. However, this particular situation I think it's non-controversial to say that idiomatic Python or Ruby programmers wouldn't use XML (they'd use Python or Ruby) while say Java programmers would use XML. Compare, for example Ant and Rake.

The big question then is whether PHP is closer to Java or Python and Ruby in this regard.

I was going to point to a nice blog article on this topic but it appears to have disappeared from the internet so I'll liberally copy and paste below:

XML is not the answer. It is not even the question. To paraphrase Jamie Zawinski on regular expressions, "Some people, when confronted with a problem, think "I know, I'll use XML." Now they have two problems."

This is a different situation than in Java, because compared to Java code, XML is agile and flexible. Compared to Python code, XML is a boat anchor, a ball and chain. In Python, XML is something you use for interoperability, not your core functionality, because you simply don't need it for that. In Java, XML can be your savior because it lets you implement domain-specific languages and increase the flexibility of your application "without coding". In Java, avoiding coding is an advantage because coding means recompiling. But in Python, more often than not, code is easier to write than XML. And Python can process code much, much faster than your code can process XML. (Not only that, but you have to write the XML processing code, whereas Python itself is already written for you.)

If you are a Java programmer, do not trust your instincts regarding whether you should use XML as part of your core application in Python. If you're not implementing an existing XML standard for interoperability reasons, creating some kind of import/export format, or creating some kind of XML editor or processing tool, then Just Don't Do It. At all. Ever. Not even just this once. Don't even think about it. Drop that schema and put your hands in the air, now! If your application or platform will be used by Python developers, they will only thank you for not adding the burden of using XML to their workload.

(The only exception to this is if your target audience really really needs XML for some strange reason. Like, they refuse to learn Python and will only pay you if you use XML, or if you plan to give them a nice GUI for editing the XML, and the GUI in question is something that somebody else wrote for editing XML and you get to use it for free. There are also other, very rare, architectural reasons to need XML. Trust me, they don't apply to your app. If in doubt, explain your use case for XML to an experienced Python developer. Or, if you have a thick skin and don't mind being laughed at, try explaining to a Lisp programmer why your application needs XML!)

from http://dirtsimple.org/2004/12/python-is-not-java.html (currently unavailable)

The point of the above isn't to convince anyone who's not convinced already but just to point out that it's perhaps a cultural/language thing, and certainly not Moodle-specific.

And finally, from the incomprehensible yet hilarous launch announcement of the Camping micro-framework:

XML situps diagram

In reply to David Scotson

Re: Philosophy

by Tony Hursh -
Or, if you have a thick skin and don't mind being laughed at, try explaining to a Lisp programmer why your application needs XML!


http://c2.com/cgi/wiki?XmlIsaPoorCopyOfEssExpressions

smile smile smile

In reply to David Scotson

Re: Philosophy

by Martín Langhoff -
> I think it's non-controversial to say that idiomatic Python or
> Ruby programmers wouldn't use XML (they'd use Python or Ruby)
> while say Java programmers would use XML.

I think it's totally crazy to put XML at the core of your application. So this is kind of pointless.

XML is for data exchange both structured and unstructured. It is not good for the basis of a programming language, auwful for internal message-passing, and it is not good for most core data storage needs.

However, it did lead to the rise of unstructured data storage engines (Lotus Notes-like) on top of XML data representations and special query languages suited for unstructured data (like XPath). Unstructured data made a comeback with it, after being pushed aside by RDBMSs and the success of SQL.

So today, if you want to use a good unstructured data storage, you end up using XML, even if it doesn't make that much sense technically. It does because people understand it, which in the end is a valid point.
In reply to Martín Langhoff

Re: Philosophy

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
It's rubbish for config files too!! Back in my Java/XML days we soldiered on for ages with XML based configuration files, but the plain fact is that old fashioned .ini files or even a long list of assignments (hello Moodle config.php) is easier to write, easier to maintain, and (most importantly probably) an order of magnitude easier to understand in the first place.

I don't care how elegant a solution is, if nobody can understand it it won't get used. This is Java's failing - it's all a bit too clever for it's own good and XML based solutions often make the same error. XML is terribly useful in certain situations but it certainly is not a panacea for all ills and I'm never writing any more myself, even with an XML editor!!
In reply to sam marshall

Re: Philosophy

by Martín Langhoff -

Hi Sam!

I love XML, and it is a great tool for what it was designed: exchange data between systems. It was meant to be a the basis for replacement formats for data exchange files, like CSV files. And it has been a great success.

Every time I have to deal with a an ill-defined data file format like CSV I cry bitter tears. People send me CSV files with random newline terminators, unescaped commas, and no definition of WTF the encoding is. Those programmers should be shot with a silver XML bullet.

Now XML has been such a great success (and had such a great marketing) that people want to use it for things it is not ideal for. And I want to stay away from that.

XML for XForms (as something to be embedded in XHTML) is a great match. The only concern is that XForms is not there yet on the browser side, and we have no idea if/when it will be supported consistently enough to make it worthwhile.

However, when you are programming, programming constructs are a better fit than XML constructs. Nothing against XML/HTML, but

   print $html->title($var);

is a whole lot better than

   print echo '<title>' . htmlentities($var, ENT_COMPAT, 'utf-8') . '</title>';

for a lot of reasons (that's Perl's CGI.pm model that I've been talking about). It

  • The function will do the right escaping for you (html escaping, url escaping, etc)
  • Syntax checkers (and syntax colouring) have a much easier time -- hybrid PHP/HTML syntax checking can never tell if you've forgotten the closing tag, or if they don't match
  • Easier to grep/sed
  • Less typing too

If XForms was prevalent, well supported and well known by programmers we'd probably make the switch straight away to take advantage of the browser support. Just like we have done with CSS. (BTW, look at the time scales of CSS from 1.0 spec to early implementations to reasonably consistent implementations across browsers).

In the meantime, changing from well understood unmediated HTML to custom XML that is Moodle-specific isn't so interesting. We lose clarity for newcomers and the fine control we have today.

I do think we can get some benefits from having functions for base HTML elements, similar to Perl's CGI.pm. It is definitely nothing fancy, and it might help. Just a couple of functions added to weblib wink

In reply to Martín Langhoff

Re: Philosophy

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
So with this sort of thing, our very first example changes from:

$xf = xml_form::load_string('
<editform langfile='resourcepage'>
<item type='text' name='name'/>
<item type='html' name='description'/>
<visible/>
</editform>
');
$xf->set_init_html_editor(true);
$xf->set_hidden('moduleid',0);
$form = new stdClass; // To hold the initial value for each form field.
$form->name = 'A name';
$form->description = 'Blah-blah-blah';
$form->visible = 1;
$xf->show(basename(__FILE__), $form);

changes to somehting like:

$form = new stdClass; // To hold the initial value for each form field.
$form->name = 'A name';
$form->description = 'Blah-blah-blah';
$form->visible = 1;
print_form_start(basename(__FILE__));
print_hidden('moduleid',0);
print_text_field($form, 'name');
print_html_field($form, 'description');
print_visible_setting($form, $course);
print_form_end();
use_html_editor(...);

Is that what you are suggesting?

For a simple example, it is a slight win, and it could be tidied up further.


However, I have two quiz-related use cases, where this approach of just calling functions to output stuff is not really good enough.

1. In the questiontype base class, I want to generate part of an editing form, but then in each actual question type, I want to take that basic form and tweak it a bit. For example add some new form fields at different places (not just at the end) and maybe hide some fields that are not relevant to this type.

This requires building an in-memory representation of the form that can be manipulated before it is dumped out all at once.


2. The example I gave earlier where I wanted an unspecifed numer of groups of elements, with client-side JavaScript to generate more groups as necessary.

This again, needs some sort of in-memory structure representing the required grouping that you can pass around.

In reply to Tim Hunt

Re: Philosophy

by Martín Langhoff -

The change would be to something more like

 $html = new WeblibLikeClass;
 print $h
 print $html->h3('A name');
 print $html->p('Blah-blah-blah');
 // you can also use plain old
 // echo '<table>' if you want/need
 // Note: some elements may have a _start
 // method if they are likely to include a large
 // chunk of other HTML code
 print $html->form_start(array(method=>'POST', action=>$CFG=>wwwroot.'/mod/foo/index.php'));
 // not strictly a tag, but a shortcut:
 print $html->input_hidden('moduleid',0);
 print $html->input_text($form, 'name');
 print $html->input_html($form, 'description');
 print $html->input_checkbox($form, $course);
 print $html->form_end();

In the questiontype base class, ...

I think you have a problem in the separation of processing in your base and subclass methods. These things are messy (and one of the reasons I strongly prefer to avoid subclassing) but are not the problem space of the html widget printing facility.

The example I gave earlier where I wanted an unspecifed numer of groups of elements

We could add a method/function to make things like that easier. It is it's AJAXy stuff though wink

In reply to Martín Langhoff

XML vs PHP for creating forms in Moodle

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
There's little advantage in such incremental output over our existing weblib functions ... the idea is to create the entire form structure first, then print it ... the Quickform example that Tim showed above in this discussion is exactly the kind of thing we need.  I think that example describes the form very clearly, and we could easily clone the object and tweak it etc.

Once we have a structure, we can choose to output it in different ways, or use it to create validation etc.   It's the main thing that is good about XML too, but I totally agree with the point of view that XML is not the best format to do this inside an application.  Moodle forms are not something we need to interchange with other systems.

Perhaps we can have both to keep everyone happy.  Sam might like to write a function that converts his XML format into the same data structure within the form object, just as if someone had called all the other little functions to build it up.
  $form = new HTML_QuickForm('myform', 'get');
  $form->addElement('samxml', $pathtofile);
In reply to Martin Dougiamas

Re: XML vs PHP for creating forms in Moodle

by Martín Langhoff -

Perhaps we can have both to keep everyone happy.

Indeed smile

the idea is to create the entire form structure first

Hmmm. Perhaps sometimes, only when needed?

I'll say something a bit crazy: Thinking only about forms is missing a good part of the picture. Any suport library needs to support HTML as we use a lot of HTML for presentation and layout.

So we may as well support HTML mixed

The cases where we need to prepare it and manipulate it in memory can be addressed very simply, and more effectively even, with a simple array.

(I came back from lunch feeling bad about my "that's not my problem" reply earlier, so...) I was just going to extend my reply to Tim saying that for cases where you need that "in memory" form building (and it's just a subset of the cases we haven't really needed it yet) you can use a stack-like array.

So say:

 $html = new WeblibLikeClass;
 $form = array();
 $form[] = array(key=>'intro', 
                 value=>$html->h3('A name')
                        . $html->p('Blah-blah-blah'));
 $form[] = array(key=>'start', 
                 value=>$html->form_start(
                         array(method=>'POST',
                          action=>$CFG=>wwwroot.'/mod/foo/index.php')));
 $form[] = array(key=>'part1', 
                 value=>$html->input_hidden('moduleid',0)
                        . $html->input_text($form, 'name'));
 $form[] = array(key=>'part2', 
                 value=>$html->input_html($form, 'description')
                        . input_checkbox($form, $course));
 $form[] = array(key=>'end', 
                 value=>$html->form_end());

(Arguably, we should use some tricks to boil this down to a more elegant representation. Not trying to be elegant on this one wink )

And in that you have defined 4 sections which are clearly labelled and can be easily overridden. All you need is a little function to do the lookup of the position by key.

Quickforms forces a model that I don't like too much: you have to create templates, which calls for yet another templating scheme sad

In reply to Martin Dougiamas

Re: XML vs PHP for creating forms in Moodle

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
It would not just be about keeping sam happy. I have been thinking about this, and I think sam's XML format is pretty close to optimal for typing the specification of the form. Let's look at an example.
<editform langfile='resourcepage'>
    <item type='text' name='modulename'/>
    ...
</editform>
Actually, that is not how it should be, it should be
<editform langfile='resourcepage'>
    <text name='modulename'/>
    ...
</editform>
There is very little redundant there. The amount of stray punctuation is pretty low too. Moodle developers are used to looking at HTML, so this is not too scary and unfamiliar. Defining PHP arrays or objects needs more keywords and puncuation.

What is the bare minimum we need? Each field in the form needs a type and a name. We get that pretty directly.

However, sometimes we want a bit more. The name= attribute is primarily used for the <input name="" ...> bit in HTML. However, by default it is also used as get_string($name, $langfile) for the label next to the element, and similarly to build the tooltip text on the help button; as "/lang/en_utf8/help$langfile/$name.html" as the help file to link to. However, in some cases you will want to override some of these defaults. For this you can use extra attributes. For example, something like:
    <text name='modulename' helpfile='' size='30'/>
Would say that this field should not have a help link, and it should be 30 characters wide.

In the situation where mostly you want defaults, but occasionally need to override things, I think that XML named attribute syntax is great. It is certainly much easier to use that PHP function calls with optional arguments where the aruments are anonymous, and have to be in a set order so you end up passing lots of meaningless false values to get to the one you want. Of course, you have to type more (the attribute names), but I find that makes it easier to understand.


I feel we are moving towards a consesus with Martin D. I think we can probably agree on:
  1. Forms have an in-memory representation.
  2. There is an optional XML format, optimised for ease of typing, that can be used to create the in-memory representation.
  3. The in-memory representation can be manipulated. I think we should write some helper functions for this to make it easer for people. add_field(), remove_field(), etc. That way most people don't have to know or care what the in_memory representation is.
  4. Then there is code to dump the in-memory representation out as HTML.
I think that only leaves one significant questions, which is what is the in-memory representation? If we can do a good job of point 3. via helper functions, then its reallly only the people writing the forms library have to care, which would be good.

I know that manipulating XML DOM trees in memory can be a pain, but, in the code we have knocked up so far at the OU, this is what we are doing and for this application, it is not so bad. Here is a some of the code from sam's library:
        $children=$parent->childNodes;
        foreach($children as $item) {
            if($item->nodeType!=XML_ELEMENT_NODE) {
                continue;
            }          
            // Call method depending on item type
            $type=$item->tagName;
            switch($type) {
                case 'display' :
                    $this->show_display($form,$item);
                    break;
                case 'text' :
                    $this->show_text($form,$item);
                    break;
                ...


    private function show_text($form,$item) {

        $name=$item->getAttribute('name');
        $size=$item->getAttribute('size');
        if(!$size) {
            $size=30;
        }
        $required=$item->getAttribute('required');

        ...
Nothing too bad in that code to my eyes. Of course, there is a small cheat here, because we are using the nice PHP5 XML library, and we would have us use something a bit more horrible to cope with PHP4.
In reply to Tim Hunt

Re: XML vs PHP for creating forms in Moodle

by David Scotson -

There's a lot of info here about how people envisage the input to the form generation process, but is everyone clear on what the output (that is, the eventual HTML, JS & CSS that will get sent to the browser) will look like?.

I'm just wondering where things like e.g.:

  • making the forms CSS stylable with appropriate class and id names for each form and input,
  • label tags that identify form items and their associated text label for easier clicking,
  • titled sections for accessiblity,
  • altered tab-ordering, or
  • as-you-type AJAX validation (with a second layer of javascript that validates the form as a whole, and a third layer of functionally equivalent server side backup written in PHP) that checks user entries against a validation format and generates appropriate and usable error text to prompt the user to re-enter data

comes in to the equation.

Surely that's why people want to use form generation libraries in the first place, because of the awkward, boring, repetitive stuff like that rather than just HTML generation and escaping. The examples given to contrast the different approaches so far all seem to be at the shallow end of the problem space.

In reply to David Scotson

Re: XML vs PHP for creating forms in Moodle

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
You're right, this is the goal.  These are all things I've assumed are solvable once we can describe a form as a structure in memory.  HTML Quickform is weak on some of this though.  Do you know any other examples of projects tackling this deep end?
In reply to Martin Dougiamas

Re: XML vs PHP for creating forms in Moodle

by David Scotson -

Quickform appears to do most of it out-of-the-box and has built in hooks for customising the rest. It also seems widely used, including being extended and customised in many different ways, as well as being generally well-respected.

It might not be perfect (what is?) but would appear to be the one to beat in this arena, the default option for handling forms with PHP. So maybe it's worth fully exploring what Quickform can't do before asking what the alternatives can.

From my reading of things I'm not sure I agree with Martín's contention that it requires/enforces MVC (in fact Googling seems to find the opposite complaint), or that any of Tim's points against it are unsolvable dealbreakers (e.g. unobtrusive javascript validation, even using AJAX to call PHP validation routines, is effectively orthagonal to the form generation if you can identify the forms with suitable CSS).

In reply to David Scotson

Re: XML vs PHP for creating forms in Moodle

by Thanh Le -

Debating the input and proccessing is challenging enough, never mind the output.   smile

For my specific project, we need to produce many presentations of collected form content; and accessible presentation is one of the many outputs.  It is for this reason that the flexibility of an XML/XSLT (with PHP inclusions if necesary) is seen as the most flexible option that will cater for multi-presentation output and for specififc requriemetns such as accessibility.

I agree, all this discussion amounts to given us flexibility at the output side.  Sadly, the ordering of teh debate means the input and processing cant be ignored.  The input and processing issue, I guess, comes down to one's prefence and requirements for how one sees the content being generated in the first place; i.e. if you want the flexibility of XML/XSLT then the input or somehere in the processing chain needs to be XML.

 

 

In reply to Tim Hunt

Re: XML vs PHP for creating forms in Moodle

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
For more realistic examples you need to consider cases where some of the data is coming from the database (lists, menus, pre-existing data such as rich HTML etc).  Not all forms can be hand-made.
In reply to Martin Dougiamas

Re: XML vs PHP for creating forms in Moodle

by sam marshall -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
Several of my existing forms (will be deployed in our October OU release) already use database information now, so we have a few working examples. smile If we start seriously attempting to put this into Moodle as a whole though it would be good to have examples of typical forms of this nature though so that they could be considered early in order to ensure that support is good. Any suggestions of two or three good forms to look at?


At present my 'dynamic' forms work by loading the basic form file then using DOM to add the required pieces, but Tim is thinking of making functions that can be used to add fields more simply, as he mentioned - DOM processing (while convenient because it's the same in all languages so everyone knows how to do it) is a bit long-winded, in addition to being a PHP5 feature.

It is of course also possible to use the <xhtml> tag with PHP inside to simply print out your necessary form bits, which is useful when you want a 'custom control'. For instance I use this for a 'delete attachments' field which lists all the attachments on something with checkboxes on each (when you submit the form, it deletes all the checked ones). I didn't want to create a separate field for each attachment (with its own label etc), and although I guess I could have made a specific field type 'multiple checkboxes in one field', I was too lazy. smile

Anyhow, doing this should be kept to a minimum for obvious reasons, but when something doesn't fit into the 'standard' types it's pretty trivial to go beyond them.

--sam

PS Big advantages of XML over the 'just call separate functions' idea Martin L proposed include that the form processing system can handle relationships between the items better (e.g. if you need to reference another item, it's easier to error-check that the other item actually exists), and that with XML you can consider using it - for new code only I would think - to assist in automatically validating input in addition to displaying the output. (My current code only displays output but validating input would be an obvious extension.) In addition, storing the information largely as structured data rather than unstructured code makes for an easier transition to other formats if this is ever necessary, such as when browsers have XForms support for example.
In reply to sam marshall

Re: XML vs PHP for creating forms in Moodle

by Martín Langhoff -

PS Big advantages of XML over the 'just call separate functions' idea Martin L proposed include that the form processing system can handle relationships between the items better ... to assist in automatically validating input in addition to displaying the output.

I'd like to have that too, but it is not dependent on XML at all. You can say $fv = new FormValidator; and register things with your form validtor as you create the form for display.

NOTE however that such validation state will need to be marshalled into the session, as the validation happens on your next HTTP request.

In reply to sam marshall

Re: XML vs PHP for creating forms in Moodle

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
Sam, some examples I'd like to see would be:

  • Add resource -> "Link to a file or web site"
  • Add quiz
  • Add database
  • Manage groups for a course
In reply to Tim Hunt

Re: XML vs PHP for creating forms in Moodle

by Martín Langhoff -

Tim,

Still not comfortable with a custom XML format for this:

  • How do you deal with presentation? Take a look at the glossary input forms, and their liberal use of tables. In mod/quiz you have a lot of tables for layout as well wink
  • Your examples look elegant, but are ignoring escaping and conditionals.
  • The in-memory structure can be resolved without form-specific code at all.
In reply to Martín Langhoff

Re: XML vs PHP for creating forms in Moodle

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Take a look at the glossary input form

Well that proves the point. If you give people enough rope, they'll shoot themselves in the foot.

I just love the way that nothing quite lines up with anything else (with different discrepancies on Windows and Mac). And the way that in the categories box, 'Not categorised' is listed, and can be selected at the same time as some real categories, leading to confusing results.

So it would not be easy to make a form like that, but it would be possible by using the output raw html feature that would be there. Or you could decide that you don't want to make forms like that.


In mod/quiz you have a lot of tables for layout as well

I have inherited a lot of tables for layout. And I really wish I hadn't.

By the way, on the question editing page, I am expecting the forms library to support several fields in a row, like grade next to answer.

And pages like the edit quiz page is outside the scope of what we are talking about here.

Your examples look elegant, but are ignoring escaping and conditionals.

Escaping what?

Conditionals are there, if I get what you mean. Look at the second example in the first message in this thread.

The in-memory structure can be resolved without form-specific code at all.

Sorry, I can't work out what you are trying to say here.

Must go to orchestra now. More later.
In reply to Tim Hunt

Re: XML for making editing forms

by Samuli Karevaara -
The form format (whatever it would be) should also have
  • place for the required/optional fields
  • "type email address OR phone number" type of rules
  • hooks for validation code, as it's not always just the required thing or the type (i.e. the zip codes, unique user names etc.)
  • hooks for other special code than just hide/show, based on selections (change the drop-down to have 28,30 or 31 options based on the selected month, for example)
  • still weirder cases, like "auto-focus the password field if the username is prefilled, otherwise focus the username field"
  • etc..
I have the feeling that all generic, however nice and clever, formats either restrict the forms to paperlike forms, or are way too generic and thus have a steep learning curve of their own (the Smarty-path) and the framework/form parser itself is very complicated and heavy to develop.

Like was discussed here, targeted and cleanly named methods to generate the form elements seem to have the least amount of hassle after all. Some "custom code" hooks, for pre- and postprosessing, validation and the javascript UI tricks would be nice too.
In reply to Samuli Karevaara

Re: XML for making editing forms

by David Scotson -

I found an example of various features of Quickform taken from the documentation:

working example

source code for the example

That does a heck of a lot, including defining the layout for certain special form elements and validation but I can't say the source seems particularly frightening even including that.

Regarding fancier validation and AJAX (e.g. checking if a username is taken without a full page reload) note that unobstrusive javascript / progressive enhancement and HiJax (s5 slides)(which I think is the way to go for 'legacy' web apps, such as Moodle, that want nice AJAX effects but require accessibility) actually make it much easier to do this kind of thing since all you need to generate (within the form at least) is nice XHTML with appropriate classes.

In reply to David Scotson

Re: XML for making editing forms

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
the source seems particularly frightening even including that.

The source stars looking a worse if you have to wrap every string in a get_string() call. It would be nice if you did not have to do that manually.

unobstrusive javascript / progressive enhancement and HiJax (s5 slides)

That is exactly the way we have done it so far in our in-house code.
In reply to Tim Hunt

Where we've got to

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Where this debate seems to have got to:

We seem to have some agreement on some general principles from some people.

The OU has some code that we find make producing Moodle editing forms infinitely nicer. It just needs a little cleaning up before it could be checked into moodle.org CVS, and in our opinion it is a good solution for Moodle.org now and into the medium term. (Jason, sam, Nick, Thanh and I had a meeting yesterday where we thrashed this out. We also thought that maybe in 5 years time we can safely assume all browsers support XForms, and that might be when we next need to change.)

I now am starting to work on some improvements to some of the Moodle question types that involve changing the editing forms. I am looking at the raw HTML forms and thinking "I don't want to touch these with a barge-pole". I would much rather switch them all to the OU forms format, which I know and love.

That is, I now have an itch to scratch.

So I would be happy to invest some time cleaning up the OU form code and getting it committed, and, as a trial, converting the question type forms to use it. Then we can all look at it and see what we think with some real examples. (If it is, the OU will probably then be paying someone to convery all the other forms to it as part of the accessibility work for 2.0.)

However, I don't want to invest that time unless there is a reasonable chance that our code will be accepted.

The two Martins, and a bunch of other Moodle geeks, are coming to the OU in 10 days time for the UK Moodle Moot. So we propose to tie them up in a small dark room until they give us our way show them what we have done and how we propose to tidy it up, and see if we can all agree that this is a good way forwards. To that end, I propose to write a brief document describing how our library will work after it has been cleaned up, and get it out by next friday, so people have something to read on their journey here, and our discussion can be well informed.
In reply to Tim Hunt

Re: Where we've got to

by David Scotson -

I had a play with converting the Choice module to Quickform for comparison purposes.

You can check it out on our Moodle, logging in with username/password both 'quickform' and see the code here:

I didn't replace the forms, simply duplicated them so you'll see the normal form followed by a Quickform doppelgänger. (I also moved some code from lib.php back into view.php to make comparison easier).

Some comments:

  • It's a quick hack and not everything works or looks and behaves as it should, but mostly because I just didn't get round to it. I didn't notice any showstoppers that prevented me from doing anything, and was generally quite impressed. It all seemed to 'just work' to a surprising degree.
  • I turned on the HTML editor for the account given above and it appears to work as expected, but I don't really use it ever so ping me if it's acting oddly.
  • I tried to refrain from changing things unrelated to the forms, but I ended up cleaning up some stuff just to check I understood what was happening, and couldn't resist the opportunity to make a couple of changes e.g. get rid of the colons after lable names and make the outputted HTML code very slightly more pleasant in some places.
  • I chose Choice because it not only has a nice range of inputs including variable number of options, javascript disabling of things etc. but the actual view that students see is basically a customised form too.
  • My only complaint about Quickform is that I had to read the source of the default renderer before I understood how to format a replacement template because it didn't appear to be documented anywhere.
  • I didn't bother to hook up the existing javascript stuff but I assume it would just work. I had wanted to investigate Quickform or a 3rd party's javascript to make the addition of extra choices more AJAX-y but didn't get around to it.
  • Another thing I might look at if I get time is passing existing Moodle form code through as HTML. So you could replace the standard stuff with Quickform and then come back later and think about how to recreate/replace the more customised elements in your own good time. I'm fairly certain this would work and reduces the already low difficulty level of incorporating Quickform from 'basically just typing' down to 'trained monkey could do it'.
  • building on the previous point I think the introduction of any centralised form library is a good time to re-assess the decisions that took advantage of the opportunity to be flexible e.g. the Glossary input screen discussed earlier (also see this post for my suggestion of how that UI could be improved). I think accepting the constraints of e.g. one item per line in a vertical list has a host of benefits (accessibility, usability, moodle for mobiles, etc.) at very little cost.
In reply to David Scotson

Re: XML for making editing forms

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
Thanks, David, that example does do a lot.

That's exactly the sort of thing I was hoping to be using.  I'd feel comfortable working with source like that, because I'd be guided along the way by function parameters.
In reply to David Scotson

Re: XML for making editing forms

by Samuli Karevaara -
"make it much easier to do this kind of thing since all you need to generate (within the form at least) is nice XHTML with appropriate classes."

For the client side stuff, yes, but I was also thinking about the pure server side checks, and how they would be guided from this form format. Maybe it's best to leave all logic out of the format, just concentrate on the presentation and leave the logic to the code itself.