Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
problem-builder
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
problem-builder
Commits
8d1bb9e6
Commit
8d1bb9e6
authored
Nov 19, 2013
by
Xavier Antoviaque
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add ability to share answers between XBlocks, using Django models
parent
a63d4d60
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
86 additions
and
3 deletions
+86
-3
mentoring/answer.py
+33
-1
mentoring/migrations/0001_initial.py
+47
-0
mentoring/migrations/__init__.py
+0
-0
mentoring/models.py
+6
-2
No files found.
mentoring/answer.py
View file @
8d1bb9e6
...
...
@@ -9,6 +9,8 @@ from xblock.core import XBlock
from
xblock.fields
import
Any
,
Scope
from
xblock.fragment
import
Fragment
from
.models
import
Answer
# Globals ###########################################################
...
...
@@ -26,12 +28,20 @@ class AnswerBlock(XBlock):
"""
student_input
=
Any
(
help
=
"Last input submitted by the student"
,
default
=
""
,
scope
=
Scope
.
user_state
)
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
(
AnswerBlock
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
if
self
.
name
:
self
.
student_input
=
self
.
get_model_object
()
.
student_input
def
student_view
(
self
,
context
=
None
):
# pylint: disable=W0613
"""Returns default student view."""
return
Fragment
(
u"<p>I can only appear inside problems.</p>"
)
def
mentoring_view
(
self
,
context
=
None
):
html
=
u'<textarea cols="100" rows="10" name="input">{}</textarea>'
.
format
(
self
.
student_input
)
html
=
u'<textarea cols="100" rows="10" maxlength="{}" name="input">{}</textarea>'
.
format
(
Answer
.
_meta
.
get_field
(
'student_input'
)
.
max_length
,
self
.
student_input
)
fragment
=
Fragment
(
html
)
fragment
.
add_javascript
(
"""
function AnswerBlock(runtime, element) {
...
...
@@ -53,3 +63,25 @@ class AnswerBlock(XBlock):
log
.
info
(
u'Answer submitted for`{}`: "{}"'
.
format
(
self
.
name
,
self
.
student_input
))
return
self
.
student_input
def
save
(
self
):
"""
Replicate data changes on the related Django model used for sharing of data accross XBlocks
"""
super
(
AnswerBlock
,
self
)
.
save
()
answer_data
=
self
.
get_model_object
()
if
answer_data
.
student_input
!=
self
.
student_input
:
answer_data
.
student_input
=
self
.
student_input
answer_data
.
save
()
def
get_model_object
(
self
):
if
not
self
.
name
:
raise
ValueError
,
'AnswerBlock.name field need to be set to a non-null/empty value'
# TODO Use a random user id
student_id
=
self
.
scope_ids
.
user_id
answer_data
,
created
=
Answer
.
objects
.
get_or_create
(
student_id
=
student_id
,
name
=
self
.
name
)
return
answer_data
mentoring/migrations/0001_initial.py
0 → 100644
View file @
8d1bb9e6
# -*- coding: utf-8 -*-
import
datetime
from
south.db
import
db
from
south.v2
import
SchemaMigration
from
django.db
import
models
class
Migration
(
SchemaMigration
):
def
forwards
(
self
,
orm
):
# Adding model 'Answer'
db
.
create_table
(
'mentoring_answer'
,
(
(
'id'
,
self
.
gf
(
'django.db.models.fields.AutoField'
)(
primary_key
=
True
)),
(
'name'
,
self
.
gf
(
'django.db.models.fields.CharField'
)(
max_length
=
20
,
db_index
=
True
)),
(
'student_id'
,
self
.
gf
(
'django.db.models.fields.CharField'
)(
max_length
=
20
,
db_index
=
True
)),
(
'student_input'
,
self
.
gf
(
'django.db.models.fields.CharField'
)(
max_length
=
10000
)),
(
'created_on'
,
self
.
gf
(
'django.db.models.fields.DateTimeField'
)(
auto_now_add
=
True
,
blank
=
True
)),
(
'modified_on'
,
self
.
gf
(
'django.db.models.fields.DateTimeField'
)(
auto_now
=
True
,
blank
=
True
)),
))
db
.
send_create_signal
(
'mentoring'
,
[
'Answer'
])
# Adding unique constraint on 'Answer', fields ['student_id', 'name']
db
.
create_unique
(
'mentoring_answer'
,
[
'student_id'
,
'name'
])
def
backwards
(
self
,
orm
):
# Removing unique constraint on 'Answer', fields ['student_id', 'name']
db
.
delete_unique
(
'mentoring_answer'
,
[
'student_id'
,
'name'
])
# Deleting model 'Answer'
db
.
delete_table
(
'mentoring_answer'
)
models
=
{
'mentoring.answer'
:
{
'Meta'
:
{
'unique_together'
:
"(('student_id', 'name'),)"
,
'object_name'
:
'Answer'
},
'created_on'
:
(
'django.db.models.fields.DateTimeField'
,
[],
{
'auto_now_add'
:
'True'
,
'blank'
:
'True'
}),
'id'
:
(
'django.db.models.fields.AutoField'
,
[],
{
'primary_key'
:
'True'
}),
'modified_on'
:
(
'django.db.models.fields.DateTimeField'
,
[],
{
'auto_now'
:
'True'
,
'blank'
:
'True'
}),
'name'
:
(
'django.db.models.fields.CharField'
,
[],
{
'max_length'
:
'20'
,
'db_index'
:
'True'
}),
'student_id'
:
(
'django.db.models.fields.CharField'
,
[],
{
'max_length'
:
'20'
,
'db_index'
:
'True'
}),
'student_input'
:
(
'django.db.models.fields.CharField'
,
[],
{
'max_length'
:
'10000'
})
}
}
complete_apps
=
[
'mentoring'
]
\ No newline at end of file
mentoring/migrations/__init__.py
0 → 100644
View file @
8d1bb9e6
mentoring/models.py
View file @
8d1bb9e6
...
...
@@ -3,6 +3,10 @@ from django.db import models
class
Answer
(
models
.
Model
):
class
Meta
:
app_label
=
'mentoring'
unique_together
=
((
'student_id'
,
'name'
),)
text
=
models
.
CharField
(
max_length
=
2000
)
pub_date
=
models
.
DateTimeField
(
'date published'
,
auto_now_add
=
True
)
name
=
models
.
CharField
(
max_length
=
20
,
db_index
=
True
)
student_id
=
models
.
CharField
(
max_length
=
20
,
db_index
=
True
)
student_input
=
models
.
CharField
(
max_length
=
10000
)
created_on
=
models
.
DateTimeField
(
'created on'
,
auto_now_add
=
True
)
modified_on
=
models
.
DateTimeField
(
'modified on'
,
auto_now
=
True
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment