Skip to content

Commit

Permalink
MDL-61614 Quiz: save slot's tags in a table when adding random question
Browse files Browse the repository at this point in the history
  • Loading branch information
rezaies committed Apr 18, 2018
1 parent 28e392a commit d62793f
Show file tree
Hide file tree
Showing 4 changed files with 461 additions and 16 deletions.
46 changes: 43 additions & 3 deletions mod/quiz/classes/local/structure/slot_random.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ class slot_random {
*/
protected $quiz = null;

/**
* @var \core_tag_tag[] List of tags for this slot.
*/
protected $tags = [];

/**
* slot_random constructor.
*
Expand All @@ -52,9 +57,8 @@ public function __construct($slotrecord = null) {
$this->record = new \stdClass();

$properties = array(
'id', 'slot', 'quizid', 'page', 'requireprevious',
'questionid', 'questioncategoryid', 'includingsubcategories',
'tags', 'maxmark');
'id', 'slot', 'quizid', 'page', 'requireprevious', 'questionid',
'questioncategoryid', 'includingsubcategories', 'maxmark');

foreach ($properties as $property) {
if (isset($slotrecord->$property)) {
Expand Down Expand Up @@ -95,6 +99,29 @@ public function set_quiz($quiz) {
$this->record->quizid = $quiz->id;
}

/**
* Set some tags for this quiz slot.
*
* @param \core_tag_tag[] $tags
*/
public function set_tags($tags) {
$this->tags = [];
foreach ($tags as $tag) {
// We use $tag->id as the key for the array so not only it handles duplicates of the same tag being given,
// but also it is consistent with the behaviour of set_tags_by_id() below.
$this->tags[$tag->id] = $tag;
}
}

/**
* Set some tags for this quiz slot. This function uses tag ids to find tags.
*
* @param int[] $tagids
*/
public function set_tags_by_id($tagids) {
$this->tags = \core_tag_tag::get_bulk($tagids, 'id, name');
}

/**
* Inserts the quiz slot at the $page page.
* It is required to call this function if you are building a quiz slot object from scratch.
Expand Down Expand Up @@ -151,6 +178,19 @@ public function insert($page) {
}

$this->record->id = $DB->insert_record('quiz_slots', $this->record);

if (!empty($this->tags)) {
$recordstoinsert = [];
foreach ($this->tags as $tag) {
$recordstoinsert[] = (object)[
'slotid' => $this->record->id,
'tagid' => $tag->id,
'tagname' => $tag->name
];
}
$DB->insert_records('quiz_slot_tags', $recordstoinsert);
}

$trans->allow_commit();
}
}
16 changes: 4 additions & 12 deletions mod/quiz/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2189,18 +2189,10 @@ function quiz_add_random_questions($quiz, $addonpage, $categoryid, $number,
$catcontext = context::instance_by_id($category->contextid);
require_capability('moodle/question:useall', $catcontext);

$tags = [];
$tags = \core_tag_tag::get_bulk($tagids, 'id, name');
$tagstrings = [];
foreach ($tagids as $tagid) {
if ($tag = core_tag_tag::get($tagid, 'id,name')) {
$tags[] = [
'id' => $tagid,
'name' => $tag->name
];
$tagstrings[] = "{$tagid},{$tag->name}";
} else if (!empty($tagid)) {
print_error('invalidtagid', 'mod_quiz');
}
foreach ($tags as $tag) {
$tagstrings[] = "{$tag->id},{$tag->name}";
}

// Find existing random questions in this category that are
Expand Down Expand Up @@ -2239,11 +2231,11 @@ function quiz_add_random_questions($quiz, $addonpage, $categoryid, $number,
$randomslotdata->questionid = $question->id;
$randomslotdata->questioncategoryid = $categoryid;
$randomslotdata->includingsubcategories = $includesubcategories ? 1 : 0;
$randomslotdata->tags = json_encode($tags);
$randomslotdata->maxmark = 1;

$randomslot = new \mod_quiz\local\structure\slot_random($randomslotdata);
$randomslot->set_quiz($quiz);
$randomslot->set_tags($tags);
$randomslot->insert($addonpage);
}
}
Expand Down
Loading

0 comments on commit d62793f

Please sign in to comment.