Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. javascript:
  2.  
  3. /* School name */
  4. var schoolName;
  5.  
  6. /* Dictionary defining unique schools along with optional predefines */
  7. var uniqueSchools = {
  8.     "Straighterline": "See exam notes",
  9.     "Western Governors University": "You will have EXAM_TIME to take your exam today. You're only allowed to use a dry erase board and a calculator. " +
  10.         "You have 0 breaks during the exam. Do you understand these rules, and will you be using these materials?",
  11.     "East Carolina University(UNC Online)": "See exam notes",
  12.     "*Internet Testing Systems (ETS)*": "See exam notes",
  13.     "*Carson, Dunlop & Associates LTD*": "See exam notes"
  14. };
  15.  
  16. /* Exam name and exam time, in minutes */
  17. var examName;
  18. var examTime;
  19.  
  20. /* If this test has breaks */
  21. var hasBreaks;
  22.  
  23. /* If this test requires destruction of scratch paper */
  24. var destroyScratch;
  25.  
  26. /* Exam confirmation message */
  27. var confirmation;
  28.  
  29. /* Exam rules message */
  30. var rules;
  31.  
  32. /* Exam resources message */
  33. var resources;
  34.  
  35. /* Confirmation HTML */
  36. var confDesc = "<dt id='confDesc'>Confirmation</dt>";
  37. var confText = "<dd id='confText'> <a id='confCopyBtn' class='js-tooltip-copy text-cursor' title='Copy Confirmation' data-toggle='tooltip'><span class='fa fa-clipboard'> </span></a></dd>";
  38.  
  39. /* Rules HTML */
  40. var rulesDesc = "<dt id='rulesDesc'>Rules</dt>";
  41. var rulesText = "<dd id='rulesText'> <a id='rulesCopyBtn' class='js-tooltip-copy text-cursor' title='Copy Rules' data-toggle='tooltip'><span class='fa fa-clipboard'> </span></a></dd>";
  42.  
  43. /* Resources HTML */
  44. var resDesc = "<dt id='resDesc'>Resources</dt>";
  45. var resText = "<dd id='resText'> <a id='resCopyBtn' class='js-tooltip-copy text-cursor' title='Copy Resources' data-toggle='tooltip'><span class='fa fa-clipboard'> </span></a></dd>";
  46.  
  47. /* Selects an element's text and copies it to the clipboard */
  48. function selectText(element) {
  49.     var range, selection;
  50.     var textNode = element.first().contents()[0];
  51.     if (document.selection) {
  52.         range = document.body.createTextRange();
  53.         range.moveToElementText(confMessage);
  54.         range.select();
  55.     } else if (window.getSelection) {
  56.         selection = window.getSelection();
  57.         range = document.createRange();
  58.         range.selectNodeContents(textNode);
  59.         selection.removeAllRanges();
  60.         selection.addRange(range);
  61.     }
  62.     document.execCommand("copy");
  63.     selection.removeAllRanges();
  64. };
  65.  
  66. /* Determines if a school is unique based on school name */
  67. function determineUniqueSchool(schoolName) {
  68.     return schoolName in uniqueSchools;
  69. }
  70.  
  71. /* Formats and returns the exam confirmation string */
  72. function getConfirmationString(examName) {
  73.     examName = examName.replace("VIP", "");
  74.     includeExam = examName.match(/exam|test/ig) ? "" : "exam";
  75.     return `My notes say you\'re scheduled for your ${examName} ${includeExam} today. Is this correct?`;
  76. }
  77.  
  78. /* Formats and returns the exam rules string */
  79. function getRulesString() {
  80.    return `You will have ${examTime} to take your exam, with ${(hasBreaks ? "" : "no")} breaks allowed. Do you understand?`;
  81. }
  82.  
  83. /* Formats and returns the exam resource string */
  84. /* todo: fix some plurality issues */
  85. function getResourceString(resourceList) {  
  86.    var resources = "For this exam, you are permitted ";
  87.  
  88.    var multipleResources = resourceList.length > 1;
  89.  
  90.    if (resourceList.length != 0) {
  91.        /* pop last resource, add commas between resources, add last resource */
  92.        lastResource = resourceList.pop();
  93.        resourceList = resourceList.join(", ");
  94.  
  95.        if (multipleResources) {
  96.            resourceList = "these materials: " + resourceList + ", and " + lastResource;
  97.        } else {
  98.            resourceList += lastResource;
  99.        }
  100.  
  101.        if (destroyScratch) {
  102.            resourceList += ". You will need to destroy your scratch paper at the end of the exam";
  103.        }
  104.  
  105.     if (multipleResources) {
  106.            resourceList += ". Will you be using these materials today?";
  107.        } else {
  108.            resourceList += ". Will you be using this today?";
  109.        }
  110.  
  111.        resourceList = resourceList.replace(new RegExp('1 Sheet'), "1 Sheet of Scratch Paper");
  112.        resourceList = resourceList.replace(new RegExp('sheets', 'gi'), "Sheets of Scratch Paper");
  113.  
  114.        resources += resourceList;
  115.    } else {
  116.        resources = "For this exam, you are not permitted any materials. Do you understand?";
  117.    }
  118.    return resources;
  119. }
  120.  
  121. /* Find appropriate exam identifiers */
  122. /* todo: must be a better way to do this */
  123. $("dt").each(function(index) {
  124.    var dt = $(this);
  125.    var dd = dt.next();
  126.  
  127.    /* For exam windows with multiple days */
  128.    /* Todo: make this work for more than 2 days */
  129.    if (dd.next() != undefined && dd.next().is("dd")) {
  130.        dd = dd.next();
  131.    }
  132.  
  133.    var descriptorText = dt.text();
  134.    var detailText = dd.text();
  135.  
  136.    switch (descriptorText) {
  137.        case 'Institution':
  138.            schoolName = detailText;
  139.            break;
  140.        case 'Exam Window':
  141.            /* Add message elements to page */
  142.            dd.after(   confDesc +
  143.                        confText +
  144.                        rulesDesc +
  145.                        rulesText +
  146.                        resDesc +
  147.                        resText);
  148.            
  149.            break;
  150.        case 'Exam Name':
  151.            examName = detailText;
  152.            break;
  153.        case 'Exam Duration':
  154.            examTime = detailText;
  155.            break;
  156.    }
  157. });
  158.  
  159. /* attach copy functionality, define element variables */
  160.  
  161. /* confirmation element and button */
  162.  
  163. var confMessage = $('#confText');
  164. var confCopyBtn = $('#confCopyBtn');
  165. confCopyBtn.click(() => {
  166.    selectText(confMessage);
  167. });
  168.  
  169. /* rules element and button */
  170.  
  171. var rulesMessage = $('#rulesText');
  172. var rulesCopyBtn = $('#rulesCopyBtn');
  173. rulesCopyBtn.click(() => {
  174.    selectText(rulesMessage);
  175. });
  176.  
  177. /* resources element and button */
  178.  
  179. var resMessage = $('#resText');
  180. var resCopyBtn = $('#resCopyBtn');
  181. resCopyBtn.click(() => {
  182.    selectText(resMessage);
  183. });
  184.  
  185. /* get all permitted resources and breaks */
  186.  
  187. var resourceListElement = $('h4:contains("Permitted Resources")').next();
  188. var resourceList = [];
  189.  
  190. resourceListElement.each(function(index) {
  191.    resourceList.push($(this).text());
  192. });
  193.  
  194. /* list of resources initially includes non-string elements */
  195. resourceList = resourceList[0].split("\n").filter(String);
  196.  
  197. /* bathroom breaks and scratch paper destruction are handled separately */
  198. resourceList = resourceList.filter(item => item !== "Bathroom breaks" && item !== "Destroy Scratch Paper");
  199.  
  200. /* format resource list into string and set appropriate variables */
  201.  
  202. /* unique schools have resources and exam times listed elsewhere */
  203.  
  204. var isUniqueSchool = determineUniqueSchool(schoolName);
  205.  
  206. if (isUniqueSchool) {
  207.    resCopyBtn.remove();
  208.    rules = uniqueSchools[schoolName].replace("EXAM_TIME", examTime);
  209.    resources = "Unique school (Use Rules for rule confirmation and resource list)";
  210. } else {
  211.    console.log(resourceList);
  212.    hasBreaks = resourceList.includes("Bathroom breaks");
  213.    destroyScratch = resourceList.includes("Destroy Scratch Paper");
  214.    rules = getRulesString();
  215.    resources = getResourceString(resourceList);
  216. }
  217.  
  218. /* exam confirmation should be the same for every school */
  219. confirmation = getConfirmationString(examName);
  220.  
  221. /* prepend all messages to description detail elements */
  222. confMessage.prepend(confirmation);
  223. rulesMessage.prepend(rules);
  224. resMessage.prepend(resources);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement