Exemplo n.º 1
0
    function validation($data, $files) {
        $errors = parent::validation($data, $files);
        $dates = array();
        $dateids = $data['sessiondateid'];
        $datecount = count($dateids);
        for ($i=0; $i < $datecount; $i++) {
            //only check timezones if datetimeknown is set
            if (!empty($data['datetimeknown'])) {
                $timezone = $data["sessiontimezone"][$i];
                if ($timezone == get_string('sessiontimezoneunknown', 'facetoface')) {
                    $errors['sessiontimezone['.$i.']'] =  get_string('error:mustspecifytimezone', 'facetoface');
                }
            }
            $starttime = $data["timestart[$i]"];
            $endtime = $data["timefinish[$i]"];
            $removecheckbox = empty($data["datedelete"]) ? array() : $data["datedelete"];
            if ($starttime > $endtime && !isset($removecheckbox[$i])) {
                $errstr = get_string('error:sessionstartafterend', 'facetoface');
                $errors['timestart['.$i.']'] = $errstr;
                $errors['timefinish['.$i.']'] = $errstr;
                unset($errstr);
                continue;
            }
            //check this date does not overlap with any previous dates - time overlap logic from a Stack Overflow post
            if (!empty($dates)) {
                foreach ($dates as $existing) {
                    if (($endtime > $existing->timestart) && ($existing->timefinish > $starttime)) {
                        //this date clashes with an existing date
                        $errors['timestart['.$i.']'] = get_string('error:sessiondatesconflict', 'facetoface');
                    }
                }
            }
            // If valid date, add to array
            $date = new object();
            $date->timestart = $starttime;
            $date->timefinish = $endtime;
            $dates[] = $date;
        }

        $datefound = false;
        if (!empty($data['datetimeknown'])) {
            for ($i = 0; $i < $data['date_repeats']; $i++) {
                if (empty($data['datedelete'][$i])) {
                    $datefound = true;
                    break;
                }
            }

            if (!$datefound) {
                $errors['datedelete[0]'] = get_string('validation:needatleastonedate', 'facetoface');
            }
        }

        // Check the availabilty of trainers if scheduling not allowed
        $trainerdata = !empty($data['trainerrole']) ? $data['trainerrole'] : array();
        $allowconflicts = !empty($data['allowconflicts']);

        if ($datefound && !$allowconflicts && is_array($trainerdata)) {
            $wheresql = '';
            $whereparams = array();
            if (!empty($this->_customdata['s'])) {
                $wheresql = ' AND s.id != ?';
                $whereparams[] = $this->_customdata['s'];
            }

            // Loop through roles
            $hasconflicts = 0;
            $context = context_course::instance($this->_customdata['course']->id);
            foreach ($trainerdata as $roleid => $trainers) {
                // Attempt to load users with this role in this context.
                $trainerlist = get_role_users($roleid, $context, true, 'u.id, u.firstname, u.lastname', 'u.id ASC');
                // Initialize error variable.
                $trainererrors = '';
                // Loop through trainers in this role.
                foreach ($trainers as $trainer) {

                    if (!$trainer) {
                        continue;
                    }

                    // Check their availability.
                    $availability = facetoface_get_sessions_within($dates, $trainer, $wheresql, $whereparams);
                    if (!empty($availability)) {
                        // Verify if trainers come in form of checkboxes or dropdown list to properly place the errors.
                        if (isset($this->_form->_types["trainerrole[{$roleid}][{$trainer}]"])) {
                            $errors["trainerrole[{$roleid}][{$trainer}]"] = facetoface_get_session_involvement($trainerlist[$trainer], $availability);
                        } else if (isset($this->_form->_types["trainerrole[{$roleid}]"])) {
                            $trainererrors .= html_writer::tag('div', facetoface_get_session_involvement($trainerlist[$trainer], $availability));
                        }
                        ++$hasconflicts;
                    }
                }

                if (isset($this->_form->_types["trainerrole[{$roleid}]"]) && $trainererrors != '') {
                    $errors["trainerrole[{$roleid}]"] = $trainererrors;
                }
            }

            // If there are conflicts, add a help message to checkbox
            if ($hasconflicts) {
                if ($hasconflicts > 1) {
                    $errors['allowconflicts'] = get_string('error:therearexconflicts', 'facetoface', $hasconflicts);
                } else {
                    $errors['allowconflicts'] = get_string('error:thereisaconflict', 'facetoface');
                }
            }
        }

        //check capcity is a number
        if (empty($data['capacity'])) {
            $errors['capacity'] = get_string('error:capacityzero', 'facetoface');
        } else {
            $capacity = $data['capacity'];
            if (!(is_numeric($capacity) && (intval($capacity) == $capacity) && $capacity > 0)) {
                $errors['capacity'] = get_string('error:capacitynotnumeric', 'facetoface');
            }
        }

        return $errors;
    }
Exemplo n.º 2
0
/**
 * Import user and signup to session
 *
 * @access  public
 * @param   object  $course             Record from the course table
 * @param   object  $facetoface         Record from the facetoface table
 * @param   object  $session            Session to signup user to
 * @param   mixed   $userid             User to signup (normally int)
 * @param   array   $params             Optional suppressemail, ignoreconflicts, bulkaddsource, discountcode, notificationtype
 *          boolean $suppressemail      Suppress notifications flag
 *          boolean $ignoreconflicts    Ignore booking conflicts flag
 *          string  $bulkaddsource      Flag to indicate if $userid is actually another field
 *          string  $discountcode       Optional A user may specify a discount code
 *          integer $notificationtype   Optional A user may choose the type of notifications they will receive
 * @return  array
 */
function facetoface_user_import($course, $facetoface, $session, $userid, $params = array()) {
    global $DB, $CFG, $USER;

    $result = array();
    $result['id'] = $userid;

    $suppressemail    = (isset($params['suppressemail'])    ? $params['suppressemail']    : false);
    $ignoreconflicts  = (isset($params['ignoreconflicts'])  ? $params['ignoreconflicts']  : false);
    $bulkaddsource    = (isset($params['bulkaddsource'])    ? $params['bulkaddsource']    : 'bulkaddsourceuserid');
    $discountcode     = (isset($params['discountcode'])     ? $params['discountcode']     : '');
    $notificationtype = (isset($params['notificationtype']) ? $params['notificationtype'] : MDL_F2F_BOTH);

    if (isset($params['approvalreqd'])) {
        // Overwrite default behaviour as bulkadd_* is requested
        $facetoface->approvalreqd = $params['approvalreqd'];
        $facetoface->ccmanager = (isset($params['ccmanager']) ? $params['ccmanager'] : 0);
    }

    // Check parameters.
    if ($bulkaddsource == 'bulkaddsourceuserid') {
        if (!is_int($userid) && !ctype_digit($userid)) {
            $result['name'] = '';
            $result['result'] = get_string('error:userimportuseridnotanint', 'facetoface', $userid);
            return $result;
        }
    }

    // Get user.
    switch ($bulkaddsource) {
        case 'bulkaddsourceuserid':
            $user = $DB->get_record('user', array('id' => $userid));
            break;
        case 'bulkaddsourceidnumber':
            $user = $DB->get_record('user', array('idnumber' => $userid));
            break;
        case 'bulkaddsourceusername':
            $user = $DB->get_record('user', array('username' => $userid));
            break;
    }
    if (!$user) {
        $result['name'] = '';
        $a = array('fieldname' => get_string($bulkaddsource, 'facetoface'), 'value' => $userid);
        $result['result'] = get_string('userdoesnotexist', 'facetoface', $a);
        return $result;
    }

    $result['name'] = fullname($user);

    if (isguestuser($user)) {
        $a = array('fieldname' => get_string($bulkaddsource, 'facetoface'), 'value' => $userid);
        $result['result'] = get_string('cannotsignupguest', 'facetoface', $a);
        return $result;
    }

    // Make sure that the user is enroled in the course
    $context   = context_course::instance($course->id);
    if (!is_enrolled($context, $user)) {

        $defaultlearnerrole = $DB->get_record('role', array('id' => $CFG->learnerroleid));

        if (!enrol_try_internal_enrol($course->id, $user->id, $defaultlearnerrole->id, time())) {
            $result['result'] = get_string('error:enrolmentfailed', 'facetoface', fullname($user));
            return $result;
        }
    }

    // Check if they are already signed up
    $minimumstatus = ($session->datetimeknown) ? MDL_F2F_STATUS_BOOKED : MDL_F2F_STATUS_REQUESTED;
    // If multiple sessions are allowed then just check against this session
    // Otherwise check against all sessions
    $multisessionid = ($facetoface->multiplesessions ? $session->id : null);
    if (facetoface_get_user_submissions($facetoface->id, $user->id, $minimumstatus, MDL_F2F_STATUS_FULLY_ATTENDED, $multisessionid)) {
        if ($user->id == $USER->id) {
            $result['result'] = get_string('error:addalreadysignedupattendeeaddself', 'facetoface');
        } else {
            $result['result'] = get_string('error:addalreadysignedupattendee', 'facetoface');
        }
        return $result;
    }

    if (!facetoface_session_has_capacity($session, $context)) {
        if ($session->allowoverbook) {
            $status = MDL_F2F_STATUS_WAITLISTED;
        } else {
            $result['result'] = get_string('full', 'facetoface');
            return $result;
        }
    }

    // Check if we are waitlisting or booking
    if ($session->datetimeknown) {
        if (!isset($status)) {
            $status = MDL_F2F_STATUS_BOOKED;
        }

        // Check if there are any date conflicts
        if (!$ignoreconflicts) {
            $dates = facetoface_get_session_dates($session->id);
            if ($availability = facetoface_get_sessions_within($dates, $user->id)) {
                $result['result'] = facetoface_get_session_involvement($user, $availability);
                $result['conflict'] = true;
                return $result;
            }
        }
    } else {
        $status = MDL_F2F_STATUS_WAITLISTED;
    }

    // Finally attempt to enrol
    if (!facetoface_user_signup(
        $session,
        $facetoface,
        $course,
        $discountcode,
        $notificationtype,
        $status,
        $user->id,
        !$suppressemail)) {
            $result['result'] = get_string('error:addattendee', 'facetoface', fullname($user));
            return $result;
    }

    $result['result'] = true;
    return $result;
}
Exemplo n.º 3
0
    function validation($data, $files) {
		global $DB;
		$errors = parent::validation($data, $files);
        $dates = array();
        $dateids = $data['sessiondateid'];
        $datecount = count($dateids);
		//print_object($datecount);
      for ($i=0; $i < $datecount; $i++) {
            //only check timezones if datetimeknown is set
			
			$timestart=date('Y-m-d H:i',$data["timestart[$i]"]);
			$timefinish=date('Y-m-d H:i',$data["timefinish[$i]"]);
			
			
				$date1=date_create("$timestart");
				$date2=date_create("$timefinish");
				$diff=date_diff($date1,$date2);
				//echo $diff->format("%H")."<br/>";
				//echo $diff->format("%I")."<br/>";
				//echo $diff->format("%S")."<br/>";
				
				if(($diff->format("%R%a")!='-0'||$diff->format("%R%a")!='0')){
					if($diff->format("%R%a")<'0' || $diff->format("%R%a")>'0'){
						$errstr = 'Session end date should be same as session start date';
                        $errors['timefinish['.$i.']'] = $errstr;
					}
					
				}
				elseif(($diff->format("%H")>'08')){
					//echo $diff->format("%H");
						$errstr = 'Session can be max for 8 hours.';
                        $errors['timefinish['.$i.']'] = $errstr;
					
				}elseif(($diff->format("%H")=='08')){
					if(($diff->format("%I")>'00')){
						
							$errstr = 'Session can be max for 8 hours.';
							$errors['timefinish['.$i.']'] = $errstr;
						
					}/*elseif(($diff->format("%S")>00)){
						
							$errstr = 'Session can be max for 8 hours.';
							$errors['timefinish['.$i.']'] = $errstr;
						
					}*/
				}
			
            if (!empty($data['datetimeknown'])) {
                $timezone = $data["sessiontimezone"][$i];
                if ($timezone == get_string('sessiontimezoneunknown', 'facetoface')) {
                    $errors['sessiontimezone['.$i.']'] =  get_string('error:mustspecifytimezone', 'facetoface');
                }
            }
			
            $starttime = userdate("%d/%m/%Y",$data["timestart[$i]"]);
		    $endtime = userdate("%d/%m/%Y",$data["timefinish[$i]"]);
			
			
			// $starttime = $data["timestart[$i]"];
			//$endtime = $data["timefinish[$i]"];
			
            if($data['f']==0&&$data['id']){
				$session_id=$data['id'];
				$facetoface=$DB->get_record_sql("SELECT facetoface FROM {facetoface_sessions} where id=$session_id");
				$batchstart=userdate("%d/%m/%Y",$DB->get_field('facetoface','startdate',array('id'=>$facetoface->facetoface)));
			    $batchend=userdate("%d/%m/%Y",$DB->get_field('facetoface','enddate',array('id'=>$facetoface->facetoface)));
			}else{
				$batchstart=userdate("%d/%m/%Y",$DB->get_field('facetoface','startdate',array('id'=>$data['f'])));
			    $batchend=userdate("%d/%m/%Y",$DB->get_field('facetoface','enddate',array('id'=>$data['f'])));
			}
	//		 $datediff = strtotime($batchstart) - strtotime($starttime);
	//	  
	//		 $diff_batchstart_starttime =  floor($datediff/(60*60*24)) ; /* This condition checked date difference between
	//
	// 											  *last session finish date and present date */
	//		echo strtotime($batchstart)."<br/>";
			//print_object($data);
			//print_object($starttime);
			//print_object($endtime);
			//print_object($batchstart);
			//print_object($batchend);
			//exit;
			//echo userdate("%d/%m/%Y",$data["timefinish[$i]"]);
			//echo '<br />';
			//echo strtotime(date("d/m/Y",$data["timefinish[$i]"]))."<br/>";
			//echo $endtime;
				//echo $batchstart.">".$endtime."<br/>".$endtime.">".$batchend;
			//if($batchstart>$starttime||$starttime>$batchend){
			if(($batchstart<$starttime&&$starttime>$batchend)||($starttime<$batchstart)){
				$errstr = 'Session start date should be (after batch start date)/(before batch end date)';
                $errors['timestart['.$i.']'] = $errstr;
                            
			}
			
			if(($batchstart>$endtime&&$endtime<$batchend)||($endtime>$batchend)){
			
			//if($endtime>$batchend){
			//if($batchend<$endtime){
				$errstr = 'Session end dates should be (before batch end date)/(after batch start date)';
                $errors['timefinish['.$i.']'] = $errstr;
             
			}
			if($starttime>$endtime){
				$errstr = 'Session end date should be same as session start date';
                $errors['timefinish['.$i.']'] = $errstr;
			}
			
			
//			if($starttime<$endtime){
//				$errstr = 'Session end dates should be before session first date)';
//                $errors['timefinish['.$i.']'] = $errstr;
//			}
			
            //$removecheckbox = empty($data["datedelete"]) ? array() : $data["datedelete"];
            //if ($starttime > $endtime && !isset($removecheckbox[$i])) {
            //    $errstr = get_string('error:sessionstartafterend', 'facetoface');
            //    //$errors['timestart['.$i.']'] = $errstr;
            //    $errors['timefinish['.$i.']'] = $errstr;
            //    unset($errstr);
            //    continue;
            //}
            ////check this date does not overlap with any previous dates - time overlap logic from a Stack Overflow post
            //if (!empty($dates)) {
            //    foreach ($dates as $existing) {
            //        if (($endtime > $existing->timestart) && ($existing->timefinish > $starttime)) {
            //            //this date clashes with an existing date
            //            $errors['timestart['.$i.']'] = get_string('error:sessiondatesconflict', 'facetoface');
            //        }
            //    }
            //}
            // If valid date, add to array
            $date = new object();
            $date->timestart = $starttime;
            $date->timefinish = $endtime;
            $dates[] = $date;
        }
         
        $datefound = false;
        if (!empty($data['datetimeknown'])) {
            for ($i = 0; $i < $data['date_repeats']; $i++) {
                if (empty($data['datedelete'][$i])) {
                    $datefound = true;
                    break;
                }
            }

            if (!$datefound) {
                $errors['datedelete[0]'] = get_string('validation:needatleastonedate', 'facetoface');
            }
        }

        // Check the availabilty of trainers if scheduling not allowed
        $trainerdata = !empty($data['trainerrole']) ? $data['trainerrole'] : array();
        $allowconflicts = !empty($data['allowconflicts']);

        if ($datefound && !$allowconflicts && is_array($trainerdata)) {
            $wheresql = '';
            $whereparams = array();
            if (!empty($this->_customdata['s'])) {
                $wheresql = ' AND s.id != ?';
                $whereparams[] = $this->_customdata['s'];
            }

            // Loop through roles
            $hasconflicts = 0;
            $context = context_course::instance($this->_customdata['course']->id);
            foreach ($trainerdata as $roleid => $trainers) {
                // Attempt to load users with this role in this context.
                $trainerlist = get_role_users($roleid, $context, true, 'u.id, u.firstname, u.lastname', 'u.id ASC');
                // Initialize error variable.
                $trainererrors = '';
                // Loop through trainers in this role.
                foreach ($trainers as $trainer) {

                    if (!$trainer) {
                        continue;
                    }

                    // Check their availability.
                    $availability = facetoface_get_sessions_within($dates, $trainer, $wheresql, $whereparams);
                    if (!empty($availability)) {
                        // Verify if trainers come in form of checkboxes or dropdown list to properly place the errors.
                        if (isset($this->_form->_types["trainerrole[{$roleid}][{$trainer}]"])) {
                            $errors["trainerrole[{$roleid}][{$trainer}]"] = facetoface_get_session_involvement($trainerlist[$trainer], $availability);
                        } else if (isset($this->_form->_types["trainerrole[{$roleid}]"])) {
                            $trainererrors .= html_writer::tag('div', facetoface_get_session_involvement($trainerlist[$trainer], $availability));
                        }
                        ++$hasconflicts;
                    }
                }

                if (isset($this->_form->_types["trainerrole[{$roleid}]"]) && $trainererrors != '') {
                    $errors["trainerrole[{$roleid}]"] = $trainererrors;
                }
            }

            // If there are conflicts, add a help message to checkbox
            if ($hasconflicts) {
                if ($hasconflicts > 1) {
                    $errors['allowconflicts'] = get_string('error:therearexconflicts', 'facetoface', $hasconflicts);
                } else {
                    $errors['allowconflicts'] = get_string('error:thereisaconflict', 'facetoface');
                }
            }
        }

        //check capcity is a number
        if (empty($data['capacity'])) {
            $errors['capacity'] = get_string('error:capacityzero', 'facetoface');
        } else {
            $capacity = $data['capacity'];
            if (!(is_numeric($capacity) && (intval($capacity) == $capacity) && $capacity > 0)) {
                $errors['capacity'] = get_string('error:capacitynotnumeric', 'facetoface');
            }
        }

        return $errors;
    }