Lingo Coding—question and answer database

 

I am using Gary Rosenzweig’s Books to “cut and paste” my basic code and supplementing as needed.  I also use various “Lingo Forums” to read/post bugs and strategies.

 

--this script builds a database and user interface

 

 

 

--the first handler creates a new record in the database by clearing the global

--gDatabase and going to the data entry screen

 

global gDatabase, gCurrentRecord

 

on newDatabase

  gDatabase = [newRecord()]--database with one record

  go to frame "Entry"

  gCurrentRecord = 1

  showRecord

end

 

--the function "on newRecord" returns a new, blank record (good for adding/replacing records)

 

on newRecord

  record = [:]

  addProp record, #Module_Number, ""

  addProp record, #Difficulty_Level, ""

  addProp record, #Question_Number, ""

  addProp record, #Question_Text, ""

  addProp record, #Correct_Answer, ""

  addProp record, #Alternate_Answer_1, ""

  addProp record, #Alternate_Answer_2, ""

  addProp record, #Alternate_Answer_3, ""

  return record

end

 

--the data entry screen allows users to enter info into newRecord

 

on showRecord --get the current record

  record = gDatabase[gCurrentRecord]

 

  --place empty fields onto data entry screen that correspond with newRecord list

 

  member("Entry - Module_Number").text = record.Module_Number

  member("Entry - Difficulty_Level").text = record.Difficulty_Level

  member("Entry - Question_Number").text = record.Question_Number

  member("Entry - Question_Text").text = record.Question_Text

  member("Entry - Correct_Answer").text = record.Correct_Answer

  member("Entry - Alternate_Answer_1").text = record.Alternate_Answer_1

  member("Entry - Alternate_Answer_2").text = record.Alternate_Answer_2

  member("Entry - Alternate_Answer_3").text = record.Alternate_Answer_3

 

  --show the record number

 

  member("Record Number").text = "Record"&&gCurrentRecord

end

 

--the handler recordRecord takes the text from all fields and places it in a

--new, empty record.  It then replaces the old record in the global database

 

on recordRecord

  --get the empty record to use

  record = newRecord()

 

  --change all fields of record to reflect user data from entry screen

  record.Module_Number = member("Entry - Module_Number").text

  record.Difficulty_Level = member("Entry - Difficulty_Level").text

  record.Question_Number = member("Entry - Question_Number").text

  record.Question_Text = member("Entry - Question_Text").text

  record.Correct_Answer = member("Entry - Correct_Answer").text

  record.Alternate_Answer_1 = member("Entry - Alternate_Answer_1").text

  record.Alternate_Answer_2 = member("Entry - Alternate_Answer_2").text

  record.Alternate_Answer_3 = member("Entry - Alternate_Answer_3").text

 

  --replace record into database

  gDatabase[gCurrentRecord] = record

end

 

--user database navigation called from main menu (edit user)

--also checks to see if database exists, and returns one rather than give error

 

on editDatabase

  if not listP(gDatabase) then

    nextDatabase

  else

    go to frame "Entry"

    gCurrentRecord = 1

    showRecord

  end if

end

 

--from Database Entry Screen, "NEXT, PREVIOUS Buttons"

--Allows user to move through database entries

 

on NextRecord

  recordRecord --write all changes to current record

  gCurrentRecord = gCurrentRecord + 1 --go to next record

  if gCurrentRecord > gDatabase.count then

    gCurrentRecord = 1 --if at end, go to first record

  end if

 

  showRecord

end

 

on PreviousRecord --same as above, but backwards

  recordRecord

  gCurrentRecord = gCurrentRecord - 1

  if gCurrentRecord < 1 then

    gCurrentRecord = gDatabase.count

  end if

 

  showRecord

end

 

--New Users enter their data here...I am not sure if I need this yet...

--Create a new user data record

 

on createRecord

  recordRecord

  gCurrentRecord = gDatabase.count + 1 --goes to end of database

  gDatabase[gCurrentRecord] = newRecord()

 

  showRecord

end

 

--Database entry screen "DONE" button records db record and returns user to main menu

 

on doneEntry

  recordRecord

  go to frame "Done"

end

 

--"Main Menu" Save and Open Database handlers

-- At this point, I am having the user define the files to open and close

-- Later, I will define the file location for the user (no Client Access)

 

on saveDatabase

  fileobj = new(Xtra "FileIO")

  filename = displaySave(fileObj, "Save Database", "database.txt")

  if filename = "" then exit

 

  createFile(fileObj, filename)

  openFile(fileObj, filename, 2)

  writeString(fileObj, string(gDatabase))

  closeFile(fileObj)

end

 

--handler below tests file to see if it is a valid Lingo list = valid db file

 

on openDatabase

  fileObj = new(Xtra "FileIO")

  filename = displayOpen(fileObj)

  if filename = "" then exit

 

  openfile(fileObj, filename, 1)

  text = readFile(fileObj)

  closeFile(fileObj)

 

  database = value(text) --value function takes a string and evaluates it as a Lingo expression

  if not listP(database) then --listP evaluates if the string is a list (True/False)

    alert "Not a valid database file"--

  else

    gDatabase = database

  end if

end

 

 

 

--This is a copy of the database functions from the book

--I need to rename all of the categories to match my own

--I also need to align the names of markers for action lingo

 

global gDatabase

 

-- display a list of all the records using HTML

on browseDatabase

 

  -- html header

  htext = "<HTML><BODY BGCOLOR=#FFFFFF>"&RETURN

 

  -- put the table headings

  put "<TABLE><TR><TH>Record</TH><TH>Module</TH><TH>Diff</TH><TH>Question #</TH><TH>Question Text</TH></TR>"\

     &RETURN after htext

 

  -- loop through database and create table rows

  repeat with i = 1 to gDatabase.count

    put "<TR>" after htext

    put "<TD>"&i&"</TD>" after htext

    put "<TD>"&gDatabase[i].Module_Number&"</TD>" after htext

    put "<TD>"&gDatabase[i].Difficulty_Level&"</TD>" after htext   

    put "<TD>"&gDatabase[i].Question_Number&"</TD>" after htext

    put "<TD>"&gDatabase[i].Question_Text&"</TD>" after htext

    put "</TR>"&RETURN after htext

  end repeat

 

  -- close out table and HTML

  put "</TABLE></BODY></HTML>" after htext

 

  -- place HTMl in text member

  member("Database List").html = htext

 

  go to frame "Browse"

end

 

-- takes the user to the search screen

on searchDatabase

  go to frame "Search"

end

 

-- display a list of records that are found in search

on performSearch

 

  -- get search term from field

  searchText = member("Search Text").text

 

  -- html header

  htext = "<HTML><BODY BGCOLOR=#FFFFFF>"&RETURN

 

  -- put the table headings

  put "<TABLE><TR><TH>Record</TH><TH>Module</TH><TH>Diff</TH><TH>Question #</TH><TH>Question Text</TH></TR>"\

     &RETURN after htext

 

  -- loop through all records

  repeat with i = 1 to gDatabase.count

    record = gDatabase[i]

   

    -- see if the record contains the search text

    -- search all properties for it

    if (record.Module_Number contains searchText) or\

       (record.Difficulty_Level contains searchText) or\

       (record.Question_Number contains searchText) or\

       (record.Question_Text contains searchText) then

     

      -- a match was found, add a row to table

      put "<TR>" after htext

      put "<TD>"&i&"</TD>" after htext

      put "<TD>"&gDatabase[i].Module_Number&"</TD>" after htext

      put "<TD>"&gDatabase[i].Difficulty_Level&"</TD>" after htext   

      put "<TD>"&gDatabase[i].Question_Number&"</TD>" after htext

      put "<TD>"&gDatabase[i].Question_Text&"</TD>" after htext

      put "</TR>"&RETURN after htext

    end if

  end repeat

 

  -- close out table and HTML

  put "</TABLE></BODY></HTML>" after htext

 

  -- put HTML into text member

  member("Search Results Table").html = htext

 

  go to frame "Search Results"

end