<commit_before><?php
/**
 * Builds the course dashboard and populates it with cool
 * graphs and data and stuff.
 *
 * @author <NAME>
 * @link http://www.benoverholts.com/
 * @license http://opensource.org/licenses/MIT
 */

class Pico_Dashboard {

	public function plugins_loaded()
	{
		
	}
	
	public function request_url(&$url)
	{
		
	}
	
	public function before_load_content(&$file)
	{
		
	}
	
	public function after_load_content(&$file, &$content)
	{
		
	}
	
	public function before_404_load_content(&$file)
	{
		
	}
	
	public function after_404_load_content(&$file, &$content)
	{
		
	}
	
	public function config_loaded(&$settings)
	{
		
	}
	
	public function before_read_file_meta(&$headers)
	{
		
	}
	
	
	public function content_parsed(&$content)
	{
		
	}

    public function file_meta(&$meta)
    {
        $this->coursename = $meta['title'];
        $this->layout = $meta['layout'];
    }
	
	public function get_pages(&$pages, &$current_page, &$prev_page, &$next_page)
	{
		
	}
	
	public function before_twig_register()
	{
		
	}
	
	public function before_render(&$twig_vars, &$twig)
	{
        if ($this->layout == 'course') {
            $twig_vars['current_page']['title'] = $this->coursename;
		    $twig_vars['dashboard'] = $this->buildDash();
        }
	}
	
	public function after_render(&$output)
	{
		
	}
	
    private function buildDash() {
    	session_start();

    	// only bother doing this function for logged in users
        if (isset($_SESSION['authed']) && $_SESSION['authed']) {

        	
        	// get the last page visited
        	// TODO: This can be made more efficient by reading backwards from the end of the file
        	$plugin_path = dirname(dirname(__FILE__));
        	$user = $_SESSION['username'];
        	$last = NULL;
        	if(($handle = fopen($plugin_path . '/log/' . $user . '.log', "r")) !== FALSE) {
        		while(($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        			if(strcmp($data[0], '[HIT]') == 0 && preg_match('/^\/' . str_replace(" ", "_", $this->coursename) .'\/(.*)$/', $data[2]) === 1) {
        				$last = $data[2];
        			}
        		}
        	}
        	if (isset($settings['base_url'])) {
            	$this->pathheader = $settings['base_url'];
        	}
        	if($last == NULL){
        		$last_page = "Visit a page in this course to see your bookmark here";
        	} else {
        		if(strcmp(substr($last, -1),"/") == 0) {
        			preg_match('/.*\.(\w*)\/\z/', str_replace("-", "_", $last), $matches);
        			$chapter = $matches[1];
        			$chapter = '<a href=' . $this->pathheader . $last . '>' . $chapter . '</a>';
        			$last_page = 'You were last at the index of ' . $chapter;
        		} else {
        			preg_match('/.*\.(\w*)\/\d+\.(\w*)\z/', str_replace("-", "_", $last), $matches);
        			$page = str_replace("_", " ", $matches[2]);
        			$chapter = str_replace("_", " ", $matches[1]);
        			$page = '<a href=' . $this->pathheader . $last . '>' . $page . '</a>';
        			$last_page = 'You were last at ' . $page . ' in ' . $chapter;
        		}
        	}

        	// build quizzes taken / average score graph
        	$quiz_avg = array(); // indexed by months
        	$quiz_count = array(); // indexed by months
        	if(($handle = fopen($plugin_path . '/log/' . $user . '.log', "r")) !== FALSE) {
        		while(($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        			if(strcmp($data[0], '[SQZ]') == 0) {
        				$month = date_parse_from_format('Y/m/d H:i:s', $data[1]);
        				$month = $month['month'];
        				preg_match('/(\d+)\/(\d+)/', $data[4], $matches);
        				if(isset($quiz_count[$month])) {
        					$quiz_count[$month] += 1;
        				} else {
        					$quiz_count[$month] = 1;
        				}
        				if(isset($quiz_avg[$month])) {
        					$quiz_avg[$month] += $matches[1]/$matches[2];
        				} else {
        					$quiz_avg[$month] = $matches[1]/$matches[2];
        				}
        			}
        		}
        	}

        	// build time spent by course graph

        	//var_dump($quiz_avg);
        	//var_dump($quiz_count);

        	// build dashboard html
        	$dashCode = '<div class="row">
				<div class="col-md-12">' .
					'<h3>Welcome to ' . $this->coursename . ', ' . $_SESSION['username'] . '</h3>' .
				'</div>
				<div class="col-md-12">
					<div class="well">
						<h4>' . $last_page . '</h4>
						<h4>Hours spent per course & Number of quizzes taken/Average quiz score</h4>
						<canvas id="myChart" width="300" height="400"></canvas><canvas id="myChart2" width="300" height="400"></canvas>
					</div>
				</div>
			</div>
			</div>
			<script type="text/javascript">
			var data = {
			    labels : ["Biology", "Business", "Finance", "Physics", "Writing"],<commit_msg>Added computations for time spent on each chapter<commit_after><?php
/**
 * Builds the course dashboard and populates it with cool
 * graphs and data and stuff.
 *
 * @author <NAME>
 * @link http://www.benoverholts.com/
 * @license http://opensource.org/licenses/MIT
 */

class Pico_Dashboard {

	public function plugins_loaded()
	{
		
	}
	
	public function request_url(&$url)
	{
		
	}
	
	public function before_load_content(&$file)
	{
		
	}
	
	public function after_load_content(&$file, &$content)
	{
		
	}
	
	public function before_404_load_content(&$file)
	{
		
	}
	
	public function after_404_load_content(&$file, &$content)
	{
		
	}
	
	public function config_loaded(&$settings)
	{
		
	}
	
	public function before_read_file_meta(&$headers)
	{
		
	}
	
	
	public function content_parsed(&$content)
	{
		
	}

    public function file_meta(&$meta)
    {
        $this->coursename = $meta['title'];
        $this->layout = $meta['layout'];
    }
	
	public function get_pages(&$pages, &$current_page, &$prev_page, &$next_page)
	{
		
	}
	
	public function before_twig_register()
	{
		
	}
	
	public function before_render(&$twig_vars, &$twig)
	{
        if ($this->layout == 'course') {
            $twig_vars['current_page']['title'] = $this->coursename;
		    $twig_vars['dashboard'] = $this->buildDash();
        }
	}
	
	public function after_render(&$output)
	{
		
	}
	
    private function buildDash() {
    	session_start();

    	$Fcoursename = str_replace(" ", "_", $this->coursename);

    	// only bother doing this function for logged in users
        if (isset($_SESSION['authed']) && $_SESSION['authed']) {

        	
        	// get the last page visited
        	// TODO: This can be made more efficient by reading backwards from the end of the file
        	$plugin_path = dirname(dirname(__FILE__));
        	$user = $_SESSION['username'];
        	$last = NULL;
        	if(($handle = fopen($plugin_path . '/log/' . $user . '.log', "r")) !== FALSE) {
        		while(($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        			if(strcmp($data[0], '[HIT]') == 0 && preg_match('/^\/' . $Fcoursename .'\/(.*)$/', $data[2]) === 1) {
        				$last = $data[2];
        			}
        		}
        	}
        	if (isset($settings['base_url'])) {
            	$this->pathheader = $settings['base_url'];
        	}
        	if($last == NULL){
        		$last_page = "Visit a page in this course to see your bookmark here";
        	} else {
        		if(strcmp(substr($last, -1),"/") == 0) {
        			preg_match('/.*\.(\w*)\/\z/', str_replace("-", "_", $last), $matches);
        			$chapter = $matches[1];
        			$chapter = '<a href=' . $this->pathheader . $last . '>' . $chapter . '</a>';
        			$last_page = 'You were last at the index of ' . $chapter;
        		} else {
        			preg_match('/.*\.(\w*)\/\d+\.(\w*)\z/', str_replace("-", "_", $last), $matches);
        			$page = str_replace("_", " ", $matches[2]);
        			$chapter = str_replace("_", " ", $matches[1]);
        			$page = '<a href=' . $this->pathheader . $last . '>' . $page . '</a>';
        			$last_page = 'You were last at ' . $page . ' in ' . $chapter;
        		}
        	}

        	// build quizzes taken / average score graph
        	$quiz_avg = array(); // indexed by months
        	$quiz_count = array(); // indexed by months
        	if(($handle = fopen($plugin_path . '/log/' . $user . '.log', "r")) !== FALSE) {
        		while(($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        			if(strcmp($data[0], '[SQZ]') == 0) {
        				$month = date_parse_from_format('Y/m/d H:i:s', $data[1]);
        				$month = $month['month'];
        				preg_match('/(\d+)\/(\d+)/', $data[5], $matches);
        				if(isset($quiz_count[$month])) {
        					$quiz_count[$month] += 1;
        				} else {
        					$quiz_count[$month] = 1;
        				}
        				if(isset($quiz_avg[$month])) {
        					$quiz_avg[$month] += $matches[1]/$matches[2];
        				} else {
        					$quiz_avg[$month] = $matches[1]/$matches[2];
        				}
        			}
        		}
        	}

        	// build time spent by course graph
        	// ! SQZ tagged lines are not read by this
        	// ! Time for last page wont be included if user goes straight from page to course dashboard (needs to refresh)
        	//		(not sure if this can be fixed in pico, may need to wait for phile implementation)

        	// this can probably be cleaned up logically

        	$chapter_last_timestamp = NULL; // indexed by chapter, needed for cumulative summation.
        	$prev_chapter = NULL;
        	$make_set = FALSE;
        	$chapter_time = array(); // indexed by chapter, number of seconds spent on each chapter.
        	if(($handle = fopen($plugin_path . '/log/' . $user . '.log', "r")) !== FALSE) {
        		while(($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        			if(strcmp($data[0], '[HIT]') == 0) {
        				if(preg_match('/\A\/(\w*)\/\d+\.(\w*).*/', $data[2], $matches) === 1 && strcmp($matches[1], $Fcoursename) == 0) {
        					if($prev_chapter != NULL) {
	        					if($make_set && (strtotime($data[1]) - $chapter_last_timestamp) < 900) {
	        						if(isset($chapter_time[$prev_chapter])) {
	        							//echo 'Added ' . (strtotime($data[1]) - $chapter_last_timestamp) . ' seconds to ' . $prev_chapter . "\n";
										$chapter_time[$prev_chapter] = $chapter_time[$prev_chapter] + (strtotime($data[1]) - $chapter_last_timestamp);
	        						} else {
	        							//echo 'Added ' . (strtotime($data[1]) - $chapter_last_timestamp) . ' seconds to ' . $prev_chapter . " (init)\n";
	        							$chapter_time[$prev_chapter] = (strtotime($data[1]) - $chapter_last_timestamp);
	        						}
	        					} elseif($make_set && (strtotime($data[1]) - $chapter_last_timestamp) > 900) {
	        						if(isset($chapter_time[$prev_chapter])) {
	        							//echo 'Added ' . 60 . ' seconds to ' . $prev_chapter . "\n";
										$chapter_time[$prev_chapter] = $chapter_time[$prev_chapter] + 60;
	        						} else {
	        							//echo 'Added ' . 60 . ' seconds to ' . $prev_chapter . " (init)\n";
	        							$chapter_time[$prev_chapter] = 60;
	        						}
	        					}
	        					$prev_chapter = $matches[2];
	        					$chapter_last_timestamp = strtotime($data[1]);
	        					$make_set = TRUE;
	        				} else {
	        					$prev_chapter = $matches[2];
	        					$chapter_last_timestamp = strtotime($data[1]);
	        					$make_set = TRUE;
	        				}
        				} else {
        					if($prev_chapter != NULL) {
	        					if($make_set && (strtotime($data[1]) - $chapter_last_timestamp) < 900) {
	        						if(isset($chapter_time[$prev_chapter])) {
	        							//echo 'Added ' . (strtotime($data[1]) - $chapter_last_timestamp) . ' seconds to ' . $prev_chapter . "\n";
										$chapter_time[$prev_chapter] = $chapter_time[$prev_chapter] + (strtotime($data[1]) - $chapter_last_timestamp);
	        						} else {
	        							//echo 'Added ' . (strtotime($data[1]) - $chapter_last_timestamp) . ' seconds to ' . $prev_chapter . " (init)\n";
	        							$chapter_time[$prev_chapter] = (strtotime($data[1]) - $chapter_last_timestamp);
	        						}
	        					} elseif($make_set && (strtotime($data[1]) - $chapter_last_timestamp) > 900) {
	        						if(isset($chapter_time[$prev_chapter])) {
	        							//echo 'Added ' . 60 . ' seconds to ' . $prev_chapter . "\n";
										$chapter_time[$prev_chapter] = $chapter_time[$prev_chapter] + 60;
	        						} else {
	        							//echo 'Added ' . 60 . ' seconds to ' . $prev_chapter . " (init)\n";
	        							$chapter_time[$prev_chapter] = 60;
	        						}
	        					}
	        					$make_set = FALSE;
	        				}  else {
	        					$prev_chapter = $matches[2];
	        					$chapter_last_timestamp = strtotime($data[1]);
	        					$make_set = TRUE;
	        				}
        				}
        			}
        		}
        	}

			var_dump($chapter_time);   	

        	// build dashboard html
        	$dashCode = '<div class="row">
				<div class="col-md-12">' .
					'<h3>Welcome to ' . $this->coursename . ', ' . $_SESSION['username'] . '</h3>' .
				'</div>
				<div class="col-md-12">
					<div class="well">
						<h4>' . $last_page . '</h4>
						<h4>Hours spent per course & Number of quizzes taken/Average quiz score</h4>
						<canvas id="myChart" width="300" height="400"></canvas><canvas id="myChart2" width="300" height="400"></canvas>
					</div>
				</div>
			</div>
			</div>
			<script type="text/javascript">
			var data = {
			    labels : ["Biology", "Business", "Finance", "Physics", "Writing"],
{'fraction_non_alphanumeric': 0.18096365924061875, 'fraction_numerical': 0.012582340315298646, 'mean_word_length': 2.039820022497188, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4523046', 'n_tokens_mistral': 5338, 'n_tokens_neox': 4562, 'n_words': 892}