2727use renderable ;
2828use renderer_base ;
2929use templatable ;
30+ use stdClass ;
3031
3132require_once ($ CFG ->dirroot . '/blocks/myoverview/lib.php ' );
3233
3940class main implements renderable, templatable {
4041
4142 /**
42- * Store the grouping preference
43+ * Store the grouping preference.
4344 *
4445 * @var string String matching the grouping constants defined in myoverview/lib.php
4546 */
4647 private $ grouping ;
4748
4849 /**
49- * Store the sort preference
50+ * Store the sort preference.
5051 *
5152 * @var string String matching the sort constants defined in myoverview/lib.php
5253 */
5354 private $ sort ;
5455
5556 /**
56- * Store the view preference
57+ * Store the view preference.
5758 *
5859 * @var string String matching the view/display constants defined in myoverview/lib.php
5960 */
6061 private $ view ;
6162
6263 /**
63- * Store the paging preference
64+ * Store the paging preference.
6465 *
6566 * @var string String matching the paging constants defined in myoverview/lib.php
6667 */
6768 private $ paging ;
6869
6970 /**
70- * Store the display categories config setting
71+ * Store the display categories config setting.
7172 *
7273 * @var boolean
7374 */
7475 private $ displaycategories ;
7576
77+ /**
78+ * Store the configuration values for the myoverview block.
79+ *
80+ * @var array Array of available layouts matching view/display constants defined in myoverview/lib.php
81+ */
82+ private $ layouts ;
83+
7684 /**
7785 * main constructor.
7886 * Initialize the user preferences
7987 *
8088 * @param string $grouping Grouping user preference
8189 * @param string $sort Sort user preference
8290 * @param string $view Display user preference
91+ *
92+ * @throws \dml_exception
8393 */
8494 public function __construct ($ grouping , $ sort , $ view , $ paging ) {
8595 $ this ->grouping = $ grouping ? $ grouping : BLOCK_MYOVERVIEW_GROUPING_ALL ;
8696 $ this ->sort = $ sort ? $ sort : BLOCK_MYOVERVIEW_SORTING_TITLE ;
87- $ this ->view = $ view ? $ view : BLOCK_MYOVERVIEW_VIEW_CARD ;
8897 $ this ->paging = $ paging ? $ paging : BLOCK_MYOVERVIEW_PAGING_12 ;
98+
8999 $ config = get_config ('block_myoverview ' );
90100 if (!$ config ->displaycategories ) {
91101 $ this ->displaycategories = BLOCK_MYOVERVIEW_DISPLAY_CATEGORIES_OFF ;
92102 } else {
93103 $ this ->displaycategories = BLOCK_MYOVERVIEW_DISPLAY_CATEGORIES_ON ;
94104 }
105+
106+ $ this ->set_available_layouts ();
107+ $ this ->view = $ view ? $ view : reset ($ this ->layouts );
95108 }
96109
110+
97111 /**
98- * Get the user preferences as an array to figure out what has been selected
112+ * Set the available layouts based on the config table settings,
113+ * if none are available, defaults to the cards view.
114+ *
115+ * @throws \dml_exception
116+ *
117+ */
118+ public function set_available_layouts () {
119+
120+ if ($ config = get_config ('block_myoverview ' , 'layouts ' )) {
121+ $ this ->layouts = explode (', ' , $ config );
122+ } else {
123+ $ this ->layouts = array (BLOCK_MYOVERVIEW_VIEW_CARD );
124+ }
125+ }
126+
127+ /**
128+ * Get the user preferences as an array to figure out what has been selected.
99129 *
100130 * @return array $preferences Array with the pref as key and value set to true
101131 */
102132 public function get_preferences_as_booleans () {
103133 $ preferences = [];
104- $ preferences [$ this ->view ] = true ;
105134 $ preferences [$ this ->sort ] = true ;
106135 $ preferences [$ this ->grouping ] = true ;
136+ // Only use the user view/display preference if it is in available layouts.
137+ if (in_array ($ this ->view , $ this ->layouts )) {
138+ $ preferences [$ this ->view ] = true ;
139+ } else {
140+ $ preferences [reset ($ this ->layouts )] = true ;
141+ }
107142
108143 return $ preferences ;
109144 }
110145
146+ /**
147+ * Format a layout into an object for export as a Context variable to template.
148+ *
149+ * @param string $layoutname
150+ *
151+ * @return \stdClass $layout an object representation of a layout
152+ * @throws \coding_exception
153+ */
154+ public function format_layout_for_export ($ layoutname ) {
155+ $ layout = new stdClass ();
156+
157+ $ layout ->id = $ layoutname ;
158+ $ layout ->name = get_string ($ layoutname , 'block_myoverview ' );
159+ $ layout ->active = $ this ->view == $ layoutname ? true : false ;
160+ $ layout ->arialabel = get_string ('aria: ' . $ layoutname , 'block_myoverview ' );
161+
162+ return $ layout ;
163+ }
164+
165+ /**
166+ * Get the available layouts formatted for export.
167+ *
168+ * @return array an array of objects representing available layouts
169+ */
170+ public function get_formatted_available_layouts_for_export () {
171+
172+ return array_map (array ($ this , 'format_layout_for_export ' ), $ this ->layouts );
173+
174+ }
175+
111176 /**
112177 * Export this data so it can be used as the context for a mustache template.
113178 *
@@ -118,16 +183,20 @@ public function export_for_template(renderer_base $output) {
118183
119184 $ nocoursesurl = $ output ->image_url ('courses ' , 'block_myoverview ' )->out ();
120185
186+ $ preferences = $ this ->get_preferences_as_booleans ();
187+ $ availablelayouts = $ this ->get_formatted_available_layouts_for_export ();
188+
121189 $ defaultvariables = [
122190 'nocoursesimg ' => $ nocoursesurl ,
123191 'grouping ' => $ this ->grouping ,
124192 'sort ' => $ this ->sort == BLOCK_MYOVERVIEW_SORTING_TITLE ? 'fullname ' : 'ul.timeaccess desc ' ,
125- 'view ' => $ this ->view ,
193+ // If the user preference display option is not available, default to first available layout.
194+ 'view ' => in_array ($ this ->view , $ this ->layouts ) ? $ this ->view : reset ($ this ->layouts ),
126195 'paging ' => $ this ->paging ,
196+ 'layouts ' => $ availablelayouts ,
127197 'displaycategories ' => $ this ->displaycategories ,
198+ 'displaydropdown ' => (count ($ availablelayouts ) > 1 ) ? true : false ,
128199 ];
129-
130- $ preferences = $ this ->get_preferences_as_booleans ();
131200 return array_merge ($ defaultvariables , $ preferences );
132201
133202 }
0 commit comments