Changeset 5caa3d3 in iPeer


Ignore:
Timestamp:
2012-12-08T13:59:26-08:00 (6 years ago)
Author:
Compass <pan.luo@…>
Branches:
3.1.x, dev, hotfix, master, pagodabox, ticket463
Children:
9db0c9c
Parents:
92dd1db
Message:

Refactored Course→find into getAccessibleCourses in course model
Refactored Course delete action

Location:
app
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • app/controllers/courses_controller.php

    r92dd1db r5caa3d3  
    309309        if ($this->Course->delete($id)) {
    310310            //Delete all corresponding data start here
    311             $course = $this->Course->findById($id);
    312 
    313311            //Instructors: Instructor record will remain in database, but the join table records will be deleted
    314             $instructors = $course['UserCourse'];
    315             if (!empty($instructors)) {
    316                 foreach ($instructors as $index -> $value) {
    317                     $this->UserCourse-del($value['id']);
    318                 }
    319             }
    320 
    321             //Students: Students who enrolled in other courses will not be deleted;
    322             //          Else, Student records will be deleted
    323             $students = $course['UserEnrol'];
    324             if (!empty($students)) {
    325                 foreach ($students as $index -> $value) {
    326                     $this->UserCourse-del($value['id']);
    327 
    328                     //Check whether there is other enrolled courses existed
    329                     $otherCourse = $this->UserCourse->getById($value['user_id']);
    330                     if (empty($otherCourse)) {
    331                         $this->User->del($value['user_id']);
    332                     }
    333                 }
    334             }
    335 
     312            $this->Course->UserCourse->deleteAll(array('UserCourse.course_id' => $id));
     313
     314            // same for students
     315            $this->Course->UserEnrol->deleteAll(array('UserEnrol.course_id' => $id));
    336316            //Events: TODO
    337             $events = $course['Event'];
    338             if (!empty($events)) {
    339 
    340             }
    341 
    342317            $this->Session->setFlash(__('The course was deleted successfully.', true), 'good');
    343318        } else {
  • app/controllers/departments_controller.php

    r92dd1db r5caa3d3  
    77
    88    public $name = 'Departments';
    9     public $uses = array('Department', 'Faculty', 'CourseDepartment', 
     9    public $uses = array('Department', 'Faculty', 'CourseDepartment',
    1010        'UserFaculty', 'Course');
    1111
     
    3232            $ret = $this->Department->getByUserFaculties($uf);
    3333        }
    34        
     34
    3535        if (empty($ret)) {
    3636            $this->Session->setFlash(__('You do not have access to any faculties', true));
    3737            $this->redirect('/pages/admin');
    3838        }
    39        
     39
    4040        $departments = array();
    4141        foreach ($ret as $department) {
    42             $tmp = array(); 
     42            $tmp = array();
    4343            $tmp['id'] = $department['Department']['id'];
    4444            $tmp['Name'] = $department['Department']['name'];
     
    5454     * @param mixed $id - The department id to be viewed
    5555     */
    56     public function view($id = null) {
     56    public function view($id) {
    5757        $this->set('title_for_layout', 'View Department');
    5858        if (!$id) {
     
    6464        $this->set('faculty', $ret['Faculty']['name']);
    6565
    66         $ret = $this->CourseDepartment->findAllByDepartmentId($id);
    67         $courses = array();
    68         foreach ($ret as $val) {
    69             $course = $this->Course->findById(
    70                 $val['CourseDepartment']['course_id']);
    71             $tmp = array();
    72             $tmp['id'] = $course['Course']['id'];
    73             $tmp['Course'] = $course['Course']['course'];
    74             $tmp['Title'] = $course['Course']['title'];
    75             $tmp['Students'] = $course['Course']['student_count'];
    76             $courses[] = $tmp;
    77         }
     66        $courses = $this->Course->getAllAccessibleCourses(User::get('id'), User::getCourseFilterPermission(), 'all', array('conditions' => array('Department.id' => $id), 'contain' => 'Department'));
    7867        $this->set('courses', $courses);
    7968    }
     
    158147            $ret = $this->Department->getByUserFaculties($uf);
    159148        }
    160        
     149
    161150        foreach ($ret as $faculty) {
    162151            $faculties[$faculty['Faculty']['id']] = $faculty['Faculty']['name'];
  • app/controllers/events_controller.php

    r92dd1db r5caa3d3  
    464464        );
    465465
    466         $course = $this->Course->find('first', array('conditions' => array('Course.id' => $courseId)));
    467 
    468466        $this->set('event_id', $id);
    469467        $this->set('data', $data);
  • app/controllers/home_controller.php

    r92dd1db r5caa3d3  
    4343        if (User::hasPermission('functions/coursemanager')) {
    4444            //General Home Rendering for others
    45             $course_list = $this->Course->getAccessibleCourses(User::get('id'), User::getCourseFilterPermission(), 'all', array('contain' => array('Event', 'Instructor')));
     45            $course_list = $this->Course->getAllAccessibleCourses(User::get('id'), User::getCourseFilterPermission(), 'all', array('contain' => array('Event', 'Instructor')));
    4646            $this->set('course_list', $this->_formatCourseList($course_list));
    4747        } else {
  • app/controllers/users_controller.php

    r92dd1db r5caa3d3  
    395395     * @return void
    396396     * */
    397     private function _initFormEnv() {
     397    private function _initFormEnv()
     398    {
    398399        $user = $this->User->find(
    399400            'first',
     
    401402        );
    402403        // get the courses that this user is allowed access to
    403         $coursesOptions = array();
    404         if (User::hasPermission('functions/user/superadmin')) {
     404        $coursesOptions = $this->Course->getAccessibleCourses(User::get('id'), User::getCourseFilterPermission(), 'list');
     405        /*if (User::hasPermission('functions/user/superadmin')) {
    405406            // superadmins should have access to all courses regardless
    406407            $coursesOptions = $this->Course->find('list');
     
    427428                $coursesOptions[$course['id']] = $course['full_name'];
    428429            }
    429         }
     430        }*/
    430431        $this->set('coursesOptions', $coursesOptions);
    431432
     
    911912    {
    912913        if (!is_null($courseId)) {
    913             $course = $this->Course->find('first', array('conditions' => array('id' => $courseId), 'recursive' => 0));
     914            $course = $this->Course->getAccessibleCourseById($courseId, User::get('id'), User::getCourseFilterPermission());
    914915            if (empty($course)) {
    915916                $this->Session->setFlash(__('Error: That course does not exist.', true));
  • app/models/course.php

    r92dd1db r5caa3d3  
    479479    }
    480480
    481 
    482     /**
    483      * getAccessibleCourses get all course that the user has access to
     481    /**
     482     * getCoursesByUserIdFilterPermission
    484483     *
    485484     * @param int    $userId     user id
     
    488487     * @param array  $options    find options
    489488     *
    490      * @access public
    491      * @return void
    492      */
    493     function getAccessibleCourses($userId, $permission, $type = 'all', $options = array())
     489     * @access protected
     490     * @return void
     491     */
     492    protected function getCoursesByUserIdFilterPermission($userId, $permission, $type = 'all', $options = array())
    494493    {
    495494        switch($permission) {
     
    520519
    521520    /**
     521     * getAccessibleCourses get all active course that the user has access to
     522     *
     523     * @param int    $userId     user id
     524     * @param mixed  $permission filter permission
     525     * @param string $type       find type
     526     * @param array  $options    find options
     527     *
     528     * @access public
     529     * @return void
     530     */
     531    function getAccessibleCourses($userId, $permission, $type = 'all', $options = array())
     532    {
     533        $default = array('conditions' => array('record_status' => 'A'));
     534        $options = array_merge($default, $options);
     535        return $this->getCoursesByUserIdFilterPermission($userId, $permission, $type, $options);
     536    }
     537
     538    /**
     539     * getAllAccessibleCourses get all course that the user has access to,
     540     * including inactive ones
     541     *
     542     * @param int    $userId     user id
     543     * @param mixed  $permission filter permission
     544     * @param string $type       find type
     545     * @param array  $options    find options
     546     *
     547     * @access public
     548     * @return void
     549     */
     550    function getAllAccessibleCourses($userId, $permission, $type = 'all', $options = array())
     551    {
     552        return $this->getCoursesByUserIdFilterPermission($userId, $permission, $type, $options);
     553    }
     554
     555    /**
    522556     * getAccessibleCourseById get one course by course id, if user do not have
    523557     * access to the course, return false.
  • app/tests/cases/controllers/courses_controller.test.php

    r92dd1db r5caa3d3  
    560560        $found = $courseModel->find('first', array( 'conditions' => array('id' => 1)));
    561561        $this->assertFalse($found);
     562        $instructors = $courseModel->UserCourse->find('all', array('conditions' => array('course_id' => 1)));
     563        $this->assertFalse($instructors);
     564        $students = $courseModel->UserEnrol->find('all', array('conditions' => array('course_id' => 1)));
     565        $this->assertFalse($students);
    562566
    563567        $message = $this->controller->Session->read('Message.flash');
  • app/views/departments/view.ctp

    r92dd1db r5caa3d3  
    1010        <tr>
    1111<?php
     12$columns = array('id', 'course', 'title', 'student_count', 'record_status');
    1213if (!empty($courses)) {
    13     $sample = $courses[0];
    14     foreach ($sample as $key => $val) {
    15         echo "<th>$key</th>";
     14    foreach ($columns as $val) {
     15        if ($val == 'email' && !User::hasPermission('functions/viewemailaddresses')) {
     16            continue;
     17        }
     18        echo "<th>".Inflector::humanize($val)."</th>";
    1619    }
    1720}
     
    2023    </thead>
    2124    <tbody>
    22 
    2325<?php
    2426foreach ($courses as $course) {
    25     $id = $course['id'];
    2627    echo "<tr>";
    27     foreach ($course as $val) {
    28         echo "<td>";
    29         echo $this->Html->link(
    30             $val,
    31             array('action' => 'view', 'controller' => 'courses', $id)
    32         );
    33         echo "</td>";
     28    foreach ($columns as $val) {
     29        if ($val == 'email' && !User::hasPermission('functions/viewemailaddresses')) {
     30            continue;
     31        }
     32        echo "<td>".$this->Html->link($course['Course'][$val], '/courses/view'.$course['Course']['id'])."</td>";
    3433    }
    3534    echo "</tr>";
    3635}
    3736?>
    38 
    3937    </tbody>
    4038</table>
     
    4644        "sPaginationType" : "full_numbers",
    4745        "aoColumnDefs": [{
    48             "bSearchable": false, 
    49             "bVisible": false, 
    50             "bSortable":false, 
    51             "aTargets": [ 0 ] 
     46            "bSearchable": false,
     47            "bVisible": false,
     48            "bSortable":false,
     49            "aTargets": [ 0 ]
    5250        }],
    5351        "aaSorting" : [[1, 'asc']]
Note: See TracChangeset for help on using the changeset viewer.