/* rescans all the installed courses and reinstalls them, to ensure that
   * the new titles etc are picked up
   */
  protected void upgradeV17() {
    File dir = new File(Storage.getCoursesPath(ctx));
    String[] children = dir.list();
    if (children != null) {
      for (String course : children) {
        publishProgress("checking: " + course);
        String courseXMLPath = "";
        String courseScheduleXMLPath = "";
        String courseTrackerXMLPath = "";
        // check that it's unzipped etc correctly
        try {
          courseXMLPath =
              dir + File.separator + course + File.separator + MobileLearning.COURSE_XML;
          courseScheduleXMLPath =
              dir + File.separator + course + File.separator + MobileLearning.COURSE_SCHEDULE_XML;
          courseTrackerXMLPath =
              dir + File.separator + course + File.separator + MobileLearning.COURSE_TRACKER_XML;
        } catch (ArrayIndexOutOfBoundsException aioobe) {
          FileUtils.cleanUp(dir, Storage.getDownloadPath(ctx) + course);
          break;
        }

        // check a module.xml file exists and is a readable XML file
        CourseXMLReader cxr;
        CourseScheduleXMLReader csxr;
        CourseTrackerXMLReader ctxr;
        try {
          cxr = new CourseXMLReader(courseXMLPath, 0, ctx);
          csxr = new CourseScheduleXMLReader(courseScheduleXMLPath);
          File trackerXML = new File(courseTrackerXMLPath);
          ctxr = new CourseTrackerXMLReader(trackerXML);
        } catch (InvalidXMLException e) {
          e.printStackTrace();
          break;
        }

        Course c = new Course(prefs.getString(PrefsActivity.PREF_STORAGE_LOCATION, ""));
        c.setVersionId(cxr.getVersionId());
        c.setTitles(cxr.getTitles());
        c.setShortname(course);
        c.setImageFile(course + File.separator + cxr.getCourseImage());
        c.setLangs(cxr.getLangs());
        c.setPriority(cxr.getPriority());

        DbHelper db = DbHelper.getInstance(ctx);
        long courseId = db.addOrUpdateCourse(c);

        if (courseId != -1) {
          db.insertActivities(cxr.getActivities(courseId));
          db.insertTrackers(ctxr.getTrackers(courseId, 0));
        }

        // add schedule
        // put this here so even if the course content isn't updated the schedule will be
        db.insertSchedule(csxr.getSchedule());
        db.updateScheduleVersion(courseId, csxr.getScheduleVersion());
      }
    }
  }
  // update all the current quiz results for the score/maxscore etc
  protected void upgradeV54() {
    DbHelper db = DbHelper.getInstance(ctx);
    ArrayList<QuizAttempt> quizAttempts = db.getAllQuizAttempts();
    long userId = db.getUserId(SessionManager.getUsername(ctx));

    ArrayList<Course> courses = db.getAllCourses();
    ArrayList<v54UpgradeQuizObj> quizzes = new ArrayList<>();

    for (Course c : courses) {
      try {
        CourseXMLReader cxr = new CourseXMLReader(c.getCourseXMLLocation(), c.getCourseId(), ctx);

        ArrayList<Activity> baseActs = cxr.getBaselineActivities();
        for (Activity a : baseActs) {
          if (a.getActType().equalsIgnoreCase("quiz")) {
            String quizContent = a.getContents("en");
            try {
              JSONObject quizJson = new JSONObject(quizContent);
              v54UpgradeQuizObj q = new v54UpgradeQuizObj();
              q.id = quizJson.getInt("id");
              q.digest = quizJson.getJSONObject("props").getString("digest");
              q.threshold = quizJson.getJSONObject("props").getInt("passthreshold");
              quizzes.add(q);
            } catch (JSONException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            }
          }
        }

        // now add the standard activities
        ArrayList<Activity> acts = cxr.getActivities(c.getCourseId());
        for (Activity a : acts) {
          if (a.getActType().equalsIgnoreCase("quiz")) {
            String quizContent = a.getContents("en");
            try {
              JSONObject quizJson = new JSONObject(quizContent);
              v54UpgradeQuizObj q = new v54UpgradeQuizObj();
              q.id = quizJson.getInt("id");
              q.digest = quizJson.getJSONObject("props").getString("digest");
              q.threshold = quizJson.getJSONObject("props").getInt("passthreshold");
              quizzes.add(q);
            } catch (JSONException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            }
          }
        }
      } catch (InvalidXMLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

    for (QuizAttempt qa : quizAttempts) {
      // data back to json obj
      try {
        JSONObject jsonData = new JSONObject(qa.getData());
        qa.setMaxscore((float) jsonData.getDouble("maxscore"));
        qa.setScore((float) jsonData.getDouble("score"));

        int quizId = jsonData.getInt("quiz_id");

        v54UpgradeQuizObj currentQuiz = null;

        // find the relevant quiz in quizzes
        for (v54UpgradeQuizObj tmpQuiz : quizzes) {
          if (tmpQuiz.id == quizId) {
            currentQuiz = tmpQuiz;
            break;
          }
        }

        if (currentQuiz == null) {
          Log.d(TAG, "not found");
        } else {
          Log.d(TAG, "Found");
          qa.setActivityDigest(currentQuiz.digest);
          if (qa.getScoreAsPercent() >= currentQuiz.threshold) {
            qa.setPassed(true);
          } else {
            qa.setPassed(false);
          }
        }

        // make the actual updates
        qa.setUserId(userId);
        db.updateQuizAttempt(qa);

      } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

    ArrayList<QuizAttempt> checkQuizAttempts = db.getAllQuizAttempts();
    for (QuizAttempt qa : checkQuizAttempts) {
      // display current data
      Log.d(TAG, "data: " + qa.getData());
      Log.d(TAG, "digest: " + qa.getActivityDigest());
      Log.d(TAG, "userid: " + qa.getUserId());
      Log.d(TAG, "courseid: " + qa.getCourseId());
      Log.d(TAG, "score: " + qa.getScore());
      Log.d(TAG, "maxscore: " + qa.getMaxscore());
      Log.d(TAG, "passed: " + qa.isPassed());
    }
  }