Changeset 4d5a23c in iPeer


Ignore:
Timestamp:
2013-08-07T16:07:37-07:00 (4 years ago)
Author:
Compass <pan.luo@…>
Branches:
3.1.x, dev, master
Children:
11dca66
Parents:
8cddd1e
git-author:
mwytang <michael.tang@…> (07/12/2013 01:55:39 PM)
git-committer:
Compass <pan.luo@…> (08/07/2013 04:07:37 PM)
Message:

Fixed courses edit

A bug was found when user A edits the user B's profile if User A does
not have access to all of the courses user B is enrolled in. The problem
was user B would be unenrolled from the courses User A does not have
access to.

I have also copied the fix from dev for another problem which is
if all the courses are unselected, the user would not be unenrolled
from any of the courses.

Location:
app
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • app/controllers/users_controller.php

    r8cddd1e r4d5a23c  
    417417            $coursesId[] = $course['id'];
    418418        }
     419        foreach ($user['Tutor'] as $course) {
     420            $coursesId[] = $course['id'];
     421        }
    419422        $this->set('coursesSelected', $coursesId);
    420423    }
     
    610613    public function edit($userId = null, $courseId = null) {
    611614        $this->set('title_for_layout', 'Edit User');
    612 
     615        $enrolCourses = $this->User->getEnrolledCourses($userId);
    613616        $role = $this->User->getRoleName($userId);
    614617
     
    620623        // save the data which involves:
    621624        if ($this->data) {
     625            if (!is_array($this->data['Courses']['id'])) {
     626                $this->data['Courses']['id'] = array();
     627            }
     628
     629            // add list of courses the user is enrolled in but the logged
     630            // in user does not have access to so that the user would not
     631            // be unenrolled from the course when their profile is edited.
     632            $append = $this->_notUnenrolCourses($this->Auth->user('id'), $userId);
     633            $this->data['Courses']['id'] = array_merge($this->data['Courses']['id'], $append);
     634
     635            // unenrol student from course, group, surveygroup
     636            // only students will go in because only they have records in Enrolment
     637            foreach ($enrolCourses as $course) {
     638                if (!in_array($course, $this->data['Courses']['id'])) {
     639                    $this->User->removeStudent($userId, $course);
     640                }
     641            }
     642
    622643            // create the enrolment entry depending on if instructor or student
    623644            // and also convert it into a CakePHP dark magic friendly format
     
    11211142    }
    11221143
     1144    /**
     1145     * helper function for users/edit, to not unenrol the user being edited from
     1146     * courses the logged user don't have access to
     1147     *
     1148     * @param int $editor
     1149     * @param int $userId
     1150     *
     1151     * @return array of courses to not unenrol
     1152     */
     1153    private function _notUnenrolCourses($editor, $userId)
     1154    {
     1155        $editor = $this->User->findById($editor);
     1156        $user = $this->User->findById($userId);
     1157        $editorCourses = array();
     1158        $userCourses = array();
     1159       
     1160        // user's list of courses
     1161        foreach ($user['Course'] as $course) {
     1162            $userCourses[] = $course['id'];
     1163        }
     1164        foreach ($user['Enrolment'] as $course) {
     1165            $userCourses[] = $course['id'];
     1166        }
     1167        foreach ($user['Tutor'] as $course) {
     1168            $userCourses[] = $course['id'];
     1169        }
     1170
     1171        // get editor's list of courses
     1172        $editorCourses = $this->Course->getAccessibleCourses(User::get('id'),
     1173            User::getCourseFilterPermission(), 'list');
     1174       
     1175        return array_diff($userCourses, array_keys($editorCourses));
     1176    }
    11231177}
  • app/models/survey_group_member.php

    r8cddd1e r4d5a23c  
    1212{
    1313    public $name = 'SurveyGroupMember';
     14
     15    public $belongsTo = array(
     16        'User' => array(
     17            'className'    => 'User',
     18            'condition'    => '',
     19            'order'        => '',
     20            'foreignKey'   => 'user_id'
     21        ),
     22        'SurveyGroupSet' => array(
     23            'className'    => 'SurveyGroupSet',
     24            'condition'    => '',
     25            'order'        => '',
     26            'foreignKey'   => 'group_set_id'
     27        ),
     28    );
    1429
    1530    /**
  • app/models/user.php

    r8cddd1e r4d5a23c  
    4545            'className' => 'SurveyInput',
    4646            'dependent' => true,
     47        ),
     48        'SurveyGroupMember' => array(
     49            'className' => 'SurveyGroupMember',
     50            'foreignKey' => 'user_id',
    4751        ),
    4852    );
     
    729733    public function removeStudent($user_id, $course_id)
    730734    {
     735        $this->Event = ClassRegistry::init('Event');
    731736        $id = $this->UserEnrol->field('id',
    732737            array('user_id' => $user_id, 'course_id' => $course_id));
     738        // query all survey events of the course
     739        $surveys = $this->Event->find('list', array(
     740            'conditions' => array(
     741                'course_id' => $course_id,
     742                'event_template_type_id' => 3),
     743            'fields' => array('Event.id')));
     744        /* query any surveyGroupMember records created based on the above
     745        survey events for the user */
     746        $members = $this->SurveyGroupMember->find('all', array(
     747            'conditions' => array(
     748                'SurveyGroupMember.user_id' => $user_id,
     749                'SurveyGroupSet.survey_id' => $surveys)));
     750        // remove the records found
     751        foreach ($members as $member) {
     752            $this->SurveyGroupMember->delete($member['SurveyGroupMember']['id']);
     753        }
     754        $members = $this->Group->find('all', array('conditions' => array('Member.id' => $user_id, 'course_id' => $course_id)));
     755        foreach ($members as $member) {
     756            $this->GroupsMember->delete($member['GroupsMember']['id']);
     757        }
    733758        return $this->UserEnrol->delete($id);
    734759    }
     
    841866        ));
    842867    }
     868   
     869    /**
     870     * Get courses a user is enrolled in
     871     *
     872     * @param mixed $userId user id
     873     *
     874     * @return list of course ids
     875     */
     876    function getEnrolledCourses($userId='')
     877    {
     878        $user = $this->find('first', array(
     879            'conditions' => array('User.id' => $userId),
     880            'contain' => array('Enrolment')
     881        ));
     882        return Set::extract($user, '/Enrolment/id');
     883    }
    843884
    844885    /*********************************
Note: See TracChangeset for help on using the changeset viewer.