/**
  *		This function is a singleton method used to instantiate the EEM_Attendee object
  *
  *		@access public
  *		@return EEM_Question_Group instance
  */
 public static function instance()
 {
     // check if instance of EEM_Attendee already exists
     if (self::$_instance === NULL) {
         // instantiate Espresso_model
         self::$_instance = new self();
     }
     // EEM_Attendee object
     return self::$_instance;
 }
    public function additional_questions($post_id, $post)
    {
        $this->_event = $this->_adminpage_obj->get_event_object();
        $event_id = $this->_event->ID();
        ?>
		<div class="inside">
			<p><strong>
					<?php 
        _e('Question Groups', 'event_espresso');
        ?>
				</strong><br />
				<?php 
        _e('Add a pre-populated', 'event_espresso');
        ?>
				<a href="admin.php?page=espresso_registration_form" target="_blank">
					<?php 
        _e('group of questions', 'event_espresso');
        ?>
				</a>
				<?php 
        _e('to your event.', 'event_espresso');
        ?>
			</p>
			<?php 
        $qsg_where['QSG_deleted'] = FALSE;
        $query_params = array($qsg_where, 'order_by' => array('QSG_order' => 'ASC'));
        $QSGs = EEM_Question_Group::instance()->get_all($query_params);
        $EQGs = !empty($event_id) ? $this->_event->get_many_related('Question_Group', array(array('Event_Question_Group.EQG_primary' => 0))) : array();
        $EQGids = array_keys($EQGs);
        if (!empty($QSGs)) {
            $html = count($QSGs) > 10 ? '<div style="height:250px;overflow:auto;">' : '';
            foreach ($QSGs as $QSG) {
                $checked = in_array($QSG->ID(), $EQGids) ? ' checked="checked" ' : '';
                $edit_link = $this->_adminpage_obj->add_query_args_and_nonce(array('action' => 'edit_question_group', 'QSG_ID' => $QSG->ID()), EE_FORMS_ADMIN_URL);
                $html .= '
					<p id="event-question-group-' . $QSG->ID() . '">
						<input value="' . $QSG->ID() . '" type="checkbox" name="add_attendee_question_groups[' . $QSG->ID() . ']"' . $checked . ' />
						<a href="' . $edit_link . '" title="' . sprintf(esc_attr__('Edit %s Group', 'event_espresso'), $QSG->get('QSG_name')) . '" target="_blank">' . $QSG->get('QSG_name') . '</a>
					</p>';
            }
            $html .= count($QSGs) > 10 ? '</div>' : '';
            echo $html;
        } else {
            echo __('There seems to be a problem with your questions. Please contact support@eventespresso.com', 'event_espresso');
        }
        do_action('AHEE__espresso_events_Registration_Form_Hooks__additional_questions__after_content');
        ?>
		</div>
		<?php 
    }
 public function build_form_from_registration()
 {
     $reg = $this->get_registration();
     if (!$reg instanceof EE_Registration) {
         throw new EE_Error(__('We cannot build the registration custom questions form because there is no registration set on it yet', 'event_espresso'));
     }
     //we want to get all their question groups
     $question_groups = EEM_Question_Group::instance()->get_all(array(array('Event_Question_Group.EVT_ID' => $reg->event_ID(), 'Event_Question_Group.EQG_primary' => $reg->count() == 1 ? TRUE : FALSE, 'OR' => array('Question.QST_system*blank' => '', 'Question.QST_system*null' => array('IS_NULL'))), 'order_by' => array('QSG_order' => 'ASC')));
     //get each question groups questions
     foreach ($question_groups as $question_group) {
         if ($question_group instanceof EE_Question_Group) {
             $this->_subsections[$question_group->ID()] = $this->build_subform_from_question_group($question_group, $reg);
         }
     }
 }
 /**
  * test that questions aren't editable until you're logged in,
  * then you can only edit your own non-system questions,
  * and then you can edit others if you have that cap,
  * and then you can edit others if you have that cap
  */
 function test_get_all__caps__edit()
 {
     //verify we only start off with NO question gruops or question group questions
     EEM_Question_Group::instance()->delete_permanently(EEM_Question_Group::instance()->alter_query_params_so_deleted_and_undeleted_items_included(), false);
     $this->assertEquals(0, EEM_Question_Group::instance()->count(EEM_Question_Group::instance()->alter_query_params_so_deleted_and_undeleted_items_included()));
     EEM_Question_Group_Question::instance()->delete(array(), false);
     $this->assertEquals(0, EEM_Question_Group_Question::instance()->count());
     global $current_user;
     $user = $this->factory->user->create_and_get();
     $qg1 = $this->new_model_obj_with_dependencies('Question_Group', array('QSG_system' => 0, 'QSG_wp_user' => $user->ID));
     $qg2_system = $this->new_model_obj_with_dependencies('Question_Group', array('QSG_system' => 3, 'QSG_wp_user' => $user->ID));
     $qg3_others = $this->new_model_obj_with_dependencies('Question_Group', array('QSG_system' => 0, 'QSG_wp_user' => 9999));
     $qg4_others_system = $this->new_model_obj_with_dependencies('Question_Group', array('QSG_system' => 4, 'QSG_wp_user' => 9999));
     $q = $this->new_model_obj_with_dependencies('Question');
     $qgq1 = $this->new_model_obj_with_dependencies('Question_Group_Question', array('QST_ID' => $q->ID(), 'QSG_ID' => $qg1->ID()));
     $qgq2 = $this->new_model_obj_with_dependencies('Question_Group_Question', array('QST_ID' => $q->ID(), 'QSG_ID' => $qg2_system->ID()));
     $qgq3 = $this->new_model_obj_with_dependencies('Question_Group_Question', array('QST_ID' => $q->ID(), 'QSG_ID' => $qg3_others->ID()));
     $qgq4 = $this->new_model_obj_with_dependencies('Question_Group_Question', array('QST_ID' => $q->ID(), 'QSG_ID' => $qg4_others_system->ID()));
     //I am not yet logged in, so I shouldnt be able to edit any
     $this->assertEquals(0, EEM_Question_Group_Question::instance()->count(array('caps' => EEM_Base::caps_edit)));
     //now log in and see I can edit my own
     $current_user = $user;
     $user->add_cap('ee_edit_question_groups');
     $i_can_edit = EEM_Question_Group_Question::instance()->get_all(array('caps' => EEM_Base::caps_edit));
     $this->assertEquals($qgq1, reset($i_can_edit));
     $this->assertEquals($qgq3, next($i_can_edit));
     $this->assertEquals(2, count($i_can_edit));
     //now give them the ability to edit system questions
     $user->add_cap('ee_edit_system_question_groups');
     $i_can_edit = EEM_Question_Group_Question::instance()->get_all(array('caps' => EEM_Base::caps_edit));
     $this->assertEquals($qgq1, reset($i_can_edit));
     $this->assertEquals($qgq2, next($i_can_edit));
     $this->assertEquals($qgq3, next($i_can_edit));
     $this->assertEquals($qgq4, next($i_can_edit));
     $this->assertEquals(4, count($i_can_edit));
 }
 /**
  * initialize_system_questions
  *
  * 	@access public
  * 	@static
  * 	@return void
  */
 public static function initialize_system_questions()
 {
     // QUESTION GROUPS
     global $wpdb;
     $table_name = EEH_Activation::ensure_table_name_has_prefix('esp_question_group');
     $SQL = "SELECT QSG_system FROM {$table_name} WHERE QSG_system != 0";
     // what we have
     $question_groups = $wpdb->get_col($SQL);
     // check the response
     $question_groups = is_array($question_groups) ? $question_groups : array();
     // what we should have
     $QSG_systems = array(1, 2);
     // loop thru what we should have and compare to what we have
     foreach ($QSG_systems as $QSG_system) {
         // reset values array
         $QSG_values = array();
         // if we don't have what we should have (but use $QST_system as as string because that's what we got from the db)
         if (!in_array("{$QSG_system}", $question_groups)) {
             // add it
             switch ($QSG_system) {
                 case 1:
                     $QSG_values = array('QSG_name' => __('Personal Information', 'event_espresso'), 'QSG_identifier' => 'personal-information-' . time(), 'QSG_desc' => '', 'QSG_order' => 1, 'QSG_show_group_name' => 1, 'QSG_show_group_desc' => 1, 'QSG_system' => EEM_Question_Group::system_personal, 'QSG_deleted' => 0);
                     break;
                 case 2:
                     $QSG_values = array('QSG_name' => __('Address Information', 'event_espresso'), 'QSG_identifier' => 'address-information-' . time(), 'QSG_desc' => '', 'QSG_order' => 2, 'QSG_show_group_name' => 1, 'QSG_show_group_desc' => 1, 'QSG_system' => EEM_Question_Group::system_address, 'QSG_deleted' => 0);
                     break;
             }
             // make sure we have some values before inserting them
             if (!empty($QSG_values)) {
                 // insert system question
                 $wpdb->insert($table_name, $QSG_values, array('%s', '%s', '%s', '%d', '%d', '%d', '%d', '%d'));
                 $QSG_IDs[$QSG_system] = $wpdb->insert_id;
             }
         }
     }
     // QUESTIONS
     global $wpdb;
     $table_name = EEH_Activation::ensure_table_name_has_prefix('esp_question');
     $SQL = "SELECT QST_system FROM {$table_name} WHERE QST_system != ''";
     // what we have
     $questions = $wpdb->get_col($SQL);
     // what we should have
     $QST_systems = array('fname', 'lname', 'email', 'address', 'address2', 'city', 'state', 'country', 'zip', 'phone');
     $order_for_group_1 = 1;
     $order_for_group_2 = 1;
     // loop thru what we should have and compare to what we have
     foreach ($QST_systems as $QST_system) {
         // reset values array
         $QST_values = array();
         // if we don't have what we should have
         if (!in_array($QST_system, $questions)) {
             // add it
             switch ($QST_system) {
                 case 'fname':
                     $QST_values = array('QST_display_text' => __('First Name', 'event_espresso'), 'QST_admin_label' => __('First Name - System Question', 'event_espresso'), 'QST_system' => 'fname', 'QST_type' => 'TEXT', 'QST_required' => 1, 'QST_required_text' => __('This field is required', 'event_espresso'), 'QST_order' => 1, 'QST_admin_only' => 0, 'QST_wp_user' => self::get_default_creator_id(), 'QST_deleted' => 0);
                     break;
                 case 'lname':
                     $QST_values = array('QST_display_text' => __('Last Name', 'event_espresso'), 'QST_admin_label' => __('Last Name - System Question', 'event_espresso'), 'QST_system' => 'lname', 'QST_type' => 'TEXT', 'QST_required' => 1, 'QST_required_text' => __('This field is required', 'event_espresso'), 'QST_order' => 2, 'QST_admin_only' => 0, 'QST_wp_user' => self::get_default_creator_id(), 'QST_deleted' => 0);
                     break;
                 case 'email':
                     $QST_values = array('QST_display_text' => __('Email Address', 'event_espresso'), 'QST_admin_label' => __('Email Address - System Question', 'event_espresso'), 'QST_system' => 'email', 'QST_type' => 'TEXT', 'QST_required' => 1, 'QST_required_text' => __('This field is required', 'event_espresso'), 'QST_order' => 3, 'QST_admin_only' => 0, 'QST_wp_user' => self::get_default_creator_id(), 'QST_deleted' => 0);
                     break;
                 case 'address':
                     $QST_values = array('QST_display_text' => __('Address', 'event_espresso'), 'QST_admin_label' => __('Address - System Question', 'event_espresso'), 'QST_system' => 'address', 'QST_type' => 'TEXT', 'QST_required' => 0, 'QST_required_text' => __('This field is required', 'event_espresso'), 'QST_order' => 4, 'QST_admin_only' => 0, 'QST_wp_user' => self::get_default_creator_id(), 'QST_deleted' => 0);
                     break;
                 case 'address2':
                     $QST_values = array('QST_display_text' => __('Address2', 'event_espresso'), 'QST_admin_label' => __('Address2 - System Question', 'event_espresso'), 'QST_system' => 'address2', 'QST_type' => 'TEXT', 'QST_required' => 0, 'QST_required_text' => __('This field is required', 'event_espresso'), 'QST_order' => 5, 'QST_admin_only' => 0, 'QST_wp_user' => self::get_default_creator_id(), 'QST_deleted' => 0);
                     break;
                 case 'city':
                     $QST_values = array('QST_display_text' => __('City', 'event_espresso'), 'QST_admin_label' => __('City - System Question', 'event_espresso'), 'QST_system' => 'city', 'QST_type' => 'TEXT', 'QST_required' => 0, 'QST_required_text' => __('This field is required', 'event_espresso'), 'QST_order' => 6, 'QST_admin_only' => 0, 'QST_wp_user' => self::get_default_creator_id(), 'QST_deleted' => 0);
                     break;
                 case 'state':
                     $QST_values = array('QST_display_text' => __('State/Province', 'event_espresso'), 'QST_admin_label' => __('State/Province - System Question', 'event_espresso'), 'QST_system' => 'state', 'QST_type' => 'STATE', 'QST_required' => 0, 'QST_required_text' => __('This field is required', 'event_espresso'), 'QST_order' => 7, 'QST_admin_only' => 0, 'QST_wp_user' => self::get_default_creator_id(), 'QST_deleted' => 0);
                     break;
                 case 'country':
                     $QST_values = array('QST_display_text' => __('Country', 'event_espresso'), 'QST_admin_label' => __('Country - System Question', 'event_espresso'), 'QST_system' => 'country', 'QST_type' => 'COUNTRY', 'QST_required' => 0, 'QST_required_text' => __('This field is required', 'event_espresso'), 'QST_order' => 8, 'QST_admin_only' => 0, 'QST_wp_user' => self::get_default_creator_id(), 'QST_deleted' => 0);
                     break;
                 case 'zip':
                     $QST_values = array('QST_display_text' => __('Zip/Postal Code', 'event_espresso'), 'QST_admin_label' => __('Zip/Postal Code - System Question', 'event_espresso'), 'QST_system' => 'zip', 'QST_type' => 'TEXT', 'QST_required' => 0, 'QST_required_text' => __('This field is required', 'event_espresso'), 'QST_order' => 9, 'QST_admin_only' => 0, 'QST_wp_user' => self::get_default_creator_id(), 'QST_deleted' => 0);
                     break;
                 case 'phone':
                     $QST_values = array('QST_display_text' => __('Phone Number', 'event_espresso'), 'QST_admin_label' => __('Phone Number - System Question', 'event_espresso'), 'QST_system' => 'phone', 'QST_type' => 'TEXT', 'QST_required' => 0, 'QST_required_text' => __('This field is required', 'event_espresso'), 'QST_order' => 10, 'QST_admin_only' => 0, 'QST_wp_user' => self::get_default_creator_id(), 'QST_deleted' => 0);
                     break;
             }
             if (!empty($QST_values)) {
                 // insert system question
                 $wpdb->insert($table_name, $QST_values, array('%s', '%s', '%s', '%s', '%d', '%s', '%d', '%d', '%d', '%d'));
                 $QST_ID = $wpdb->insert_id;
                 // QUESTION GROUP QUESTIONS
                 if (in_array($QST_system, array('fname', 'lname', 'email'))) {
                     $system_question_we_want = EEM_Question_Group::system_personal;
                 } else {
                     $system_question_we_want = EEM_Question_Group::system_address;
                 }
                 if (isset($QSG_IDs[$system_question_we_want])) {
                     $QSG_ID = $QSG_IDs[$system_question_we_want];
                 } else {
                     $id_col = EEM_Question_Group::instance()->get_col(array(array('QSG_system' => $system_question_we_want)));
                     if (is_array($id_col)) {
                         $QSG_ID = reset($id_col);
                     } else {
                         //ok so we didn't find it in the db either?? that's weird because we should have inserted it at the start of this method
                         EE_Log::instance()->log(__FILE__, __FUNCTION__, sprintf(__('Could not associate question %1$s to a question group because no system question group existed', 'event_espresso'), $QST_ID), 'error');
                         continue;
                     }
                 }
                 // add system questions to groups
                 $wpdb->insert(EEH_Activation::ensure_table_name_has_prefix('esp_question_group_question'), array('QSG_ID' => $QSG_ID, 'QST_ID' => $QST_ID, 'QGQ_order' => $QSG_ID == 1 ? $order_for_group_1++ : $order_for_group_2++), array('%d', '%d', '%d'));
             }
         }
     }
 }
 /**
  * @param            $per_page
  * @param int        $current_page
  * @param bool|false $count
  * @return \EE_Soft_Delete_Base_Class[]
  */
 public function get_question_groups($per_page, $current_page = 1, $count = FALSE)
 {
     /** @type EEM_Question_Group $questionGroupModel */
     $questionGroupModel = EEM_Question_Group::instance();
     $query_params = $this->get_query_params($questionGroupModel, $per_page, $current_page);
     $questionGroups = $questionGroupModel->get_all($query_params);
     //note: this a subclass of EEM_Soft_Delete_Base, so this is actually only getting non-trashed items
     return $questionGroups;
 }
 /**
  * method for performing updates to question order
  * @return array results array
  */
 public function update_question_group_order()
 {
     $success = __('Question group order was updated successfully.', 'event_espresso');
     // grab our row IDs
     $row_ids = isset($this->_req_data['row_ids']) && !empty($this->_req_data['row_ids']) ? explode(',', rtrim($this->_req_data['row_ids'], ',')) : FALSE;
     $perpage = !empty($this->_req_data['perpage']) ? (int) $this->_req_data['perpage'] : NULL;
     $curpage = !empty($this->_req_data['curpage']) ? (int) $this->_req_data['curpage'] : NULL;
     if (is_array($row_ids)) {
         //figure out where we start the row_id count at for the current page.
         $qsgcount = empty($curpage) ? 0 : ($curpage - 1) * $perpage;
         global $wpdb;
         for ($i = 0; $i < count($row_ids); $i++) {
             //Update the questions when re-ordering
             if (EEM_Question_Group::instance()->update(array('QSG_order' => $qsgcount), array(array('QSG_ID' => $row_ids[$i]))) === FALSE) {
                 $success = FALSE;
             }
             $qsgcount++;
         }
     } else {
         $success = FALSE;
     }
     $errors = !$success ? __('An error occurred. The question group order was not updated.', 'event_espresso') : FALSE;
     echo json_encode(array('return_data' => FALSE, 'success' => $success, 'errors' => $errors));
     die;
 }
    public function primary_questions($post_id, $post)
    {
        $this->_event = $this->_adminpage_obj->get_event_object();
        $event_id = $this->_event->ID();
        ?>
		<div class="inside">
			<p><strong>
					<?php 
        _e('Question Groups', 'event_espresso');
        ?>
				</strong><br />
				<?php 
        _e('Add a pre-populated', 'event_espresso');
        ?>
				<a href="admin.php?page=espresso_registration_form" target="_blank">
					<?php 
        _e('group of questions', 'event_espresso');
        ?>
				</a>
				<?php 
        _e('to your event. The personal information group is required for all events.', 'event_espresso');
        ?>
			</p>
			<?php 
        $qsg_where['QSG_deleted'] = FALSE;
        $query_params = array($qsg_where, 'order_by' => array('QSG_order' => 'ASC'));
        $QSGs = EEM_Question_Group::instance()->get_all($query_params);
        $EQGs = !empty($event_id) ? $this->_event->get_many_related('Question_Group', array(array('Event_Question_Group.EQG_primary' => 1))) : array();
        $EQGids = array_keys($EQGs);
        //EEH_Debug_Tools::printr( $QSGs, '$QSGs  <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' );
        if (!empty($QSGs)) {
            $html = count($QSGs) > 10 ? '<div style="height:250px;overflow:auto;">' : '';
            foreach ($QSGs as $QSG) {
                $checked = in_array($QSG->ID(), $EQGids) || $QSG->get('QSG_system') == 1 ? ' checked="checked"' : '';
                $visibility = $QSG->get('QSG_system') == 1 ? ' style="visibility:hidden"' : '';
                $edit_query_args = $this->_adminpage_obj->is_caf() ? array('action' => 'edit_question_group', 'QSG_ID' => $QSG->ID()) : array('action' => 'question_groups');
                $edit_link = $this->_adminpage_obj->add_query_args_and_nonce($edit_query_args, EE_FORMS_ADMIN_URL);
                $html .= '
					<p id="event-question-group-' . $QSG->ID() . '">
						<input value="' . $QSG->ID() . '" type="checkbox"' . $visibility . ' name="question_groups[' . $QSG->ID() . ']"' . $checked . ' />
						<a href="' . $edit_link . '" title="' . sprintf(esc_attr__('Edit %s Group', 'event_espresso'), $QSG->get('QSG_name')) . '" target="_blank">' . $QSG->get('QSG_name') . '</a>
					</p>';
            }
            $html .= count($QSGs) > 10 ? '</div>' : '';
            echo $html;
        } else {
            echo __('There seems to be a problem with your questions. Please contact support@eventespresso.com', 'event_espresso');
        }
        do_action('AHEE_event_editor_questions_notice');
        ?>
		</div>
		<?php 
    }