summaryrefslogtreecommitdiff
path: root/production_static_files/yaksh/js/edit_question.js
blob: 7a0f56d9176b1ee6bf1e4616fb7956058fb43f2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
function render_question(frm)
{
    for(var i=1;i<=frm.description.length;i++)
    {
        document.getElementById('my'+i).innerHTML = frm.description[i-1].value;
    }
   
}

function increase(frm,n)
{
	var newValue = document.getElementById('id_points'+ (n-1)).value ;
		
	if( newValue == "")
	{
		document.getElementById('id_points'+(n-1)).value = "0.5";
		return;
	}
	document.getElementById('id_points' + (n-1)).value = parseFloat(newValue) + 0.5;
}

function decrease(frm,n)
{
	var newValue = document.getElementById('id_points'+ (n-1)).value ;
		
	if( newValue > 0)
	{
		document.getElementById('id_points' + (n-1)).value = parseFloat(newValue) - 0.5;
	}
	else
	{
		document.getElementById('id_points' + (n-1)).value = 0;
	}
}

function grade_data(showHideDiv)
{
       var ele=document.getElementById(showHideDiv);
       if (ele.style.display=="block")
       {
               ele.style.display = "none";
       }
       else
       {
               ele.style.display = "block";
       }
}

function setSelectionRange(input, selectionStart, selectionEnd) 
{
    if (input.setSelectionRange) 
    {
		input.focus();
		input.setSelectionRange(selectionStart, selectionEnd);
    }
    else if (input.createTextRange) 
    {
		var range = input.createTextRange();
		range.collapse(true);
		range.moveEnd('character', selectionEnd);
		range.moveStart('character', selectionStart);
		range.select();
    }
}

function replaceSelection (input, replaceString) 
{
    if (input.setSelectionRange) 
    {
		var selectionStart = input.selectionStart;
		var selectionEnd = input.selectionEnd;
		input.value = input.value.substring(0, selectionStart)+ replaceString + input.value.substring(selectionEnd);
		if (selectionStart != selectionEnd)
		{ 
			setSelectionRange(input, selectionStart, selectionStart + 	replaceString.length);
		}
		else
		{
			setSelectionRange(input, selectionStart + replaceString.length, selectionStart + replaceString.length);
		}
    }
    else if (document.selection) 
    {
		var range = document.selection.createRange();
		if (range.parentElement() == input) 
		{
			var isCollapsed = range.text == '';
			range.text = replaceString;
			if (!isCollapsed)  
			{
				range.moveStart('character', -replaceString.length);
				range.select();
			}
		}
    }
}

function data(showContent,showHideDiv,a,summary)
{
    var con = document.getElementById(showContent);
    var ele=document.getElementById(showHideDiv);
    var atag=document.getElementById(a);
    if (ele.style.display=="block")
    {
        con.style.display = "none"
        ele.style.display = "none";
		atag.text = summary;
    }
    else
    {
        con.style.display = "block";
        ele.style.display = "block";
    }
}

function textareaformat()
{
	var point = document.getElementsByName('points');
	var test =  document.getElementsByName('test');
	var option =  document.getElementsByName('options');
	var descriptions = document.getElementsByName('description');
	var snippets = document.getElementsByName('snippet');
	var language = document.getElementsByName('language')
	var type = document.getElementsByName('type');
    var tags = document.getElementsByName('tags');
	for (var i=0;i<point.length;i++)
	{
		point[i].id = point[i].id + i;
		descriptions[i+1].id=descriptions[i+1].id + i;
		test[i].id=test[i].id + i;
		snippets[i].id=snippets[i].id + i; 
		option[i].id=option[i].id + i;
		type[i].id = type[i].id + i;
		language[i].id = language[i].id + i;
        tags[i].id = tags[i].id + i;
	}
	for(var i=0;i<point.length;i++)
	{
		var point_id = document.getElementById('id_points'+i);
		point_id.setAttribute('class','mini-text');
	        var tags_id = document.getElementById('id_tags'+i);
		tags_id.setAttribute('class','ac_input');
        	tags_id.setAttribute('autocomplete','off');
		var language_id = document.getElementById('id_language'+i);
		var type_id = document.getElementById('id_type'+i);
		type_id.setAttribute('class','select-type');
		type_id.onchange = showOptions;
		var value = type_id.value;
		var desc_id = document.getElementById('id_description'+i);
		desc_id.onfocus = gainfocus;
		desc_id.onblur = lostfocus;
		var test_id = document.getElementById('id_test' + i);
		test_id.onfocus = gainfocus;
		test_id.onblur = lostfocus;
		var snippet_id = document.getElementById('id_snippet'+i);
		$(snippet_id).bind('focus', function(event){
			console.log("dv")
			this.rows = 5;
		});
		$(snippet_id).bind('keydown', function (event){
			catchTab(snippet_id,event);
		});

		$(language_id).bind('focus', function(event){
		this.style.border = '1px solid #ccc';
		});
		$(type_id).bind('focus', function(event){
		this.style.border = '1px solid #ccc';
		});

		var option_id = document.getElementById('id_options' + i);
		option_id.onfocus = gainfocus;
		option_id.onblur = lostfocus;		
		if(value == 'code' )
		{
			document.getElementById('id_options'+i).style.visibility='hidden';
			document.getElementById('label_option'+(i+1)).innerHTML = "";
		}
		document.getElementById('my'+ (i+1)).innerHTML = desc_id.value;
		jQuery().ready(function(){ 
          		  jQuery("#id_tags" + i).autocomplete("/taggit_autocomplete_modified/json", { multiple: true });
        	});

	}
}

function catchTab(item,e)
{
    if(navigator.userAgent.match("Gecko"))
    {
		c=e.which;
    }
    else
    {
		c=e.keyCode;
    }
    if(c==9)
    {
		replaceSelection(item,String.fromCharCode(9));
		setTimeout("document.getElementById('"+item.id+"').focus();",0);	
		return false;
    }
}

function showOptions(e)
{
		var value = this.value;
		var no = parseInt(this.id.substring(this.id.length-1));
		if( value == 'mcq' || value == 'mcc')
		{	
			document.getElementById('id_options'+no).style.visibility = 'visible';
			document.getElementById('label_option'+ (no+1)).innerHTML = "Options : "
			document.getElementById('label_option'+ (no+1)).style.fontWeight = 'bold';
		}
		else
		{
			document.getElementById('id_options'+no).value = "";
			document.getElementById('id_options'+no).style.visibility = 'hidden';
			document.getElementById('label_option'+ (no+1)).innerHTML = "";
		}
}

function gainfocus(e)
{
	this.rows = 5;
}
function lostfocus(e)
{
	this.rows = 1;
}

function changeColor(element)
{
	element.style.border = 'solid red';
}
function autosubmit()
{
	var total_form = document.getElementsByName('summary').length;
	var empty_options = 0 ;
	var count_mcq = 0;
	var language;
	var type;

	for (var i=0;i<total_form;i++)
	{
		language = document.getElementById('id_language'+i);
	 	type = document.getElementById('id_type'+i);
		if(language.value == 'select')
		{
			changeColor(language);
			return false;
		}
		if(type.value == 'select')
		{
			changeColor(type);
			return false;
		}

		if (document.getElementById('id_type' + i).value == 'code')
		{
			continue;
		}
		else
		{
			count_mcq = count_mcq + 1;
			var options = document.getElementById('id_options' + i).value;
			var total_words = options.split("\n").length ;
			if ( total_words < 4)
				empty_options = empty_options + 1 ;
		}
	}
	if (empty_options > 0)
	{
		alert('Enter 4 options. One option per line.');
		return false;
	}
	return true;
}