how to create assign web service

how to create assign web service

by juan guardado -
Number of replies: 2

hi partners


I want create a web service that allow me create a new task in a course,


any idea?



Average of ratings: -
In reply to juan guardado

Re: how to create assign web service

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Your starting point would be the Web Services API - that documentation links to a detailed tutorial and a template plugin.

As for what the function called by the web service does (the "external function" in moodle-speak), if you give more detail of what you mean by "create a new task", someone may be able to give you some more pointers.

In reply to Mark Johnson

Re: how to create assign web service

by juan guardado -

Hi Mark, thanks for youre replay


i want to do this following:   -add a new assignement

im using template plugin and trying set params from  mod/assign/locallib/ assign_add_instance function , now, when i call the web service, the assignment is created and shown in the calendar events section but not in the assignments section.

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

mod/assign/lib:

function assign_add_instance(stdClass $data, mod_assign_mod_form $form = null) {

    global $CFG;

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

    $assignment = new assign(context_module::instance($data->coursemodule), null, null);

    return $assignment->add_instance($data, true);

}

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

template/myplugin/externallib

class local_myplugin_external extends external_api {

    /*

     * Returns description of method parameters

     * @return external_function_parameters

     */

    public static function create_task_parameters() {

        return new external_function_parameters(

            array(

                'groups' => new external_multiple_structure(

                    new external_single_structure(

                        array(

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

'coursemodule' => new external_value(PARAM_RAW, 'id del modulo'),

'name' => new external_value(PARAM_TEXT, 'Nombre de tarea'),

'intro' => new external_value(PARAM_TEXT, 'Descripcion'),

'introformat' => new external_value (PARAM_INT,'INTRO FORMAT', VALUE_DEFAULT, 1),

'alwaysshowdescription' => new external_value (PARAM_INT,'alwaysshowdescription', VALUE_DEFAULT, 1),

'nosubmission' => new external_value (PARAM_INT,'numero submission', VALUE_DEFAULT, 0),

'submissiondrafts' => new external_value (PARAM_INT,'submissiondrafts', VALUE_DEFAULT, 0),

'sendnotifications' => new external_value (PARAM_INT,'sendnotifications', VALUE_DEFAULT, 0),

'sendlatenotifications' => new external_value (PARAM_INT,'sendlatenotifications', VALUE_DEFAULT, 0),

'duedate' => new external_value (PARAM_INT,'duedate', VALUE_DEFAULT, 0),

'allowsubmissionsfromdate' => new external_value (PARAM_INT,'allowsubmissionsfromdate', VALUE_DEFAULT, 0),

'grade' => new external_value (PARAM_INT,'grade', VALUE_DEFAULT, 100),

'timemodified' => new external_value (PARAM_INT,'timemodified', VALUE_DEFAULT, 0),

'requiresubmissionstatement' => new external_value (PARAM_INT,'requiresubmissionstatement', VALUE_DEFAULT, 0),

'completionsubmit' => new external_value (PARAM_INT,'completionsubmit', VALUE_DEFAULT, 0),

'cutoffdate' => new external_value (PARAM_INT,'cutoffdate', VALUE_DEFAULT, 0),

'teamsubmission' => new external_value (PARAM_INT,'teamsubmission', VALUE_DEFAULT, 0),

'requireallteammemberssubmit' => new external_value (PARAM_INT,'requireallteammemberssubmit', VALUE_DEFAULT, 0),

'teamsubmissiongroupingid' => new external_value (PARAM_INT,'teamsubmissiongroupingid', VALUE_DEFAULT, 0),

'blindmarking' => new external_value (PARAM_INT,'blindmarking', VALUE_DEFAULT, 0),

'revealidentities' => new external_value (PARAM_INT,'revealidentities', VALUE_DEFAULT, 0),

'attemptreopenmethod' => new external_value (PARAM_TEXT,'attemptreopenmethod', VALUE_DEFAULT, none),

'maxattempts' => new external_value (PARAM_INT,'maxattempts', VALUE_DEFAULT, -1),

'markingworkflow' => new external_value (PARAM_INT,'markingworkflow', VALUE_DEFAULT, 0),

'markingallocation' => new external_value (PARAM_INT,'markingallocation', VALUE_DEFAULT, 0),

'sendstudentnotifications' => new external_value (PARAM_INT,'sendstudentnotifications', VALUE_DEFAULT, 1),

)

                    )

                )

            )

        );

    }

public static function create_task_returns() {

        return //new external_multiple_structure(

            new external_single_structure(

                array(

               //     'id' => new external_value(PARAM_INT, 'group record id'),

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

                )

        );

    }

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

        global $CFG, $DB;

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

        $params = self::validate_parameters(self::create_task_parameters(), array('groups'=>$groups));

        $transaction = $DB->start_delegated_transaction(); //If an exception is thrown in the below code, all DB queries in this code will be rollback.

        $groups = array();

        foreach ($params['groups'] as $group) {

            $group = (object)$group;

            $group->id = assign_add_instance($group);

            $groups[] = (array)$group;

        }

        $transaction->allow_commit();

        return array('realizo'=>1);

    }

}

Attachment web services error.PNG