/**
   * Attempts to sign in or register the account specified by the login form. If there are form
   * errors (invalid IdNo, missing fields, etc.), the errors are presented and no actual login
   * attempt is made.
   */
  public void attemptLogin() {

    // Reset errors.
    mIdNoView.setError(null);
    mCodeView.setError(null);
    mPasswordView.setError(null);

    // Store values at the time of the login attempt.
    mIdNo = mIdNoView.getText().toString();
    mCode = mCodeView.getText().toString();
    mPassword = mPasswordView.getText().toString();

    boolean cancel = false;
    View focusView = null;
    String path = null;

    // Check for a valid IdNo.
    if (TextUtils.isEmpty(mIdNo)) {
      mIdNoView.setError(getString(R.string.error_field_required));
      focusView = mIdNoView;
      cancel = true;
    }

    // check for a non-empty course code
    if (TextUtils.isEmpty(mCode)) {
      mCodeView.setError(getString(R.string.error_field_required));
      focusView = mCodeView;
      cancel = true;
    }

    // Check for a valid password.
    if (TextUtils.isEmpty(mPassword)) {
      mPasswordView.setError(getString(R.string.error_field_required));
      focusView = mPasswordView;
      cancel = true;
    } else if (mPassword.length() < 4) {
      mPasswordView.setError(getString(R.string.error_invalid_password));
      focusView = mPasswordView;
      cancel = true;
    } else {
      // password entered is the password of the compressed test file
      StudentTestDB db = new StudentTestDB(this);
      Cursor testData = db.getTestDataCursor(mCode);

      // TODO If a test is already attempted, prevent it from the solution
      // being resent -- check status column in DB and give a dialog
      // box/set a label along
      // with questions notifying the student
      // TODO Check test day/time before allowing access to a test

      // TODO Set test duration from DB

      fileName = mCode;
      int status;
      if (!testData.moveToFirst()) { // if cursor is null, then that test does
        // not exist in database.
        Toast.makeText(
                getApplicationContext(),
                "Test \"" + mCode + "\" does not exist in the database.",
                Toast.LENGTH_LONG)
            .show();
        cancel = true;

      } else {
        teacherName = testData.getString(2);
        duration = testData.getString(5);
        fileName = testData.getString(6);
        status = testData.getInt(7);
        path = Environment.getExternalStorageDirectory() + "/Exam/";

        // checks whether the file exists, if yes, extracts the contents
        // to
        // a folder else throws incorrect p/w if not
        // found
        File file = new File(path + fileName + ".zip");
        ExtractAllFiles ef = new ExtractAllFiles(path, mPassword, fileName);
        boolean ext = ef.extract();
        if (!ext) {
          mPasswordView.setError(getString(R.string.error_incorrect_password));
          focusView = mPasswordView;
          cancel = true;
        }
      }
    }

    if (cancel) {
      // There was an error; don't attempt login and focus the first
      // form field with an error.
      focusView.requestFocus();
    } else {
      Intent intent = new Intent(LoginActivity.this, Questions.class);
      intent.putExtra("teacher", teacherName);
      intent.putExtra("path", path);
      intent.putExtra("fileName", fileName);
      intent.putExtra("crsCode", mCode);
      intent.putExtra("idNo", mIdNo);
      intent.putExtra("duration", duration);
      startActivity(intent);
      finish();
    }
  }