3

I want to create a drupal site to record student's grades. Since every student will have a class, I want to make an assignment page that contains all the students' names(belonging to a class) so that I can record their individual grades. I do not want to edit individual nodes form by form. However, since each student's grade is considered a node, I can only list the nodes in a view, which is not editable(unless I use the alpha release of Editable module). This can be done easily in Filemaker but I do not know how to do it in Drupal. I wish to know what is the idea to achieve the goal? I think doing in Views is not ideal. What will you do to achieve this goal?

2

If you are comfortable with PHP, you could create a form that has all the students in a class and displays the student's name and then their grade.

Rather quickly, you could create a custom module and include several drupal hooks in it. It would start out with something like this:

function YOURMODULE_menu() {
  $items=array();

  $items['class/grades'] = array(
    'title' => 'Class Grades',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('YOURMODULE_class_grades_form'),
    'access callback' => 'user_is_logged_in',
  );

  // any other menu items here

  return $items;
}

This creates a menu item so that when someone goes to

www.yoursite.com/class/grades

they will be shown a form. NOTE: With access callback set to user_is_logged_in, that means ANY logged in user on your site can do this but I am just trying to show you the broad strokes here.

We need to get the form, but before we go there, for another broad stroke, you will need to create a view that returns a list of student nodes. In the fields, just return the nid of the node. If you hit preview, we want to see something like 13, 27, 55, 99, or whatever the nids are. In that node, I am also going to assume you have a text field called grade that stores the grade for that student in the class. For this, I will also assume this view is called just_the_nids.

Now, let's define that form:

function YOURMODULE_class_grades_form() {
  $form=array();

  $result=views_get_view_result('just_the_nids');

  foreach($result as $r) {
    $node=node_load($r->nid);
    $form[$r->nid] = array(
      '#title' => 'Grade for ' . check_plain($node->title),
      '#default_value' => !empty($node->field_grade[0]['value']) ? $node->field_grade[0]['value'] : '',
      '#type' => 'radios',
      '#options' => array(
        'A+' => 'A+',
        'A' => 'A',
        'A-' => 'A-',
        'B+' => 'B+',
        'B' => 'B',
        'B-' => 'B-',
        'C+' => 'C+',
        'C' => 'C',
        'C-' => 'C-',
        'D+' => 'D+',
        'D' => 'D',
        'D-' => 'D-',
        'F' => 'F',
      ),
    );
  }

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Grade'),
  );

  return $form;

}

What this does is grab the view you created and loops through it, loading each node and grabbing the title, which I hope is the student's name and then displays the grade of that student with radio buttons. I know we could have returned the title and grade in the view and used it here, but just trying to "show the math" which might help later on :)

At this point, if you saved everything and flushed your menu cache, you could go to the page and see some like this:

Jill Doe
 ( ) A+
 ( ) A
 ( ) A-
  .
  .
  .

Doug Drupal
 ( ) A+
  .
  .

[GRADE]

However, pressing the [GRADE] button at the bottom of the page wouldn't do anything because we haven't declared what it should do other than just submit the form. So we do that like this:

function YOURMODULE_class_grades_form_submit($form, &$form_state) {
  foreach ($form_state['values'] as $nid => $grade) {
    if (is_numeric($nid) && !empty($grade)) {
      $node=node_load($nid);
      $node->field_grade[0]['value']=$grade;
      node_save($node);
      drupal_set_message(check_plain($node->title) . '\'s grade set to ' . $grade);
    }
  }
}

What this does is go though the form and load each node again and set the grade field to what you clicked on in the form and then save the node back again with just that field changed (along with a perky drupal message telling you what it did :)

Now, what this doesn't do that it should for primetime:

Menu per class (unless you only have one)

Security (every logged in user can do this)

Internationalization (I didn't use t() where appropriate)

CSS Style (it isn't fancy, eg, the radios all go down vertically instead of horizontally, etc)

I assumed A-F grading, and also saved the text, You could get fancy here and store 4.0 for an A+, 3.7 for an A, etc, but right now it saves the actual text of the grade but anyway I hope it might give you some ideas on how to do it.

I also assumed D6, D7 has slightly different field structures.

A lot of information on the ins and outs of all this can be found in the Drupal Form API Quickstart Guide

  • The skill required for do that is too technical. My Drupal is still at using modules only. But I will try to understand your explanation. Thanks. – Standstill Jul 14 '12 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.