MDL-16651 - add in delete attempt capability - thanks Dan/Tim.
[moodle.git] / mod / scorm / report.php
index 0969406..02155de 100755 (executable)
@@ -10,6 +10,8 @@
     $b = optional_param('b', '', PARAM_INT);     // SCO ID
     $user = optional_param('user', '', PARAM_INT);  // User ID
     $attempt = optional_param('attempt', '1', PARAM_INT);  // attempt number
+    $action     = optional_param('action', '', PARAM_ALPHA);
+    $attemptids = optional_param('attemptid', array(), PARAM_RAW); //get array of responses to delete.
 
     if (!empty($id)) {
         if (! $cm = get_coursemodule_from_id('scorm', $id)) {
@@ -43,9 +45,9 @@
 
     require_login($course->id, false, $cm);
 
-    if (!has_capability('mod/scorm:viewreport', get_context_instance(CONTEXT_MODULE,$cm->id))) {
-        print_error('cannotcallscript');
-    }
+    $contextmodule = get_context_instance(CONTEXT_MODULE,$cm->id);
+
+    require_capability('mod/scorm:viewreport', $contextmodule);
 
     add_to_log($course->id, 'scorm', 'report', 'report.php?id='.$cm->id, $scorm->id, $cm->id);
 
         $strreport  = get_string('report', 'scorm');
         $strattempt  = get_string('attempt', 'scorm');
         $strname  = get_string('name');
-        
+
         if (empty($b)) {
             if (empty($a)) {
                 $navigation = build_navigation('', $cm);
                 print_header("$course->shortname: ".format_string($scorm->name), $course->fullname,$navigation,
                              '', '', true);
             } else {
-                
+
                 $navlinks = array();
-                $navlinks[] = array('name' => $strreport, 'link' => "report.php?id=$cm->id", 'type' => 'title');    
+                $navlinks[] = array('name' => $strreport, 'link' => "report.php?id=$cm->id", 'type' => 'title');
                 $navlinks[] = array('name' => "$strattempt $attempt - ".fullname($userdata), 'link' => '', 'type' => 'title');
                 $navigation = build_navigation($navlinks, $cm);
-                    
+
                 print_header("$course->shortname: ".format_string($scorm->name), $course->fullname,
                              $navigation, '', '', true);
             }
         } else {
 
             $navlinks = array();
-            $navlinks[] = array('name' => $strreport, 'link' => "report.php?id=$cm->id", 'type' => 'title');    
+            $navlinks[] = array('name' => $strreport, 'link' => "report.php?id=$cm->id", 'type' => 'title');
             $navlinks[] = array('name' => "$strattempt $attempt - ".fullname($userdata), 'link' => "report.php?a=$a&user=$user&attempt=$attempt", 'type' => 'title');
             $navlinks[] = array('name' => $sco->title, 'link' => '', 'type' => 'title');
             $navigation = build_navigation($navlinks, $cm);
-            
+
             print_header("$course->shortname: ".format_string($scorm->name), $course->fullname, $navigation,
                      '', '', true);
         }
         print_heading(format_string($scorm->name));
     }
 
+    if ($action == 'delete' && has_capability('mod/scorm:deleteresponses',$contextmodule)) {
+        if (scorm_delete_responses($attemptids, $scorm->id)) { //delete responses.
+            notify(get_string('scormresponsedeleted', 'scorm'), 'notifysuccess');
+        }
+    }
+
     $scormpixdir = $CFG->modpixpath.'/scorm/pix';
 
     if (empty($b)) {
         if (empty($a)) {
             // No options, show the global scorm report
-            
+
             if (!empty($CFG->enablegroupings) && !empty($cm->groupingid)) {
                 $sql = "SELECT st.userid, st.scormid
                         FROM {scorm_scoes_track} st
                             INNER JOIN {groups_members} gm ON st.userid = gm.userid
-                            INNER JOIN {groupings_groups} gg ON gm.groupid = gg.groupid 
+                            INNER JOIN {groupings_groups} gg ON gm.groupid = gg.groupid
                         WHERE st.scormid = ? AND gg.groupingid = ?
                         GROUP BY st.userid,st.scormid
                         ";
                 $params = array($scorm->id, $cm->groupingid);
             } else {
                 $sql = "SELECT st.userid, st.scormid
-                        FROM {scorm_scoes_track} st 
+                        FROM {scorm_scoes_track} st
                         WHERE st.scormid = ?
                         GROUP BY st.userid,st.scormid
                         ";
                 $params = array($scorm->id);
             }
-            
+
             if ($scousers=$DB->get_records_sql($sql, $params)) {
                 $table = new stdClass();
-                $table->head = array(' ', get_string('name'));
-                $table->align = array('center', 'left');
-                $table->wrap = array('nowrap', 'nowrap');
+                $table->head = array();
                 $table->width = '100%';
-                $table->size = array(10, '*');
+                if (has_capability('mod/scorm:deleteresponses',$contextmodule)) {
+                    $table->head[]  = ' ';
+                    $table->align[] = 'center';
+                    $table->wrap[]  = 'nowrap';
+                    $table->size[]  = '10';
+                }
+
+                $table->head[]  = ' ';
+                $table->align[] = 'center';
+                $table->wrap[]  = 'nowrap';
+                $table->size[]  = '10';
+
+                $table->head[]  = get_string('name');
+                $table->align[] = 'left';
+                $table->wrap[]  = 'nowrap';
+                $table->size[]  = '*';
 
                 $table->head[]= get_string('attempt','scorm');
                 $table->align[] = 'center';
 
                 foreach($scousers as $scouser){
                     $userdata = scorm_get_user_data($scouser->userid);
-                    $attempt = scorm_get_last_attempt($scorm->id,$scouser->userid);    
+                    $attempt = scorm_get_last_attempt($scorm->id,$scouser->userid);
                     for ($a = 1; $a<=$attempt; $a++) {
                         $row = array();
+                        if (has_capability('mod/scorm:deleteresponses',$contextmodule)) {
+                            $row[] = '<input type="checkbox" name="attemptid[]" value="'. $scouser->userid . ':' . $a . '" />';
+                        }
                         $row[] = print_user_picture($scouser->userid, $course->id, $userdata->picture, false, true);
                         $row[] = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$scouser->userid.'&amp;course='.$course->id.'">'.
                                  fullname($userdata).'</a>';
                         $row[] = '<a href="report.php?a='.$scorm->id.'&amp;user='.$scouser->userid.'&amp;attempt='.$a.'">'.$a.'</a>';
                         $select = 'scormid = ? and userid = ? and attempt = ?';
                         $params = array($scorm->id, $scouser->userid, $a);
-                        $timetracks = $DB->get_record_select('scorm_scoes_track', $select, $params, 'min(timemodified) as started, max(timemodified) as last');      
+                        $timetracks = $DB->get_record_select('scorm_scoes_track', $select, $params, 'min(timemodified) as started, max(timemodified) as last');
+                        // jump out here if this attempt doesnt exist
+                        if (!$timetracks->started) {
+                            continue;
+                        }
                         $row[] = userdate($timetracks->started, get_string('strftimedaydatetime'));
                         $row[] = userdate($timetracks->last, get_string('strftimedaydatetime'));
+
                         $row[] = scorm_grade_user_attempt($scorm, $scouser->userid, $a);
                         $table->data[] = $row;
                     }
                 }
+                echo '<div id="scormtablecontainer">';
+                if (has_capability('mod/scorm:deleteresponses',$contextmodule)) {
+                    echo '<form id="attemptsform" method="post" action="'.$_SERVER['PHP_SELF'].'" onsubmit="var menu = document.getElementById(\'menuaction\'); return (menu.options[menu.selectedIndex].value == \'delete\' ? \''.addslashes(get_string('deleteattemptcheck','quiz')).'\' : true);">';
+                    echo '<input type="hidden" name="id" value="'.$id.'">';
+                    print_table($table);
+                    echo '<a href="javascript:select_all_in(\'DIV\',null,\'scormtablecontainer\');">'.get_string('selectall', 'quiz').'</a> / ';
+                    echo '<a href="javascript:deselect_all_in(\'DIV\',null,\'scormtablecontainer\');">'.get_string('selectnone', 'quiz').'</a> ';
+                    echo '&nbsp;&nbsp;';
+                    $options = array('delete' => get_string('delete'));
+                    echo choose_from_menu($options, 'action', '', get_string('withselected', 'quiz'), 'if(this.selectedIndex > 0) submitFormById(\'attemptsform\');', '', true);
+                    echo '<noscript id="noscriptmenuaction" style="display: inline;">';
+                    echo '<div>';
+                    echo '<input type="submit" value="'.get_string('go').'" /></div></noscript>';
+                    echo '<script type="text/javascript">'."\n<!--\n".'document.getElementById("noscriptmenuaction").style.display = "none";'."\n-->\n".'</script>';
+                    echo '</form>';
+                } else {
+                    print_table($table);
+                }
+                echo '</div>';
+            } else {
+                notify(get_string('noactivity', 'scorm'));
             }
-            print_table($table);
         } else {
             if (!empty($user)) {
                 // User SCORM report
         if (!empty($userdata)) {
             print_simple_box_start('center');
             //print_heading(format_string($sco->title));
-            print_heading('<a href="'.$CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;mode=browse&amp;scoid='.$sco->id.'" target="_new">'.format_string($sco->title).'</a>'); 
+            print_heading('<a href="'.$CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;mode=browse&amp;scoid='.$sco->id.'" target="_new">'.format_string($sco->title).'</a>');
             echo '<div align="center">'."\n";
             print_user_picture($user, $course->id, $userdata->picture, false, false);
             echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user&amp;course=$course->id\">".
             $strstatus.'" />&nbsp;'.$trackdata->total_time.'<br />'.$scoreview.'<br />';
             echo '</div>'."\n";
             echo '<hr /><h2>'.get_string('details','scorm').'</h2>';
-            
+
             // Print general score data
             $table = new stdClass();
             $table->head = array(get_string('element','scorm'), get_string('value','scorm'));
             $table->wrap = array('nowrap', 'nowrap');
             $table->width = '100%';
             $table->size = array('*', '*');
-            
+
             $existelements = false;
             if ($scorm->version == 'SCORM_1.3') {
                 $elements = array('raw' => 'cmi.score.raw',
             if ($existelements) {
                 echo '<h3>'.get_string('general','scorm').'</h3>';
                 print_table($table);
-            }                
-            
+            }
+
             // Print Interactions data
             $table = new stdClass();
             $table->head = array(get_string('identifier','scorm'),
             $table->wrap = array('nowrap', 'nowrap', 'nowrap', 'nowrap');
             $table->width = '100%';
             $table->size = array('*', '*', '*', '*', '*');
-            
+
             $existinteraction = false;
-            
+
             $i = 0;
             $interactionid = 'cmi.interactions.'.$i.'.id';
-            
+
             while (isset($trackdata->$interactionid)) {
                 $existinteraction = true;
                 $printedelements[]=$interactionid;
                     }
                 }
                 $table->data[] = $row;
-                
+
                 $i++;
                 $interactionid = 'cmi.interactions.'.$i.'.id';
             }
                 echo '<h3>'.get_string('interactions','scorm').'</h3>';
                 print_table($table);
             }
-            
+
             // Print Objectives data
             $table = new stdClass();
             $table->head = array(get_string('identifier','scorm'),
             $table->wrap = array('nowrap', 'nowrap', 'nowrap', 'nowrap', 'nowrap');
             $table->width = '100%';
             $table->size = array('*', '*', '*', '*', '*');
-            
+
             $existobjective = false;
-            
+
             $i = 0;
             $objectiveid = 'cmi.objectives.'.$i.'.id';
-            
+
             while (isset($trackdata->$objectiveid)) {
                 $existobjective = true;
                 $printedelements[]=$objectiveid;
                     }
                 }
                 $table->data[] = $row;
-                
+
                 $i++;
                 $objectiveid = 'cmi.objectives.'.$i.'.id';
             }
             $table->wrap = array('nowrap', 'wrap');
             $table->width = '100%';
             $table->size = array('*', '*');
-            
+
             $existelements = false;
-            
+
             foreach($trackdata as $element => $value) {
-                if (substr($element,0,3) == 'cmi') { 
+                if (substr($element,0,3) == 'cmi') {
                     if (!(in_array ($element, $printedelements))) {
                         $existelements = true;
                         $row = array();
             if ($existelements) {
                 echo '<h3>'.get_string('othertracks','scorm').'</h3>';
                 print_table($table);
-            }                
+            }
             print_simple_box_end();
         } else {
             print_error('missingparameter');