コード例 #1
0
ファイル: lib.php プロジェクト: achocoza/moodle26
function questionnaire_add_instance($questionnaire)
{
    // Given an object containing all the necessary data,
    // (defined by the form in mod.html) this function
    // will create a new instance and return the id number
    // of the new instance.
    global $COURSE, $DB, $CFG;
    require_once $CFG->dirroot . '/mod/questionnaire/questionnaire.class.php';
    require_once $CFG->dirroot . '/mod/questionnaire/locallib.php';
    // Check the realm and set it to the survey if it's set.
    if (empty($questionnaire->sid)) {
        // Create a new survey.
        $cm = new Object();
        $qobject = new questionnaire(0, $questionnaire, $COURSE, $cm);
        if ($questionnaire->create == 'new-0') {
            $sdata = new Object();
            $sdata->name = $questionnaire->name;
            $sdata->realm = 'private';
            $sdata->title = $questionnaire->name;
            $sdata->subtitle = '';
            $sdata->info = '';
            $sdata->theme = '';
            // Theme is deprecated.
            $sdata->thanks_page = '';
            $sdata->thank_head = '';
            $sdata->thank_body = '';
            $sdata->email = '';
            $sdata->owner = $COURSE->id;
            if (!($sid = $qobject->survey_update($sdata))) {
                print_error('couldnotcreatenewsurvey', 'questionnaire');
            }
        } else {
            $copyid = explode('-', $questionnaire->create);
            $copyrealm = $copyid[0];
            $copyid = $copyid[1];
            if (empty($qobject->survey)) {
                $qobject->add_survey($copyid);
                $qobject->add_questions($copyid);
            }
            // New questionnaires created as "use public" should not create a new survey instance.
            if ($copyrealm == 'public') {
                $sid = $copyid;
            } else {
                $sid = $qobject->sid = $qobject->survey_copy($COURSE->id);
                // All new questionnaires should be created as "private".
                // Even if they are *copies* of public or template questionnaires.
                $DB->set_field('questionnaire_survey', 'realm', 'private', array('id' => $sid));
            }
        }
        $questionnaire->sid = $sid;
    }
    $questionnaire->timemodified = time();
    // May have to add extra stuff in here.
    if (empty($questionnaire->useopendate)) {
        $questionnaire->opendate = 0;
    }
    if (empty($questionnaire->useclosedate)) {
        $questionnaire->closedate = 0;
    }
    if ($questionnaire->resume == '1') {
        $questionnaire->resume = 1;
    } else {
        $questionnaire->resume = 0;
    }
    // Field questionnaire->navigate used for branching questionnaires. Starting with version 2.5.5.
    /* if ($questionnaire->navigate == '1') {
           $questionnaire->navigate = 1;
       } else {
           $questionnaire->navigate = 0;
       } */
    if (!($questionnaire->id = $DB->insert_record("questionnaire", $questionnaire))) {
        return false;
    }
    questionnaire_set_events($questionnaire);
    return $questionnaire->id;
}
コード例 #2
0
function questionnaire_add_instance($questionnaire)
{
    /// Given an object containing all the necessary data,
    /// (defined by the form in mod.html) this function
    /// will create a new instance and return the id number
    /// of the new instance.
    global $COURSE;
    // Check the realm and set it to the survey if it's set.
    if (!empty($questionnaire->sid) && !empty($questionnaire->realm)) {
        // JR not needed
        //        set_field('questionnaire_survey', 'realm', $questionnaire->realm, 'id', $questionnaire->sid);
    } else {
        if (empty($questionnaire->sid)) {
            /// Create a new survey:
            $cm = new Object();
            $qobject = new questionnaire(0, $questionnaire, $COURSE, $cm);
            if ($questionnaire->create == 'new-0') {
                $sdata = new Object();
                $sdata->name = $questionnaire->name;
                $sdata->realm = 'private';
                $sdata->title = $questionnaire->name;
                $sdata->subtitle = '';
                $sdata->info = '';
                $sdata->theme = 'default.css';
                // JR DEV
                $sdata->thanks_page = '';
                $sdata->thank_head = '';
                $sdata->thank_body = '';
                $sdata->email = '';
                $sdata->owner = $COURSE->id;
                if (!($sid = $qobject->survey_update($sdata))) {
                    error('Could not create a new survey!');
                }
            } else {
                $copyid = explode('-', $questionnaire->create);
                $copyrealm = $copyid[0];
                $copyid = $copyid[1];
                if (empty($qobject->survey)) {
                    $qobject->add_survey($copyid);
                    $qobject->add_questions($copyid);
                }
                // JR new questionnaires created as "use public" should not create a new survey instance
                if ($copyrealm == 'public') {
                    $sid = $copyid;
                } else {
                    $sid = $qobject->sid = $qobject->survey_copy($COURSE->id);
                    // JR all new questionnaires should be created as "private", even if they are *copies* of public or template questionnaires
                    set_field('questionnaire_survey', 'realm', 'private', 'id', $questionnaire->sid);
                }
            }
            $questionnaire->sid = $sid;
        }
    }
    $questionnaire->timemodified = time();
    # May have to add extra stuff in here #
    if (empty($questionnaire->useopendate)) {
        $questionnaire->opendate = 0;
    }
    if (empty($questionnaire->useclosedate)) {
        $questionnaire->closedate = 0;
    }
    if ($questionnaire->resume == '1') {
        //JR
        $questionnaire->resume = 1;
    } else {
        $questionnaire->resume = 0;
    }
    $questionnaire->navigate = 1;
    // not used at all!
    return insert_record("questionnaire", $questionnaire);
}