/**
  * Fetch all objects. Return the object with the required params
  */
 public function test_fetch_ok()
 {
     $params = array('modeid' => '14');
     $result = evalcomix_modes_extra::fetch($params);
     $this->assertIsA($result, 'evalcomix_modes');
     unset($params, $result);
 }
Ejemplo n.º 2
0
    /**
     * Builds the grade table and returns it in HTML
     * @return string HTML
     */
    public function create_grade_table()
    {
        global $CFG, $OUTPUT, $USER;
        include_once $CFG->dirroot . '/blocks/evalcomix/classes/evalcomix_tasks.php';
        include_once $CFG->dirroot . '/blocks/evalcomix/classes/evalcomix_assessments.php';
        include_once $CFG->dirroot . '/blocks/evalcomix/classes/evalcomix_modes_time.php';
        include_once $CFG->dirroot . '/blocks/evalcomix/classes/evalcomix_modes_extra.php';
        include_once $CFG->dirroot . '/blocks/evalcomix/classes/evalcomix_modes.php';
        include_once $CFG->dirroot . '/blocks/evalcomix/classes/webservice_evalcomix_client.php';
        include_once $CFG->dirroot . '/blocks/evalcomix/classes/evalcomix_grades.php';
        include_once $CFG->dirroot . '/blocks/evalcomix/classes/evalcomix_allowedusers.php';
        $context = context_course::instance($this->courseid);
        $table = '
			<style type="text/css">
					.wrapper1, .wrapper2{width: 100%; border: none 0px RED;
					overflow-x: scroll; overflow-y:hidden;}
					.wrapper1{height: 20px; }
					.wrapper2{}
					#div1 {width:500px; height: 10px; }
					#div2 {width:500px;
					overflow: auto;}
					#div {width:100%}

				</style>
			<div id="div"></div>
			<div class="wrapper1">
				<div class="div1" id="div1" style="width:100%">
				</div>
			</div>
			<div class="wrapper2">
				<div class="div2" id="div2" style="width:100%;">
				
			<!-- <div id="wrapper2" style="overflow:auto;overflow-y:hidden;"> -->
			<table border=1 style="font-size:0.8em;text-align:right;" id="user-grades" class="gradestable flexible boxaligncenter generaltable">';
        //Obtains course´s users
        $users = $this->load_users();
        //if(!has_capability('block/evalcomix:edit',$context, $USER->id)){
        if (!has_capability('moodle/grade:viewhidden', $context, $USER->id)) {
            $coursegroups = $this->load_groups();
            $coursegroupings = $this->load_groupings();
        }
        $table .= $this->get_headers();
        $table .= "\n\t\t\t\t<script type=\"text/javascript\" src=\"../ajax.js\"></script>\n\t\t\t\t<script type=\"text/javascript\">\n\t\t\t\t\tvar recarga = 0;\n\t\t\t\t\n\t\t\t\t\t function relocate(page,params)\n\t\t\t\t\t {\n\t\t\t\t\t\t  var body = document.body;\n\t\t\t\t\t\t  form=document.createElement('form'); \n\t\t\t\t\t\t  form.method = 'POST'; \n\t\t\t\t\t\t  form.action = page;\n\t\t\t\t\t\t  form.name = 'jsform';\n\t\t\t\t\t\t  for (index in params)\n\t\t\t\t\t\t  {\n\t\t\t\t\t\t\t\tvar input = document.createElement('input');\n\t\t\t\t\t\t\t\tinput.type='hidden';\n\t\t\t\t\t\t\t\tinput.name=index;\n\t\t\t\t\t\t\t\tinput.id=index;\n\t\t\t\t\t\t\t\tinput.value=params[index];\n\t\t\t\t\t\t\t\tform.appendChild(input);\n\t\t\t\t\t\t  }\t  \t\t  \t\t\t  \n\t\t\t\t\t\t  body.appendChild(form);\n\t\t\t\t\t\t  form.submit();\n\t\t\t\t\t }\n\t\t\t\t\t \n\t\t\t\t\tfunction change_recarga(){\n\t\t\t\t\t\trecarga = 1;\n\t\t\t\t\t}\n\t\t\t\t\n\t\t\t\t\tfunction urlDetalles(u){\n\t\t\t\t\t\twin2 = window.open(u, 0, \"menubar=0,location=1,scrollbars,resizable,width=780,height=500\");\n\t\t\t\t\t\tcheckChildDetalles();\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\tfunction url(u, alu, cmidassign, page, courseid, nombre){\n\t\t\t\t\t\twin2 = window.open(u, nombre, \"menubar=0,location=0,scrollbars,resizable,width=780,height=500\");\n\t\t\t\t\t\tcheckChild(alu, cmidassign, page, courseid);\t\t\t\t\t\t\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\tfunction checkChild(alu, cmidassign, page, course) {\n\t\t\t\t\t\tif (win2.closed) {\n\t\t\t\t\t\t\t/*relocate(window.location, {'stu':alu, 'cma':cmidassign});\t*/\t\n\t\t\t\t\t\t\tdoWork('evalcomixtablegrade_'+alu+'_'+cmidassign, 'servidor.php?id=" . $this->courseid . "&eva=" . $USER->id . "', 'courseid='+course+'&page='+page+'&stu='+alu+'&cma='+cmidassign);\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse setTimeout(\"checkChild(\"+alu+\",\"+cmidassign+\")\",1);\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\t\n\t\t\t\t\t\n\t\t\t\t\tfunction checkChildDetalles() {\n\t\t\t\t\t\tif (win2.closed) {\n\t\t\t\t\t\t\tif (recarga == 1) {\n\t\t\t\t\t\t\t\twindow.location.reload(true);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\trecarga = 0;\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse {\n\t\t\t\t\t\t\tsetTimeout(\"checkChildDetalles()\",1);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tfunction checkChild_old(alu) {\t\t\t\t\t\t\n\t\t\t\t\t\tif (win2.closed) {\n\t\t\t\t\t\t\tvar url = String(window.location);\n\t\t\t\t\t\t\tvar long = url.length;\n\t\t\t\t\t\t\tif (url.substring(long-6, long-1) != \"&stu=\") {\n\t\t\t\t\t\t\t\tlocation.href = window.location + \"&stu=\"+alu;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\telse window.location.reload(true);\n\t\t\t\t\t\t\t\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse setTimeout(\"checkChild_old(\"+alu+\")\",1);\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t</script>\n\t\t\t\t\n  <script type='text/javascript' src='" . $CFG->wwwroot . "/blocks/evalcomix/javascript/jquery-1.4.2.js'></script>\n\t\t\t\t<script type='text/javascript'>//<![CDATA[ \n\t\t\t\t\tvar table = document.getElementById('user-grades');\n\t\t\t\t\t\n\t\t\t\t\tvar div11 = document.getElementById('div1');\n\t\t\t\t\tvar div22 = document.getElementById('div2');\n\t\t\t\t\tvar div = document.getElementById('div');\n\t\t\t\t\tvar documentwidth = \$(div).width();\n\t\t\t\t\t\n\t\t\t\t\tif((table.offsetWidth+100) < documentwidth){\n\t\t\t\t\t\tdiv11.style.width = (documentwidth - 50) +'px';\n\t\t\t\t\t\tdiv22.style.width = (documentwidth - 50)+'px';\n\t\t\t\t\t\t\$(div22).css('text-align','center');\n\t\t\t\t\t\t\$(table).css('margin','0 auto');\n\t\t\t\t\t}\n\t\t\t\t\telse{\n\t\t\t\t\t\tdiv11.style.width = (table.offsetWidth+100)+'px';\n\t\t\t\t\t\tdiv22.style.width = (table.offsetWidth+100)+'px';\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\t\$(window).load(function(){\n\t\t\t\t\t\$(function(){\n\t\t\t\t\t\t\$(\".wrapper1\").scroll(function(){\n\t\t\t\t\t\t\t\$(\".wrapper2\")\n\t\t\t\t\t\t\t\t.scrollLeft(\$(\".wrapper1\").scrollLeft());\n\t\t\t\t\t\t});\n\t\t\t\t\t\t\$(\".wrapper2\").scroll(function(){\n\t\t\t\t\t\t\t\$(\".wrapper1\")\n\t\t\t\t\t\t\t\t.scrollLeft(\$(\".wrapper2\").scrollLeft());\n\t\t\t\t\t\t});\n\t\t\t\t\t});\n\t\t\t\t\t});//]]>  \n\n\t\t\t\t\t</script>\n\n\t\t\t\t<noscript>\n\t\t\t\t\t<div style='color: #f00;'>" . get_string('alertjavascript', 'block_evalcomix') . "</div>\n\t\t\t\t</noscript>\n";
        $tools = $this->load_tools();
        $lang = current_language();
        //array $finalgrades with two dimensions [$taskinstance][$userid] that contains the finalgrades.
        $finalgrades = evalcomix_grades::get_grades($this->courseid);
        //$finalgrades = $this->get_grades('moodleevx');
        //print_r($this->activities_ids);
        $index = 0;
        if (isset($this->activities['id'])) {
            $num_activities = count($this->activities['id']);
        } else {
            $num_activities = 0;
        }
        //$configured = array();
        $tasks_array = array();
        $groupmodes = array();
        $type_instrument = array();
        $cm = array();
        $whoassesses = array();
        global $DB;
        for ($i = 0; $i < $num_activities; $i++) {
            $cmid = $this->activities['id'][$i];
            //$cm[$cmid] = $this->get_cm($cmid);
            if (!$this->canviewhidden && $this->cm[$cmid]->visible == 0) {
                continue;
            }
            //$configured[$cmid] = $this->configured_activity($cmid);
            $groupmodes[$cmid] = $this->cm[$cmid]->groupmode;
            //$type_instrument[$cmid] = evalcomix_tasks::get_type_task($cmid);
            $type_instrument[$cmid] = $this->activities['modulename'][$i];
            //$mode_time[$cmid]['teacher'] = $this->get_modes_time($cmid, 'teacher');
            if ($tasks_array[$cmid] = evalcomix_tasks::fetch(array('instanceid' => $cmid))) {
                $taskid = $tasks_array[$cmid]->id;
                $mode_teacher[$cmid] = evalcomix_modes::fetch(array('taskid' => $taskid, 'modality' => 'teacher'));
                $mode_time[$cmid]['self'] = $this->get_modestime($tasks_array[$cmid]->id, 'self');
                $mode_time[$cmid]['peer'] = $this->get_modestime($tasks_array[$cmid]->id, 'peer');
                if ($mode_peer[$cmid] = evalcomix_modes::fetch(array('taskid' => $taskid, 'modality' => 'peer'))) {
                    if ($mode_peer_extra = evalcomix_modes_extra::fetch(array('modeid' => $mode_peer[$cmid]->id))) {
                        $whoassesses[$cmid] = $mode_peer_extra->whoassesses;
                    }
                }
            }
            //$mode_time[$cmid]['self'] = $this->get_modes_time($cmid, 'self');
            //$mode_time[$cmid]['peer'] = $this->get_modes_time($cmid, 'peer');
        }
        //To know if the documents uploaded by the students must be shown
        $assessments = $this->load_assessments($tasks_array);
        $now = getdate();
        $now_timestamp = mktime($now["hours"], $now["minutes"], $now["seconds"], $now["mon"], $now["mday"], $now["year"]);
        foreach ($users as $user) {
            //$mode = $this->get_type_evaluation($user->id);
            $mode = grade_report_evalcomix::get_type_evaluation($user->id, $this->courseid);
            if ($mode == 'self' || $mode == 'peer') {
                $gid_loginuser = $this->get_groupids($USER->id);
                $gid_user = $this->get_groupids($user->id);
            }
            $background = 'background-color:#ffffff';
            if ($index % 2 == 0) {
                $background = 'background-color:#ededed';
            }
            $index++;
            $aux = array();
            $table .= '
					<tr style="border:1px solid #146C84;' . $background . '">
					<td style="width:35px;padding:3px;"><div class="userpic">' . $OUTPUT->user_picture($user) . '</div></td>
					<td style="border:1px solid #146C84;"><a href="' . $CFG->wwwroot . '/user/view.php?id=' . $user->id . '&course=' . $this->courseid . '">' . fullname($user) . '</a></td>';
            for ($i = 0; $i < $num_activities; $i++) {
                $allowedusers = array();
                $evaluate = '';
                $cmid = $this->activities['id'][$i];
                if (!$this->canviewhidden && $this->cm[$cmid]->visible == 0) {
                    continue;
                }
                //$configured = $this->configured_activity($this->activities['id'][$i]);
                //boolean, if there is not a grade it will not show the details link
                $showdetails = true;
                $table .= '<td style="border:1px solid #146C84;" title="' . htmlentities(fullname($user) . "\n" . $this->activities['name'][$i], ENT_QUOTES, "UTF-8") . '">';
                $table .= '<div id="evalcomixtablegrade_' . $user->id . '_' . $cmid . '">';
                //Only show the user´s grade or all grades if the USER is a teacher or admin
                if ($this->cm[$cmid]->visible == 0) {
                    $table .= '<span style="font-style:italic;color:#838383">Actividad Oculta</span>';
                } elseif ((has_capability('moodle/grade:viewhidden', $context, $USER->id) || $user->id == $USER->id) && isset($finalgrades[$this->activities['id'][$i]][$user->id]) && $finalgrades[$this->activities['id'][$i]][$user->id] > -3) {
                    if ($finalgrades[$cmid][$user->id] > -1) {
                        $table .= format_float($finalgrades[$cmid][$user->id], 2);
                        $aux[] = $finalgrades[$cmid][$user->id];
                    } else {
                        $table .= '-';
                    }
                    //if there is not anything to assess
                    if ($finalgrades[$cmid][$user->id] == -2) {
                        $showdetails = false;
                    }
                } else {
                    //there is not grade
                    //configured
                    //if($configured[$cmid]){
                    if ($this->activities_configured[$cmid]) {
                        $table .= '-';
                    } else {
                        $table .= '<span style="font-style:italic;color:#f54927">' . get_string('noconfigured', 'block_evalcomix') . '</span>';
                    }
                    $showdetails = false;
                }
                //Checks if $this->activities['id'] is configured in evalcomix
                //if ($configured[$cmid]){
                if ($this->activities_configured[$cmid]) {
                    if ($this->cm[$cmid]->visible == 0) {
                        $table .= '<div><span style="font-style:italic;color:#838383;font-weight:bold">Configurada</span></div>';
                        continue;
                    }
                    //if($tool = get_evalcomix_modality_tool($this->courseid, $tasks_array[$cmid]->id, $mode)){
                    $taskid = $tasks_array[$cmid]->id;
                    if (isset($tools[$taskid][$mode])) {
                        $tool = $tools[$taskid][$mode];
                        //$url_instrument = webservice_evalcomix_client::get_ws_assessment_form($tool->idtool, $this->courseid, $type_instrument[$cmid], $this->activities['id'][$i], $user->id, $USER->id, $mode, MOODLE_NAME, 'assess', $lang.'_utf8');
                        $url_instrument = 'assessment_form.php?id=' . $this->courseid . '&a=' . $cmid . '&t=' . $tool->idtool . '&s=' . $user->id . '&mode=assess';
                        $evaluate = '<input type="image" value="' . get_string('evaluate', 'block_evalcomix') . '" title="' . get_string('evaluate', 'block_evalcomix') . '" style="background-color:transparent;width:16px;" src="../images/evaluar.png" onclick="javascript:url(\'' . $url_instrument . '\',\'' . $user->id . '\',\'' . $this->activities['id'][$i] . '\',\'' . $this->page . '\',\'' . $this->courseid . '\');"/>';
                        //if($assessmentgrade = evalcomix_assessments::fetch(array('taskid'=>$tasks_array[$cmid]->id, 'assessorid'=>$USER->id, 'studentid'=>$user->id))){
                        $assessorid = $USER->id;
                        $studentid = $user->id;
                        if (isset($assessments[$taskid][$assessorid][$studentid])) {
                            $evaluate = '<input type="image" value="' . get_string('evaluate', 'block_evalcomix') . '" title="' . get_string('evaluate', 'block_evalcomix') . '" style="background-color:transparent;width:16px;" src="../images/evaluar2.png" onclick="javascript:url(\'' . $url_instrument . '\',\'' . $user->id . '\',\'' . $this->activities['id'][$i] . '\', \'' . $this->page . '\',\'' . $this->courseid . '\');"/>';
                        }
                        //}
                    }
                    if ($showdetails) {
                        $details = '<input  type="image" value="' . get_string('details', 'block_evalcomix') . '" style="width:16px;background-color:transparent;" title=' . get_string('details', 'block_evalcomix') . ' src="../images/lupa.png" onclick="javascript:urlDetalles(\'' . $CFG->wwwroot . '/blocks/evalcomix/assessment/details.php?cid=' . $context->id . '&itemid=' . $tasks_array[$cmid]->id . '&userid=' . $user->id . '&popup=1\');"/>';
                    } else {
                        $details = '';
                    }
                    //Show user´s documents
                    //echo $showdocuments;
                    if ($mode == 'teacher') {
                        $title = get_string('studentwork1', 'block_evalcomix') . get_string('studentwork2', 'block_evalcomix') . $this->activities['name'][$i];
                        $table .= ' <input type="image" value="' . $title . '" style="background-color:transparent;width:13px" title="' . $title . '" src="../images/task.png" 
							onclick="javascript:urlDetalles(\'' . $CFG->wwwroot . '/blocks/evalcomix/assessment/user_activity.php?id=' . $user->id . '&course=' . $this->courseid . '&mod=' . $cmid . '\');"/>';
                    }
                    //If the $USER isn´t a teacher or admin evaluate if it should show Evaluate and Details buttons
                    if ($mode == 'self' || $mode == 'peer') {
                        //echo $finalgrades[$this->activities['id'][$i]][$user->id];
                        //Obtains the groupmode of the activity
                        //$groupmode = $this->get_groupmode($this->activities['id'][$i]);
                        $groupmode = $groupmodes[$cmid];
                        $groupmembersonly = 0;
                        if (isset($this->cm[$cmid]->groupmembersonly)) {
                            $groupmembersonly = $this->cm[$cmid]->groupmembersonly;
                        }
                        $groupingid = $this->cm[$cmid]->groupingid;
                        $same_grouping = false;
                        $same_group = $this->same_group($USER->id, $user->id);
                        if ($groupingid != 0) {
                            $same_grouping = $this->same_grouping_by_users($USER->id, $user->id, $this->cm[$cmid]);
                        }
                        /*Groupmode == 1 -> Separated Groups */
                        $condition = true;
                        if (isset($whoassesses[$cmid])) {
                            switch ($whoassesses[$cmid]) {
                                case 0:
                                    $condition = true;
                                    break;
                                case 1:
                                    $condition = !$groupmembersonly && ($same_grouping && ($groupmode != 1 || $same_group) || !$groupingid && ($groupmode != 1 || $same_group)) || $groupmembersonly && (!$groupingid && ($groupmode != 1 && $gid_loginuser != -1 && $gid_user != -1 || $same_group) || $same_grouping && ($groupmode != 1 || $same_group));
                                    break;
                                case 2:
                                    if ($evalcomixallowedusers = evalcomix_allowedusers::fetch_all(array('cmid' => $cmid, 'assessorid' => $USER->id))) {
                                        foreach ($evalcomixallowedusers as $auser) {
                                            $indexuser = $auser->studentid;
                                            $allowedusers[$indexuser] = true;
                                        }
                                    }
                                    $userid = $user->id;
                                    $condition = false;
                                    if (isset($allowedusers[$userid])) {
                                        $condition = $allowedusers[$userid];
                                    } elseif ($USER->id == $user->id) {
                                        $condition = true;
                                    }
                            }
                        }
                        /*if((!$groupmembersonly && (
                        								($same_grouping && (
                        										($groupmode != 1 
                        										|| $same_group))
                        								) 
                        								|| 
                        								(!$groupingid && 
                        									(($groupmode != 1 
                        										|| $same_group)))
                        								)
                        							)
                        							|| ($groupmembersonly  && (
                        //								(!$this->cm[$cmid]->groupingid && $gid_loginuser != -1 && $gid_user != -1)
                        								(!$groupingid && 
                        									((($groupmode != 1 && $gid_loginuser != -1 && $gid_user != -1) 
                        										|| $same_group)))
                        								||
                        								($same_grouping && (
                        									($groupmode != 1 
                        										|| $same_group)))
                        								)
                        							)
                        							)*/
                        if ($condition) {
                            //($this->cm[$cmid]->groupmembersonly && $same_grouping && $groupmode == 1) ||
                            //($groupmode == 1 && ($gid_loginuser == $gid_user || $this->same_grouping($gid_loginuser, $gid_user, $this->activities['id'][$i])))){
                            //$mode_time = $this->get_modes_time($this->activities['id'][$i], $mode);
                            if ($mode == 'self') {
                                //Details always are shown in selfassessment
                                $table .= $details;
                            }
                            if ($mode_time[$cmid][$mode] != false) {
                                $available = $mode_time[$cmid][$mode]->timeavailable;
                                $due = $mode_time[$cmid][$mode]->timedue;
                                //If the availability of the activity started
                                if ($now_timestamp >= $available) {
                                    $title = get_string('studentwork1', 'block_evalcomix') . get_string('studentwork2', 'block_evalcomix') . $this->activities['name'][$i];
                                    $table .= ' <input type="image" value="' . $title . '" style="background-color:transparent;width:13px" title="' . $title . '" src="../images/task.png" 
										onclick="javascript:urlDetalles(\'' . $CFG->wwwroot . '/blocks/evalcomix/assessment/user_activity.php?id=' . $user->id . '&course=' . $this->courseid . '&mod=' . $cmid . '\');"/>';
                                }
                                /*if($mode == 'self') { //Details always are shown in selfassessment
                                			$table .= $details;
                                		}*/
                                if ($now_timestamp >= $due && $mode == 'peer' && $showdetails == true) {
                                    $url_peer_instrument = webservice_evalcomix_client::get_ws_view_assessment($this->courseid, $type_instrument[$cmid], $this->activities['id'][$i], $USER->id, $user->id, 'peer', MOODLE_NAME);
                                    $table .= '<input type="image" value="' . get_string('details', 'block_evalcomix') . '" style="width:16px" title=' . get_string('details', 'block_evalcomix') . ' src="../images/lupa.png" onclick="javascript:urlDetalles(\'' . $url_peer_instrument . '\');"/>';
                                }
                                //Show the buttons if they must be availables
                                if ($now_timestamp >= $available && $now_timestamp < $due) {
                                    $table .= $evaluate;
                                }
                            }
                        }
                    } else {
                        //if $mode == 'teacher'
                        //$table .= $details.$evaluate;
                        $table .= $details;
                        if ($mode_teacher[$cmid] != null && $mode_teacher[$cmid]->modality == 'teacher') {
                            $table .= $evaluate;
                        }
                    }
                }
                $table .= '</div>';
            }
            $table .= '</td>';
            //	}
            //Calculates average
            if (count($aux) == 0) {
                $average = 0;
            } else {
                $average = $this->calculator->calculate_one_array($aux);
                $average = round($average, 2, PHP_ROUND_HALF_UP);
                unset($aux);
            }
            $table .= '<td style="border:1px solid #146C84;">' . $average . '</td></tr>';
        }
        $table .= '</table></div>';
        $table .= '</div>';
        return $table;
    }
Ejemplo n.º 3
0
    $PAGE->set_pagelayout('popup');
}
$PAGE->set_title(get_string('ratingsforitem', 'block_evalcomix'));
echo $OUTPUT->header();
include_once $CFG->dirroot . '/blocks/evalcomix/classes/evalcomix_assessments.php';
include_once $CFG->dirroot . '/blocks/evalcomix/classes/evalcomix_tasks.php';
include_once $CFG->dirroot . '/blocks/evalcomix/classes/evalcomix_modes.php';
include_once $CFG->dirroot . '/blocks/evalcomix/classes/evalcomix_modes_time.php';
include_once $CFG->dirroot . '/blocks/evalcomix/classes/evalcomix_modes_extra.php';
include_once $CFG->dirroot . '/blocks/evalcomix/classes/webservice_evalcomix_client.php';
include_once $CFG->dirroot . '/blocks/evalcomix/classes/evalcomix_grades.php';
include_once $CFG->dirroot . '/blocks/evalcomix/javascript/popup.php';
global $DB, $USER;
//GETTING DATAS----------------------------------------------------------------------------------------
require_capability('block/evalcomix:view', $context, $userid);
$user = $DB->get_record('user', array('id' => $userid), '*');
if (!($task = evalcomix_tasks::fetch(array('id' => $itemid)))) {
    print_error('Wrong parameters');
}
if ($modeEI = evalcomix_modes::fetch(array('taskid' => $task->id, 'modality' => 'peer'))) {
    $modeEItime = evalcomix_modes_time::fetch(array('modeid' => $modeEI->id));
    $modesExtra = evalcomix_modes_extra::fetch(array('modeid' => $modeEI->id));
} else {
    $modeEItime = '';
}
$cm = $DB->get_record('course_modules', array('id' => $task->instanceid), '*');
$module = $DB->get_record('modules', array('id' => $cm->module), '*');
$maxgrade = $task->maxgrade;
$data_activity = $DB->get_record($module->name, array('id' => $cm->instance));
// If a teacher has done click on Delete button
//if($assid && has_capability('block/evalcomix:edit', $context)){
 public function after_restore()
 {
     global $DB, $COURSE, $CFG;
     include_once $CFG->dirroot . '/blocks/evalcomix/configeval.php';
     include_once $CFG->dirroot . '/blocks/evalcomix/classes/evalcomix_tasks.php';
     include_once $CFG->dirroot . '/blocks/evalcomix/classes/evalcomix_modes.php';
     include_once $CFG->dirroot . '/blocks/evalcomix/classes/evalcomix_modes_time.php';
     include_once $CFG->dirroot . '/blocks/evalcomix/classes/evalcomix_modes_extra.php';
     include_once $CFG->dirroot . '/blocks/evalcomix/classes/evalcomix_assessments.php';
     include_once $CFG->dirroot . '/blocks/evalcomix/classes/webservice_evalcomix_client.php';
     include_once $CFG->dirroot . '/blocks/evalcomix/classes/evalcomix_grades.php';
     include_once $CFG->dirroot . '/blocks/evalcomix/classes/evalcomix_allowedusers.php';
     $settings = $this->task->get_info()->root_settings;
     //$this->oldcontextid; busco en tabla mdl_context->instanceid es el courseid; con el courseid obtengo todos los cms antiguos y nuevos y voy copiando uno a uno
     //$this->basepath   = $CFG->dataroot . '/temp/backup/' . $controller->get_tempdir();
     //$blockid = $this->get_old_blockid();
     //$xml = simplexml_load_file($this->get_basepath() . '/course/blocks/evalcomix_'. $blockid .'/evalcomix.xml');
     $fullpath = $this->task->get_taskbasepath();
     // We MUST have one fullpath here, else, error
     if (empty($fullpath)) {
         throw new restore_step_exception('restore_structure_step_undefined_fullpath');
     }
     // Append the filename to the fullpath
     $fullpath = rtrim($fullpath, '/') . '/' . $this->filename;
     // And it MUST exist
     if (!file_exists($fullpath)) {
         // Shouldn't happen ever, but...
         throw new restore_step_exception('missing_moodle_backup_xml_file', $fullpath);
     }
     $xml = simplexml_load_file($fullpath);
     //echo $this->task->get_taskbasepath() . '/course/blocks/evalcomix_'. $blockid .'/evalcomix.xml';
     $evxid_old = (int) $xml->evalcomix['id'];
     $viewmode_old = (string) $xml->evalcomix->viewmode;
     if (isset($xml->evalcomix->environment->courseid)) {
         $courseid_old = $xml->evalcomix->environment->courseid;
     }
     if (isset($xml->evalcomix->environment->moodlename)) {
         $moodlename_old = $xml->evalcomix->environment->moodlename;
     }
     include_once $CFG->dirroot . '/blocks/evalcomix/configeval.php';
     //include_once($CFG->dirroot . '/backup/lib.php');
     //$courseid_new = $this->plan->get_courseid();
     $courseid_new = $this->get_courseid();
     $moodlename_new = MOODLE_NAME;
     $block_evalcomix = $DB->get_record('block_evalcomix', array('courseid' => $courseid_new));
     //$coursecontext = get_context_instance(CONTEXT_COURSE, $courseid_new);
     $coursecontext = context_course::instance($courseid_new);
     $tasksid = '';
     if (isset($xml->evalcomix->tasks[0])) {
         $assessmentids = array();
         foreach ($xml->evalcomix->tasks[0] as $task) {
             $task_id_old = (int) $task['id'];
             $task_instanceid_old = (int) $task->instanceid;
             $task_maxgrade_old = (string) $task->maxgrade;
             $task_weighing_old = (string) $task->weighing;
             $cm = $DB->get_record('block_evalcomix', array('courseid' => $courseid_new));
             $cm_mapping = $this->get_mapping('course_module', $task_instanceid_old);
             $newcmid = $cm_mapping->newitemid;
             $visibletask = '1';
             if (isset($task->visible)) {
                 $visibletask = (string) $task->visible;
             }
             if (!($task_fetch = evalcomix_tasks::fetch(array('instanceid' => $newcmid)))) {
                 $task_object = new evalcomix_tasks('', $newcmid, $task_maxgrade_old, $task_weighing_old, '', $visibletask);
                 $newtaskid = $task_object->insert();
                 $tasksid .= $task_instanceid_old . '-' . $newcmid . ',';
                 foreach ($task->modes[0] as $mode) {
                     $mode_id_old = (int) $mode['id'];
                     $mode_toolid_old = (string) $mode->toolid;
                     $mode_modality_old = (string) $mode->modality;
                     $mode_weighing_old = (string) $mode->weighing;
                     $tool_mapping = $this->get_mapping('evalcomix_tool', $mode_toolid_old);
                     if (!empty($tool_mapping) && ($newtoolid = $tool_mapping->newitemid)) {
                         if (!($mode_object = evalcomix_modes::fetch(array('taskid' => $newtaskid, 'toolid' => $newtoolid, 'modality' => $mode_modality_old)))) {
                             $mode_object = new evalcomix_modes('', $newtaskid, $newtoolid, $mode_modality_old, $mode_weighing_old);
                             $newmodeid = $mode_object->insert();
                             if (isset($mode->mode_time['id'])) {
                                 $mode_time_id_old = (string) $mode->mode_time['id'];
                                 $mode_time_timeavailable_old = (string) $mode->mode_time->timeavailable;
                                 $mode_time_timedue_old = (string) $mode->mode_time->timedue;
                                 if (!evalcomix_modes_time::fetch(array('modeid' => $newmodeid))) {
                                     $mode_time_object = new evalcomix_modes_time('', $newmodeid, $mode_time_timeavailable_old, $mode_time_timedue_old);
                                     $mode_time_object->insert();
                                 }
                             }
                             if (isset($mode->mode_extra)) {
                                 $mode_extra_id_old = (string) $mode->mode_extra['id'];
                                 $mode_extra_timeavailable_old = (string) $mode->mode_extra->anonymous;
                                 $mode_extra_visible = $mode->mode_extra->visible;
                                 $mode_extra_whoassesses = $mode->mode_extra->whoassesses;
                                 if (!($mode_extra_object = evalcomix_modes_extra::fetch(array('modeid' => $newmodeid)))) {
                                     $mode_extra_object = new evalcomix_modes_extra('', $newmodeid, $mode_extra_timeavailable_old, $mode_extra_visible, $mode_extra_whoassesses);
                                     $mode_extra_object->insert();
                                 }
                             }
                         }
                     }
                 }
             }
             if ($settings['users'] == 1) {
                 foreach ($task->assessments[0] as $assessment) {
                     $assessment_id_old = (string) $assessment['id'];
                     $assessment_assessorid_old = (string) $assessment->assessorid;
                     $assessment_studentid_old = (string) $assessment->studentid;
                     $assessment_grade_old = (string) $assessment->grade;
                     $assessor_user = $this->get_mapping('user', $assessment_assessorid_old);
                     $student_user = $this->get_mapping('user', $assessment_studentid_old);
                     if (!isset($assessor_user->newitemid) || !isset($student_user->newitemid)) {
                         continue;
                     }
                     if (!($assessment_object = evalcomix_assessments::fetch(array('taskid' => $newtaskid, 'assessorid' => $assessor_user->newitemid, 'studentid' => $student_user->newitemid)))) {
                         $assessment_object = new evalcomix_assessments('', $newtaskid, $assessor_user->newitemid, $student_user->newitemid, $assessment_grade_old);
                         $assessment_object->insert();
                     }
                     //$modulename = get_module_type ($courseid_new, $newcmid);
                     $modulename = evalcomix_tasks::get_type_task($newcmid);
                     $mode = '';
                     if ($student_user->newitemid == $assessor_user->newitemid) {
                         $mode = 'self';
                     } elseif (has_capability('moodle/grade:viewhidden', $coursecontext, $assessor_user->newitemid)) {
                         $mode = 'teacher';
                     } else {
                         $mode = 'peer';
                     }
                     //$courseid_old, $modulename, $task_instanceid_old, $assessment_studentid_old, $assessment_assessorid_old, $mode, $moodlename_old
                     $str = $courseid_old . '_' . $modulename . '_' . $task_instanceid_old . '_' . $assessment_studentid_old . '_' . $assessment_assessorid_old . '_' . $mode . '_' . $moodlename_old;
                     $assessmentid_old = md5($str);
                     $str = $courseid_new . '_' . $modulename . '_' . $newcmid . '_' . $student_user->newitemid . '_' . $assessor_user->newitemid . '_' . $mode . '_' . $moodlename_new;
                     //echo $str . ' -- ';
                     $assessmentid_new = md5($str);
                     $object = new stdClass();
                     $object->oldid = $assessmentid_old;
                     $object->newid = $assessmentid_new;
                     $assessmentids[] = $object;
                 }
             }
         }
     }
     if ($tasksid != '') {
         $tasksid = substr($tasksid, 0, -1);
     }
     if ($settings['users'] == 1) {
         if (isset($xml->evalcomix->grades[0])) {
             foreach ($xml->evalcomix->grades[0] as $grade) {
                 $cm_mapping = $this->get_mapping('course_module', $grade->cmid);
                 $newcmid = $cm_mapping->newitemid;
                 $student = $this->get_mapping('user', (int) $grade->userid);
                 if (!isset($student->newitemid)) {
                     continue;
                 }
                 $params = array('finalgrade' => (double) $grade->finalgrade, 'courseid' => $courseid_new, 'cmid' => $newcmid, 'userid' => $student->newitemid);
                 if (!($grade_object = evalcomix_grades::fetch(array('courseid' => $courseid_new, 'cmid' => $newcmid, 'userid' => $student->newitemid)))) {
                     $grade_object = new evalcomix_grades($params);
                     $newgradeid = $grade_object->insert();
                 }
             }
         }
         if (isset($xml->evalcomix->allowedusers[0])) {
             foreach ($xml->evalcomix->allowedusers[0] as $users) {
                 $cm_mapping = $this->get_mapping('course_module', $users->cmid);
                 $newcmid = $cm_mapping->newitemid;
                 $assessor = $this->get_mapping('user', (int) $users->assessorid);
                 $student = $this->get_mapping('user', (int) $users->studentid);
                 if (!isset($student->newitemid) || !isset($assessor->newitemid)) {
                     continue;
                 }
                 $params = array('assessorid' => (int) $assessor->newitemid, 'studentid' => $student->newitemid, 'cmid' => $newcmid);
                 if (!($allowedusers_object = evalcomix_allowedusers::fetch($params))) {
                     $allowedusers_object = new evalcomix_allowedusers($params);
                     $newid = $allowedusers_object->insert();
                 }
             }
         }
     }
     if (isset($xml->evalcomix->tools[0])) {
         $hashtools = array();
         foreach ($xml->evalcomix->tools[0] as $tool) {
             if ((string) $tool->type == 'tmp') {
                 continue;
             }
             $idtoolold = (string) $tool->idtool;
             $tool_mapping = $this->get_mapping('evalcomix_tool', (string) $tool['id']);
             $toolnew = evalcomix_tool::fetch(array('id' => $tool_mapping->newitemid));
             $idtoolnew = $toolnew->idtool;
             $object = new stdClass();
             $object->oldid = $idtoolold;
             $object->newid = $idtoolnew;
             $hashtools[] = $object;
         }
     }
     if (isset($hashtools) && isset($assessmentids)) {
         $result = webservice_evalcomix_client::duplicate_course($assessmentids, $hashtools);
     }
     //$newtools = webservice_evalcomix_client::get_ws_list_tool($courseid_new, $moodlename_new);
 }