openwebwork / webwork2

Course management front end for WeBWorK
http://webwork.maa.org/wiki/Main_Page
Other
141 stars 164 forks source link

Add filter action to achievements manager. #2328

Closed somiaj closed 4 months ago

somiaj commented 5 months ago

This adds a action to filter achievements as suggested by @Alex-Jordan.

pstaabp commented 5 months ago

Overall, looks good. Seems like the filter by category should be a dropdown/select list of categories, instead of requiring it to be typed in exactly. The default list only has 6 categories, and this should be easy to pull out of the list.

somiaj commented 5 months ago

@pstaabp I thought about that, but the only way I could think to get the list of all categories is to first pull all achievements from the database and cycle through them getting all categories. But once a filter is applied not all achievements are pulled from the database, only the filtered ones.

drgrice1 commented 5 months ago

There are some real problems with the javascript that you are using. This goes back to what you did in #2294.

You are repeatedly adding event handlers to various elements each time the form is submitted. Those event handlers pile up. Note that the once: true option makes the event handler go away if it is called, but each time the form is submitted while that has not been called another event handler is added. Eventually all of those fire at the same time if the event does occur.

Also you getElementById in many cases where you have already obtained a reference to that element. That is an expensive query. Each time it is called, the entire DOM has to be searched until that element is found. For example on line 24 of achievementlist.js you find the filter_select element (and even save a reference in a variable), and then on line 36 you search the DOM again, instead of just using the already found reference. Several places you just blindly search the DOM each time without saving a reference. For example, on line 43 you find the filter_text_err_msg, and then on line 49 you search for it again.

I mentioned the redundancy in the code before, but this is just a total kludge really. The achievementlist.js file is 233 lines long, merely to validate a few form fields. That is really excessive.

somiaj commented 5 months ago

I have been thinking about ways to clean up the code and remove the redundancy, though unsure on the best approach here. My thought was to have helper functions do the work.

As for the change event methods, the reason I was calling getElementById again is I didn't realize that inside the event function I had access to the previously defined variables. Now that I know that I do have access, I'll update things to avoid calling getElementById multiple times when not needed.

I'm not quite sure the what to do about multiple events, it sounds like I should also be removing these events to avoid them piling up when one of the companion events fires?

somiaj commented 5 months ago

I will have time to look at this and see what I can come up with this weekend.

drgrice1 commented 5 months ago

I did a little with the multiple event thing for userlist.js in #2335. Look at what I did there for a start.

somiaj commented 5 months ago

@drgrice1 There is my approach at reducing the code duplication and adding/removing event listeners, so they don't pile up. If you prefer this, and any code cleanup you may see, I can apply this to the other two pages as well (though maybe I should leave userlist.js alone since you have gotten it in your PR).

drgrice1 commented 5 months ago

Yeah, that looks considerably better. Go ahead and do that for the other two pages as well. I will work my changes to userlist.js into your scheme.

somiaj commented 5 months ago

I have updated the other two action form validation location to match what was done here.

somiaj commented 5 months ago

There was some achievement transition code that was suggested to remove around 2017....I can open another PR for that if preferred.

drgrice1 commented 5 months ago

Deleting that code is fine.

The updated javascript is considerably better. Perhaps in the future it would be nice to do this more uniformly for all pages that use action tabs (and the actiontabs.js file). But this is good for now.

Alex-Jordan commented 4 months ago

Thanks for this. I was testing it, and I thought I would make the category selection be a dropdown. I opened a PR to your branch (@somiaj ):

https://github.com/somiaj/webwork2/pull/13

somiaj commented 4 months ago

@Alex-Jordan updated this so the category filter is now a drop down menu instead of a text field.

pstaabp commented 4 months ago

Already 2 approvals, but this looks better to me now.

drgrice1 commented 4 months ago

I will merge this with 2+1 approvals.