<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Plato - modules/healthcenters/controllers/healthcenters.ctrl.js</title>

  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="description" content="">
  <meta name="author" content="">

  <!--[if lt IE 9]>
  <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
  <![endif]-->

  <link href="../../assets/css/vendor/morris.css" rel="stylesheet">
  <link href="../../assets/css/vendor/bootstrap.css" rel="stylesheet">
  <link href="../../assets/css/vendor/font-awesome.css" rel="stylesheet">
  <link href="../../assets/css/vendor/codemirror.css" rel="stylesheet">
  <link href="../../assets/css/plato.css" rel="stylesheet">
  <link href="../../assets/css/plato-file.css" rel="stylesheet">

</head>

<body>

<div class="navbar navbar-fixed-top">
  <div class="container">
    <a class="navbar-brand" href="http://github.com/es-analysis/plato">Plato on Github</a>
    <ul class="nav navbar-nav">
      <li>
        <a href="../../index.html">Report Home</a>
      </li>
    </ul>
  </div>
</div>

<div class="jumbotron">
  <div class="container">
    <h1>modules/healthcenters/controllers/healthcenters.ctrl.js</h1>
  </div>
</div>

<div class="container aggregate-stats">
  <div class="row">
    <div class="col-md-6">
      <h2 class="header">Maintainability <a href="http://blogs.msdn.com/b/codeanalysis/archive/2007/11/20/maintainability-index-range-and-meaning.aspx"><i class="icon icon-info-sign" rel="popover" data-placement="top" data-trigger="hover" data-content="A value between 0 and 100 that represents the relative ease of maintaining the code. A high value means better maintainability." data-original-title="Maintainability Index"  data-container="body"></i></a></h2>
      <p class="stat">72.54</p>
    </div>
    <div class="col-md-6">
      <h2 class="header">Lines of code <i class="icon icon-info-sign" rel="popover" data-placement="top" data-trigger="hover" data-content="Source Lines of Code / Logical Lines of Code" data-original-title="SLOC/LSLOC" data-container="body"></i></h2>
      <p class="stat">286</p>
    </div>
  </div>
  <div class="row historical">
    <div class="col-md-6">
      <p id="chart_historical_maint" class="chart"></p>
    </div>
    <div class="col-md-6">
      <p id="chart_historical_sloc" class="chart"></p>
    </div>
  </div>
  <div class="row">
    <div class="col-md-6">
      <h2 class="header">Difficulty  <a href="http://en.wikipedia.org/wiki/Halstead_complexity_measures"><i class="icon icon-info-sign" rel="popover" data-placement="top" data-trigger="hover" data-content="The difficulty measure is related to the difficulty of the program to write or understand." data-original-title="Difficulty" data-container="body"></i></a></h2>
      <p class="stat">29.17</p>
    </div>
    <div class="col-md-6">
      <h2 class="header">Estimated Errors  <a href="http://en.wikipedia.org/wiki/Halstead_complexity_measures"><i class="icon icon-info-sign" rel="popover" data-placement="top" data-trigger="hover" data-content="Halstead's delivered bugs is an estimate for the number of errors in the implementation." data-original-title="Delivered Bugs" data-container="body"></i></a></h2>
      <p class="stat">2.11</p>
    </div>
  </div>
</div>

<div class="container charts">
  <div class="row">
    <h2 class="header">Function weight</h2>
  </div>
  <div class="row">
    <div class="col-md-6">
      <h3 class="chart-header">By Complexity <a href="http://en.wikipedia.org/wiki/Cyclomatic_complexity"><i class="icon icon-info-sign" rel="popover" data-placement="top" data-trigger="hover" data-content="This metric counts the number of distinct paths through a block of code. Lower values are better." data-original-title="Cyclomatic Complexity" data-container="body"></i></a></h3>
      <div id="fn-by-complexity" class="stat"></div>
    </div>
    <div class="col-md-6">
      <h3 class="chart-header">By SLOC  <i class="icon icon-info-sign" rel="popover" data-placement="top" data-trigger="hover" data-content="Source Lines of Code / Logical Lines of Code" data-original-title="SLOC/LSLOC" data-container="body"></i></h3>
      <div id="fn-by-sloc" class="stat"></div>
    </div>
  </div>
</div>

<div class="container">
  <div class="row">
    <textarea id="file-source" class="col-md-12">&#039;use strict&#039;;
angular.module(&#039;com.module.healthcenters&#039;)
  .controller(&#039;HealthcentersCtrl&#039;, function($scope, $state, $stateParams, CoreService,
    FormHelper, gettextCatalog, Healthcenter, HealthcentersService,$timeout, $rootScope,Bioportal,ProtocolApproval,LoopBackAuth) {


$scope.user=LoopBackAuth.currentUserData;
$scope.isadmin=$rootScope.isadmin;
$scope.autocompleteResults=[];
$scope.protocolApproval={};

  
var healthcenterId=$stateParams.id||&#039;&#039;;
if ( healthcenterId.length===24 ) {

// console.log(&quot;there is a healthcenter id&quot;);
Healthcenter.findById({
  id: healthcenterId
}, function(healthcenter) {
  // console.log(healthcenter);
  $scope.healthcenter = healthcenter;
  loadForm();
  // $scope.fetchApproval();
  // $scope.fetchHealthcenters();
  
}, function(err) {
  $state.go(&#039;app.healthcenters.list&#039;);
  console.log(err);
});


} else {

$scope.healthcenter = {};
loadForm();
}


//autocomplete fetch from bioportal
$scope.bioportalAutocomplete = function(search,field) {
  if(search.length&gt;2){
  Bioportal.autocomplete(search,field.templateOptions.bioportal)
  .then(function(res){
    var list=res.data.collection;
    if(list.length&lt;1) {
      list.push({
        cui:[-1,0],
        prefLabel:&#039;No Results&#039;
      });
    }    
    $scope.autocompleteResults=field.templateOptions.options=list.map(function(obj){

        var id=obj[&#039;cui&#039;][0]; 
        var link=(obj[&#039;id&#039;]||obj[&#039;@id&#039;]); 
        return { 
          &quot;label&quot;: obj.prefLabel, 
          &quot;id&quot;: id,
          &quot;link&quot;:link
        };
    });
  },function(err){console.log(err);});
  }
};


  function capFirst(str) { return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}); }
  var propname=function(str){
    return capFirst(str.split(CategorySplitter)[1]||&quot; &quot;);
  };  
  var catname=function(str){
    return capFirst(str.split(CategorySplitter)[0]||&quot; &quot;);
  };
  $scope.cName=catname;
  $scope.pName=propname;


    
/*LOGIC FUNCTIONS */    

    $scope.toggleHealthcenterApproval=function(){
      console.log($scope.protocolApproval);
      if(!$scope.protocolApproval.id){
          ProtocolApproval.create($scope.protocolApproval, function(res) {
          $scope.protocolApproval=res;
          updateDashboard();
          $scope.fetchApproval();
          CoreService.toastSuccess(gettextCatalog.getString(&#039;Healthcenter usage&#039;),
              gettextCatalog.getString(&#039;You are now using this healthcenter.&#039;));
              
          });
        } else {
          ProtocolApproval.deleteById({&quot;id&quot;:$scope.protocolApproval.id}, function(res) {
          updateDashboard();
          $scope.fetchApproval();
          CoreService.toastWarning(gettextCatalog.getString(&#039;Healthcenter usage stopped&#039;),
              gettextCatalog.getString(&#039;You have stopped using this healthcenter.&#039;));
          });   
  
        }
    }

    $scope.delete = function(id) {
      HealthcentersService.deleteHealthcenter(id, function() {
        updateDashboard();
        $state.go(&#039;app.healthcenters.list&#039;, {}, { reload: true });
      });
    };

    
    $scope.fetchApproval=function(protocolId){
      // var query={
      //       filter: {
      //         where:  {
      //           &quot;protocolId&quot;: protocolId,
      //           &quot;healthcenterId&quot;: $scope.healthcenter.id
      //         }
      //       }
      //     };
          
      // ProtocolApproval.find(query,function(usages) {
         
      //   // $scope.fetchUsers();
      //   if(usages.length&gt;0) {
      //       $scope.protocolApproval=usages[0];
      //   } else {
      //     $scope.protocolApproval={
      //       &quot;protocolId&quot;:protocolId,
      //       &quot;healthcenterId&quot;:$scope.healthcenter.id
      //     };
      //   }
      //   },function(err){  
      //   console.log(err);
      //   });
      
    };
    
    
    $scope.fetchUsers=function(){
       Healthcenter.usedBy({id:$scope.healthcenter.id},function(users) {
           if(users.length&gt;0) $scope.pusers=users;
           else $scope.pusers=[{
            &quot;firstName&quot;:&quot;Nobody&quot;
          }];
        },function(err){  
          console.log(err);
        });
    };
    
    $scope.fetchHealthcenters=function(){
       Healthcenter.healthcenters({id:$scope.healthcenter.id},function(centers) {
        // console.log(centers);
         if(centers.length&gt;0) $scope.pcenters=centers;
         else  $scope.pcenters=[{
            &quot;name&quot;:&quot;No Issuing bodies&quot;
          }];
          
        },function(err){  
          console.log(err);
        });
    };
    

    $scope.onSubmit = function() {
      if($stateParams.parentId){
          $scope.healthcenter.id=null;
          delete $scope.healthcenter[&#039;id&#039;];
          $scope.healthcenter.parentId=$stateParams.parentId;
          Healthcenter.create($scope.healthcenter, function() {
          CoreService.toastSuccess(gettextCatalog.getString(&#039;Healthcenter created&#039;),
            gettextCatalog.getString(&#039;Your healthcenter is safe with us!&#039;));
            updateDashboard();

          $state.go(&#039;app.healthcenters.list&#039;, {}, { reload: true });
          }, function(err) {
          console.log(err);
          });
      
      } else {
        if($scope.healthcenter.id){
        Healthcenter.upsert($scope.healthcenter, function() {
          CoreService.toastSuccess(gettextCatalog.getString(&#039;Healthcenter saved&#039;),
            gettextCatalog.getString(&#039;Your healthcenter is safe with us!&#039;));
            updateDashboard();

          $state.go(&#039;app.healthcenters.list&#039;, {}, { reload: true });
        }, function(err) {
          console.log(err);
        });
        } else {
          Healthcenter.create($scope.healthcenter, function() {
          CoreService.toastSuccess(gettextCatalog.getString(&#039;Healthcenter created&#039;),
            gettextCatalog.getString(&#039;Your healthcenter is safe with us!&#039;));
            updateDashboard();
          $state.go(&#039;app.healthcenters.list&#039;, {}, { reload: true });
          }, function(err) {
          console.log(err);
          });
        }
      }

    };
    
    function updateDashboard(){
      // Healthcenter.find(function(prs){
      //   $rootScope.updateDashboardBox(prs.length,&#039;allhealthcenters&#039;);
      //   $rootScope.updateDashboardBox($rootScope.countOwn(prs,$scope.user.id),&#039;ownhealthcenters&#039;);
      // });
      // ProtocolApproval.count({
      //   where:{
      //       &quot;protocolId&quot;:protocolId
      //     }
      // },function(num){
      //   $rootScope.updateDashboardBox(num.count,&#039;usedhealthcenters&#039;);
      // });
    };
    
    //init
    
    
  
    


function loadForm(){

$scope.tabs = 
[{
    title: &#039;General&#039;,
    active: true,
    form: {
        options: {},
        model: $scope.healthcenter,
        fields: [{
            key: &#039;general_name&#039;,
            type: &#039;input&#039;,
            templateOptions: {
                label: &#039;Issuing body name&#039;,
                placeholder: &#039;Issuing body name..&#039;,
                required: true
            }
        }, {
            key: &#039;general_desc&#039;,
            type: &#039;textarea&#039;,
            templateOptions: {
                label: &#039;Issuing body description&#039;,
                placeholder: &#039;Issuing body description..&#039;,
                required: false
            }
        }, {
            key: &#039;general_location&#039;,
            type: &#039;input&#039;,
            templateOptions: {
                label: &#039;Location&#039;,
                placeholder: &#039;e.g address,city,country, postal code..&#039;,
                required: false
            }
        }, {
            &quot;key&quot;: &quot;general_type&quot;,
            &quot;type&quot;: &quot;select&quot;,
            &quot;defaultValue&quot;: &quot;hospital&quot;,
            &quot;templateOptions&quot;: {
                &quot;label&quot;: &quot;Organization Type&quot;,
                &quot;options&quot;: [{
                    &quot;name&quot;: &quot;Hospital&quot;,
                    &quot;value&quot;: &quot;hospital&quot;
                }, {
                    &quot;name&quot;: &quot;Institution&quot;,
                    &quot;value&quot;: &quot;institution&quot;
                }, {
                    &quot;name&quot;: &quot;Authoritative institution&quot;,
                    &quot;value&quot;: &quot;authoritative_institution&quot;
                }]
            }
        }]
    }
}];


};






  });
  
     
   //SUPER SEARCH FILTERS!!!
   
  // $scope.availableSearchParams=[];
  // var obj={};
  // for (var key in $scope.schemaProps) {
  //   obj=$scope.schemaProps[key];
  //   if(
  //     obj.type===&quot;string&quot; &amp;&amp;
  //     key!==&quot;release_url&quot; &amp;&amp;
  //     key!==&quot;release_version&quot; &amp;&amp;
  //     key!==&quot;release_date&quot; &amp;&amp;
  //     key!==&quot;evidence_url&quot; &amp;&amp;
  //     key!==&quot;evidence_date&quot;
  //   ) {
  //     // console.log(key+&quot; is inserted to filters&quot;);
  //     $scope.availableSearchParams.push({&quot;key&quot;:key,&quot;name&quot;:catname(key)+&quot; &quot;+propname(key), &quot;placeholder&quot;: propname(key)+&quot;...&quot;});
  //   }
  //   }

  //   // console.log(JSON.stringify($scope.schema));
  //   // console.log(JSON.stringify($scope.form));
  //   console.log($scope.schema);
  //   console.log($scope.form);
  // });</textarea>
  </div>
</div>

<footer class="footer">
  <div class="container">
    <p>.</p>
  </div>
</footer>

<script type="text/html" id="complexity-popover-template">
  <div class="complexity-notice">
    Complexity : {{ complexity.cyclomatic }} <br>
    Length : {{ complexity.halstead.length }} <br>
    Difficulty : {{ complexity.halstead.difficulty.toFixed(2) }} <br>
    Est # bugs : {{ complexity.halstead.bugs.toFixed(2) }}<br>
  </div>
</script>

<script type="text/javascript" src="../../assets/scripts/bundles/core-bundle.js"></script>
<script type="text/javascript" src="../../assets/scripts/bundles/codemirror.js"></script>
<script type="text/javascript" src="../../assets/scripts/codemirror.markpopovertext.js"></script>
<script type="text/javascript" src="report.js"></script>
<script type="text/javascript" src="report.history.js"></script>
<script type="text/javascript" src="../../assets/scripts/plato-file.js"></script>
</body>
</html>
