Last active
July 28, 2020 04:33
-
-
Save x-magic/70f26bdc5217e77fa42ffab010ebba05 to your computer and use it in GitHub Desktop.
Make Mahara diary marking easier. You will need Greasemonkey/Tampermonkey to make this work. Click "Raw" button on the top-right corner should prompt you to install this script.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Mahara Diary Assistant | |
// @namespace https://mahara.infotech.monash.edu.au/mahara/ | |
// @version 0.5.2 | |
// @description Make diary marking easier | |
// @author Bill Gong <[email protected]> | |
// @match https://mahara.infotech.monash.edu.au/mahara/* | |
// @require https://cdnjs.cloudflare.com/ajax/libs/datejs/1.0/date.min.js | |
// @updateURL https://gist.githubusercontent.com/x-magic/70f26bdc5217e77fa42ffab010ebba05/raw/mahara-diary-assistant.user.js | |
// @downloadURL https://gist.githubusercontent.com/x-magic/70f26bdc5217e77fa42ffab010ebba05/raw/mahara-diary-assistant.user.js | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
var timer; //Timer global variable | |
$(document).ready(function(e) { | |
if (/\/mahara\/$/.test(location.pathname)) { | |
// Construct home page dashboard text box | |
var dashboard_editable_html = $('#main-column-container .dashboard-editable')[0].outerHTML; | |
var dashboard_diary_helper = '<div id="diary-iframe-target"><div><h3>Open following links in new window/tab: </h3></div><div><textarea id="diary-url-list" style="width:100%;height:200px;font-family:monospace;" placeholder="Any Mahara link here, one url per line"></textarea></div><div><button id="diary-open-urls">Open all links</button></div></div>'; | |
$('#main-column-container').html(dashboard_diary_helper + dashboard_editable_html); | |
//When button clicked, open all diaries page | |
$('#diary-open-urls').click(function(e){ | |
var diary_urls = $("#diary-url-list").val().split("\n"); | |
diary_urls = diary_urls.filter(function(e) {return e}); //Filter empty lines out of the array | |
$.each(diary_urls, function(n, target_diary_url) { | |
window.open(target_diary_url, '_blank'); | |
}); | |
}); | |
} | |
if (/\/mahara\/view\/view.php\?id=\d+$/.test(location.pathname + location.search)) { | |
//On diary-page page | |
var page_header_text = $('h1.page-header'); | |
if (/[Rr]eflecti(on|ve)|[Dd]iar(y|ies)/.test(page_header_text.text())) { | |
var potential_diary = $('div.post-heading a').eq(0); | |
//Matching diary titles | |
if (/[Ww]eek(\s|-)?(\d{1,2})/.test(potential_diary.text())) { | |
var diary_url = potential_diary.attr('href'); | |
var timer_banner = '<div id="diary-timer-banner" class="alert alert-info">Latest diary found! <span style="float: right;"><a href="' + diary_url + '">Click here</a> to go to the latest diary</span></div>'; | |
if (document.referrer == "https://mahara.infotech.monash.edu.au/mahara/") { | |
//Only pages come from homepage should be auto-redirected | |
timer_banner = '<div id="diary-timer-banner" class="alert alert-warning">Latest diary found! You will be redirected in 5 seconds<a href="#" id="cancel-timer" style="float: right;">Cancel auto redirection</a></div>'; | |
timer = setTimeout(function(){ | |
window.location.replace(diary_url); | |
}, 5000); | |
} | |
$('div#messages').append(timer_banner); | |
} else { | |
//If no diary titles are found, do nothing | |
console.log("Current page title: " + page_header_text.text().trim()); | |
var not_found_banner = '<div id="not-found-banner" class="alert alert-danger">It seems this page does not contain any diary! <span style="float: right;">This message will self-destruct in 5 seconds</span></div>'; | |
$('div#messages').append(not_found_banner); | |
$('div#not-found-banner').delay(5000).fadeOut(function(){ | |
$(this).remove(); | |
}); | |
} | |
//Dismiss timer link | |
$('a#cancel-timer').click(function(e){ | |
e.preventDefault(); | |
if (typeof timer !== "undefined") { | |
clearTimeout(timer); | |
} | |
$('div#diary-timer-banner').fadeOut(function(){ | |
$(this).remove(); | |
}); | |
}); | |
} | |
} | |
if (/\/mahara\/artefact\/artefact.php\?artefact=\d+&view=\d+$/.test(location.pathname + location.search)) { | |
// On diary entry page | |
// Widen the content and make diary font larger (easier to read) | |
$('#main-column-container .col-md-9').css("width", "100%"); | |
$('div.postcontent').css('zoom', 1.5); | |
var deadlines = [ // 2020 S1 | |
Date.parse('2020-08-02 17:00'), // Week 0 | |
Date.parse('2020-08-09 17:00'), // Week 1 | |
Date.parse('2020-08-16 17:00'), // Week 2 | |
Date.parse('2020-08-23 17:00'), // Week 3 | |
Date.parse('2020-08-30 17:00'), // Week 4 | |
Date.parse('2020-09-06 17:00'), // Week 5 | |
Date.parse('2020-09-13 17:00'), // Week 6 | |
Date.parse('2020-09-20 17:00'), // Week 7 | |
Date.parse('2020-10-11 17:00'), // Week 8 | |
Date.parse('2020-10-18 17:00'), // Week 9 | |
Date.parse('2020-10-25 17:00'), // Week 10 | |
Date.parse('2020-11-01 17:00'), // Week 11 | |
Date.parse('2020-11-08 17:00') // Week 12 | |
]; | |
var week_number = $('h1.page-header span.subsection-heading').text().match(/[Ww][Ee][Ee][Kk][\s\-_]?(\d{1,2})/); | |
if (week_number !== null) { | |
week_number = parseInt(week_number[1]); | |
var deadline = deadlines[week_number]; | |
var date_string = $('div.postdetails.metadata'); | |
var target_date_str = date_string.text().match(/Posted by (.*) on (.*)/)[2]; | |
var updated_datetime_str = date_string.text().match(/Last updated (.*)/); | |
if (updated_datetime_str !== null) { | |
//When student submit diary with modification | |
target_date_str = updated_datetime_str[1]; | |
} | |
var target_date = Date.parse(target_date_str); | |
var second_difference, diary_time_banner; | |
if (target_date > deadline) { | |
//Diary is overdue | |
second_difference = target_date - deadline; | |
diary_time_banner = '<div id="not-found-banner" class="alert alert-danger">Week ' + week_number + ' - It seems this diary is overdue! <span style="float: right;">Late for ' + humanReadableTime(second_difference/1000) + '</span></div>'; | |
} else { | |
//Diary is on time | |
second_difference = deadline - target_date; | |
diary_time_banner = '<div id="not-found-banner" class="alert alert-success">Week ' + week_number + ' - This diary is submitted on time! <span style="float: right;">' + humanReadableTime(second_difference/1000) + ' before deadline</span></div>'; | |
} | |
$('div#messages').append(diary_time_banner); | |
} else { | |
console.log("Current page title: " + $('h1.page-header span.subsection-heading').text().trim()); | |
console.log("Cannot parse week number, probably not a diary page, will not continue. "); | |
} | |
//Add rubric to the comment section | |
$('#add_feedback_heading').after("<div id=\"diary-rubric\" style=\"padding-bottom:20px\"><p>Grades for diaries over the past fortnight:</p><table style=\"border-collapse:collapse\"><tbody><tr><td style=\"border:1px solid black;padding:5px;\"></td><td style=\"border:1px solid black;padding:5px;\"><b>Beyond our Expectations</b></td><td style=\"border:1px solid black;padding:5px;\"><b>Above Expectations</b></td><td style=\"border:1px solid black;padding:5px;\"><b>Meets Expectations</b></td><td style=\"border:1px solid black;padding:5px;\"><b>Approaching Expectations</b></td><td style=\"border:1px solid black;padding:5px;\"><b>Below Expectations</b></td><td style=\"border:1px solid black;padding:5px;\"><b>No submission</b></td></tr><tr><td style=\"border:1px solid black;padding:5px;\"><b>Points</b></td><td style=\"border:1px solid black;padding:5px;\"><b>8</b></td><td style=\"border:1px solid black;padding:5px;\"><b>7</b></td><td style=\"border:1px solid black;padding:5px;\"><b>6</b></td><td style=\"border:1px solid black;padding:5px;\"><b>4</b></td><td style=\"border:1px solid black;padding:5px;\"><b>1</b></td><td style=\"border:1px solid black;padding:5px;\"><b>0</b></td></tr><tr><td style=\"border:1px solid black;padding:5px;\"><b>Reflective Thinking (Evaluation) & Analysis</b></td><td style=\"border:1px solid black;padding:5px;\">The reflection is a <b>meaningful, critical and thoughtful</b> analysis of the learning experience encountered about themselves & their interactions with team members / project / mentors / industry / client rep.</td><td style=\"border:1px solid black;padding:5px;\">The reflection is an <b>in-depth (meaningful)</b> analysis of the learning experience encountered about themselves & their interactions with team members / project / mentors / industry / client rep.</td><td style=\"border:1px solid black;padding:5px;\">The reflection is an <b>analysis</b> of the learning experience encountered about themselves & their interactions with team members / project / mentors / industry / client rep.</td><td style=\"border:1px solid black;padding:5px;\">The reflection <b>attempts to demonstrate</b> their learning and thinking about themselves but is vague and / or unclear.</td><td style=\"border:1px solid black;padding:5px;\">The reflection <b>does not address the student’s thinking</b> and / or learning about themselves.The reflection does not move beyond a description of the learning experience.</td><td style=\"border:1px solid black;padding:5px;\">Reflective diary entry is <b>not submitted or overdue.</b></td></tr><tr><td style=\"border:1px solid black;padding:5px;\"><b>Points</b></td><td style=\"border:1px solid black;padding:5px;\"><b>2</b></td><td style=\"border:1px solid black;padding:5px;\"><b>1.5</b></td><td style=\"border:1px solid black;padding:5px;\"><b>1</b></td><td style=\"border:1px solid black;padding:5px;\"><b>.5</b></td><td style=\"border:1px solid black;padding:5px;\"><b>0</b></td><td style=\"border:1px solid black;padding:5px;\"><b>0</b></td></tr><tr><td style=\"border:1px solid black;padding:5px;\"><b>Action Plan</b></td><td style=\"border:1px solid black;padding:5px;\"><b>Constructively and critically</b> review one’s own practices, with a view to meaningfully improving future practice. A range of strategies and techniques stated to draw upon, with thought given to how they might work.</td><td style=\"border:1px solid black;padding:5px;\"><b>Constructively review</b> one’s own practices, with a view to improving future practice. <b>A range of strategies and techniques</b> stated to draw upon.</td><td style=\"border:1px solid black;padding:5px;\"><b>Constructively review</b> one’s own practices, with a view to improving future practice, <b>with some strategies and techniques</b> stated to draw upon.</td><td style=\"border:1px solid black;padding:5px;\">Actions plan <b>stated. but with no real thought</b> about past experiences etc.</td><td style=\"border:1px solid black;padding:5px;\">Though action is required, The reflection <b>does not articulate any actions</b> to be undertaken.</td><td style=\"border:1px solid black;padding:5px;\">Reflective diary entry is <b>not submitted or overdue</b>.</td></tr></tbody></table><p></p></div><div><p>OR <button id=\"diary-send-noted-comment\">Send \"Noted\" Comment</button></p></div>"); | |
$('#diary-rubric td').click(function(e) { | |
if($(this).css('background-color') != "rgb(173, 255, 47)") { | |
$(this).css('background-color', 'greenyellow'); | |
} else { | |
$(this).css('background-color', ''); | |
} | |
$(this).attr('style', 'border:1px solid black;padding:5px;background-color:' + $(this).css('background-color')); | |
tinymce.get('add_feedback_form_message').getBody().innerHTML = $('#diary-rubric')[0].outerHTML; | |
}); | |
//Allow one-click "noted" comment | |
$('#diary-send-noted-comment').click(function(e) { | |
tinymce.get('add_feedback_form_message').getBody().innerHTML = "Your diary is read. Feedback for both entries will be attached to next diary."; | |
$('#add_feedback_form_submit').trigger('click'); | |
}); | |
} | |
/** | |
* Translates seconds into human readable format of seconds, minutes, hours, days, and years | |
* From https://stackoverflow.com/a/34270811/11296357 | |
* | |
* @param {number} seconds The number of seconds to be processed | |
* @return {string} The phrase describing the the amount of time | |
*/ | |
function humanReadableTime (seconds) { | |
var levels = [ | |
[Math.floor(seconds / 31536000), 'years'], | |
[Math.floor((seconds % 31536000) / 86400), 'days'], | |
[Math.floor(((seconds % 31536000) % 86400) / 3600), 'hours'], | |
[Math.floor((((seconds % 31536000) % 86400) % 3600) / 60), 'minutes'], | |
[(((seconds % 31536000) % 86400) % 3600) % 60, 'seconds'], | |
]; | |
var returntext = ''; | |
for (var i = 0, max = levels.length; i < max; i++) { | |
if ( levels[i][0] === 0 ) continue; | |
returntext += ' ' + levels[i][0] + ' ' + (levels[i][0] === 1 ? levels[i][1].substr(0, levels[i][1].length-1): levels[i][1]); | |
}; | |
return returntext.trim(); | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment