<?PHP //$Id: block_use_stats.php,v 1.1.2.5 2012/06/11 10:13:08 diml Exp $

defined('MOODLE_INTERNAL') || die();

require_once($CFG->dirroot.'/blocks/use_stats/locallib.php');

/**
*/
class block_use_stats extends block_base {

    function init() {
        global $COURSE;

        $context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
        if (has_capability('block/use_stats:view', $context)){
            $this->title = get_string('blockname','block_use_stats');
        } else {
            $this->title = get_string('blocknameforstudents','block_use_stats');
        }
        $this->version = 2011042400;
    }

    /**
    * is the bloc configurable ?
    */
    function has_config() {
        return true;
    }
    
    /**
    * do we have local config
    */
    function instance_allow_config() {
        global $COURSE;

        // if admin always configure
        $context = get_context_instance(CONTEXT_SYSTEM, 0);
        if (has_capability('moodle/site:doanything', $context)) return true;

        // if not "MyMoodle" and is teacher, can configure
        $context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
        if (has_capability('block/use_stats:view', $context)){
            if ($COURSE->id > 1) return true;
        }
        return false;
    }

    function applicable_formats() {
        return array('all' => true);
    }
    
    /**
    *
    */
    function user_can_addto($page) {
        global $CFG, $COURSE;

        // guests never see anything
        if (isguest()) return false;

        if (has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))){
            return true;
        }        

        $context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
        if (has_capability('block/use_stats:view', $context)){
            if ($COURSE->id > 1){ // if not MyMoodle, students see something if a teacher allowed to
                if (empty($this->config->studentscanuse)){
                    return false;
                }
            } else { // if MyMoodle students see something if they are allowed to by global config
                if (empty($CFG->block_use_stats_studentscanuse)){
                    return false;
                }
            }
        }
        return true;
    }

    /**
    *
    */
    function user_can_edit() {
        global $CFG, $COURSE;

       // guests never see anything
        if (isguest()) return false;
        
        if (has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))){
            return true;
        }        

        $context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
        if (has_capability('block/use_stats:view', $context)){
            if ($COURSE->id > 1){ // if not MyMoodle, students see something if a teacher allowed to
                if (empty($this->config->studentscanuse)){
                    return false;
                }
            } else { // if MyMoodle students see something if they are allowed to by global config
                if (empty($CFG->block_use_stats_studentscanuse)){
                    return false;
                }
            }
        }
        return true;
    }

    /**
    * Produce content for the bloc
    */
    function get_content() {
        global $USER, $CFG, $COURSE;

        if ($this->content !== NULL) {
            return $this->content;
        }

        $this->content = new stdClass;
        $this->content->text = '';
        $this->content->footer = '';
        
        if (empty($this->instance)) {
            return $this->content;
        }
        
        // Get context so we can check capabilities.
        $context = get_context_instance(CONTEXT_BLOCK, $this->instance->id);
        $systemcontext = get_context_instance(CONTEXT_SYSTEM);
        if (!has_capability('block/use_stats:view', $context)){
            return $this->content;
        }

        $id = optional_param('id', PARAM_INT);
        $fromwhen = 30;
        if (!empty($CFG->block_use_stats_fromwhen)) {
            $fromwhen = optional_param('ts_from',$CFG->block_use_stats_fromwhen, PARAM_INT);
        }
        $daystocompilelogs = $fromwhen * DAYSECS;
        $timefrom = time() - $daystocompilelogs;
        
        if (has_any_capability(array('block/use_stats:seesitedetails', 'block/use_stats:seecoursedetails', 'block/use_stats:seegroupdetails'), $context, $USER->id)){
            $userid = optional_param('uid', $USER->id, PARAM_INT);
        } else {
            $userid = $USER->id;
        }

        $logs = use_stats_extract_logs($timefrom, time(), $userid);
        $lasttime = $timefrom;
        $totalTime = 0;
        $totalTimeCourse = array();
        $totalTimeModule = array();
        if ($logs){
            foreach($logs as $aLog){
                $delta = $aLog->time - $lasttime;
                if ($delta < @$CFG->block_use_stats_threshold * MINSECS){
                    $totalTime = $totalTime + $delta;

                    if (!array_key_exists($aLog->course, $totalTimeCourse))
                        $totalTimeCourse[$aLog->course] = 0;
                    else
                        $totalTimeCourse[$aLog->course] = $totalTimeCourse[$aLog->course] + $delta;

                    if (!array_key_exists($aLog->course, $totalTimeModule))
                        $totalTimeModule[$aLog->course][$aLog->module] = 0;
                    elseif (!array_key_exists($aLog->module, $totalTimeModule[$aLog->course]))
                        $totalTimeModule[$aLog->course][$aLog->module] = 0;
                    else
                        $totalTimeModule[$aLog->course][$aLog->module] = $totalTimeModule[$aLog->course][$aLog->module] + $delta;
                }
                $lasttime = $aLog->time;
            }
            
            $hours = floor($totalTime/HOURSECS);
            $remainder = $totalTime - $hours * HOURSECS;
            $min = floor($remainder/MINSECS);

            if (file_exists("$CFG->dirroot}/theme/{$CFG->theme}/block_use_stats.css")){
                $this->content->text = "<link rel=\"stylesheet\" href=\"{$CFG->wwwroot}/theme/".current_theme()."/block_use_stats.css\" type=\"text/css\" />";
            } elseif (file_exists("$CFG->dirroot}/theme/default/block_use_stats.css")){
                $this->content->text = "<link rel=\"stylesheet\" href=\"{$CFG->wwwroot}/theme/default/block_use_stats.css\" type=\"text/css\" />";
            }

            $this->content->text .= "<div class=\"message\">";
            $this->content->text .= " <form style=\"display:inline\" name=\"ts_changeParms\" method=\"post\" action=\"#\">";
            $this->content->text .= "<input type=\"hidden\" name=\"id\" value=\"$id\" />";
            if (has_capability('block/use_stats:seesitedetails', $context, $USER->id)){
                $users = get_records('user', 'deleted', '0', 'lastname', 'id, firstname, lastname');
            } 
            else if (has_capability('block/use_stats:seecoursedetails', $context, $USER->id)){
            	$coursecontext = get_context_instance(CONTEXT_COURSE, $COURSE->id);
                $users = get_users_by_capability($coursecontext, 'moodle/course:view', 'u.id, firstname, lastname');
            }
            else if (has_capability('block/use_stats:seegroupdetails', $context, $USER->id)){
            	$mygroups = groups_get_user_groups($COURSE->id);
            	print_object($mygroups);
            	$users = array();
            	// get all users in my groups
            	foreach($mygroupids as $mygroupid){
	            	$users = $fellows + groups_get_members ($groupid, 'u.id, firstname, lastname');
	            }
            }

            if (!empty($users)){
                $usermenu = array();
                foreach($users as $user){
                    $usermenu[$user->id] = fullname($user);
                }
	            $this->content->text .= choose_from_menu($usermenu, 'uid', $userid, 'choose', 'document.ts_changeParms.submit();', '0', true);
            }
            $this->content->text .= get_string('from', 'block_use_stats');
            $this->content->text .= "<select name=\"ts_from\" onChange=\"document.ts_changeParms.submit();\">";
            foreach(array(5,15,30,60,90,365) as $interval){
                $selected = ($interval == $fromwhen) ? "selected=\"selected\"" : '' ;
                $this->content->text .= "<option value=\"{$interval}\" {$selected} >{$interval} ".get_string('days')."</option>";
            }
            $this->content->text .= "</select>";
            $this->content->text .= "</form><br/>";
            $this->content->text .= get_string('youspent', 'block_use_stats');
            $this->content->text .= $hours . ' ' . get_string('hours') . ' ' . $min . ' ' . get_string('mins'); 
            $this->content->text .= get_string('onthisMoodlefrom', 'block_use_stats');
            $this->content->text .= userdate($timefrom);
            if (count(array_keys($totalTimeCourse))){
                $this->content->text .= "<table width=\"100%\">";
                foreach(array_keys($totalTimeCourse) as $aCourseId){
                    $aCourse = get_record('course', 'id', $aCourseId);
                    if ($totalTimeCourse[$aCourseId] < 60) continue;
                    if ($aCourse){
                        $hours = floor($totalTimeCourse[$aCourseId] / HOURSECS);
                        $remainder = $totalTimeCourse[$aCourseId] - $hours * HOURSECS;
                        $min = floor($remainder/MINSECS);
                        $courseelapsed = $hours . ' ' . get_string('hours') . ' ' . $min . ' ' . get_string('mins'); 
                        $this->content->text .= "<tr><td class=\"teacherstatsbycourse\" align=\"left\" title=\"".htmlspecialchars(format_string($aCourse->fullname))."\">{$aCourse->shortname}</td><td class=\"teacherstatsbycourse\" align=\"right\">{$courseelapsed}</td></tr>";
                    }
                }
                $this->content->text .= "</table>";
            }
            $this->content->text .= "</div>";

            if (has_any_capability(array('block/use_stats:seeowndetails', 'block/use_stats:seesitedetails', 'block/use_stats:seecoursedetails', 'block/use_stats:seegroupdetails'), $context, $USER->id)){
                $showdetailstr = get_string('showdetails', 'block_use_stats');
                $fromclause = (!empty($fromwhen)) ? "&amp;ts_from={$fromwhen}" : '' ;
                $this->content->text .= "<a href=\"{$CFG->wwwroot}/blocks/use_stats/detail.php?id={$this->instance->id}&amp;userid={$userid}{$fromclause}&course={$COURSE->id}\">$showdetailstr</a>";
            }
        } else {
            $this->content->text = "<div class=\"message\">";
            $this->content->text .= get_string('noavailablelogs', 'block_use_stats');
            $this->content->text .= "<br/>";
            $this->content->text .= " <form style=\"display:inline\" name=\"ts_changeParms\" method=\"post\" action=\"#\">";
            $this->content->text .= "<input type=\"hidden\" name=\"id\" value=\"$id\" />";
            if (has_capability('block/use_stats:seesitedetails', $context, $USER->id)){
                $users = get_records('user', 'deleted', '0', 'lastname', 'id, firstname, lastname');
            }
            else if (has_capability('block/use_stats:seecoursedetails', $context, $USER->id)){
            	$coursecontext = get_context_instance(CONTEXT_COURSE, $COURSE->id);
                $users = get_users_by_capability($coursecontext, 'moodle/course:view', 'u.id, firstname, lastname');
            }
            else if (has_capability('block/use_stats:seegroupdetails', $context, $USER->id)){
            	$mygroupings = groups_get_user_groups($COURSE->id);

				$mygroups = array();            	
            	foreach($mygroupings as $grouping){
            		$mygroups = $mygroups + $grouping;
            	}
            	
            	$users = array();
            	// get all users in my groups
            	foreach($mygroups as $mygroupid){
            		$members = groups_get_members($mygroupid, 'u.id, firstname, lastname');
            		if ($members){
		            	$users = $users + $members;
		            }
	            }
            }
            if (!empty($users)){
                $usermenu = array();
                foreach($users as $user){
                    $usermenu[$user->id] = fullname($user);
                }
            	$this->content->text .= choose_from_menu($usermenu, 'uid', $userid, 'choose', 'document.ts_changeParms.submit();', '0', true);
            }
            $this->content->text .= get_string('from', 'block_use_stats');
            $this->content->text .= "<select name=\"ts_from\" onChange=\"document.ts_changeParms.submit();\">";
            foreach(array(5,15,30,60,90,365) as $interval){
                $selected = ($interval == $fromwhen) ? "selected=\"selected\"" : '' ;
                $this->content->text .= "<option value=\"{$interval}\" {$selected} >{$interval} ".get_string('days')."</option>";
            }
            $this->content->text .= "</select>";
            $this->content->text .= "</form><br/>";
            $this->content->text .= "</div>";
        }        

        return $this->content;
    }


	/**
	 * Setup the XMLRPC service, RPC calls and default block parameters.
	 * @return boolean TRUE if the installation is successfull, FALSE otherwise.
	 */
	function after_install() {
		global $CFG;
		
		// Initialising
		$result = true;
		
		/*
		 * Installing use_stats service
		 */
		if (!get_record('mnet_service', 'name', 'use_stats')) {
			// Installing service
			$service = new stdclass;
			$service->name = 'use_stats';
			$service->description = get_string('use_stats_rpc_service_name', 'use_stats');
			$service->apiversion = 1;
			$service->offer = 1;
			if (!$serviceid = insert_record('mnet_service', $service)){
				notify('Error installing use_stats service.');
				$result = false;
			}
		}
		
		/*
		 * Installing RPC call 'get_stats'
		 */
		// Checking if it is already installed
		if (!get_record('mnet_rpc', 'function_name', 'use_stats_rpc_get_stats')) {
			
			// Creating RPC call
			$rpc = new stdclass;
			$rpc->function_name = 'use_stats_rpc_get_stats';
			$rpc->xmlrpc_path = 'blocks/use_stats/rpclib.php/use_stats_rpc_get_stats';
			$rpc->parent_type = 'block';  
			$rpc->parent = 'use_stats';
			$rpc->enabled = 0; 
			$rpc->help = 'get remotely use stats information.';
			$rpc->profile = '';
			
			// Adding RPC call
			if (!$rpcid = insert_record('mnet_rpc', $rpc)) {
				notify('Error installing use_stats RPC call "get_stats".');
				$result = false;
			} else {
				// Mapping service and call
				$rpcmap = new stdclass;
				$rpcmap->serviceid = $serviceid;
				$rpcmap->rpcid = $rpcid;
				if (!insert_record('mnet_service2rpc', $rpcmap)) {
					notify('Error mapping RPC call "get_stats" to the "use_stats" service.');
					$result = false;
				}
			}
		}

		if (!get_record('mnet_rpc', 'function_name', 'use_stats_rpc_get_scores')) {
			
			// Creating RPC call
			$rpc = new stdclass;
			$rpc->function_name = 'use_stats_rpc_get_scores';
			$rpc->xmlrpc_path = 'blocks/use_stats/rpclib.php/use_stats_rpc_get_scores';
			$rpc->parent_type = 'block';  
			$rpc->parent = 'use_stats';
			$rpc->enabled = 0; 
			$rpc->help = 'get remotely scores information.';
			$rpc->profile = '';
			
			// Adding RPC call
			if (!$rpcid = insert_record('mnet_rpc', $rpc)) {
				notify('Error installing use_scores RPC call "get_scores".');
				$result = false;
			} else {
				// Mapping service and call
				$rpcmap = new stdclass;
				$rpcmap->serviceid = $serviceid;
				$rpcmap->rpcid = $rpcid;
				if (!insert_record('mnet_service2rpc', $rpcmap)) {
					notify('Error mapping RPC call "get_scores" to the "use_scores" service.');
					$result = false;
				}
			}
		}
				
		// Returning result
		return $result;    
	}
	
	/**
	 * Remove the XMLRPC service.
	 * @return					boolean				TRUE if the deletion is successfull, FALSE otherwise.
	 */
	function before_delete() {
		global $CFG;
				
		// Checking if use_stats service is installed
		if (!($service = get_record('mnet_service', 'name', 'use_stats')))
			return true;
		
		// Uninstalling use_stats service
		delete_records('mnet_host2service', 'serviceid', $service->id);
		delete_records('mnet_service2rpc', 'serviceid', $service->id);
		delete_records('mnet_rpc', 'parent', 'use_stats');
		delete_records('mnet_service', 'name', 'use_stats');
		
		// Returning result
		return true;
	}
}

?>