Changeset 1df4091 in iPeer


Ignore:
Timestamp:
2012-08-28T17:48:18-07:00 (5 years ago)
Author:
John Hsu <john.hsu@…>
Branches:
3.1.x, dev, hotfix, master, pagodabox, ticket463
Children:
f747120
Parents:
e72240f
git-author:
John Hsu <john.hsu@…> (08/28/2012 05:23:47 PM)
git-committer:
John Hsu <john.hsu@…> (08/28/2012 05:48:18 PM)
Message:

User import refactor, fix password display.

Change the form to take advantage of more CakePHP automagic stuff, also
no more tables for layout. Removed the two separate instruction
elements, as they were pretty much identical. Since the instructions
weren't being reused elsewhere, they shouldn't be elements anyway.

Fix password not displaying. It used to just display the password hash
instead of the actual generated or imported password. Only new users
will have their password displayed. Exisiting users cannot have their
password changed through import.

Files:
2 deleted
9 edited

Legend:

Unmodified
Added
Removed
  • app/controllers/users_controller.php

    re72240f r1df4091  
    10661066     * import
    10671067     *
    1068      * @access public
    1069      * @return void
    1070      */
    1071     function import()
    1072     {
    1073         $this->set('title_for_layout', __('Import Students From Text (.txt) or CSV File (.csv)', true));
    1074         if (isset($this->params['form']['course_id'])) {
    1075             $this->Session->write('ipeerSession.courseId', $this->params['form']['course_id']);
    1076         }
     1068     * @param int $courseId The id the course to import users into
     1069     *
     1070     * @return void
     1071     */
     1072    public function import($courseId = null)
     1073    {
     1074        $this->set('title_for_layout',
     1075            __('Import Students From Text (.txt) or CSV File (.csv)', true));
    10771076       
    1078         // instructors
    1079         if (!User::hasPermission('controllers/departments')) {
     1077        // users can only import to courses they have access to
     1078        if (User::hasPermission('functions/superadmin')) {
     1079            // superadmins have access to everything
     1080            $courseList = $this->Course->find('list');
     1081        }
     1082        else if (User::hasPermission('functions/user/admin')) {
     1083            // admins have access to their departments
     1084            $courseList = User::getMyDepartmentsCourseList('list');
     1085        }
     1086        else if (!User::hasPermission('functions/user/instructor')) {
     1087            // instructors have access to courses they're teaching
    10801088            $courseList = User::getMyCourseList();
    1081         // super admins
    1082         } else if (User::hasPermission('functions/superadmin')) {
    1083             $courseList = $this->Course->find('list');
    1084         // admins
    1085         } else {
    1086             $courseList = User::getMyDepartmentsCourseList('list');
    1087         }
    1088 
    1089         $this->set('courseList', $courseList);
    1090         $this->set('courseParams', array('courseList' => $courseList, 'id_prefix' => 'import'));
    1091         $this->set('user_type', 'S');
    1092     }
    1093 
    1094 
    1095     /**
    1096      * importPreview
    1097      * TODO: add import preview
    1098      *
    1099      *
    1100      * @access public
    1101      * @return void
    1102      */
    1103     function importPreview()
    1104     {
    1105     }
    1106 
    1107 
    1108     /**
    1109      * importFile
    1110      *
    1111      * @access public
    1112      * @return void
    1113      */
    1114     function importFile()
    1115     {
    1116         $this->autoRender = false;
     1089        }
     1090        else {
     1091            // no permission to import user
     1092            $this->Session->setFlash("Access denied to import user.");
     1093            $this->redirect('/home');
     1094        }
     1095        $this->set('courses', $courseList);
     1096
     1097        // make sure we know the course we're importing users into
     1098        if ($courseId == null && !empty($this->data)) {
     1099            $courseId = $this->data['Course']['Course'];
     1100        }
     1101        $this->set('courseId', $courseId);
    11171102
    11181103        if (!empty($this->data)) {
     1104            // check that file upload worked
    11191105            if ($this->FileUpload->success) {
    11201106                $uploadFile = $this->FileUpload->uploadDir.DS.$this->FileUpload->finalFile;
    11211107            } else {
    11221108                $this->Session->setFlash($this->FileUpload->showErrors());
    1123                 $this->redirect('import');
    1124             }
    1125         }
    1126 
    1127         $data = Toolkit::parseCSV($uploadFile);
    1128         foreach ($data as &$user) {
    1129             if (empty($user[User::IMPORT_PASSWORD])) {
    1130                 $user[User::GENERATED_PASSWORD] = $this->PasswordGenerator->generate();
    1131             } else {
    1132                 $user[User::GENERATED_PASSWORD] = '';
    1133             }
    1134         }
    1135 
    1136         $result = $this->User->addUserByArray($data, true);
    1137 
    1138         if (!$result) {
    1139             $this->Session->setFlash($this->User->showErrors());
    1140             $this->redirect('import');
    1141         }
    1142        
    1143         $insertedIds = array();
    1144         foreach ($this->User->insertedIds as $new) {
    1145             $insertedIds[] = $new;
    1146         }
    1147         foreach ($result['updated_students'] as $old) {
    1148             $insertedIds[] = $old['User']['id'];
    1149         }
    1150        
    1151         $this->Course->enrolStudents($insertedIds, $this->data['User']['course_id']);
    1152 
    1153         $this->FileUpload->removeFile($uploadFile);
    1154 
    1155         $this->set('data', $result);
    1156 
    1157         $this->render('userSummary');
    1158     }
    1159 
     1109                return;
     1110            }
     1111
     1112            $data = Toolkit::parseCSV($uploadFile);
     1113            // generation password for users who weren't given one
     1114            foreach ($data as &$user) {
     1115                if (empty($user[User::IMPORT_PASSWORD])) {
     1116                    $user[User::GENERATED_PASSWORD] = $this->PasswordGenerator->generate();
     1117                } else {
     1118                    $user[User::GENERATED_PASSWORD] = '';
     1119                }
     1120            }
     1121
     1122            // add the users to the database
     1123            $result = $this->User->addUserByArray($data, true);
     1124
     1125            if (!$result) {
     1126                $this->Session->setFlash("Unable to import users. Do they already exist?");
     1127                return;
     1128            }
     1129           
     1130            $insertedIds = array();
     1131            foreach ($this->User->insertedIds as $new) {
     1132                $insertedIds[] = $new;
     1133            }
     1134            foreach ($result['updated_students'] as $old) {
     1135                $insertedIds[] = $old['User']['id'];
     1136            }
     1137           
     1138            // enrol the users in the selectec course
     1139            $this->Course->enrolStudents($insertedIds,
     1140                $this->data['Course']['Course']);
     1141            $this->FileUpload->removeFile($uploadFile);
     1142            $this->set('data', $result);
     1143            $this->render('userSummary');
     1144        }
     1145    }
    11601146
    11611147    /**
  • app/plugins/file_upload/controllers/components/file_upload.php

    re72240f r1df4091  
    185185    }
    186186    // end
     187    //
    187188   
    188189    if($this->uploadDetected){
     
    272273    if(count($this->uploadedFiles) && empty($this->currentFile)){
    273274      $this->_error('FileUpload: You\'re using a deprecated standard of uploading files manually.  Don\'t call processFile() directly. Instead, call processAllFiles().');
    274       $this->setCurrentFile($this->uploadedFiles[0]);
     275      return;
    275276    }
    276277   
     
    345346  function processAllFiles(){
    346347    foreach($this->uploadedFiles as $file){
    347       $this->_setCurrentFile($file[$this->options['fileVar']]);
     348      $this->currentFile = $file[$this->options['fileVar']];
    348349      $this->Uploader->file = $this->options['fileModel'] ? $file[$this->options['fileVar']] : $file;
    349350      $this->processFile();
    350     }
    351   }
    352  
    353   /**
    354     * Set's the current file to process.
    355     *
    356     * @access private
    357     * @param associative array of file
    358     * @return void
    359     */
    360   function _setCurrentFile($file){
    361     if($this->options['fileModel']){
    362       $this->currentFile = $file[$this->options['fileVar']];
    363     }
    364     else {
    365       $this->currentFile = $file;
    366351    }
    367352  }
  • app/views/elements/courses/submenu.ctp

    re72240f r1df4091  
    1616      $items,
    1717      array('name' => 'Add Student', 'link' => "/users/add/$course_id"),
    18       array('name' => 'Import Students', 'link' => "/users/import"),
     18      array('name' => 'Import Students', 'link' => "/users/import/$course_id"),
    1919      array('name' => 'List Students',
    2020        'link' => "/users/goToClassList/$course_id"),
  • app/views/elements/users/user_summary_list.ctp

    re72240f r1df4091  
    1 <?php $showPasswords = isset($showPasswords) ? $showPasswords : true; ?>
    2 <?php $columns = isset($columns) ? $columns : array('username' => __('Username', true),
    3                                                     'first_name' => __('First Name', true),
    4                                                     'last_name' => __('Last Name',true),
    5                                                     'email' => __('E-mail', true),
    6                                                     'password' => __('Password', true),
    7                                                    );
     1<?php
     2$columns = isset($columns) ?
     3    $columns :
     4    array(
     5        'username' => __('Username', true),
     6        'first_name' => __('First Name', true),
     7        'last_name' => __('Last Name',true),
     8        'email' => __('E-mail', true),
     9        'password' => __('Password', true),
     10    );
    811// Summary for single record creation -> convert to multiple user entry
    912if (isset($data['id'])) {
     
    1518
    1619<table class="user-list-table">
    17   <tr>
    18     <?php foreach($columns as $k => $v): ?>
    19       <th><?php echo $v?></th>
    20                 <?php endforeach;?>
    21   </tr>
    22                 <!-- Summary for import record creation -->
    23                 <?php foreach($data as $user): ?>
    24             <?php
    25               if($showPasswords) {
    26                 if(isset($user['User']['tmp_password']))
    27                   $password = $user['User']['tmp_password'];
    28                 else if(isset($user['User']['password']))
    29                   $password = $user['User']['password'];
    30                 else
    31                   $password = '';
    32               } else {
    33                 $password = '******';
    34               }
    35             ?>
    36             <tr>
    37               <?php foreach($columns as $k => $v): ?>
    38                 <td><?php echo $user['User'][$k]?></td>
    39               <?php endforeach; ?>
    40             </tr>
    41      <?php endforeach;?>
     20    <tr>
     21    <?php
     22    foreach($columns as $k => $v) {
     23        echo "<th>$v</th>";
     24    }
     25    ?>
     26    </tr>
     27    <!-- Summary for import record creation -->
     28    <?php
     29    foreach($data as $user) {
     30        if(isset($showPasswords)) {
     31            if(!empty($user['User']['tmp_password']))
     32                $password = $user['User']['tmp_password'];
     33            else if(!empty($user['User']['import_password']))
     34                $password = $user['User']['import_password'];
     35            else
     36                $password = '';
     37        } else {
     38            $password = '******';
     39        }
     40        $user['User']['password'] = $password;
     41        echo "<tr>";
     42        foreach($columns as $k => $v) {
     43            echo "<td>".$user['User'][$k]."</td>";
     44        }
     45        echo "</tr>";
     46    }
     47    ?>
    4248</table>
  • app/views/users/import.ctp

    re72240f r1df4091  
    1 <div class="content-container" align=center>
    2     <table width="95%"  border="0" cellspacing="2" cellpadding="8">
    3         <tr class="tableheader">
    4             <td><?php __('Instructions')?></td>
    5         </tr>
    6         <tr class="tablecell2">
    7             <td><?php echo $this->element('users/user_import_info');?></td>
    8         </tr>
    9         <tr class="tableheader">
    10             <td><?php __('Import')?></td>
    11         </tr>
    12         <tr class="tablecell2">
    13             <td>
    14             <?php echo $this->Form->create(null, array('action' => 'importFile',
    15                                                        'type' => 'file',
    16                                                        'onSubmit' => 'return import_validate();',
    17                                                        'id' => 'importfrm',
    18                                                        'name' => 'importfrm'));?>
     1<div id="UserImport">
     2<h2><?php __('Instructions')?></h2>
     3<ul>
     4    <li><?php __('All fields mandatory, except email and password.')?></li>
     5    <li><?php __('If email column is missing, students can update their profiles by clicking on their name (near the Logout link).')?></li>
     6    <li><?php __('If password column is missing, system will generate random password for each student.')?></li>
     7    <li><?php __('If the user already exists, their password will NOT be changed.')?></li>
     8    <li><?php __('If an external authentication module is enabled (e.g. CWL or Shibboleth), password column can be ignored. Students will use external authentication module to login.')?></li>
     9    <li><?php __('Please make sure to remove header from the CSV file.')?></li>
     10</ul>
    1911
    20             <input type="hidden" name="required" value="course_id"/>
    21             <?php echo $this->Form->hidden('User/role'); ?>
    22             <ol>
    23                 <li><h3><?php __('Please select a TXT or CSV file to import:')?></h3><?php echo $this->Form->file('file', array('name' => 'file', 'class' => 'required')); ?></li>
    24                 <li><h3><?php __('Select the course to import into:')?></h3><?php echo $this->element('courses/course_selection_box', $courseParams); ?></li>
    25                 <li><h3><?php __('Click the button below to import the students:')?></h3><?php echo $this->Form->submit(__('Import', true))?></li>
    26             </ol>
    27             <?php echo $this->Form->end();?>
    28             </td>
    29         </tr>
    30     </table>
     12<h3><?php __('Formatting:')?></h3>
     13<pre style='background-color: white; border:1px solid black; padding:5px; margin:5px'>
     14<?php __('Username,First Name,Last Name,Student#,<i>Email(optional),Password(optional)</i>')?>
     15</pre>
     16
     17<h3><?php __('Examples:')?></h3>
     18<pre style='background-color: white; border:1px solid black; padding:5px; margin:5px'>
     19user1,first1,last1,student1,1@example.com,password1
     20user2,first2,last2,student2,2@example.com,password2
     21user3,first3,last3,student3,3@example.com,password3
     22user4,first4,last4,student4,4@example.com,password4
     23</pre>
     24
     25<h2><?php __('Import')?></h2>
     26
     27<?php
     28echo $this->Form->create(null, array('type' => 'file'));
     29echo $this->Form->input('file', array('type' => 'file', 'name' => 'file'));
     30echo $this->Form->input('Course',
     31    array('multiple' => false, 'default' => $courseId));
     32echo $this->Form->submit(__('Import', true));
     33echo $this->Form->end();
     34?>
     35
    3136</div>
  • app/views/users/user_summary.ctp

    re72240f r1df4091  
    1010  <?php if (isset($data['created_students'])) : ?>
    1111    <div class="list-title"><?php echo $msg?></div>
    12     <?php echo $this->element('users/user_summary_list', array('data'=>$data['created_students']));?>
     12    <?php echo $this->element('users/user_summary_list', array('data'=>$data['created_students'], 'showPasswords' => true));?>
    1313  <?php endif; ?>
    1414
     
    1717  <?php if (isset($data['updated_students'])) : ?>
    1818    <div class="list-title"><?php echo $msg?></div>
    19     <?php echo $this->element('users/user_summary_list', array('data'=>$data['updated_students'], 'showPasswords' => false));?>
     19    <?php echo $this->element('users/user_summary_list', array('data'=>$data['updated_students']));?>
    2020  <?php endif; ?>
    2121
    2222  <div class="button-row">
    23     &nbsp;<?php echo $html->link(__('Back to User Add', true), '/users/add/'); ?> &nbsp;|&nbsp;
    24     <?php echo $html->link(__('Back to Student Import', true), '/users/import/'); ?> &nbsp;|&nbsp;
    25     <?php echo $html->link(__('Back to User Listing', true), '/users/index/'); ?>
    26     <?php echo (!empty($course_id)) ? '&nbsp;|&nbsp;' . $html->link(__('Back to Course Home', true), '/courses/home/'.$course_id) : '';?>
     23<?php
     24echo $html->link(__('Back to User Add', true), '/users/add/');
     25echo " | ";
     26echo $html->link(__('Back to Student Import', true), "/users/import/$courseId");
     27echo " | ";
     28echo $html->link(__('Back to User Listing', true), '/users/index/');
     29echo " | ";
     30echo $html->link(__('Back to Course', true), "/courses/home/$courseId");
     31?>
    2732  </div>
    2833
  • app/webroot/css/ipeer.css

    re72240f r1df4091  
    9999}
    100100
    101 .text label, .password label, .textarea label, .select label, .datetime label, .date label, .radio legend, .penaltyLabel
     101.text label, .password label, .textarea label, .select label, .datetime label, .date label, .radio legend, .penaltyLabel, .file label
    102102{
    103103    float: left;
     
    110110
    111111.text input, .password input, .textarea textarea, .select select,
    112 .datetime select, .date select, .penaltyInput input
     112.datetime select, .date select, .penaltyInput input, .file input
    113113{   
    114114    margin: 0.35em 0.35em;
     
    718718.userViewTable {
    719719    width: 100%;
     720}
     721
     722/* users/import */
     723
     724#UserImport ul li {
     725    list-style-type: circle;
     726    list-style-position: inside;
     727    margin: 0.5em;
     728}
     729
     730#UserImport .file input {
     731    width: 25em;
    720732}
    721733
  • app/webroot/js/ipeer.js

    re72240f r1df4091  
    8484}
    8585
    86 function import_validate() {
    87   if($("import_course_id").value == -1) {
    88     alert("Please select a course to import into!");
    89     return false;
    90   }
    91   return true;
    92 }
  • build/phpcs.xml

    re72240f r1df4091  
    3434    <rule ref="Generic.PHP.LowerCaseConstant"/>
    3535    <rule ref="Squiz.Arrays.ArrayBracketSpacing"/>
    36     <rule ref="Squiz.ControlStructures.ControlSignature"/>
    3736    <rule ref="Squiz.ControlStructures.ForEachLoopDeclaration"/>
    3837    <rule ref="Squiz.ControlStructures.ForLoopDeclaration"/>
Note: See TracChangeset for help on using the changeset viewer.