Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Component code. Can someone have a look over.

Status
Not open for further replies.

boab

Programmer
May 30, 2001
75
0
0
GB
Hi guys,
I think I've taken this thing as far as I can without throwing the pc out the nearest window. Can someone have a look at the coide below and offer some suggestions as to where I have screwed up!!

The purpose of the component is to have a SCORM compliant question component. The question accepts five question types:
MM - Multiple choice / Multiple answers (Check boxes)
MS - Multiple choice / Single Answer (Radio button)
TF - True / False (Boolean radio button)
SA - Short Answers (text input)
EA - Essay Answers (text area)

The number of options for the multiple choice is governed by an array which contains the option labels.

the problems I am having are:

Adjusting the height of the main movie in the component to fit the number of options (I realise I could limit the number of options but it defeats the object of the exercise)

Adjusting the positioning of the Question text and options relative to each other and the background of the component
(ditto the last point with regards to set label widths etc.)

Adjusting the width of the option labels and the number of lines that they show. What I mean by this is how do I show 2 or more line of text in a label where the content is dynamic

I realise that there is probably something stupid I have ommited or forgotten all suggestions welcome code is below
Code:
class Question extends mx.core.UIComponent {
	static var className:String = "Question";
	static var symbolOwner:Object = Question;
	static var symbolName:String = "Question";
	private var __oLabels:Array;
	private var __cOption:Array;
	private var __uOption:Array;
	private var __cResp:String;
	private var __uResp:String;
	private var __qNum:String;
	private var __qTxt:String;
	private var __qType:String;
	private var PrevBtn:MovieClip;
	private var NextBtn:MovieClip;
	private var QuestionNo:MovieClip;
	private var QuestionText:MovieClip;
	private var container_mc:MovieClip;
	private var background_mc:MovieClip;
	function Question() {
		trace("new question");
		var id = "'cmi.interactions.n.id','"+__qNum+"'";
		fscommand("LMSSetValue", id);
		switch (qType) {
		case ("MM") :
			__cResp == this.cAnswer;
			var val:String = "cmi.interactions.n.correct_responses.n.pattern,'"+__cResp+"'";
			fscommand("LMSSetValue", val);
			break;
		case ("MS") :
			var val:String = "cmi.interactions.n.correct_responses.n.pattern,'"+__cResp+"'";
			fscommand("LMSSetValue", val);
			break;
		case ("TF") :
			var val:String = "cmi.interactions.n.correct_responses.n.pattern,'"+__cResp+"'";
			fscommand("LMSSetValue", val);
			break;
		}
	}
	public function set cResponse(resp:String):Void {
		__uResp = resp;
	}
	public function get cResponse():String {
		return (__cResp);
	}
	public function set uResponse(resp:String):Void {
		__uResp = resp;
		invalidate;
	}
	public function get uResponse():String {
		return (__uResp);
	}
	[Inspectable(type=String, defaultValue="This is the text for question "+__qNum)]
	public function set questionTxt(txt:String):Void {
		__qTxt = txt;
		invalidate;
	}
	public function get questionTxt():String {
		return (__qTxt);
	}
	[Inspectable(type=String, defaultValue="Q #")]
	public function set qNum(num:String):Void {
		__qNum = num;
		invalidate;
	}
	public function get qNum():String {
		return (__qNum);
	}
	//50
	[Inspectable(enumeration="MM,MS,TF,SA,EA", defaultValue="MM")]
	public function set qType(type:String):Void {
		__qType = type;
		invalidate;
	}
	public function get qType():String {
		return (__qType);
	}
	[Inspectable( type="Array", defaultValue="option0,option1,option2,option3,option4")]
	public function set oLab(arr:Array):Void {
		__oLabels = arr;
		invalidate;
	}
	public function get oLab():Array {
		return (__oLabels);
	}
	public function set uAnswer(arr:Array):Void {
		__uOption = arr;
		invalidate;
	}
	public function get uAnswer():String {
		var Len:Number = __uOption.length;
		var ans:String;
		var sLen:Number;
		for (var i = 0; i<Len; i++) {
			ans = ans+String(__uOption[i])+",";
		}
		sLen = ans.length-1;
		ans = ans.slice(0, sLen);
		return (ans);
	}
	[Inspectable( type="Array", defaultValue="0,0,0,0,0")]
	public function set cAnswer(arr:Array):Void {
		__cOption = arr;
		invalidate;
	}
	public function get cAnswer():String {
		var Len:Number = __cOption.length;
		var ans:String;
		var sLen:Number;
		for (var i = 0; i<Len; i++) {
			ans = ans+String(__cOption[i])+",";
		}
		sLen = ans.length-1;
		ans = ans.slice(0, sLen);
		return (ans);
	}
	function createChildren(Void):Void {
		var widB = background_mc._width;
		var htB = background_mc.height;
		var bBot = background_mc.x+htB;
		var bRt = background_mc.y+widB;
		attachMovie("Label", "QuestionNo", 4);
		attachMovie("Label", "QuestionText", 3);
		attachMovie("Button", "PrevBtn", 2);
		attachMovie("Button", "NextBtn", 1);
	}
	function draw(Void):Void {
		QuestionNo.text = __qNum;
		QuestionText._width = 400;
		QuestionText.text = __qTxt;
		PrevBtn.onRelease = prevBtn();
		NextBtn.onRelease = nextBtn();
		var container:MovieClip = this.createEmptyMovieClip("container_mc", 0);
		var item:MovieClip;
		switch (__qType) {
		case ("MM") :
			var len:Number = __oLabels.length;
			var yPos:Number = 30;
			var dif:Number;
			for (var i = 0; i<len; i++) {
				var id = "option"+i;
				item = container.attachMovie("CheckBox", id, i, {label:__oLabels[i], _x:2, _y:yPos, _width:container.width});
				yPos = yPos+25;
			}
			var type = "cmi.interactions.0.type','choice'";
			fscommand("LMSSetValue", type);
			item.addEventListener("click", handleEvent);
			break;
		case ("MS") :
			//100
			var len:Number = (__oLabels.length)-1;
			var yPos:Number = 30;
			var dif:Number;
			for (var i = 0; i<len; i++) {
				item = container.attachMovie("RadioButton", "option"+i, i, {label:__oLabels[i], groupName:"radio_"+__qNum, data:i, _x:2, _y:yPos});
				yPos = yPos+25;
			}
			//set interaction type
			var type = "cmi.interactions.0.type','choice'";
			fscommand("LMSSetValue", type);
			//listen for events
			item.addEventListener("click", handleEvent);
			break;
		case ("TF") :
			item = container.attachMovie("RadioButton", "option_t", 5, {label:"true", groupName:"radio_"+__qNum, data:"t", _x:2, _y:30});
			item = container.attachMovie("RadioButton", "option_f", 6, {label:"false", groupName:"radio_"+__qNum, data:"f", _x:2, _y:60});
			//set interaction type
			var type = "cmi.interactions.0.type','true-false'";
			fscommand("LMSSetValue", type);
			//listen for events
			item.addEventListener("click", handleEvent);
			break;
		case ("SA") :
			item = container.attachMovie("TextInput", __qNum+"_txt", 5, {_x:2, _y:30, _width:400, editable:true});
			//listen for events
			item.addEventListener("change", handleEvent);
			//set interaction type
			var type = "cmi.interactions.0.type','fill-in'";
			fscommand("LMSSetValue", type);
			break;
		case ("EA") :
			var w = container.width;
			item = container.attachMovie("TextArea", __qNum+"_txt", 5, {_x:2, _y:30, _width:w, _height:300});
			//listen for events
			item.addEventListener("change", handleEvent);
			//set interaction type
			var type = "cmi.interactions.0.type','fill-in'";
			fscommand("LMSSetValue", type);
			break;
		default :
			QuestionText.text = "The question type is not supported";
		}
	}
	function size(Void):Void {
		trace("size");
		doLayout();
		super.size();
	}
	function doLayout():Void {
		var container:MovieClip = this.container_mc;
		container._xscale=34;
		container._width =background_mc._width=1000;
		container._height=background_mc.width;
		container._x = container._y=2;
		var item:MovieClip;
		var len:Number = __oLabels.length;
		var xPos:Number;
		var yPos:Number;
		var __width = container.width;
		var qLen = __qNum.length*8;
		var qLen2 = (__qTxt.length*8)-qLen;
		QuestionNo.moveTo(2, 2);
		QuestionNo._width=qLen;
		QuestionText._width=qLen2;
		QuestionText._xscale=qLen2;
		QuestionText.moveTo(QuestionNo.width+2, 2);
		NextBtn._x=2;
		NextBtn._y=background_mc.y-background_mc.height;
		
		
		
		
		var it:Number = 2;
		while (QuestionText.width<__width-QuestionNo.width) {
			QuestionText._width = QuestionText.width/2;
			QuestionText._height = QuestionText.height*it;
			it *= 2;
		}
		xPos = 2;
		yPos = container.x+QuestionText.height;
		switch (__qType) {
		case ("MM") :
		case ("MS") :
			for (var i = 0; i<len; i++) {
				var id = "option"+i;
				item = container[id];
				item.label.width = 8*__oLabels[i].length;
				it = 2;
				while (item.label.width>__width) {
					item.label._width = item.label.width/2;
					item.label._height = item.label.height*it;
					it *= 2;
				}
				item.moveTo(xPos, yPos);
			}
			break;
		case ("TF") :
			for (var i = 0; i<2; i++) {
				if (i=0) {
					item = container["option_t"];
					item.moveTo(xPos, yPos);
				} else {
					item = container["option_f"];
					item.moveTo(xPos, yPos+container["option_t"].height+2);
				}
			}
			break;
		case ("SA") :
		case ("EA") :
			item = container[__qNum+"_txt"];
			break;
		}
	}
	function nextBtn() {
		trace("next Question");
		var answer:String;
		var ans:String;
		var cAns:String;
		var crtAnswer:String;
		if (__qType == "MM") {
			ans = this.uAnswer;
			cAns = this.cAnswer;
		} else {
			ans = this.uResponse;
			cAns = this.cResponse;
		}
		answer = "'cmi.interactions.0.student_response','"+ans+"'";
		fscommand("LMSSetValue", answer);
		crtAnswer = "'cmi.interactions.0.correct_responses.0.pattern','"+cAns+"'";
		fscommand("LMSSetValue", crtAnswer);
		this._parent.nextFrame();
	}
	function prevBtn() {
		trace("Previous Question");
		var answer:String;
		var ans:String;
		var cAns:String;
		var crtAnswer:String;
		if (__qType == "MM") {
			ans = this.uAnswer;
			cAns = this.cAnswer;
		} else {
			ans = this.uResponse;
			cAns = this.cResponse;
		}
		answer = "'cmi.interactions.0.student_response','"+ans+"'";
		fscommand("LMSSetValue", answer);
		crtAnswer = "'cmi.interactions.0.correct_responses.0.pattern','"+cAns+"'";
		fscommand("LMSSetValue", crtAnswer);
		this._parent.prevFrame();
	}
	function handleEvent(event_obj):Void {
		if (event_obj.type="click") {
			switch (__qType) {
			case ("MM") :
				var id = this._name;
				var num:Number = parseInt(id.slice(-1, 1));
				if (id.selected != false) {
					__uOption[num] == 0;
				} else {
					__uOption[num] == 1;
				}
				break;
			case ("MS") :
				var id = "radio_"+__qNum;
				this.uResponse = id.data;
				break;
			case ("TF") :
				var id = "radio_"+__qNum;
				this.uResponse = id.data;
				break;
			}
		} else if (event_obj.type="change") {
			switch (__qType) {
			case ("SA") :
				var id = __qNum+"_txt";
				this.uResponse = id.text;
				break;
			case ("EA") :
				var id = __qNum+"_txt";
				var id = __qNum+"_txt";
				this.uResponse = id.text;
				break;
			}
		}
	}
}



THe Start of wisdom is to realise you know nothing. I'll be a genius then!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top