在moodle中使用rest api创建测验

时间:2014-07-14 06:24:33

标签: php rest moodle

我是moodle的新手,我正在尝试在moodle中创建rest api,在moodle中使用以下代码添加新的测验。它在测验表中添加了条目,但在课程UI中没有显示,原因是什么?

任何人都可以帮助我使用rest api添加测验吗?

public static function create_quiz($courseid, $quizname, $timeopen, $timeclose, $attemptallowed, $intro, $questions) {
        global $DB;
        $params = self::validate_parameters(self::create_quiz_parameters(), array('courseid' => $courseid,
                    'quizname' => $quizname,
                    'timeopen' => $timeopen,
                    'timeclose' => $timeclose,
                    'attemptallowed' => $attemptallowed,
                    'intro' => $intro,
                    'questions' => $questions));
        $q_param = array('courseid' => $params['courseid'],
            'quizname' => $params['quizname']);
        //var_dump("Query parameter".$q_param);
        $sql = "SELECT q.id
           FROM {quiz} q          
           WHERE q.course=:courseid AND q.name=:quizname";
        $rqa = $DB->get_record_sql($sql, $q_param);
        if (isset($rqa) && $rqa != null) {
            $result = array();
            $result['quizid'] = 0;
            $result['status'] = "failure";
            $result['message'] = "This quiz name already exists in this course. Please try with different quiz name";
        } else {
            $quiz = new stdClass();
            $quiz->course = $courseid;
            $quiz->name = $quizname;
            $quiz->timeopen = $timeopen;
            $quiz->timeclose = $timeclose;
            $quiz->attempts = $attemptallowed;
            $quiz->intro = $intro;
            $quiz->questions = $questions;
            $rqa = $DB->insert_record('quiz', $quiz);
            if (isset($rqa)) {
                $result = array();
                $result['quizid'] = $rqa;
                $result['status'] = "success";
                $result['message'] = "Quiz created succesfully";
            } else {
                $result = array();
                $result['quizid'] = 0;
                $result['status'] = "failure";
                $result['message'] = "Some error occured please try again";
            }
    }
    return $result;
}

/**
 * Describes the create_category return value
 * @return external_single_structure
 * @since  Moodle 2.4
 */
public static function create_quiz_returns() {
    return new external_single_structure(
                    array('quizid' => new external_value(PARAM_INT, 'id of quiz'),
                        'status' => new external_value(PARAM_ALPHA, "Status of quiz"),
                        'message' => new external_value(PARAM_TEXT, "quiz message")
            ));
} 

1 个答案:

答案 0 :(得分:1)

您需要将模块添加到课程中,这里有一些代码可以执行此操作

addmoduletocourse($courseid, $rqa, 'quiz');

function addmoduletocourse($courseid, $instanceid, $modname, $sectionnum = 0) {
    global $DB;

    course_create_sections_if_missing($courseid, $sectionnum);

    $moduleid = $DB->get_field('modules', 'id', array('name' => $modname), MUST_EXIST);
    $sectionid = $DB->get_field('course_sections', 'id', array('course' => $courseid, 'section' => $sectionnum), MUST_EXIST);

    // Add the module to the course.
    $newcm = new stdClass();
    $newcm->course           = $courseid;
    $newcm->module           = $moduleid;
    $newcm->section          = $sectionid;
    $newcm->added            = time();
    $newcm->instance         = $instanceid;
    $newcm->visible          = 1;
    $newcm->groupmode        = 0;
    $newcm->groupingid       = 0;
    $newcm->groupmembersonly = 0;
    $newcm->showdescription  = 0;
    $cmid = add_course_module($newcm);

    // And add it to the section.
    course_add_cm_to_section($courseid, $cmid, $sectionnum);

}