Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

MDL-31121 File resource: add options to display file type and size

  • Loading branch information...
commit a2c5766674bfe4871408c10be3c4532724e5f89f 1 parent f6b4ec2
@sammarshallou sammarshallou authored
View
13 mod/resource/lang/en/resource.php
@@ -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.';
View
53 mod/resource/lib.php
@@ -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);
@@ -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;
@@ -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;
}
/**
@@ -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
View
72 mod/resource/locallib.php
@@ -292,6 +292,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
* @param object $cm
@@ -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();
}
}
View
21 mod/resource/mod_form.php
@@ -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) {
@@ -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;
+ }
}
}
View
6 mod/resource/settings.php
@@ -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));
View
4 mod/resource/styles.css
@@ -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;}
Please sign in to comment.
Something went wrong with that request. Please try again.