Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

 

BeanShell
import com.atlassian.spring.container.ContainerManager;
import com.atlassian.confluence.spaces.SpaceManager;
import com.atlassian.confluence.spaces.Space;
import com.atlassian.confluence.user.UserDetailsManager;
import com.atlassian.confluence.user.actions.ProfilePictureInfo;
import com.atlassian.user.User;
import com.atlassian.user.Group;







// ********************************
//  Instantiate Manager Components
// ********************************

SpaceManager spaceManager = (SpaceManager) ContainerManager.getComponent("spaceManager"); 
UserDetailsManager userDetailsManager = (UserDetailsManager) ContainerManager.getComponent("userDetailsManager");




// ********************************
//  Define Student Class
// ********************************


class Student
{
  String username;
  String spaceKey;
  String name;
  String surname;
}



// ********************************
//  Functions
// ********************************

String getInstitutionFromUsername(String username)
{
// Work out the user's institution from their username - every user's
// username has to include their institution for Shibboleth to work.
	String institution = "";
 
	if (username.contains("@bham"))
	{
		institution = "University of Birmingham";
	}
	else if (username.contains("@nottingham"))
	{
		institution = "University of Nottingham";
	}
	else if (username.contains("@ntu"))
	{
		institution = "Nottingham Trent University";
	}
	else if (username.contains("@bcu"))
	{
		institution = "Birmingham City University";
	}
	else if (username.contains("@dmu"))
	{
		institution = "De Montfort University";
	}
	else if (username.contains("@le"))
	{
		institution = "University of Leicester";
	}
	else
	{
		institution = "";
	}

	return institution;
}


String stripCharactersFromUsername(String username)
{
// Take the username and remove full stops, ampersands and whitespace
	String spaceKey = username.replace("@", "");
	spaceKey = spaceKey.replace(".", "");
	spaceKey = spaceKey.replace(" ", "");

	return spaceKey;
}

 
String getSpaceKeyForStudent(String studentUsername, String cohort)
{
	if (cohort.equals("2014"))
	{
	// For Cohort 1 students, we have to map their username to their
	// legacy Space ID, which is based on their old UoN Associate ID.
		switch (studentUsername)
    	{
   			case "aaxer@nottingham.ac.uk":
	        	return "bbaer";
	        case "aaxhk1@nottingham.ac.uk":
	        	return "bbahk";
	        case "aaxhm@nottingham.ac.uk":
				return "bbahm1";
	        case "abxaf1@nottingham.ac.uk":
	   	     	return "bbaaf1";
	        case "acxar@nottingham.ac.uk":
	            return "bbaar";
	        case "acxnph@nottingham.ac.uk":
	            return "bbanh2";
	        case "adxeb3@nottingham.ac.uk":
	            return "bbaeb2";
	        case "adxkmd@nottingham.ac.uk":
	            return "bbakd";
	        case "aexhdj@nottingham.ac.uk":
	            return "bbahj1";
	        case "aexjs4@nottingham.ac.uk":
	            return "bbajs2";
	        case "aexseo@nottingham.ac.uk":
	            return "bbaso1";
	        case "afxsjwi@nottingham.ac.uk":
	            return "bbasw2";
	        case "ahxev@nottingham.ac.uk":
	            return "bbaev1";
	        case "ahxkg@nottingham.ac.uk":
	            return "bbakg1";
	        case "ahxmdh@nottingham.ac.uk":
	            return "bbamh5";
	        case "amxsn2@nottingham.ac.uk":
	            return "bbasn";
	        case "apxcm@nottingham.ac.uk":
	            return "bbacm1";
	        case "asxah4@nottingham.ac.uk":
	            return "bbaah1";
	        case "atxmpw@nottingham.ac.uk":
	            return "bbamw3";
	        case "atxtm2@nottingham.ac.uk":
	            return "bbatm1";
	        case "ahxcsd@nottingham.ac.uk":
	            return "bbacd1";
	        case "bbahk@nottingham.ac.uk":
	            return "bbahk";
	        case "bbats@nottingham.ac.uk":
	            return "bbats";
	        case "llxemkr@nottingham.ac.uk":
	            return "bbaek";
	        case "ucars@nottingham.ac.uk":
	            return "ucars";
	        case "AXL285@bham.ac.uk":
	            return "ucaal";
	        case "AXW440@bham.ac.uk":
	            return "ucaaw";
	        case "CNW604@bham.ac.uk":
	            return "ucacw";
	        case "EMC063@bham.ac.uk":
	            return "ucaec1";
	        case "EWJ002@bham.ac.uk":
	            return "ucaej";
	        case "GCR270@bham.ac.uk":
	            return "ucagr";
	        case "jet287@bham.ac.uk":
	            return "ucajt";
	        case "JVP562@bham.ac.uk":
	            return "ucajk1";
	        case "lxs228@bham.ac.uk":
	            return "ucals";
	        case "LXW936@bham.ac.uk":
	            return "ucalw";
	        case "MDR465@bham.ac.uk":
	            return "ucamr";
	        case "mxb494@bham.ac.uk":
	            return "ucamb";
	        case "mxs749@bham.ac.uk":
	            return "ucams";
	        case "NSH039@bham.ac.uk":
	            return "ucanh2";
	        case "PXE991@bham.ac.uk":
	            return "ucape";
	        case "RJF436@bham.ac.uk":
	            return "ucarf";
	        case "RTO373@bham.ac.uk":
	            return "ucaro";
	        case "RXH202@bham.ac.uk":
	            return "ucarh1";
	        case "smn203@bham.ac.uk":
	            return "ucasn";
	        case "TXB380@bham.ac.uk":
	            return "ucatb";
	        case "TXP351@bham.ac.uk":
	            return "ucatp1";
	        case "WEB192@bham.ac.uk":
	            return "ucawb";
	        case "ajo23@leicester.ac.uk":
	            return "ucaao1";
	        case "bae7@leicester.ac.uk":
	            return "ucabe";
	        case "cb483@leicester.ac.uk":
	            return "ucacb2";
	        case "crb27@leicester.ac.uk":
	            return "ucacb1";
	        case "ejb62@leicester.ac.uk":
	            return "ucaeb2";
	        case "nmb21@leicester.ac.uk":
	            return "ucanb1";
	        case "sjb156@leicester.ac.uk":
	            return "ucasb";
	        case "sjc107@leicester.ac.uk":
	            return "ucasc1";
	        case "ss908@leicester.ac.uk":
	            return "ucass";
	        case "trad1@leicester.ac.uk":
	            return "ucatd";
	        case "pjc51@leicester.ac.uk":
	            return "ucapc";
	        case "N0324194@ntu.ac.uk":
	            return "ucaec";
	        case "N0408697@ntu.ac.uk":
	            return "ucajd2";
	        case "N0415122@ntu.ac.uk":
	            return "ucarc2";
	        case "N0464785@ntu.ac.uk":
	            return "ucasb3";
	        case "N0579527@ntu.ac.uk":
	            return "ucakb";
	        case "P10524765@dmu.ac.uk":
	            return "ucadh";
	        case "P1306375X@dmu.ac.uk":
	            return "ucarg";
	        case "P14171460@dmu.ac.uk":
	            return "ucarw";
	        case "alberto.condotta@bcu.ac.uk":
	            return "ucaac1";
	        case "danielsluman@bcu.ac.uk":
	            return "ucads";
	        case "edwardmckeon@bcu.ac.uk":
	            return "ucaem1";
	        case "s14139692@bcu.ac.uk":
	            return "ucaem1";
	        case "id115444@bcu.ac.uk":
	            return "ucach";
	        case "s06382235@bcu.ac.uk":
	            return "ucaai";
	        case "s09485614@bcu.ac.uk":
	            return "ucadg";
	        case "s12791367@bcu.ac.uk":
	            return "ucasm1";
	        case "tamarapeeters@bcu.ac.uk":
	            return "ucatp";
			case "s14139695":
				return "ucatp";
	        case "s14139695@bcu.ac.uk":
	            return "ucatp";
	        case "s14139695":
	            return "ucatp";
			case "paulnorman":
	            return "ucapn";
	        case "paulnorman@bcu.ac.uk":
	            return "ucapn";
			case "s04337733@bcu.ac.uk":
	            return "ucapn";
	        case "s05360494@bcu.ac.uk":
	            return "ucakp";
	        case "s05360494":
	            return "ucakp";
	        case "s12791366@bcu.ac.uk":
	            return "ucajc";
			case "abxtksi@nottingham.ac.uk":
				return "bbats";
	        default:
				return "";
	    }
	}
	else if (cohort.equals("2015"))
	{
		String spaceKey = stripCharactersFromUsername(studentUsername);
 
    // Leicester students in the 2015 cohort were originally imported with usernames as *.le.ac.uk, then this was changed to leicester.ac.uk.
    // So now their username does not match their space key, which is based on the *.le.ac.uk username.
    // Fix this by replacing "icester" with an empty string...except for Amy Wale, whose space was created manually by Fred Riley with a
	// leicesteracuk suffix.
 
		if (!spaceKey.startsWith("aw378"))
		{
			spaceKey = spaceKey.replace("icester", "");
		}
 
	// This user's surname has changed and their username no longer matches their space key...
		if (spaceKey.equals("jmd53leacuk"))
		{
			spaceKey = "jmb53leacuk";
		}
 
	// Unknown why Richard Burn's username doesn't match his space key...
		if (spaceKey.equals("s15135275bcuacuk"))
		{
			spaceKey = "richardburnbcuacuk";
		}
	// All the other BCU students from 2015 seem to have the same issue...not reported for 3 years...
		if (spaceKey.equals("s08442158bcuacuk"))
		{
			spaceKey = "AliceStoreybcuacuk";
		}
		if (spaceKey.equals("s11760924bcuacuk"))
		{
			spaceKey = "amnanazirbcuacuk";
		}
		if (spaceKey.equals("s13184727bcuacuk"))
		{
			spaceKey = "mayaverlaakbcuacuk";
		}
		if (spaceKey.equals("s15135273bcuacuk"))
		{
			spaceKey = "andrewtooveybcuacuk";
		}
 
		return spaceKey;
	}
	else if (cohort.equals("2016"))
	{
		String spaceKey = stripCharactersFromUsername(studentUsername);
 
    // Once again most Leicester students in the 2016 cohort got imported with usernames as *.le.ac.uk, and this was then changed to 
	// leicester.ac.uk, so their usernames once again don't match their space key, which is based on the *.le.ac.uk username.
    // Fix this by replacing "icester" with an empty string...except for Philip Hughes, who was re-imported due to a problem with a space
	// at the end of his username and ended up with a spacekey in the correct format, and Alister Sutherland, who also has the right format
    // because his account was re-created later.
 
		if ((!spaceKey.startsWith("pjh41")) && (!spaceKey.startsWith("ahs20")))
		{
			spaceKey = spaceKey.replace("icester", "");
		}
 
    // Another anomalous user, created with the wrong username and now having a space key that retains that error
		if (spaceKey.equals("lmm51leacuk"))
		{
			spaceKey = "mm51leacuk";
		}

	// ...another anomalous user...
		if (spaceKey.equals("abxlhannottinghamacuk"))
		{
			spaceKey = "abxlhanottinghamacuk";
		}

	// Username changed as it was wrongly set up with his MA information
		if (spaceKey.equals("abxgffnottinghamacuk"))
		{
			spaceKey = "abxgfnottinghamacuk";
		}

	// Joseph Adams; looks like his username was changed
		if (spaceKey.equals("apxja1nottinghamacuk"))
		{
			spaceKey = "apxjdanottinghamacuk";
		}
 
	// For Cohort 3 DMU students, their space key is missing the DMUACUK suffix, due to an error in the import
	// which was subsequently corrected manually,
	// so this must be removed.
		if (spaceKey.equals("p08283852dmuacuk"))
		{
			spaceKey = "p08283852";
		}
		else if (spaceKey.equals("p11235624dmuacuk"))
		{
			spaceKey = "p11235624";
		}
		else if (spaceKey.equals("p14171122dmuacuk"))
		{
			spaceKey = "p14171122";
		}
		else if (spaceKey.equals("p09255090dmuacuk"))
		{
			spaceKey = "p09255090";
		}
 
	// BCU student whose space key doesn't match their username. Issue reported Sep 2018 as a new issue, but it's likely 
	// they have never appeared in the profiles before
 
		if (spaceKey.equals("s16136416bcuacuk"))
		{
			spaceKey = "angelaenglishbcuacuk";
		}
 
	// All the other BCU students; the same issue, brought to light by Angela's report...
 
		if (spaceKey.equals("s10504425bcuacuk"))
		{
			spaceKey = "charliesarsonbcuacuk";
		}		
		if (spaceKey.equals("s10516234bcuacuk"))
		{
			spaceKey = "tadasstalygabcuacuk";
		}		
		if (spaceKey.equals("s11704566bcuacuk"))
		{
			spaceKey = "emilybettisonbcuacuk";
		}
		if (spaceKey.equals("s11764543bcuacuk"))
		{
			spaceKey = "sarahwaldenbcuacuk";
		}
		if (spaceKey.equals("s13185133bcuacuk"))
		{
			spaceKey = "sophiehedderwickbcuacuk";
		}
		if (spaceKey.equals("s15150968bcuacuk"))
		{
			spaceKey = "coreymwambabcuacuk";
		}
		if (spaceKey.equals("s16110141bcuacuk"))
		{
			spaceKey = "simonfleurybcuacuk";
		}
		if (spaceKey.equals("s16141933bcuacuk"))
		{
			spaceKey = "selinaschmidtbcuacuk";
		}


		return spaceKey;
	}
	else if (cohort.equals("2017"))
	{
		String spaceKey = stripCharactersFromUsername(studentUsername);
 
    // Once again most Leicester students in the 2017 cohort got imported with usernames as *.le.ac.uk, and this was then changed to 
	// leicester.ac.uk, so their usernames once again don't match their space key, which is based on the *.le.ac.uk username.
    // Fix this by replacing "icester" with an empty string...except for Natasha Bailey, Jody Hannis, Susan Bishop, Rory Booth,
	// Harry Wilkinson AND Yvonne O'Dell, who got imported correctly as @leicester.ac.uk.
 
		if ((!spaceKey.startsWith("nkb11")) && (!spaceKey.startsWith("hew16")) && (!spaceKey.startsWith("jh720")) && (!spaceKey.startsWith("skb47")) && (!spaceKey.startsWith("rjb91"))  && (!spaceKey.startsWith("yvod")))
		{
			spaceKey = spaceKey.replace("icester", "");
		}

    // Another anomalous user, Daniel Bilton, created with the wrong username and now having a space key that retains that error
		if (spaceKey.equals("n0503082ntuacuk"))
		{
			spaceKey = "n0503802ntuacuk";
		}
 
    // Another anomalous user, Thomas Rusbridge, created with 'student.' prefix to his username and now having a space key that retains that error
		if (spaceKey.equals("tbr678bhamacuk"))
		{
			spaceKey = "tbr678studentbhamacuk";
		}


	// BCU students got imported with usernames @mail.bcu.ac.uk. These were changed to @bcu.ac.uk but their spacekeys still include 'mail'.
	// Shibboleth is not currently working for these users though; it may be that the Shibboleth server is now sending us domain name
	// @mail.bcu.ac.uk and we need to change our Shibboleth mapping for BCU - in which case, the usernames may need to be changed back
	// again and the following line of code would then need to be removed...
	// Exceptions for Jill Robinson and Daniel Sluman: their username is in a different format to the rest of this year's BCU intake
		if (spaceKey.equals("s17150726bcuacuk"))
		{
			spaceKey = "jillrobinsonbcuacuk";
		}
		else if (spaceKey.equals("s14139338bcuacuk"))
		{
			spaceKey = "danielslumanmailbcuacuk";
		}
	// 10/9/2018: Reviewing all the BCU students, these ones from 2017 were also unmapped...
		else if (spaceKey.equals("s17148671bcuacuk"))
		{
			spaceKey = "HelenHopkins2mailbcuacuk";
		}
		else if (spaceKey.equals("s17149343bcuacuk"))
		{
			spaceKey = "KristianGathmailbcuacuk";
		}
		else if (spaceKey.equals("martincoxbcuacuk"))
		{
			spaceKey = "MartinCoxmailbcuacuk";
		}
 
	// DMU students got imported with usernames @my365.dmu.ac.uk. These were changed to @dmu.ac.uk but their spacekeys still include 'my365'.

		spaceKey = spaceKey.replace("dmuacuk", "my365dmuacuk");
 
		return spaceKey;
	}
	else if (cohort.equals("2018"))
	{
		String spaceKey = stripCharactersFromUsername(studentUsername);

 
	// Student imported using wrong username...
		if (spaceKey.equals("apxkl3nottinghamacuk"))
		{
			spaceKey = "apxkl5nottinghamacuk";
		}
 
		return spaceKey;
	}
	else
	{
	// For all other cohorts, their username (with characters removed)
	// should map straight to their space key.
		String spaceKey = stripCharactersFromUsername(studentUsername);
 
		return spaceKey;
	}
}

void quicksortStudentArray(int lo, int hi)
{
	if (lo < hi)
	{
		int p = partitionStudentArray(lo, hi);
		quicksortStudentArray(lo, p);
		quicksortStudentArray(p+1, hi);
	}
}

int partitionStudentArray(int lo, int hi)
{
	String pivot = studentsList.get(lo).surname;
	int i = lo - 1;
	int j = hi + 1;
 	while (true)
	{
		do
		{
			i++;
		}
		while (studentsList.get(i).surname.compareTo(pivot) < 0);

		do
		{
			j--;
		}
		while (studentsList.get(j).surname.compareTo(pivot) > 0);

		if (i >= j)
		{
			return j;
		}

		Student temp = studentsList.get(i);
		studentsList.set(i, studentsList.get(j));
		studentsList.set(j, temp);
	}
}



// ********************************
//  Main Script
// ********************************

List studentsList = new ArrayList();

// Get a list of all usernames: this will include disabled user accounts.
// Note that userAccessor is a built-in object in Beanshell for Confluence.
List usernamesList = new ArrayList();
usernamesList = userAccessor.getUserNamesWithConfluenceAccess();


// Loop through the list of usernames looking for students to add to the list of students

for (username: usernamesList)
{
    if (!userAccessor.isDeactivated(username))
    {
    // Get list of all groups the user is a member of
        List groupNamesList = new ArrayList(); 
        groupNamesList = userAccessor.getGroupNamesForUserName(username);

        boolean isStudent = false;
	    boolean isSupervisor = false;

    // Loop through all the groups the user is a member of, and for students, create a Student object,
	// get their space key, work out their first name and surname from their space name, add their 
	// details to the object and then add the Student object to the list of Students.

		for (String groupName : groupNamesList)
        {
			if (groupName.contains("tudent"))   // Some groups are named "Student" and others are "student"
		    {
			    User user = userAccessor.getUserByName(username);

				if (user != null && !username.equals("c.thompson@nottingham.ac.uk") && !username.equals("bbzclt@nottingham.ac.uk"))
				{
					Student student = new Student();
					student.username = username;

					if (groupName.equals("BB-m3c-Students2014"))
					{
						student.spaceKey = getSpaceKeyForStudent(username, "2014");
					}
					else if (groupName.equals("bb-m3c-students2015"))
					{
						student.spaceKey = getSpaceKeyForStudent(username, "2015");						
					}
					else if (groupName.equals("bb-m3c-students2016"))
					{
						student.spaceKey = getSpaceKeyForStudent(username, "2016");
					}
					else if (groupName.equals("bb-m3c-students2017"))
					{
						student.spaceKey = getSpaceKeyForStudent(username, "2017");
					}
					else if (groupName.equals("bb-m3c-students2018"))
					{
						student.spaceKey = getSpaceKeyForStudent(username, "2018");
					}
					else
					{
						student.spaceKey = getSpaceKeyForStudent(username, "Other");						
					}

					Space space = spaceManager.getSpace(student.spaceKey);

					if (space != null)
					{	
						student.name = space.getName().trim();
						int lastSpace = student.name.lastIndexOf(" ");
						student.surname = student.name.substring(lastSpace+1);					
						studentsList.add(student);
					}
				}
		    }
        }
    }
}

/*
// Retired because this is too slow:
// Bubble sort the list of students by surname

rowsSwapped = true;
 
while (rowsSwapped)
{
	rowsSwapped = false;
 
	for (int i=0; i< studentsList.size()-1; i++)
	{
		if (studentsList.get(i).surname.compareTo(studentsList.get(i+1).surname) > 0)
		{
			rowsSwapped = true;
			studentsList.add(i+2, studentsList.get(i));
			studentsList.remove(i);
		}
	}
}
*/


quicksortStudentArray(0, studentsList.size() - 1);


// Loop through the list of students and print the HTML for the people cards

print("<div class=\"grid\">");

for (student: studentsList)
{
	String username = student.username;
	User user = userAccessor.getUserByName(username);
	String position = userDetailsManager.getStringProperty(user, "position");
	String department = userDetailsManager.getStringProperty(user, "department");

    if (position==null)
    {
        position = "";
    }

    if (department==null)
    {
        department = "";
    }

	String IndustrialStrategyGroupMembership = "";

	if (userAccessor.hasMembership("industrial-strategy", username))
	{
		IndustrialStrategyGroupMembership = "Industrial-Strategy-and-CDA";
	}
 
    ProfilePictureInfo profilePictureInfo = userAccessor.getUserProfilePicture(user);
	String downloadPath = profilePictureInfo.getDownloadPath();
	String institution = getInstitutionFromUsername(username);

	print("<div class=\"element-item people-card " + IndustrialStrategyGroupMembership + " " + institution.replace(" ", "-") + " \" data-category=\"" + IndustrialStrategyGroupMembership + " " + institution.replace(" ", "-") + " \">");
	print("<a href=\"https://vpp.midlands3cities.ac.uk/display/" + student.spaceKey + "\">");
	print("<div class=\"pull-left-img\">");
	print("<img class=\"people-image\" src=\"" + downloadPath + "\" alt=\"" + student.name + "\">");
	print("</div>");
	print("<div class=\"people-body\">");
	print("<h3 class=\"name\">" + student.name + "</h3>");
	print("<p class=\"department\">" + department + "</p>");
	print("<p class=\"university\">" + institution + "</p>");
	print("</div>");
print("<div class=\"thesis-div\"><p class=\"thesis\">" + position + "</p></div>");
	print("</a>");
	print("</div>");
}

print("</div>");







 

...