I would probably create two custom tables (one for the courses and one for the attendees) and then add a new row to the attendees table (in a FormIt hook) when a user registers.
Here is some example code how you could do something like this with MIGXdb:
In the “Package Manager” create a new package “courses” with this schema.
<?xml version="1.0" encoding="UTF-8"?>
<model package="courses" baseClass="xPDOObject" platform="mysql" defaultEngine="InnoDB" version="1.1">
<object class="CoursesCourse" table="courses_course" extends="xPDOSimpleObject">
<field key="name" dbtype="varchar" precision="191" phptype="string" null="false" default=""/>
<field key="places" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
<composite alias="Attendees" class="CoursesAttendee" local="id" foreign="id_course" cardinality="many" owner="local" />
</object>
<object class="CoursesAttendee" table="courses_attendee" extends="xPDOSimpleObject">
<field key="name" dbtype="varchar" precision="191" phptype="string" null="false" default=""/>
<field key="id_course" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
<aggregate alias="Course" class="CoursesCourse" local="id_course" foreign="id" cardinality="one" owner="foreign" />
<index alias="course" name="course" primary="false" unique="false" type="BTREE">
<column key="id_course" length="" collation="A" null="false" />
</index>
</object>
</model>
Add a new MIGX config “courses” (to be able to edit the courses in the manager)
- Settings: Name = courses | unique MIGX ID = courses
- Formtabs:
- Fieldname = name | Caption = Name
- Fieldname = places | Caption = Places
- Caption = Attendees | Input TV type = migxdb | Configs = courses_attendees
- Columns:
- Header: ID | Field: id
- Header: Name | Field: name
- Contextmenues : update | remove
- Actionbuttons: addItem
- MIGXdb-Settings: Package = courses | Classname = CoursesCourse
Add another MIGX config “courses_attendees” (to be able to edit the attendees)
- Settings: Name = courses_attendees | unique MIGX ID = courses_attendees
- Formtabs:
- Fieldname = name | Caption = Name
- Columns:
- Header: ID | Field: id
- Header: Name | Field: name
- Contextmenues : update | remove
- Actionbuttons: addItem
- MIGXdb-Settings: Package = courses | Classname = CoursesAttendee | Load Grid = auto | Check Resource = yes | Join Alias = Course
Add a new menu item: Action = index | Parameters = &configs=courses
| Namespace = migx
The hook snippet to add a new row to the attendees table: (Assumes the form has a field “attendee_name”)
<?php
//load package
$base_path = $modx->getOption('core_path').'components/courses/';
$modx->addPackage('courses', $base_path.'model/');
$course_id = $hook->formit->config['courseId']; //Read property 'courseId' of FormIt call
$attendee_name = trim($hook->getValue('attendee_name')); //Read value of form field 'attendee_name'
//create new database entry
$new_t = $modx->newObject('CoursesAttendee');
$new_t->set('name',$attendee_name);
$new_t->set('id_course',$course_id);
$new_t->save();
return true;
[[!FormIt?
&hooks=`my_custom_hook`
&courseId=`1`
]]
A snippet to output all the courses:
<?php
//load package
$base_path = $modx->getOption('core_path').'components/courses/';
$modx->addPackage('courses', $base_path.'model/');
$q = $modx->newQuery('CoursesCourse');
$q->select($modx->getSelectColumns('CoursesCourse', 'CoursesCourse'));
$q->select(array('places_taken' => '(SELECT COUNT(id) FROM modx_courses_attendee WHERE id_course = CoursesCourse.id)'));
$courses = $modx->getCollection('CoursesCourse',$q);
$output = '';
foreach ($courses as $course) {
$course_array = $course->toArray('',false,true);
$course_array['available_places'] = $course_array['places'] - $course_array['places_taken'];
$output .= $modx->getChunk('myCourseTpl',$course_array);
}
return $output;
Chunk myCourseTpl:
<h3>[[+name]]</h3>
Total places: [[+places]]<br>
Available places: [[+available_places]]