Example #1
0
/**
 * 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 (only property 'displayoptions' is used here)
 * @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']) || !empty($options['showdate'])) {
        if (!array_key_exists('filedetails', $options)) {
            $filedetails = resource_get_file_details($resource, $cm);
        } else {
            $filedetails = $options['filedetails'];
        }
        $size = '';
        $type = '';
        $date = '';
        $langstring = '';
        $infodisplayed = 0;
        if (!empty($options['showsize'])) {
            if (!empty($filedetails['size'])) {
                $size = display_size($filedetails['size']);
                $langstring .= 'size';
                $infodisplayed += 1;
            }
        }
        if (!empty($options['showtype'])) {
            if (!empty($filedetails['type'])) {
                $type = $filedetails['type'];
                $langstring .= 'type';
                $infodisplayed += 1;
            }
        }
        if (!empty($options['showdate']) && (!empty($filedetails['modifieddate']) || !empty($filedetails['uploadeddate']))) {
            if (!empty($filedetails['modifieddate'])) {
                $date = get_string('modifieddate', 'mod_resource', userdate($filedetails['modifieddate'], get_string('strftimedatetimeshort', 'langconfig')));
            } else {
                if (!empty($filedetails['uploadeddate'])) {
                    $date = get_string('uploadeddate', 'mod_resource', userdate($filedetails['uploadeddate'], get_string('strftimedatetimeshort', 'langconfig')));
                }
            }
            $langstring .= 'date';
            $infodisplayed += 1;
        }
        if ($infodisplayed > 1) {
            $details = get_string("resourcedetails_{$langstring}", 'resource', (object) array('size' => $size, 'type' => $type, 'date' => $date));
        } else {
            // Only one of size, type and date is set, so just append.
            $details = $size . $type . $date;
        }
    }
    return $details;
}
Example #2
0
File: lib.php Project: dg711/moodle
/**
 * Given a course_module object, this function returns any
 * "extra" information that may be needed when printing
 * this activity in a course listing.
 *
 * See {@link get_array_of_activities()} in course/lib.php
 *
 * @param stdClass $coursemodule
 * @return cached_cm_info info
 */
function resource_get_coursemodule_info($coursemodule)
{
    global $CFG, $DB;
    require_once "{$CFG->libdir}/filelib.php";
    require_once "{$CFG->dirroot}/mod/resource/locallib.php";
    require_once $CFG->libdir . '/completionlib.php';
    $context = context_module::instance($coursemodule->id);
    if (!($resource = $DB->get_record('resource', array('id' => $coursemodule->instance), 'id, name, display, displayoptions, tobemigrated, revision, intro, introformat'))) {
        return NULL;
    }
    $info = new cached_cm_info();
    $info->name = $resource->name;
    if ($coursemodule->showdescription) {
        // Convert intro to html. Do not filter cached version, filters run at display time.
        $info->content = format_module_intro('resource', $resource, $coursemodule->id, false);
    }
    if ($resource->tobemigrated) {
        $info->icon = 'i/invalid';
        return $info;
    }
    $fs = get_file_storage();
    $files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder DESC, id ASC', false);
    // TODO: this is not very efficient!!
    if (count($files) >= 1) {
        $mainfile = reset($files);
        $info->icon = file_file_icon($mainfile, 24);
        $resource->mainfile = $mainfile->get_filename();
    }
    $display = resource_get_final_display_type($resource);
    if ($display == RESOURCELIB_DISPLAY_POPUP) {
        $fullurl = "{$CFG->wwwroot}/mod/resource/view.php?id={$coursemodule->id}&redirect=1";
        $options = empty($resource->displayoptions) ? array() : unserialize($resource->displayoptions);
        $width = empty($options['popupwidth']) ? 620 : $options['popupwidth'];
        $height = empty($options['popupheight']) ? 450 : $options['popupheight'];
        $wh = "width={$width},height={$height},toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
        $info->onclick = "window.open('{$fullurl}', '', '{$wh}'); return false;";
    } else {
        if ($display == RESOURCELIB_DISPLAY_NEW) {
            $fullurl = "{$CFG->wwwroot}/mod/resource/view.php?id={$coursemodule->id}&redirect=1";
            $info->onclick = "window.open('{$fullurl}'); return false;";
        }
    }
    // If any optional extra details are turned on, store in custom data,
    // add some file details as well to be used later by resource_get_optional_details() without retriving.
    // Do not store filedetails if this is a reference - they will still need to be retrieved every time.
    if (($filedetails = resource_get_file_details($resource, $coursemodule)) && empty($filedetails['isref'])) {
        $displayoptions = @unserialize($resource->displayoptions);
        $displayoptions['filedetails'] = $filedetails;
        $info->customdata = serialize($displayoptions);
    } else {
        $info->customdata = $resource->displayoptions;
    }
    return $info;
}