This Drupal 7 CodeLet creates a link to the SCORM or HTML 5 course from the uploaded zip file. This associates an SCORM or HTML course to a node in Drupal.
CodeLet
<?php/** * @file * Add a link to node page after a SCORM or HTML5 course has been uploaded * * @author DrupalD <https://twitter.com/DrupalD>*/function course_link_menu() { $items = array(); $items['admin/config/content/course-link'] = array( 'type' => MENU_NORMAL_ITEM, 'title' => t('Course Link'), 'description' => t('Configure file upload fields for Course Link'), 'page callback' => 'drupal_get_form', 'page arguments' => array('course_link_admin_form'), 'access arguments' => array('administer site configuration '), 'file' => 'course_link_admin.inc', 'file path' => drupal_get_path('module', 'course_link'), ); return $items;}/*** Implementation of hook_node_insert** @param object $node* @author DrupalD <https://twitter.com/DrupalD>*/function course_link_node_insert($node) { $file_upload_fields = array(); $file_field = array(); $file_upload_fields = variable_get('course_link_field'); //Default public directory path to extract zip files to $destination = _file_directory_path(); if (!empty($file_upload_fields)) { foreach($file_upload_fields as $key => $value) { $file_field_info = field_info_field_by_id($value); $file_field = array($file_field_info['field_name'] => $file_field_info['field_name']); } } if (!empty($file_field)) { foreach ($file_field as $key => $value) { //Get the correct field that is associated with existing node and exit the loop if (array_key_exists($key, (array) $node)) { $node_file_field = $key; break; } } // Check if the node has one of those file fields. If it does, get the URI of the uploaded file. $file_uri = _file_get_location($node->$node_file_field['und'][0]['fid']); if (!empty($file_uri)) { // Insert data in course_link_node_file to retrieve it on node page $data = array( 'nid' => $node->nid, 'fid' => $node->$node_file_field['und'][0]['fid'], ); drupal_write_record('course_link_node_file', $data); //Unzip the file $zip = new ZipArchive(); if ($zip->open($destination . basename($file_uri['uri'], '.zip') . '.zip') === TRUE) { $zip->extractTo($destination . basename($file_uri['uri'], '.zip')); $zip->close(); } else { drupal_set_message(t('Unable to open zip file'), 'error'); } } }}/*** Implemenation of hook_node_view** @param object $node* @param string $view_mode* @param string $langcode* @author DrupalD <https://twitter.com/DrupalD>*/function course_link_node_view($node, $view_mode, $langcode) { $file_fid = array(); $file_filename = array(); //Get the fid from course_link_node_file table $file_fid = db_select('course_link_node_file', 'cl') ->fields('cl', array('fid')) ->condition('nid', $node->nid, '=') ->execute() ->fetchAssoc(); //Get the filename using the FID $file_filename = _file_get_location($file_fid['fid']); $course_directory = basename($file_filename['uri'], '.zip'); $file_directory = _file_directory_path(); $course_link_index_directory = scandir($file_directory . $course_directory); $course_link_index_file = array_search('index.html', $course_link_index_directory) ? '/index.html' : '/res/index.html'; //Add the link to node page $course_link = l(t('Course Link'), $file_directory . $course_directory . $course_link_index_file, array('attributes' => array('target' => '_blank'))); $node->content['course_link'] = array( '#markup' => $course_link, );}/*** _file_get_location* Get the location of the uploaded zip file** @param int $fid: Id of the file* @param bool $uri: Whether to fetch uri or not* @author DrupalD <https://twitter.com/DrupalD>*/function _file_get_location($fid) { //Get the URI of the uploaded file using the FID $result = db_select('file_managed', 'fm') ->fields('fm', array('uri')) ->condition('fid', $fid, '=') ->execute() ->fetchAssoc(); return $result;}/*** _file_directory_path** Path to files directory** @author DrupalD <https://twitter.com/DrupalD>*/function _file_directory_path() { return variable_get('file_public_path', conf_path() . '/files/');}?>
course_link_admin.inc
<?php/*** @file* * Admin configuration form for course link* * @author DrupalD <https://twitter.com/DrupalD>**//*** course_link_admin_form** Admin configuration for course link** @author DrupalD <https://twitter.com/DrupalD>*/function course_link_admin_form() { $form= array(); $course_link_field = array(); $course_link_field =variable_get('course_link_field'); $course_field_values = _course_link_get_upload_field(); $form['course_link_field'] = array( '#type' => 'select', '#title' => t('File upload fields'), '#description' => t('Select one or more file upload field to be processed by Course Link'), '#multiple' => TRUE, '#size' => 5, '#options' => $course_field_values, '#default_value' => $course_link_field, ); return system_settings_form($form);}/*** _course_link_get_upload_field** Helper function to get a list of all the file field which allows zip upload** @author DrupalD <https://twitter.com/DrupalD>*/function _course_link_get_upload_field() { $file_upload_fields = array(); $file_field = array(); $file_upload = array(); $file_upload_fields = field_info_fields(); foreach ($file_upload_fields as $file_field) { if ($file_field['type'] == 'file') { $file_upload['Course fields'] = array($file_field['id'] => $file_field['field_name']); } } return $file_upload;}?>
Info file details
name = Course Link
description = Add a course link to node after a SCORM or HTML5 course has been uploaded. Developed by <a href="http://jos.cx/jcs" target='_blank'>JCS</a>
dependencies[] = file
configure = admin/config/content/course-link
core = 7.x
package = Course
Install file details
<?php>/** * * @file * * Schema for storing node and file association details * * @author DrupalD <https://twitter.com/DrupalD> **//*** Implementation of hook_schema** @author DrupalD <https://twitter.com/DrupalD>*/function course_link_schema() { $schema = array(); $schema['course_link_node_file'] = array( 'description' => t('Store node and uploaded zip file association details'), 'fields' => array( 'id' => array( 'type' => 'serial', 'length' => 11, 'not null' => TRUE, ), 'nid' => array( 'type' => 'int', 'length' => 11, 'not null' => TRUE, ), 'fid' => array( 'type' => 'int', 'length' => 11, 'not null' => TRUE, ), ), 'primary key' => array('id'), ); return $schema;}?>