Ejemplo n.º 1
0
 /**
  * Remove Turnitin class and assignment links from database
  * so that new classes and assignments will be created.
  *
  * @param type $eventdata
  * @return boolean
  */
 public static function course_reset(\core\event\course_reset_ended $event)
 {
     global $DB;
     $eventdata = $event->get_data();
     $courseid = (int) $eventdata['courseid'];
     $resetcourse = true;
     $resetassign = isset($eventdata['other']['reset_options']['reset_assign_submissions']) ? $eventdata['other']['reset_options']['reset_assign_submissions'] : 0;
     $resetforum = isset($eventdata['other']['reset_options']['reset_forum_all']) ? $eventdata['other']['reset_options']['reset_forum_all'] : 0;
     // Get the modules that support the Plagiarism plugin by whether they have a class file.
     $supportedmods = array();
     foreach (scandir(__DIR__ . '/classes/modules/') as $filename) {
         if (!in_array($filename, array(".", ".."))) {
             $filename_ar = explode('.', $filename);
             $classname_ar = explode('_', $filename_ar[0]);
             // $filename_ar[0] is class name.
             $supportedmods[] = $classname_ar[1];
             // $classname_ar[1] is module name.
         }
     }
     foreach ($supportedmods as $supportedmod) {
         $module = $DB->get_record('modules', array('name' => $supportedmod));
         // Get all the course modules that have Turnitin enabled
         $sql = "SELECT cm.id\n                    FROM {course_modules} cm\n                    RIGHT JOIN {plagiarism_turnitin_config} ptc ON cm.id = ptc.cm\n                    WHERE cm.module = :moduleid\n                    AND cm.course = :courseid\n                    AND ptc.name = 'turnitin_assignid'";
         $params = array('courseid' => $courseid, 'moduleid' => $module->id);
         $modules = $DB->get_records_sql($sql, $params);
         if (count($modules) > 0) {
             $reset = "reset" . $supportedmod;
             if (!empty(${$reset})) {
                 // Remove Plagiarism plugin submissions and assignment id from DB for this module.
                 foreach ($modules as $mod) {
                     $DB->delete_records('plagiarism_turnitin_files', array('cm' => $mod->id));
                     $DB->delete_records('plagiarism_turnitin_config', array('cm' => $mod->id, 'name' => 'turnitin_assignid'));
                 }
             } else {
                 $resetcourse = false;
             }
         }
     }
     // If all turnitin enabled modules for this course have been reset
     // then remove the Turnitin course id from the database
     if ($resetcourse) {
         $DB->delete_records('turnitintooltwo_courses', array('courseid' => $courseid, 'course_type' => 'PP'));
     }
     return true;
 }