@@ -39,7 +39,15 @@ class contentcontroller {
3939 /** @var array events that are relevant for the townsquare */
4040 public array $ events ;
4141
42- /** @var array letters and other content that will be shown to the user */
42+ /** @var array stores the letters in objects with the day the letters are from.
43+ * The content is structured in a way that mustache can parse it easily:
44+ *
45+ * ['Y-m-d'] => {
46+ * string $day: 'Y-m-d;
47+ * array $letters: key => $letterobject
48+ * }
49+ * At the end, the array is normalized with array_values so mustache can iterate over it.
50+ */
4351 public array $ content ;
4452
4553 /** @var array courses that show content in townsquare (not the same as enrolled courses) */
@@ -66,13 +74,14 @@ public function build_content(): array {
6674
6775 $ index = 0 ;
6876 $ appearedcourses = [];
77+
6978 // Build a letter for each event.
7079 foreach ($ this ->events as $ event ) {
7180 match ($ event ->eventtype ) {
72- 'post ' => $ templetter = new local \letter \post_letter ($ index , $ event ),
73- 'expectcompletionon ' => $ templetter = new local \letter \activitycompletion_letter ($ index , $ event ),
81+ 'post ' => $ templetter = new local \letter \post_letter ($ index++ , $ event ),
82+ 'expectcompletionon ' => $ templetter = new local \letter \activitycompletion_letter ($ index++ , $ event ),
7483 default => $ templetter = new local \letter \letter (
75- $ index ,
84+ $ index++ ,
7685 $ event ->courseid ,
7786 $ event ->modulename ,
7887 $ event ->instancename ,
@@ -82,17 +91,30 @@ public function build_content(): array {
8291 ),
8392 };
8493 $ templetter = $ templetter ->export_letter ();
85- $ this ->content [$ index ] = $ templetter ;
94+
95+ // Group the letters by its day.
96+ $ day = date ('d.m.Y ' , $ templetter ['createdtimestamp ' ]);
97+ if (!isset ($ this ->content [$ day ])) {
98+ $ this ->content [$ day ] = (object ) [
99+ 'day ' => $ day ,
100+ 'letters ' => [],
101+ ];
102+ $ tempcontent = new orientation_marker ($ index ++, $ day );
103+ $ this ->content [$ day ]->letters [] = $ tempcontent ->export_data ();
104+
105+ }
106+ $ this ->content [$ day ]->letters [] = $ templetter ;
86107
87108 // Collect the courses shown in the townsquare to be able to filter them afterwards.
88- if (!array_key_exists ($ this ->content [$ index ]['courseid ' ], $ appearedcourses )) {
89- $ this ->courses [] = ['courseid ' => $ this ->content [$ index ]['courseid ' ],
90- 'coursename ' => $ this ->content [$ index ]['coursename ' ], ];
109+ if (!array_key_exists ($ templetter ['courseid ' ], $ appearedcourses )) {
110+ $ this ->courses [] = [
111+ 'courseid ' => $ templetter ['courseid ' ],
112+ 'coursename ' => $ templetter ['coursename ' ],
113+ ];
91114 $ appearedcourses [$ event ->courseid ] = true ;
92115 }
93- $ index ++;
94116 }
95- return $ this ->content ;
117+ return array_values ( $ this ->content ) ;
96118 }
97119
98120 // Getter.
0 commit comments