Want students to print their database

Want students to print their database

by Doug Moody -
Number of replies: 15

I saw this discussed in the documentation, but never explained. Please help...

I created a database for students to enter definitions of words. On their demand, I want students to be able to print a formatted copy of their definitions. This will be used as a study guide for their personal use.

Is this possible? If so, can I create a button for them to do this?

Thank you.

Average of ratings: -
In reply to Doug Moody

Re: Want students to print their database

by Itamar Tzadok -

They can print what they see on the page via the browser print option. You can add a button to template to open the print dialogue:

<button id="printbutton" onclick="window.print();">PRINT</button>
You can add @media print css in the css tab of the activity to remove blocks, headers etc. or to add some styles to the printout. Something like:

@media print {

html, body {
margin: 0;
padding: 0;
width: 100%;
}

body {
background-color: White;
color: Black;
font-size: 75%;
}

/* Hide the side blocks. */
#block-region-side-pre {
display: none;
}
}
hth smile
In reply to Itamar Tzadok

Re: Want students to print their database

by Doug Moody -

Itamar,

Thank you for the quick response. I will try it today and report back here. I do hope it works, but will let you know.

That's what I love about Moodlers. They are SOO helpful! smile

In reply to Doug Moody

Re: Want students to print their database

by Doug Moody -

Itamar,

OOPS! Something bad happened when I tried out your script. The Add Entry Tab (see below) has disappeared in that course. I tried deleting the database, resetting the templates, putting in other code, purging caches. Everything I could think of, but the Add Entry tab is now missing in this course.

I even tried recreating the database assignment, but to no avail.

See the graphics below that help explain what is happening. 

What am I missing?


In reply to Doug Moody

Re: Want students to print their database

by Doug Moody -
Never mind. I found the solution here:


https://moodle.org/mod/forum/discuss.php?d=238064#p1067922


So I will do some more testing on your code and let you know how it works out.


Thanks again.


In reply to Doug Moody

Re: Want students to print their database

by Doug Moody -

Itamar,
I got the print button working, but the solution to format the printed output isn't working. I get the entire course page, including blocks, headers, etc. but all I want is just the list of definitions from the database that the student created. The idea is to make this a study guide.


I put your code in the CSS template, but have I missed something else?


I am almost there, but I am probably missing a fundamental step. Your help is most appreciated.

In reply to Doug Moody

Re: Want students to print their database

by Itamar Tzadok -

The css I posted is just a stub. It really depends on the theme you're using. Typically you would inspect the page html with the browser dev tools, find the container elements for the areas you wish to remove from the printout and add css rules display: none for these elements in the css tab under the @media print. smile

In reply to Itamar Tzadok

Re: Want students to print their database

by Doug Moody -

This might sound naive, but why is it so hard to create a simple report from the database? After all, that's a fundamental reason that data is stored in databases - so that we can get useful information out in reports!

There are options to delete a record, and add a record, so why not have  an additional option to print?

I was thinking that if I could even tie in to the Export option, I should be able to at least export the fields into a spreadsheet. That proves to me that the data is available. Now if I could just figure out how to print it in a more human readable form...

Just venting...

Thanks

In reply to Doug Moody

Re: Want students to print their database

by Itamar Tzadok -

Maybe because there is simply no such thing as one-size-fits-all report.

The Dataform module is better equipped than the Database for this purpose although some of relevant functions have not been given attention lately and are likely broken.

One currently broken option is the export to portfolio which allows for exporting entries to html file. I don't think that I will be able to fix that in the near future, unless someone commissions the work. But, it may be possible to simply add an option to send dataform view content directly into an html file. I'll look into that and if not too time consuming I'll add it to one of the upcoming releases.

A working option which got some attention and should work reasonably well is the pdf view. It's a view type which allows the user to export the view content into a pdf file. It is currently not included in the standard release of the Dataform but can be downloaded from git at https://github.com/itamart/moodle-dataformview_pdf.

smile

In reply to Itamar Tzadok

Re: Want students to print their database

by Doug Moody -

Itamar,


I would like some education about this please...

The "database" in Moodle: Is it a real database that exists separate from the main Moodle database?

If so, then how is it structured and what is it?

If not, then I am assuming that this data gets written into the main Moodle database, right? And if that is so, then how hard is it to just write a script that extracts the field data from the relevant table(s) in the database?

A php script is obviously extracting that data out in the export function and writing it as an Excel and ODS format. So why is it hard amplify that so that it exports in some other format besides Excel or ODS?


Lots of questions, I know, and likely the answers are "technical". But you seem to understand the framework of how this is all put together. Can you enlighten us common folk about how this works? Maybe someone who has some time can come up with a way to make this work

In reply to Doug Moody

Re: Want students to print their database

by Itamar Tzadok -
The "database" in Moodle: Is it a real database that exists separate from the main Moodle database?

No. The DB support for the Database module consists of several tables in the Moodle DB.

A php script is obviously extracting that data out in the export function and writing it as an Excel and ODS format. So why is it hard amplify that so that it exports in some other format besides Excel or ODS?

Excel and ODS formats and even CSV format are not that simple and require specialized php libraries. CSV is considered simplest but it is only an apparent simplicity. The content exported by CSV is simple text but the content that is stored in DB isn't always meaningful when presented as is in simple text. For example, the content of the time field is stored in DB as unix timestamp, and what you get in the export file if you don't convert that timestamp to a date/time string is not very useful. You can find plenty of disucssions about that down this forum. Another example is exporting the textarea content. You get all the html there which in a spreadsheet is not very useful, and you don't get the content of images, so you don't get everything which is also not very useful. And converting rich content to something that could work in a spreadsheet is not simple.

Other formats are less simple than spreadsheets. The simplest of those is probably html as it could be "what you see is what you export". But there is still the question of what exactly to export. For you the content of the export should include only entries. Others may want to export also blocks if included.

It is possible to write a script that would do that, but the development costs and further maintenance costs more. Someone needs to comission the work and the ongoing maintenance.

In some cases, workarounds are actually preferrable to features because they are cheaper and more flexible. You can be the master of your workaround. The CSS approach in the Database activity is one such workaround.

smile
In reply to Itamar Tzadok

Re: Want students to print their database

by Doug Moody -

Thank you. That was  a very reasonable response and made a lot of sense. It helps me (a non-programmer) to understand somewhat what you programmer types have to contend with when you make what seems to us as "simple"

So let me ask one more thing please...

Seeing as there is already an export function, how hard is it to make a button that simply "activates" the export function? Since I can't get a formatted report of the data, I would settle for an Excel output. I can teach the kids to format the Excel output. It might even be a good adjunct lesson for them!

Is this an "easy" thing to do? If so, how?

Thanks again.

In reply to Doug Moody

Re: Want students to print their database

by Itamar Tzadok -

The link to the Database export appears in the Administration block and looks something like:

http://your-moodle-path/mod/data/export.php?d=1

where 1 in d=1 is the instance id.

The problem is that the link goes to a designated export form where you select what and where to export. There is no shortcut to a default export. So it might not be very useful for you.

Your requirement is very reasonable but it would require development to add it to the Database module and given the already long wishlist, I'm not sure when it will get implemented, if at all. At any rate you need to add a tracker issue, if not already exists, to give this requirement a chance to be considered.

If you want CSV export(s) which you can predefine and then allow access by simple links, try the Dataform module.

hth smile 

In reply to Doug Moody

Re: Want students to print their database

by Itamar Tzadok -

Here is another possible workaround that should be generic enough to apply in any Database activity regardless of theme. It increases the page size in the background so might reach some limit if the page displays a large amount of content to begin with.

The solution requires css and javascript but both are fairly simple. The javascript adds a new element under body and copy into it the content of the main region. The css makes sure that only this element will be sent to the printer to the effect that only the activity title and list of entries will be printed. In the header section of the list template we can add a print a button as a shortcut to the browser window printing. In the footer section of the list template we add an inline script that calls the javascript function we added in the javascript tab. That's it.

If you use Windows 8 you can print to Microsoft XPS Ducoment Writer which sends the content to oxps file which you can then open for viewing with the integral xps viewer. 

If you use chrome browser the PRINT button will open the Chrome Print Preview and the content can be viewed there before printing.

hth smile

Javascript tab
function preparePrint() {
    var mainregion = document.getElementById('region-main');
    var printarea = document.createElement("DIV");
    printarea.className = 'printme';
    printarea.innerHTML = mainregion.innerHTML;
    document.body.insertBefore(printarea, document.body.childNodes[0]);
}


CSS tab
body .printme {
    display: none;
}

@media print {
     body > * {
          display: none;
     }
    
     body .printme {
          display: block;
     }
}

List template Header
<button id="printbutton" onclick="window.print()">PRINT</button>


List template Footer
<script>
preparePrint();
</script>


Average of ratings: Useful (2)
In reply to Itamar Tzadok

Re: Want students to print their database

by Itamar Tzadok -

Or better, you can omit the list template footer described above and change the list tempalte header part to

<button id="printbutton" onclick="preparePrint();window.print()">PRINT</button>

One more thing. The region-main element may still be theme dependent, so you need to inspect the page to make sure you refer to the correct element. Alternately, you can wrap the content of the list view with an identified element and use that element instead of region-main. To do that you can open a div element in the list template header

<div id="myListContent">

and close it in the list template footer.

</div>

and of course change the respective line in the javascript to

    var mainregion = document.getElementById('myListContent');

Note that since the html in the header and footer parts of the template is effectively broken, you will have to turn off the editor when editing the template so that it is preserved.

hth smile

Average of ratings: Useful (2)
In reply to Itamar Tzadok

Re: Want students to print their database

by ryan sanders -

very cool setup there itmar!

it looks like you are creating a <div id="something">this is database output</div>  element around the database output on page. and then hiding the entire page. expect for the <div> you created within the database template. 

err i take that back... you are replacing the entire page with what is in the <div id="something"> </div>    and wiping it all out not just hiding it!