how to make a cutom developed block content available to all?

how to make a cutom developed block content available to all?

by amal koshy -
Number of replies: 0

Hi,


I just developed a block with downloadable content . but i need to make available the content to all . my code is given below 

block_useful_resources

<?php

class block_useful_resources extends block_base {

    public function init() {

        $this->title = get_string('useful_resources', 'block_useful_resources');

    }

        function specialization() {

    }

function instance_allow_multiple() {

        return false;

    }


        function applicable_formats() {

         return array('all' => true);

    }


        function instance_allow_config() {

        return true;

    }

    public function get_content() {

        global $CFG;        

        require_once($CFG->libdir . '/filelib.php');

if ($this->content !== null) {

return $this->content;

}

                if (empty($this->instance)) {

                return null;

                }

global $CFG,$DB,$USER,$OUTPUT;

                

                

                $this->content = new stdClass();

                $this->content->text ='<div id="content">';

                file_rewrite_pluginfile_urls($this->content->text, 'pluginfile.php', $this->context->id, 'block_useful_resources', 'content', NULL);

$table = 'files';

$select = "component = 'block_useful_resources' AND contextid = '" .$this->context->id ."' AND filename != '.'";

$fields = 'filename';

$sort = 'filename';//die($select);

$images = $DB->get_records_select($table, $select, NULL, $sort, $fields);

                 //die(print_r($images));     

foreach ($images as $image) {

$imagefile = $image->filename;

$url = $CFG->wwwroot . '/pluginfile.php/' .$this->context->id . '/block_useful_resources/content/' . $imagefile;

$this->content->text .= '<a href="'.$url.'">'.$imagefile.'</a>'.'<br>' ;

}

                        $this->content->text.= '</div>';

   

return $this->content;

    } 


}

 

edit_form

<?php



class block_useful_resources_edit_form extends block_edit_form

{

    protected function specific_definition($mform) {

        global $CFG,$USER;

        $context = context_user::instance($USER->id);

        $maxbytes = $CFG->userquota;

        $maxareabytes = $CFG->userquota;

        if (has_capability('moodle/user:ignoreuserquota', $context)) {

        $maxbytes = USER_CAN_IGNORE_FILE_SIZE_LIMITS;

        $maxareabytes = FILE_AREA_MAX_BYTES_UNLIMITED;

    }

        


        $mform->addElement('header', 'configheader', get_string('blocksettings', 'block'));

        $mform->addElement('filemanager', 'config_file', get_string('files', 'block_useful_resources'), null,

array('subdirs' => 1, 'maxbytes' => $maxbytes, 'maxfiles' => -1, 'accepted_types' => '*','areamaxbytes' => $maxareabytes ));

        

    }

    

    

    function set_data($defaults) {

global  $USER;

        if (empty($entry->id)) {

            $entry = new stdClass;

            $entry->id = null;

}

 

$draftitemid = file_get_submitted_draft_itemid('config_file');

file_prepare_draft_area($draftitemid, $this->block->context->id, 'block_useful_resources', 'content', 0,

        array('subdirs'=>true));

$entry->attachments = $draftitemid;

parent::set_data($defaults);    


if ($data = parent::get_data()) {


file_save_draft_area_files($data->config_file, $this->block->context->id, 'block_useful_resources', 'content', 0, 

array('subdirs' => true));

}


   }

 

}



lib

function block_useful_resources_pluginfile($course, $birecord_or_cm, $context, $filearea, $args, $forcedownload, array $options=array()) {

    global $DB, $CFG;

            

    if ($context->contextlevel != CONTEXT_BLOCK) {

        send_file_not_found();

    }


    // If block is in course context, then check if user has capability to access course.

    if ($context->get_course_context(false)) {

        require_course_login($course);

    } else if ($CFG->forcelogin) {

        require_login();

    } else {

        // Get parent context and see if user have proper permission.

        $parentcontext = $context->get_parent_context();

        if ($parentcontext->contextlevel === CONTEXT_COURSECAT) {

            // Check if category is visible and user can view this category.

            $category = $DB->get_record('course_categories', array('id' => $parentcontext->instanceid), '*', MUST_EXIST);

            if (!$category->visible) {

                require_capability('moodle/category:viewhiddencategories', $parentcontext);

            }

        }

        // At this point there is no way to check SYSTEM or USER context, so ignoring it.

    }


    if ($filearea !== 'content') {

        send_file_not_found();

    }


    $fs = get_file_storage();


    $filename = array_pop($args);

    $filepath = $args ? '/'.implode('/', $args).'/' : '/';

    if (!$file = $fs->get_file($this->block->context->contextlevel, 'block_useful_resources', 'content', 0, $filepath, $filename) or $file->is_directory()) {  

        send_file_not_found();

    }

    

    if ($parentcontext = context::instance_by_id($birecord_or_cm->parentcontextid, IGNORE_MISSING)) {

        if ($parentcontext->contextlevel == CONTEXT_USER) {

            // force download on all personal pages including /my/

            //because we do not have reliable way to find out from where this is used

            $forcedownload = true;

        }

    } else {

        // weird, there should be parent context, better force dowload then

        $forcedownload = true;

    }


    session_get_instance()->write_close();

    send_stored_file($file, 60*60, 0, $forcedownload, $options);

}


upload part is working fine. but the content uploaded by admin is not available for students.   pls help me

Average of ratings: -