コード例 #1
0
 /**
  * Function to create a question.
  *
  * @param questionnaire $questionnaire
  * @param array|stdClass $record
  * @param array|stdClass $data - accompanying data for question - e.g. choices
  * @return \mod_questionnaire\question\base the question object
  */
 public function create_question(questionnaire $questionnaire, $record = null, $data = null)
 {
     global $DB;
     // Increment the question count.
     $this->questioncount++;
     $record = (array) $record;
     $record['position'] = count($questionnaire->questions);
     if (!isset($record['survey_id'])) {
         throw new coding_exception('survey_id must be present in phpunit_util::create_question() $record');
     }
     if (!isset($record['name'])) {
         throw new coding_exception('name must be present in phpunit_util::create_question() $record');
     }
     if (!isset($record['type_id'])) {
         throw new coding_exception('typeid must be present in phpunit_util::create_question() $record');
     }
     if (!isset($record['content'])) {
         $record['content'] = 'Random ' . $this->type_str($record['type_id']) . ' ' . uniqid();
     }
     // Get question type.
     $typeid = $record['type_id'];
     if ($typeid === QUESRATE && !isset($record['length'])) {
         $record['length'] = 5;
     }
     if ($typeid !== QUESPAGEBREAK && $typeid !== QUESSECTIONTEXT) {
         $qtype = $DB->get_record('questionnaire_question_type', ['id' => $typeid]);
         if (!$qtype) {
             throw new coding_exception('Could not find question type with id ' . $typeid);
         }
         // Throw an error if this requires choices and it hasn't got them.
         $this->validate_question($qtype->typeid, $data);
     }
     $record = (object) $record;
     // Add the question.
     $record->id = $DB->insert_record('questionnaire_question', $record);
     $typename = \mod_questionnaire\question\base::qtypename($record->type_id);
     $question = questionnaire::question_factory($typename, $record->id, $record);
     // Add the question choices if required.
     if ($typeid !== QUESPAGEBREAK && $typeid !== QUESSECTIONTEXT) {
         if ($question->has_choices()) {
             $this->add_question_choices($question, $data);
             $record->opts = $data;
         }
     }
     // Update questionnaire.
     $questionnaire->add_questions();
     return $question;
 }
コード例 #2
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;
}
コード例 #3
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);
}