Name
sn_interview_temp.InterviewTemplates
Description
No description available
Script
var InterviewTemplates = Class.create();
InterviewTemplates.prototype = {
initialize: function() {
this.QUESTION_TEMPLATE_TABLE = 'sn_interview_question_template';
this.TAGS_M2M_TABLE = 'sn_interview_template_tag_m2m';
this.SORT_FIELD = 'name';
this.limit = gs.getProperty('sn_interview_temp.template_limit') > 30 ? 30 : gs.getProperty('sn_interview_temp.template_limit');
},
/**
* Returns a list of interview questions based on search term and the table name
*
* @param term {string} - search term
* @param table {string} - table for which the interview template is defined
* @returns {Array} - list of all interview questions where term is found in the name or question_template_body
*/
getQuestions: function(term, table) {
if (gs.nil(table)) {
return;
}
var gr = new GlideRecordSecure(this.QUESTION_TEMPLATE_TABLE);
if (!gs.nil(term))
gr.addQuery('nameLIKE' + term + '^ORquestion_template_bodyLIKE' + term);
gr.addQuery('table=' + table + "^ORtableISEMPTY");
gr.addActiveQuery();
gr.setLimit(this.limit);
gr.orderBy(this.SORT_FIELD);
gr.query();
var result = [];
while (gr.next()) {
var tags = this._getTags(gr.sys_id);
result.push({
name: gr.name.getDisplayValue() + '',
sys_id: gr.getUniqueValue(),
question_template_body: gr.question_template_body.getDisplayValue(),
question_text: this._removeTag(gr.question_template_body.getDisplayValue()),
tags: tags.tagCategories,
tagText: tags.tagsText
});
}
return result;
},
/**
* Returns all tags for particular interview question template
*
* @param templateId {string} - sysId of the interview question template
* @returns {Object}
*/
_getTags: function(templateId) {
if (gs.nil(templateId))
return;
var gr = new GlideRecordSecure(this.TAGS_M2M_TABLE);
gr.addQuery('template', templateId);
gr.query();
var tagCategories = [];
var tag = {};
var category;
var table;
while (gr.next()) {
if (gr.template_tag.tag_type == 'custom') {
category = gr.template_tag.custom_category.getDisplayValue() + '';
if (tag.hasOwnProperty(category))
tag[category] = tag[category] + ', ' + gr.template_tag.custom_tag.getDisplayValue();
else
tag[category] = gr.template_tag.custom_tag.getDisplayValue() + '';
} else { // referenced category
table = gr.template_tag.table;
var grt = new GlideRecord(table);
category = grt.getLabel() + '';
if (tag.hasOwnProperty(category))
tag[category] = tag[category] + ', ' + gr.template_tag.record_tag.name.getDisplayValue();
else
tag[category] = gr.template_tag.record_tag.name.getDisplayValue() + '';
}
}
var allTags = '';
Object.keys(tag).forEach(function(key) {
tagCategories.push({
'name': key.length > 0 ? gs.getMessage('For {0} ',key) : '',
'tags': tag[key]
});
allTags += key + ' : ' + tag[key] + ', ';
});
var lastTwo = allTags.slice(-2);
if (lastTwo == ', ') {
allTags = allTags.slice(0, -2);
}
return {
tagCategories: tagCategories,
tagsText: allTags.length > 0 ? gs.getMessage('For {0} ', allTags) : ''
};
},
/**
* Return true if a user has a sn_interview_temp.reader role and there is at least one active interview question template exists
* for a particular table or if no table is specified.
* Used for UI action condition check
* @param table {string} - table name for which the interview templates are defined
* @returns {boolean}
*/
isAvailable: function(table) {
if (gs.nil(table) || !gs.hasRole('sn_interview_temp.reader'))
return false;
var gr = new GlideRecord(this.QUESTION_TEMPLATE_TABLE);
gr.addActiveQuery();
gr.addQuery('table', table)
.addOrCondition('table', 'ISEMPTY', '');
gr.query();
if (gr.next())
return true;
return false;
},
/**
* Removes HTML tags from the string
*
* @param string {string} - Input string
* @returns {String}
*/
_removeTag: function(string) {
return string.replace(/<[^>]*>/g, ' ')
.replace(/\s{2,}/g, ' ')
.trim();
},
/**
* Fetch all tags assigned to templates defined on a table or with no tables
*
* @param string {string} - tablename
* @returns {JSONObject}
*/
getAllTemplateTags: function(tableName) {
var tagObject = {};
var m2m = new GlideAggregate(this.TAGS_M2M_TABLE);
m2m.addQuery('template.table', tableName).addOrCondition('template.table', '');
m2m.groupBy('template_tag');
m2m.query();
while (m2m.next()) {
var categoryValue, categoryForLabel, categoryLabel, tagValue, tagLabel;
var currTag = m2m.template_tag;
if (currTag.tag_type == "referenced") {
categoryValue = currTag.table;
var gr = new GlideRecord(categoryValue);
if (gr.get(currTag.record_tag)) {
categoryLabel = gr.getClassDisplayValue();
tagLabel = gr.getDisplayValue();
}
categoryForLabel = 'For ' + currTag.table.getDisplayValue();
tagValue = currTag.sys_id;
} else if (currTag.tag_type == "custom") {
categoryValue = currTag.custom_category;
categoryLabel = currTag.custom_category.getDisplayValue();
categoryForLabel = currTag.custom_category.getDisplayValue();
tagValue = currTag.sys_id;
tagLabel = currTag.custom_tag.getDisplayValue();
}
if (tagObject[categoryValue])
tagObject[categoryValue].children.push({
id: tagValue.toString() + '#' + categoryValue,
label: tagLabel
});
else
tagObject[categoryValue] = {
id: categoryValue.toString(),
label: categoryLabel,
children: [{
id: tagValue.toString() + '#' + categoryValue,
label: tagLabel
}]
};
}
return tagObject;
},
/**
* Fetch templates matching the query & category count selected on filters
*
* @param categoryCount {integer}
* @param encodedQuery {string}
* @returns array of matching template sys-ids
*/
getMatchingTemplates: function(categoryCount, encodedQuery) {
var matchingTemplates = [];
var currTemplate = '';
var currCategories = [];
var m2m = new GlideRecord("sn_interview_template_tag_m2m");
m2m.addEncodedQuery(encodedQuery);
m2m.query();
while (m2m.next()) {
if (matchingTemplates.length > 20)
break;
var category = m2m.template_tag.table + '' || m2m.template_tag.custom_category + '';
if (currTemplate == m2m.template) {
currCategories.push(category);
} else if (!currTemplate) {
currTemplate = m2m.template + '';
currCategories.push(category);
} else {
var uniqueArr = uniqueArray(currCategories);
if (uniqueArr.length == categoryCount)
matchingTemplates.push(currTemplate+'');
currTemplate = m2m.template + '';
currCategories = [category];
}
}
var uniqueArr = uniqueArray(currCategories);
if (uniqueArr.length == categoryCount && matchingTemplates.length < 20)
matchingTemplates.push(currTemplate+'');
return (matchingTemplates);
function uniqueArray(arr) {
var a = [];
for (var i = 0, l = arr.length; i < l; i++)
if (a.indexOf(arr[i]) === -1 && arr[i] !== '')
a.push(arr[i]);
return a;
}
},
type: 'InterviewTemplates'
};
Sys ID
84fb7a5353a22010ff1fddeeff7b12f0