Skip to content

Commit

Permalink
MDL-31121 File resource: add options to display file type and size
Browse files Browse the repository at this point in the history
  • Loading branch information
sammarshallou committed Jan 19, 2012
1 parent f6b4ec2 commit a2c5766
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 17 deletions.
13 changes: 13 additions & 0 deletions mod/resource/lang/en/resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@
$string['printintro'] = 'Display resource description';
$string['printintroexplain'] = 'Display resource description bellow content? Some display types may not display description even if enabled.';
$string['resourcecontent'] = 'Files and subfolders';
$string['resourcedetails_sizetype'] = '{$a->size} {$a->type}';
$string['resource:exportresource'] = 'Export resource';
$string['resource:view'] = 'View resource';
$string['selectmainfile'] = 'Please select the main file by clicking the icon next to file name.';
$string['showsize'] = 'Show size';
$string['showsize_help'] = 'Displays the file size, such as \'3.1 MB\', beside links to the file.
If there are multiple files in this resource, the total size of all files is displayed.';
$string['showsize_desc'] = 'Display file size on course page?';
$string['showtype'] = 'Show type';
$string['showtype_desc'] = 'Display file type (e.g. \'Word document\') on course page?';
$string['showtype_help'] = 'Displays the type of the file, such as \'Word document\', beside links to the file.
If there are multiple files in this resource, the start file type is displayed.
If the file type is not known to the system, it will not display.';
53 changes: 39 additions & 14 deletions mod/resource/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,8 @@ function resource_add_instance($data, $mform) {
require_once("$CFG->libdir/resourcelib.php");
$cmid = $data->coursemodule;
$data->timemodified = time();
$displayoptions = array();
if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
$displayoptions['popupwidth'] = $data->popupwidth;
$displayoptions['popupheight'] = $data->popupheight;
}
if (in_array($data->display, array(RESOURCELIB_DISPLAY_AUTO, RESOURCELIB_DISPLAY_EMBED, RESOURCELIB_DISPLAY_FRAME))) {
$displayoptions['printheading'] = (int)!empty($data->printheading);
$displayoptions['printintro'] = (int)!empty($data->printintro);
}
$data->displayoptions = serialize($displayoptions);

resource_set_display_options($data);

$data->id = $DB->insert_record('resource', $data);

Expand All @@ -122,6 +114,21 @@ function resource_update_instance($data, $mform) {
$data->id = $data->instance;
$data->revision++;

resource_set_display_options($data);

$DB->update_record('resource', $data);
resource_set_mainfile($data);
return true;
}

/**
* Updates display options based on form input.
*
* Shared code used by resource_add_instance and resource_update_instance.
*
* @param object $data Data object
*/
function resource_set_display_options($data) {
$displayoptions = array();
if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
$displayoptions['popupwidth'] = $data->popupwidth;
Expand All @@ -131,11 +138,13 @@ function resource_update_instance($data, $mform) {
$displayoptions['printheading'] = (int)!empty($data->printheading);
$displayoptions['printintro'] = (int)!empty($data->printintro);
}
if (!empty($data->showsize)) {
$displayoptions['showsize'] = 1;
}
if (!empty($data->showtype)) {
$displayoptions['showtype'] = 1;
}
$data->displayoptions = serialize($displayoptions);

$DB->update_record('resource', $data);
resource_set_mainfile($data);
return true;
}

/**
Expand Down Expand Up @@ -302,9 +311,25 @@ function resource_get_coursemodule_info($coursemodule) {
$info->onclick = "window.open('$fullurl'); return false;";
}

// If any optional extra details are turned on, store in custom data
$info->customdata = resource_get_optional_details($resource, $coursemodule);

return $info;
}

/**
* Called when viewing course page. Shows extra details after the link if
* enabled.
*
* @param cm_info $cm Course module information
*/
function resource_cm_info_view(cm_info $cm) {
$details = $cm->get_custom_data();
if ($details) {
$cm->set_after_link(' ' . html_writer::tag('span', $details,
array('class' => 'resourcelinkdetails')));
}
}

/**
* Lists all browsable file areas
Expand Down
72 changes: 69 additions & 3 deletions mod/resource/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,61 @@ function resource_print_heading($resource, $cm, $course, $ignoresettings=false)
}
}

/**
* Gets optional details for a resource, depending on resource settings.
*
* Result may include the file size and type if those settings are chosen,
* or blank if none.
*
* @param object $resource Resource table row
* @param object $cm Course-module table row
* @return string Size and type or empty string if show options are not enabled
*/
function resource_get_optional_details($resource, $cm) {
global $DB;

$details = '';

$options = empty($resource->displayoptions) ? array() : unserialize($resource->displayoptions);
if (!empty($options['showsize']) || !empty($options['showtype'])) {
$context = context_module::instance($cm->id);
$size = '';
$type = '';
if (!empty($options['showsize'])) {
$size = display_size($DB->get_field_sql(
'SELECT SUM(filesize) FROM {files} WHERE contextid=?', array($context->id)));
}
if (!empty($options['showtype'])) {
// For a typical file resource, the sortorder is 1 for the main file
// and 0 for all other files. This sort approach is used just in case
// there are situations where the file has a different sort order
$mimetype = $DB->get_field_sql(
'SELECT mimetype FROM {files} WHERE contextid=? ORDER BY sortorder DESC',
array($context->id), IGNORE_MULTIPLE);
// Only show type if it is not unknown
if ($mimetype && $mimetype !== 'document/unknown') {
$type = get_mimetype_description($mimetype);
// There are some known mimetypes which don't have descriptions
if ($type === get_string('document/unknown','mimetypes')) {
$type = '';
}
}
}

if ($size && $type) {
// Depending on language it may be necessary to show both options in
// different order, so use a lang string
$details = get_string('resourcedetails_sizetype', 'resource',
(object)array('size'=>$size, 'type'=>$type));
} else {
// Either size or type is set, but not both, so just append
$details = $size . $type;
}
}

return $details;
}

/**
* Print resource introduction.
* @param object $resource
Expand All @@ -303,10 +358,21 @@ function resource_print_intro($resource, $cm, $course, $ignoresettings=false) {
global $OUTPUT;

$options = empty($resource->displayoptions) ? array() : unserialize($resource->displayoptions);
if ($ignoresettings or !empty($options['printintro'])) {
if (trim(strip_tags($resource->intro))) {

$extraintro = resource_get_optional_details($resource, $cm);
if ($extraintro) {
// Put a paragaph tag around the details
$extraintro = html_writer::tag('p', $extraintro, array('class' => 'resourcedetails'));
}

if ($ignoresettings || !empty($options['printintro']) || $extraintro) {
$gotintro = trim(strip_tags($resource->intro));
if ($gotintro || $extraintro) {
echo $OUTPUT->box_start('mod_introbox', 'resourceintro');
echo format_module_intro('resource', $resource, $cm->id);
if ($gotintro) {
echo format_module_intro('resource', $resource, $cm->id);
}
echo $extraintro;
echo $OUTPUT->box_end();
}
}
Expand Down
21 changes: 21 additions & 0 deletions mod/resource/mod_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ function definition() {
$mform->addHelpButton('display', 'displayselect', 'resource');
}

$mform->addElement('checkbox', 'showsize', get_string('showsize', 'resource'));
$mform->setDefault('showsize', $config->showsize);
$mform->setAdvanced('showsize', $config->showsize_adv);
$mform->addHelpButton('showsize', 'showsize', 'resource');
$mform->addElement('checkbox', 'showtype', get_string('showtype', 'resource'));
$mform->setDefault('showtype', $config->showtype);
$mform->setAdvanced('showtype', $config->showtype_adv);
$mform->addHelpButton('showtype', 'showtype', 'resource');

if (array_key_exists(RESOURCELIB_DISPLAY_POPUP, $options)) {
$mform->addElement('text', 'popupwidth', get_string('popupwidth', 'resource'), array('size'=>3));
if (count($options) > 1) {
Expand Down Expand Up @@ -175,6 +184,18 @@ function data_preprocessing(&$default_values) {
if (!empty($displayoptions['popupheight'])) {
$default_values['popupheight'] = $displayoptions['popupheight'];
}
if (!empty($displayoptions['showsize'])) {
$default_values['showsize'] = $displayoptions['showsize'];
} else {
// Must set explicitly to 0 here otherwise it will use system
// default which may be 1.
$default_values['showsize'] = 0;
}
if (!empty($displayoptions['showtype'])) {
$default_values['showtype'] = $displayoptions['showtype'];
} else {
$default_values['showtype'] = 0;
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions mod/resource/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@
$settings->add(new admin_setting_configselect_with_advanced('resource/display',
get_string('displayselect', 'resource'), get_string('displayselectexplain', 'resource'),
array('value'=>RESOURCELIB_DISPLAY_AUTO, 'adv'=>false), $displayoptions));
$settings->add(new admin_setting_configcheckbox_with_advanced('resource/showsize',
get_string('showsize', 'resource'), get_string('showsize_desc', 'resource'),
array('value'=>0, 'adv'=>false)));
$settings->add(new admin_setting_configcheckbox_with_advanced('resource/showtype',
get_string('showtype', 'resource'), get_string('showtype_desc', 'resource'),
array('value'=>0, 'adv'=>false)));
$settings->add(new admin_setting_configtext_with_advanced('resource/popupwidth',
get_string('popupwidth', 'resource'), get_string('popupwidthexplain', 'resource'),
array('value'=>620, 'adv'=>true), PARAM_INT, 7));
Expand Down
4 changes: 4 additions & 0 deletions mod/resource/styles.css
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
.path-mod-resource .resourcecontent {text-align: center;}

.path-mod-resource .resourcedetails {font-size: 0.8em; color: #555;}

.resourcelinkdetails {font-size: 0.8em; color: #555;}

0 comments on commit a2c5766

Please sign in to comment.