General developer forum

Get download link of a user assignment

 
 
Picture of Kiran S
Get download link of a user assignment
 

Hi,


How can I get the download link of a user assignment that was submitted in a course? I have been analyzing locallib.php and lib.php in assign module, but in vain.


Any help would be appreciated!


Thanks,

CC

 
Average of ratings: -
Picture of Kiran S
Re: Get download link of a user assignment
 
Finally I came across the function I am looking for:

/mod/assign/renderable.php
public function preprocess($dir, $filearea, $component) {
        global $CFG;
        foreach ($dir['subdirs'] as $subdir) {
            $this->preprocess($subdir, $filearea, $component);
        }
        foreach ($dir['files'] as $file) {
            $file->portfoliobutton = '';
            if (!empty($CFG->enableportfolios)) {
                $button = new portfolio_add_button();
                if (has_capability('mod/assign:exportownsubmission', $this->context)) {
                    $portfolioparams = array('cmid' => $this->cm->id, 'fileid' => $file->get_id());
                    $button->set_callback_options('assign_portfolio_caller',
                                                  $portfolioparams,
                                                  'mod_assign');
                    $button->set_format_by_file($file);
                    $file->portfoliobutton = $button->to_html(PORTFOLIO_ADD_ICON_LINK);
                }
            }
            $path = '/' .
                    $this->context->id .
                    '/' .
                    $component .
                    '/' .
                    $filearea .
                    '/' .
                    $file->get_itemid() .
                    $file->get_filepath() .
                    $file->get_filename();
            $url = file_encode_url("$CFG->wwwroot/pluginfile.php", $path, true);
            $filename = $file->get_filename();
            $file->fileurl = html_writer::link($url, $filename);
        }
    }

Not sure what arguments do I need to pass into this function. sad
preprocess($dir, $filearea, $component)

Could someone help me out?

Thanks,
CC
 
Average of ratings: -
Picture of triveni mergu
Re: Get download link of a user assignment
 

Hello cc.

please find the following code it will help you.

 

require_once('../../config.php');
require_once($CFG->dirroot . '/mod/assign/locallib.php');


$id = 16; //assume assignment id = 16
$cm = get_coursemodule_from_id('assign', $id, 0, false, MUST_EXIST);
$context = context_module::instance($cm->id);

$fileinfo = $DB->get_record_sql("select * from {files} where filename != '.' and contextid=".$context->id);

$path = '/' .$context->id .'/' .$fileinfo->component.'/' .$fileinfo->filearea .'/' .$fileinfo->itemid.$fileinfo->filepath.$fileinfo->filename;
$url = file_encode_url("$CFG->wwwroot/pluginfile.php", $path, true);
echo 'url: '.$url;

 
Average of ratings: -
Picture of Kiran S
Re: Get download link of a user assignment
 
Hi Triveni,

I tried pasting your code in /mod/assign/view.php

$id = 16; //assume assignment id = 16
$cm = get_coursemodule_from_id('assign', $id, 0, false, MUST_EXIST);

When I tried to print_r($cm) this... I get an error.

Error reading from database
Debug info: Table 'moodle26dev.mdl_mdl_assign' doesn't exist
SELECT cm.*, m.name, md.name AS modname 
FROM mdl_course_modules cm
JOIN mdl_modules md ON md.id = cm.module
JOIN mdl_mdl_assign m ON m.id = cm.instance

WHERE cm.id = ? AND md.name = ?

[array (
0 => 7,
1 => 'mdl_assign',
)]
Error code: dmlreadexception
Stack trace:
  • line 443 of /lib/dml/moodle_database.php: dml_read_exception thrown
  • line 996 of /lib/dml/mysqli_native_moodle_database.php: call to moodle_database->query_end()
  • line 1428 of /lib/dml/moodle_database.php: call to mysqli_native_moodle_database->get_records_sql()
  • line 1344 of /lib/datalib.php: call to moodle_database->get_record_sql()
  • line 57 of /mod/assign/view.php: call to get_coursemodule_from_id()
Coding error detected, it must be fixed by a programmer: Invalid state passed to moodle_page::set_state. We are in state 3 and state 3 was requested.
Debug info: 
Error code: codingerror
Stack trace:
  • line 850 of /lib/pagelib.php: coding_exception thrown
  • line 83 of /theme/essential/renderers/core_renderer.php: call to moodle_page->set_state()
  • line 2600 of /lib/outputrenderers.php: call to theme_essential_core_renderer->footer()
  • line 362 of /lib/setuplib.php: call to core_renderer->fatal_error()
  • line ? of unknownfile: call to default_exception_handler()
Thanks,
CC
 
Average of ratings: -
Picture of triveni mergu
Re: Get download link of a user assignment
 

Hi cc,

can you please attach your /mod/assign/view.php here.

i just want to look at the file which you are using.

and which moodle version you are using

 
Average of ratings: -
Picture of triveni mergu
Re: Get download link of a user assignment
 

Hi cc,

i have gone through with your code.

please replace $cm = get_coursemodule_from_id('mdl_assign', $id, 0, false);

with

$cm = get_coursemodule_from_id('assign', $id, 0, false);

 
Average of ratings: -