Return the list of groups used in a question.
@param stdClass $question the question data.
@return array the groups used, or false if an error occurs.
540845
protected function get_array_of_placeholders($question) {
        $qtext = $question->questiontext;
        $error = '<b> ERROR</b>: Please check the form for this question. ';
        if (!$qtext) {
            echo $error . 'The question text is empty!';
            return false;
        }

        // Get the slots.
        $slots = $this->getEmbeddedTextArray($question);

        if (!$slots) {
            echo $error . 'The question text is not in the correct format!';
            return false;
        }

        $output = array();
        foreach ($slots as $slot) {
            $output[] = substr($slot, 2, strlen($slot) - 4); // 2 is for '[[' and 4 is for '[[]]'.
        }
        return $output;
    }
841329
protected function validateSolutions(array $question)
    {
        return $this->keywordValidator->validateCollection($question['solutions'], [Validation::NO_SCHEMA, Validation::VALIDATE_SCORE]);
    }
1255275
protected function askForGroups()
    {
        $default = 0;
        $groups = $this->groups();
        $group_names = collect($groups)->pluck('name')->toArray();
        $selected_group_names =  $this->choice('Groups?', $group_names ,$default, null, true);

        $groups = collect($groups)->filter(function ($group) use ($selected_group_names) {
            return in_array($group->name,$selected_group_names);
        });

        return $groups->pluck('id');
    }
1042842
protected function create_default_options($question) {
        // Create a default question options record.
        $options = new stdClass();
        $options->questionid = $question->id;

        // Get the default strings and just set the format.
        $options->correctfeedback = get_string('correctfeedbackdefault', 'question');
        $options->correctfeedbackformat = FORMAT_HTML;
        $options->partiallycorrectfeedback = get_string('partiallycorrectfeedbackdefault', 'question');;
        $options->partiallycorrectfeedbackformat = FORMAT_HTML;
        $options->incorrectfeedback = get_string('incorrectfeedbackdefault', 'question');
        $options->incorrectfeedbackformat = FORMAT_HTML;

        $config = get_config('qtype_multichoice');
        $options->single = $config->answerhowmany;
        if (isset($question->layout)) {
            $options->layout = $question->layout;
        }
        $options->answernumbering = $config->answernumbering;
        $options->shuffleanswers = $config->shuffleanswers;
        $options->shownumcorrect = 1;

        return $options;
    }
1127660
private function serializeObjects(Item $question, array $options = [])
    {
        return array_values(array_map(function (ItemObject $object) use ($options) {
            return $this->itemObjectSerializer->serialize($object, $options);
        }, $question->getObjects()->toArray()));
    }
55073
protected function import_category($question) {
        $qo = new stdClass();
        $qo->qtype = 'category';
        $qo->category = $this->import_text($question['#']['category'][0]['#']['text']);
        $qo->info = '';
        $qo->infoformat = FORMAT_MOODLE;
        if (array_key_exists('info', $question['#'])) {
            $qo->info = $this->import_text($question['#']['info'][0]['#']['text']);
            // The import should have the format in human readable form, so translate to machine readable format.
            $qo->infoformat = $this->trans_format($question['#']['info'][0]['@']['format']);
        }
        return $qo;
    }
710770
public function getResourceGroups(Query $query = null) {
        if (!$query) {
            $query = new Query();
        }

        return $this->getCommand('GetResourceGroups', array(
            'page' => $query->page(),
            'limit' => $query->limit(),
        ))->execute();
    }
271886
public function validateSolutions(array $question)
    {
        $errors = [];

        // check solution IDs are consistent with proposals IDs
        $proposalIds = array_map(function (array $proposal) {
            return $proposal['id'];
        }, $question['firstSet']);

        $labelIds = array_map(function (array $label) {
            return $label['id'];
        }, $question['secondSet']);

        $maxScore = -1;
        foreach ($question['solutions'] as $index => $solution) {
            if (!in_array($solution['firstId'], $proposalIds)) {
                $errors[] = [
                    'path' => "/solutions[{$index}]",
                    'message' => "id {$solution['firstId']} doesn't match any proposal id",
                ];
            }

            if (!in_array($solution['secondId'], $labelIds)) {
                $errors[] = [
                    'path' => "/solutions[{$index}]",
                    'message' => "id {$solution['secondId']} doesn't match any label id",
                ];
            }

            if ($solution['score'] > $maxScore) {
                $maxScore = $solution['score'];
            }
        }

        // Checks there is a positive score solution
        if ($maxScore <= 0) {
            $errors[] = [
                'path' => '/solutions',
                'message' => 'There is no solution with a positive score',
            ];
        }

        return $errors;
    }
413057
protected function validateSolutions(array $question)
    {
        $errors = [];

        $itemIds = [];
        // there is not 2 items at the same coords
        $usedCoords = [];
        foreach ($question['items'] as $index => $item) {
            $itemIds[] = $item['id'];
            if (!empty($item['coordinates'])) {
                if (in_array($item['coordinates'], $usedCoords)) {
                    $errors[] = [
                        'path' => "/items[{$index}]",
                        'message' => 'two items cannot be pinned at the same coordinates.',
                    ];
                    break;
                }

                // max Y coordinate < rows
                if ($question['rows'] <= $item['coordinates'][1]) {
                    $errors[] = [
                        'path' => "/items[{$index}]",
                        'message' => 'pinned items must be in the grid.',
                    ];
                    break;
                }

                $usedCoords[] = $item['coordinates'];
            }
        }

        if (empty($errors)) {
            // no shuffle if no item pinned
            if (empty($usedCoords) && $question['random']) {
                $errors[] = [
                    'path' => '/random',
                    'message' => 'you must pin at least one item to use random.',
                ];
            }

            // solutions references
            foreach ($question['solutions'] as $indexSolution => $solution) {
                foreach ($solution['itemIds'] as $index => $item) {
                    if (!in_array($item, $itemIds)) {
                        $errors[] = [
                            'path' => "solutions[{$indexSolution}]/itemIds[{$index}]",
                            'message' => 'solution itemIds must reference question items.',
                        ];
                    }
                }
            }
        }

        return $errors;
    }
286265
public function process_common($quest) {
        $question = $this->defaultquestion();
        $text = $quest->QUESTION_BLOCK->text;
        $questiontext = $this->cleaned_text_field($text);
        $question->questiontext = $questiontext['text'];
        $question->questiontextformat = $questiontext['format']; // Needed because add_blank_combined_feedback uses it.
        if (isset($questiontext['itemid'])) {
            $question->questiontextitemid = $questiontext['itemid'];
        }
        $question->name = $this->create_default_question_name($question->questiontext,
                get_string('defaultname', 'qformat_blackboard_six' , $quest->id));
        $question->generalfeedback = '';
        $question->generalfeedbackformat = FORMAT_HTML;
        $question->generalfeedbackfiles = array();

        return $question;
    }
217633
protected function use_question($questionid) {
        if (isset($this->recentlyusedquestions[$questionid])) {
            $this->recentlyusedquestions[$questionid] += 1;
        } else {
            $this->recentlyusedquestions[$questionid] = 1;
        }

        foreach ($this->availablequestionscache as $categorykey => $questionsforcategory) {
            foreach ($questionsforcategory as $numuses => $questionids) {
                if (!isset($questionids[$questionid])) {
                    continue;
                }
                unset($this->availablequestionscache[$categorykey][$numuses][$questionid]);
                if (empty($this->availablequestionscache[$categorykey][$numuses])) {
                    unset($this->availablequestionscache[$categorykey][$numuses]);
                }
            }
        }
    }
17944
protected function render_question_text($question) {
        global $OUTPUT;

        $text = question_rewrite_question_preview_urls($question->questiontext, $question->id,
                $question->contextid, 'question', 'questiontext', $question->id,
                $this->context->id, 'quiz_statistics');

        return $OUTPUT->box(format_text($text, $question->questiontextformat,
                array('noclean' => true, 'para' => false, 'overflowdiv' => true)),
                'questiontext boxaligncenter generalbox boxwidthnormal mdl-align');
    }
634691
protected function getDefaultAttributes($question)
    {
        return [
            'discussion_id' => $question->discussion_id,
            'question'      => $question->question,
            'isEnded'       => $question->isEnded(),
            'endDate'       => $question->end_date.' UTC',
            'isPublic'      => (bool) $question->public_poll,
        ];
    }
522714
protected function get_regraded_questions() {
        global $DB;

        $qubaids = $this->get_qubaids_condition();
        $regradedqs = $DB->get_records_select('quiz_overview_regrades',
                'questionusageid ' . $qubaids->usage_id_in(), $qubaids->usage_id_in_params());
        return quiz_report_index_by_keys($regradedqs, array('questionusageid', 'slot'));
    }
614620
public function import_match($question) {
        // Get common parts.
        $qo = $this->import_headers($question);

        // Header parts particular to matching.
        $qo->qtype = 'match';
        $qo->shuffleanswers = $this->trans_single($this->getpath($question,
                array('#', 'shuffleanswers', 0, '#'), 1));

        // Run through subquestions.
        $qo->subquestions = array();
        $qo->subanswers = array();
        foreach ($question['#']['subquestion'] as $subqxml) {
            $qo->subquestions[] = $this->import_text_with_files($subqxml,
                    array(), '', $this->get_format($qo->questiontextformat));

            $answers = $this->getpath($subqxml, array('#', 'answer'), array());
            $qo->subanswers[] = $this->getpath($subqxml,
                    array('#', 'answer', 0, '#', 'text', 0, '#'), '', true);
        }

        $this->import_combined_feedback($qo, $question, true);
        $this->import_hints($qo, $question, true, false, $this->get_format($qo->questiontextformat));

        return $qo;
    }
289169
public function load_and_initialise_questions_for_calculations($quiz) {
        // Load the questions.
        $questions = quiz_report_get_significant_questions($quiz);
        $questionids = array();
        foreach ($questions as $question) {
            $questionids[] = $question->id;
        }
        $fullquestions = question_load_questions($questionids);
        foreach ($questions as $qno => $question) {
            $q = $fullquestions[$question->id];
            $q->maxmark = $question->maxmark;
            $q->slot = $qno;
            $q->number = $question->number;
            $questions[$qno] = $q;
        }
        return $questions;
    }
696330
public function search($query) {
        global $CFG;
        if ($result = parent::search($query)) {
            return $result;
        }

        $found = false;
        require_once($CFG->dirroot . '/question/engine/bank.php');
        foreach (question_bank::get_all_qtypes() as $qtype) {
            if (strpos(core_text::strtolower($qtype->local_name()), $query) !== false) {
                $found = true;
                break;
            }
        }
        if ($found) {
            $result = new stdClass();
            $result->page     = $this;
            $result->settings = array();
            return array($this->name => $result);
        } else {
            return array();
        }
    }