From 4322f02c60659479947d6310430cbca85c1fd0e7 Mon Sep 17 00:00:00 2001 From: Dan Minkevitch Date: Thu, 29 Dec 2016 14:16:54 -0800 Subject: [PATCH] Order recent announcements widget by `posted_at` Additionally, remove default time limit from recent announcements posting so that all announcements are shown. Fixes CNVS-34030 Test Plan: * As a teacher, go to a Course's settings page * Enable "Show recent announcments on homepage" * Make sure you have the course homepage set to the wiki front page * Create multiple announcements * Post them in a different order than you created them, e.g. by using the `Delay Posting` setting * Make sure that the announcements `posted_at` ranges from longer than two weeks ago to further out than two weeks from now * View the homepage * The recent announcement list should be ordered by the date/time that you posted the announcement and not in the order that they were created, in order from most recent to least * It should display all the relevant announcements, even if they were posted longer that two weeks ago or further out than two weeks from now * Students should not see announcements that have not yet been posted or that they don't otherwise have access to Change-Id: Ie575f8561614529b7fd6245a24ffe9fdad5d8111 Reviewed-on: https://gerrit.instructure.com/98610 Reviewed-by: Clay Diffrient Tested-by: Jenkins QA-Review: David Tan Product-Review: Dan Minkevitch --- .../bundles/wiki_page_show.coffee | 22 ++++++++++++------- app/jsx/announcements/AnnouncementList.jsx | 2 +- app/models/discussion_topic.rb | 2 +- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/app/coffeescripts/bundles/wiki_page_show.coffee b/app/coffeescripts/bundles/wiki_page_show.coffee index 0a7a13f7034..4ac6f221854 100644 --- a/app/coffeescripts/bundles/wiki_page_show.coffee +++ b/app/coffeescripts/bundles/wiki_page_show.coffee @@ -4,13 +4,14 @@ require [ 'compiled/models/WikiPage' 'compiled/views/wiki/WikiPageView' 'compiled/util/markAsDone' + 'compiled/util/natcompare' 'react' 'react-dom' 'axios' 'jsx/announcements/AnnouncementList' 'instructure-ui/Spinner' 'compiled/jquery/ModuleSequenceFooter' -], ($, I18n, WikiPage, WikiPageView, MarkAsDone, React, ReactDOM, axios, AnnouncementList, { default: Spinner }) -> +], ($, I18n, WikiPage, WikiPageView, MarkAsDone, natcompare, React, ReactDOM, axios, AnnouncementList, { default: Spinner }) -> renderReactComponent = (component, target, props) -> ReactDOM.render( @@ -18,6 +19,11 @@ require [ document.querySelector(target) ) + sortByPosting = (a, b) -> + aPosted = a.delayed_post_at || a.posted_at + bPosted = b.delayed_post_at || b.posted_at + natcompare.strings(bPosted, aPosted) + $ -> $('#content').on('click', '#mark-as-done-checkbox', -> MarkAsDone.toggle(this) @@ -46,11 +52,11 @@ require [ if ENV.SHOW_ANNOUNCEMENTS renderReactComponent Spinner, '#announcements_on_home_page', {title: I18n.t('Loading Announcements'), size: 'small'} - axios.get("/api/v1/announcements?context_codes[]=course_#{ENV.COURSE_ID}&per_page=#{ENV.ANNOUNCEMENT_LIMIT || 3}&page=1") + axios.get("/api/v1/announcements?context_codes[]=course_#{ENV.COURSE_ID}&per_page=#{ENV.ANNOUNCEMENT_LIMIT || 3}&page=1&start_date=1900-01-01&end_date=2100-12-31") .then (response) => - renderReactComponent AnnouncementList, '#announcements_on_home_page', announcements: response.data.map (a) -> - id: a.id, - title: a.title, - message: a.message, - posted_at: a.posted_at, - url: a.url + renderReactComponent AnnouncementList, '#announcements_on_home_page', announcements: response.data.sort(sortByPosting).map (a) -> + id: a.id, + title: a.title, + message: a.message, + posted_at: a.delayed_post_at || a.posted_at, + url: a.url diff --git a/app/jsx/announcements/AnnouncementList.jsx b/app/jsx/announcements/AnnouncementList.jsx index 5e3ab3caabf..ecb08b17db1 100644 --- a/app/jsx/announcements/AnnouncementList.jsx +++ b/app/jsx/announcements/AnnouncementList.jsx @@ -14,7 +14,7 @@ define([ static propTypes = { announcements: React.PropTypes.arrayOf( React.PropTypes.shape({ - id: React.PropTypes.number.isRequired, + id: React.PropTypes.string.isRequired, title: React.PropTypes.string.isRequired, message: React.PropTypes.string.isRequired, posted_at: React.PropTypes.string.isRequired, diff --git a/app/models/discussion_topic.rb b/app/models/discussion_topic.rb index 36684faf72f..04f5441ed8c 100644 --- a/app/models/discussion_topic.rb +++ b/app/models/discussion_topic.rb @@ -564,7 +564,7 @@ class DiscussionTopic < ActiveRecord::Base def can_unlock? # Only used for course/account setting to lock replies to announcements - !is_announcement || (is_announcement && !self.course&.lock_all_announcements) + self.new_record? || !self.is_announcement || (self.is_announcement && !self.course&.lock_all_announcements) end def lock(opts = {})