dirroot . '/mod/quiz/locallib.php');
define('NUM_QS_TO_SHOW_IN_RANDOM', 3);
/**
* Remove a question from a quiz
* @param object $quiz the quiz object.
* @param int $questionid The id of the question to be deleted.
*/
function quiz_remove_question($quiz, $questionid) {
global $DB;
$questionids = explode(',', $quiz->questions);
$key = array_search($questionid, $questionids);
if ($key === false) {
return;
}
unset($questionids[$key]);
$quiz->questions = implode(',', $questionids);
$DB->set_field('quiz', 'questions', $quiz->questions, array('id' => $quiz->id));
$DB->delete_records('quiz_question_instances', array('quiz' => $quiz->instance, 'question' => $questionid));
}
/**
* Remove an empty page from the quiz layout. If that is not possible, do nothing.
* @param string $layout the existinng layout, $quiz->questions.
* @param integer $index the position into $layout where the empty page should be removed.
* @return the updated layout
*/
function quiz_delete_empty_page($layout, $index) {
$questionids = explode(',', $layout);
if ($index < -1 || $index >= count($questionids) - 1) {
return $layout;
}
if (($index >= 0 && $questionids[$index] != 0) || $questionids[$index + 1] != 0) {
return $layout; // This was not an empty page.
}
unset($questionids[$index + 1]);
return implode(',', $questionids);
}
/**
* Add a question to a quiz
*
* Adds a question to a quiz by updating $quiz as well as the
* quiz and quiz_question_instances tables. It also adds a page break
* if required.
* @param int $id The id of the question to be added
* @param object $quiz The extended quiz object as used by edit.php
* This is updated by this function
* @param int $page Which page in quiz to add the question on. If 0 (default), add at the end
* @return boolean false if the question was already in the quiz
*/
function quiz_add_quiz_question($id, $quiz, $page = 0) {
global $DB;
$questions = explode(',', quiz_clean_layout($quiz->questions));
if (in_array($id, $questions)) {
return false;
}
// remove ending page break if it is not needed
if ($breaks = array_keys($questions, 0)) {
// determine location of the last two page breaks
$end = end($breaks);
$last = prev($breaks);
$last = $last ? $last : -1;
if (!$quiz->questionsperpage || (($end - $last - 1) < $quiz->questionsperpage)) {
array_pop($questions);
}
}
if (is_int($page) && $page >= 1) {
$numofpages = quiz_number_of_pages($quiz->questions);
if ($numofpages<$page) {
//the page specified does not exist in quiz
$page = 0;
} else {
// add ending page break - the following logic requires doing
//this at this point
$questions[] = 0;
$currentpage = 1;
$addnow = false;
foreach ($questions as $question) {
if ($question == 0) {
$currentpage++;
//The current page is the one after the one we want to add on,
//so we add the question before adding the current page.
if ($currentpage == $page + 1) {
$questions_new[] = $id;
}
}
$questions_new[] = $question;
}
$questions = $questions_new;
}
}
if ($page == 0) {
// add question
$questions[] = $id;
// add ending page break
$questions[] = 0;
}
// Save new questionslist in database
$quiz->questions = implode(',', $questions);
$DB->set_field('quiz', 'questions', $quiz->questions, array('id' => $quiz->id));
// update question grades
$quiz->grades[$id] = $DB->get_field('question', 'defaultgrade', array('id' => $id));
quiz_update_question_instance($quiz->grades[$id], $id, $quiz->instance);
return true;
}
function quiz_add_random_questions($quiz, $addonpage, $categoryid, $number, $includesubcategories) {
global $DB, $QTYPES;
$category = $DB->get_record('question_categories', array('id' => $categoryid));
if (!$category) {
print_error('invalidcategoryid', 'error');
}
$catcontext = get_context_instance_by_id($category->contextid);
require_capability('moodle/question:useall', $catcontext);
// Find existing random questions in this category that are
// not used by any quiz.
if ($existingquestions = $DB->get_records_sql(
"SELECT q.id,q.qtype FROM {question} q
WHERE qtype = '" . RANDOM . "'
AND category = ?
AND " . $DB->sql_compare_text('questiontext') . " = ?
AND NOT EXISTS (SELECT * FROM {quiz_question_instances} WHERE question = q.id)
ORDER BY id", array($category->id, $includesubcategories))) {
// Take as many of these as needed.
while (($existingquestion = array_shift($existingquestions)) && $number > 0) {
quiz_add_quiz_question($existingquestion->id, $quiz, $addonpage);
$number -= 1;
}
}
if ($number <= 0) {
return;
}
// More random questions are needed, create them.
$form->questiontext = array('text' => $includesubcategories, 'format' => 0);
$form->defaultgrade = 1;
$form->hidden = 1;
for ($i = 0; $i < $number; $i += 1) {
$form->category = $category->id . ',' . $category->contextid;
$form->stamp = make_unique_id_code(); // Set the unique code (not to be changed)
$question = new stdClass;
$question->qtype = RANDOM;
$question = $QTYPES[RANDOM]->save_question($question, $form);
if (!isset($question->id)) {
print_error('cannotinsertrandomquestion', 'quiz');
}
quiz_add_quiz_question($question->id, $quiz, $addonpage);
}
}
/**
* Add a page break after at particular position$.
* @param string $layout the existinng layout, $quiz->questions.
* @param integer $index the position into $layout where the empty page should be removed.
* @return the updated layout
*/
function quiz_add_page_break_at($layout, $index) {
$questionids = explode(',', $layout);
if ($index < 0 || $index >= count($questionids)) {
return $layout;
}
array_splice($questionids, $index, 0, '0');
return implode(',', $questionids);
}
/**
* Add a page break after a particular question.
* @param string $layout the existinng layout, $quiz->questions.
* @param integer $qustionid the question to add the page break after.
* @return the updated layout
*/
function quiz_add_page_break_after($layout, $questionid) {
$questionids = explode(',', $layout);
$key = array_search($questionid, $questionids);
if ($key === false || !$questionid) {
return $layout;
}
array_splice($questionids, $key + 1, 0, '0');
return implode(',', $questionids);
}
/**
* Update the database after $quiz->questions has been changed. For example,
* this deletes preview attempts and updates $quiz->sumgrades.
* @param $quiz the quiz object.
*/
function quiz_save_new_layout($quiz) {
global $DB;
$DB->set_field('quiz', 'questions', $quiz->questions, array('id' => $quiz->id));
quiz_update_sumgrades($quiz);
quiz_delete_previews($quiz);
}
/**
* Save changes to question instance
*
* Saves changes to the question grades in the quiz_question_instances table.
* It does not update 'sumgrades' in the quiz table.
* @return boolean Indicates success or failure.
* @param integer grade The maximal grade for the question
* @param integer $questionid The id of the question
* @param integer $quizid The id of the quiz to update / add the instances for.
*/
function quiz_update_question_instance($grade, $questionid, $quizid) {
global $DB;
if ($instance = $DB->get_record('quiz_question_instances', array('quiz' => $quizid, 'question' => $questionid))) {
$instance->grade = $grade;
return $DB->update_record('quiz_question_instances', $instance);
} else {
unset($instance);
$instance->quiz = $quizid;
$instance->question = $questionid;
$instance->grade = $grade;
return $DB->insert_record('quiz_question_instances', $instance);
}
}
// Private function used by the following two.
function _quiz_move_question($layout, $questionid, $shift) {
if (!$questionid || !($shift == 1 || $shift == -1)) {
return $layout;
}
$questionids = explode(',', $layout);
$key = array_search($questionid, $questionids);
if ($key === false) {
return $layout;
}
$otherkey = $key + $shift;
if ($otherkey < 0 || $otherkey >= count($questionids) - 1) {
return $layout;
}
$temp = $questionids[$otherkey];
$questionids[$otherkey] = $questionids[$key];
$questionids[$key] = $temp;
return implode(',', $questionids);
}
/**
* Move a particular question one space earlier in the $quiz->questions list.
* If that is not possible, do nothing.
* @param string $layout the existinng layout, $quiz->questions.
* @param integer $questionid the id of a question.
* @return the updated layout
*/
function quiz_move_question_up($layout, $questionid) {
return _quiz_move_question($layout, $questionid, -1);
}
/**
* Move a particular question one space later in the $quiz->questions list.
* If that is not possible, do nothing.
* @param string $layout the existinng layout, $quiz->questions.
* @param integer $questionid the id of a question.
* @return the updated layout
*/
function quiz_move_question_down($layout, $questionid) {
return _quiz_move_question($layout, $questionid, +1);
}
/**
* Prints a list of quiz questions for the edit.php main view for edit
* ($reordertool = false) and order and paging ($reordertool = true) tabs
*
* @return int sum of maximum grades
* @param object $quiz This is not the standard quiz object used elsewhere but
* it contains the quiz layout in $quiz->questions and the grades in
* $quiz->grades
* @param object $pageurl The url of the current page with the parameters required
* for links returning to the current page, as a moodle_url object
* @param boolean $allowdelete Indicates whether the delete icons should be displayed
* @param boolean $reordertool Indicates whether the reorder tool should be displayed
* @param boolean $quiz_qbanktool Indicates whether the question bank should be displayed
* @param boolean $hasattempts Indicates whether the quiz has attempts
*/
function quiz_print_question_list($quiz, $pageurl, $allowdelete, $reordertool,
$quiz_qbanktool, $hasattempts, $defaultcategoryobj) {
global $USER, $CFG, $QTYPES, $DB, $OUTPUT;
$strorder = get_string('order');
$strquestionname = get_string('questionname', 'quiz');
$strgrade = get_string('grade');
$strremove = get_string('remove', 'quiz');
$stredit = get_string('edit');
$strview = get_string('view');
$straction = get_string('action');
$strmove = get_string('move');
$strmoveup = get_string('moveup');
$strmovedown = get_string('movedown');
$strsave = get_string('save', 'quiz');
$strreorderquestions = get_string('reorderquestions', 'quiz');
$strselectall = get_string('selectall', 'quiz');
$strselectnone = get_string('selectnone', 'quiz');
$strtype = get_string('type', 'quiz');
$strpreview = get_string('preview', 'quiz');
if ($quiz->questions) {
list($usql, $params) = $DB->get_in_or_equal(explode(',', $quiz->questions));
$questions = $DB->get_records_sql("SELECT q.*,c.contextid
FROM {question} q,
{question_categories} c
WHERE q.id $usql
AND q.category = c.id", $params);
} else {
$questions = array();
}
$layout = quiz_clean_layout($quiz->questions);
$order = explode(',', $layout);
$lastindex = count($order) - 1;
$disabled = '';
$pagingdisabled = '';
if ($hasattempts) {
$disabled = 'disabled="disabled"';
}
if ($hasattempts || $quiz->shufflequestions) {
$pagingdisabled = 'disabled="disabled"';
}
$reordercontrolssetdefaultsubmit = '
";
if ($reordertool) {
echo '';
}
return $sumgrade;
}
/**
* Print all the controls for adding questions directly into the
* specific page in the edit tab of edit.php
*
* @param unknown_type $quiz
* @param unknown_type $pageurl
* @param unknown_type $page
* @param unknown_type $hasattempts
*/
function quiz_print_pagecontrols($quiz, $pageurl, $page, $hasattempts, $defaultcategoryobj) {
global $CFG, $OUTPUT;
static $randombuttoncount = 0;
$randombuttoncount++;
echo '
';
// Get the current context
$thiscontext = get_context_instance(CONTEXT_COURSE, $quiz->course);
$contexts = new question_edit_contexts($thiscontext);
// Get the default category.
list($defaultcategoryid) = explode(',', $pageurl->param('cat'));
if (empty($defaultcategoryid)) {
$defaultcategoryid = $defaultcategoryobj->id;
}
// Create the url the question page will return to
$returnurladdtoquiz = new moodle_url($pageurl, array('addonpage' => $page));
// Print a button linking to the choose question type page.
$returnurladdtoquiz = str_replace($CFG->wwwroot, '', $returnurladdtoquiz->out(false));
$newquestionparams = array('returnurl' => $returnurladdtoquiz,
'cmid' => $quiz->cmid, 'appendqnumstring' => 'addquestion');
create_new_question_button($defaultcategoryid, $newquestionparams, get_string('addaquestion', 'quiz'),
get_string('createquestionandadd', 'quiz'), $hasattempts);
if ($hasattempts) {
$disabled = 'disabled="disabled"';
} else {
$disabled = '';
}
?>
/>
help_icon('addarandomquestion', 'quiz'); ?>
";
}
/**
* Print a given single question in quiz for the edit tab of edit.php.
* Meant to be used from quiz_print_question_list()
*
* @param object $question A question object from the database questions table
* @param object $returnurl The url to get back to this page, for example after editing.
* @param object $quiz The quiz in the context of which the question is being displayed
*/
function quiz_print_singlequestion($question, $returnurl, $quiz) {
global $QTYPES;
echo '
\n";
}
/**
* Print a given random question in quiz for the edit tab of edit.php.
* Meant to be used from quiz_print_question_list()
*
* @param object $question A question object from the database questions table
* @param object $questionurl The url of the question editing page as a moodle_url object
* @param object $quiz The quiz in the context of which the question is being displayed
* @param boolean $quiz_qbanktool Indicate to this function if the question bank window open
*/
function quiz_print_randomquestion(&$question, &$pageurl, &$quiz, $quiz_qbanktool) {
global $DB, $QTYPES, $OUTPUT;
echo '
';
if (!$category = $DB->get_record('question_categories', array('id' => $question->category))) {
echo $OUTPUT->notification('Random question category not found!');
return;
}
echo '
';
if ($questioncount == 0) {
// No questions in category, give an error plus instructions
echo '';
print_string('noquestionsnotinuse', 'quiz');
echo '';
echo ' ';
// Embed the link into the string with instructions
$a = new stdClass;
$a->catname = '' . $category->name . '';
$a->link = $linkcategorycontents;
echo get_string('addnewquestionsqbank', 'quiz', $a);
} else {
// Category has questions
// Get a sample from the database,
$toshow = array_slice($questionids, 0, NUM_QS_TO_SHOW_IN_RANDOM);
$questionidstoshow = array();
foreach ($toshow as $a) {
$questionidstoshow[] = $a->id;
}
$questionstoshow = $DB->get_records_list('question', 'id', $questionidstoshow,
'', 'id,qtype,name,questiontext,questiontextformat');
// list them,
echo '
';
foreach ($questionstoshow as $question) {
echo '
';
}
/**
* Print a given single question in quiz for the reordertool tab of edit.php.
* Meant to be used from quiz_print_question_list()
*
* @param object $question A question object from the database questions table
* @param object $questionurl The url of the question editing page as a moodle_url object
* @param object $quiz The quiz in the context of which the question is being displayed
*/
function quiz_print_singlequestion_reordertool($question, $returnurl, $quiz) {
echo '
\n";
}
/**
* Print a given random question in quiz for the reordertool tab of edit.php.
* Meant to be used from quiz_print_question_list()
*
* @param object $question A question object from the database questions table
* @param object $questionurl The url of the question editing page as a moodle_url object
* @param object $quiz The quiz in the context of which the question is being displayed
*/
function quiz_print_randomquestion_reordertool(&$question, &$pageurl, &$quiz) {
global $DB, $QTYPES, $OUTPUT;
// Load the category, and the number of available questions in it.
if (!$category = $DB->get_record('question_categories', array('id' => $question->category))) {
echo $OUTPUT->notification('Random question category not found!');
return;
}
$questioncount = count($QTYPES['random']->get_usable_questions_from_category(
$category->id, $question->questiontext == '1', '0'));
$reordercheckboxlabel = '';
echo '
';
}
/**
* Print an icon to indicate the 'include subcategories' state of a random question.
* @param $question the random question.
*/
function print_random_option_icon($question) {
global $OUTPUT;
if (!empty($question->questiontext)) {
$icon = 'withsubcat';
$tooltip = get_string('randomwithsubcat', 'quiz');
} else {
$icon = 'nosubcat';
$tooltip = get_string('randomnosubcat', 'quiz');
}
echo '';
}
/**
* Creates a textual representation of a question for display.
*
* @param object $question A question object from the database questions table
* @param boolean $showicon If true, show the question's icon with the question. False by default.
* @param boolean $showquestiontext If true (default), show question text after question name.
* If false, show only question name.
* @param boolean $return If true (default), return the output. If false, print it.
*/
function quiz_question_tostring(&$question, $showicon = false, $showquestiontext = true, $return = true) {
global $COURSE;
$result = '';
$result .= '';
if ($showicon) {
$result .= print_question_icon($question, true);
echo " ";
}
$result .= shorten_text(format_string($question->name), 200) . '';
if ($showquestiontext) {
$formatoptions = new stdClass;
$formatoptions->noclean = true;
$formatoptions->para = false;
$questiontext = strip_tags(format_text($question->questiontext,
$question->questiontextformat,
$formatoptions, $COURSE->id));
$questiontext = shorten_text($questiontext, 200);
$result .= '';
if (!empty($questiontext)) {
$result .= $questiontext;
} else {
$result .= '';
$result .= get_string('questiontextisempty', 'quiz');
$result .= '';
}
$result .= '';
}
if ($return) {
return $result;
} else {
echo $result;
}
}
/**
* A column type for the add this question to the quiz.
*/
class question_bank_add_to_quiz_action_column extends question_bank_action_column_base {
protected $stradd;
public function init() {
parent::init();
$this->stradd = get_string('addtoquiz', 'quiz');
}
public function get_name() {
return 'addtoquizaction';
}
protected function display_content($question, $rowclasses) {
// for RTL languages: switch right and left arrows
if (right_to_left()) {
$movearrow = 't/removeright';
} else {
$movearrow = 't/moveleft';
}
$this->print_icon($movearrow, $this->stradd, $this->qbank->add_to_quiz_url($question->id));
}
public function get_required_fields() {
return array('q.id');
}
}
/**
* A column type for the name followed by the start of the question text.
*/
class question_bank_question_name_text_column extends question_bank_question_name_column {
public function get_name() {
return 'questionnametext';
}
protected function display_content($question, $rowclasses) {
echo '
';
}
public function get_required_fields() {
$fields = parent::get_required_fields();
$fields[] = 'q.questiontext';
$fields[] = 'q.questiontextformat';
return $fields;
}
}
/**
* Subclass to customise the view of the question bank for the quiz editing screen.
*/
class quiz_question_bank_view extends question_bank_view {
protected $quizhasattempts = false;
protected function known_field_types() {
$types = parent::known_field_types();
$types[] = new question_bank_add_to_quiz_action_column($this);
$types[] = new question_bank_question_name_text_column($this);
return $types;
}
protected function wanted_columns() {
return array('addtoquizaction', 'checkbox', 'qtype', 'questionnametext', 'editaction', 'previewaction');
}
/**
* Let the question bank display know whether the quiz has been attempted,
* hence whether some bits of UI, like the add this question to the quiz icon,
* should be displayed.
* @param boolean $quizhasattempts whether the quiz has attempts.
*/
public function set_quiz_has_attempts($quizhasattempts) {
$this->quizhasattempts = $quizhasattempts;
if ($quizhasattempts && isset($this->visiblecolumns['addtoquizaction'])) {
unset($this->visiblecolumns['addtoquizaction']);
}
}
public function preview_question_url($questionid) {
global $CFG;
return $CFG->wwwroot . '/question/preview.php?id=' . $questionid . $this->quizorcourseid;
}
public function add_to_quiz_url($questionid) {
global $CFG;
$params = $this->baseurl->params();
$params['addquestion'] = $questionid;
$params['sesskey'] = sesskey();
return new moodle_url('/mod/quiz/edit.php', $params);
}
public function display($tabname, $page, $perpage, $sortorder,
$sortorderdecoded, $cat, $recurse, $showhidden, $showquestiontext) {
global $OUTPUT;
if ($this->process_actions_needing_ui()) {
return;
}
// Display the current category.
if (!$category = $this->get_current_category($cat)) {
return;
}
$this->print_category_info($category);
echo $OUTPUT->box_start('generalbox questionbank');
$this->display_category_form($this->contexts->having_one_edit_tab_cap($tabname),
$this->baseurl, $cat);
// continues with list of questions
$this->display_question_list($this->contexts->having_one_edit_tab_cap($tabname), $this->baseurl, $cat, $this->cm,
$recurse, $page, $perpage, $showhidden, $sortorder, $sortorderdecoded, $showquestiontext,
$this->contexts->having_cap('moodle/question:add'));
$this->display_options($recurse, $showhidden, $showquestiontext);
echo $OUTPUT->box_end();
}
protected function print_choose_category_message($categoryandcontext) {
global $OUTPUT;
echo $OUTPUT->box_start('generalbox questionbank');
$this->display_category_form($this->contexts->having_one_edit_tab_cap('edit'), $this->baseurl, $categoryandcontext);
echo "
';
}
}
/**
* Prints the form for setting a quiz' overall grade
*
* @param object $quiz The quiz object of the quiz in question
* @param object $pageurl The url of the current page with the parameters required
* for links returning to the current page, as a moodle_url object
* @param integer $tabindex The tabindex to start from for the form elements created
* @return integer The tabindex from which the calling page can continue, that is,
* the last value used +1.
*/
function quiz_print_grading_form($quiz, $pageurl, $tabindex) {
global $USER, $OUTPUT;
$strsave = get_string('save', 'quiz');
echo "
";
echo '';
echo "
\n";
return $tabindex + 1;
}
/**
* Print the status bar
*
* @param object $quiz The quiz object of the quiz in question
*/
function quiz_print_status_bar($quiz) {
global $CFG;
$numberofquestions = quiz_number_of_questions_in_quiz($quiz->questions);
?>