In C:\versiontest\moodle_194: "C:\Program Files\TortoiseCVS\cvs.exe" "-q" "diff" "-u" "-r" "1.283.2.62" "backup/restorelib.php"
CVSROOT=:pserver:anonymous@us.cvs.moodle.org:/cvsroot/moodle

cvs.exe diff: Empty password used - try 'cvs login' with a real password

Index: backup/restorelib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/backup/restorelib.php,v
retrieving revision 1.283.2.62
diff -u -r1.283.2.62 restorelib.php
--- backup/restorelib.php	24 Jan 2009 00:20:52 -0000	1.283.2.62
+++ backup/restorelib.php	9 Feb 2009 20:53:15 -0000
@@ -1878,6 +1878,16 @@
                                 // keep in sync with activity idnumber
                                 $dbrec->idnumber = $cm->idnumber;
 
+								/********MODIFICATION*******/
+								//add a data object to an array of grade items, items with an idnumber.
+								//these idnumbers don't match up, so we use the item's ID (ie.primary key in table)
+									$temp=new object();
+									$temp->idnumber=$dbrec->idnumber;
+									$temp->id=$idnumber;
+	
+									$hasID[$dbrec->idnumber]=$temp;
+								/********END*******/
+
                             } else {
                                 $idnumber = backup_todb($info['GRADE_ITEM']['#']['IDNUMBER']['0']['#'], false);
 
@@ -1885,6 +1895,16 @@
                                     //make sure the new idnumber is unique
                                     $dbrec->idnumber  = $idnumber;
                                 }
+
+								/********MODIFICATION*******/
+								//save manual grade_item information for later use.
+								if ($itemtype == "manual")
+								{
+									$dolater[]=$data;
+
+								}
+								/********END*******/
+
                             }
 
                             $dbrec->itemname        = backup_todb($info['GRADE_ITEM']['#']['ITEMNAME']['0']['#'], false);
@@ -1927,6 +1947,18 @@
 
                             $grade_item = new grade_item($dbrec, false);
                             $grade_item->insert('restore');
+							
+
+							/********MODIFICATION*******/
+							///save the newly created grade_item id so we can add it to calculation string
+							if(isset($hasID[$grade_item->idnumber])){
+								$temp=new object();
+								$temp->idnumber=$grade_item->idnumber;
+								$temp->id=$grade_item->id;
+								$hasID[$grade_item->idnumber]=$temp;
+							}
+							/********END*******/
+
                             if ($restoreall) {
                                 // set original parent if restored
                                 $oldcat = $info['GRADE_ITEM']['#']['CATEGORYID']['0']['#'];
@@ -2003,6 +2035,90 @@
                         }
                     }
                 }
+				/********MODIFICATION*******/
+
+											if(!empty($dolater))
+											{
+												foreach($dolater as $data)
+												{
+												// foreach calculated grade_item replace grade_items in calculation string 
+														 $info = $data->info;
+														 // first find out if category or normal item
+														$itemtype =  backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#'], false);
+																
+																//if grade item is manual.
+																if($itemtype=="manual")
+																{
+																	 $old_idnumber = $data->old_id;
+													
+																	 $new_idnumber = backup_getid($restore->backup_unique_code,"grade_items", $old_idnumber );
+																	
+																	//get an instance of grade item from DB to update later.
+																	// save as $up_grade_item
+																	if($new_idnumber->new_id)
+																	{
+																		//$temp_item is 
+																		$temp_item=get_record('grade_items','id', $new_idnumber->new_id);
+																		$up_grade_item = new grade_item(array('id'=>$temp_item->id, 'courseid'=>$temp_item->courseid));
+																		grade_item::set_properties($up_grade_item, $temp_item);
+
+																	}
+
+																	//now change calculation of $up_grade_item;
+																	
+																	//=sum(. .gi259. .,. .gi260. .,. .gi261. .)
+																	//probably unncessecary to change string.
+																	$temp_calc=$up_grade_item->calculation;
+
+																	$temp_calc = str_replace("##",". .",$temp_calc);
+																	$temp_array = explode(".",$temp_calc);
+																	$newArray=array();
+
+																	/**
+																		for each grade_item in calculation string, if it matches ##gi_ID_## then replace _ID_ with the new ID created for the respective grade_item during the restore
+																	**/
+																	///handle items of type ##gi___##
+																	foreach($temp_array as $gitem)
+																	{
+																			//if gitem matches ##gi___##
+																			//then replace old item id with newly restored id
+																			if (preg_match("/gi[0-9]*/i", $gitem)) 
+																			{
+																				$gitem_id =  str_replace("gi","",$gitem);
+
+																				$new_gitem_id = backup_getid($restore->backup_unique_code,"grade_items", $gitem_id);
+
+																				$gitem= preg_replace("/gi[0-9]*/i", "gi".$new_gitem_id->new_id, $gitem);
+
+																			}
+																		//save into array
+																		$newArray[]=$gitem;
+																	}
+																	$temp_calc = implode(".",$newArray);
+																	$temp_calc = str_replace(". .","##",$temp_calc);
+																	
+																	/**
+																		for each grade_item in calculation string, if it matches [[IDNUMBER]] then replace it with the new ID created for the respective grade_item during the restore in ##giID## form
+																	**/
+																	///handle items of type [[___]]
+																	foreach($hasID as $hitem)
+																	{
+																		//handle any items assigned [[idnumber]]
+																		//change to ##giID##
+																		//newly created id is saved in array earlier
+																		$temp_calc = str_replace("[[".$hitem->idnumber."]]","##gi".$hitem->id."##",$temp_calc);
+																	}
+																	//update calculated grade item.
+																	$up_grade_item->calculation = $temp_calc;
+																	$up_grade_item->update();
+
+																}
+												}
+
+											}
+										/*end of added changes*/
+							/********END*******/
+
             }
         }