Last active
May 5, 2018 21:22
-
-
Save init90/e6986d664f524f74729ba58c739dc6ee to your computer and use it in GitHub Desktop.
Drupal 7. Quiz module. Add programality new questions to quiz.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Create new multichoice questions. | |
| */ | |
| function cba_quiz_new_multichoice_question() { | |
| global $user; | |
| $question = new stdClass(); | |
| $question->title = t('Questions template'); | |
| $question->type = 'multichoice'; | |
| $question->body = array(LANGUAGE_NONE => array(array('value' => t('Your questions text')))); | |
| $question->choice_multi = 0; | |
| $question->choice_random = 0; | |
| $question->choice_boolean = 0; | |
| $question->language = LANGUAGE_NONE; | |
| $question->uid = $user->uid; | |
| $question->status = 1; | |
| $question->alternatives = cba_quiz_return_default_answers(); | |
| node_object_prepare($question); | |
| $question = node_submit($question); | |
| node_save($question); | |
| $edit_questions_url = "node/{$question->nid}/edit"; | |
| $url_options = array('query' => array('destination' => 'manage/quizzes')); | |
| drupal_goto($edit_questions_url, $url_options); | |
| } | |
| /** | |
| * Default answers settings form. | |
| */ | |
| function cba_quiz_default_answers($form, &$form_state) { | |
| $answer_description = t('Enter one answer per line'); | |
| $form = array(); | |
| $form['invoice']['cba_quiz_default_answers'] = array( | |
| '#type' => 'textarea', | |
| '#title' => t('Default answers list'), | |
| '#description' => $answer_description, | |
| '#default_value' => variable_get('cba_quiz_default_answers', ''), | |
| ); | |
| return system_settings_form($form); | |
| } | |
| /** | |
| * Return defaults answers list for question. | |
| * | |
| * @return array $answers_list | |
| */ | |
| function cba_quiz_return_default_answers() { | |
| $answers_list = array(); | |
| $default_answers = variable_get('cba_quiz_default_answers', ''); | |
| if ($default_answers) { | |
| $default_answers = explode("\r\n", $default_answers); | |
| foreach ($default_answers as $answer_text) { | |
| $answers_list[] = cba_quiz_return_answer_array($answer_text); | |
| } | |
| } | |
| return $answers_list; | |
| } | |
| /** | |
| * Return answer render array. | |
| * | |
| * @param string $answer_text | |
| * @return array $answer | |
| */ | |
| function cba_quiz_return_answer_array($answer_text) { | |
| $answer = array( | |
| 'answer' => array( | |
| 'value' => $answer_text, | |
| 'format' => 'plain_text', | |
| ), | |
| 'feedback_if_chosen' => array( | |
| 'format' => 'plain_text', | |
| 'value' => '', | |
| ), | |
| 'feedback_if_not_chosen' => array( | |
| 'format' => 'plain_text', | |
| 'value' => '', | |
| ), | |
| 'score_if_chosen' => 0, | |
| ); | |
| return $answer; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment