Forum OpenACS Q&A: Re: XoTCL Newbie

Collapse
4: Re: XoTCL Newbie (response to 1)
Posted by Nima Mazloumi on
Here a little sample as I start learning XoTcl. You can run this in the developer shell:

# we define a class group with several parameters
Class Group -parameter {{name ""} size type start_date end_date {admin_id ""}}

#We define the default constructor
Group instproc init {} {

if {[empty_string_p [my admin_id]]} {
my set admin_id [ad_conn user_id]
}

my set size 0
my set type open
my set start_date [clock format [clock seconds] -format {%D %X}]
my set end_date [clock format [expr [clock seconds] + 3600] -format {%D %X}]
}

#we create class method toXML
Group instproc toXML {} {

acs_user::get -user_id [my admin_id] -array admin

set result ""
append result "?xml version=\"1.0\" encoding=\"[encoding system]\"?\n"
append result "<group name=\"[my name]\" start_date=\"[my start_date]\" end_date=\"[my end_date]\" admin=\"$admin(first_names) $admin(last_name)\" />\n"
return $result
}

#we create class method toString
Group instproc toString {} {
set result ""
append result "Group: [my name]\n"
append result "Size: [my size]\n"
append result "Starting: [my start_date]\n"
append result "Ending: [my end_date]\n"
return $result
}

#We test our code with two new groups
set result ""

append result "Group 1:\n"
Group group -name "default"
append result "String: [group toString]"
append result "\nGroup 2:\n"
Group group -name "Nimas first group" -size 30 -type closed
append result "XML: [group toXML]"

Now wouldn't it be great if that would be directly stored to the database. Without us using sql?