Set default value for a field

Set default value for a field

by Paul Young -
Number of replies: 16

Dear all,

I'd like to set a default value for a field, say school. In the JavaScript template, I inserted the following code (|| stands for square brackets):

function doload () {
if (document.getElementByID('||school#id||').value=="") 
document.getElementByID('||school#id||').value='Sunrise Elementary School';
}
window.Xonload= doload

It did not work. However, if I replace ||school#id|| with the actual field id I looked up in the mdl_data_fields table,  it worked. Can anyone explain why this had happened?

Thanks!

Average of ratings: -
In reply to Paul Young

Re: Set default value for a field

by Itamar Tzadok -

Yes. The ||school#id|| requires interpretation and the interpretation of this particular tag is local to the add template (you don't have such #id tags in the other templates and at any rate these builtin tags are not interpreted in the js template). So, you need to declare this tag in the add template.

Here is an alternative approach which should have the same effect. At the bottom of the add template add the following:

<script type="text/javascript">

document.getElementByID('||school#id||').value='Sunrise Elementary School';

</script>

hth smile

Average of ratings: Useful (2)
In reply to Itamar Tzadok

Re: Set default value for a field

by Paul Young -

Thank you so much for your help! Strangely, it did not work when I inserted your code at the bottom of the Add template. However, it worked when I put my original code at the bottom of the Add template. It seems that window.onload = doload is needed.

It is important to point out that the editor needs to be disabled. Otherwise, it automatically changes whatever javascript code you add into <script type="text/javascript"></script>.

In reply to Itamar Tzadok

Re: Set default value for a field

by William Lu -

Thank you Itamar,

Your instruction works well for me.

As you said, the 'Filed ids' are NOT interpreted in JS template, very sad. 

So, to disable the editor and add the JS into 'Add Template' is the only way to get it the 'Field ids' to work.

(I understood that the 'Field ids' are not needed in the List & Single Templates because we can easily define our own ids with any name we like.

But we can NOT manually add the id='xxxx' inside the <input  value=''> tag. the #id is randomly generated, something like field_123.)


In reply to Paul Young

Re: Set default value for a field

by Todd Almarez -

I have a similar issue I'm trying to resolve (Moodle 1.9).  We are using seperate groups in this course named 001, 002, 003 etc.  I'm trying to find a way that the field "Group" in the Add Entry tab defaults to their respective group #. 

Any help would be much appreciated.

Thanks,

Todd

 

In reply to Todd Almarez

Re: Set default value for a field

by Todd Almarez -

My previous post may be confusing.  In a course, we are using seperate groups with each group named 001, 002, 003, etc.  Maybe some way to have a Field default to their respective group #?

In reply to Todd Almarez

Re: Set default value for a field

by Itamar Tzadok -

Why do you need a field Group in the first place? If what you want is the group affiliation of an entry, it is given by the group affiliation of its author and should not be editable (with or w/o defaults) otherwise its content may be distorted. This bit of info should but is not availbale in the standard module simpliciter. One possible approach for adding it to the entries in display is by javascript. And again one possible approach with javascript is to prepare an array of student id => group id/name pairs, and add a function which iterates the entries in the list view, identify for each entry its author id and add in a designated place the group id (or name if you prefer). The author (user) id should be availabe from e.g. the ##user## tag but you need to parse the tag's content and extract the id from the href of the its 'a' html tag. hth smile

In reply to Itamar Tzadok

Re: Set default value for a field

by Todd Almarez -

I've been trying to trouble shoot this issue with no luck.  The reasoning behind displaying the Group name with each entry is teachers are having a hard time quickly identifying which groups have added entries and which have not.  View List shows all entries at once but you have to click the drop-down box and select a group to see if that particular group has added an entry.  We have lots of entries with lots of groups and this is very tedious to go thru each group and see if there are any entries.

I've tried coming at this from multiple angles but feel like I'm not getting anywhere.  I think the easiest way to get the desired effect is using the ##user## tag.  Currently this tag, when entered in the template, displays First Name and Last Name of the person who created the entry.  Can this tag be modified to display First Name, Last Name, and Group Name?

 

In reply to Todd Almarez

Re: Set default value for a field

by Itamar Tzadok -

Modifying the ##user## to do what you want would require the same work as creating a ##group## tag which is what I would do and will do in the dataform. If you don't want to go into hacking the code perhaps javascript may work for you.You can put in the js template an array of student - group pairs and a function which iterates the list of entries in the list view and adds next to each name the group name. Of course this would have to be added to each database activity separately and so the effectiveness of such a solution depends on the level of complexity of your env. hth smile

In reply to Itamar Tzadok

Re: Set default value for a field

by Todd Almarez -

Thank you for your response Itamar.  I agree with you and look forward to this function in dataform (hopefully for moodle 1.9 big grin).  I do feel more comfortable working in the code rather than in javascript and creating the tag ##group## also provides more flexibility in our environment so I thought I would give it a shot.  Here is what I've done so far.

I've added the following code to templates.php

echo '<option value="##group##">' .get_string('group'). ' - ##group##</option>';

 

 

And the following code to lib.php:

$patterns[]='##group##'; $replacement[] = print($record->groupid);

 

Templates now show a tag "Group - ##group##".  However, in the List Views instead of seeing the group name I'm only seeing the number "1".  Obviously I'm missing something. 

In reply to Todd Almarez

Re: Set default value for a field

by Itamar Tzadok -

Yes, the groupid is just an id. You need to fetch the group name by the  id and assign it to the replacement. You can probaly find how to get the group name in the lib function that prints the groups menu (groups_print_activity_menu). smile

Average of ratings: Useful (1)
In reply to Itamar Tzadok

Re: Set default value for a field

by Todd Almarez -

Thanks Itamar for your advice.  Per your suggestion, I reviewed the grouplib file and am focusing on this new line: 

$replacement[] = groups_get_group_name($record->groupid);

But it still doesn't work.  I know I am on the right track because when I manually enter a groupid #, e.g. 87, for "$record->groupid", the output is the correct group name.  Is there a better way to fetch the groupid associated with each record?

In reply to Todd Almarez

Re: Set default value for a field

by Itamar Tzadok -

I'm looking into it, although in the dataform in which it is done in a different way (the group info is retrieved as part of the entry info. At any rate, whatever the reason it doesn't display the name is, calling the 'groups_get_group_name' inside the loop may be inefficient. 'groups_get_group_name' makes a query to the database and since you call it for each entry this may result in a lot of queries. You should probably fetch once before the loop a recordset of the group id-name pairs and assign the replacements from that array. smile

In reply to Todd Almarez

Displaying entry group info (Re: Set default value for a field)

by Itamar Tzadok -

So I've done a quick implementation of the group info in the dataform. Here's how it works.

There are two groups in the current course, Pandas and Lions (with repsective group picture). In the current activity, one entry is assigned to Pandas and another to Lions. The entries view displays the group picture and group name (these are two separate entities though and you can display any one of them and/or also the group description and group id).

In the first illustration the group menu is set to 'all participant' and the entries view shows the entries from all groups:

 

If you edit an entry and you have the required capability (manageentries) you can assign the entry to one of the available groups. The illustration shows the editing of the entry that is assigned to Lions:

 

Then, if you add a new entry, it will be assigned to the current group and if you have the right capablities (as above) you can change that assignmet:

smile

In reply to Itamar Tzadok

Re: Displaying entry group info (Re: Set default value for a field)

by Todd Almarez -

Excellent!  When do you anticipate release in dataform?  For Moodle 1.9 and/or Moodle 2.0?

 

In reply to Todd Almarez

Re: Displaying entry group info (Re: Set default value for a field)

by Itamar Tzadok -

The dataform is 2.1 and above. It is currently in a beta stage. A version with this feature should be released via git some time this week. smile

In reply to Paul Young

Re: Set default value for a field

by William Lu -

Hi Paul,

I believe that you have seen Itamar's solution in this thread.

He suggested that you add the JS into the 'Add template' rather than the 'JS template', because the field#ids are not interpreted there.

You have to 'Disable editor' to do so.

If you really want to keep your JS in JS template, you have to find out the field#ids by:

  1. Add field#id into 'Add template'. 
  2. Click on 'Add entry' tab and you will see the number, something like field_123
  3. Use this number into your 'JS template' and remove the field#id from the 'Add template'

Another way to find the randomly generated #id

  1. Click on 'Add entry' tab, find the field, then 'Right-click' to 'Inspect element' (Chrome)
  2. You will find something like this: <input class="basefieldinput" type="text" name="field_123" id="field_123" value="">


Neither of above methods are as good as Itamar's way because:

  1. If you copy this Database into another course, you have to find out the #id again
  2. You can NOT export it as a preset, same reason, the #ids wouldn't be same.