Commit c39b2db6 by Rocky Duan

retrieve inline discussion by ajax

parent 701f8716
......@@ -13,16 +13,9 @@ import json
class DiscussionModule(XModule):
def get_html(self):
context = {
'threads': comment_client.get_threads(self.discussion_id, recursive=False),
'time_ago_in_words': time_ago_in_words,
'parse': dateutil.parser.parse,
'discussion_id': self.discussion_id,
'search_bar': '',
'user_info': comment_client.get_user_info(self.user_id, raw=True),
'tags': comment_client.get_threads_tags(raw=True),
'course_id': self.course_id,
}
return self.system.render_template('discussion/inline.html', context)
return self.system.render_template('discussion/_show_discussion.html', context)
def __init__(self, system, location, definition, instance_state=None, shared_state=None, **kwargs):
XModule.__init__(self, system, location, definition, instance_state, shared_state, **kwargs)
......@@ -33,8 +26,6 @@ class DiscussionModule(XModule):
self.discussion_id = xml_data.attrib['id']
self.title = xml_data.attrib['for']
self.discussion_category = xml_data.attrib['discussion_category']
self.user_id = instance_state['user_id']
self.course_id = instance_state['course_id']
class DiscussionDescriptor(RawDescriptor):
module_class = DiscussionModule
......@@ -124,13 +124,6 @@ def get_module(user, request, location, student_module_cache, position=None):
'''
descriptor = modulestore().get_item(location)
user_id = user.id
import re
course_id = re.search(r'^/courses/(?P<course_id>[^/]+/[^/]+/[^/]+)/', request.path)
if course_id:
course_id = course_id.group('course_id')
instance_module = student_module_cache.lookup(descriptor.category,
descriptor.location.url())
......@@ -142,11 +135,6 @@ def get_module(user, request, location, student_module_cache, position=None):
shared_module = None
instance_state = instance_module.state if instance_module is not None else {}
instance_hash = json.loads(instance_state) if isinstance(instance_state, str) or isinstance(instance_state, unicode) \
else instance_state
instance_state = json.dumps(dict(instance_hash.items() + [("user_id", user.id), ("course_id", course_id)]))
shared_state = shared_module.state if shared_module is not None else None
# TODO (vshnayder): fix hardcoded urls (use reverse)
......
......@@ -4,5 +4,6 @@ import django_comment_client.forum.views
urlpatterns = patterns('django_comment_client.forum.views',
url(r'search$', 'search', name='search'),
url(r'(?P<discussion_id>\w+)/threads/(?P<thread_id>\w+)$', 'single_thread', name='single_thread'),
url(r'(?P<discussion_id>\w+)/inline$', 'inline_discussion', name='inline_discussion'),
url(r'(?P<discussion_id>\w+)$', 'forum_form_discussion', name='forum_form_discussion'),
)
......@@ -40,6 +40,12 @@ def render_discussion(request, course_id, threads, discussion_id=None, search_te
}
return render_to_string('discussion/inline.html', context)
def inline_discussion(request, course_id, discussion_id):
print "rendering inline"
threads = comment_client.get_threads(discussion_id, recursive=False)
html = render_discussion(request, course_id, threads, discussion_id)
return HttpResponse(html, content_type="text/plain")
def render_search_bar(request, course_id, discussion_id=None, text=''):
if not discussion_id:
return ''
......@@ -54,10 +60,6 @@ def forum_form_discussion(request, course_id, discussion_id):
course = check_course(course_id)
_, course_name, _ = course_id.split('/')
url_course_id = course_id.replace('/', '_').replace('.', '_')
search_text = request.GET.get('text', '')
if len(search_text) > 0:
......@@ -67,9 +69,7 @@ def forum_form_discussion(request, course_id, discussion_id):
context = {
'csrf': csrf(request)['csrf_token'],
'COURSE_TITLE': course.title,
'course': course,
'init': '',
'content': render_discussion(request, course_id, threads, discussion_id, search_text),
'accordion': render_accordion(request, course, discussion_id),
}
......
......@@ -9,9 +9,13 @@ $ ->
$('#open_close_accordion a').click @toggle
$('#accordion').show()
$(".discussion-module").each (index, elem) ->
Discussion.initializeDiscussionModule(elem)
$("section.discussion").each (index, discussion) ->
Discussion.bindDiscussionEvents(discussion)
Discussion.initializeDiscussion(discussion)
Discussion.bindDiscussionEvents(discussion)
generateLocal = (elem) ->
......@@ -82,12 +86,52 @@ Discussion =
upload : "/courses/#{$$course_id}/discussion/upload"
search : "/courses/#{$$course_id}/discussion/forum/search"
tags_autocomplete : "/courses/#{$$course_id}/discussion/threads/tags/autocomplete"
retrieve_discussion : "/courses/#{$$course_id}/discussion/forum/#{param}/inline"
}[name]
handleAnchorAndReload: (response) ->
#window.location = window.location.pathname + "#" + response['id']
window.location.reload()
initializeDiscussionModule: (elem) ->
$discussionModule = $(elem)
$local = generateLocal($discussionModule)
handleShowDiscussion = (elem) ->
$elem = $(elem)
if $elem.attr("disabled")
return
if not $local("section.discussion").length
$elem.attr("disabled", "disabled")
discussion_id = $elem.attr("discussion_id")
url = Discussion.urlFor 'retrieve_discussion', discussion_id
$.ajax
url: url
method: "GET"
success: (data, textStatus, xhr) ->
$discussionModule.append(data)
discussion = $local("section.discussion")
Discussion.initializeDiscussion(discussion)
Discussion.bindDiscussionEvents(discussion)
$elem.removeAttr("disabled")
$elem.html("Hide Discussion")
$elem.unbind('click').click ->
handleHideDiscussion(this)
dataType: 'html'
else
$local("section.discussion").show()
$elem.html("Hide Discussion")
$elem.unbind('click').click ->
handleHideDiscussion(this)
handleHideDiscussion = (elem) ->
$local("section.discussion").hide()
$elem = $(elem)
$elem.html("Show Discussion")
$elem.unbind('click').click ->
handleShowDiscussion(this)
$local(".discussion-show").click ->
handleShowDiscussion(this)
initializeDiscussion: (discussion) ->
initializeVote = (index, content) ->
......@@ -340,8 +384,9 @@ Discussion =
converter = Markdown.getMathCompatibleConverter()
$local(".content-body").html(converter.makeHtml(raw_text))
id = $content.attr("_id")
if not ($$annotated_content_info[id] || [])['editable']
$local(".discussion-edit").remove()
if $$annotated_content_info?
if not ($$annotated_content_info[id] || [])['editable']
$local(".discussion-edit").remove()
......
......@@ -36,6 +36,10 @@
document.write('\x3Cscript type="text/javascript" src="' +
document.location.protocol + '//www.youtube.com/player_api">\x3C/script>');
</script>
<script type="text/javascript">
var $$course_id = "${course.id}";
</script>
</%block>
<%include file="course_navigation.html" args="active_page='courseware'" />
......
<div class="discussion-module">
<a class="discussion-show control-button" href="javascript:void(0)" discussion_id="${discussion_id}">Show Discussion</a>
</div>
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