Implement a submit and display student grades scenario

In this article we will explain the implementation of a scenario in which a teacher will fill out a form with student grades, and each student will only need to log in to view his grade on the website, in a menu item. Here's how it's done:

Download the sample form

 

Form fields

The grade submission form's structure needs to contain the following fields:

  • student: a dropdown which will be filled in, using a custom script, with all available user accounts
  • grade: a textbox in which the teacher will type in the student's grade
  • submit: a submit button through which the form will be submitted
 

Custom code

As mentioned above, a part of the functionality is implemented through the use of a custom script:

  • Head to Components > RSForm!Pro > Manage Forms > edit your form > Components > edit the student field and add the following code in its Items area:
    //<code>
    $items = array();   // Prepare the empty array
    $db = Joomla\CMS\Factory::getDbo();    // Prepare the database connection
    // Keep this if you'd like a "Please select" option, otherwise comment or remove it
    $items[] = "|Please Select[c]";
    // Run the SQL query and store it in $results
    $db->setQuery("SELECT name, id FROM #__users");
    $results = $db->loadObjectList();
    // Now, we need to convert the results into a readable RSForm! Pro format.
    // The Items field will accept values in this format:
    // value-to-be-stored|value-to-be-shown
    // Eg. m|M-sized T-shirt
    foreach ($results as $result) {
      $value = $result->id;
      $label = $result->name;
      $items[] = $value.'|'.$label;
    }
    // Multiple values are separated by new lines, so we need to do this now
    $items = implode("\n", $items);
    return $items;
    //</code>
 

Database mappings

RSForm!Pro includes a Database mappings feature which allows us to save submitted data in any database table of our choosing. We will use this feature to replace the user id associated with the submission (which currently is, by default, completed with the submitter's id - the teacher's user id in our case) with the student's user id. We will explain the logic behind this a little later on.

Head to Components > RSForm!Pro > Manage Forms > edit your form > Properties > Mappings and create a database mapping as follows:

  • click on New Query
  • set the Connection type - choose Local if the database table is located on the same server as the website, Remote otherwise
  • set the Method field to Update
  • click on Connect to database
  • Select the _rsform_submissions table in the Database table field
  • add {student:value} in the UserId field
  • Scroll down to the WHERE area and add {global:submissionid} in the SubmissionId field.
 

Frontend menu item

We will use an instance of the View Submissions menu item to allow the student to view their grades.

After creating the menu item, edit it and, in the Options area, apply the following:

  • select your form in the Form dropdown
  • set Enable Viewing Submissions ? to Yes
  • Add the code below in the Row Layout area:
    <tr><td>{details}Your grade is: {/details}</td><td>{grade:value}</td><td>{detailspdf}Download PDF{/detailspdf}</td></tr>
  • Add the code below in the Details Layout area:
    <table width="100%"><tr><td>Grade: </td><td>{grade:value}</td></tr></table>
  • set Show submissions for user ID to login

By setting up the View Submissions menu item in this manner, all the students need to do in order to view their grades is to log in to the website and access it. This is possible because we have replaced the submitter's id (which should have been the teacher's user id) with the student's id, so the component will display the submission associated with the student.

  • Please note that RSForm!Pro exports do not include database mappings. You will need to create the mapping explained above even if you restore the sample form in your installation.
  • The same goes for the View Submissions menu item, you need to create it manually after importing the form.

5 persons found this article helpful.


Was this article helpful?

Yes No
Sorry about that

You Should Also Read

Custom PHP code HOT

Display PHP variables by default when form is shown HOT

PHP Scripts HOT

Custom validation rules HOT

Controlling the calendar HOT