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.
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.
$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.
OK, but how do I get the ID of the current logged-on student so I can assign it to $uid?
Try using the global object $USER. See http://docs.moodle.org/en/Developer_FAQ#How_do_I_find_out_the_currently-logged-on_user.3F.
Hope this helps!
Ken
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.
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.