/**
  *		This function is a singleton method used to instantiate the EEM_Attendee object
  *
  *		@access public
  *		@return EEM_Question 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;
 }
 /**
  * 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));
 }
 protected function _insert_or_update_question_group($new_question_group = TRUE)
 {
     do_action('AHEE_log', __FILE__, __FUNCTION__, '');
     $set_column_values = $this->_set_column_values_for($this->_question_group_model);
     if ($new_question_group) {
         $QSG_ID = $this->_question_group_model->insert($set_column_values);
         $success = $QSG_ID ? 1 : 0;
     } else {
         $QSG_ID = absint($this->_req_data['QSG_ID']);
         unset($set_column_values['QSG_ID']);
         $success = $this->_question_group_model->update($set_column_values, array(array('QSG_ID' => $QSG_ID)));
     }
     // update the existing related questions
     // BUT FIRST...  delete the phone question from the Question_Group_Question if it is being added to this question group (therefore removed from the existing group)
     if (isset($this->_req_data['questions'], $this->_req_data['questions'][EEM_Attendee::phone_question_id])) {
         // delete where QST ID = system phone question ID and Question Group ID is NOT this group
         EEM_Question_Group_Question::instance()->delete(array(array('QST_ID' => EEM_Attendee::phone_question_id, 'QSG_ID' => array('!=', $QSG_ID))));
     }
     /** @type EE_Question_Group $question_group */
     $question_group = $this->_question_group_model->get_one_by_ID($QSG_ID);
     $questions = $question_group->questions();
     // make sure system phone question is added to list of questions for this group
     if (!isset($questions[EEM_Attendee::phone_question_id])) {
         $questions[EEM_Attendee::phone_question_id] = EEM_Question::instance()->get_one_by_ID(EEM_Attendee::phone_question_id);
     }
     foreach ($questions as $question_ID => $question) {
         // first we always check for order.
         if (!empty($this->_req_data['question_orders'][$question_ID])) {
             //update question order
             $question_group->update_question_order($question_ID, $this->_req_data['question_orders'][$question_ID]);
         }
         // then we always check if adding or removing.
         if (isset($this->_req_data['questions'], $this->_req_data['questions'][$question_ID])) {
             $question_group->add_question($question_ID);
         } else {
             // not found, remove it (but only if not a system question for the personal group)
             if (!($question->is_system_question() && $question_group->system_group() === EEM_Question_Group::system_personal)) {
                 $question_group->remove_question($question_ID);
             }
         }
     }
     // save new related questions
     if (isset($this->_req_data['questions'])) {
         foreach ($this->_req_data['questions'] as $QST_ID) {
             $question_group->add_question($QST_ID);
             if (isset($this->_req_data['question_orders'][$QST_ID])) {
                 $question_group->update_question_order($QST_ID, $this->_req_data['question_orders'][$QST_ID]);
             }
         }
     }
     if ($success !== FALSE) {
         $msg = $new_question_group ? sprintf(__('The %s has been created', 'event_espresso'), $this->_question_group_model->item_name()) : sprintf(__('The %s has been updated', 'event_espresso'), $this->_question_group_model->item_name());
         EE_Error::add_success($msg);
     }
     $this->_redirect_after_action(FALSE, '', '', array('action' => 'edit_question_group', 'QSG_ID' => $QSG_ID), TRUE);
 }
 /**
  * @param $questionObjectOrID
  * @param $qst_order
  * @return int
  */
 public function update_question_order($questionObjectOrID, $qst_order)
 {
     $qst_ID = $questionObjectOrID instanceof EE_Question ? $questionObjectOrID->ID() : (int) $questionObjectOrID;
     return EEM_Question_Group_Question::instance()->update(array('QGQ_order' => $qst_order), array(array('QST_ID' => $qst_ID, 'QSG_ID' => $this->ID())));
 }