使用moodle rest api在moodle课程模块中上传scorm包

时间:2018-03-13 06:47:52

标签: moodle moodle-api

我已经使用moodle RestAPI(函数名称:core_course_create_courses)创建了一个课程。然后我想在moodle课程模块/活动中上传一个scorm包。 我已经为此编写了一些代码,这段代码将在该课程中创建一个模块。我不知道如何在该课程模块中上传scorm包。

这是我的代码

  1. 在第327行的lib / db / services.php中添加函数core_course_create_modules

    'core_course_create_modules' => array( 'classname' => 'core_course_external', 'methodname' => 'create_modules', 'classpath' => 'course/externallib.php', 'description' => 'Creates modules in a course', 'type' => 'write', 'capabilities' => 'moodle/course:manageactivities', )

  2. 在Courses / externallib.php文件中添加3方法

  3. 2.1)create_modules_parameters

    => `public static function create_modules_parameters() {
            $courseconfig = get_config('moodlecourse'); //needed for many default values
            return new external_function_parameters(
                array(
                    'courseid' => new external_value(PARAM_INT, 'ID of the course'),
                    'modules' => new external_multiple_structure(
                        new external_single_structure(
                            array(
                                'modulename' => new external_value(PARAM_TEXT, 'Name of the module'),
                                'section' => new external_value(PARAM_INT, 'Sectionnumber'),
                                'name' => new external_value(PARAM_TEXT, "Title of the module", VALUE_OPTIONAL),
                                'visible' => new external_value(PARAM_INT, '1: available to student, 0:not available', VALUE_OPTIONAL),
                                'description' => new external_value(PARAM_TEXT, 'the new module description', VALUE_OPTIONAL),
                                'descriptionformat' => new external_format_value(PARAM_INT, 'description', VALUE_DEFAULT),
                                'groupmode' => new external_value(PARAM_INT, 'no group, separate, visible', VALUE_DEFAULT, $courseconfig->groupmode),
                                'groupmembersonly' => new external_value(PARAM_INT, '1: yes, 0: no', VALUE_DEFAULT, 0),
                                'groupingid' => new external_value(PARAM_INT, 'grouping id',VALUE_DEFAULT, 0)
                            )
                        )
                    )
                )
            );
        }`
    

    2.2)create_modules

    `public static function create_modules($courseid, $modules) {
        global $CFG, $DB;
    
        require_once($CFG->dirroot . "/course/lib.php");
        require_once($CFG->dirroot . "/course/modlib.php");
    
        $moduleinfo = new stdClass();
        $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
    
        // Clean the parameters.
        $params = self::validate_parameters(self::create_modules_parameters(), array('courseid' => $courseid,'modules' => $modules));
    
        $context = context_course::instance($course->id);
        require_capability('moodle/course:manageactivities', $context);
    
        foreach ($params['modules'] as $mod) {
            $module = (object) $mod;
            $moduleobject = $DB->get_record('modules', array('name'=>$module->modulename), '*', MUST_EXIST);
    
            if(trim($module->modulename) == ''){
                throw new invalid_parameter_exception('Invalid module name');
            }
    
            if (!course_allowed_module($course, $module->modulename)){
                throw new invalid_parameter_exception('Module "'.$module->modulename.'" is disabled');
            }
    
            if(is_null($module->visible)){
                $module->visible = 1;
            }
    
            if(is_null($module->description)){
                $module->description = '';
            }
    
            if(!is_null($module->name) && trim($module->name) != ''){
                $moduleinfo->name = $module->name;
            }
    
            $moduleinfo->modulename = $module->modulename;
    
            $moduleinfo->visible = $module->visible;
            $moduleinfo->course = $courseid;
            $moduleinfo->section = $module->section;
            $moduleinfo->introeditor = array('text' => $module->description, 'format' => $module->descriptionformat, 'itemid' => 0);
            $moduleinfo->quizpassword = '';
            $moduleinfo->groupmode = $module->groupmode;
            $moduleinfo->groupmembersonly = $module->groupmembersonly;
            $moduleinfo->groupingid = $module->groupingid;
            $retVal = create_module($moduleinfo);
    
            $result[] = array('id'=>$retVal->id);
        }
        return $result;
    }`
    
    2.3)create_modules_returns
    `public static function create_modules_returns() {
        return new external_multiple_structure(
            new external_single_structure(
                array(
                    'id' => new external_value(PARAM_INT, 'new module id')
                )
            )
        );
    }`
    

    调用函数的参数

    $module['courseid'] = 1; //course id
        $module['modules'][] = array(
        "section" => 2,//PARAM_INT, 'Sectionnumber'
        "modulename" => "scorm",//PARAM_TEXT, 'Name of the module'
        "name" => "Testing1",// OPTIONAL PARAM_TEXT, "Title of the module",
        "visible" => 1,// OPTIONAL PARAM_INT, '1: available to student, 0:not available',
        "description"=>"testing module using rest api", // OPTIONAL PARAM_TEXT, 'the new module description',
        "descriptionformat"=>1,//PARAM_INT, 'description', VALUE_DEFAULT
        //"groupmode" => "",//PARAM_INT, 'no group, separate, visible', VALUE_DEFAULT, $courseconfig->groupmode
        //"groupmembersonly" => "",//PARAM_INT, '1: yes, 0: no', VALUE_DEFAULT, 0
        //"groupingid" => "",//PARAM_INT, 'grouping id',VALUE_DEFAULT, 0
      );
    

    按照上面的代码,  它的名字字段作为scorm包,在管理端,它的显示名称为scorm,当我点击它显示错误时:

    screenshot for error

    所以,我不知道如何使用moodle Rest api在课程模块中上传scorm包,我该怎么办?

    谢谢。

0 个答案:

没有答案