Skip to content

Commit

Permalink
question bank: MDL-12787, MDL-17870 field in the question editing for…
Browse files Browse the repository at this point in the history
…m to tag questions, and load and save the tags from the DB.
  • Loading branch information
tjhunt committed Jan 16, 2009
1 parent 8942fd7 commit c599a68
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
16 changes: 11 additions & 5 deletions lib/questionlib.php
Expand Up @@ -836,10 +836,11 @@ function question_load_questions($questionids, $extrafields = '', $join = '') {
* Private function to factor common code out of get_question_options().
*
* @param object $question the question to tidy.
* @param boolean $loadtags load the question tags from the tags table. Optional, default false.
* @return boolean true if successful, else false.
*/
function _tidy_question(&$question) {
global $QTYPES;
function _tidy_question(&$question, $loadtags = false) {
global $CFG, $QTYPES;
if (!array_key_exists($question->qtype, $QTYPES)) {
$question->qtype = 'missingtype';
$question->questiontext = '<p>' . get_string('warningmissingtype', 'quiz') . '</p>' . $question->questiontext;
Expand All @@ -850,6 +851,10 @@ function _tidy_question(&$question) {
unset($question->_partiallyloaded);
}
}
if ($loadtags && !empty($CFG->usetags)) {
require_once($CFG->dirroot . '/tag/lib.php');
$question->tags = tag_get_tags_array('question', $question->id);
}
return $success;
}

Expand All @@ -862,18 +867,19 @@ function _tidy_question(&$question) {
*
* @param mixed $questions Either an array of question objects to be updated
* or just a single question object
* @param boolean $loadtags load the question tags from the tags table. Optional, default false.
* @return bool Indicates success or failure.
*/
function get_question_options(&$questions) {
function get_question_options(&$questions, $loadtags = false) {
if (is_array($questions)) { // deal with an array of questions
foreach ($questions as $i => $notused) {
if (!_tidy_question($questions[$i])) {
if (!_tidy_question($questions[$i], $loadtags)) {
return false;
}
}
return true;
} else { // deal with single question
return _tidy_question($questions);
return _tidy_question($questions, $loadtags);
}
}

Expand Down
18 changes: 8 additions & 10 deletions question/question.php
Expand Up @@ -46,18 +46,15 @@
}
$contexts = new question_edit_contexts($thiscontext);


if (!$returnurl) {
$returnurl = "{$CFG->wwwroot}/question/edit.php?courseid={$COURSE->id}";
}



if ($id) {
if (!$question = $DB->get_record('question', array('id' => $id))) {
print_error('questiondoesnotexist', 'question', $returnurl);
}
get_question_options($question);
get_question_options($question, true);
} else if ($categoryid && $qtype) { // only for creating new questions
$question = new stdClass;
$question->category = $categoryid;
Expand All @@ -72,7 +69,7 @@
}

//permissions
$question->formoptions = new object();
$question->formoptions = new stdClass;

$categorycontext = get_context_instance_by_id($category->contextid);
$addpermission = has_capability('moodle/question:add', $categorycontext);
Expand All @@ -99,22 +96,19 @@
}
}


} else { // creating a new question
require_capability('moodle/question:add', $categorycontext);
$formeditable = true;
$question->formoptions->repeatelements = true;
$question->formoptions->movecontext = false;
}


// Validate the question type.
if (!isset($QTYPES[$question->qtype])) {
print_error('unknownquestiontype', 'question', $returnurl, $question->qtype);
}
$CFG->pagepath = 'question/type/' . $question->qtype;


// Create the question editing form.
if ($wizardnow!=='' && !$movecontext){
if (!method_exists($QTYPES[$question->qtype], 'next_wizard_form')){
Expand Down Expand Up @@ -183,7 +177,12 @@
}
}

$question = $QTYPES[$question->qtype]->save_question($question, $fromform, $COURSE, $wizardnow);
$question = $QTYPES[$question->qtype]->save_question($question, $fromform, $COURSE, $wizardnow, true);
if (!empty($CFG->usetags)) {
require_once($CFG->dirroot.'/tag/lib.php');
tag_set('question', $question->id, $fromform->tags);
}

if (($QTYPES[$question->qtype]->finished_edit_wizard($fromform)) || $movecontext){
if ($inpopup) {
notify(get_string('changessaved'), '');
Expand Down Expand Up @@ -242,7 +241,6 @@
print_header_simple($streditingquestion, '', $navigation);
}


// Display a heading, question editing form and possibly some extra content needed for
// for this question type.
$QTYPES[$question->qtype]->display_question_editing_page($mform, $question, $wizardnow);
Expand Down
5 changes: 5 additions & 0 deletions question/type/edit_question_form.php
Expand Up @@ -146,6 +146,11 @@ function definition() {
// Any questiontype specific fields.
$this->definition_inner($mform);

if (!empty($CFG->usetags)) {
$mform->addElement('header', 'tagsheader', get_string('tags'));
$mform->addElement('tags', 'tags', get_string('tags'));
}

if (!empty($this->question->id)){
$mform->addElement('header', 'createdmodifiedheader', get_string('createdmodifiedheader', 'question'));
$a = new object();
Expand Down

0 comments on commit c599a68

Please sign in to comment.