Problem/Motivation

This is a nice module used within the Opigno LMS distribution. We are customizing the distribution and one of the requirements is to have list of certificates on the user's dashboard. It would have been very nice to have a way to prepare this with views. The problem is that such integration is not present for the certificate_* tables.

This way any list of user certificates, class certificates, etc will require quite a lot of custom code.

Proposed resolution

Integrate the module's custom tables data to views through hook_views_data and hook_views_data_alter (needed to add the reverse relationships from nodes and users).

It's pretty stand-alone task - pure feature addition through some hooks.

Remaining tasks

Discussion, patch, etc.

User interface changes

None - extending views UI.

API changes

New feature - API addition.

Data model changes

None. We will just expose the existing data model to be read from views.
Maybe add missing indexes to speed-up db queries.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ndobromirov created an issue. See original summary.

djdevin’s picture

Title: Views support » Certificate snapshots as entities
Version: 7.x-2.x-dev » 7.x-3.x-dev

Hi,

There's actually a 3.x version in the works (new) which turns the Certificate templates into Entities (they were nodes before) which is a start towards this functionality.

I think this would only work when "certificate snapshots" were turned on, because then it stores the record of which certificate(s) you received. If you exposed certificate snapshots as an entity too, then you could just relate it with an Entity API relationship and not have to write any custom views code.

For our purposes we use Certificate with the Course module (http://drupal.org/project/course) which provides a transcript of all courses, so that serves as the certificates list, but if you're using something other than Course it makes sense to want to display earned certificates somewhere.

ndobromirov’s picture

Hi,

I know that you've moved the issue against version 3, but As Opigno LMS are using version 2 and updates of their dependencies are very, VERY risky... I went with my proposal.

I was then able to make a view that starts from the "User certificates" group in views UI and join with the user and course and providing a download link. Note the configuration that generates only 1 certificate for user/course pair, as otherwise downloads started duplicating the listing entries...

If you need the reverse relationships, you should implement the MY_MODULE_views_data_alter(&$data), and provide them. Examples can be found in views module integration with node.

I have not provided a patch, as this is not the way this should be extended as already stated in #2.

Here is the code that fixed it for me:

/**
 * Imlplements hook_views_data().
 *
 * @return array
 *   Meta-data definitions.
 */
function MY_MODULE_views_data() {
  $data = array();

  $data['certificate_snapshots']['table'] = array(
    'group' => t('Certificates'),
    'base' => array(
      'field' => 'csid',
      'title' => t('User certificates'),
      'help' => t('Represents the concrete certificate instances assigned to users.'),
    ),
    'default_relationship' => array(
      'owner' => array(
        'table' => 'users',
        'field' => 'uid',
      ),
      'certificate' => array(
        'table' => 'node',
        'field' => 'nid',
      ),
    ),
  );

  $data['certificate_snapshots']['uid'] = array(
    'title' => t('Certificate owner UID'),
    'help' => t('References a user instance.'),

    'field' => array(
      'handler' => 'views_handler_field_user',
      'click sortable' => TRUE,
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_user_uid',
      'name field' => 'name',
    ),
    'filter' => array(
      'title' => t('Certificate owner UID'),
      'handler' => 'views_handler_filter_user_name',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'relationship' => array(
      'base' => 'users',
      'base field' => 'uid',
      'field' => 'uid',
      'handler' => 'views_handler_relationship',
      'label' => t('Owner'),
      'group' => t('Certificates'),
      'title' => t('Certificate owner relationship'),
      'help' => t('Certificate owner relationship HELP'),
    ),
  );

  $data['certificate_snapshots']['nid'] = array(
    'title' => t('Course NID'),
    'help' => t('Refers a course node instance.'),

    'field' => array(
      'handler' => 'views_handler_field_node',
      'click sortable' => TRUE,
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_node_nid',
      'name field' => 'title',
    ),
    'relationship' => array(
      'base' => 'node',
      'base field' => 'nid',
      'field' => 'nid',
      'handler' => 'views_handler_relationship',
      'label' => t('Course'),
      'group' => t('Certificates'),
      'title' => t('Certificate course relationship'),
      'help' => t('Certificate course relationship HELP'),
    ),
  );

  return $data;
}
PetarB’s picture

I'm trying to get this to work.

Created a new module, gone to views, saw 'User Certificates' and made a new view:

https://i.imgur.com/stu7Ms2.png

Under 'Relationships' I have 'Certificate course relationship' and 'Certificate owner relationship' chosen.

Not sure what to do next...

PetarB’s picture

Actually, I think I have solved this on a closer reading of the entire thread. I will write up a 'how to' shortly.

dquez9’s picture

Hi,
I attempting to create the module above in order to use the solution on my Opigno LMS site.
Is there any other info concerning or tips for assistance in recreation. ?

Were you all successful with the function above?

dquez9’s picture

djdevin’s picture

Status: Active » Needs review
FileSize
20.9 KB
djdevin’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.