Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement editing of questions #5614

Merged
merged 46 commits into from
Dec 10, 2018
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
d9f2824
Save.
tjiang11 Aug 30, 2018
f0cb957
Save.
tjiang11 Aug 30, 2018
9c8e702
Save. UrlInterpolationError topic null.
tjiang11 Aug 30, 2018
c93f186
Save.
tjiang11 Aug 30, 2018
f695dd6
Modify backend to return associated skills when fetching a question.
tjiang11 Aug 30, 2018
066faec
save.
tjiang11 Aug 30, 2018
07a11fb
Saving content works.
tjiang11 Aug 30, 2018
2386f69
Copy interaction.
tjiang11 Aug 30, 2018
4360648
Apply QuestionUpdateService for all changes.
tjiang11 Aug 30, 2018
a68adef
Circumvent race condition on initialization.
tjiang11 Aug 30, 2018
dd9d25f
Lint.
tjiang11 Aug 30, 2018
8a2c2bd
Clone UndoRedoService for questions.
tjiang11 Aug 30, 2018
4782099
Fetch question summaries after edit question.
tjiang11 Aug 30, 2018
a20b0ab
Create reusable function in QuestionEditorDirective.
tjiang11 Aug 30, 2018
4668f51
Lint.
tjiang11 Aug 30, 2018
fd91189
Add test.
tjiang11 Sep 1, 2018
2f4f1cc
Fix test.
tjiang11 Sep 1, 2018
3050886
Lint.
tjiang11 Sep 1, 2018
6d52d8c
Disable flag.
tjiang11 Sep 1, 2018
43807b1
Review changes.
tjiang11 Sep 3, 2018
7a4ff6f
Review changes.
tjiang11 Sep 9, 2018
41798c6
Review comments.
tjiang11 Sep 13, 2018
c496793
Lint.
tjiang11 Sep 13, 2018
fbdaa83
Review changes.
tjiang11 Sep 23, 2018
732be38
Deduplicate UndoRedoService.
tjiang11 Sep 23, 2018
7756f6d
Move updateFunction into QuestionUpdateService.
tjiang11 Sep 23, 2018
17ca89f
Fix test.
tjiang11 Oct 14, 2018
5785784
Lint.
tjiang11 Oct 14, 2018
13254ea
Update documentation.
tjiang11 Oct 15, 2018
fc4981d
Add tests for get_models_by_question_id
tjiang11 Oct 15, 2018
9ef2940
Update setQuestionStateData.
tjiang11 Oct 15, 2018
5861a5a
Fix test.
tjiang11 Oct 15, 2018
4ccea9c
Fix test.
tjiang11 Oct 15, 2018
0c589eb
Merge develop.
tjiang11 Oct 15, 2018
c0ad966
Lint.
tjiang11 Oct 15, 2018
9896d99
Add test.
tjiang11 Oct 25, 2018
4d3c329
Merge develop.
tjiang11 Oct 28, 2018
d37ab38
Lint.
tjiang11 Oct 28, 2018
f6f7e09
Edit questions in modal.
tjiang11 Oct 28, 2018
5722ca1
Add blank commit message.
tjiang11 Nov 4, 2018
3b5983f
Remove duplicate function; Fix issue with not being able to edit a qu…
tjiang11 Dec 1, 2018
c4c9b93
Merge develop.
tjiang11 Dec 1, 2018
98227d0
Lint.
tjiang11 Dec 1, 2018
5abc2c8
Lint.
tjiang11 Dec 1, 2018
387a9cb
Fix.
tjiang11 Dec 1, 2018
7a10a7b
Flip flag.
tjiang11 Dec 7, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions core/controllers/question_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,17 @@ def get(self, question_id):
raise self.PageNotFoundException(
'The question with the given id doesn\'t exist.')

associated_skill_ids = [link.skill_id for link in (
question_services.get_question_skill_links_of_question(
question_id))]
associated_skills = skill_services.get_multi_skills(
associated_skill_ids)
associated_skill_dicts = [
skill.to_dict() for skill in associated_skills]

self.values.update({
'question_dict': question.to_dict()
'question_dict': question.to_dict(),
'associated_skill_dicts': associated_skill_dicts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

})
self.render_json(self.values)

Expand Down Expand Up @@ -151,7 +160,7 @@ def put(self, question_id):

question_dict = question_services.get_question_by_id(
question_id).to_dict()
return self.render_json({
self.render_json({
'question_dict': question_dict
})

Expand Down
26 changes: 23 additions & 3 deletions core/domain/question_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def get_question_skill_links_of_skill(skill_id):
Returns:
list(QuestionSkillLinkModel). The list of question skill link
domain objects that are linked to the skill ID or an empty list
if the skill does not exist.
if the skill does not exist.
"""

question_skill_link_models = (
Expand All @@ -196,6 +196,26 @@ def get_question_skill_links_of_skill(skill_id):
return question_skill_link_models


def get_question_skill_links_of_question(question_id):
"""Returns a list of QuestionSkillLinkModels of
a particular question ID.

Args:
question_id: str. ID of the question.

Returns:
list(QuestionSkillLinkModel). The list of question skill link
domain objects that are linked to the question ID or an empty list
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the domain objects for QuestionSkillLink are being returned currently. Can you change the returned object to be a list of QuestionSkillLink domain objects instead of datastore objects?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, I think the docstring should also be changed to QuestionSkillLink, instead of QuestionSkillLinkModel?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pending comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

if the question does not exist.
"""

question_skill_link_models = (
question_models.QuestionSkillLinkModel.get_models_by_question_id(
question_id)
)
return question_skill_link_models


def update_skill_ids_of_questions(curr_skill_id, new_skill_id):
"""Updates the skill ID of QuestionSkillLinkModels to the superseding
skill ID.
Expand Down Expand Up @@ -316,9 +336,9 @@ def apply_change_list(question_id, change_list):
if (change.property_name ==
question_domain.QUESTION_PROPERTY_LANGUAGE_CODE):
question.update_language_code(change.new_value)
elif (change.cmd ==
elif (change.property_name ==
question_domain.QUESTION_PROPERTY_QUESTION_STATE_DATA):
question.update_question_data(change.new_value)
question.update_question_state_data(change.new_value)

return question

Expand Down
35 changes: 35 additions & 0 deletions core/domain/question_services_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,38 @@ def test_created_question_rights(self):
Exception, 'Entity for class QuestionRightsModel with id '
'question_id not found'):
question_services.get_question_rights('question_id')

def test_get_question_skill_links_of_question(self):
# If the question id doesnt exist at all, it returns an empty list.
question_skill_links = (
question_services.get_question_skill_links_of_question(
'non_existent_question_id'))
self.assertEqual(len(question_skill_links), 0)

question_id_2 = question_services.get_new_question_id()
self.save_new_question(
question_id_2, self.editor_id,
self._create_valid_question_data('ABC'))

question_id_3 = question_services.get_new_question_id()
self.save_new_question(
question_id_3, self.editor_id,
self._create_valid_question_data('ABC'))
question_services.create_new_question_skill_link(
self.question_id, 'skill_1')
question_services.create_new_question_skill_link(
question_id_2, 'skill_1')
question_services.create_new_question_skill_link(
question_id_2, 'skill_2')
question_services.create_new_question_skill_link(
question_id_3, 'skill_2')

question_skill_links = (
question_services.get_question_skill_links_of_question(
question_id_2))

self.assertEqual(len(question_skill_links), 2)
skill_ids = [question_skill.skill_id for question_skill
in question_skill_links]
self.assertItemsEqual(
skill_ids, ['skill_1', 'skill_2'])
16 changes: 16 additions & 0 deletions core/domain/skill_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,22 @@ def get_multi_skill_summaries(skill_ids):
return skill_summaries


def get_multi_skills(skill_ids):
"""Returns a list of skills matching the skill IDs provided.

Args:
skill_ids: list(str). List of skill IDs to get skills for.

Returns:
list(Skill). The list of skills matching the provided IDs.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not seem to reflect the implementation just excluding skills that are None.

Either throw an exception if a skill is requested that does not exist, or return None in the appropriate position; probably the former is better if you don't expect this to occur in practice. But just silently removing stuff altogether seems like it's not the right thing to do -- you'll end up with the length of the input list not matching the output list which is something the caller won't expect given the function contract.

There should also be tests for this error case, it's quite important.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed by raising Exception

"""
local_skill_models = skill_models.SkillModel.get_multi(skill_ids)
skills = [
get_skill_from_model(skill_model)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if any of the skill_id doesn't exist? In that case, won't a None be returned?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

for skill_model in local_skill_models]
return skills


def get_skill_summary_from_model(skill_summary_model):
"""Returns a domain object for an Oppia skill summary given a
skill summary model.
Expand Down
3 changes: 2 additions & 1 deletion core/domain/state_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,8 @@ def validate(self, exp_param_specs_dict, allow_null_interaction):
if not set(content_id_list) <= set(available_content_ids):
raise utils.ValidationError(
'Expected state content_ids_to_audio_translations to have all '
'of the listed content ids %s' % content_id_list)
'of the listed content ids %s, found %s' %
(content_id_list, available_content_ids))
if not isinstance(self.content_ids_to_audio_translations, dict):
raise utils.ValidationError(
'Expected state content_ids_to_audio_translations to be a dict,'
Expand Down
16 changes: 16 additions & 0 deletions core/storage/question/gae_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,22 @@ def get_models_by_skill_id(cls, skill_id):
return QuestionSkillLinkModel.query().filter(
cls.skill_id == skill_id).fetch()

@classmethod
def get_models_by_question_id(cls, question_id):
"""Returns a list of QuestionSkillLink domains of a particular
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QuestionSkillLink domains --> QuestionSkillLinkModels, right?

question ID.

Args:
question_id: str. ID of the question.

Returns:
list(QuestionSkillLinkModel)|None. The list of question skill link
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto -- question skill link domains --> question skill link models.

Domain objects refer to stuff defined in core/domain ("plain old python objects").

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

domains that are linked to the question ID. None if the question
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this correctly differentiate between "question ID doesn't exist" and "there are no question skill link models corresponding to this question ID"? Looking at the implementation I'm not sure it does, we might need to update the docstring to be clearer here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

ID doesn't exist.
"""
return QuestionSkillLinkModel.query().filter(
cls.question_id == question_id).fetch()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't cls.deleted == false be also checked?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


@classmethod
def put_multi_question_skill_links(cls, question_skill_links):
"""Puts multiple question skill links into the datastore.
Expand Down
124 changes: 124 additions & 0 deletions core/templates/dev/head/domain/editor/undo_redo/UndoRedoService.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,127 @@ oppia.factory('UndoRedoService', [
return UndoRedoService;
}
]);

// TODO(tjiang11): Figure out way to create new instance of service without
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just thinking on reading this that a lot of this code seems pretty general. Why can't we reuse the existing UndoRedoService instead of duplicating?

I'm concerned that this becomes one of those TODOs that never gets done (e.g. #4968 just appeared in my inbox) so I feel like we should do it properly since we'll need it for other things too? In general code duplication should be avoided as much as possible (and this is really quite a lot of it).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deduplicated

// code duplication.
oppia.factory('QuestionUndoRedoService', [
'$rootScope', 'EVENT_UNDO_REDO_SERVICE_CHANGE_APPLIED',
function($rootScope, EVENT_UNDO_REDO_SERVICE_CHANGE_APPLIED) {
var QuestionUndoRedoService = {};

var _appliedChanges = [];
var _undoneChanges = [];

var _dispatchMutation = function() {
$rootScope.$broadcast(EVENT_UNDO_REDO_SERVICE_CHANGE_APPLIED);
};
var _applyChange = function(changeObject, domainObject) {
changeObject.applyChange(domainObject);
_dispatchMutation();
};
var _reverseChange = function(changeObject, domainObject) {
changeObject.reverseChange(domainObject);
_dispatchMutation();
};

/**
* Pushes a change domain object onto the change stack and applies it to the
* provided domain object. When a new change is applied, all undone changes
* are lost and cannot be redone. This will fire an event as defined by the
* constant EVENT_UNDO_REDO_SERVICE_CHANGE_APPLIED.
*/
QuestionUndoRedoService.applyChange = function(changeObject, domainObject) {
_applyChange(changeObject, domainObject);
_appliedChanges.push(changeObject);
_undoneChanges = [];
};

/**
* Undoes the last change to the provided domain object. This function
* returns false if there are no changes to undo, and true otherwise. This
* will fire an event as defined by the constant
* EVENT_UNDO_REDO_SERVICE_CHANGE_APPLIED.
*/
QuestionUndoRedoService.undoChange = function(domainObject) {
if (_appliedChanges.length !== 0) {
var change = _appliedChanges.pop();
_undoneChanges.push(change);
_reverseChange(change, domainObject);
return true;
}
return false;
};

/**
* Reverses an undo for the given domain object. This function returns false
* if there are no changes to redo, and true if otherwise. This will fire an
* event as defined by the constant EVENT_UNDO_REDO_SERVICE_CHANGE_APPLIED.
*/
QuestionUndoRedoService.redoChange = function(domainObject) {
if (_undoneChanges.length !== 0) {
var change = _undoneChanges.pop();
_appliedChanges.push(change);
_applyChange(change, domainObject);
return true;
}
return false;
};

/**
* Returns the current list of changes applied to the provided domain
* object. This list will not contain undone actions. Changes to the
* returned list will not be reflected in this service.
*/
QuestionUndoRedoService.getChangeList = function() {
// TODO(bhenning): Consider integrating something like Immutable.js to
// avoid the slice here and ensure the returned object is truly an
// immutable copy.
return _appliedChanges.slice();
};

/**
* Returns a list of commit dict updates representing all chosen changes in
* this service. Changes to the returned list will not affect this service.
* Furthermore, the returned list is ready to be sent to the backend.
*/
QuestionUndoRedoService.getCommittableChangeList = function() {
var committableChangeList = [];
for (var i = 0; i < _appliedChanges.length; i++) {
committableChangeList[i] = _appliedChanges[i].getBackendChangeObject();
}
return committableChangeList;
};

QuestionUndoRedoService.setChangeList = function(changeList) {
_appliedChanges = angular.copy(changeList);
};

/**
* Returns the number of changes that have been applied to the domain
* object.
*/
QuestionUndoRedoService.getChangeCount = function() {
return _appliedChanges.length;
};

/**
* Returns whether this service has any applied changes.
*/
QuestionUndoRedoService.hasChanges = function() {
return _appliedChanges.length !== 0;
};

/**
* Clears the change history. This does not reverse any of the changes
* applied from applyChange() or redoChange(). This will fire an event as
* defined by the constant EVENT_UNDO_REDO_SERVICE_CHANGE_APPLIED.
*/
QuestionUndoRedoService.clearChanges = function() {
_appliedChanges = [];
_undoneChanges = [];
_dispatchMutation();
};

return QuestionUndoRedoService;
}
]);
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ oppia.factory('InteractionObjectFactory', [
this.hints = newValue;
};

Interaction.prototype.copy = function(otherInteraction) {
this.answerGroups = angular.copy(otherInteraction.answerGroups);
this.confirmedUnclassifiedAnswers =
angular.copy(otherInteraction.confirmedUnclassifiedAnswers);
this.customizationArgs = angular.copy(otherInteraction.customizationArgs);
this.defaultOutcome = angular.copy(otherInteraction.defaultOutcome);
this.hints = angular.copy(otherInteraction.hints);
this.id = angular.copy(otherInteraction.id);
this.solution = angular.copy(otherInteraction.solution);
};

Interaction.prototype.toBackendDict = function() {
return {
answer_groups: this.answerGroups.map(function(answerGroup) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,12 @@ oppia.factory('EditableQuestionBackendApiService', [
});

$http.get(questionDataUrl).then(function(response) {
var questionDict = angular.copy(response.data.question_dict);
var data = angular.copy(response.data);
if (successCallback) {
successCallback(questionDict);
successCallback({
question_dict: data.question_dict,
associated_skill_dicts: data.associated_skill_dicts
});
}
}, function(errorResponse) {
if (errorCallback) {
Expand All @@ -82,7 +85,7 @@ oppia.factory('EditableQuestionBackendApiService', [
var putData = {
version: questionVersion,
commit_message: commitMessage,
change_dicts: changeList
change_list: changeList
};
$http.put(editableQuestionDataUrl, putData).then(function(response) {
// The returned data is an updated question dict.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ describe('Editable question backend API service', function() {
},
language_code: 'en',
version: 1
}
},
associated_skill_dicts: []
};
}));

Expand All @@ -95,7 +96,7 @@ describe('Editable question backend API service', function() {
$httpBackend.flush();

expect(successHandler).toHaveBeenCalledWith(
sampleDataResults.question_dict);
sampleDataResults);
expect(failHandler).not.toHaveBeenCalled();
}
);
Expand Down Expand Up @@ -126,11 +127,10 @@ describe('Editable question backend API service', function() {
sampleDataResults);

EditableQuestionBackendApiService.fetchQuestion('0').then(
function(questionDict) {
question = questionDict;
function(data) {
question = data.question_dict;
});
$httpBackend.flush();

question.question_state_data.content.html = 'New Question Content';
question.version = '2';
var questionWrapper = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ oppia.factory('QuestionObjectFactory', [
return this._stateData;
};

Question.prototype.setStateData = function(stateData) {
this._stateData.copy(stateData);
};

Question.prototype.getLanguageCode = function() {
return this._languageCode;
};
Expand Down
Loading