Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

NanoGong/Converting to Moodle 2.0: Difference between revisions

From MoodleDocs
(Note about plan not to migrate this page to the new developer resources. See template for more info.)
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Template:WillNotMigrate}}
{{Work in progress}}
{{Work in progress}}


Line 16: Line 17:
Add language folder: '''\lang\en\filter_nanogong.php''' with the following content:
Add language folder: '''\lang\en\filter_nanogong.php''' with the following content:


<code php>
<syntaxhighlight lang="php">
$string['filtername'] = 'NanoGong audio';
$string['filtername'] = 'NanoGong audio';
</code>
</syntaxhighlight>


=== Filter function ===
=== Filter function ===
Line 24: Line 25:
The filter function is wrapped inside a class:
The filter function is wrapped inside a class:
   
   
<code php>
<syntaxhighlight lang="php">
class filter_nanogong extends moodle_text_filter {
class filter_nanogong extends moodle_text_filter {
     function filter($text, array $options = array()){
     function filter($text, array $options = array()){
Line 30: Line 31:
     }
     }
}
}
</code>
</syntaxhighlight>


'''Note''': The callback function has to be '''outside''' this class definition!
'''Note''': The callback function has to be '''outside''' this class definition!
Line 47: Line 48:
* I then added this full URL as attribute to the NanoGong tag:
* I then added this full URL as attribute to the NanoGong tag:


<code php>
<syntaxhighlight lang="php">
<nanogong  
<nanogong  
     caption="Testing NanoGong..."  
     caption="Testing NanoGong..."  
     url="http://localhost/moodle-MOODLE_20_WEEKLY/pluginfile.php/130/mod_resource/content/1/sentence.wav"
     url="http://localhost/moodle-MOODLE_20_WEEKLY/pluginfile.php/130/mod_resource/content/1/sentence.wav"
/>
/>
</code>
</syntaxhighlight>


* As the URL is already in its full format I just commented out the following lines in '''filter.php'''.
* As the URL is already in its full format I just commented out the following lines in '''filter.php'''.


<code php>
<syntaxhighlight lang="php">
if ($url != "") {
if ($url != "") {
     if ($CFG->slasharguments)
     if ($CFG->slasharguments)
Line 63: Line 64:
         $url; # = "{$CFG->wwwroot}/file.php?file=$url";
         $url; # = "{$CFG->wwwroot}/file.php?file=$url";
}
}
</code>
</syntaxhighlight>


* This works as a proof of concept.
* This works as a proof of concept.
Line 72: Line 73:


; NanoGong tag
; NanoGong tag
<code php>
<syntaxhighlight lang="php">
<nanogong  
<nanogong  
     caption="Testing NanoGong..."  
     caption="Testing NanoGong..."  
     url="/130/mod_resource/content/1/sentence.wav"
     url="/130/mod_resource/content/1/sentence.wav"
/>
/>
</code>
</syntaxhighlight>


; filter.php
; filter.php
<code php>
<syntaxhighlight lang="php">
if ($url != "") {
if ($url != "") {
     if ($CFG->slasharguments)
     if ($CFG->slasharguments)
Line 87: Line 88:
         $url; # = "{$CFG->wwwroot}/file.php?file=$url";
         $url; # = "{$CFG->wwwroot}/file.php?file=$url";
}
}
</code>
</syntaxhighlight>


==== Documentation ====
==== Documentation ====
Line 107: Line 108:
I basically create a draft area and pass the draft item id to the uploading php script which resides in a designated file. In that script I create an instance of repository_upload and call its upload method:
I basically create a draft area and pass the draft item id to the uploading php script which resides in a designated file. In that script I create an instance of repository_upload and call its upload method:


<code php>
<syntaxhighlight lang="php">
$repo = new repository_upload($repo_id, null, array('ajax'=>true, 'name'=>'', 'type'=>'upload'));  
$repo = new repository_upload($repo_id, null, array('ajax'=>true, 'name'=>'', 'type'=>'upload'));  
try {
try {
Line 116: Line 117:
}
}
print $saveas_filename. ' uploaded';
print $saveas_filename. ' uploaded';
</code>
</syntaxhighlight>


The repository_upload looks for the uploaded file in a specific place and this place should be indicated in the javascript call in the upload button (btw, just a standard button, not a submit):
The repository_upload looks for the uploaded file in a specific place and this place should be indicated in the javascript call in the upload button (btw, just a standard button, not a submit):


<<code php>
<syntaxhighlight lang="php">
    var ret = recorder.sendGongRequest('PostToForm',
var ret = recorder.sendGongRequest('PostToForm',
                                        inputpage,
                                  inputpage,
                                        'repo_upload_file',
                                  'repo_upload_file',
                                        '',
                                  '',
                                        filename);
                                  filename);
</code>
</syntaxhighlight>


Then all that remains is to fetch the file from the draft area and store it in a proper area.
Then all that remains is to fetch the file from the draft area and store it in a proper area.


I use the repository_upload because I like to reuse existing code as much as possible. This may or may not be the best way. I'll look into that more thoroughly when I return to finalize the field.
I use the repository_upload because I like to reuse existing code as much as possible. This may or may not be the best way. I'll look into that more thoroughly when I return to finalize the field.
=== Further references ===
* [[Repository API]]


== See also ==
== See also ==

Latest revision as of 13:44, 24 June 2022


Warning: This page is no longer in use. The information contained on the page should NOT be seen as relevant or reliable.


Note: This page is a work-in-progress. Feedback and suggested improvements are welcome. Please join the discussion on moodle.org or use the page comments.


This page is for collecting relevant information for converting NanoGong to Moodle 2.0. Any help is welcome! --Frank Ralf 10:35, 7 April 2011 (UTC)

Moodle 2.0


Java settings

(Not sure if this is relevant, but better to keep in mind ...)

Filter

General information on filters in Moodle 2.0 can be found at Filters 2.0 and for developers at Filters 2.0.

Language folder

Add language folder: \lang\en\filter_nanogong.php with the following content:

$string['filtername'] = 'NanoGong audio';

Filter function

The filter function is wrapped inside a class:

class filter_nanogong extends moodle_text_filter {
    function filter($text, array $options = array()){
    ...
    }
}

Note: The callback function has to be outside this class definition!

Preventing caching

$CFG->currenttextiscacheable = false;

is deprecated, outcommented

File API

Proof of concept

  • Instead of the old file.php the new File API uses pluginfile.php.
  • I uploaded a test file (sentence.wav) as a resource to find out the internal URL Moodle uses for serving this file.
  • I then added this full URL as attribute to the NanoGong tag:
<nanogong 
    caption="Testing NanoGong..." 
    url="http://localhost/moodle-MOODLE_20_WEEKLY/pluginfile.php/130/mod_resource/content/1/sentence.wav"
/>
  • As the URL is already in its full format I just commented out the following lines in filter.php.
if ($url != "") {
    if ($CFG->slasharguments)
        $url; # = "{$CFG->wwwroot}/file.php$url";
    else
        $url; # = "{$CFG->wwwroot}/file.php?file=$url";
}
  • This works as a proof of concept.

Getting closer ...

This modification does also work:

NanoGong tag
<nanogong 
    caption="Testing NanoGong..." 
    url="/130/mod_resource/content/1/sentence.wav"
/>
filter.php
if ($url != "") {
    if ($CFG->slasharguments)
        $url = "{$CFG->wwwroot}/pluginfile.php$url";
    else
        $url; # = "{$CFG->wwwroot}/file.php?file=$url";
}

Documentation

Itamar' first shot

Note: Just copied over from http://moodle.org/mod/forum/post.php?reply=807695 --Frank Ralf 17:49, 13 September 2011 (WST)

In response to Frank's request to join forces I thought I'd give it a shot and happy to let ya all know that I've managed to make it work as a dataform field:

It is still just a stub and at any rate the implementation assumes all kinds of things the dataform does, so the code itself may not be very useful. But the approach and some relevant bits of code may help so here's a summary and if you have any questions just let me know.

I basically create a draft area and pass the draft item id to the uploading php script which resides in a designated file. In that script I create an instance of repository_upload and call its upload method:

$repo = new repository_upload($repo_id, null, array('ajax'=>true, 'name'=>'', 'type'=>'upload')); 
try {
    $ret = $repo->upload($saveas_filename, $maxbytes);
} catch (moodle_exception $e) {
    print $e->errorcode;
    die;
}
print $saveas_filename. ' uploaded';

The repository_upload looks for the uploaded file in a specific place and this place should be indicated in the javascript call in the upload button (btw, just a standard button, not a submit):

var ret = recorder.sendGongRequest('PostToForm',
                                  inputpage,
                                  'repo_upload_file',
                                  '',
                                  filename);

Then all that remains is to fetch the file from the draft area and store it in a proper area.

I use the repository_upload because I like to reuse existing code as much as possible. This may or may not be the best way. I'll look into that more thoroughly when I return to finalize the field.

Further references

See also

Necessary updating for Moodle 1.9
Migrating to Moodle 2.0
Forums
Moodle plugin database
NanoGong documentation