adding ##user## into database field

adding ##user## into database field

by Allison Soo -
Number of replies: 10
I would appreciate if anyone out there can help me with this.

I am creating a database using database module and would like to insert user name into one of the database field automatically (I don't want students to put in themselves) but I have no clue how to do this. I only managed to show the username in Add Entry form but not able to add that into database.

I had tried many ways myself but obviously to no avail. Thanks in advance for any help.
Average of ratings: -
In reply to Allison Soo

Re: adding ##user## into database field

by Robert Brenstein -
The token ##user## refers to an internal field that is maintained by the database module automatically. In other words, you just need to specify ##user## in the list or single-view template in the place you want to have it displayed. That's all. No need to have it on the Add Entry form.
In reply to Robert Brenstein

Re: adding ##user## into database field

by Allison Soo -
Yes, I can have ##user## display on the page, but it is not in the database as I need this user name to be inside 'student name' field in the database and later to be exported out in excel.
In reply to Allison Soo

Re: adding ##user## into database field

by Robert Brenstein -
This field is in the database, just it is not available in the same way as the fields that you defined. Exporting that info is unfortunately not supported at present. See MDL-16593. It should not be difficult to hack your Moodle to do it for you, though.
In reply to Allison Soo

Re: adding ##user## into database field

by Itamar Tzadok -
An alternative to hacking the code is adding to the list view some commented out lines which when activated (and the others commented out and the database made unavailable to students) output the info in csv format which you can copy/paste to txt file and open in Excel. smile
In reply to Itamar Tzadok

Re: adding ##user## into database field

by Allison Soo -
As I am new to moodle, give me some time to hack to the code level.

'adding to the list view some commented out lines which when activated (and the others commented out', pardon me for not able to get the point. Are you able to illustrate more on this hack ?
In reply to Allison Soo

Re: adding ##user## into database field

by Itamar Tzadok -

Yes and sorry. smile This is not a hack but rather an alternative way to think about the list view of the database. Suppose that your database displays a list of person-hobby pairs such that the person is the one entering the info. So your database would have a hobby field and the name of the person will be displayed by means of the ##user## field. Now, typically the list view template would be set as a table and look like this (simplified):

header

<table border="1" cellpadding="5">
<tbody>
<tr>
<th>Name</th>
<th>Hobby</th>
</tr>

body

<tr>
<td>##user##</td>
<td>||hobby||</td>
</tr>

footer

</tbody>
</table>

(|| stands for square brackets)This table with two entries should display the following:

Name Hobby
John Doe Snowboarding
Jane Doe Hang-gliding


You can add the following commented out lines (bold only for emphasis):

header

<table border="1" cellpadding="5">
<tbody>
<tr>
<th>Name</th>
<th>Hobby</th>
</tr>
<!--
Name,Hobby<br />
-->

body

<tr>
<td>##user##</td>
<td>||hobby||</td>
</tr>
<!--
##user##,||hobby||<br />
-->

footer

</tbody>
</table>

So far there is no change to the display. However, if you switch the comment tags position to comment out the table:

header

<!--
<table border="1" cellpadding="5">
<tbody>
<tr>
<th>Name</th>
<th>Hobby</th>
</tr>
-->
Name,Hobby<br />

body

<!--
<tr>
<td>##user##</td>
<td>||hobby||</td>
</tr>
-->
##user##,||hobby||<br />

footer

<!--
</tbody>
</table>
-->

The display will change to:

Name,Hobby
John Doe,Snowboarding
Jane Doe,Hang-gliding

which you can copy and paste into a text file, change the .txt extension to .csv and open with Excel.

Hope this helps.

smile


In reply to Itamar Tzadok

Re: adding ##user## into database field

by Allison Soo -
Thanks. It worked.

However users of database, the teachers, are not comfortable with the html handling. So I added additional field in database just to store student names as alternative solution at this moment.
In reply to Allison Soo

Re: adding ##user## into database field

by Itamar Tzadok -
As a more user-friendly alternative to the solution I suggested above, you can keep only the table and add a button which calls a javascript function that traverses the table, collects the information, formats it as csv and displays it in a popup window from which the teacher can copy and paste into a text file without doing html. See image below for an illustration of the idea implemented for generating Moodle xml. smile

Attachment data-categoriesgenerator-listview1.png
In reply to Itamar Tzadok

Re: adding ##user## into database field

by Allison Soo -
cool ! Will try this. Thank you very much.