This CodeLet allows you to upload SCORM & HTML5 packages. It extracts them, save under files directory and create a link on Node view page.
<?php
/**
* @file
* Add a link to node page after a SCORM or HTML5 course has been uploaded
*
* @author DrupalD <https://twitter.com/DrupalD>
*/
/**
* Implementation of hook_menu
*
* @return array $items
* @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 .'/courses/'. 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) {
$course_link = _get_course_link($node->nid);
$node->content['course_link'] = array(
'#markup' => $course_link,
);
}
/**
* _get_course_link
* Get the link to the course to display on node page
*
* @param int $nid
* @return string
* @author DrupalD <https://twitter.com/DrupalD>
*/
function _get_course_link($nid) {
$file_fid = array();
$file_filename = array();
//Get the fid from course_link_node_file table
$record = db_select('course_link_node_file', 'cl')
->fields('cl', array('fid'))
->condition('nid', $nid, '=')
->execute();
while ($cl_fid = $record->fetchAssoc()) {
$am_fid[] = $cl_fid;
}
if (count($am_fid) > 1) {
$file_0_fid = array_slice($am_fid, -1,1) ;
$file_fid = $file_0_fid[0];
}
else {
$file_fid = $am_fid[0];
}
//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 .'/courses/'. $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
$node = node_load($nid);
$course_link = l($node->title, $file_directory .'/courses/'. $course_directory .
$course_link_index_file, array('attributes' => array('target' => '_blank')));
return $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');
}
name = Course Link
description = Add a course link to node after a SCORM or HTML5 course has been uploaded. Developed by <a href="http://twitter.com/DrupalD" target='_blank'>DrupalD</a>
dependencies[] = file
dependencies[] = views_php
configure = admin/config/content/course-link
core = 7.x
package = Course
<?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;
}
File | Size | Author |
---|---|---|
1.45 KB | DrupalD |