library(tidyverse)
library(janitor)
library(lubridate)
library(stringi)
library(Hmisc)
library(ggeasy)
library(plotly)

theme_set(theme_classic())
log1A <- read_csv(here::here("research_conferences", "ScienceMoodleWellbeing", "logs_PSYC1001-5216_00336_20210816-0953.csv")) %>%
  clean_names() %>%
  filter(component == "Book") %>%
  filter(user_full_name %nin% c("Jenny Richmond", "Nadia Menon")) 

glimpse(log1A)
## Rows: 1,976
## Columns: 9
## $ time           <chr> "15/08/21, 23:00", "15/08/21, 23:00", "15/08/21, 18:50"…
## $ user_full_name <chr> "Vanessa Samra", "Vanessa Samra", "Megan Harris", "Mega…
## $ affected_user  <chr> "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", …
## $ event_context  <chr> "Book: Self-management for Success and Wellbeing", "Boo…
## $ component      <chr> "Book", "Book", "Book", "Book", "Book", "Book", "Book",…
## $ event_name     <chr> "Chapter viewed", "Course module viewed", "Chapter view…
## $ description    <chr> "The user with id '2502116' viewed the chapter with id …
## $ origin         <chr> "web", "web", "web", "web", "web", "web", "web", "web",…
## $ ip_address     <chr> "27.32.122.81", "27.32.122.81", "2406:3400:24d:fe00:d43…
log1A$time <- dmy_hm(log1A$time) # parse dates

log1A <- log1A %>%
  mutate(date = date(time), week = week(time)) # pull date and week

log1A$week <- factor(log1A$week, labels = c("week0", "week1", "week2", "week3", "week4", "week5", "week6", "week7", "week8", "week9", "week10", "week11", "week12")) #relabel week variable 

# new dataframe for chapter views

chapters <- log1A %>%
  filter(event_name == "Chapter viewed")

# pull which chapter out of description

chapters_id <- chapters %>%
  mutate(description = str_remove(description, "for the book with course module id '3851996'.")) %>% # drop the course module info from description
  mutate(studentid = parse_number(description)) %>% # parse the first number in the description
  mutate(chapterid = stri_extract_last_regex(description, "\\d{6}")) # parse the last 6 digit number

chapters_labels <- chapters_id %>%
  mutate(label = case_when(chapterid == '315673' ~ '1. Self management for success and wellbeing',
                           chapterid == '315674' ~ '1.1. Get help now',
                            chapterid == '315675' ~ '2. Time-management and getting important things done', 
                           chapterid == '315676' ~ '2.1. Plan out your assessments' ,
            chapterid == '315677' ~ '2.2. Set goals for the term' ,
              chapterid == '315678' ~ '2.3. Plan your week and prioritise your to do list' ,
              chapterid == '315679' ~ '2.4. Develop your career readiness' ,
          chapterid == '315680' ~ '2.5. The Science and more' ,
            chapterid == '315681' ~ '3. But I worked so hard!', 
                chapterid == '315682' ~ '3.1. What study strategies do you use?', 
            chapterid == '315683' ~ '3.2. Take good notes', 
              chapterid == '315684' ~ '3.3. Get the most out of lectures.', 
              chapterid == '315685' ~ '3.4. Tips for successful study', 
            chapterid == '315686' ~ '3.5. Strategies from the Learning Scientists', 
            chapterid == '315687' ~ '3.6. Try mindfulness', 
          chapterid == '315688' ~ '3.7. Exams with less stress', 
          chapterid == '315689' ~ '3.8. The Science and more', 
          chapterid == '315690' ~ '4. Paralysing P', 
chapterid == '315691' ~ '4.1. Procrastination and Perfectionism', 
chapterid == '315692' ~ '4.2. Problem thinking and asking for help', 
chapterid == '315693' ~ '4.3. The Science and more', 
chapterid == '315694' ~ '5. Making the most of performance feedback', 
chapterid == '315695' ~ '5.1. Reflecting on how you handled your assessment', 
chapterid == '315696' ~ '5.2. Receiving feedback', 
chapterid == '315697' ~ '5.3. Flexibility week check in', 
chapterid == '315698' ~ '6. Wrangling stress', 
chapterid == '315699' ~ '6.1. Tips for stress relief', 
chapterid == '315700' ~ '6.2. Positive psychology tools', 
chapterid == '315701' ~ '6.3. Problem-solving worksheet', 
chapterid == '315702' ~ '6.4. Future vs.present balance and more', 
chapterid == '315703' ~ '6.5. Stressors out of your control? Applying for special consideration', 
chapterid == '315704' ~ '6.6. Ongoing Health Issues Possibly Compromising Learning?', 
chapterid == '315705' ~ '6.7. The Science and more', 
chapterid == '315706' ~ '7. Physical and Psychological Health', 
chapterid == '315707' ~ '7.1. Connecting with other students at UNSW', 
chapterid == '315708' ~ '7.2. Sexual Health and consent', 
chapterid == '315709' ~ '8. How to learn more', 
chapterid == '315710' ~ '8.1. Courses about wellbeing and mental health', 
chapterid == '315711' ~ '8.2. Leadership Skills Training', 
chapterid == '315712' ~ '8.3. Courses about academic skills, career and finance', 
chapterid == '315713' ~ '9. Money, Housing and Living away from home', 
chapterid == '315714' ~ '10. Academic skills', 
chapterid == '315715' ~ '10.1. Help with writing', 
chapterid == '315716' ~ '10.2. Help with English as a second language', 
chapterid == '315717' ~ '10.3. Help with Group work', 
chapterid == '315718' ~ '10.4. Communicating professionally', 
          TRUE ~ "noID_YET")) %>%
  mutate(chapter_number = parse_number(label)) %>%
  mutate(chapter_no = floor(chapter_number)) # round chapter number down

chapters_labels$chapter_no <- factor(chapters_labels$chapter_no, 
                                     labels = c("1_intro", "2_time", "3_study", "4_ps", "5_feedback", "6_stress", "7_health", "8_learnmore", "9_money", "10_academics"))

COUNTS

# views

log1A %>%
  count(event_name)
## # A tibble: 2 x 2
##   event_name               n
##   <chr>                <int>
## 1 Chapter viewed        1799
## 2 Course module viewed   177
# unique students

n_distinct(log1A$user_full_name)
## [1] 246

PLOTS

views by date

chapters %>%
  group_by(date, week) %>%
  count() %>%
  ggplot(aes(x = date, y = n, label = week)) +
  geom_line() +
  labs(y = "chapter views") +
  geom_text(data = data.frame(x = as.Date("2021-06-12"),
y = 75.6580407457739, label = "Week 3"), mapping = aes(x = x, y = y, label = label), inherit.aes = FALSE) +
  geom_text(data = data.frame(x = as.Date("2021-07-04"),
y = 31.1947115465317, label = "Week 6"),
mapping = aes(x = x, y = y, label = label),
inherit.aes = FALSE) +
  geom_text(data = data.frame(x = as.Date("2021-08-11"),
y = 40.2215582085328, label = "stuvac"),
mapping = aes(x = x, y = y, label = label),
inherit.aes = FALSE) +
  geom_text(data = data.frame(x = as.Date("2021-06-01"),
y = 199.472227330307, label = "Week 1"),
mapping = aes(x = x, y = y, label = label),
inherit.aes = FALSE)

views by week

chapters %>%
  group_by(week) %>%
  count() %>%
  ggplot(aes(x = week, y = n, fill = week)) +
  geom_col() +
  labs(y = "chapter views") +
  easy_remove_legend() +
  scale_y_continuous(expand = c(0,0))

views by chapter

chapters_labels %>%
  count(chapter_no) %>%
  ggplot(aes(x = chapter_no, y = n, fill = chapter_no)) +
  geom_col() +
  labs(y = "chapter views") +
  easy_remove_legend() +
  scale_y_continuous(expand = c(0,0))

views by week and chapter

chapters_labels %>%
  group_by(week, chapter_no) %>%
  count() %>%
  ggplot(aes(x = week, y = n, colour = chapter_no, group = chapter_no)) +
  geom_point() +
  geom_line() +
  labs(y = "chapter views")