Commit 8c36918a by cahrens

Support display names for choices.

parent e29c940e
......@@ -2,7 +2,11 @@
<label class="label setting-label" for="<%= uniqueId %>"><%= model.get('display_name') %></label>
<select class="input setting-input" id="<%= uniqueId %>" name="<%= model.get('display_name') %>">
<% _.each(model.get('options'), function(option) { %>
<option value="<%= option %>"><%= option %></option>
<% if (option['display_name'] !== undefined) { %>
<option value="<%= option['display_name'] %>"><%= option['display_name'] %></option>
<% } else { %>
<option value="<%= option %>"><%= option %></option>
<% } %>
<% }) %>
</select>
<button class="action setting-clear inactive" type="button" name="setting-clear" value="Clear" data-tooltip="Clear">
......
......@@ -3,10 +3,12 @@ CMS.Models.Metadata = Backbone.Model.extend({
url: '',
defaults: {
"field_name": null,
"display_name": null,
"value" : null,
"explicitly_set": null,
"default_value" : null
"default_value" : null,
"options" : null
},
initialize: function() {
......@@ -45,6 +47,14 @@ CMS.Models.Metadata = Backbone.Model.extend({
this.set('value', value);
},
getFieldName: function () {
return this.get('field_name');
},
getOptions: function () {
return this.get('options');
},
clear: function() {
this.set('explicitly_set', false);
this.set('value', this.get('default_value'));
......
......@@ -6,8 +6,6 @@ CMS.Views.Metadata.Editor = Backbone.View.extend({
events : {
},
views : {}, // child views
initialize : function() {
var self = this;
// instantiates an editor template for each update in the collection
......@@ -17,8 +15,17 @@ CMS.Views.Metadata.Editor = Backbone.View.extend({
self.template = _.template(raw_template);
self.$el.append(self.template({metadata_entries: self.model.attributes}));
var counter = 0;
_.each(self.model.attributes,
function(item, key) {
// Sort entries by display name.
var sortedObject = _.sortBy(self.model.attributes,
function (val) {
return val.display_name
});
self.views = [];
_.each(sortedObject,
function (item) {
var data = {
el: self.$el.find('.metadata_entry')[counter++],
model: new CMS.Models.Metadata(item)
......@@ -26,10 +33,10 @@ CMS.Views.Metadata.Editor = Backbone.View.extend({
if (item.options.length > 0) {
// Right now, all our option types only hold strings. Should really support
// any type though.
self.views[key] = new CMS.Views.Metadata.Option(data);
self.views.push(new CMS.Views.Metadata.Option(data));
}
else {
self.views[key] = new CMS.Views.Metadata.String(data);
self.views.push(new CMS.Views.Metadata.String(data));
}
});
......@@ -40,9 +47,9 @@ CMS.Views.Metadata.Editor = Backbone.View.extend({
getModifiedMetadataValues: function () {
var modified_values = {};
_.each(this.views,
function (item, key) {
function (item) {
if (item.modified()) {
modified_values[key] = item.getValue();
modified_values[item.getFieldName()] = item.getValue();
}
}
);
......@@ -114,6 +121,10 @@ CMS.Views.Metadata.AbstractEditor = Backbone.View.extend({
getValue: function() {
return this.model.getValue();
},
getFieldName: function() {
return this.model.getFieldName();
}
});
......@@ -152,10 +163,27 @@ CMS.Views.Metadata.Option = CMS.Views.Metadata.AbstractEditor.extend({
},
getValueFromEditor : function () {
return this.$el.find('#' + this.uniqueId).find(":selected").text();
var selectedText = this.$el.find('#' + this.uniqueId).find(":selected").text();
var selectedValue;
_.each(this.model.getOptions(), function (modelValue) {
if (modelValue === selectedText) {
selectedValue = modelValue;
}
else if (modelValue['display_name'] === selectedText) {
selectedValue = modelValue['value'];
}
});
return selectedValue;
},
setValueInEditor : function (value) {
// Value here is the json value as used by the field. The choice may instead be showing display names.
// Find the display name matching the value passed in.
_.each(this.model.getOptions(), function (modelValue) {
if (modelValue['value'] === value) {
value = modelValue['display_name'];
}
});
$('#' + this.uniqueId + " option").filter(function() {
return $(this).text() === value;
}).prop('selected', true);
......
......@@ -68,10 +68,18 @@ class CapaFields(object):
graceperiod = Timedelta(help="Amount of time after the due date that submissions will be accepted", scope=Scope.settings)
showanswer = String(display_name="Show Answer",
help="Specifies when to show the answer to this problem. A default value can be set course-wide in Advanced Settings.",
scope=Scope.settings, default="closed", values=["answered", "always", "attempted", "closed", "never"])
scope=Scope.settings, default="closed",
values=[{"display_name": "Always", "value": "always"},
{"display_name": "Answered", "value": "answered"},
{"display_name": "Attempted", "value": "attempted"},
{"display_name": "Closed", "value": "closed"},
{"display_name": "Never", "value": "never"}])
force_save_button = Boolean(help="Whether to force the save button to appear on the page", scope=Scope.settings, default=False)
rerandomize = Randomization(display_name="Randomization", help="Specifies whether variable inputs for this problem are randomized each time a student loads the problem. This only applies to problems that have randomly generated numeric variables. A default value can be set course-wide in Advanced Settings.",
default="always", scope=Scope.settings, values=["always", "onreset", "never", "per_student"])
default="always", scope=Scope.settings, values=[{"display_name": "Always", "value": "always"},
{"display_name": "On Reset", "value": "onreset"},
{"display_name": "Never", "value": "never"},
{"display_name": "Per Student", "value": "per_student"}])
data = String(help="XML data for the problem", scope=Scope.content)
correct_map = Object(help="Dictionary with the correctness of current student answers", scope=Scope.user_state, default={})
input_state = Object(help="Dictionary for maintaining the state of inputtypes", scope=Scope.user_state)
......
......@@ -651,10 +651,17 @@ class XModuleDescriptor(XModuleFields, HTMLSnippet, ResourceTemplates, XBlock):
values = [] if field.values is None else field.values
for index, choice in enumerate(values):
values[index] = field.to_json(choice)
simple_metadata[field.name] = {'value': field.to_json(value),
json_choice = choice
# TODO: test this logic.
if 'value' in json_choice:
json_choice['value'] = field.to_json(json_choice['value'])
else:
json_choice = field.to_json(json_choice)
values[index] = json_choice
simple_metadata[field.name] = {'field_name' : field.name,
'display_name' : field.display_name,
'value': field.to_json(value),
'options' : values,
'default_value': field.to_json(default_value),
'inheritable': inheritable,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment