+ Post New Thread
Results 1 to 9 of 9
Coding Thread, PaperCut Code - is this correct? in Coding and Web Development; Hi Edugeekers, I'm looking to use a javascript in PaperCut to restrict students from colour printing when Staff are not ...
  1. #1
    Mr.Ben's Avatar
    Join Date
    Jan 2008
    Location
    A Pirate Ship
    Posts
    942
    Thank Post
    182
    Thanked 157 Times in 126 Posts
    Blog Entries
    2
    Rep Power
    65

    PaperCut Code - is this correct?

    Hi Edugeekers,

    I'm looking to use a javascript in PaperCut to restrict students from colour printing when Staff are not in the classroom.

    So far I've managed this:

    Code:
    //Script to restrict students colour printing to lessons only.
    function getMinutesSinceMidnight (hours, minutes) {
    	return 60 * hours + minutes;
    }
    
    function printJobHook(inputs, actions) {
    	//Variable for Job Submission time
    	var jobtime = getMinutesSinceMidnight (hours, minutes);
    	//Variables to set the start and end times here
    	var schoolstart = getMinutesSinceMidnight(9, 0);
    	var schoolend = getMinutesSinceMidnight(15, 45); 
    	var breakstart = getMinutesSinceMidnight(10, 45);
    	var breakend = getMinutesSinceMidnight(11, 0);
    	var lunchstart = getMinutesSinceMidnight(12, 45);
    	var lunchend = getMinutesSinceMidnight(13, 45);
            //Variables to define times to deny jobs
    	var schoolcloseddeny = (schoolstart <= jobtime && jobtime <= schoolend);
    	var breakdeny = (breakstart <= jobtime && jobtime <= breakend);
    	var lunchdeny =(lunchstart <= jobtime && jobtime <= lunchend);
      
    	
            if (inputs.user.isInGroup("Printing_Students")) && (schoolcloseddeny || breakdeny || lunchdeny) {
    	actions.job.cancel();
    	// send message to the user's client tool
    	actions.client.sendMessage('No Colour Jobs can be printed outside of timetabled periods');
    	// log a message for debugging
    	actions.log.debug('Canceling job from user ' + inputs.job.username + 'because of colour print time restrictions');
    	}
    }
    Have I got anything wrong here?

    All help is much appreciated!

    Ben
    Last edited by Mr.Ben; 25th March 2013 at 09:50 PM.

  2. #2

    nephilim's Avatar
    Join Date
    Nov 2008
    Location
    Dunstable
    Posts
    11,918
    Thank Post
    1,626
    Thanked 1,893 Times in 1,407 Posts
    Blog Entries
    2
    Rep Power
    429
    It looks right to a point, but you need to declare all variables.

  3. Thanks to nephilim from:

    Mr.Ben (25th March 2013)

  4. #3
    Mr.Ben's Avatar
    Join Date
    Jan 2008
    Location
    A Pirate Ship
    Posts
    942
    Thank Post
    182
    Thanked 157 Times in 126 Posts
    Blog Entries
    2
    Rep Power
    65
    I'm working from this example:

    PaperCut KB | Script for Time-Based Printer Access

    So I am assuming that PaperCut has defined 'hours' and 'minutes' as global variables, or are there other ones I need to declare?

    This is my first bit of Javascript for almost ten years, so be forgiving :-)
    Last edited by Mr.Ben; 25th March 2013 at 10:31 PM.

  5. #4

    nephilim's Avatar
    Join Date
    Nov 2008
    Location
    Dunstable
    Posts
    11,918
    Thank Post
    1,626
    Thanked 1,893 Times in 1,407 Posts
    Blog Entries
    2
    Rep Power
    429
    Your schoolstart, schoolend etc. All should be declared if I am not mistaken.

  6. Thanks to nephilim from:

    Mr.Ben (25th March 2013)

  7. #5

    plexer's Avatar
    Join Date
    Dec 2005
    Location
    Norfolk
    Posts
    13,458
    Thank Post
    646
    Thanked 1,614 Times in 1,444 Posts
    Rep Power
    419
    Not neccessarily the case that variables need to be defined before using them for the first time.

    Ben

  8. 2 Thanks to plexer:

    Mr.Ben (25th March 2013), nephilim (25th March 2013)

  9. #6
    Mr.Ben's Avatar
    Join Date
    Jan 2008
    Location
    A Pirate Ship
    Posts
    942
    Thank Post
    182
    Thanked 157 Times in 126 Posts
    Blog Entries
    2
    Rep Power
    65
    Just double checked with the power of Google:

    JavaScript Variables

    @plexer gets the bonus points!
    Last edited by Mr.Ben; 25th March 2013 at 10:22 PM.

  10. #7

    plexer's Avatar
    Join Date
    Dec 2005
    Location
    Norfolk
    Posts
    13,458
    Thank Post
    646
    Thanked 1,614 Times in 1,444 Posts
    Rep Power
    419
    Same in vbscript you can decalre them by assigning a value but if you use option explicit then you have to declare variables before you can assign values to them.

    Ben

  11. #8
    Mr.Ben's Avatar
    Join Date
    Jan 2008
    Location
    A Pirate Ship
    Posts
    942
    Thank Post
    182
    Thanked 157 Times in 126 Posts
    Blog Entries
    2
    Rep Power
    65
    Just tried to validate it in Papercut and it failed -

    if (inputs.user.isInGroup("Printing_Students")) && (schoolcloseddeny || breakdeny || lunchdeny) {

    needs an extra pair of brackets:

    if ((inputs.user.isInGroup("Printing_Students")) && (schoolcloseddeny || breakdeny || lunchdeny)) {

    I will test in the morning to see results!
    Last edited by Mr.Ben; 25th March 2013 at 11:20 PM.

  12. #9
    Mr.Ben's Avatar
    Join Date
    Jan 2008
    Location
    A Pirate Ship
    Posts
    942
    Thank Post
    182
    Thanked 157 Times in 126 Posts
    Blog Entries
    2
    Rep Power
    65
    Script now works after a couple of other changes to the logic (the schoolcloseddeny var has been changed to an 'or' statement and I did have to define hours and minutes)

    Code:
    //Script to restrict students printing to lessons only. By Ben Ford 
    function getMinutesSinceMidnight(hours, minutes) {
      return 60 * hours + minutes;
    }
    
    
    function printJobHook(inputs, actions) {
      //Variable for Job Submission time
      var hours = inputs.job.date.getHours();
      var minutes = inputs.job.date.getMinutes();
      var jobtime = getMinutesSinceMidnight (hours, minutes);
      //Variables to set the start and end times in 24 hour format
      var schoolstart = getMinutesSinceMidnight(9, 0); //9am
      var schoolend = getMinutesSinceMidnight(15, 45); //3.45pm
      var breakstart = getMinutesSinceMidnight(10, 45); //10.45am
      var breakend = getMinutesSinceMidnight(11, 0); //11am
      var lunchstart = getMinutesSinceMidnight(12, 45); //12.45pm
      var lunchend = getMinutesSinceMidnight(13, 45); //1.45pm
      //Variables to define times to deny jobs
      var schoolcloseddeny = (schoolstart >= jobtime || jobtime >= schoolend);
      var breakdeny = (breakstart <= jobtime && jobtime <= breakend);
      var lunchdeny =(lunchstart <= jobtime && jobtime <= lunchend);
      
    
      if ((inputs.user.isInGroup("Printing_Students")) && ( schoolcloseddeny || breakdeny || lunchdeny )) {
        actions.job.cancel();
        //Send message to the user's client tool
        actions.client.sendMessage('No Colour jobs can be printed outside of timetable periods');
        //Log a message for debugging
        actions.log.debug('Canceling job from user ' + inputs.job.username + ' because of colour print time restrictions');
      }
    }
    @papercut - One to add in as a recipe?
    Last edited by Mr.Ben; 26th March 2013 at 07:03 PM.

SHARE:
+ Post New Thread

Similar Threads

  1. Vmware , is this correct ?
    By 2097 in forum Thin Client and Virtual Machines
    Replies: 27
    Last Post: 19th June 2012, 04:19 PM
  2. Is this correct?
    By MattDLEA in forum Licensing Questions
    Replies: 8
    Last Post: 9th December 2011, 02:42 PM
  3. Migrating files to new server using DFS - is this correct?
    By iSteve in forum Windows Server 2008
    Replies: 2
    Last Post: 12th July 2011, 12:42 PM
  4. Is This A Network Problem ?
    By Pear in forum Windows
    Replies: 21
    Last Post: 12th December 2005, 03:01 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •