diff --git a/application/controllers/RegisterController.php b/application/controllers/RegisterController.php
index 1867bfcc6c0..ddfdf4fb139 100644
--- a/application/controllers/RegisterController.php
+++ b/application/controllers/RegisterController.php
@@ -21,243 +21,356 @@
*/
class RegisterController extends LSYii_Controller {
+ public $layout = 'bare';
+
function actionAJAXRegisterForm($surveyid)
{
Yii::app()->loadHelper('database');
Yii::app()->loadHelper('replacements');
$redata = compact(array_keys(get_defined_vars()));
- $iSurveyID = sanitize_int($surveyid);
- $oSurvey = Survey::model()->find('sid=:sid',array(':sid' => $iSurveyID)) or show_error("Can't find survey data");
+ $iSurveyId = $surveyid;
+ $oSurvey = Survey::model()->find('sid=:sid',array(':sid' => $iSurveyId));
+ if (!$oSurvey){
+ throw new CHttpException(404, "The survey in which you are trying to participate does not seem to exist. It may have been deleted or the link you were given is outdated or incorrect.");
+ }
+ // Don't test if survey allow registering .....
+ $sLanguage = Yii::app()->request->getParam('lang',$oSurvey->language);
+ Yii::import('application.libraries.Limesurvey_lang');
+ Yii::app()->lang = new Limesurvey_lang($sLanguage);
+
$thistpl=getTemplatePath(validateTemplateDir($oSurvey->template));
- $data['sid'] = $iSurveyID;
+ $data['sid'] = $iSurveyId;
$data['startdate'] = $oSurvey->startdate;
$data['enddate'] = $oSurvey->expires;
- $data['thissurvey'] = getSurveyInfo($iSurveyID , $oSurvey->language);
- Yii::import('application.libraries.Limesurvey_lang');
- Yii::app()->lang = new Limesurvey_lang($oSurvey->language);
- echo templatereplace(file_get_contents("$thistpl/register.pstpl"),array(),$data,'register.php',false,NULL,$data);
- unset($_SESSION['survey_'.$iSurveyID]['register_errormsg']);
-
+ $data['thissurvey'] = getSurveyInfo($iSurveyId , $oSurvey->language);
+ echo self::getRegisterForm($iSurveyId);
+ Yii::app()->end();
}
/**
- * register::index()
+ * Default action register
* Process register form data and take appropriate action
+ * @param $sid Survey Id to register
+ * @param $aRegisterErrors array of errors when try to register
* @return
*/
- function actionIndex($iSurveyID = null)
+ function actionIndex($sid = null)
{
- Yii::app()->loadHelper('database');
- Yii::app()->loadHelper('replacements');
- $sLanguage = Yii::app()->request->getParam('lang','');
- if($iSurveyID == null)
- {
- $iSurveyID = Yii::app()->request->getPost('sid');
- }
- if (!$iSurveyID)
- {
- $this->redirect(Yii::app()->baseUrl);
- }
- if ($sLanguage=="" )
- {
- $sBaseLanguage = Survey::model()->findByPk($iSurveyID)->language;
- }
+ if(!is_null($sid))
+ $iSurveyId=$sid;
else
- {
- $sBaseLanguage = $sLanguage;
- }
- Yii::import('application.libraries.Limesurvey_lang');
- Yii::app()->lang = new Limesurvey_lang($sBaseLanguage);
- $clang = Yii::app()->lang;
- $thissurvey=getSurveyInfo($iSurveyID,$sBaseLanguage);
+ $iSurveyId=Yii::app()->request->getPost('sid');
+ $oSurvey=Survey::model()->find("sid=:sid",array(':sid'=>$iSurveyId));
- $register_errormsg = "";
- // Check the security question's answer
- if (function_exists("ImageCreate") && isCaptchaEnabled('registrationscreen',$thissurvey['usecaptcha']) )
+ $sLanguage = Yii::app()->request->getParam('lang');
+ if (!$sLanguage)
{
- if (!isset($_POST['loadsecurity']) ||
- !isset($_SESSION['survey_'.$iSurveyID]['secanswer']) ||
- Yii::app()->request->getPost('loadsecurity') != $_SESSION['survey_'.$iSurveyID]['secanswer'])
- {
- $register_errormsg .= $clang->gT("The answer to the security question is incorrect.")."
\n";
- }
+ $sLanguage = Yii::app()->request->getPost('register_lang',Survey::model()->findByPk($iSurveyId)->language);
}
- //Check that the email is a valid style address
- if (!validateEmailAddress(Yii::app()->request->getPost('register_email')))
- {
- $register_errormsg .= $clang->gT("The email you used is not valid. Please try again.");
- }
-
- // Check for additional fields
- $attributeinsertdata = array();
- foreach (GetParticipantAttributes($iSurveyID) as $field => $data)
- {
- if (empty($data['show_register']) || $data['show_register'] != 'Y')
- continue;
-
- $value = sanitize_xss_string(Yii::app()->request->getPost('register_' . $field));
- if (trim($value) == '' && $data['mandatory'] == 'Y')
- $register_errormsg .= sprintf($clang->gT("%s cannot be left empty"), $thissurvey['attributecaptions'][$field]);
- $attributeinsertdata[$field] = $value;
+ if (!$oSurvey){
+ throw new CHttpException(404, "The survey in which you are trying to participate does not seem to exist. It may have been deleted or the link you were given is outdated or incorrect.");
+ }elseif($oSurvey->allowregister!='Y' || !tableExists("{{tokens_{$iSurveyId}}}")){
+ throw new CHttpException(404,"The survey in which you are trying to register don't accept registration. It may have been updated or the link you were given is outdated or incorrect.");
}
- if ($register_errormsg != "")
- {
- $_SESSION['survey_'.$iSurveyID]['register_errormsg']=$register_errormsg;
- $this->redirect($this->createUrl("survey/index/sid/{$iSurveyID}",array('lang'=>$sBaseLanguage)));
+ elseif(!is_null($oSurvey->expires) && $oSurvey->expires < dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i", Yii::app()->getConfig('timeadjust'))){
+ $this->redirect(array('survey/index','sid'=>$iSurveyId,'lang'=>$sLanguage));
}
- //Check if this email already exists in token database
- $oToken=TokenDynamic::model($iSurveyID)->find('email=:email',array(':email'=>Yii::app()->request->getPost('register_email')));
- if ($oToken)
- {
- $register_errormsg=$clang->gT("The email you used has already been registered.");
- $_SESSION['survey_'.$iSurveyID]['register_errormsg']=$register_errormsg;
- $this->redirect($this->createUrl("survey/index/sid/{$iSurveyID}",array('lang'=>$sBaseLanguage)));
- //include "index.php";
- //exit;
- }
-
- $mayinsert = false;
+ Yii::import('application.libraries.Limesurvey_lang');
+ Yii::app()->lang = new Limesurvey_lang($sLanguage);
+ // Test if we come from register form (and submit)
+
+ $aSended=array('sent'=>false,'message'=>'');
+ // New event to replace complety register system
+ $event = new PluginEvent('beforeRegister');
+ $event->set('surveyid', $iSurveyId);
+ $event->set('lang', $sLanguage);
+ App()->getPluginManager()->dispatchEvent($event);
- // Get the survey settings for token length
+ $bIsSubmited=$event->get('submited',Yii::app()->request->getPost('register_email',false));
+ $aSended=$event->get('aSended',array('sent'=>false,'message'=>''));
- $tokenlength = $thissurvey['tokenlength'];
- //if tokenlength is not set or there are other problems use the default value (15)
- if(!isset($tokenlength) || $tokenlength == '')
- {
- $tokenlength = 15;
+ if($bIsSubmited!==false){
+ $aRegisterErrors=self::getRegisterErrors($iSurveyId);
+ if(empty($aRegisterErrors)){
+ $aSended=self::sendRegistrationEmail($iSurveyId);
+ }
+ }else{
+ $aRegisterErrors=null;
}
- while ($mayinsert != true)
- {
- $newtoken = randomChars($tokenlength);
- $oTokenExist=TokenDynamic::model($iSurveyID)->find('token=:token',array(':token'=>$newtoken));
- if (!$oTokenExist) {$mayinsert = true;}
+ $aData['surveyid']=$surveyid=$iSurveyId;
+ $aData['thissurvey']=getSurveyInfo($iSurveyId,$sLanguage);
+ $sTemplate=getTemplatePath(validateTemplateDir($aData['thissurvey']['template']));
+ Yii::app()->setConfig('surveyID',$iSurveyId);//Needed for languagechanger
+ $aData['languagechanger']=makeLanguageChangerSurvey($sLanguage);
+ $aData['sitename']=Yii::app()->getConfig('sitename');
+ // Work only with controllers survey : did we fix it and use register url for registering ?
+ sendCacheHeaders();
+ doHeader();
+ echo templatereplace(file_get_contents("{$sTemplate}/startpage.pstpl"),array(),$aData);
+ echo templatereplace(file_get_contents("{$sTemplate}/survey.pstpl"),array(),$aData);
+ if(!$aSended['sent']){
+ echo self::getRegisterForm($iSurveyId,array($aSended['message']));
+ }else{
+ echo templatereplace($aSended['message']);
}
+ echo templatereplace(file_get_contents("{$sTemplate}/endpage.pstpl"),array(),$aData);
+ doFooter();
+# $aViewData['sTemplate']=$sTemplate;
+# $aViewData['aData']=$aData;
+# $aViewData['content']=self::getRegisterForm($iSurveyId);
+# $this->render('display',$aViewData);// Need register/display.php view
+ }
- $postfirstname=sanitize_xss_string(strip_tags(Yii::app()->request->getPost('register_firstname')));
- $postlastname=sanitize_xss_string(strip_tags(Yii::app()->request->getPost('register_lastname')));
- $starttime = sanitize_xss_string(Yii::app()->request->getPost('startdate'));
- $endtime = sanitize_xss_string(Yii::app()->request->getPost('enddate'));
- /*$postattribute1=sanitize_xss_string(strip_tags(returnGlobal('register_attribute1')));
- $postattribute2=sanitize_xss_string(strip_tags(returnGlobal('register_attribute2'))); */
+ /**
+ * Validate a register form
+ * @param $iSurveyId Survey Id to register
+ * @return array of errors when try to register (empty array => no error)
+ */
+ public function getRegisterErrors($iSurveyId){
+ $aRegisterErrors=array();
+ $clang = Yii::app()->lang;
+ $aSurveyInfo=getSurveyInfo($iSurveyId,$clang->langcode);
- // Insert new entry into tokens db
- $oToken = Token::create($thissurvey['sid']);
- $oToken->firstname = $postfirstname;
- $oToken->lastname = $postlastname;
- $oToken->email = Yii::app()->request->getPost('register_email');
- $oToken->emailstatus = 'OK';
- $oToken->token = $newtoken;
- if ($starttime && $endtime)
+ // Check the security question's answer
+ if (function_exists("ImageCreate") && isCaptchaEnabled('registrationscreen',$aSurveyInfo['usecaptcha']) )
{
- $oToken->validfrom = $starttime;
- $oToken->validuntil = $endtime;
+ $sLoadsecurity=Yii::app()->request->getPost('loadsecurity','');
+ $sSecAnswer=(isset($_SESSION['survey_'.$iSurveyId]['secanswer']))?$_SESSION['survey_'.$iSurveyId]['secanswer']:"";
+ if ($sLoadsecurity!=$sSecAnswer)
+ {
+ $aRegisterErrors[] = $clang->gT("The answer to the security question is incorrect.");
+ }
}
- $oToken->setAttributes($attributeinsertdata, false);
- $result = $oToken->save();
- //$tid = $oToken->tid;// Not needed any more
- $fieldsarray["{ADMINNAME}"]=$thissurvey['adminname'];
- $fieldsarray["{ADMINEMAIL}"]=$thissurvey['adminemail'];
- $fieldsarray["{SURVEYNAME}"]=$thissurvey['name'];
- $fieldsarray["{SURVEYDESCRIPTION}"]=$thissurvey['description'];
- $fieldsarray["{FIRSTNAME}"]=$postfirstname;
- $fieldsarray["{LASTNAME}"]=$postlastname;
- $fieldsarray["{EXPIRY}"]=$thissurvey["expiry"];
- $fieldsarray["{TOKEN}"]=$oToken->token;
- $fieldsarray["{EMAIL}"]=$oToken->email;
- $token=$oToken->token;
-
-
- $message=preg_replace("/{TOKEN:([A-Z0-9_]+)}/","{"."$1"."}",$thissurvey['email_register']);
- $subject=preg_replace("/{TOKEN:([A-Z0-9_]+)}/","{"."$1"."}",$thissurvey['email_register_subj']);
+ //Check that the email is a valid style address
+ $sRegisterEmail=Yii::app()->request->getPost('register_email','');
+ if($sRegisterEmail==""){
+ $aRegisterErrors[]= $clang->gT("You must enter a valid email. Please try again.");
+ }elseif (!validateEmailAddress($sRegisterEmail)){
+ $aRegisterErrors[]= $clang->gT("The email you used is not valid. Please try again.");
+ }
+ //Check and validate attribute
+ $aRegisterAttributes=$aSurveyInfo['attributedescriptions'];
+ foreach ($aSurveyInfo['attributedescriptions'] as $key => $aAttribute)
+ {
+ if ($aAttribute['show_register'] == 'Y' && $aAttribute['mandatory'] == 'Y')
+ {
+ $sRegisterAttribute=Yii::app()->request->getPost('register_'.$key);
+ if(empty($sRegisterAttribute)){
+ $sAttributeCaption=($aSurveyInfo['attributecaptions'][$key]?$aSurveyInfo['attributecaptions'][$key] : ($aAttribute['description']?$aAttribute['description'] : $key));
+ $aRegisterErrors[]= sprintf($clang->gT("%s cannot be left empty").".", $sAttributeCaption);
+ }
+ }
+ }
+ return $aRegisterErrors;
+ }
- $from = "{$thissurvey['adminname']} <{$thissurvey['adminemail']}>";
+ public function getRegisterForm($iSurveyId,$aRegisterErrors = null){
+ $clang = Yii::app()->lang;
+ $aSurveyInfo=getSurveyInfo($iSurveyId,$clang->langcode);
+ $sTemplate=getTemplatePath(validateTemplateDir($aSurveyInfo['template']));
- $surveylink = $this->createAbsoluteUrl("/survey/index/sid/{$iSurveyID}",array('lang'=>$sBaseLanguage,'token'=>$newtoken));
- $optoutlink = $this->createAbsoluteUrl("/optout/tokens/surveyid/{$iSurveyID}",array('langcode'=>$sBaseLanguage,'token'=>$newtoken));
- $optinlink = $this->createAbsoluteUrl("/optin/tokens/surveyid/{$iSurveyID}",array('langcode'=>$sBaseLanguage,'token'=>$newtoken));
- if (getEmailFormat($iSurveyID) == 'html')
- {
- $useHtmlEmail = true;
- $fieldsarray["{SURVEYURL}"]="".$surveylink."";
- $fieldsarray["{OPTOUTURL}"]="".$optoutlink."";
- $fieldsarray["{OPTINURL}"]="".$optinlink."";
+ // Event to replace register form
+ $event = new PluginEvent('beforeRegisterForm');
+ $event->set('surveyid', $iSurveyId);
+ $event->set('lang', $clang->langcode);
+ $event->set('aRegistersErrors', $aRegisterErrors);
+ App()->getPluginManager()->dispatchEvent($event);
+ if(!is_null($event->get('registerForm')))
+ return $event->get('registerForm');
+ // Allways keep the value
+ $sFirstName=Yii::app()->request->getPost('register_firstname','');
+ $sLastName=Yii::app()->request->getPost('register_lastname','');
+ $sEmail=Yii::app()->request->getPost('register_email','');
+ $aRegisterAttributes=$aSurveyInfo['attributedescriptions'];
+ foreach($aRegisterAttributes as $key=>$aRegisterAttribute){
+ if($aRegisterAttribute['show_register']!='Y'){
+ unset($aRegisterAttributes[$key]);
+ }else{
+ $aRegisterAttributes[$key]['caption']=($aSurveyInfo['attributecaptions'][$key]?$aSurveyInfo['attributecaptions'][$key] : ($aRegisterAttribute['description']?$aRegisterAttribute['description'] : $key));
+ $aRegisterAttributes[$key]['value']=Yii::app()->request->getPost("register_{$key}",'');
+ }
}
+ $aData['iSurveyId'] = $iSurveyId;
+ $aData['sLanguage'] = $clang->langcode;
+ $aData['clang'] = $clang;
+ $aData['sFirstName'] = $sFirstName;
+ $aData['sLastName'] = $sLastName;
+ $aData['sEmail'] = $sEmail;
+ $aData['aExtraAttributes']=$aRegisterAttributes;
+ $aData['urlAction']=App()->createUrl('register/index',array('sid'=>$iSurveyId));
+ $aData['bCaptcha'] = function_exists("ImageCreate") && isCaptchaEnabled('registrationscreen', $aSurveyInfo['usecaptcha']);
+ $aReplacement['REGISTERFORM']=$this->renderPartial('registerForm',$aData,true);
+ if(is_array($aRegisterErrors))
+ $sRegisterError=implode('
',$aRegisterErrors);
else
- {
- $useHtmlEmail = false;
- $fieldsarray["{SURVEYURL}"]= $surveylink;
- $fieldsarray["{OPTOUTURL}"]= $optoutlink;
- $fieldsarray["{OPTINURL}"]= $optinlink;
- }
-
- $message=ReplaceFields($message, $fieldsarray);
- $subject=ReplaceFields($subject, $fieldsarray);
+ $sRegisterError='';
+ $aReplacement['REGISTERERROR'] = $sRegisterError;
+ $aReplacement['REGISTERMESSAGE1'] = $clang->gT("You must be registered to complete this survey");
+ $aReplacement['REGISTERMESSAGE2'] = $clang->gT("You may register for this survey if you wish to take part.")."
\n".$clang->gT("Enter your details below, and an email containing the link to participate in this survey will be sent immediately.");
+ $aData['thissurvey'] = $aSurveyInfo;
+ Yii::app()->setConfig('surveyID',$iSurveyId);//Needed for languagechanger
+ $aData['languagechanger'] = makeLanguageChangerSurvey($clang->langcode);
+ return templatereplace(file_get_contents("$sTemplate/register.pstpl"),$aReplacement,$aData);
+ }
- $html = ""; //Set variable
- $sitename = Yii::app()->getConfig('sitename');
+ /**
+ * Send the register email with $_POST value
+ * @param $iSurveyId Survey Id to register
+ * @return array sent: boolean, message :the message to be shown
+ */
+ public function sendRegistrationEmail($iSurveyId){
+
+ $clang = Yii::app()->lang;
+ $aSurveyInfo=getSurveyInfo($iSurveyId,$clang->langcode);
+ $sLanguage=$clang->langcode;
- if (SendEmailMessage($message, $subject, Yii::app()->request->getPost('register_email'), $from, $sitename,$useHtmlEmail,getBounceEmail($iSurveyID)))
- {
- // TLR change to put date into sent
- $today = dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i", Yii::app()->getConfig('timeadjust'));
- $oToken->sent=$today;
- $oToken->save();
- $html="
'.$clang->gT("This is a controlled survey. You need a valid token to participate.")."
";
@@ -993,11 +994,10 @@ function buildsurveysession($surveyid,$preview=false)
: | " - . ": | \n" - . ": | \n" - . "$attribute) - { - if (empty($attribute['show_register']) || $attribute['show_register'] != 'Y') - continue; - - $_registerform .= ' - | ||
: | -- | ||||
: | |||||
" - . " |
+ | + 'register_firstname','class'=>'text')); ?> + | +
---|---|
+ | + 'register_lastname','class'=>'text')); ?> + | +
+ | + 'register_email','class'=>'text')); ?> + | +
+ | + "register_{$key}",'class'=>'text')); ?> + | +
+ | + 'loadsecurity','class'=>'text','size'=>'5','maxlength'=>'3')); ?> + | +
gT("Continue"),array('class'=>'button submit','id'=>'register')); ?> |