
    Qg                         d dl mZ d dlmZ d dlmZmZmZ d dlm	Z	 ddl
mZmZmZmZmZ d dlmZ d dlmZ d	d
lmZ ed        Zed        Zy)    )login_required)	Paginator)get_object_or_404redirectrender)messages   )QuizQuestionAnswerOptionCompletedQuiz)Profile)now   )quizc           	      	   t        t        |      }t        t        | j                        }|j                  j                  d      }t        |d      }t        j                  j                  ||      j                         }|j                  j                         }| j                  j                  d      }|s||k  r	|dz  dz   }nd}|j                  |      }	t        |	j                        }
|
D cg c]  }|j                    }}t        j                  j                  ||      }|D ci c]  }|j"                  | }}|
D ]"  }|j                  |j                         |_        $ t'        |	j(                  |j*                  z  d	z        }| j,                  d
k(  r||
|	|d}t/        | d|      S d}| j,                  dk(  r| j0                  j3                         D ]  \  }}|j5                  d      st'        |j7                  d      d         }t        t8        ||      }	 |j:                  dk(  r| j0                  j=                  d|j                          }t        j                  j?                  ||d      \  }}|j@                  jC                  tD        j                  j                  |             |jG                          |jI                          n|j:                  dk(  rm|}t        j                  j?                  ||d      \  }}tD        j                  j                  |      |_%        |jG                          |jI                          n]|j:                  dk(  rN|}t        j                  j?                  ||d      \  }}||_&        |jG                          |jI                           |r||
|	|d}t/        | d|      S | j0                  j                  d      }| j0                  j                  d      }|rtW        | jX                   d|       S |rtW        | jX                   d|       S |	j[                         sBt\        j                  j_                  ||       tS        j`                  | d       tW        d      S tW        | jX                        S yc c}w c c}w # tN        $ r:}d}||_(        tS        jP                  | d|jT                   d|        Y d}~d}~ww xY w)z
    Displays a paginated view of the quiz questions, 3 questions per page.
    Allows users to save their answers, navigate between pages, and resume the quiz.
    )id)userr      )profilequestion__quizpager	   )r   question__ind   GET)r   current_questionspage_objprogress_percentagezquiz/quiz.htmlFPOST	question__)r   r   )r   questionanswer_type)id__inr   Tz"Error saving answer for question 'z': Nnextpreviousz?page=)r   r   zQuiz submitted successfully!r   )1r   r
   r   r   	questionsorder_byr   r   objectsfiltercountr   getget_pagelistobject_listr   question_iduser_answerintnumber	num_pagesmethodr   r    items
startswithsplitr   question_typegetlistget_or_createselected_optionssetr   
full_cleansaveselected_optionanswer_text	Exceptionerrorr   question_textr   pathhas_nextr   createsuccess)requestquiz_idr   r   r(   	paginatoranswered_questions_counttotal_questionspage_numberr   r   qquestion_idsuser_answersansweranswers_dictr#   r   contexterrors_occurredkeyvaluer1   selected_option_idscreatedselected_option_idrB   e	next_pageprevious_pages                                 6/var/www/peopleoo.sandbox-dev.co.uk/apps/quiz/views.py	quiz_viewr`      s    Tg.Dgll;G ''-I)Q'I  &~~44WUY4Z``bnn**,O ++//&)K#o53q8A=KK!!+.H X112"34QADD4L4 >>((|(TL >JJ6F&&.JLJ% =+//<= x1D1DDKL ~~!2 #6	
 g/99O~~!,,,,. %	qJC~~k*!#))C."34,X+DQ q--2.5ll.B.BYx{{mC\.]+*0..*F*F$+hA +G + //33FNN4I4IQd4I4ef))+!//14-2**0..*F*F$+hA +G + 281C1CGY1C1Z.))+!//14&+*0..*F*F$+hA +G + .9*))+A%	qP %6$':	G '#3W== LL$$V,	((4w||nF=/BCCw||nF9+>??   "!!((t(DW&DEI&& %%M  1 5 Kj ! q&*O%&HNNN7.PQYQgQgPhhklmkn,oppqs%   4R&.R+(FR00	S39/S..S3c                 H    t        t        |      }d|i}t        | d|      S )N)pkr   zquiz/quiz_intro.html)rU   )r   r
   r   )rJ   rK   r   rU   s       r_   
quiz_startrc      s,    Tg.DG '17CC    N)django.contrib.auth.decoratorsr   django.core.paginatorr   django.shortcutsr   r   r   django.contribr   modelsr
   r   r   r   r   apps.carers.modelsr   django.utils.timezoner    r   r`   rc    rd   r_   <module>rn      sP    9 + @ @ # A A & %  {& {&| D Drd   