/**
  * Build the content for this action
  *
  * @return void
  * @access public
  * @since 4/26/05
  */
 function buildContent()
 {
     $cm = Services::getService("CourseManagement");
     $defaultTextDomain = textdomain("polyphony");
     $actionRows = $this->getActionRows();
     $pageRows = new Container(new YLayout(), OTHER, 1);
     $harmoni = Harmoni::instance();
     $searchNumber = RequestContext::value('search_number');
     if (is_null($searchNumber)) {
         $searchNumber = "";
     }
     ob_start();
     /***************************************************************************************************
      * The following performs a wild-card search through LDAP and sucks the courses into the database. *
      ***************************************************************************************************/
     $self = $harmoni->request->quickURL();
     print "<h2>Search for course offerings by the following criteria:</h2>" . "";
     print "\n\t<form action='{$self}' method='post'>\n\t\t\t\n\t<div>";
     print "<table>";
     print "\n\t<tr><td>Number: </td><td><input type='text' name='search_number' value='" . $searchNumber . "'/></td></tr>";
     print "\n\t<tr><td>Term: </td><td><select name='search_term'>";
     print "\n\t<option value=''";
     print "selected='selected'";
     print ">Choose a term</option>";
     //@TODO this sorting is probably pretty slow--it's multiple queries per term.
     $numOfImproperOfferingTerms = -1;
     $terms = $cm->getTerms();
     while ($terms->hasNextTerm()) {
         $term = $terms->nextTerm();
         $schedule = $term->getSchedule();
         if ($schedule->hasNextScheduleItem()) {
             $item1 = $schedule->nextScheduleItem();
             $terms2[$item1->getStart()] = $term;
         } else {
             $terms2[$numOfImproperOfferingTerms] = $term;
             $numOfImproperOfferingTerms--;
         }
     }
     krsort($terms2);
     foreach ($terms2 as $term) {
         $id = $term->getId();
         print "\n\t<option value='" . $id->getIdString() . "'";
         /*if($searchTerm==$id->getIdString()){
         			print "selected='selected'";
         		}*/
         print ">" . $term->getDisplayName() . "</option>";
     }
     print "\n\t</select></td></tr>";
     print "\n</table>";
     print "\n\t<p><input type='submit' value='" . _("Search") . "'/></p>";
     print "\n\t</div></form>";
     //$actionRows =$this->getActionRows();
     $actionRows->add(new Block(ob_get_contents(), 2), "100%", null, CENTER, TOP);
     ob_end_clean();
     $searchTerm = RequestContext::value('search_term');
     if (is_null($searchTerm)) {
         $searchTerm = "";
     }
     /*
     if ($search_criteria = $harmoni->request->get('search_criteria')) {
     		//$typeParts = explode("::", @html_entity_decode($search_type, ENT_COMPAT, 'UTF-8'));
     
     
     $searchType = new HarmoniType("Agent & Group Search", "edu.middlebury.harmoni", "TokenSearch");
     		//$searchType = new HarmoniType("Agent & Group Search", "edu.middlebury.harmoni", "WildcardSearch");
     		$string=	"*".$search_criteria."*";
     		$agents =$agentManager->getAgentsBySearch($string, $searchType);
     		print "search: " . $search_criteria;
     
     
     while ($agents->hasNext()) {
     		$agent =$agents->next();
     		$id =$agent->getId();
     
     
     
     $harmoni->history->markReturnURL("polyphony/agents/edit_agent_details");
     print "\n<p align='center'><a href='".$harmoni->request->quickURL("agents","edit_agent_details", array("agentId"=>$id->getIdString()))."'>";
     		print "\n".$agent->getDisplayName()."</a>";
     		print "\n - <a href=\"Javascript:alert('"._("Id:").'\n\t'.addslashes($id->getIdString())."')\">Id</a></p>";
     		}
     		print "\n</div>";
     $actionRows->add(new Block(ob_get_contents(), STANDARD_BLOCK), "100%", null, LEFT, CENTER);
     		ob_end_clean();
     
     }
     */
     if ($searchNumber != "" || $searchTerm != "") {
         ob_start();
         $pageRows->add(new Heading("Canonical course search results", STANDARD_BLOCK), "100%", null, LEFT, CENTER);
         ob_start();
         print "<p><h2>Search Results</h2></p>";
         $searchType = new ClassTokenSearch();
         $string = "*" . $searchNumber . "*";
         $DNs = $searchType->getClassDNsBySearch($string);
         print "search: " . $searchNumber;
         /*
         
         			$dbHandler = Services::getService("DBHandler");
         			$query= new SelectQuery;
         			$query->addTable('cm_offer');
         			$query->addColumn('id');
         
         			if($searchNumber!=null){
         
         			$query->addWhere("number like '%".addslashes($searchNumber)."%'");
         			}
         			if($searchTerm!=null){
         
         			$query->addWhere("fk_cm_term='".addslashes($searchTerm)."'");
         			}
         
         
         			$res =$dbHandler->query($query);*/
         $sections = array();
         $cm = Services::getService("CourseManagement");
         $im = Services::getService("Id");
         foreach ($DNs as $idString) {
             if (substr($idString, strlen($idString) - 42, 42) != ",OU=Classes,OU=Groups,DC=middlebury,DC=edu") {
                 continue;
             }
             $len = 0;
             while ($idString[$len + 3] != "," && $len < strlen($idString)) {
                 $len++;
             }
             $name = substr($idString, 3, $len);
             /*
             if(!$term somthings $searchTerm){
             //continue
             }
             */
             //filter out semesters
             if (substr($name, strlen($name) - 4, 1) != "-") {
                 continue;
             }
             //filter out gym--actually, that's not fair, is it?
             //if(substr($name,0,4)=="phed"){
             //
             //	continue;
             //}
             $sections[] = suck_by_agentAction::_figureOut($name, $agentId = null);
         }
         $offerings = array();
         $termId = null;
         if ($searchTerm != "") {
             //$term = substr($name, strlen($name)-3,3);
             $idManager = Services::getService("Id");
             // $term =$cm->getTerm($idManager->getId($searchTerm));
             // $termId =$term->getId();
             $termId = $idManager->getId($searchTerm);
         }
         foreach ($sections as $section) {
             $offering = $section->getCourseOffering();
             $term2 = $offering->getTerm();
             $term2Id = $term2->getId();
             if (!is_null($termId) && !$termId->isEqual($term2Id)) {
                 continue;
             }
             $offeringId = $offering->getId();
             $offerings[$offeringId->getIdString()] = $offering;
         }
         /*
         
         			foreach($DNs as $DN){
         
         
         
         
         
         			$id =$im->getId($row['id']);
         			$array[] =$cm->getCourseOffering($id);
         			}*/
         $iter = new HarmoniCourseOfferingIterator($offerings);
         edit_agent_detailsAction::printCourseOfferings($iter);
         $actionRows->add(new Block(ob_get_contents(), 2), "100%", null, CENTER, TOP);
         ob_end_clean();
     }
 }
 /**
  *Given a string, figure out if ets a term from the three letter name, creating it if necesary
  **/
 function _figureOut($ldapName, $agentId)
 {
     $term = suck_by_agentAction::_getTerm(substr($ldapName, strlen($ldapName) - 3, 3));
     $can = suck_by_agentAction::_getCanonicalCourse($ldapName);
     $offer = suck_by_agentAction::_getCourseOffering($can, $term, $ldapName);
     $section = suck_by_agentAction::_getCourseSection($offer, $ldapName);
     if ($agentId == null) {
         return $section;
     }
     $dbManager = Services::getService("DatabaseManager");
     $query = new SelectQuery();
     $query->addTable('cm_enroll');
     $query->addWhere("fk_cm_section='" . addslashes($section->_id->getIdString()) . "'");
     $query->addWhere("fk_student_id='" . addslashes($agentId->getIdString()) . "'");
     //I don't need Id, but I need to select something for the query to work
     $query->addColumn('id');
     //@TODO select count instead
     $res = $dbManager->query($query);
     if ($res->getNumberOfRows() == 0) {
         $section->addStudent($agentId, $p = new Type("EnrollmentStatusType", "edu.middlebury", "LDAP"));
     }
     return $section;
 }
 /**
  * shows the details of the agent's properties and gives menu of actions
  * 
  * @param object Agent $agent
  * @return void
  * @access public
  * @since 7/19/05
  */
 function viewAgentDetails($agent)
 {
     suck_by_agentAction::refreshAgentDetails($agent);
     $agentId = $agent->getId();
     $agentIdString = $agentId->getIdString();
     //display agent info
     print "\n<h3>" . $agent->getDisplayName() . "</h3>";
     print "\n<table><tr><td>";
     print "\n<table bgcolor='#AAAAAA' cellspacing='1' cellpadding='3'>";
     /*<tr bgcolor='#DDDDDD'>
     		<td>
     		Property
     		</td>
     		<td>
     		Value
     		</td>
     		</tr>";*/
     $propertiesArray = edit_agent_detailsAction::_getUsableProperties($agent);
     //show the uneditable list of properties and their types and values
     //foreach($propertiesArray as $key=>$property){
     //		print "<tr bgcolor='#FFFFFF'>
     //			<td>$key</td>
     //			<td>".$property['value']."</td>
     //			<td>".$property['type']."</td>
     //			</tr>";
     //
     //}
     edit_agent_detailsAction::_printRowFromPropertiesArray($propertiesArray, "name", "Name:");
     edit_agent_detailsAction::_printRowFromPropertiesArray($propertiesArray, "username", "Username:"******"email", "Email address:");
     edit_agent_detailsAction::_printRowFromPropertiesArray($propertiesArray, "department", "Department:");
     print "\n</table>";
     print "\n</td><td>";
     //actions menu
     $harmoni = Harmoni::instance();
     $url = $harmoni->request->mkURL();
     print "<ul>\n\t\t\t\t<li><a href='" . $url->write("furtherAction", "edit_agent_detailsAction::editAgent") . "'>Edit agent</a></li>\n\t\t\t\t<li><a href='";
     $harmoni->request->startNamespace("polyphony-authorizations");
     print $harmoni->request->quickURL("authorization", "edit_authorizations", array("agentId" => $agentId->getIdString()));
     $harmoni->request->endNamespace();
     print "'>Edit authorizations</a></li>\n\t\t\t\t<li><a href='" . $url->write("furtherAction", "edit_agent_detailsAction::confirmClearProperties") . "'>Clear properties</a></li>\n\t\t\t\t<li><a href='" . $url->write("furtherAction", "edit_agent_detailsAction::confirmDeleteAgent") . "'>Delete agent</a></li>\n\t\t\t\t<li><a href='" . $url->write("furtherAction", "edit_agent_detailsAction::advancedViewAgentDetails") . "'>Advanced View</a></li>\n\t\t\t\t</ul>";
     print "\n</td></tr></table>";
     print "<h2>Classes:</h2>";
     //sort the courses by term and the terms by date
     $cm = Services::getService("CourseManagement");
     $offerings = $cm->getCourseOfferings($agentId);
     edit_agent_detailsAction::printCourseOfferings($offerings);
 }