Extra fields: Add option to copy values from one item to another.

Fix issue when copying an exercise and a question #3646
pull/3671/head
Julio Montoya 5 years ago
parent 2c28ffb5a3
commit 45e776f11a
  1. 6
      main/exercise/exercise.class.php
  2. 12
      main/exercise/question.class.php
  3. 24
      main/inc/lib/extra_field_value.lib.php

@ -1958,6 +1958,7 @@ class Exercise
$this->id, $this->id,
$this->sessionId $this->sessionId
); );
if ($linkInfo !== false) { if ($linkInfo !== false) {
GradebookUtils::remove_resource_from_course_gradebook($linkInfo['id']); GradebookUtils::remove_resource_from_course_gradebook($linkInfo['id']);
} }
@ -3076,6 +3077,7 @@ class Exercise
$categories = $exerciseObject->getCategoriesInExercise(true); $categories = $exerciseObject->getCategoriesInExercise(true);
// Get all questions no matter the order/category settings // Get all questions no matter the order/category settings
$questionList = $exerciseObject->getQuestionOrderedList(); $questionList = $exerciseObject->getQuestionOrderedList();
$sourceId = $exerciseObject->iId;
// Force the creation of a new exercise // Force the creation of a new exercise
$exerciseObject->updateTitle($exerciseObject->selectTitle().' - '.get_lang('Copy')); $exerciseObject->updateTitle($exerciseObject->selectTitle().' - '.get_lang('Copy'));
// Hides the new exercise // Hides the new exercise
@ -3092,6 +3094,9 @@ class Exercise
$em = Database::getManager(); $em = Database::getManager();
if ($newId && !empty($questionList)) { if ($newId && !empty($questionList)) {
$extraField = new ExtraFieldValue('exercise');
$extraField->copy($sourceId, $newId);
// Question creation // Question creation
foreach ($questionList as $oldQuestionId) { foreach ($questionList as $oldQuestionId) {
$oldQuestionObj = Question::read($oldQuestionId, null, false); $oldQuestionObj = Question::read($oldQuestionId, null, false);
@ -3099,7 +3104,6 @@ class Exercise
if ($newQuestionId) { if ($newQuestionId) {
$newQuestionObj = Question::read($newQuestionId, null, false); $newQuestionObj = Question::read($newQuestionId, null, false);
if (isset($newQuestionObj) && $newQuestionObj) { if (isset($newQuestionObj) && $newQuestionObj) {
//$newQuestionObj->addToList($newId);
$sql = "INSERT INTO $exerciseRelQuestionTable (c_id, question_id, exercice_id, question_order) $sql = "INSERT INTO $exerciseRelQuestionTable (c_id, question_id, exercice_id, question_order)
VALUES ($courseId, ".$newQuestionId.", ".$newId.", '$count')"; VALUES ($courseId, ".$newQuestionId.", ".$newId.", '$count')";
Database::query($sql); Database::query($sql);

@ -1438,6 +1438,10 @@ abstract class Question
question_id = ".$id; question_id = ".$id;
Database::query($sql); Database::query($sql);
// Add extra fields.
$extraField = new ExtraFieldValue('question');
$extraField->deleteValuesByItem($this->iid);
api_item_property_update( api_item_property_update(
$this->course, $this->course,
TOOL_QUIZ, TOOL_QUIZ,
@ -1454,7 +1458,7 @@ abstract class Question
} else { } else {
// just removes the exercise from the list // just removes the exercise from the list
$this->removeFromList($deleteFromEx, $courseId); $this->removeFromList($deleteFromEx, $courseId);
if (api_get_setting('search_enabled') == 'true' && extension_loaded('xapian')) { if (api_get_setting('search_enabled') === 'true' && extension_loaded('xapian')) {
// disassociate question with this exercise // disassociate question with this exercise
$this->search_engine_edit($deleteFromEx, false, true); $this->search_engine_edit($deleteFromEx, false, true);
} }
@ -1483,7 +1487,7 @@ abstract class Question
* *
* @param array $courseInfo Course info of the destination course * @param array $courseInfo Course info of the destination course
* *
* @return false|string ID of the new question * @return false|int ID of the new question
*/ */
public function duplicate($courseInfo = []) public function duplicate($courseInfo = [])
{ {
@ -1541,6 +1545,10 @@ abstract class Question
WHERE iid = $newQuestionId"; WHERE iid = $newQuestionId";
Database::query($sql); Database::query($sql);
// Add extra fields.
$extraField = new ExtraFieldValue('question');
$extraField->copy($this->iid, $newQuestionId);
if (!empty($options)) { if (!empty($options)) {
// Saving the quiz_options // Saving the quiz_options
foreach ($options as $item) { foreach ($options as $item) {

@ -1215,4 +1215,28 @@ class ExtraFieldValue extends Model
return $valueList; return $valueList;
} }
public function copy($sourceId, $destinationId)
{
if (empty($sourceId) || empty($destinationId)) {
return false;
}
$extraField = new ExtraField($this->type);
$allFields = $extraField->get_all();
$extraFieldValue = new ExtraFieldValue($this->type);
foreach ($allFields as $field) {
$variable = $field['variable'];
$sourceValues = $extraFieldValue->get_values_by_handler_and_field_variable($sourceId, $variable);
if (!empty($sourceValues) && isset($sourceValues['value']) && $sourceValues['value'] != '') {
$params = [
'extra_'.$variable => $sourceValues['value'],
'item_id' => $destinationId
];
$extraFieldValue->saveFieldValues($params, true);
}
}
return true;
}
} }

Loading…
Cancel
Save