which fucntion can i use to create a new html resource

which fucntion can i use to create a new html resource

by Ravishankar Somasundaram -
Number of replies: 5
Dear moodlers,

I am trying to create a new html resource by my program.

i have the following data

$resource = new Object();
$resource->MAX_FILE_SIZE = 157286400;
$resource->type = 'html';
$resource->name = 'resource1';
$resource->summary = 'this is the descritption of the sample resource';
$resource->alltext = ' click this link<a href="google.com">google</a>';
$resource->windowpopup = 0;
$resource->blockdisplay = 1;
$resource->mform_showadvanced_last = 1;
$resource->width = 620;
$resource->height = 450;
$resource->visible = 1;
$resource->cmidnumber ='';
$resource->course = 12;
$resource->coursemodule ='';
$resource->section = 0;
$resource->module = 13;
$resource->modulename ='resource';
$resource->instance ='';
$resource->add = 'resource';
$resource->update = 0;
$resource->return = 0;
$resource->submitbutton = 'Save and display';
$resource->groupingid = 0;
$resource->groupmembersonly = 0;
$resource->groupmode = 0;
require_once $CFG->dirroot.'/mod/resource/lib.php';
resource_add_instance($resource);


to which function can i pass this and get the resource created ?
If some parameters above are missing which are essential to create a resource please indicate me.


Average of ratings: -
In reply to Ravishankar Somasundaram

Re: which fucntion can i use to create a new html resource

by sathish kamaraj -
Please follow the following steps.

1. Fill the form for creating the resource.
2. Find the location where the form will be submitted.(action page)
3. On the action page read all the data that is being POSTed. (print the array with a following "die" statement. )
4. Now you will see what data moodle will POST to create a resource.

This is the quickest way possible to find it.

In reply to sathish kamaraj

Re: which fucntion can i use to create a new html resource

by Ravishankar Somasundaram -
i just did the exact same to get the above data smile

the problem is am confused to find which function is actually getting the parameters to create the course , because there a re lot of functions involved wihich has no comments and finding one amongst is going to take some time sad


In reply to Ravishankar Somasundaram

Re: which fucntion can i use to create a new html resource

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
As I told you before on IRC, this has previously been discussed here and you just have to search:

http://moodle.org/mod/forum/discuss.php?d=124679

http://moodle.org/mod/forum/discuss.php?d=110464
In reply to Tim Hunt

Re: which fucntion can i use to create a new html resource

by Ravishankar Somasundaram -
Thank you tim i dont know with what keyword you searched the forumsto get these results, i tried resource manual add, and some other things where i was not able to find these ,....my bad


the above links was very usefull in understanding the structure which is exactly what i needed,

i figured out till this
  • Include the mod/$moduleName/lib.php for the module type your adding
  • Check module type is allowed in course
  • Get the section object you want to add the new module to
  • Create a new empty object() for the new module to be added
  • Set the correct properties for it depending on its type (course id, section id, module type etc)
  • Call the $moduleName_add_instance() function to create an actual module instance

but missed out to
  • Associate the module instance with the course
  • Associate the module instance with the section in the course

have changed my code but still facing problems,

Am using moodle 1.9.4
i wanted a resource to be created when the course gets created , so i placed the code in the
file course/edit.php after the lines which say

// ensure we can use the course right after creating it
// this means trigger a reload of accessinfo...
mark_context_dirty($context->
path);

Follow is the code that i placed in the file to create a html resource after the above given lines.The problem is the course gets created but the resource is not showing up, please indicate me what am i missing.


amjust trying things out so you can see that the code is untidy , please do bare with me


$resource = new Object();

$resource->MAX_FILE_SIZE = 157286400;
$resource->type = 'html';
$resource->name = 'xyz';
$resource->summary = 'this is the descritption of the sample resource';
$resource->alltext = 'click this link<a href="google.com">google</a>';
$resource->windowpopup = 0;
$resource->blockdisplay = 1;
$resource->mform_showadvanced_last = 1;
$resource->width = 620;
$resource->height = 450;
$resource->visible = 1;
$resource->cmidnumber ='';
$resource->course = '$COURSE->id';
$resource->coursemodule ='';
$resource->section = 0;
$resource->module = 13;
$resource->modulename ='resource';
$resource->instance ='';
$resource->add = 'resource';
$resource->update = 0;
$resource->return = 0;
$resource->submitbutton = 'Save and return to course';
$resource->groupingid = 0;
$resource->groupmembersonly = 0;
$resource->groupmode = 0;
require_once $CFG->dirroot.'/mod/resource/lib.php';

$resource->instance = resource_add_instance($resource);

if (! $resource->coursemodule = add_course_module($resource) ) {
return false;
} else {
echo "skipping";
}

if (! $sectionid = add_mod_to_section($resource) ) {
return false;
} else {
echo "skipping <= added resource on section".$sectionid;
}

rebuild_course_cache($course->id);
grade_regrade_final_grades($course->id)

add_to_log($course->id, "course", "add mod", "../mod/$resource->modulename/view.php?id=$resource->coursemodule", "$resource->modulename $resource->instance");
add_to_log($course->id, $resource->modulename, "add", "view.php?id=$resource->coursemodule", "$resource->instance", $resource->coursemodule);




In reply to Ravishankar Somasundaram

Re: which fucntion can i use to create a new html resource

by Ravishankar Somasundaram -
Accomplished it, posting here for the benefit of others.

//construct an object with necessary parameters

$resource = new Object();
$resource->MAX_FILE_SIZE = 157286400;
$resource->type = 'html';
$resource->name = 'your resource name';
$resource->summary = 'this is the description of the sample resource';
$resource->alltext = 'click this link<a href="google.com">google</a>'; //here goes your html body
$resource->windowpopup = 0;
$resource->blockdisplay = 1;
$resource->mform_showadvanced_last = 1;
$resource->width = 620;
$resource->height = 450;
$resource->visible = true;
$resource->cmidnumber ='';
$resource->course = "$course->id";
$resource->coursemodule ='';
$resource->section = 0;
$resource->module = 13;
$resource->modulename ='resource';
$resource->instance ='';
$resource->add = 'resource';
$resource->update = 0;
$resource->return = 0;
$resource->submitbutton2 = 'Save and return to course';
$resource->groupingid = 0;
$resource->groupmembersonly = 0;
$resource->groupmode = 0;
$resource->timemodified = time();

require_once $CFG->dirroot.'/mod/resource/lib.php';

$resource->instance = resource_add_instance($resource);

//add the module to the course cache
if (! $resource->coursemodule = add_course_module($resource) ) {
error("Could not add a new course module");
}

//add module to the respective section in the course
if (! $sectionid = add_mod_to_section($resource) ) {
error("Could not add the new course module to that section");
}

//check for the required params before finalizing things wrt course and module addition on a section
if (! set_field("course_modules", "section", $sectionid, "id", $resource->coursemodule)) {
error("Could not update the course module with the correct section");
}

// make sure visibility is set correctly (in particular in calendar)
set_coursemodule_visible($resource->coursemodule, $resource->visible);

/* if (isset($resource->cmidnumber)) { //label
// set cm idnumber
set_coursemodule_idnumber($resource->coursemodule, $resource->cmidnumber);
}
*/

// log the creation process
add_to_log($course->id, "course", "add mod",
"../mod/$resource->modulename/view.php?id=$resource->coursemodule",
"$resource->modulename $resource->instance");
add_to_log($course->id, $resource->modulename, "add",
"view.php?id=$resource->coursemodule",
"$resource->instance", $resource->coursemodule);

//rebuild the course cache to show the created resource immediately.

rebuild_course_cache($course->id);