Filter database records according to Date

Filter database records according to Date

by Shane Symonds -
Number of replies: 18
Hello all: I am trying to set up a database to display announcements (because the forum views on the formats do not let me categorise the forum posts - they just display as one big long list...) so I have created some fields with Subject, Message and the like. BUT...how do I get it do NOT display messages past a certain date, ie their expiry date?

In short, I want an announcement database that displays like the social forum but with some extra fields, such as Year Level.

Any ideas? I have a field for Start Date and End Date and can display those in the List View, but it will theoretically hold those forever, whereas some of them need only have a life of a single day.

Looking forward to some ideas...Regards, Shane
Average of ratings: -
In reply to Shane Symonds

Re: Filter database records according to Date

by Itamar Tzadok -
Have you considered using separate announcement forums for the different categories? Then you could enable RSS for each forum and include the feeds in a remote RSS feed block and set the title of each feed to the category name and the announcements should be displayed in that one block but divided to categories. smile
In reply to Itamar Tzadok

Re: Filter database records according to Date

by Chris Liang-Vergara -
@Shane
@Itamar

Great to see this discussion on categories for announcements! I've been searching for a similar solution too - my situation sounds exactly like Shane's.

I have considered the different forums feeding into one - however I have an issue - our announcements sometimes apply only to one class (Ex: freshman) but other times they apply to multiple or all grade levels. Thus, if someone wants an announcement about a club to show up on all 4 grade level newsfeeds, he/she would have to post it 4 different times = no practical.

Ideally, a staff member would be able to fill out a text box, set the date of announcement, and check boxes off next to each grade level he/she would like for it to appear it. Fast forward to afternoon announcements, and the 9th grade advisory teacher would be able to simply pull up the 9th grade feed and see any and everything that applies - while 10th-12th grade announcements are filtered out.

Any ideas? smile
In reply to Itamar Tzadok

Re: Filter database records according to Date

by Shane Symonds -

Itamar : Thanks for the suggestion. I have tried this just now, thinking that I might hav emissed something BUT...

We are running v1.9.8 and RSS blocks only go to the side columns, yes? So 100 Notices is not going to format too well...Cron only runs every 30 mins or so, and even if I run it more regularly, it is still not a totally current display.

If I could get the RSS into the centre, maybe I would keep looking. Ideas?

Thanks again, Shane

In reply to Shane Symonds

Re: Filter database records according to Date

by Itamar Tzadok -
Shouldn't be too difficult to get the RSS into the center. See this post for links to two possible solutions.

You can also use the database for entering those announcements and generating manually the RSS (so as to update upon changes and not only new entries as the database RSS currently works) which you can then display wherever you want using one of the solutions described in the link above. smile
In reply to Itamar Tzadok

Re: Filter database records according to Date

by Shane Symonds -
Itamar: Thanks for the idea re using RSS out of the forums and combining them. I was happy with that, except...

1. The beauty of the "normal" forum is that the message displays immediately, whereas a combo RSS block, with all forum RSS feeds, is reliant upon Cron happening. A message added at 0800 may not display in the summary until 0830 or whatever...So unless Cron updates all the time, or is manually triggered all the time...

2. I need to also figure out the formatting of the RSS and have posted to that forum. Is it dependant on the theme?

Using Flexpage I was able to get the RSS block into the centre.

I am still looking for a solution to this. Regards, Shane
In reply to Shane Symonds

Re: Filter database records according to Date

by Chris Liang-Vergara -
I messed around with the database for an extended period of time yesterday – it looks like I might have something that will suit my needs (not perfect, but pretty good). I don’t have any way to have the older notices “drop-off” – but by using the date field and having users fill it in according to the “day it applies to”, the most recent announcements will stay on top. The year level is filters by a basic checkbox field.

The good:
-No need to repost reminders about an announcement – it will stay near the top until more recent news overtake it

The bad:
-Need to set some limit to how “in advance” an announcement can be posted – if people post a bunch of news for the next month, it will obviously overcrowd the top of the newsfeed.

For the sorting
-The user just has to open up the database and under advanced search you can order the entries by date – and just check the box next to the grade level you want to see.

I included fields for a small picture and two file attachments – which will be perfect for when we have permission slips or whatever else that needs to be easily distributed. The picture will go a long way for imaging/logos – hopefully each club/team can grab a simple image to post with every update – so users can easily find what their looking for in the list.

I attached a copy of the present to this post – let me know what you think. I’ll try to get some of my summer school teachers to use it for few weeks to tweak a bit more.

Thanks!
Chris

(oh - I only formatted the list, single, and add templates - need to tweak the rss)
In reply to Chris Liang-Vergara

Re: Filter database records according to Date

by Shane Symonds -

Chris ( and others) : I will attach a screenshot of what I have done with my own database but it is the date thing which is most annoying. The screen does not show the start and end date fields (start and end) but they are present when adding an entry. I would have expected to be able to create some code/hack which only displays (in list view) when end is less than Today; ie it has expired.

Could that be a possibility?

My entries are grouped according to Year Level.

Regards, Shane

Attachment db-sample-1.jpg
In reply to Shane Symonds

Re: Filter database records according to Date

by Itamar Tzadok -
Try this.

Add to the footer section in the list template the following code:

<script type="text/javascript">
var today=new Date();
var myList=document.getElementById('itemList');
var myRows=myList.firstChild.rows;
for (var i=1;i<myRows.length;i++){
var endDate=new Date(myRows[i].cells[8].innerHTML);
if (endDate.getTime()>today.getTime()) myRows[i].className='iamexpired';
}
myList.style.display='';
</script>
This code assumes that the table of items is called 'itemlist', that it has <tbody>, that the first row is a header row, and that the end date is in cell 8. Of course, you can select another name and put the end date in another cell, in which case simply change the corresponding parts of the code. The table should also be set to display:none so that it will be displayed only after the filtering.

The code does traverses the rows in the display and checks whether endDate of a record is bigger than today, and if so it assigns the css class 'iamexpired' to that row.

Now add to the css template the following class definition:

.iamexpired{display:none;}

Clear cache and try. With a bit of luck it will work. smile

(P.S. goes without saying that all entries should reside in one table, but said just in case smile )
In reply to Itamar Tzadok

Re: Filter database records according to Date

by Shane Symonds -
Itamar: Thanks for the advice but I am going to have to profess to being useless with both Javascript and databases!

1. The table of items? in the MySQL database? or within the actual Moodle database as displayed in the database module GUI? My database is called "Notices" so is that what goes in there?

2. The end date is in cell 8? Do you mean the 8th field in the Moodle database activity? or is this again in the MySQL?

3. The CSS template you refer to is that in Database activity, yes?

I have tried to get this to work at home on a local install using MAMP but have had no luck deciphering the MySQL data. Where do I begin looking? The database activity is not in the Course folder in my moodledata folder so where does it store itself?

Sorry for the ignorance and I get the feeling that the javascript will do what I need but I just can't figure out how to use it!

Regards, Shane
In reply to Shane Symonds

Re: Filter database records according to Date

by Itamar Tzadok -
That's a local solution in the database activity module (and btw it's time to change the name of this module to avoid the confusion; I suggest dataform).

  1. That's the table of items in the list view which you set up in the list view template. You have to disable the editor for doing that and once it is done you cannot use the editor for modification but will have to switch to text mode. See this thread for related discussion.
  2. Given the table, cell 8 is the ninth td tag in the repeated entry section which is also the ninth column (the first td tag is cell 0). Of course it can be any column you want. Also you don't have to display it, in which case the td tag may look like:

    <td style="display:none;">||EndDate||</td>

  3. Yes.
All these changes should be done in the database activity templates, not in the Moodle database. Try again and let me know how it went. smile
In reply to Itamar Tzadok

Re: Filter database records according to Date

by Shane Symonds -
Itamar: I really appreciate the help you are giving me. Thanks. I have still had no luck and tried with a new database using the following code: Repeated Entry
Footer I have also placed the css as suggested. Any ideas? Is it something minor, like a number in the wrong place? The table as displayed should be fairly simple with probably 3 columns (Year | Subject | Message). There are other fields but I am just trying to get the old material to not display... Regards, Shane
In reply to Itamar Tzadok

Re: Filter database records according to Date

by Shane Symonds -

Sorry for the other post, Itamar. I pasted the code in and it displayed funny. Here is a screenshot of the code. Hope that helps.

Shane

Attachment code1.jpg
In reply to Shane Symonds

Re: Filter database records according to Date

by Itamar Tzadok -
Much better, thank you.

Now, in the Repeated entry box delete the first line (<div class="defaulttemplate"><table cellpadding="5">) and the last line (</table><div><hr />).

Then, in the Header box add the line:

<table id="itemlist" cellpadding="5" style="display:none;"><tbody>

and in the Footer box add before the script tag the line:

</tbody></table>

Remember to disable editor before making any changes.

Let me know if that worked. smile



In reply to Itamar Tzadok

Re: Filter database records according to Date

by Shane Symonds -
Itamar: I am getting determined to get to the bottom of this! Have you actually got it working for you?

Anyway, I can't get it. The List table seems to hit the display:none in the header area and that kills the display totally. If I remove that code, the whole thing displays with no filtering.

I have followed the instructions to the letter but of course, I have been wrong before. Does the name of the variable endDate matter? If I have the field called end in my table? Does the javascript rely on the database fields being called anything specific, or does it make its own naming in the variables?

Regards, Shane
In reply to Shane Symonds

Re: Filter database records according to Date

by Itamar Tzadok -
Ah, I see now that in the javascript you refer to the table as Notices. So in the Header box you should have the line:

<table id="Notices" cellpadding="5" style="display:none;"><tbody>

smile


In reply to Shane Symonds

Re: Filter database records according to Date

by Itamar Tzadok -
Also, the EndDate field is actually in cell 4 (first cell is 0), so this line in your code

var EndDate=new Date(myRows[i].cells[5].innerHTML);

should be

var EndDate=new Date(myRows[i].cells[4].innerHTML);

smile
In reply to Itamar Tzadok

Re: Filter database records according to Date

by Shane Symonds -

Itamar : getting closer all the time...But still nothing. I get 5 notices, now, but the new ones are not displaying.

Code is attached. It is very simple at the moment so I must be missing a simple thing, like a comma or something!

Regards and many thanks for your time so far! Shane

In reply to Shane Symonds

Re: Filter database records according to Date

by Itamar Tzadok -
Yes, well the javascript is local and filters only what is already displayed. It does not query the Moodle database. If you set the default sort on timeadded and descending you will have the most recently added notices displayed and these will be filtered. But in any case you should probably display as many items per page as possible. smile