How to use file_rewrite_pluginfile_urls function

How to use file_rewrite_pluginfile_urls function

by Bezk Intos -
Number of replies: 16

Hi.

Would somebody be so kind to explain in more detail or give an example of how to use the file_rewrite_pluginfile_urls function. I have some basic/modest SQL knowledge so I'm able to make some SQL statements directly on moodle database tables in order to customize my moodle based web page. But I'm struggling with this @@PLUGINFILE@@ instead of URL to file (e.g. image)

So, I found this explanation:

file_rewrite_pluginfile_urls($text, $file, $contextid, $component, $filearea, $itemid, array $options=null)

  • param: string $text The content that may contain ULRs in need of rewriting.
  • param: string $file The script that should be used to serve these files. pluginfile.php, draftfile.php, etc.
  • param: int $contextid This parameter and the next two identify the file area to use.
  • param: string $component
  • param: string $filearea helps identify the file area.
  • param: int $itemid helps identify the file area.
  • param: array $options text and file options ('forcehttps'=>false)
  • return: string the processed text.

So, If I have this:

<p><img style="float: left; margin-left: 5px; margin-right: 5px;" src="@ @PLUGINFILE@@/image.jpg" alt="image" width="100" height="85" />Image</p>

inside this:

table: mdl_data_content

attribute: content

id(row): 5732


According to above, is it possible to derive those parameters for file_rewrite_pluginfile_urls function?


I thought maybe it's something like this:

$text = "@ @PLUGINFILE@@/image.jpg";

$component = "mod_database";     >>> according to some example I found with "mod_lesson"

$filearea = "content";

$itemid = "5732";


But probably it's not, or even if it is what about other parameters?

Which files do I have to include (require_once) to my php for this to work?

Average of ratings: -
In reply to Bezk Intos

Re: How to use file_rewrite_pluginfile_urls function

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Usually the best way to find out how to use the file_rewrite_pluginfile_urls is to look at how it is used already in the relevant activity. 
You mention 'mod_database', so I'm going to assume you are using the database activity.
A quick search (in Moodle 2.7), found the following on line 214 of mod/data/field/textarea/field.class.php:
$str = file_rewrite_pluginfile_urls($content->content, 'pluginfile.php', $this->context->id, 'mod_data', 'content', $content->id, $this->get_options());

The first parameter is the entire content of the text (not just the @@ PLUGINFILE@@ part).
The second parameter is almost always 'pluginfile.php' (and you shouldn't need to worry about the cases where it isn't this value).
The third parameter is the contextid - for an activity, this can be determined by context_module::instance($cmid). If you don't have the $cmid, this can be found by $cm = get_coursemodule_from_instance('data', $dataid) (where $dataid is the ID of the relevant record in mdl_data).
The fourth parameter will always be 'mod_data', for the database activity.
The fifth parameter is 'content' in this case (as that is the name that the database module has chosen to use for this particular file area).
The sixth parameter is an itemid chosen to best distinguish between the different file areas, in this case it is the ID of the relevant record in mdl_data_content (by comparison, in a forum, it is the ID of the mdl_forum_post that the file is attached to, in other cases it may just be 0, if there is only one file area for that activity).
The final parameter is optional. For consistency with the database module, you should probably set it to: array('trusttext' => false, 'forcehttps' => false, 'subdirs' => false, 'maxfiles' => -1, 'context' => $context, 'maxbytes' => 0, 'changeformat' => 0, 'noclean' => false)

 
Average of ratings: Useful (5)
In reply to Davo Smith

Re: How to use file_rewrite_pluginfile_urls function

by Bezk Intos -

Thank you very very much. I'm getting right on it!

In reply to Bezk Intos

Re: How to use file_rewrite_pluginfile_urls function

by Bezk Intos -

I finally did it! Thank you Davo Smith once again!

I will put my code here explained in newbie way, so that other newbies like myself can understand it:


<?php

require_once('config.php');   // needed for file_rewrite_pluginfile_urls  & //needed for get_coursemodule_from_instance
require_once($CFG->dirroot.'/repository/lib.php');  // needed for za file_rewrite_pluginfile_urls

//////////////////////////////////////////////////////////////////////////////////////////////

//parameters ($text, $dataid, $itemid should be obtained dynamically by sql - this is just an example)
$text = "<p><img src='@ @PLUGINFILE@@/image.jpg' alt='' width='100' height='85' /></p>";  //there shouldn't be space between @@ - I put one space to avoid triggering the file_rewrite_pluginfile_urls function here in this forum
$file = "pluginfile.php";
$component = "mod_data";   //if activity: database
$dataid = 30;  // ID from table mdl_data (for activity: database)
$filearea = "content";
$itemid = 5747;    // ID from table mdl_data_content (for activity: database) - row in the table where the above $text is stored

//contextid parameter
$cm = get_coursemodule_from_instance('data', $dataid);
$cmid = $cm->id;
$context = context_module::instance($cmid);
$contextid = $context->id;

$str = file_rewrite_pluginfile_urls($text, $file, $contextid, $component, $filearea, $itemid);

echo $str;

?>


In reply to Bezk Intos

Re: How to use file_rewrite_pluginfile_urls function

by vijaykanth dondapati -

hii 

thank u for ur explanation with real time

i am new to moodle development.

i am developing a plugin out of moodle  means (moodle/myplugin_name) 

when i am retriving image path from database same @@plugin@@/image.jpg

can u plzz share me the complete code to display the images 

thanking you...

In reply to vijaykanth dondapati

Re: How to use file_rewrite_pluginfile_urls function

by vijaykanth dondapati -

how to get context id for 

file_rewrite_pluginfile_urls ();

in arguments

In reply to vijaykanth dondapati

Re: How to use file_rewrite_pluginfile_urls function

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
You get the contextid from the relevant context object.

Context objects can be created by calling (in this case) context_module::instance($cmid), where $cmid is the course module id for the activity.

The course module id is the ?id=XX part of the view.php URL.

Alternatively, if you just look at the existing code that calls file_rewrite_pluginfile_urls, you should be able to see where the contextid comes from.

In reply to Davo Smith

Re: How to use file_rewrite_pluginfile_urls function

by suri babu -
Hi...

 I am Facing the problem image url  <img src="http://moodle.consultfora.com/moodle/pluginfile.php/4/question/questiontext/1/1-sym-cparen-h1.gif">

source code:

<?php

$question_text=mysql_query("SELECT * 

FROM  `question`  where category=23");

while($q_text=mysql_fetch_array($question_text))

{

$questions=$q_text['questiontext'];

//echo $questions;

$question=mysql_query("SELECT * 

FROM  `files`  where contextid=1

");

while($image=mysql_fetch_array($question))

{

$file='pluginfile.php';

$cntxid=$image['contextid'];

$component=$image['component'];

$filearea=$image['filearea'];

$itemid=$image['itemid'];

$filepath=$image['filepath'];

$filename=$image['filename'];


//$cm = get_coursemodule_from_instance('data', $itemid);

//$cmid = $cm->id;

//$context = context_module::instance(364);

//$cntxtid = $context->id;



//echo 'hello';

}

$str=file_rewrite_pluginfile_urls($questions,$file,4,$component,$filearea,$itemid);

echo $str.'<br>';

}

?>


but i want to display 

image url: <img src="http://moodle.consultfora.com/moodle/pluginfile.php/4/question/questiontext/192/1/1/1-sym-cparen-h1.gif"> this url display the image..

previous url missing the context id, how to fetch the this path...plz help me...


Average of ratings: Useful (1)
In reply to suri babu

Re: How to use file_rewrite_pluginfile_urls function

by suri babu -
From which table $contextid and $cmid coming in file_rewrite_pluginfile_url and what are the rows names in database table. 
In reply to suri babu

Re: How to use file_rewrite_pluginfile_urls function

by suri babu -

Hi

I am facing the problem i will try no of times but solution Image not displayed,

In moodle database in questions table to fetch the images ...but image url path is missing original path is

:::moodle.consultfora.com/moodle/pluginfile.php/670/question/questiontext/69/16/83/mc016-1.jpg" alt="mc016-1.jpg

But i got path is::::moodle.consultfora.com/moodle/pluginfile.php/670/question/questiontext/1/mc002-1.jpg


Please how to find the missing image path.



In reply to suri babu

Re: How to use file_rewrite_pluginfile_urls function

by suri babu -

Hi Smith,

how to use file_rewrite_pluginfile_urls  function please help me. where is located these modules on database.

In reply to Bezk Intos

Re: How to use file_rewrite_pluginfile_urls function

by Rajakumar Jillella -

Dear Bezk,


How did you get the following data from mdl_data table.

$dataid = 30;

$itemid = 5747;


In my case, i don't have any data in mdl_data table.

It is most helpful to me.


Thanks in Advance

In reply to Rajakumar Jillella

Re: How to use file_rewrite_pluginfile_urls function

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

mdl_data only has entries in it if you have created an instance of a 'database' activity in one of your courses.

If you have no database activities, then you have nothing in mdl_data.

In reply to Davo Smith

Re: How to use file_rewrite_pluginfile_urls function

by Rajakumar Jillella -

Dear Davo Smith,


Thanks for your immediate reply.

Here is i pasted my code for displaying questions, their options and their answers.

And i got the result like below screen.

If you want to check please go through this url


http://moodle.consultfora.com/moodle/quiz_pp/suriindex.php?testcategory=104


<?php

$course = mysql_query("SELECT DISTINCT `course` FROM `course_modules`");

while($coursecount = mysql_fetch_array($course)){

$coursecountrslt = $coursecount['course'];

$cmdqry = mysql_query("SELECT * FROM `course_modules` where instance='".$_GET['testcategory']."' AND `course`=$coursecountrslt");

while($cmodules = mysql_fetch_array($cmdqry)){

$cmodulesresult = $cmodules['id'];

$context = mysql_query("SELECT * FROM `context` WHERE `instanceid`=$cmodulesresult");

while($contextrslt = mysql_fetch_array($context)){

$contextdata = $contextrslt['id'];

$quesusage = mysql_query("SELECT * FROM `question_usages` WHERE `contextid`=$contextdata");

while($quesusagerslt = mysql_fetch_array($quesusage)){

$quesusagedata = $quesusagerslt['id'];

$quesattempts = mysql_query("SELECT * FROM `question_attempts` WHERE `questionusageid`=$quesusagedata");

while($quesattemptsrslt = mysql_fetch_array($quesattempts)){

$quesattemptsdata = $quesattemptsrslt['questionid'];

$question = mysql_query("SELECT * FROM `question` WHERE `id`=$quesattemptsdata");

while($questiondata = mysql_fetch_array($question)){

$ans = $questiondata['id'];

$ansrslt = mysql_query("SELECT * FROM `question_answers` WHERE `question`=$ans");

echo "<table border=0>";

echo "<br /><br /><tr><td colspan=2>".$questiondata['questiontext']."</td><td></td></tr>";

while($ansdata = mysql_fetch_array($ansrslt)){

echo "<tr><td colspan=2>".$ansdata['answer']."</td><td></td></tr>";

}echo "<tr><td style='padding-right: 30px;'><strong>Correct Answer : </strong>".$quesattemptsrslt['rightanswer']."</td><td><strong>Your Answer : </strong>".$quesattemptsrslt['responsesummary']."</td></tr>";

echo "</table>";

}

}

}

}

}

}

}?>

In reply to Davo Smith

Display images using file name

by suri babu -

questions_attempts table saving rightanswer with image file name only.

It is saving like  "[mc001-3.jpg]".

But, i want to display image using this file name and along with file_rewriteurl.

I have already displaying options from question_answer table. B'coz the "answer" column saving images with their respective tags.

I unable to display images using file name only


Thanks in Advance!!

In reply to Bezk Intos

Re: How to use file_rewrite_pluginfile_urls function

by Rajakumar Jillella -

Dear Bezk,


http://moodle.consultfora.com/moodle/quiz_pp/suriindex.php

Browse the above url,

The images displaying in "Full Length Test" only.

The other quizes are not displaying images.

undisplayed images are not getting "contextid" and "itemid" when i inspect them.


src=""


Could you please help me with this. How to get and display images in other quozes.

Here is my code,

--------------------------

<?php

if($_GET['testcategory'])

{

$course = mysql_query("SELECT DISTINCT `course` FROM `course_modules`");

while($coursecount = mysql_fetch_array($course)){

$coursecountrslt = $coursecount['course'];

$cmdqry = mysql_query("SELECT * FROM `course_modules` where instance='".$_GET['testcategory']."' AND `course`=$coursecountrslt");

while($cmodules = mysql_fetch_array($cmdqry)){

$cmodulesresult = $cmodules['id'];

$context = mysql_query("SELECT * FROM `context` WHERE `instanceid`=$cmodulesresult");

while($contextrslt = mysql_fetch_array($context)){

$contextdata = $contextrslt['id'];

$quesusage = mysql_query("SELECT * FROM `question_usages` WHERE `contextid`=$contextdata");

while($quesusagerslt = mysql_fetch_array($quesusage)){

$quesusagedata = $quesusagerslt['id'];

$quesattempts = mysql_query("SELECT * FROM `question_attempts` WHERE `questionusageid`=$quesusagedata");

while($quesattemptsrslt = mysql_fetch_array($quesattempts)){

$quesattemptsdata = $quesattemptsrslt['questionid'];

$question = mysql_query("SELECT * FROM `question` WHERE `id`=$quesattemptsdata");

echo "<table border=0>";

while($questiondata = mysql_fetch_array($question)){

$text = $questiondata['questiontext'];

$qtextformat = $questiondata['questiontextformat'];

$qtextid = $questiondata['id'];

$file = "pluginfile.php";

$itemdata = mysql_query("SELECT * FROM `files` WHERE `contextid`=$contextdata");

$itemrslt = mysql_fetch_array($itemdata);

$contextid = $itemrslt['contextid'];

$component = $itemrslt['component'];

$filearea = $itemrslt['filearea'];

$itemid = $itemrslt['itemid'];

$itemm = $itemid."/".$qtextformat."/".$qtextid;

$str=file_rewrite_pluginfile_urls($text,$file,$contextid,'question','questiontext',$itemm);

echo html_entity_decode($str);

$result1 = mysql_query("SELECT * FROM `question_answers` WHERE `question`=".$questiondata['id']);

while($row1 = mysql_fetch_array($result1)){

echo "<tr><td colspan=2>".$row1['answer']."</td><td></td></tr>";}

}echo "<tr><td><strong>Correct Answer : </strong>".$quesattemptsrslt['rightanswer']."</td><td style='padding-left: 25px'><strong>Your Answer : </strong>".$quesattemptsrslt['responsesummary']."</td></tr>";


echo "</table>";

}

}

}

}

}

}?>



In reply to Davo Smith

Re: How to use file_rewrite_pluginfile_urls function

by Rajakumar Jillella -

HI Smith,


http://moodle.consultfora.com/moodle/quiz_pp/suriindex.php


From the above url, "Full Length Test" only displaying images in questions.

Remaining quizes are not displaying.


The problem is, All the quizes are not getting 'contextid' and 'itemid' except "Full Length Test" quiz.


Your answer will helps me a lot.

Thanks in Advance