Create module page

Create module page

by Carlos Jimenez -
Number of replies: 0

Hi,

I'm trying to develop a web service that create new resource module page in moodle from a webservice.

Doing some tests and tracking the database via pgadmin3, i make some insertions into the db, but i am doing something wrong, because moodle can't show these insertions.

If someone can help me, please, i'll be greated.

PS. This is my webservice code if anybody can test and make it works.


<?php

require_once("$CFG->libdir/externallib.php");

 

class local_mypage_external extends external_api {

 

    /**

     * Returns description of method parameters

     * @return external_function_parameters

     */

    public static function create_page_parameters() {

        return new external_function_parameters(

            array(

                'page' => new external_multiple_structure(

                    new external_single_structure(

                        array(

                            'course' => new external_value(PARAM_INT, 'page record id'),

                    'coursemodule' => new external_value(PARAM_INT, 'id of course'),

                    'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'),

                    'content'=> new external_value(PARAM_RAW, 'page description text'),


                        )

                    )

                )

            )

        );

    }


public static function create_page_returns() {

        return new external_single_structure(

            array(

                  'id'       => new external_value(PARAM_INT, 'course id'),

                               )

          );

        

    }

 public static function create_page($page) { //Don't forget to set it as static

        global $CFG, $DB;

        require_once("$CFG->dirroot/course/modlib.php");

  require_once("$CFG->dirroot/mod/page/lib.php");

        // Attempt to include module library before we make any changes to DB.

    $course=2;

    $moduleinfo = set_moduleinfo_defaults($moduleinfo);

    $moduleinfo= new stdClass();

    $moduleinfo->course = 2;

    $moduleinfo->modulename ='page';

    $moduleinfo->coursemodule='15';

    $moduleinfo->name='Pagina de prueba';

    $moduleinfo->content='pagina';

    $moduleinfo->section=1;

    


    // First add course_module record because we need the context.

    $newcm = new stdClass();

    $newcm->course           = 2;

    $newcm->module           = 15;

    $newcm->instance         = 0; // Not known yet, will be updated later (this is similar to restore code).

    $newcm->visible          = 1;

    $newcm->visibleold       = 1;

   

    $newcm->groupmode        = 0;

    $newcm->groupingid       = 0;

    $transaction = $DB->start_delegated_transaction();


    if (!$moduleinfo->coursemodule = add_course_module($newcm)) {

        print_error('cannotaddcoursemodule');

    }

    $addinstancefunction    = $moduleinfo->modulename."_add_instance";

    try {

        $returnfromfunc = page_add_instance($moduleinfo, $mform);

    } catch (moodle_exception $e) {

        $returnfromfunc = 'FAIL';

    }

    if (!$returnfromfunc or !is_number($returnfromfunc)) {

        // Undo everything we can. This is not necessary for databases which

        // support transactions, but improves consistency for other databases.

        $modcontext = context_module::instance($moduleinfo->coursemodule);

        context_helper::delete_instance(CONTEXT_MODULE, $moduleinfo->coursemodule);

        $DB->delete_records('course_modules', array('id'=>$moduleinfo->coursemodule));


        if ($e instanceof moodle_exception) {

            throw $e;

        } else if (!is_number($returnfromfunc)) {

            print_error('invalidfunction', '', course_get_url($course, $moduleinfo->section));

        } else {

            print_error('cannotaddnewmodule', '', course_get_url($course, $moduleinfo->section), $moduleinfo->modulename);

        }

    }


    $moduleinfo->instance = $returnfromfunc;


    $DB->set_field('course_modules', 'instance', $returnfromfunc, array('id'=>$moduleinfo->coursemodule));


// Update embedded links and save files.

     $modcontext = context_module::instance($moduleinfo->coursemodule);

       if (!empty($introeditor)) {

           $moduleinfo->intro = file_save_draft_area_files($introeditor['itemid'], $modcontext->id,

                                                         'mod_'.$moduleinfo->modulename, 'intro', 0,

                                                        array('subdirs'=>true), $introeditor['text']);

$DB->set_field($moduleinfo->modulename, 'intro', $moduleinfo->intro, array('id'=>$moduleinfo->instance));

      }



$cm = $DB->get_record('page', array('id' => $returnfromfunc), '*', MUST_EXIST);

 

              // Check if we have not yet confirmed they have permission in this course.


                  $context = context_course::instance($cm->course);

                 self::validate_context($context);

  $modcontext = context_module::instance($cm->id);              

   require_capability('mod/page:addinstance', $modcontext);

    $eventdata = clone $moduleinfo;

    $eventdata->modname = $eventdata->modulename;

     $eventdata->id = $eventdata->coursemodule;

      $event = \core\event\course_module_created::create_from_cm($eventdata, $modcontext);

    $event->trigger();

     $moduleinfo = edit_module_post_actions($moduleinfo, $course);

     $transaction->allow_commit();

  return array('id'=>$returnfromfunc);

 }

}

Average of ratings: -