Passing logon info to a form

Passing logon info to a form

by Ronald Zellner -
Number of replies: 4
I have a weekly survey that students complete that uses PHP & SQL in a separate web page. How can I have this form open with the student's name and ID (Moodle password) already entered into the form?
Thanks in advance.
Average of ratings: -
In reply to Ronald Zellner

Re: Passing logon info to a form

by Paul Holden -
Picture of Core developers Picture of Moodle HQ Picture of Moodle Workplace team Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers
To get the name of the user, you'd first need to know their ID (this is a field in the mdl_user table). You could then do something like the following, where $uid contains a student ID;

$user = get_record('user', 'id', $uid);
$name = fullname($user);

The variable $name now contains the full name of the user.

I don't think you can pull their password from the database in a similar manner as it is stored as an MD5 hash.
In reply to Paul Holden

Re: Passing logon info to a form

by Ronald Zellner -
OK, but how do I get the ID of the current logged-on student so I can assign it to $uid?
In reply to Ronald Zellner

Re: Passing logon info to a form

by Ronald Zellner -
In case anyone else needs to do this, this is the final solution we found:

We put this code into a survey file (index.php):

<?php
require_once("../../moodle_projects/config.php") ;
require_login();

$uid = $USER->id;
$fname = $USER->firstname;
$lname = $USER->lastname;
$teachercode = $USER->idnumber;

?>

We used the optional field "idnumber" in the user profile to contain a unique identifier for each student. Any of the optional fields can be used this way.

Then we put this code for each variable inside the form to display the results in the survey in a read-only text field:

<input name="FirstName" type="text" id="FirstName" onfocus="this.blur()" value="<? print $fname; ?>">
<input name="LastName" type="text" id="LastName" onfocus="this.blur()" value="<? print $lname; ?>">
<input name="TeacherCode" type="text" id="TeacherCode" onfocus="this.blur()" value="<? print $teachercode; ?>">

These values then get submitted with the survey data to accurately identify the student.