Esempio n. 1
0
 /**
  * import
  *
  * @param int $courseId The id the course to import users into
  *
  * @return void
  */
 public function import($courseId = null)
 {
     if (!is_null($courseId)) {
         $course = $this->Course->getAccessibleCourseById($courseId, User::get('id'), User::getCourseFilterPermission());
         if (empty($course)) {
             $this->Session->setFlash(__('Error: That course does not exist.', true));
             $this->redirect('/courses');
         }
         $this->breadcrumb->push(array('course' => $course['Course']));
     }
     $courses = $this->Course->getAccessibleCourses(User::get('id'), User::getCourseFilterPermission(), 'list');
     $this->set('courses', $courses);
     // make sure we know the course we're importing users into
     if ($courseId == null && !empty($this->data)) {
         $courseId = $this->data['Course']['Course'];
     }
     $this->set('courseId', $courseId);
     $this->set('breadcrumb', $this->breadcrumb->push(__('Import Students From Text (.txt) or CSV File (.csv)', true)));
     if (!empty($this->data)) {
         // check that file upload worked
         if ($this->FileUpload->success) {
             $uploadFile = $this->FileUpload->uploadDir . DS . $this->FileUpload->finalFile;
         } else {
             $this->Session->setFlash($this->FileUpload->showErrors());
             return;
         }
         $data = Toolkit::parseCSV($uploadFile);
         $usernames = array();
         // generation password for users who weren't given one
         foreach ($data as &$user) {
             if (empty($user[User::IMPORT_PASSWORD])) {
                 $user[User::GENERATED_PASSWORD] = $this->PasswordGenerator->generate();
             } else {
                 $user[User::GENERATED_PASSWORD] = '';
             }
             $usernames[] = $user[User::IMPORT_USERNAME];
         }
         if ($this->data['User']['update_class']) {
             $this->User->removeOldStudents($usernames, $courseId);
         }
         // add the users to the database
         $result = $this->User->addUserByArray($data, true);
         if (!$result) {
             $this->Session->setFlash("Error: Unable to import users.");
             return;
         }
         $insertedIds = array();
         foreach ($this->User->insertedIds as $new) {
             $insertedIds[] = $new;
         }
         foreach ($result['updated_students'] as $old) {
             $insertedIds[] = $old['User']['id'];
         }
         // enrol the users in the selectec course
         $this->Course->enrolStudents($insertedIds, $this->data['Course']['Course']);
         $this->FileUpload->removeFile($uploadFile);
         $this->set('data', $result);
         $this->render('userSummary');
     }
 }
Esempio n. 2
0
 /**
  * import
  *
  * @access public
  * @return void
  */
 function import()
 {
     if (!empty($this->data)) {
         // check that file upload worked
         if ($this->FileUpload->success) {
             $uploadFile = $this->FileUpload->uploadDir . DS . $this->FileUpload->finalFile;
         } else {
             $this->Session->setFlash($this->FileUpload->showErrors());
             return;
         }
         $students = Toolkit::parseCSV($uploadFile);
         $identifiers = Set::extract('/' . Course::IDENTIFIER, $students);
         $data = $this->data['Course'];
         $move = $data['action'];
         $field = $data['identifiers'];
         $fieldText = $field == 'student_no' ? __('student number', true) : __('username', true);
         // create a copy of the source survey else grab destSurveys id
         if (isset($data['sourceSurveys'])) {
             if ($data['surveyChoices']) {
                 $event = $this->Event->findById($data['sourceSurveys']);
                 $event['Event']['id'] = null;
                 $event['Event']['course_id'] = $data['destCourses'];
                 if (!$this->Event->save($event['Event'])) {
                     $this->Session->setFlash(__('Error: Event was unable to be created.', true));
                     $this->redirect('import');
                     return;
                 }
                 $destEventId = $this->Event->getLastInsertID();
             } else {
                 $destEventId = $data['destSurveys'];
             }
         }
         $error = array();
         $success = array();
         $users = array();
         if (!empty($identifiers)) {
             $users = $this->User->find('all', array('conditions' => array('User.' . $field => $identifiers, 'Role.id' => $this->User->USER_TYPE_STUDENT), 'contain' => array('Role')));
         }
         $invalid = array_diff($identifiers, Set::extract('/User/' . $field, $users));
         foreach ($invalid as $inv) {
             $error[$inv] = sprintf(__('No student with %s %s exists.', true), $fieldText, $inv);
         }
         $enrolled = $this->UserEnrol->findAllByCourseId($data['sourceCourses']);
         $enrolled = Set::extract('/UserEnrol/user_id', $enrolled);
         $destEnrolled = $this->UserEnrol->findAllByCourseId($data['destCourses']);
         $destEnrolled = Set::extract('/UserEnrol/user_id', $destEnrolled);
         if ($move) {
             $event = $this->Event->findAllByCourseId($data['sourceCourses']);
             $eventIds = Set::extract('/Event/id', $event);
             $sub = $this->EvaluationSubmission->find('all', array('conditions' => array('grp_event_id !=' => null, 'submitter_id' => $enrolled, 'EvaluationSubmission.event_id' => $eventIds)));
             $submission = Set::combine($sub, '{n}.EvaluationSubmission.id', '{n}.EvaluationSubmission', '{n}.EvaluationSubmission.submitter_id');
         }
         // enrol student
         foreach ($users as $user) {
             $identifier = $user['User'][$field];
             // enrol student to destination course if not already enrolled
             if (!in_array($user['User']['id'], $destEnrolled)) {
                 if (!$this->User->addStudent($user['User']['id'], $data['destCourses'])) {
                     $error[$identifier] = __('The student could not be enrolled to the destination course.', true);
                     continue;
                 }
             }
             // move or copy survey submission
             if (isset($data['sourceSurveys'])) {
                 $sub = $this->EvaluationSubmission->getEvalSubmissionByEventIdSubmitter($data['sourceSurveys'], $user['User']['id']);
                 $destSub = $this->EvaluationSubmission->getEvalSubmissionByEventIdSubmitter($data['destSurveys'], $user['User']['id']);
                 if ($sub && empty($destSub)) {
                     $inputs = $this->SurveyInput->getByEventIdUserId($data['sourceSurveys'], $user['User']['id']);
                     // if choose to copy set id to null
                     if (!$move) {
                         $sub['EvaluationSubmission']['id'] = null;
                     }
                     $sub['EvaluationSubmission']['event_id'] = $destEventId;
                     $sInputs = array();
                     foreach ($inputs as $input) {
                         $tmp = $input['SurveyInput'];
                         if (!$move) {
                             $tmp['id'] = null;
                         }
                         $tmp['event_id'] = $destEventId;
                         $sInputs[] = $tmp;
                     }
                     if (!($this->EvaluationSubmission->save($sub) && $this->SurveyInput->saveAll($sInputs))) {
                         $error[$identifier] = __("The student's survey submission could not be transferred, however they are enrolled in the destination course.", true);
                         continue;
                     }
                 }
             }
             if (!isset($error[$identifier])) {
                 $success[$identifier] = __('Success.', true);
             }
             if ($move && in_array($user['User']['id'], $enrolled)) {
                 if (!$this->User->removeStudent($user['User']['id'], $data['sourceCourses'])) {
                     $success[$identifier] .= __(' However they were unsuccessfully unenrolled from the source course.', true);
                 }
             } else {
                 if (!in_array($user['User']['id'], $enrolled)) {
                     $success[$identifier] .= sprintf(__(' However no student with %s %s was enrolled in the source course.', true), $fieldText, $identifier);
                 }
             }
             if ($move && isset($submission[$user['User']['id']])) {
                 $success[$identifier] .= "\n" . __("The student has already submitted a peer evaluation in the source course.", true);
             }
             if (isset($data['sourceSurveys']) && !empty($destSub) && $sub) {
                 $success[$identifier] .= "\n" . __("The student has already submitted to the\n                        destination survey, therefore the survey submission from the source survey was not transferred.", true);
             }
         }
         $this->set('errors', $error);
         $this->set('success', $success);
         $this->set('courseId', $data['destCourses']);
         $this->set('identifier', ucwords($fieldText));
         $this->FileUpload->removeFile($uploadFile);
         $this->render('import_summary');
     }
     $destCourses = $this->Course->getAccessibleCourses(User::get('id'), User::getCourseFilterPermission(), 'list');
     //$sourceEvents = $this->Event->getActiveSurveyEvents(array_keys($destCourses));
     //$courseIds = array_unique(Set::extract('/Event/course_id', $sourceEvents));
     $courseIds = array_keys($destCourses);
     $sourceCourses = $this->Course->getCourseList($courseIds);
     asort($sourceCourses);
     $this->set('sourceCourses', $sourceCourses);
     $this->set('sourceSurveys', array());
     $this->set('destCourses', $destCourses);
     $this->set('destSurveys', array());
 }
Esempio n. 3
0
 /**
  * import
  *
  * @param int $courseId
  *
  * @access public
  * @return void
  */
 function import($courseId = null)
 {
     if (empty($courseId) || !($course = $this->Course->getAccessibleCourseById($courseId, User::get('id'), User::getCourseFilterPermission()))) {
         $this->Session->setFlash(__('Error: Course does not exist or you do not have permission to view this course.', true));
         $this->redirect('/courses');
         return;
     }
     $this->breadcrumb->push(array('course' => $course['Course']));
     // Just render :-)
     if (!empty($this->params['form'])) {
         $courseId = $this->params['data']['Group']['Course'];
         $this->params['data']['Group']['course_id'] = $courseId;
         $filename = $this->params['form']['file']['name'];
         $update = $this->params['data']['Group']['update_groups'] ? true : false;
         $identifier = $this->params['data']['Group']['identifiers'];
         //check that a file is attached
         if (trim($filename) == "") {
             $this->Session->setFlash(__('Please select a file to upload.', true));
             $this->redirect('import/' . $courseId);
             return;
         }
         if ($this->FileUpload->success) {
             $uploadFile = $this->FileUpload->uploadDir . DS . $this->FileUpload->finalFile;
             // Get file into an array.
             $lines = Toolkit::parseCSV($uploadFile);
             //Mass create groups
             $this->_addGroupByImport($lines, $courseId, $update, $identifier);
             // Delete the uploaded file
             $this->FileUpload->removeFile($uploadFile);
         } else {
             $this->Session->setFlash($this->FileUpload->showErrors());
             return;
         }
     }
     $coursesList = $this->Course->getAccessibleCourses(User::get('id'), User::getCourseFilterPermission(), 'list');
     $this->set('breadcrumb', $this->breadcrumb->push(__('Import Groups From Text (.txt) or CSV File (.csv)', true)));
     $this->set("courses", $coursesList);
     $this->set("courseId", $courseId);
 }