Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Javascript-interface for repeat elements function

From MoodleDocs
Revision as of 21:35, 13 October 2010 by Oleg Sychev (talk | contribs) (Example where it useful set)

GOAL: improve usability by using dynamic form editing.

EXAMPLE: let's imagine you have a multichoice question with 8 or more choices and the order of choices matters. Imagine you found you want add new choice at the start or delete first choice. With current interface you are in a BIG trouble.

At this moment form editing, especially adding new blank answers, units, choices and fields requires page reloading. Editing blanks order or blank removing is not possible without page reloading.

TASK: The main task is development of JavaScript-interface for repeat_elemens function. Other task is make ADD, REMOVE and MOVE actions without page reloading. If JavaScript is not available in user's browser this actions must work with page reloading, like it works now.


Where it will be.

Here, there are some pages, where this function uses, in the list below :

  • Course editing -> Adding a new Choice - Adding fields to form;
  • Course editing -> Adding a new Quiz - Adding new feedback fields;
  • Course editing -> Editing Quiz-> Editing a calculated question - Adding Blanks for choices;
  • Course editing -> Editing Quiz-> Editing a calculated question - Adding Blanks for Units;
  • Course editing -> Editing Quiz-> Editing a Calculated Multichoice question - Adding Blanks for choices;
  • Course editing -> Editing Quiz-> Editing a Simple Calculated question - Adding Blanks for choices;
  • Course editing -> Editing Quiz-> Editing a Simple Calculated question Adding Blanks for Units;
  • Course editing -> Editing Quiz-> Editing a Matching question - Adding Blanks for choices;
  • Course editing -> Editing Quiz-> Editing a Multiple choice question - Adding Blanks for choices;
  • Course editing -> Editing Quiz-> Editing a Numerical question - Adding Blanks for choices;
  • Course editing -> Editing Quiz-> Editing a Numerical question - Adding Blanks for choices;
  • Course editing -> Editing Quiz-> Adding a short answer question - Adding Blanks for choices;

etc. As repeat_elements is core function, it should work anywhere.

This core patch will add new buttons to the form interface. http://imglink.ru/pictures/21-09-10/f85258328f02d65164e6a15644e5d8a9.jpg 7.jpg

Adding new blanks

Adding new blanks realize by pushing "Add blanks for SMTH" button. There will be a hidden empty blank. When user will push the button, it's duplicate will be added in the end of blank's list.

Removing blanks.

It's possible to remove only 'Empty' blanks. Empty blank is that blank, where all key-fields are empty or have default values. List of elements, which will be check:

  • FILE.

File upload input box with browse button. Empty, when filepath is empty.

  • FILEPICKER

General replacement of file element in Moodle 2.0 . Empty, when filepath is empty.

  • HTMLEDITOR

Empty when it doesn't contains eny content.

  • PASSWORD and PASSWORDUNMASK

A password element. Empty, when it doesn't contains any text. TEXT Simple text input element. Empty, when it doesn't contains any text.

  • TEXTAREA

A textarea element. Empty, when it doesn't contains any text.

Other form elements like DATEPICKER, RECAPTCHA, SELECT, HIDDEN and others can contain's any values, becuse they won't be check.

Blank's removing realize by pushind remove button(see screenshot) and confirming your choise. If blank's removing isn't possible user will get allert with reason.

Moving blanks

Blanks moving is available by dragging them with move button. Blank's original order will be preserved , by using hidden elements, named "ord", which will save order number of each blank.

For example: Creation:

 We've got 3 empty blanks. Values of their "ords" are -1,-1,-1.

Editing:

 We edit all blanks. We've got 3 blanks with ords -1,-1,-1.

Saving:

 We save question(or smth else), Ords: -1,-1,-1

Loading:

 Ords: 0,1,2

Removing:

 We remove blank, from the middle of blank-list. We've got 2 blanks with ords 0,2.

Adding:

 We add two blanks. We've got ords: 0,2,-1,-1

Moving:

 We move last blank betwen first and second. Ords: 0,-1,2,-1

So, when we create or add new blanks, initial values of their ords are -1. When we load blanks, which were saved earlier, their ord's values are 0,1,2,3...,n .

Function Interface

Interface of repeat_elements function will receive 7 params instead of 8 in original version. New param $prefix gets prefix for additional form elements, added by the repeat_elements function. In original vewrsion this names were separated and saved in two params: $repeathiddenname and $addfieldsname. This preliminary change, simplifying the interface and helps to solve the basic problem without changing it.