function refresh_key()
 {
     // set up an RPC request
     $mnetrequest = new mnet_xmlrpc_client();
     // Use any method - listServices is pretty lightweight.
     $mnetrequest->set_method('system/listServices');
     // Do RPC call and store response
     if ($mnetrequest->send($this) === true) {
         // Ok - we actually don't care about the result
         $temp = new mnet_peer();
         $temp->set_id($this->id);
         if ($this->public_key != $temp->public_key) {
             $newkey = param_clean($temp->public_key, PARAM_PEM);
             if (!empty($newkey)) {
                 $this->public_key = $newkey;
                 return true;
             }
         }
     }
     return false;
 }
    /**
     * Processes the file to import the ALIS data
     *
     * Opens the file, loops through each row. Cleans the values in each column,
     * and inserts or updates the statistics for each subject, then loops over 
     * the records in the table and flags any quality issues.
     *
     * Returns a report of successess and failures.
     *
     * @see open_file()
     * @global object $DB Database interface
     * @return string A report of successes and failures.
     */
    public function process()
    {
        global $DB;
        $file = $this->open_file();
        $qualtype = false;
        $import->qualcount = 0;
        $import->subjectcount = 0;
        $import->updatecount = 0;
        while ($line = \fgetcsv($file, 0, '|')) {
            // If there's only one column on this line, then it's a qualification heading
            if (\count($line) == 1) {
                $qualname = param_clean($line[0], \PARAM_ALPHANUM);
                // Create a new qualtype record if there isn't one already.
                if (!($qualtype = $DB->get_record_select('report_targetgrades_qualtype', $DB->sql_compare_text('name') . ' = ?', array($qualname)))) {
                    if (!($qualscale = $DB->get_record('scale', array('name' => $qualname . ' MTG')))) {
                        if ($scale = get_scale($qualname)) {
                            $qualscale = new \stdClass();
                            $qualscale->name = $qualname . ' MTG';
                            $qualscale->scale = $scale;
                            $qualscale->description = $qualname . ' Minimum/Target Grades';
                            $qualscale->id = $DB->insert_record('scale', $qualscale);
                        }
                    }
                    if ($qualscale) {
                        $qualtype = new \stdClass();
                        $qualtype->name = $qualname;
                        $qualtype->scaleid = $qualscale->id;
                        $qualtype->id = $DB->insert_record('report_targetgrades_qualtype', $qualtype);
                        $import->qualcount++;
                    }
                }
            } else {
                // If we have a record for this course's qualtype
                if ($qualtype) {
                    $name = \clean_param($line[0], \PARAM_TEXT);
                    $samplesize = \clean_param(str_replace(',', '', $line[1]), \PARAM_INT);
                    $gradient = \clean_param($line[2], \PARAM_FLOAT);
                    $intercept = \clean_param($line[3], \PARAM_FLOAT);
                    $correlation = \clean_param($line[4], \PARAM_FLOAT);
                    $standarddeviation = \clean_param($line[5], \PARAM_FLOAT);
                    if ($subject = $DB->get_record_select('report_targetgrades_alisdata', $DB->sql_compare_text('name') . ' = ? AND qualtypeid = ?', array($name, $qualtype->id))) {
                        $subject->samplesize = $samplesize;
                        $subject->gradient = $gradient;
                        $subject->intercept = $intercept;
                        $subject->correlation = $correlation;
                        $subject->standarddeviation = $standarddeviation;
                        $DB->update_record('report_targetgrades_alisdata', $subject);
                        $import->updatecount++;
                    } else {
                        $subject = new \stdClass();
                        $subject->name = $name;
                        $subject->samplesize = $samplesize;
                        $subject->gradient = $gradient;
                        $subject->intercept = $intercept;
                        $subject->correlation = $correlation;
                        $subject->standarddeviation = $standarddeviation;
                        $subject->qualtypeid = $qualtype->id;
                        $DB->insert_record('report_targetgrades_alisdata', $subject);
                        $import->subjectcount++;
                    }
                }
            }
        }
        \fclose($file);
        // All the stats are now in the DB, so do a pass over the table to flag up any quality issues with the data
        $averagesize = round($DB->get_record_sql('SELECT AVG(samplesize) as avg FROM {report_targetgrades_alisdata}')->avg);
        $select = 'SELECT ta.*, tq.name as qualification ';
        $from = 'FROM {report_targetgrades_alisdata} ta 
	    	JOIN {report_targetgrades_qualtype} tq ON ta.qualtypeid = tq.id';
        $alisdata = $DB->get_records_sql($select . $from);
        foreach ($alisdata as $alis) {
            if ($alis->samplesize < $averagesize) {
                if ($alis->samplesize < $averagesize / 2) {
                    if ($alis->samplesize < $averagesize / 4) {
                        $alis->quality_samplesize = 3;
                    } else {
                        $alis->quality_samplesize = 2;
                    }
                } else {
                    $alis->quality_samplesize = 1;
                }
            } else {
                $alis->quality_samplesize = 0;
            }
            if ($alis->correlation < CORRELATION_THRESHOLD) {
                $alis->quality_correlation = 1;
            } else {
                $alis->quality_correlation = 0;
            }
            switch ($alis->qualification) {
                case ALIS_GCSE:
                case ALIS_BTEC_FIRST_DIPLOMA:
                case ALIS_IB_STANDARD:
                case ALIS_IB_HIGHER:
                case ALIS_OCR_NATIONAL_CERTIFICATE:
                case ALIS_OCR_NATIONAL_DIPLOMA:
                    $boundary = 1;
                    break;
                case ALIS_ADVANCED_GCE:
                case ALIS_ADVANCED_GCE_DOUBLE:
                    $boundary = 20;
                    break;
                case ALIS_ADVANCED_SUBSIDIARY_GCE:
                case ALIS_ADVANCED_SUBSIDIARY_GCE_DOUBLE:
                    $boundary = 10;
                    break;
                case ALIS_BTEC_NATIONAL_AWARD:
                case ALIS_BTEC_NATIONAL_DIPLOMA:
                case ALIS_BTEC_NATIONAL_CERTIFICATE:
                    $boundary = 40;
                    break;
                case ALIS_CACHE_L3_DIPLOMA:
                    $boundary = 60;
                    break;
            }
            if ($alis->standarddeviation > $boundary) {
                if ($alis->standarddeviation > $boundary * 2) {
                    $alis->quality_deviation = 2;
                } else {
                    $alis->quality_deviation = 1;
                }
            } else {
                $alis->quality_deviation = 0;
            }
            $DB->update_record('report_targetgrades_alisdata', $alis);
        }
        return $import;
    }