Skyhawk 172/Cutlass 172
Flight Crew Instrument; Flight Crew Multiengine; Flight Crew Flight Instructor; Flight Crew Commercial
Flight Crew Last 90 Days 289; Flight Crew Total 1472; Flight Crew Type 1123
Situational Awareness; Communication Breakdown; Distraction
ATC Issue All Types; Conflict NMAC; Deviation - Altitude Excursion From Assigned Altitude; Deviation - Track / Heading All Types; Deviation / Discrepancy - Procedural Published Material / Policy; Deviation / Discrepancy - Procedural Clearance
Flight Crew Took Evasive Action
Airspace Structure; Procedure; Human Factors
I was in the traffic pattern doing touch and go landings with a student at ZZZ (one of five aircraft in the pattern).  We were approaching mid-field right downwind for Runway XX; and tower advised that we were following another aircraft on base; which we advised we had in-sight (which was number two; making us number 3.) As we approached our base leg; tower told us the base turn was our discretion; and we began turning base.  As soon as we did so; the traffic we were following (Aircraft Y) called tower and advised that they needed to do a 360 for spacing (it appeared they had turned their base leg too soon; and were too close to the traffic they were following.  Tower approved and notified them that once they completed their 360 to watch out for us; and that they would be now following us (making us now #2 for the runway; and them #3).My student turned final and we were on an approximately 1.5 nm final; descending through 700-800 feet; when my student stated; 'I think they're turning right at us!'Simultaneously Aircraft Y had apparently just completed their 360 turn and Tower inquired if they had us in sight and were to follow us; they stated they were looking for traffic.I looked up to suddenly see the other aircraft heading directly across our final approach path; and almost on a head-on course; coming at us approximately from 45 degrees off of our left nose.  As I looked; I also simultaneously received an ADS-B traffic alert from Foreflight on my iPad; indicated the other traffic; heading directly at us and at the same altitude.I immediately stated 'my controls'; took over control of the aircraft; and quickly began a descending turn to the right to create distance from the other traffic; I looked up again and saw the other aircraft also start a sharp bank to their right; while simultaneously telling the tower that they now had us in-sight.  I remained in control of the aircraft and executed an (otherwise) uneventful landing.Tower did not acknowledge the event.  It seemed that the controller was very task saturated with the five aircraft in the pattern.  It seemed they were also distracted by a King Air 350 that was handed over to tower from ZZZ Approach and setup for a right downwind for Runway XX. Initially tower had told them to enter left downwind; but they were adamant that they wanted right downwind; which I believe both added to the confusion; as well as tied up the tower frequency unnecessarily with their continued back and forth communications. It did not help that the King Air had requested and was then doing a 360 turn for spacing of their own; right on the upwind leg of Runway XX when there were multiple other aircraft in the traffic pattern.It should be noted that while we were on short final; the Aircraft Y involved in this incident had once again turned base too soon; and I noticed that they were less than 0.5nm behind us.  They were so close that they in fact were ordered to go-around by the Tower because we had just touched down and were still rolling out when they crossed the runway threshold behind us.I believe contributing to the incident were: The pilot of Aircraft Y failing to maintain situational awareness resulting in inadequate spacing thus requesting multiple 360 turns; their failure to adequately scan for traffic and maintain a proper rectangular course in the traffic pattern causing them to cross our final approach course at a 45 degree angle; tower controller being task saturated with multiple aircraft in the traffic pattern; and the King Air traffic adding to the confusion by adamantly requesting to enter the right downwind (which conflicted with the other aircraft already in the traffic pattern for Runway XX; which is right traffic.)
C172 flight instructor reported a Piper Arrow making a nonstandard pattern; along with ATC's lack of awareness; resulted in a NMAC.