Línea 139... |
Línea 139... |
139 |
*/
|
139 |
*/
|
140 |
public function setCategory($category) {
|
140 |
public function setCategory($category) {
|
141 |
if (count($this->questions)) {
|
141 |
if (count($this->questions)) {
|
142 |
debugging('You shouldn\'t call setCategory after setQuestions');
|
142 |
debugging('You shouldn\'t call setCategory after setQuestions');
|
143 |
}
|
143 |
}
|
- |
|
144 |
$context = context::instance_by_id($category->contextid);
|
- |
|
145 |
if ($context->contextlevel !== CONTEXT_MODULE) {
|
- |
|
146 |
throw new \core\exception\moodle_exception('Invalid contextlevel for target category, must be CONTEXT_MODULE');
|
- |
|
147 |
}
|
144 |
$this->category = $category;
|
148 |
$this->category = $category;
|
145 |
$this->importcontext = context::instance_by_id($this->category->contextid);
|
149 |
$this->importcontext = $context;
|
146 |
}
|
150 |
}
|
Línea 147... |
Línea 151... |
147 |
|
151 |
|
148 |
/**
|
152 |
/**
|
149 |
* Set the specific questions to export. Should not include questions with
|
153 |
* Set the specific questions to export. Should not include questions with
|
Línea 310... |
Línea 314... |
310 |
|
314 |
|
311 |
// loop through installed questiontypes checking for
|
315 |
// loop through installed questiontypes checking for
|
312 |
// function to handle this question
|
316 |
// function to handle this question
|
313 |
foreach (question_bank::get_all_qtypes() as $qtype) {
|
317 |
foreach (question_bank::get_all_qtypes() as $qtype) {
|
314 |
if (method_exists($qtype, $methodname)) {
|
318 |
if (method_exists($qtype, $methodname)) {
|
315 |
if ($question = $qtype->$methodname($data, $question, $this, $extra)) {
|
319 |
if ($importedquestion = $qtype->$methodname($data, $question, $this, $extra)) {
|
316 |
return $question;
|
320 |
return $importedquestion;
|
317 |
}
|
321 |
}
|
318 |
}
|
322 |
}
|
319 |
}
|
323 |
}
|
320 |
return false;
|
324 |
return false;
|
Línea 479... |
Línea 483... |
479 |
|
483 |
|
480 |
if (isset($question->questiontextitemid)) {
|
484 |
if (isset($question->questiontextitemid)) {
|
481 |
$question->questiontext = file_save_draft_area_files($question->questiontextitemid,
|
485 |
$question->questiontext = file_save_draft_area_files($question->questiontextitemid,
|
482 |
$this->importcontext->id, 'question', 'questiontext', $question->id,
|
486 |
$this->importcontext->id, 'question', 'questiontext', $question->id,
|
- |
|
487 |
$fileoptions, $question->questiontext);
|
483 |
$fileoptions, $question->questiontext);
|
488 |
file_clear_draft_area($question->questiontextitemid);
|
484 |
} else if (isset($question->questiontextfiles)) {
|
489 |
} else if (isset($question->questiontextfiles)) {
|
485 |
foreach ($question->questiontextfiles as $file) {
|
490 |
foreach ($question->questiontextfiles as $file) {
|
486 |
question_bank::get_qtype($question->qtype)->import_file(
|
491 |
question_bank::get_qtype($question->qtype)->import_file(
|
487 |
$this->importcontext, 'question', 'questiontext', $question->id, $file);
|
492 |
$this->importcontext, 'question', 'questiontext', $question->id, $file);
|
488 |
}
|
493 |
}
|
489 |
}
|
494 |
}
|
490 |
if (isset($question->generalfeedbackitemid)) {
|
495 |
if (isset($question->generalfeedbackitemid)) {
|
491 |
$question->generalfeedback = file_save_draft_area_files($question->generalfeedbackitemid,
|
496 |
$question->generalfeedback = file_save_draft_area_files($question->generalfeedbackitemid,
|
492 |
$this->importcontext->id, 'question', 'generalfeedback', $question->id,
|
497 |
$this->importcontext->id, 'question', 'generalfeedback', $question->id,
|
- |
|
498 |
$fileoptions, $question->generalfeedback);
|
493 |
$fileoptions, $question->generalfeedback);
|
499 |
file_clear_draft_area($question->generalfeedbackitemid);
|
494 |
} else if (isset($question->generalfeedbackfiles)) {
|
500 |
} else if (isset($question->generalfeedbackfiles)) {
|
495 |
foreach ($question->generalfeedbackfiles as $file) {
|
501 |
foreach ($question->generalfeedbackfiles as $file) {
|
496 |
question_bank::get_qtype($question->qtype)->import_file(
|
502 |
question_bank::get_qtype($question->qtype)->import_file(
|
497 |
$this->importcontext, 'question', 'generalfeedback', $question->id, $file);
|
503 |
$this->importcontext, 'question', 'generalfeedback', $question->id, $file);
|
Línea 506... |
Línea 512... |
506 |
$result = question_bank::get_qtype($question->qtype)->save_question_options($question);
|
512 |
$result = question_bank::get_qtype($question->qtype)->save_question_options($question);
|
507 |
$event = \core\event\question_created::create_from_question_instance($question, $this->importcontext);
|
513 |
$event = \core\event\question_created::create_from_question_instance($question, $this->importcontext);
|
508 |
$event->trigger();
|
514 |
$event->trigger();
|
Línea 509... |
Línea 515... |
509 |
|
515 |
|
510 |
if (core_tag_tag::is_enabled('core_question', 'question')) {
|
516 |
if (core_tag_tag::is_enabled('core_question', 'question')) {
|
511 |
// Is the current context we're importing in a course context?
|
- |
|
512 |
$importingcontext = $this->importcontext;
|
- |
|
513 |
$importingcoursecontext = $importingcontext->get_course_context(false);
|
- |
|
514 |
$isimportingcontextcourseoractivity = !empty($importingcoursecontext);
|
- |
|
515 |
|
- |
|
516 |
if (!empty($question->coursetags)) {
|
- |
|
517 |
if ($isimportingcontextcourseoractivity) {
|
517 |
// Course tags on question now deprecated so merge them into the question tags.
|
518 |
$mergedtags = array_merge($question->coursetags, $question->tags);
|
- |
|
519 |
|
518 |
$mergedtags = array_merge($question->coursetags ?? [], $question->tags ?? []);
|
520 |
core_tag_tag::set_item_tags('core_question', 'question', $question->id,
|
- |
|
521 |
$question->context, $mergedtags);
|
519 |
core_tag_tag::set_item_tags('core_question',
|
522 |
} else {
|
- |
|
523 |
core_tag_tag::set_item_tags('core_question', 'question', $question->id,
|
- |
|
524 |
context_course::instance($this->course->id), $question->coursetags);
|
- |
|
525 |
|
520 |
'question',
|
526 |
if (!empty($question->tags)) {
|
- |
|
527 |
core_tag_tag::set_item_tags('core_question', 'question', $question->id,
|
- |
|
528 |
$importingcontext, $question->tags);
|
521 |
$question->id,
|
529 |
}
|
522 |
$question->context,
|
530 |
}
|
- |
|
531 |
} else if (!empty($question->tags)) {
|
- |
|
532 |
core_tag_tag::set_item_tags('core_question', 'question', $question->id,
|
- |
|
533 |
$question->context, $question->tags);
|
523 |
$mergedtags
|
534 |
}
|
524 |
);
|
Línea 535... |
Línea 525... |
535 |
}
|
525 |
}
|
536 |
|
526 |
|
537 |
if (!empty($result->error)) {
|
527 |
if (!empty($result->error)) {
|
Línea 611... |
Línea 601... |
611 |
$context = context::instance_by_id($contextid);
|
601 |
$context = context::instance_by_id($contextid);
|
612 |
require_capability('moodle/question:add', $context);
|
602 |
require_capability('moodle/question:add', $context);
|
613 |
} else {
|
603 |
} else {
|
614 |
$context = context::instance_by_id($this->category->contextid);
|
604 |
$context = context::instance_by_id($this->category->contextid);
|
615 |
}
|
605 |
}
|
- |
|
606 |
|
- |
|
607 |
// If the file had categories in a deprecated context then the categories need to be applied to a default system type
|
- |
|
608 |
// mod_qbank instance on the course instead.
|
- |
|
609 |
if ($context->contextlevel !== CONTEXT_MODULE) {
|
- |
|
610 |
$qbank = \core_question\local\bank\question_bank_helper::get_default_open_instance_system_type($this->course, true);
|
- |
|
611 |
$context = context_module::instance($qbank->id);
|
- |
|
612 |
}
|
- |
|
613 |
|
616 |
$this->importcontext = $context;
|
614 |
$this->importcontext = $context;
|
Línea 617... |
Línea 615... |
617 |
|
615 |
|
618 |
// Now create any categories that need to be created.
|
616 |
// Now create any categories that need to be created.
|
619 |
foreach ($catnames as $key => $catname) {
|
617 |
foreach ($catnames as $key => $catname) {
|
Línea 958... |
Línea 956... |
958 |
$questionbankentry = question_bank::load_question($question->id);
|
956 |
$questionbankentry = question_bank::load_question($question->id);
|
959 |
$qcategory = $questionbankentry->category;
|
957 |
$qcategory = $questionbankentry->category;
|
960 |
$contextid = $DB->get_field('question_categories', 'contextid', ['id' => $qcategory]);
|
958 |
$contextid = $DB->get_field('question_categories', 'contextid', ['id' => $qcategory]);
|
961 |
$question->contextid = $contextid;
|
959 |
$question->contextid = $contextid;
|
962 |
$question->idnumber = $questionbankentry->idnumber;
|
960 |
$question->idnumber = $questionbankentry->idnumber;
|
- |
|
961 |
|
- |
|
962 |
// Do not export hidden questions.
|
- |
|
963 |
if ($question->status === \core_question\local\bank\question_version_status::QUESTION_STATUS_HIDDEN) {
|
- |
|
964 |
continue;
|
- |
|
965 |
}
|
- |
|
966 |
|
963 |
if ($question->status === \core_question\local\bank\question_version_status::QUESTION_STATUS_READY) {
|
967 |
if ($question->status === \core_question\local\bank\question_version_status::QUESTION_STATUS_READY) {
|
964 |
$question->status = 0;
|
968 |
$question->status = 0;
|
965 |
} else {
|
969 |
} else {
|
966 |
$question->status = 1;
|
970 |
$question->status = 1;
|
967 |
}
|
971 |
}
|
Línea 968... |
Línea -... |
968 |
|
- |
|
969 |
// do not export hidden questions
|
- |
|
970 |
if (!empty($question->hidden)) {
|
- |
|
971 |
continue;
|
- |
|
972 |
}
|
- |
|
973 |
|
972 |
|
974 |
// do not export random questions
|
973 |
// do not export random questions
|
975 |
if ($question->qtype == 'random') {
|
974 |
if ($question->qtype == 'random') {
|
976 |
continue;
|
975 |
continue;
|
Línea 1023... |
Línea 1022... |
1023 |
$count++;
|
1022 |
$count++;
|
1024 |
}
|
1023 |
}
|
1025 |
}
|
1024 |
}
|
1026 |
}
|
1025 |
}
|
Línea 1027... |
Línea 1026... |
1027 |
|
1026 |
|
1028 |
// continue path for following error checks
|
- |
|
1029 |
$course = $this->course;
|
- |
|
1030 |
$continuepath = "{$CFG->wwwroot}/question/bank/exportquestions/export.php?courseid={$course->id}";
|
- |
|
1031 |
|
- |
|
1032 |
// did we actually process anything
|
1027 |
// Did we actually process anything? Then continue path for following error checks.
|
- |
|
1028 |
if ($count==0) {
|
- |
|
1029 |
$contextid = $DB->get_field('question_categories', 'contextid', ['id' => $this->category->id]);
|
- |
|
1030 |
$context = context::instance_by_id($contextid);
|
1033 |
if ($count==0) {
|
1031 |
$continuepath = "{$CFG->wwwroot}/question/bank/exportquestions/export.php?cmid={$context->instanceid}";
|
1034 |
throw new \moodle_exception('noquestions', 'question', $continuepath);
|
1032 |
throw new \moodle_exception('noquestions', 'question', $continuepath);
|
Línea 1035... |
Línea 1033... |
1035 |
}
|
1033 |
}
|
1036 |
|
1034 |
|